import { expect, test } from "vitest";
import { mockJsonPlugin } from "./mock-json-plugin.js";
import { openLix } from "../lix/open-lix.js";
const createPluginFile = (data) => ({
    id: "mock",
    path: "/mock.json",
    data,
    metadata: {},
    directory_id: null,
    name: "mock",
    extension: "json",
    hidden: false,
});
test("it handles insert changes", async () => {
    const lix = await openLix({
        providePlugins: [mockJsonPlugin],
    });
    const before = new TextEncoder().encode(JSON.stringify({
        Name: "Anna",
        Age: 20,
    }));
    const after = new TextEncoder().encode(JSON.stringify({
        Name: "Anna",
        Age: 20,
        City: "Berlin",
    }));
    // Insert the initial file
    await lix.db
        .insertInto("file")
        .values({
        id: "mock",
        path: "/mock.json",
        data: before,
    })
        .execute();
    // Update the file with new data
    await lix.db
        .updateTable("file")
        .set({ data: after })
        .where("id", "=", "mock")
        .execute();
    const changes = await lix.db
        .selectFrom("change")
        .where("file_id", "=", "mock")
        .where("plugin_key", "=", mockJsonPlugin.key)
        .selectAll()
        .execute();
    const { fileData: applied } = mockJsonPlugin.applyChanges({
        file: createPluginFile(before),
        changes,
    });
    expect(new TextDecoder().decode(applied)).toEqual(new TextDecoder().decode(after));
});
test("it handles update changes", async () => {
    const lix = await openLix({
        providePlugins: [mockJsonPlugin],
    });
    const before = new TextEncoder().encode(JSON.stringify({
        Name: "Samuel",
        City: "Berlin",
    }));
    const after = new TextEncoder().encode(JSON.stringify({
        Name: "Samuel",
        City: "New York",
    }));
    // Insert the initial file
    await lix.db
        .insertInto("file")
        .values({
        id: "mock",
        path: "/mock.json",
        data: before,
    })
        .execute();
    // Update the file with new data
    await lix.db
        .updateTable("file")
        .set({ data: after })
        .where("id", "=", "mock")
        .execute();
    const changes = await lix.db
        .selectFrom("change")
        .where("file_id", "=", "mock")
        .where("plugin_key", "=", mockJsonPlugin.key)
        .selectAll()
        .execute();
    const { fileData: applied } = mockJsonPlugin.applyChanges({
        file: createPluginFile(before),
        changes,
    });
    expect(new TextDecoder().decode(applied)).toEqual(new TextDecoder().decode(after));
});
test("it handles delete changes", async () => {
    const lix = await openLix({
        providePlugins: [mockJsonPlugin],
    });
    const before = new TextEncoder().encode(JSON.stringify({
        Name: "Samuel",
        property_to_delete: "value",
    }));
    const after = new TextEncoder().encode(JSON.stringify({
        Name: "Samuel",
    }));
    // Insert the initial file
    await lix.db
        .insertInto("file")
        .values({
        id: "mock",
        path: "/mock.json",
        data: before,
    })
        .execute();
    // Update the file with new data
    await lix.db
        .updateTable("file")
        .set({ data: after })
        .where("id", "=", "mock")
        .execute();
    const changes = await lix.db
        .selectFrom("change")
        .where("file_id", "=", "mock")
        .where("plugin_key", "=", mockJsonPlugin.key)
        .selectAll()
        .execute();
    const { fileData: applied } = await mockJsonPlugin.applyChanges({
        file: createPluginFile(before),
        changes,
    });
    expect(new TextDecoder().decode(applied)).toEqual(new TextDecoder().decode(after));
});
test("it handles nested properties and arrays", async () => {
    const lix = await openLix({
        providePlugins: [mockJsonPlugin],
    });
    const before = new TextEncoder().encode(JSON.stringify({
        nested: {
            name: "Anna",
            list: ["a", "b", "c"],
        },
    }));
    const after = new TextEncoder().encode(JSON.stringify({
        nested: {
            name: "Peter",
            list: ["a", "b", "c", "d"],
        },
    }));
    // Insert the initial file
    await lix.db
        .insertInto("file")
        .values({
        id: "mock",
        path: "/mock.json",
        data: before,
    })
        .execute();
    // Update the file with new data
    await lix.db
        .updateTable("file")
        .set({ data: after })
        .where("id", "=", "mock")
        .execute();
    const changes = await lix.db
        .selectFrom("change")
        .where("file_id", "=", "mock")
        .where("plugin_key", "=", mockJsonPlugin.key)
        .selectAll()
        .execute();
    const { fileData: applied } = await mockJsonPlugin.applyChanges({
        file: createPluginFile(before),
        changes,
    });
    expect(new TextDecoder().decode(applied)).toEqual(new TextDecoder().decode(after));
});
//# sourceMappingURL=mock-json-plugin.test.js.map