// SPEC-452 — Generates edge-case tests from Zod schemas
import { scanZodOptionalFields } from '../schema-parity-auditor/zod-scanner.js';
const ZOD_TYPE_RE = /(\w+):\s*z\.(\w+)\(/;
export function extractAllFields(fileContent) {
    const fields = [];
    const seen = new Set();
    for (const line of fileContent.split('\n')) {
        const match = ZOD_TYPE_RE.exec(line);
        if (match) {
            const fieldName = match[1] ?? '';
            const zodType = match[2] ?? '';
            if (fieldName && !seen.has(fieldName)) {
                seen.add(fieldName);
                fields.push({ fieldName, zodType });
            }
        }
    }
    return fields;
}
export function generateEdgeCases(fields, optionalFieldNames) {
    const optionalSet = new Set(optionalFieldNames);
    const cases = [];
    for (const field of fields) {
        if (optionalSet.has(field.fieldName)) {
            cases.push({
                fieldName: field.fieldName,
                zodType: field.zodType,
                input: undefined,
                description: `${field.fieldName} omitted (optional field)`,
                testCode: `  it('handles ${field.fieldName} being undefined (optional)', () => {\n    // Field is .optional() in Zod — handler must use ?? or null-check\n    const input = { ...validInput, ${field.fieldName}: undefined };\n    expect(() => handler(input)).not.toThrow();\n  });`,
            });
        }
        switch (field.zodType) {
            case 'string':
                cases.push({
                    fieldName: field.fieldName,
                    zodType: 'string',
                    input: '',
                    description: `${field.fieldName} empty string`,
                    testCode: `  it('handles ${field.fieldName} as empty string', () => {\n    const input = { ...validInput, ${field.fieldName}: '' };\n    expect(() => handler(input)).not.toThrow();\n  });`,
                });
                break;
            case 'array':
                cases.push({
                    fieldName: field.fieldName,
                    zodType: 'array',
                    input: [],
                    description: `${field.fieldName} empty array`,
                    testCode: `  it('handles ${field.fieldName} as empty array', () => {\n    const input = { ...validInput, ${field.fieldName}: [] };\n    expect(() => handler(input)).not.toThrow();\n  });`,
                });
                break;
            case 'number':
                cases.push({
                    fieldName: field.fieldName,
                    zodType: 'number',
                    input: 0,
                    description: `${field.fieldName} zero value`,
                    testCode: `  it('handles ${field.fieldName} as zero', () => {\n    const input = { ...validInput, ${field.fieldName}: 0 };\n    expect(() => handler(input)).not.toThrow();\n  });`,
                });
                break;
            case 'boolean':
                cases.push({
                    fieldName: field.fieldName,
                    zodType: 'boolean',
                    input: false,
                    description: `${field.fieldName} false value`,
                    testCode: `  it('handles ${field.fieldName} as false', () => {\n    const input = { ...validInput, ${field.fieldName}: false };\n    expect(() => handler(input)).not.toThrow();\n  });`,
                });
                break;
        }
    }
    return cases;
}
export async function generateEdgeTests(toolName, registerFile, fileContent) {
    const allFields = extractAllFields(fileContent);
    const optionalFields = await scanZodOptionalFields(registerFile);
    const optionalNames = optionalFields.map((f) => f.fieldName);
    const cases = generateEdgeCases(allFields, optionalNames);
    const lines = [
        `// Edge-case tests auto-generated for ${toolName}`,
        `// These test boundary inputs that Zod allows but might crash handlers`,
        ``,
        `import { describe, it, expect } from 'vitest';`,
        ``,
        `describe('${toolName} — edge cases', () => {`,
        `  const validInput = {}; // TODO: fill with valid base input`,
        `  const handler = () => {}; // TODO: import actual handler`,
        ``,
    ];
    for (const c of cases) {
        lines.push(c.testCode);
        lines.push('');
    }
    lines.push('});');
    lines.push('');
    return { toolName, registerFile, cases, fileContent: lines.join('\n') };
}
//# sourceMappingURL=zod-edge-generator.js.map