Skip to content

Commit

Permalink
refactor: use kysely recommended types
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelstroschein committed Aug 27, 2024
1 parent 0f16d67 commit 6c28408
Show file tree
Hide file tree
Showing 22 changed files with 565 additions and 353 deletions.
11 changes: 9 additions & 2 deletions inlang/source-code/sdk2/src/database/serializeJsonPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,15 @@ class ParseJsonTransformer extends OperationNodeTransformer {
}
}

function serializeJson(value: any): string {
if (typeof value === "object" || Array.isArray(value)) {
function serializeJson(value: any): any {
if (
// binary data
value instanceof ArrayBuffer ||
// uint8array, etc
ArrayBuffer.isView(value)
) {
return value;
} else if (typeof value === "object" || Array.isArray(value)) {
return JSON.stringify(value);
}
return value;
Expand Down
14 changes: 5 additions & 9 deletions inlang/source-code/sdk2/src/lix-plugin/applyChanges.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, expect } from "vitest";
import { loadProjectInMemory } from "../project/loadProjectInMemory.js";
import { newProject } from "../project/newProject.js";
import type { Change } from "@lix-js/sdk";
import type { Change, NewChange } from "@lix-js/sdk";
import type { Bundle } from "../schema/schemaV2.js";
import { applyChanges } from "./applyChanges.js";
import { loadDatabaseInMemory } from "sqlite-wasm-kysely";
Expand All @@ -12,7 +12,7 @@ test("it should be able to delete", async () => {
blob: await newProject(),
});

const changes: Change[] = [
const changes: NewChange[] = [
{
id: "1",
parent_id: undefined,
Expand All @@ -21,7 +21,6 @@ test("it should be able to delete", async () => {
plugin_key: "mock",
type: "bundle",
meta: { id: "mock" },
// @ts-expect-error - type error somewhere
value: {
id: "mock",
alias: {
Expand All @@ -39,7 +38,6 @@ test("it should be able to delete", async () => {
meta: {
id: "mock",
},
// @ts-expect-error - type error somewhere
value: {
id: "mock",
alias: {
Expand Down Expand Up @@ -80,7 +78,7 @@ test("it should be able to delete", async () => {
const dbFileAfter = await applyChanges({
lix: project.lix,
file: dbFile,
changes,
changes: changes as Change[],
});

const db = initDb({
Expand All @@ -97,7 +95,7 @@ test("it should be able to upsert (insert & update)", async () => {
blob: await newProject(),
});

const changes: Change[] = [
const changes: NewChange[] = [
{
id: "1",
parent_id: undefined,
Expand All @@ -106,7 +104,6 @@ test("it should be able to upsert (insert & update)", async () => {
plugin_key: "mock",
type: "bundle",
meta: { id: "mock" },
// @ts-expect-error - type error somewhere
value: {
id: "mock",
alias: {
Expand All @@ -124,7 +121,6 @@ test("it should be able to upsert (insert & update)", async () => {
meta: {
id: "mock",
},
// @ts-expect-error - type error somewhere
value: {
id: "mock",
alias: {
Expand Down Expand Up @@ -154,7 +150,7 @@ test("it should be able to upsert (insert & update)", async () => {
const dbFileAfter = await applyChanges({
lix: project.lix,
file: dbFile,
changes,
changes: changes as Change[],
});

const db = initDb({
Expand Down
106 changes: 62 additions & 44 deletions inlang/source-code/sdk2/src/lix-plugin/detectConflicts.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { test, expect } from "vitest";
import { inlangLixPluginV1 } from "./inlangLixPluginV1.js";
import { newLixFile, openLixInMemory, type Change } from "@lix-js/sdk";
import {
newLixFile,
openLixInMemory,
type Change,
type NewChange,
} from "@lix-js/sdk";

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 change: Change = {
id: "1",
parent_id: undefined,
operation: "create",
file_id: "mock",
plugin_key: "mock",
type: "mock",
// @ts-expect-error - type error in lix
value: JSON.stringify(["change 1"]),
};
await sourceLix.db.insertInto("change").values([change]).execute();
const changes = await sourceLix.db
.insertInto("change")
.values([
{
id: "1",
parent_id: undefined,
operation: "create",
file_id: "mock",
plugin_key: "mock",
type: "mock",
value: { id: "change 1" },
},
])
.returningAll()
.execute();
const conflicts = await inlangLixPluginV1.detectConflicts!({
sourceLix,
targetLix,
leafChangesOnlyInSource: [change],
leafChangesOnlyInSource: changes,
});
expect(conflicts).toHaveLength(0);
});
Expand All @@ -39,15 +48,16 @@ test.todo(
file_id: "mock",
plugin_key: "mock",
type: "mock",
// @ts-expect-error - type error in lix
value: JSON.stringify(["change 1"]),
value: {
id: "change 1",
},
},
])
.execute();

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

const changesNotInTarget: Change[] = [
const changesNotInTarget: NewChange[] = [
{
id: "2",
parent_id: "1",
Expand All @@ -67,7 +77,7 @@ test.todo(
const conflicts = await inlangLixPluginV1.detectConflicts!({
sourceLix,
targetLix,
leafChangesOnlyInSource: changesNotInTarget,
leafChangesOnlyInSource: changesNotInTarget as Change[],
});
expect(conflicts).toHaveLength(1);
expect(conflicts[0]?.change_id).toBe("1");
Expand All @@ -80,42 +90,45 @@ 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 commonChanges: Change[] = [
const commonChanges: NewChange[] = [
{
id: "12s",
parent_id: undefined,
operation: "create",
file_id: "mock",
plugin_key: "mock",
type: "mock",
// @ts-expect-error - type error in lix
value: JSON.stringify(["change 12s"]),
value: {
id: "change 12s",
},
},
];

const changesOnlyInTarget: Change[] = [
const changesOnlyInTarget: NewChange[] = [
{
id: "3sd",
parent_id: "12s",
operation: "update",
file_id: "mock",
plugin_key: "mock",
type: "mock",
// @ts-expect-error - type error in lix
value: JSON.stringify(["change 3sd"]),
value: {
id: "change 3sd",
},
},
];

const changesOnlyInSource: Change[] = [
const changesOnlyInSource: NewChange[] = [
{
id: "2qa",
parent_id: "12s",
operation: "update",
file_id: "mock",
plugin_key: "mock",
type: "mock",
// @ts-expect-error - type error in lix
value: JSON.stringify(["change 2qa"]),
value: {
id: "change 2qa",
},
},
];

Expand All @@ -130,7 +143,7 @@ test("it should report an UPDATE as a conflict if leaf changes are conflicting",
.execute();

const conflicts = await inlangLixPluginV1.detectConflicts!({
leafChangesOnlyInSource: changesOnlyInSource,
leafChangesOnlyInSource: changesOnlyInSource as Change[],
sourceLix: sourceLix,
targetLix: targetLix,
});
Expand All @@ -148,29 +161,31 @@ test("it should NOT report an UPDATE as a conflict if the common ancestor is the
const targetLix = await openLixInMemory({ blob: await newLixFile() });
const sourceLix = await openLixInMemory({ blob: await targetLix.toBlob() });

const commonChanges: Change[] = [
const commonChanges: NewChange[] = [
{
id: "12s",
parent_id: undefined,
operation: "create",
file_id: "mock",
plugin_key: "mock",
type: "mock",
// @ts-expect-error - type error in lix
value: JSON.stringify(["change 12s"]),
value: {
id: "change 12s",
},
},
];

const changesOnlyInTarget: Change[] = [
const changesOnlyInTarget: NewChange[] = [
{
id: "3sd",
parent_id: "12s",
operation: "update",
file_id: "mock",
plugin_key: "mock",
type: "mock",
// @ts-expect-error - type error in lix
value: JSON.stringify(["change 3sd"]),
value: {
id: "change 3sd",
},
},
{
id: "23a",
Expand All @@ -179,12 +194,13 @@ test("it should NOT report an UPDATE as a conflict if the common ancestor is the
file_id: "mock",
plugin_key: "mock",
type: "mock",
// @ts-expect-error - type error in lix
value: JSON.stringify(["change 23a"]),
value: {
id: "change 23a",
},
},
];

const changesOnlyInSource: Change[] = [];
const changesOnlyInSource: NewChange[] = [];

await sourceLix.db
.insertInto("change")
Expand All @@ -197,7 +213,7 @@ test("it should NOT report an UPDATE as a conflict if the common ancestor is the
.execute();

const conflicts = await inlangLixPluginV1.detectConflicts!({
leafChangesOnlyInSource: changesOnlyInSource,
leafChangesOnlyInSource: changesOnlyInSource as Change[],
sourceLix: sourceLix,
targetLix: targetLix,
});
Expand All @@ -217,15 +233,16 @@ test("it should NOT report a DELETE as a conflict if the parent of the target an
file_id: "mock",
plugin_key: "mock",
type: "mock",
// @ts-expect-error - type error in lix
value: JSON.stringify(["change 12s"]),
value: {
id: "change 12s",
},
},
])
.execute();

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

const changesNotInTarget: Change[] = [
const changesNotInTarget: NewChange[] = [
{
id: "3sd",
parent_id: "12s",
Expand All @@ -237,16 +254,17 @@ test("it should NOT report a DELETE as a conflict if the parent of the target an
},
];

const changesNotInSource: Change[] = [
const changesNotInSource: NewChange[] = [
{
id: "2qa",
parent_id: "12s",
operation: "update",
file_id: "mock",
plugin_key: "mock",
type: "mock",
// @ts-expect-error - type error in lix
value: JSON.stringify(["change 2qa"]),
value: {
id: "2qa",
},
},
];

Expand All @@ -257,7 +275,7 @@ test("it should NOT report a DELETE as a conflict if the parent of the target an
const conflicts = await inlangLixPluginV1.detectConflicts!({
sourceLix,
targetLix,
leafChangesOnlyInSource: changesNotInTarget,
leafChangesOnlyInSource: changesNotInTarget as Change[],
});

expect(conflicts).toHaveLength(1);
Expand Down
6 changes: 3 additions & 3 deletions inlang/source-code/sdk2/src/lix-plugin/detectConflicts.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
getLowestCommonAncestor,
getLeafChange,
type Conflict,
type LixPlugin,
type NewConflict,
} from "@lix-js/sdk";

export const detectConflicts: LixPlugin["detectConflicts"] = async ({
sourceLix,
targetLix,
leafChangesOnlyInSource,
}) => {
const result: Conflict[] = [];
const result: NewConflict[] = [];
for (const change of leafChangesOnlyInSource) {
const lowestCommonAncestor = await getLowestCommonAncestor({
sourceChange: change,
Expand All @@ -29,7 +29,7 @@ export const detectConflicts: LixPlugin["detectConflicts"] = async ({
});

if (lowestCommonAncestor.id === leafChangeInTarget.id) {
// no conflict. the lowest common ancestor is
// no conflict. the lowest common ancestor is
// the leaf change in the target. aka, no changes
// in target have been made that could conflict with the source
continue;
Expand Down
Loading

0 comments on commit 6c28408

Please sign in to comment.