Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: plugin.diff.file|type -> plugin.detectChanges #3175

3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"esbenp.prettier-vscode",
"inlang.vs-code-extension",
"vitest.explorer",
"aaron-bond.better-comments"
"aaron-bond.better-comments",
"yoavbls.pretty-ts-errors"
]
}
75 changes: 33 additions & 42 deletions lix/packages/sdk/src/change-queue.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
import { expect, test, vi } from "vitest";
import { openLixInMemory } from "./open/openLixInMemory.js";
import { newLixFile } from "./newLix.js";
import type { DiffReport, LixPlugin } from "./plugin.js";
import type { DetectedChange, LixPlugin } from "./plugin.js";

test("should use queue and settled correctly", async () => {
const mockPlugin: LixPlugin = {
key: "mock-plugin",
glob: "*",
diff: {
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 [
{
type: "text",
entity_id: "test",
before: textBefore ? { text: textBefore } : undefined,
after: textAfter ? { text: textAfter } : undefined,
},
];
},
detectChanges: 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 [
{
type: "text",
entity_id: "test",
snapshot: textAfter ? { text: textAfter } : undefined,
},
];
},
};

Expand Down Expand Up @@ -210,28 +207,22 @@ test("changes should contain the author", async () => {
const mockPlugin: LixPlugin = {
key: "mock-plugin",
glob: "*",
diff: {
file: vi.fn().mockResolvedValue([
{
type: "mock",
entity_id: "mock",
before: undefined,
after: {
text: "value1",
},
detectChanges: vi.fn().mockResolvedValue([
{
type: "mock",
entity_id: "mock",
snapshot: {
text: "value1",
},
{
type: "mock",
entity_id: "mock",
before: {
text: "value1",
},
after: {
text: "value2",
},
},
{
type: "mock",
entity_id: "mock",
snapshot: {
text: "value2",
},
] satisfies DiffReport[]),
},
},
] satisfies DetectedChange[]),
};
const lix = await openLixInMemory({
blob: await newLixFile(),
Expand Down
41 changes: 19 additions & 22 deletions lix/packages/sdk/src/commit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { expect, test } from "vitest";
import { openLixInMemory } from "./open/openLixInMemory.js";
import { newLixFile } from "./newLix.js";
Expand All @@ -9,27 +9,24 @@ test("should be able to add and commit changes", async () => {
const mockPlugin: LixPlugin = {
key: "mock-plugin",
glob: "*",
diff: {
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 [
{
type: "text",
entity_id: "test",
before: textBefore ? { text: textBefore } : undefined,
after: textAfter ? { text: textAfter } : undefined,
},
];
},
detectChanges: 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 [
{
type: "text",
entity_id: "test",
snapshot: textAfter ? { text: textAfter } : undefined,
},
];
},
};

Expand Down
36 changes: 10 additions & 26 deletions lix/packages/sdk/src/discussion/discussion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,16 @@ import type { LixPlugin } from "../plugin.js";
const mockPlugin: LixPlugin = {
key: "mock-plugin",
glob: "*",
diff: {
file: async ({ before }) => {
return [
!before
? {
type: "text",
before: undefined,
entity_id: "test",
after: {
text: "inserted text",
},
}
: {
type: "text",
entity_id: "test",
before: {
text: "inserted text",
},
after: {
text: "updated text",
},
},
];
},
detectChanges: async ({ after }) => {
return [
{
type: "text",
entity_id: "test",
snapshot: {
text: after ? new TextDecoder().decode(after.data) : undefined,
},
},
];
},
};

Expand All @@ -52,9 +38,7 @@ test("should be able to start a discussion on changes", async () => {

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

const discussion = await lix.createDiscussion({
Expand Down
Loading
Loading