import { describe, expect, test } from "vitest";
import { buildSchemaIndexSpecs } from "./schema-indexes.js";
describe("buildSchemaIndexSpecs", () => {
    test("creates primary key index", () => {
        const schema = {
            "x-lix-key": "bench_pk",
            "x-lix-version": "1.0",
            type: "object",
            additionalProperties: false,
            properties: {
                id: { type: "string" },
                code: { type: "string" },
            },
            required: ["id", "code"],
            "x-lix-primary-key": ["/code"],
        };
        const specs = buildSchemaIndexSpecs(schema);
        expect(specs).toEqual(expect.arrayContaining([
            expect.objectContaining({
                kind: "pk",
                unique: true,
                columns: [
                    "version_id",
                    "json_extract(snapshot_content, '$.code')",
                    "file_id",
                ],
            }),
        ]));
    });
    test("creates unique index for nested pointer", () => {
        const schema = {
            "x-lix-key": "bench_unique",
            "x-lix-version": "1.0",
            type: "object",
            additionalProperties: false,
            properties: {
                id: { type: "string" },
                details: {
                    type: "object",
                    additionalProperties: false,
                    properties: { slug: { type: "string" } },
                },
            },
            required: ["id", "details"],
            "x-lix-unique": [["/details/slug"]],
        };
        const specs = buildSchemaIndexSpecs(schema);
        expect(specs).toEqual(expect.arrayContaining([
            expect.objectContaining({
                kind: "unique",
                unique: true,
                columns: [
                    "version_id",
                    "json_extract(snapshot_content, '$.details.slug')",
                    "file_id",
                ],
            }),
        ]));
    });
    test("creates foreign key index for immediate mode", () => {
        const schema = {
            "x-lix-key": "bench_fk",
            "x-lix-version": "1.0",
            type: "object",
            additionalProperties: false,
            properties: {
                id: { type: "string" },
                target_id: { type: "string" },
            },
            required: ["id", "target_id"],
            "x-lix-foreign-keys": [
                {
                    properties: ["/target_id"],
                    references: { schemaKey: "bench_target", properties: ["/id"] },
                },
            ],
        };
        const specs = buildSchemaIndexSpecs(schema);
        expect(specs).toEqual(expect.arrayContaining([
            expect.objectContaining({
                kind: "foreign",
                columns: [
                    "version_id",
                    "inherited_from_version_id",
                    "json_extract(snapshot_content, '$.target_id')",
                    "file_id",
                ],
            }),
        ]));
    });
    test("foreign key respects custom scope", () => {
        const schema = {
            "x-lix-key": "bench_fk_scope",
            "x-lix-version": "1.0",
            type: "object",
            additionalProperties: false,
            properties: {
                id: { type: "string" },
                target_id: { type: "string" },
            },
            required: ["id", "target_id"],
            "x-lix-foreign-keys": [
                {
                    properties: ["/target_id"],
                    references: { schemaKey: "bench_target", properties: ["/id"] },
                    scope: ["version_id"],
                },
            ],
        };
        const specs = buildSchemaIndexSpecs(schema);
        expect(specs).toEqual(expect.arrayContaining([
            expect.objectContaining({
                kind: "foreign",
                columns: [
                    "version_id",
                    "inherited_from_version_id",
                    "json_extract(snapshot_content, '$.target_id')",
                    // file_id intentionally omitted because scope excludes it
                ],
            }),
        ]));
    });
});
//# sourceMappingURL=schema-indexes.test.js.map