Skip to content

Commit

Permalink
branch support
Browse files Browse the repository at this point in the history
  • Loading branch information
janfjohannes committed Sep 16, 2024
1 parent 0e0504d commit fb5a174
Show file tree
Hide file tree
Showing 23 changed files with 636 additions and 691 deletions.
95 changes: 95 additions & 0 deletions inlang/source-code/sdk2/src/lix-plugin/detectConflicts.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { test, expect } from "vitest";
import { inlangLixPluginV1 } from "./inlangLixPluginV1.js";

import {
newLixFile,
openLixInMemory,
Expand All @@ -11,6 +12,13 @@ import {
test("a create operation should not report a conflict given that the change does not exist in target", async () => {
const targetLix = await openLixInMemory({ blob: await newLixFile() });
const sourceLix = await openLixInMemory({ blob: await newLixFile() });

const currentBranch = await sourceLix.db
.selectFrom("branch")
.selectAll()
.where("active", "=", true)
.executeTakeFirstOrThrow();

const changes = await sourceLix.db
.insertInto("change")
.values([
Expand All @@ -26,6 +34,16 @@ test("a create operation should not report a conflict given that the change does
])
.returningAll()
.execute();

await sourceLix.db
.insertInto("branch_change")
.values({
branch_id: currentBranch.id,
change_id: "1",
seq: 1,
})
.execute();

const conflicts = await inlangLixPluginV1.detectConflicts!({
sourceLix,
targetLix,
Expand Down Expand Up @@ -90,6 +108,17 @@ test("it should report an UPDATE as a conflict if leaf changes are conflicting",
const targetLix = await openLixInMemory({ blob: await newLixFile() });
const sourceLix = await openLixInMemory({ blob: await targetLix.toBlob() });

const currentTargetBranch = await targetLix.db
.selectFrom("branch")
.selectAll()
.where("active", "=", true)
.executeTakeFirstOrThrow();
const currentSourceBranch = await sourceLix.db
.selectFrom("branch")
.selectAll()
.where("active", "=", true)
.executeTakeFirstOrThrow();

const commonChanges: NewChange[] = [
{
id: "12s",
Expand Down Expand Up @@ -137,11 +166,43 @@ test("it should report an UPDATE as a conflict if leaf changes are conflicting",
.values([...commonChanges, ...changesOnlyInSource])
.execute();

await sourceLix.db
.insertInto("branch_change")
.values([
{
branch_id: currentSourceBranch.id,
change_id: "12s",
seq: 1,
},
{
branch_id: currentSourceBranch.id,
change_id: "2qa",
seq: 2,
},
])
.execute();

await targetLix.db
.insertInto("change")
.values([...commonChanges, ...changesOnlyInTarget])
.execute();

await targetLix.db
.insertInto("branch_change")
.values([
{
branch_id: currentTargetBranch.id,
change_id: "12s",
seq: 1,
},
{
branch_id: currentTargetBranch.id,
change_id: "3sd",
seq: 2,
},
])
.execute();

const conflicts = await inlangLixPluginV1.detectConflicts!({
leafChangesOnlyInSource: changesOnlyInSource as Change[],
sourceLix: sourceLix,
Expand Down Expand Up @@ -223,6 +284,13 @@ test("it should NOT report an UPDATE as a conflict if the common ancestor is the

test("it should NOT report a DELETE as a conflict if the parent of the target and source are identical", async () => {
const targetLix = await openLixInMemory({ blob: await newLixFile() });

const currentTargetBranch = await targetLix.db
.selectFrom("branch")
.where("active", "=", true)
.selectAll()
.executeTakeFirstOrThrow();

await targetLix.db
.insertInto("change")
.values([
Expand All @@ -240,6 +308,15 @@ test("it should NOT report a DELETE as a conflict if the parent of the target an
])
.execute();

await targetLix.db
.insertInto("branch_change")
.values({
branch_id: currentTargetBranch.id,
change_id: "12s",
seq: 1,
})
.execute();

const sourceLix = await openLixInMemory({ blob: await targetLix.toBlob() });

const changesNotInTarget: NewChange[] = [
Expand Down Expand Up @@ -270,8 +347,26 @@ test("it should NOT report a DELETE as a conflict if the parent of the target an

await sourceLix.db.insertInto("change").values(changesNotInTarget).execute();

await sourceLix.db
.insertInto("branch_change")
.values({
branch_id: currentTargetBranch.id,
change_id: "3sd",
seq: 2,
})
.execute();

await targetLix.db.insertInto("change").values(changesNotInSource).execute();

await targetLix.db
.insertInto("branch_change")
.values({
branch_id: currentTargetBranch.id,
change_id: "2qa",
seq: 2,
})
.execute();

const conflicts = await inlangLixPluginV1.detectConflicts!({
sourceLix,
targetLix,
Expand Down
4 changes: 0 additions & 4 deletions inlang/source-code/sdk2/src/lix-plugin/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ test("it should update the variant to the source's value", async () => {
}),
// @ts-expect-error - https://github.com/opral/inlang-message-sdk/issues/123
meta: JSON.stringify(undefined),
commit_id: "c8ad005b-a834-4ca3-84fb-9627546f2eba",
},
{
id: "24f74fec-fc8a-4c68-b31c-d126417ce3af",
Expand All @@ -50,7 +49,6 @@ test("it should update the variant to the source's value", async () => {
}),
// @ts-expect-error - https://github.com/opral/inlang-message-sdk/issues/123
meta: JSON.stringify(undefined),
commit_id: "c8ad005b-a834-4ca3-84fb-9627546f2eba",
},
{
id: "aaf0ec32-0c7f-4d07-af8c-922ce382aef1",
Expand All @@ -73,7 +71,6 @@ test("it should update the variant to the source's value", async () => {
}),
// @ts-expect-error - https://github.com/opral/inlang-message-sdk/issues/123
meta: JSON.stringify(undefined),
commit_id: "c8ad005b-a834-4ca3-84fb-9627546f2eba",
},
];

Expand Down Expand Up @@ -102,7 +99,6 @@ test("it should update the variant to the source's value", async () => {
meta: JSON.stringify({
id: "6a860f96-0cf3-477c-80ad-7893d8fde852",
}),
commit_id: "df455c78-b5ed-4df0-9259-7bb694c9d755",
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
import { test, expect } from "vitest";
import { newProject } from "../project/newProject.js";
import { loadProjectInMemory } from "../project/loadProjectInMemory.js";
import {
isInSimulatedCurrentBranch,
resolveConflictBySelecting,
} from "@lix-js/sdk";
import { resolveConflictBySelecting } from "@lix-js/sdk";

test("it should resolve a conflict with the selected change", async () => {
const project = await loadProjectInMemory({ blob: await newProject() });
Expand Down Expand Up @@ -122,6 +119,48 @@ test("it should resolve a conflict with the selected change", async () => {
.returningAll()
.execute();

const currentBranch = await project.lix.db
.selectFrom("branch")
.select("id")
.where("active", "=", true)
.executeTakeFirstOrThrow();

await project.lix.db
.insertInto("branch_change")
.values([
{
id: "branch-change-1",
change_id: changes[0]?.id,
branch_id: currentBranch.id,
seq: 0,
},
{
id: "branch-change-2",
change_id: changes[1]?.id,
branch_id: currentBranch.id,
seq: 1,
},
{
id: "branch-change-3",
change_id: changes[2]?.id,
branch_id: currentBranch.id,
seq: 2,
},
{
id: "branch-change-4",
change_id: "samuels-change",
branch_id: currentBranch.id,
seq: 3,
},
{
id: "branch-change-5",
change_id: "peters-change",
branch_id: currentBranch.id,
seq: 4,
},
])
.execute();

const conflicts = await project.lix.db
.insertInto("conflict")
.values([
Expand All @@ -143,15 +182,18 @@ test("it should resolve a conflict with the selected change", async () => {
await project.lix.settled();

const changesInCurrentBranch = await project.lix.db
.selectFrom("change")
.selectFrom("branch_change")
.leftJoin("change", "change.id", "branch_change.change_id")
.selectAll()
.where(isInSimulatedCurrentBranch)
.where("branch_id", "=", currentBranch.id)
.orderBy("seq")
.execute();

expect(changesInCurrentBranch.map((c) => c.id)).toEqual([
changes[0]?.id,
changes[1]?.id,
changes[2]?.id,
"samuels-change",
"peters-change",
]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,4 @@ export class ResourceFileImportError extends Error {
this.cause = args.cause;
this.path = args.path;
}
}
}
Loading

0 comments on commit fb5a174

Please sign in to comment.