Skip to content

Commit

Permalink
Merge pull request #3170 from opral/lixdk-187-add-changeentity_id
Browse files Browse the repository at this point in the history
refactor: add entity id
  • Loading branch information
samuelstroschein authored Oct 16, 2024
2 parents 9d37b16 + 754eff8 commit e16b755
Show file tree
Hide file tree
Showing 21 changed files with 225 additions and 279 deletions.
91 changes: 35 additions & 56 deletions lix/packages/sdk/src/change-queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,28 @@ import { newLixFile } from "./newLix.js";
import type { DiffReport, LixPlugin } from "./plugin.js";

test("should use queue and settled correctly", async () => {
const mockPlugin: LixPlugin<{
text: { id: string; text: string };
}> = {
const mockPlugin: LixPlugin = {
key: "mock-plugin",
glob: "*",
diff: {
file: async ({ before, after }) => {
const dec = new TextDecoder();
// console.log("diff", after, before?.data, after?.data);
const textBefore = dec.decode(after?.data);
const textAfter = dec.decode(before?.data);
const textBefore = before
? new TextDecoder().decode(before?.data)
: undefined;
const textAfter = after
? new TextDecoder().decode(after.data)
: undefined;

if (textBefore === textAfter) {
return [];
}

return await mockPlugin.diff.text({
before: before
? {
id: "test",
text: textAfter,
}
: undefined,
after: after
? {
id: "test",
text: textBefore,
}
: undefined,
});
},
text: async ({ before, after }) => {
// console.log("text", before, after);
if (before?.text === after?.text) {
return [];
}

return [
!before
? {
type: "text",
before: undefined,
after: {
id: "test",
text: after?.text,
},
}
: {
type: "text",
before: {
id: "test",
text: before?.text,
},
after: {
id: "test",
text: after?.text,
},
},
{
type: "text",
entity_id: "test",
before: textBefore ? { text: textBefore } : undefined,
after: textAfter ? { text: textAfter } : undefined,
},
];
},
},
Expand Down Expand Up @@ -118,7 +82,8 @@ test("should use queue and settled correctly", async () => {
const changes = await lix.db
.selectFrom("change")
.innerJoin("snapshot", "snapshot.id", "change.snapshot_id")
.selectAll()
.selectAll('change')
.select('snapshot.value')
.execute();

expect(changes).toEqual([
Expand All @@ -128,11 +93,11 @@ test("should use queue and settled correctly", async () => {
created_at: changes[0]?.created_at,
snapshot_id: changes[0]?.snapshot_id,
parent_id: null,
entity_id: "test",
type: "text",
file_id: "test",
plugin_key: "mock-plugin",
value: {
id: "test",
text: "test",
},
meta: null,
Expand Down Expand Up @@ -187,7 +152,8 @@ test("should use queue and settled correctly", async () => {
const updatedChanges = await lix.db
.selectFrom("change")
.innerJoin("snapshot", "snapshot.id", "change.snapshot_id")
.selectAll()
.selectAll('change')
.select('snapshot.value')
.execute();

expect(updatedChanges).toEqual([
Expand All @@ -197,11 +163,11 @@ test("should use queue and settled correctly", async () => {
created_at: updatedChanges[0]?.created_at,
snapshot_id: updatedChanges[0]?.snapshot_id,
parent_id: null,
entity_id: "test",
type: "text",
file_id: "test",
plugin_key: "mock-plugin",
value: {
id: "test",
text: "test",
},
meta: null,
Expand All @@ -210,6 +176,7 @@ test("should use queue and settled correctly", async () => {
{
author: null,
commit_id: null,
entity_id: "test",
created_at: updatedChanges[1]?.created_at,
snapshot_id: updatedChanges[1]?.snapshot_id,
file_id: "test",
Expand All @@ -219,7 +186,6 @@ test("should use queue and settled correctly", async () => {
plugin_key: "mock-plugin",
type: "text",
value: {
id: "test",
text: "test updated text",
},
},
Expand All @@ -232,10 +198,10 @@ test("should use queue and settled correctly", async () => {
id: updatedChanges[2]?.id,
meta: null,
parent_id: updatedChanges[1]?.id,
entity_id: "test",
plugin_key: "mock-plugin",
type: "text",
value: {
id: "test",
text: "test updated text second update",
},
},
Expand All @@ -252,8 +218,21 @@ test("changes should contain the author", async () => {
file: vi.fn().mockResolvedValue([
{
type: "mock",
entity_id: "mock",
before: undefined,
after: {} as any,
after: {
text: "value1",
},
},
{
type: "mock",
entity_id: "mock",
before: {
text: "value1",
},
after: {
text: "value2",
},
},
] satisfies DiffReport[]),
},
Expand Down
135 changes: 46 additions & 89 deletions lix/packages/sdk/src/commit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,24 @@ test("should be able to add and commit changes", async () => {
key: "mock-plugin",
glob: "*",
diff: {
file: async ({ before: old }) => {
file: async ({ before, after }) => {
const textBefore = before
? new TextDecoder().decode(before?.data)
: undefined;
const textAfter = after
? new TextDecoder().decode(after.data)
: undefined;

if (textBefore === textAfter) {
return [];
}
return [
!old
? {
type: "text",
before: undefined,
after: {
id: "test",
text: "inserted text",
},
}
: {
type: "text",
before: {
id: "test",
text: "inserted text",
},
after: {
id: "test",
text: "updated text",
},
},
{
type: "text",
entity_id: "test",
before: textBefore ? { text: textBefore } : undefined,
after: textAfter ? { text: textAfter } : undefined,
},
];
},
},
Expand All @@ -53,39 +49,28 @@ test("should be able to add and commit changes", async () => {

await lix.db
.insertInto("file")
.values({ id: "test", path: "test.txt", data: enc.encode("test") })
.values({ id: "test", path: "test.txt", data: enc.encode("value1") })
.execute();

await lix.settled();

const changes = await lix.db
.selectFrom("change")
.innerJoin("snapshot", "snapshot.id", "change.snapshot_id")
.selectAll()
.innerJoin("snapshot as snapshot", "snapshot.id", "change.snapshot_id")
.selectAll('change')
.select('snapshot.value')
.execute();

// console.log(await lix.db.selectFrom("queue").selectAll().execute());

expect(changes).toEqual([
{
id: changes[0]?.id,
author: null,
created_at: changes[0]?.created_at,
snapshot_id: changes[0]?.snapshot_id,
expect.objectContaining({
parent_id: null,
type: "text",
file_id: "test",
plugin_key: "mock-plugin",
value: {
id: "test",
text: "inserted text",
text: "value1",
},
commit_id: null,
meta: null,
},
}),
]);

await lix.commit({ description: "test" });
await lix.commit({ description: "first commit" });

const secondRef = await lix.db
.selectFrom("ref")
Expand All @@ -99,26 +84,15 @@ test("should be able to add and commit changes", async () => {
const commitedChanges = await lix.db
.selectFrom("change")
.innerJoin("snapshot", "snapshot.id", "change.snapshot_id")
.selectAll()
.selectAll('change')
.select('snapshot.value')
.execute();

expect(commitedChanges).toEqual([
{
id: commitedChanges[0]?.id,
author: null,
created_at: changes[0]?.created_at,
snapshot_id: changes[0]?.snapshot_id,
parent_id: null,
type: "text",
file_id: "test",
plugin_key: "mock-plugin",
value: {
id: "test",
text: "inserted text",
},
meta: null,
expect.objectContaining({
...changes[0],
commit_id: commits[0]?.id!,
},
}),
]);

expect(commits).toEqual([
Expand All @@ -127,77 +101,60 @@ test("should be able to add and commit changes", async () => {
author: null,
created: commits[0]?.created!,
created_at: commits[0]?.created_at!,
description: "test",
description: "first commit",
parent_id: "00000000-0000-0000-0000-000000000000",
},
]);

await lix.db
.updateTable("file")
.set({ data: enc.encode("test updated text") })
.set({ data: enc.encode("value2") })
.where("id", "=", "test")
.execute();

await lix.settled();

const updatedChanges = await lix.db
const changesAfter = await lix.db
.selectFrom("change")
.innerJoin("snapshot", "snapshot.id", "change.snapshot_id")
.selectAll()
.selectAll('change')
.select('snapshot.value')
.execute();

expect(updatedChanges).toEqual([
{
id: updatedChanges[0]?.id!,
author: null,
created_at: updatedChanges[0]?.created_at,
snapshot_id: updatedChanges[0]?.snapshot_id,
expect(changesAfter).toEqual([
expect.objectContaining({
parent_id: null,
type: "text",
file_id: "test",
plugin_key: "mock-plugin",
value: {
id: "test",
text: "inserted text",
text: "value1",
},
meta: null,
commit_id: commits[0]?.id,
},
{
id: updatedChanges[1]?.id!,
author: null,
parent_id: updatedChanges[0]?.id!,
created_at: updatedChanges[1]?.created_at,
snapshot_id: updatedChanges[1]?.snapshot_id,
type: "text",
file_id: "test",
plugin_key: "mock-plugin",
}),
expect.objectContaining({
parent_id: changesAfter[0]?.id!,
value: {
id: "test",
text: "updated text",
text: "value2",
},
meta: null,
commit_id: null,
},
}),
]);

await lix.commit({ description: "test 2" });
await lix.commit({ description: "second commit" });
const newCommits = await lix.db.selectFrom("commit").selectAll().execute();
expect(newCommits).toEqual([
{
id: newCommits[0]?.id!,
author: null,
created: commits[0]?.created!,
created_at: newCommits[0]?.created_at!,
description: "test",
description: "first commit",
parent_id: "00000000-0000-0000-0000-000000000000",
},
{
id: newCommits[1]?.id!,
author: null,
created: commits[0]?.created!,
created_at: newCommits[1]?.created_at!,
description: "test 2",
description: "second commit",
parent_id: newCommits[0]?.id!,
},
]);
Expand Down
Loading

0 comments on commit e16b755

Please sign in to comment.