import { test, assertType } from "vitest";
test("a json change schema should be infer the properties", () => {
    const jsonChangeSchema = {
        "x-lix-version": "1.0",
        "x-lix-key": "mock",
        type: "object",
        properties: {
            name: { type: "string" },
            age: { type: "number" },
            location: { type: "object" },
        },
        required: ["name", "age", "location"],
        additionalProperties: false,
    };
    const snapshot = {
        name: "John",
        age: 5,
        location: {
            city: "New York",
            country: "USA",
        },
    };
    assertType(snapshot);
});
test("FromLixSchemaDefinition preserves nested object property types", () => {
    const schema = {
        "x-lix-version": "1.0",
        "x-lix-key": "location_example",
        type: "object",
        properties: {
            id: { type: "string" },
            location: {
                type: "object",
                properties: {
                    city: { type: "string" },
                    country: { type: "string" },
                },
                required: ["city", "country"],
            },
            metadata: {
                type: "object",
            },
        },
        required: ["id", "location"],
        additionalProperties: false,
    };
    const validSnapshot = {
        id: "123",
        location: { city: "Berlin", country: "DE" },
        metadata: { extra: "value" },
    };
    assertType(validSnapshot);
    // @ts-expect-error - city must remain a string
    const invalidLocation = { city: 123, country: "DE" };
    void invalidLocation;
});
test("x-lix-override-lixcols typing", () => {
    const schema = {
        "x-lix-key": "defaults",
        "x-lix-version": "1.0",
        type: "object",
        properties: {
            id: { type: "string" },
        },
        required: ["id"],
        additionalProperties: false,
        "x-lix-entity-views": ["state"],
        "x-lix-override-lixcols": {
            lixcol_file_id: '"lix"',
            attempts: "3",
        },
    };
    assertType(schema["x-lix-override-lixcols"]);
    assertType(schema["x-lix-entity-views"]);
});
test("x-lix-default typing", () => {
    const schema = {
        "x-lix-key": "cel_default",
        "x-lix-version": "1.0",
        type: "object",
        properties: {
            id: {
                type: "string",
                "x-lix-default": "lix_uuid_v7()",
            },
        },
        required: ["id"],
        additionalProperties: false,
    };
    assertType(schema.properties.id?.["x-lix-default"]);
});
test("LixInsertable combined with LixGenerated makes columns optional", () => {
    // pass (all properties are provided)
    const z = {
        id: "123",
        name: "Test",
    };
    // pass (LixGenerated makes id optional)
    const a = {
        name: "Test",
    };
    // @ts-expect-error - Name is required in MockType
    const x = {};
    // @ts-expect-error - Name is required in MockType
    const y = {
        id: "123",
    };
});
test("LixSelectable unwraps LixGenerated fields to their base types", () => {
    // All fields should be present and LixGenerated should be unwrapped
    const selected = {
        id: "123",
        name: "Test",
        count: 42,
        metadata: { foo: "bar" },
    };
    // Type checking - all fields are required
    // @ts-expect-error - All fields are required in selectable
    const missing1 = {
        id: "123",
        name: "Test",
        count: 42,
        // missing metadata
    };
    // @ts-expect-error - All fields are required in selectable
    const missing2 = {
        // missing id
        name: "Test",
        count: 42,
        metadata: { foo: "bar" },
    };
    // @ts-expect-error - All fields are required in selectable
    const missing3 = {
        id: "123",
        // missing name
        count: 42,
        metadata: { foo: "bar" },
    };
    const idValue = "test";
    const countValue = 123;
});
test("LixUpdateable makes all fields optional and unwraps LixGenerated", () => {
    // All fields should be optional in updates
    const update1 = {}; // Empty object is valid
    const update2 = {
        id: "new-id",
    };
    const update3 = {
        name: "Updated Name",
    };
    const update4 = {
        count: 100,
    };
    const update5 = {
        metadata: { foo: "updated" },
    };
    const update6 = {
        optional: "now set",
    };
    // Can update multiple fields at once
    const update7 = {
        id: "new-id",
        name: "New Name",
        count: 99,
        metadata: { foo: "baz" },
        optional: "value",
    };
    // These should all work since fields are optional
    const idUpdate = undefined;
    const countUpdate = undefined;
    const nameUpdate = undefined;
    // But when provided, must be correct type
    const wrongType1 = {
        // @ts-expect-error - id must be string when provided
        id: 123,
    };
    const wrongType2 = {
        // @ts-expect-error - count must be number when provided
        count: "not a number",
    };
    const wrongType3 = {
        // @ts-expect-error - metadata must match structure when provided
        metadata: { bar: "wrong key" },
    };
});
test("FromLixSchemaDefinition transforms schema with x-lix-default to LixGenerated types", () => {
    // Define a test schema with x-lix-default properties
    const TestSchema = {
        "x-lix-key": "test_entity",
        "x-lix-version": "1.0",
        "x-lix-primary-key": ["/id"],
        type: "object",
        properties: {
            id: {
                type: "string",
                "x-lix-default": "lix_nano_id()",
            },
            name: {
                type: "string",
            },
        },
        required: ["id", "name"],
        additionalProperties: false,
    };
    // Test with LixInsertable - generated fields should be optional
    const insertable = {
        // id is optional
        name: "test",
    };
    // Can also provide generated fields
    const insertableWithGenerated = {
        id: "custom-id",
        name: "test",
    };
});
test("FromLixSchemaDefinition transforms empty object types to Record<string, any>", () => {
    const TestSchema = {
        "x-lix-key": "test_entity",
        "x-lix-version": "1.0",
        "x-lix-primary-key": ["/id"],
        "x-lix-immutable": true,
        type: "object",
        properties: {
            id: { type: "string" },
            payload: { type: "object" },
            config: { type: "object", nullable: true },
        },
        required: ["id", "payload"],
        additionalProperties: false,
    };
    // Test that payload is Record<string, any>
    const entity = {
        id: "test",
        payload: {
            author: "test-user",
            created_at: new Date().toISOString(),
            nested: { deep: { value: 123 } },
        },
        config: null,
    };
    // Verify types
    assertType(entity.payload);
    assertType(entity.config);
});
//# sourceMappingURL=definition.test-d.js.map