import { TOOL_PHASE_MAP, TOOLS_WITHOUT_PHASE } from "./tool-phases.js";
import * as fs from "fs";
import * as path from "path";
function findAllToolNames() {
    const toolNames = [];
    function walk(dir) {
        for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
            const full = path.join(dir, entry.name);
            if (entry.isDirectory()) {
                walk(full);
                continue;
            }
            if (!entry.name.endsWith(".ts"))
                continue;
            const content = fs.readFileSync(full, "utf-8");
            const matches = content.matchAll(/const TOOL_NAME\s*=\s*["']([^"']+)["']/g);
            for (const m of matches)
                toolNames.push(m[1]);
        }
    }
    walk(path.resolve(__dirname, "./tools"));
    return toolNames;
}
describe("tool-phase-coverage", () => {
    it("every registered tool has a phase or is explicitly excluded", () => {
        const allTools = findAllToolNames();
        expect(allTools.length).toBeGreaterThan(0);
        const missing = allTools.filter(name => !(name in TOOL_PHASE_MAP) && !TOOLS_WITHOUT_PHASE.has(name));
        if (missing.length > 0) {
            throw new Error(`These tools are not in TOOL_PHASE_MAP or TOOLS_WITHOUT_PHASE in src/tool-phases.ts:\n` +
                missing.map(n => `  - ${n}`).join("\n") +
                `\nAdd them to the appropriate export in src/tool-phases.ts`);
        }
    });
    it("TOOL_PHASE_MAP contains only valid phases", () => {
        const validPhases = new Set(["analyzing", "generating", "executing", "maintaining", "reporting"]);
        for (const [tool, mapping] of Object.entries(TOOL_PHASE_MAP)) {
            if (typeof mapping === "string") {
                expect(validPhases.has(mapping)).toBe(true);
            }
            else {
                expect(validPhases.has(mapping.before)).toBe(true);
                expect(validPhases.has(mapping.after)).toBe(true);
            }
        }
    });
});
