import { buildDriftAnalysisPrompt } from "./drift-analysis-prompt.js";
import { buildDriftOutputChecklist } from "./driftAnalysisSections.js";
describe("buildDriftOutputChecklist — final-step recommendations guidance", () => {
    const STATE_FILE = "/tmp/skyramp-analysis-abc123.json";
    it("non-inline mode includes recommendations and updateInstructions in final step", () => {
        const checklist = buildDriftOutputChecklist(3, 0, false, STATE_FILE);
        // Must instruct the LLM to pass recommendations to skyramp_actions
        expect(checklist).toContain("recommendations");
        // Must mention updateInstructions so the LLM knows to populate it
        expect(checklist).toContain("updateInstructions");
        // Must reference the stateFile path
        expect(checklist).toContain(STATE_FILE);
        // Must call skyramp_actions as the final action
        expect(checklist).toContain("skyramp_actions");
    });
    it("non-inline mode does not contain JSON shape — schema is authoritative", () => {
        const checklist = buildDriftOutputChecklist(3, 0, false, STATE_FILE);
        // The JSON shape was moved to inputSchema — prompt must not duplicate it
        expect(checklist).not.toContain('"testFile":');
        expect(checklist).not.toContain('"action":');
    });
    it("inline mode does not reference skyramp_actions or stateFile", () => {
        const checklist = buildDriftOutputChecklist(3, 0, true, STATE_FILE);
        // Inline mode applies changes directly — no skyramp_actions call
        expect(checklist).not.toContain("skyramp_actions");
        expect(checklist).not.toContain(STATE_FILE);
    });
    it("full prompt (non-inline) includes recommendations guidance", () => {
        const prompt = buildDriftAnalysisPrompt({
            existingTests: [],
            scannedEndpoints: [],
            repositoryPath: "/repo",
            stateFile: STATE_FILE,
        });
        expect(prompt).toContain("recommendations");
        expect(prompt).toContain("updateInstructions");
    });
});
describe("buildDriftAnalysisPrompt - inline mode", () => {
    beforeEach(() => { process.env.SKYRAMP_FEATURE_TESTBOT = "1"; });
    afterEach(() => { delete process.env.SKYRAMP_FEATURE_TESTBOT; });
    function inlinePrompt() {
        return buildDriftAnalysisPrompt({
            existingTests: [],
            scannedEndpoints: [],
            repositoryPath: "/repo",
            // stateFile omitted → inline mode
        });
    }
    it("wraps inline rules in drift_analysis_rules XML tags", () => {
        const prompt = inlinePrompt();
        expect(prompt).toContain("<drift_analysis_rules>");
        expect(prompt).toContain("</drift_analysis_rules>");
    });
    it("does not contain the persona statement", () => {
        const prompt = inlinePrompt();
        expect(prompt).not.toContain("You are acting as a Skyramp Integration Architect");
    });
    it("does not contain the standalone Test Health Analysis header", () => {
        const prompt = inlinePrompt();
        expect(prompt).not.toContain("# Test Health Analysis");
    });
    it("does not contain the skyramp_actions CTA (that belongs to standalone mode)", () => {
        const prompt = inlinePrompt();
        // Inline mode final step directs applying changes directly, not calling skyramp_actions
        expect(prompt).not.toContain("call `skyramp_actions`");
    });
});
describe("buildDriftAnalysisPrompt - scanned endpoints rendering", () => {
    // Reproduces the [object Object] bug: skeletonEndpoints from analyzeChangesTool
    // stores methods as objects { method: string, ... }, not plain strings.
    const skeletonMethodObjects = [
        {
            path: "/api/v1/",
            methods: [{ method: "GET", description: "", queryParams: [], authRequired: true, sourceFile: "main.py", interactions: [] }],
            resourceGroup: "v1",
            pathParams: [],
        },
        {
            path: "/api/v1/orders",
            methods: [
                { method: "GET", description: "", queryParams: [], authRequired: true, sourceFile: "orders.py", interactions: [] },
                { method: "POST", description: "", queryParams: [], authRequired: true, sourceFile: "orders.py", interactions: [] },
            ],
            resourceGroup: "orders",
            pathParams: [],
        },
    ];
    it("renders HTTP methods as strings, not [object Object]", () => {
        const prompt = buildDriftAnalysisPrompt({
            existingTests: [],
            scannedEndpoints: skeletonMethodObjects,
            repositoryPath: "/repo",
            stateFile: "/tmp/state.json",
        });
        expect(prompt).not.toContain("[object Object]");
        expect(prompt).toContain("GET /api/v1/");
        expect(prompt).toContain("GET|POST /api/v1/orders");
        // CTA should appear exactly once (not duplicated)
        const ctaCount = (prompt.match(/call `skyramp_actions`/g) || []).length;
        expect(ctaCount).toBe(1);
    });
    it("also works with plain string methods (ScannedEndpoint format)", () => {
        const stringMethods = [
            { path: "/api/v1/products", methods: ["GET", "POST"], sourceFile: "products.py" },
        ];
        const prompt = buildDriftAnalysisPrompt({
            existingTests: [],
            scannedEndpoints: stringMethods,
            repositoryPath: "/repo",
            stateFile: "/tmp/state.json",
        });
        expect(prompt).not.toContain("[object Object]");
        expect(prompt).toContain("GET|POST /api/v1/products");
    });
});
