import { expect, test } from "vitest";
import { openLix } from "../../lix/open-lix.js";
import { applyStateWithTombstonesView } from "./state-with-tombstones.js";
import { withWriterKey } from "../writer.js";
const testSchemaForDeleted = {
    "x-lix-key": "test_schema_for_deleted",
    "x-lix-version": "1.0",
    type: "object",
    additionalProperties: false,
    properties: {
        v: { type: "string" },
    },
    required: ["v"],
};
const testSchemaForDeletedWriter = {
    "x-lix-key": "test_schema_for_deleted_writer",
    "x-lix-version": "1.0",
    type: "object",
    additionalProperties: false,
    properties: {
        v: { type: "string" },
    },
    required: ["v"],
};
async function storeTestSchemas(lix) {
    await lix.db
        .insertInto("stored_schema")
        .values({ value: testSchemaForDeleted })
        .execute();
    await lix.db
        .insertInto("stored_schema")
        .values({ value: testSchemaForDeletedWriter })
        .execute();
}
test("state_with_tombstones exposes tracked deletions as tombstones", async () => {
    const lix = await openLix({
        keyValues: [
            {
                key: "lix_deterministic_mode",
                value: { enabled: true },
                lixcol_version_id: "global",
            },
        ],
    });
    // Create the view (temporary until wired into schema bootstrap)
    applyStateWithTombstonesView({ engine: lix.engine });
    await storeTestSchemas(lix);
    const active = await lix.db
        .selectFrom("active_version")
        .selectAll()
        .executeTakeFirstOrThrow();
    // Insert a tracked row into active version
    await lix.db
        .insertInto("state_by_version")
        .values({
        entity_id: "e_del",
        schema_key: "test_schema_for_deleted",
        file_id: "file_tombstone",
        version_id: active.version_id ?? active.id,
        plugin_key: "test_plugin",
        schema_version: "1.0",
        snapshot_content: { v: "live" },
    })
        .execute();
    // Delete to create a tracked tombstone
    await lix.db
        .deleteFrom("state_by_version")
        .where("entity_id", "=", "e_del")
        .where("schema_key", "=", "test_schema_for_deleted")
        .where("file_id", "=", "file_tombstone")
        .where("version_id", "=", active.version_id ?? active.id)
        .execute();
    // Default state_by_version should hide the deletion
    const hidden = await lix.db
        .selectFrom("state_by_version")
        .where("entity_id", "=", "e_del")
        .where("schema_key", "=", "test_schema_for_deleted")
        .where("file_id", "=", "file_tombstone")
        .selectAll()
        .execute();
    expect(hidden).toHaveLength(0);
    // state_with_tombstones should expose the tombstone (snapshot_content = null)
    const withDeleted = await lix.db
        .selectFrom("state_with_tombstones")
        .where("entity_id", "=", "e_del")
        .where("schema_key", "=", "test_schema_for_deleted")
        .where("file_id", "=", "file_tombstone")
        .selectAll()
        .execute();
    expect(withDeleted).toHaveLength(1);
    const row = withDeleted[0];
    expect(row.snapshot_content).toBeNull();
    expect(row.change_id).toBeTruthy();
    expect(row.commit_id).toBeTruthy();
});
test("state_with_tombstones keeps writer_key for tombstones (delete)", async () => {
    const lix = await openLix({
        keyValues: [
            {
                key: "lix_deterministic_mode",
                value: { enabled: true },
                lixcol_version_id: "global",
            },
        ],
    });
    await storeTestSchemas(lix);
    const active = await lix.db
        .selectFrom("active_version")
        .selectAll()
        .executeTakeFirstOrThrow();
    const vId = active.version_id ?? active.id;
    // Insert a tracked row into active version
    await lix.db
        .insertInto("state_by_version")
        .values({
        entity_id: "e_del_writer",
        schema_key: "test_schema_for_deleted_writer",
        file_id: "file_tombstone_writer",
        version_id: vId,
        plugin_key: "test_plugin",
        schema_version: "1.0",
        snapshot_content: { v: "live" },
    })
        .execute();
    // Delete with writer set via helper
    const WRITER = "spec:writer#delete";
    await withWriterKey(lix.db, WRITER, async (trx) => {
        await trx
            .deleteFrom("state_by_version")
            .where("entity_id", "=", "e_del_writer")
            .where("schema_key", "=", "test_schema_for_deleted_writer")
            .where("file_id", "=", "file_tombstone_writer")
            .where("version_id", "=", vId)
            .execute();
    });
    const rows = await lix.db
        .selectFrom("state_with_tombstones")
        .where("entity_id", "=", "e_del_writer")
        .where("schema_key", "=", "test_schema_for_deleted_writer")
        .where("file_id", "=", "file_tombstone_writer")
        .selectAll()
        .execute();
    expect(rows).toHaveLength(1);
    const row = rows[0];
    expect(row.snapshot_content).toBeNull();
    expect(row.writer_key).toBe(WRITER);
});
//# sourceMappingURL=state-with-tombstones.test.js.map