import { test, expect } from "vitest";
import { validateLixSchema, validateLixSchemaDefinition, } from "./validate-lix-schema.js";
test("validateLixSchemaDefinition passes for valid Lix schema", () => {
    const validSchema = {
        "x-lix-key": "test_entity",
        "x-lix-version": "1.0",
        type: "object",
        properties: {
            id: { type: "string" },
        },
        additionalProperties: false,
    };
    expect(() => validateLixSchemaDefinition(validSchema)).not.toThrow();
    expect(validateLixSchemaDefinition(validSchema)).toBe(true);
});
test("validateLixSchemaDefinition throws for invalid schema", () => {
    const invalidSchema = {
        "x-lix-version": "1.0",
        type: "object",
        properties: {
            id: { type: "string" },
        },
        additionalProperties: false,
        // Missing required x-lix-key
    };
    expect(() => validateLixSchemaDefinition(invalidSchema)).toThrow("Invalid Lix schema definition");
});
test("validateLixSchema validates both schema and data successfully", () => {
    const schema = {
        "x-lix-key": "user",
        "x-lix-version": "1.0",
        type: "object",
        properties: {
            id: { type: "string" },
            name: { type: "string" },
        },
        required: ["id", "name"],
        additionalProperties: false,
    };
    const validData = {
        id: "123",
        name: "John Doe",
    };
    expect(() => validateLixSchema(schema, validData)).not.toThrow();
    expect(validateLixSchema(schema, validData)).toBe(true);
});
test("validateLixSchema throws when schema is invalid", () => {
    const invalidSchema = {
        // Missing x-lix-key
        "x-lix-version": "1.0",
        type: "object",
        properties: {
            id: { type: "string" },
        },
        additionalProperties: false,
    };
    const data = { id: "123" };
    expect(() => validateLixSchema(invalidSchema, data)).toThrow("Invalid Lix schema definition");
});
test("validateLixSchema throws when data doesn't match schema", () => {
    const schema = {
        "x-lix-key": "user",
        "x-lix-version": "1.0",
        type: "object",
        properties: {
            id: { type: "string" },
            name: { type: "string" },
        },
        required: ["id", "name"],
        additionalProperties: false,
    };
    const invalidData = {
        id: "123",
        // Missing required 'name' field
    };
    expect(() => validateLixSchema(schema, invalidData)).toThrow("Data validation failed");
});
test("validateLixSchemaDefinition rejects when additionalProperties is missing", () => {
    const schemaMissingAdditionalProps = {
        "x-lix-key": "user",
        "x-lix-version": "1.0",
        type: "object",
        properties: {
            id: { type: "string" },
        },
        required: ["id"],
    };
    expect(() => validateLixSchemaDefinition(schemaMissingAdditionalProps)).toThrow("Invalid Lix schema definition");
});
test("additional properties must be false", () => {
    const schemaWithAdditionalProps = {
        "x-lix-key": "user",
        "x-lix-version": "1.0",
        type: "object",
        properties: {
            id: { type: "string" },
            name: { type: "string" },
        },
        required: ["id", "name"],
        additionalProperties: true, // This should cause validation to fail
    };
    const data = {
        id: "123",
        name: "John Doe",
        extraField: "not allowed", // Extra field not defined in schema
    };
    expect(() => validateLixSchemaDefinition(schemaWithAdditionalProps)).toThrow();
    const validSchema = {
        "x-lix-key": "user",
        "x-lix-version": "1.0",
        type: "object",
        properties: {
            id: { type: "string" },
            name: { type: "string" },
        },
        required: ["id", "name"],
        additionalProperties: false, // Correctly set to false
    };
    expect(() => validateLixSchemaDefinition(validSchema)).not.toThrow();
    expect(validateLixSchemaDefinition(validSchema)).toBe(true);
    expect(() => validateLixSchema(validSchema, data)).toThrow("Data validation failed");
});
test("validateLixSchemaDefinition rejects missing primary key properties", () => {
    const schema = {
        "x-lix-key": "missing_pk",
        "x-lix-version": "1.0",
        type: "object",
        properties: {
            value: { type: "string" },
        },
        required: ["value"],
        "x-lix-primary-key": ["/entity_id"],
        additionalProperties: false,
    };
    expect(() => validateLixSchemaDefinition(schema)).toThrow("x-lix-primary-key references missing property");
});
test("validateLixSchemaDefinition rejects missing unique constraint properties", () => {
    const schema = {
        "x-lix-key": "missing_unique",
        "x-lix-version": "1.0",
        type: "object",
        properties: {
            value: { type: "string" },
        },
        "x-lix-unique": [["/entity_id", "/value"]],
        additionalProperties: false,
    };
    expect(() => validateLixSchemaDefinition(schema)).toThrow("x-lix-unique references missing property");
});
//# sourceMappingURL=validate-lix-schema.test.js.map