/**
 * Unit tests for trace-parser.ts — specifically the scanTraceArtifactDirs scoping
 * introduced to prevent demo/fixture files (e.g. frontend/public/traces/) from being
 * misidentified as testbot-generated traces.
 */
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { discoverTraceFiles, discoverPlaywrightZips } from "./trace-parser.js";
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function mkdirp(dir) {
    fs.mkdirSync(dir, { recursive: true });
}
function touch(file) {
    mkdirp(path.dirname(file));
    fs.writeFileSync(file, "");
}
function withTempRepo(fn) {
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), "trace-parser-test-"));
    try {
        fn(dir);
    }
    finally {
        fs.rmSync(dir, { recursive: true, force: true });
    }
}
// ---------------------------------------------------------------------------
// discoverPlaywrightZips — scoping tests
// ---------------------------------------------------------------------------
describe("discoverPlaywrightZips — scanTraceArtifactDirs scoping", () => {
    it("does NOT discover playwright zip in frontend/public/traces/ (demo fixture dir)", () => {
        withTempRepo(repo => {
            touch(path.join(repo, "frontend", "public", "traces", "ui_test_playwright.zip"));
            expect(discoverPlaywrightZips(repo)).toEqual([]);
        });
    });
    it("discovers playwright zip in tests/ (test-artifact dir)", () => {
        withTempRepo(repo => {
            const zip = path.join(repo, "tests", "ui_test_playwright.zip");
            touch(zip);
            expect(discoverPlaywrightZips(repo)).toContain(zip);
        });
    });
    it("discovers playwright zip in .skyramp/ (test-artifact dir)", () => {
        withTempRepo(repo => {
            const zip = path.join(repo, ".skyramp", "recording_playwright.zip");
            touch(zip);
            expect(discoverPlaywrightZips(repo)).toContain(zip);
        });
    });
    it("discovers playwright zip in e2e/ (test-artifact dir)", () => {
        withTempRepo(repo => {
            const zip = path.join(repo, "e2e", "flow_playwright.zip");
            touch(zip);
            expect(discoverPlaywrightZips(repo)).toContain(zip);
        });
    });
    it("discovers playwright zip in playwright/ (test-artifact dir)", () => {
        withTempRepo(repo => {
            const zip = path.join(repo, "playwright", "trace.zip");
            touch(zip);
            expect(discoverPlaywrightZips(repo)).toContain(zip);
        });
    });
    it("does NOT discover zip in src/ (not a test-artifact dir)", () => {
        withTempRepo(repo => {
            touch(path.join(repo, "src", "recordings", "ui_playwright.zip"));
            expect(discoverPlaywrightZips(repo)).toEqual([]);
        });
    });
    it("does NOT discover zip in deeply nested non-test dir", () => {
        withTempRepo(repo => {
            touch(path.join(repo, "frontend", "src", "assets", "demo_playwright.zip"));
            expect(discoverPlaywrightZips(repo)).toEqual([]);
        });
    });
});
// ---------------------------------------------------------------------------
// discoverTraceFiles — scoping tests
// ---------------------------------------------------------------------------
describe("discoverTraceFiles — scanTraceArtifactDirs scoping", () => {
    it("does NOT discover trace.json nested under frontend/public/traces/", () => {
        withTempRepo(repo => {
            touch(path.join(repo, "frontend", "public", "traces", "backend_trace.json"));
            const found = discoverTraceFiles(repo);
            // fixed-name root candidates don't match "backend_trace.json", and scan won't reach frontend/
            expect(found.some(f => f.includes("frontend"))).toBe(false);
        });
    });
    it("discovers trace.json in tests/ dir", () => {
        withTempRepo(repo => {
            const f = path.join(repo, "tests", "backend_trace.json");
            touch(f);
            expect(discoverTraceFiles(repo)).toContain(f);
        });
    });
    it("discovers trace.json in .skyramp/ dir", () => {
        withTempRepo(repo => {
            const f = path.join(repo, ".skyramp", "skyramp_trace.json");
            touch(f);
            expect(discoverTraceFiles(repo)).toContain(f);
        });
    });
    it("discovers root-level trace.json", () => {
        withTempRepo(repo => {
            const f = path.join(repo, "trace.json");
            touch(f);
            expect(discoverTraceFiles(repo)).toContain(f);
        });
    });
    it("discovers root-level skyramp_traces.json via fixed-name check", () => {
        withTempRepo(repo => {
            const f = path.join(repo, "skyramp_traces.json");
            touch(f);
            expect(discoverTraceFiles(repo)).toContain(f);
        });
    });
    it("does NOT discover scenario_ json files (excluded by predicate)", () => {
        withTempRepo(repo => {
            touch(path.join(repo, "tests", "scenario_orders_trace.json"));
            expect(discoverTraceFiles(repo)).toEqual([]);
        });
    });
    it("does NOT discover _test.json files (excluded by predicate)", () => {
        withTempRepo(repo => {
            touch(path.join(repo, "tests", "orders_trace_test.json"));
            expect(discoverTraceFiles(repo)).toEqual([]);
        });
    });
    it("results are deduplicated when fixed-name and scan both find the same root file", () => {
        withTempRepo(repo => {
            const f = path.join(repo, "trace.json");
            touch(f);
            const found = discoverTraceFiles(repo);
            expect(found.filter(x => x === f)).toHaveLength(1);
        });
    });
});
