"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const zod_1 = tslib_1.__importDefault(require("zod"));
const McpSchemaCompatLayer_1 = require("./McpSchemaCompatLayer");
const toolFindFieldsArgs_1 = require("./tools/toolFindFieldsArgs");
const toolRunMetricQueryArgs_1 = require("./tools/toolRunMetricQueryArgs");
const mcpSchemaCompatLayer = new McpSchemaCompatLayer_1.McpSchemaCompatLayer();
const mapZodSchema = (schema) => mcpSchemaCompatLayer.processZodType(schema);
describe('McpSchemaCompatLayer', () => {
    describe('baseline', () => {
        const schema = zod_1.default.object({
            name: zod_1.default.string().describe('The name of the person'),
            age: zod_1.default.coerce.number().describe('The age of the person'),
            bio: zod_1.default.string().nullable().describe('The bio of the person'),
            details: zod_1.default
                .object({
                height: zod_1.default.number().describe('The height of the person'),
                weight: zod_1.default.coerce
                    .number()
                    .min(1)
                    .int()
                    .describe('The weight of the person')
                    .nullable(),
                hair: zod_1.default
                    .object({
                    color: zod_1.default
                        .string()
                        .nullable()
                        .describe('The color of the hair'),
                    length: zod_1.default
                        .string()
                        .nullable()
                        .describe('The length of the hair'),
                })
                    .nullable()
                    .describe('The hair of the person'),
            })
                .nullable()
                .describe('The details of the person'),
            pets: zod_1.default
                .array(zod_1.default
                .union([
                zod_1.default.object({
                    type: zod_1.default.literal('dog'),
                    name: zod_1.default
                        .string()
                        .describe('The name of the dog'),
                    breed: zod_1.default
                        .union([
                        zod_1.default
                            .literal('labrador')
                            .describe('the most popular dog breed'),
                        zod_1.default
                            .literal('golden retriever')
                            .describe('the second most popular dog breed'),
                    ])
                        .nullable()
                        .describe('The breed of the dog'),
                    barks: zod_1.default
                        .boolean()
                        .describe('Whether the dog barks')
                        .nullable(),
                }),
                zod_1.default.object({
                    type: zod_1.default.literal('cat'),
                    name: zod_1.default
                        .string()
                        .describe('The name of the cat'),
                    meows: zod_1.default
                        .boolean()
                        .describe('Whether the cat meows')
                        .nullable(),
                }),
            ])
                .describe('pet description'))
                .nullable()
                .describe('pets of the person'),
            preference: zod_1.default
                .enum(['dog person', 'cat person'])
                .nullable()
                .describe('The preference of the person'),
            occupation: zod_1.default
                .string()
                .default('plumber')
                .nullable()
                .describe('The occupation of the person'),
        });
        const mapped = mapZodSchema(schema);
        test('should handle top-level nullable types', () => {
            expect(mapped.parse({
                name: 'John',
                age: 30,
            })).toEqual({
                name: 'John',
                age: 30,
                bio: null,
                details: null,
                pets: null,
                preference: null,
                occupation: 'plumber',
            });
        });
        test('should handle nullable-object', () => {
            expect(mapped.parse({
                name: 'John',
                age: 30,
                bio: 'hello',
                details: {
                    height: 180,
                },
            })).toEqual({
                name: 'John',
                age: 30,
                bio: 'hello',
                details: {
                    height: 180,
                    weight: null,
                    hair: null,
                },
                pets: null,
                preference: null,
                occupation: 'plumber',
            });
            expect(mapped.parse({
                name: 'John',
                age: 30,
                bio: 'hello',
                details: {
                    height: 180,
                    weight: 70,
                    hair: {
                        color: 'brown',
                    },
                },
            })).toEqual({
                name: 'John',
                age: 30,
                bio: 'hello',
                details: {
                    height: 180,
                    weight: 70,
                    hair: {
                        color: 'brown',
                        length: null,
                    },
                },
                pets: null,
                preference: null,
                occupation: 'plumber',
            });
        });
        test('should handle enum', () => {
            expect(mapped.parse({
                name: 'John',
                age: 30,
                preference: 'dog person',
            })).toEqual({
                name: 'John',
                age: 30,
                bio: null,
                details: null,
                pets: null,
                preference: 'dog person',
                occupation: 'plumber',
            });
        });
        test('should handle nullable-array', () => {
            expect(mapped.parse({
                name: 'John',
                age: 30,
                bio: 'hello',
                pets: [
                    {
                        type: 'dog',
                        name: 'Rex',
                    },
                    {
                        type: 'cat',
                        name: 'Whiskers',
                    },
                ],
            })).toEqual({
                name: 'John',
                age: 30,
                bio: 'hello',
                details: null,
                pets: [
                    {
                        type: 'dog',
                        name: 'Rex',
                        breed: null,
                        barks: null,
                    },
                    {
                        type: 'cat',
                        name: 'Whiskers',
                        meows: null,
                    },
                ],
                preference: null,
                occupation: 'plumber',
            });
            expect(mapped.parse({
                name: 'John',
                age: 30,
                bio: 'hello',
                pets: [
                    {
                        type: 'dog',
                        name: 'Rex',
                        breed: 'labrador',
                        barks: true,
                    },
                    {
                        type: 'cat',
                        name: 'Whiskers',
                        meows: false,
                    },
                ],
            })).toEqual({
                name: 'John',
                age: 30,
                bio: 'hello',
                details: null,
                pets: [
                    {
                        type: 'dog',
                        name: 'Rex',
                        breed: 'labrador',
                        barks: true,
                    },
                    {
                        type: 'cat',
                        name: 'Whiskers',
                        meows: false,
                    },
                ],
                preference: null,
                occupation: 'plumber',
            });
        });
        test('should handle number coercion', () => {
            expect(mapped.parse({
                name: 'John',
                age: '30',
            })).toEqual({
                name: 'John',
                age: 30,
                bio: null,
                details: null,
                pets: null,
                preference: null,
                occupation: 'plumber',
            });
            expect(mapped.parse({
                name: 'John',
                age: '30',
                bio: 'hello',
                details: {
                    height: 180,
                    weight: '70',
                },
            })).toEqual({
                name: 'John',
                age: 30,
                bio: 'hello',
                details: {
                    height: 180,
                    weight: 70,
                    hair: null,
                },
                pets: null,
                preference: null,
                occupation: 'plumber',
            });
        });
        test('should handle default().nullable() and nullable().default()', () => {
            const fooSchema = zod_1.default.object({
                foo: zod_1.default.boolean().describe('foo').nullable().default(true),
            });
            const mappedX = mapZodSchema(fooSchema);
            expect(mappedX.parse({
                foo: true,
            })).toEqual({
                foo: true,
            });
            expect(mappedX.parse({})).toEqual({
                foo: true,
            });
            const barSchema = zod_1.default.object({
                bar: zod_1.default.boolean().describe('bar').default(true).nullable(),
            });
            const mappedBar = mapZodSchema(barSchema);
            expect(mappedBar.parse({
                bar: true,
            })).toEqual({
                bar: true,
            });
            expect(mappedBar.parse({})).toEqual({
                bar: true,
            });
        });
        test('should handle transforms', () => {
            const fooSchema = zod_1.default.object({
                foo: zod_1.default
                    .object({
                    bar: zod_1.default.string(),
                })
                    .nullable(),
            });
            const fooTransformed = fooSchema.transform((val) => ({
                foo: val.foo ? { bar: val.foo.bar } : { bar: 'baz' },
            }));
            const mappedFoo = mapZodSchema(fooSchema);
            expect(fooTransformed.parse(mappedFoo.parse({}))).toEqual({
                foo: { bar: 'baz' },
            });
        });
        test('should handle .positive/.int anumbers', () => {
            expect(mapped.safeParse({
                name: 'John',
                age: 30,
                bio: 'hello',
                details: {
                    height: 180,
                    weight: 70.2,
                },
            }).error?.issues?.[0]?.code).toBe('invalid_type');
            expect(mapped.safeParse({
                name: 'John',
                age: 30,
                bio: 'hello',
                details: {
                    height: 180,
                    weight: -70,
                },
            }).error?.issues?.[0]?.code).toBe('too_small');
        });
    });
    describe('runMetricQueryArgs', () => {
        const schema = toolRunMetricQueryArgs_1.toolRunMetricQueryArgsSchema;
        const schemaTransformed = toolRunMetricQueryArgs_1.toolRunMetricQueryArgsSchemaTransformed;
        const mapped = mapZodSchema(schema);
        const baseVizConfig = {
            exploreName: 'test',
            metrics: [],
            dimensions: [],
            sorts: [],
        };
        const base = {
            vizConfig: baseVizConfig,
        };
        test('should handle empty filters', () => {
            expect(mapped.parse({
                ...base,
            })).toEqual({
                ...base,
                customMetrics: null,
                vizConfig: {
                    ...baseVizConfig,
                    limit: null,
                },
                filters: null,
                tableCalculations: null,
            });
        });
        test('should handle filters', () => {
            const parsed = mapped.parse({
                ...base,
                filters: {
                    type: 'and',
                    dimensions: [
                        {
                            values: ['2023-01-01'],
                            fieldId: 'orders_order_date_year',
                            operator: 'equals',
                            fieldType: 'date',
                            fieldFilterType: 'date',
                        },
                    ],
                },
            });
            expect(parsed).toEqual({
                ...base,
                customMetrics: null,
                vizConfig: {
                    ...baseVizConfig,
                    limit: null,
                },
                filters: {
                    type: 'and',
                    metrics: null,
                    dimensions: [
                        {
                            values: ['2023-01-01'],
                            fieldId: 'orders_order_date_year',
                            operator: 'equals',
                            fieldType: 'date',
                            fieldFilterType: 'date',
                        },
                    ],
                    tableCalculations: null,
                },
                tableCalculations: null,
            });
            expect(() => schemaTransformed.parse(parsed)).not.toThrow();
        });
        test('should handle sorts', () => {
            expect(mapped.parse({
                ...base,
                customMetrics: [],
                vizConfig: {
                    ...baseVizConfig,
                    sorts: [
                        {
                            fieldId: 'test',
                            descending: true,
                            nullsFirst: true,
                        },
                    ],
                },
            })).toEqual({
                ...base,
                customMetrics: [],
                filters: null,
                vizConfig: {
                    ...baseVizConfig,
                    limit: null,
                    sorts: [
                        {
                            fieldId: 'test',
                            descending: true,
                            nullsFirst: true,
                        },
                    ],
                },
                tableCalculations: null,
            });
            expect(mapped.parse({
                ...base,
                customMetrics: [],
                vizConfig: {
                    ...baseVizConfig,
                    sorts: [
                        {
                            fieldId: 'test',
                            descending: true,
                        },
                    ],
                },
            })).toEqual({
                ...base,
                customMetrics: [],
                filters: null,
                vizConfig: {
                    ...baseVizConfig,
                    limit: null,
                    sorts: [
                        {
                            fieldId: 'test',
                            descending: true,
                            nullsFirst: null,
                        },
                    ],
                },
                tableCalculations: null,
            });
        });
    });
    describe('JSON Schema $ref generation', () => {
        // Regression test: zodToJsonSchema must not produce $ref pointers in
        // the run_metric_query schema. The MCP Gateway cannot resolve them and
        // returns 500 ERR_INVALID_URL when filters are present.
        test('toolRunMetricQueryArgsSchema should not produce $ref pointers', () => {
            // Use the same zodToJsonSchema that the MCP SDK uses internally
            // eslint-disable-next-line @typescript-eslint/no-var-requires, global-require
            const { zodToJsonSchema } = require('zod-to-json-schema');
            const processed = mcpSchemaCompatLayer.processZodType(toolRunMetricQueryArgs_1.toolRunMetricQueryArgsSchema);
            const jsonSchema = zodToJsonSchema(processed, {
                strictUnions: true,
                pipeStrategy: 'input',
            });
            const serialized = JSON.stringify(jsonSchema);
            expect(serialized).not.toContain('"$ref"');
        });
    });
    describe('pagination schema', () => {
        const schema = toolFindFieldsArgs_1.toolFindFieldsArgsSchema;
        const schemaTransformed = toolFindFieldsArgs_1.toolFindFieldsArgsSchemaTransformed;
        const mapped = mapZodSchema(schema);
        const base = {
            table: 'test',
            fieldSearchQueries: [
                {
                    label: 'test',
                },
            ],
        };
        test('should handle pagination', () => {
            expect(mapped.parse({
                ...base,
            })).toEqual({
                ...base,
                page: null,
            });
            expect(mapped.parse({
                ...base,
                page: 1,
            })).toEqual({
                ...base,
                page: 1,
            });
            expect(mapped.parse({
                ...base,
                page: '23',
            })).toEqual({
                ...base,
                page: 23,
            });
            expect(() => schemaTransformed.parse(mapped.parse({
                ...base,
                page: '23',
            }))).not.toThrow();
        });
    });
});
//# sourceMappingURL=McpSchemaCompatLayer.test.js.map