"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const ts = __importStar(require("typescript"));
function detectMissingValidation(source) {
    const sourceFile = ts.createSourceFile('test.ts', source, ts.ScriptTarget.Latest, true);
    let count = 0;
    const isHandlerLike = (n) => ts.isFunctionDeclaration(n) ||
        ts.isFunctionExpression(n) ||
        ts.isArrowFunction(n);
    function checkNode(n) {
        if (isHandlerLike(n)) {
            let funcName = '';
            let body;
            if (ts.isFunctionDeclaration(n) && n.name) {
                funcName = n.name.text;
                body = n.body;
            }
            else if (ts.isFunctionExpression(n) || ts.isArrowFunction(n)) {
                let p = n.parent;
                while (p) {
                    if (ts.isVariableDeclaration(p) && ts.isIdentifier(p.name)) {
                        funcName = p.name.text;
                        break;
                    }
                    p = p.parent;
                }
                body = n.body;
            }
            const isHandler = funcName.toLowerCase().includes('handler') ||
                funcName.toLowerCase().includes('route') ||
                funcName.toLowerCase().includes('endpoint') ||
                funcName.toLowerCase().includes('controller');
            if (isHandler) {
                let hasValidation = false;
                const checkBody = (node) => {
                    if (ts.isCallExpression(node)) {
                        const ct = node.expression.getText();
                        if (ct.includes('.parse') ||
                            ct.includes('.validate') ||
                            ct.includes('.safeParse') ||
                            ct.includes('check(') ||
                            ct.includes('body(') ||
                            ct.includes('query(')) {
                            hasValidation = true;
                        }
                    }
                    ts.forEachChild(node, checkBody);
                };
                if (body)
                    checkBody(body);
                if (!hasValidation)
                    count++;
            }
        }
        ts.forEachChild(n, checkNode);
    }
    checkNode(sourceFile);
    return count;
}
describe('Validation detection (Unit 1.3.5)', () => {
    test('arrow function handler without validation is flagged', () => {
        const code = `
      const createUserHandler = async (req, res) => {
        const user = await db.user.create(req.body);
        res.json(user);
      };
    `;
        expect(detectMissingValidation(code)).toBe(1);
    });
    test('arrow function handler WITH zod parse is NOT flagged', () => {
        const code = `
      const createUserHandler = async (req, res) => {
        const data = schema.parse(req.body);
        const user = await db.user.create(data);
        res.json(user);
      };
    `;
        expect(detectMissingValidation(code)).toBe(0);
    });
    test('function declaration handler without validation is flagged', () => {
        const code = `
      async function getUserHandler(req, res) {
        const user = await db.user.find(req.query.id);
        res.json(user);
      }
    `;
        expect(detectMissingValidation(code)).toBe(1);
    });
    test('non-handler function is NOT checked', () => {
        const code = `
      const processData = async (data) => {
        return data.map(x => x * 2);
      };
    `;
        expect(detectMissingValidation(code)).toBe(0);
    });
});
//# sourceMappingURL=code-scanner-validation.test.js.map