import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { scanAllRepoEndpoints, scanRelatedEndpoints } from "./repoScanner.js";
import { classifyEndpointsByChangedFiles } from "./routeParsers.js";
describe("scanAllRepoEndpoints", () => {
    const tempRepos = [];
    afterEach(() => {
        for (const repo of tempRepos.splice(0)) {
            fs.rmSync(repo, { recursive: true, force: true });
        }
    });
    function makeTempRepo() {
        const repo = fs.mkdtempSync(path.join(os.tmpdir(), "repo-scanner-"));
        tempRepos.push(repo);
        return repo;
    }
    it("preserves same router-relative paths from different source files", () => {
        const repo = makeTempRepo();
        const apiDir = path.join(repo, "src/prefect/server/api");
        fs.mkdirSync(apiDir, { recursive: true });
        fs.writeFileSync(path.join(apiDir, "flows.py"), `
@router.delete(
    "/{id:uuid}",
    status_code=204,
)
async def delete_flow(): pass

@router.post("/bulk_delete")
async def bulk_delete_flows(): pass
`);
        fs.writeFileSync(path.join(apiDir, "deployments.py"), `
@router.delete(
    "/{id:uuid}",
    status_code=204,
)
async def delete_deployment(): pass

@router.post("/bulk_delete")
async def bulk_delete_deployments(): pass
`);
        const endpoints = scanAllRepoEndpoints(repo);
        const relatedEndpoints = scanRelatedEndpoints(repo, ["src/prefect/server/api/flows.py"]);
        expect(endpoints).toEqual(expect.arrayContaining([
            { path: "/{id:uuid}", methods: ["DELETE"], sourceFile: "src/prefect/server/api/flows.py" },
            { path: "/bulk_delete", methods: ["POST"], sourceFile: "src/prefect/server/api/flows.py" },
            { path: "/{id:uuid}", methods: ["DELETE"], sourceFile: "src/prefect/server/api/deployments.py" },
            { path: "/bulk_delete", methods: ["POST"], sourceFile: "src/prefect/server/api/deployments.py" },
        ]));
        expect(relatedEndpoints).toEqual(expect.arrayContaining([
            { path: "/{id:uuid}", methods: ["DELETE"], sourceFile: "src/prefect/server/api/flows.py" },
            { path: "/bulk_delete", methods: ["POST"], sourceFile: "src/prefect/server/api/flows.py" },
        ]));
        const classified = classifyEndpointsByChangedFiles({
            currentBranch: "feature/admin-key",
            baseBranch: "origin/main",
            changedFiles: ["src/prefect/server/api/flows.py"],
            diffContent: "",
            diffStat: "",
            newFiles: [],
            deletedFiles: [],
        }, endpoints);
        expect(classified.changedEndpoints).toEqual(expect.arrayContaining([
            { path: "/{id:uuid}", methods: ["DELETE"], sourceFile: "src/prefect/server/api/flows.py" },
            { path: "/bulk_delete", methods: ["POST"], sourceFile: "src/prefect/server/api/flows.py" },
        ]));
        expect(classified.changedEndpoints).not.toEqual(expect.arrayContaining([
            { path: "/{id:uuid}", methods: ["DELETE"], sourceFile: "src/prefect/server/api/deployments.py" },
        ]));
    });
    it("does not suppress removed router-relative endpoints with same-path siblings", () => {
        const classified = classifyEndpointsByChangedFiles({
            currentBranch: "feature/remove-flows",
            baseBranch: "origin/main",
            changedFiles: ["src/prefect/server/api/flows.py"],
            diffContent: "",
            diffStat: "",
            newFiles: [],
            deletedFiles: ["src/prefect/server/api/flows.py"],
        }, [
            { path: "/{id:uuid}", methods: ["DELETE"], sourceFile: "src/prefect/server/api/deployments.py" },
            { path: "/bulk_delete", methods: ["POST"], sourceFile: "src/prefect/server/api/deployments.py" },
        ], [
            { path: "/{id:uuid}", methods: ["DELETE"], sourceFile: "src/prefect/server/api/flows.py" },
            { path: "/bulk_delete", methods: ["POST"], sourceFile: "src/prefect/server/api/flows.py" },
        ]);
        expect(classified.removedEndpoints).toEqual(expect.arrayContaining([
            { path: "/{id:uuid}", methods: ["DELETE"], sourceFile: "src/prefect/server/api/flows.py" },
            { path: "/bulk_delete", methods: ["POST"], sourceFile: "src/prefect/server/api/flows.py" },
        ]));
    });
});
