jest.mock("@skyramp/skyramp", () => ({ Skyramp: class {
    } }));
import { mergeEnrichedScenarios } from "./registerRecommendTestsPrompt.js";
import { ScenarioSource } from "../../types/RepositoryAnalysis.js";
import { TestType } from "../../types/TestTypes.js";
function makeScenario(overrides = {}) {
    return {
        scenarioName: "base-scenario",
        description: "base",
        category: "crud",
        priority: "medium",
        steps: [{ order: 1, method: "GET", path: "/api/items", description: "list", interactionType: "success", expectedStatusCode: 200 }],
        chainingKeys: [],
        requiresAuth: true,
        estimatedComplexity: "simple",
        source: ScenarioSource.CodeInferred,
        testType: TestType.CONTRACT,
        ...overrides,
    };
}
const VALID_STEP = { order: 1, method: "post", path: "/api/orders", expectedStatusCode: 201 };
describe("mergeEnrichedScenarios — happy path", () => {
    it("merges a valid agent scenario into server scenarios", () => {
        const server = [makeScenario({ scenarioName: "existing" })];
        const raw = JSON.stringify([{
                scenarioName: "new-orders-flow",
                category: "business_rule",
                steps: [VALID_STEP],
            }]);
        const { scenarios, rejectionNotes } = mergeEnrichedScenarios(server, raw);
        expect(rejectionNotes).toHaveLength(0);
        expect(scenarios.find(s => s.scenarioName === "new-orders-flow")).toBeDefined();
        expect(scenarios.find(s => s.scenarioName === "existing")).toBeDefined();
        expect(scenarios).toHaveLength(2);
    });
    it("overrides a server scenario when agent provides same scenarioName", () => {
        const server = [makeScenario({ scenarioName: "orders-flow", description: "server version" })];
        const raw = JSON.stringify([{
                scenarioName: "orders-flow",
                category: "business_rule",
                description: "agent version",
                steps: [VALID_STEP],
            }]);
        const { scenarios } = mergeEnrichedScenarios(server, raw);
        expect(scenarios).toHaveLength(1);
        expect(scenarios[0].description).toBe("agent version");
        expect(scenarios[0].source).toBe("agent-enriched");
    });
    it("normalizes method to uppercase", () => {
        const raw = JSON.stringify([{
                scenarioName: "uppercase-test",
                category: "crud",
                steps: [{ order: 1, method: "post", path: "/api/items", expectedStatusCode: 201 }],
            }]);
        const { scenarios } = mergeEnrichedScenarios([], raw);
        expect(scenarios[0].steps[0].method).toBe("POST");
    });
    it("preserves bugCatchingTarget when provided", () => {
        const raw = JSON.stringify([{
                scenarioName: "formula-test",
                category: "business_rule",
                bugCatchingTarget: "total = price * qty",
                steps: [VALID_STEP],
            }]);
        const { scenarios } = mergeEnrichedScenarios([], raw);
        expect(scenarios[0].bugCatchingTarget).toBe("total = price * qty");
    });
    it("falls back to server scenarios on empty agent array", () => {
        const server = [makeScenario({ scenarioName: "server-only" })];
        const { scenarios, rejectionNotes } = mergeEnrichedScenarios(server, "[]");
        // Empty array → no agent scenarios, return server ones unchanged
        expect(scenarios).toEqual(server);
        expect(rejectionNotes).toHaveLength(0);
    });
});
describe("mergeEnrichedScenarios — rejection cases", () => {
    it("rejects scenario with missing scenarioName", () => {
        const raw = JSON.stringify([{ category: "crud", steps: [VALID_STEP] }]);
        const { scenarios, rejectionNotes } = mergeEnrichedScenarios([], raw);
        expect(scenarios).toHaveLength(0);
        expect(rejectionNotes[0]).toMatch(/missing scenarioName/);
    });
    it("rejects scenario with missing steps array", () => {
        const raw = JSON.stringify([{ scenarioName: "no-steps", category: "crud" }]);
        const { rejectionNotes } = mergeEnrichedScenarios([], raw);
        expect(rejectionNotes[0]).toMatch(/missing or empty steps/);
    });
    it("rejects scenario with empty steps array", () => {
        const raw = JSON.stringify([{ scenarioName: "empty-steps", category: "crud", steps: [] }]);
        const { rejectionNotes } = mergeEnrichedScenarios([], raw);
        expect(rejectionNotes[0]).toMatch(/missing or empty steps/);
    });
    it("rejects scenario with missing category", () => {
        const raw = JSON.stringify([{ scenarioName: "no-cat", steps: [VALID_STEP] }]);
        const { rejectionNotes } = mergeEnrichedScenarios([], raw);
        expect(rejectionNotes[0]).toMatch(/missing category/);
    });
    it("rejects scenario with unknown category", () => {
        const raw = JSON.stringify([{ scenarioName: "bad-cat", category: "not_a_real_category", steps: [VALID_STEP] }]);
        const { rejectionNotes } = mergeEnrichedScenarios([], raw);
        expect(rejectionNotes[0]).toMatch(/unknown category/);
    });
    it("falls back to server scenarios on invalid JSON", () => {
        const server = [makeScenario()];
        const { scenarios, rejectionNotes } = mergeEnrichedScenarios(server, "{ bad json");
        expect(scenarios).toEqual(server);
        expect(rejectionNotes[0]).toMatch(/invalid JSON/);
    });
    it("falls back to server scenarios when JSON is not an array", () => {
        const server = [makeScenario()];
        const { scenarios, rejectionNotes } = mergeEnrichedScenarios(server, JSON.stringify({ not: "array" }));
        expect(scenarios).toEqual(server);
        expect(rejectionNotes[0]).toMatch(/expected a JSON array/);
    });
    it("accepts valid scenarios and rejects invalid ones in the same batch", () => {
        const raw = JSON.stringify([
            { scenarioName: "valid-one", category: "crud", steps: [VALID_STEP] },
            { category: "crud", steps: [VALID_STEP] }, // missing scenarioName
        ]);
        const { scenarios, rejectionNotes } = mergeEnrichedScenarios([], raw);
        expect(scenarios).toHaveLength(1);
        expect(scenarios[0].scenarioName).toBe("valid-one");
        expect(rejectionNotes).toHaveLength(1);
    });
});
