import { getWorkspaceBaseUrl } from "./workspaceAuth.js";
const REPO = "/some/repo";
jest.mock("@skyramp/skyramp", () => {
    const twoServiceConfig = {
        services: [
            {
                serviceName: "backend",
                language: "python",
                testDirectory: "backend/tests",
                api: { baseUrl: "http://localhost:8000" },
            },
            {
                serviceName: "frontend",
                language: "typescript",
                testDirectory: "frontend/tests",
                api: { baseUrl: "http://localhost:5173" },
            },
        ],
    };
    return {
        WorkspaceConfigManager: jest.fn().mockImplementation(() => ({
            exists: jest.fn().mockResolvedValue(true),
            read: jest.fn().mockResolvedValue(twoServiceConfig),
        })),
    };
});
describe("getWorkspaceBaseUrl", () => {
    it("should return no match when testFile is not provided", async () => {
        const result = await getWorkspaceBaseUrl(REPO);
        expect(result.baseUrl).toBeUndefined();
        expect(result.candidates).toHaveLength(0);
    });
    it("should match backend service by testDirectory prefix", async () => {
        const result = await getWorkspaceBaseUrl(REPO, `${REPO}/backend/tests/test_api.py`);
        expect(result.baseUrl).toBe("http://localhost:8000");
        expect(result.candidates).toHaveLength(0);
    });
    it("should match frontend service by testDirectory prefix", async () => {
        const result = await getWorkspaceBaseUrl(REPO, `${REPO}/frontend/tests/auth.spec.ts`);
        expect(result.baseUrl).toBe("http://localhost:5173");
        expect(result.candidates).toHaveLength(0);
    });
    it("should return no match when testFile does not match any service testDirectory", async () => {
        const result = await getWorkspaceBaseUrl(REPO, `${REPO}/other/tests/test_something.py`);
        expect(result.baseUrl).toBeUndefined();
        expect(result.candidates).toHaveLength(0);
    });
    it("should strip pathname from baseUrl (scheme+host+port only)", async () => {
        const { WorkspaceConfigManager } = await import("@skyramp/skyramp");
        WorkspaceConfigManager.mockImplementationOnce(() => ({
            exists: jest.fn().mockResolvedValue(true),
            read: jest.fn().mockResolvedValue({
                services: [
                    {
                        serviceName: "api",
                        testDirectory: "tests",
                        api: { baseUrl: "http://localhost:8000/api/v2" },
                    },
                ],
            }),
        }));
        const result = await getWorkspaceBaseUrl(REPO, `${REPO}/tests/test_api.py`);
        expect(result.baseUrl).toBe("http://localhost:8000");
        expect(result.candidates).toHaveLength(0);
    });
    it("should return no match when workspace does not exist", async () => {
        const { WorkspaceConfigManager } = await import("@skyramp/skyramp");
        WorkspaceConfigManager.mockImplementationOnce(() => ({
            exists: jest.fn().mockResolvedValue(false),
            read: jest.fn(),
        }));
        const result = await getWorkspaceBaseUrl(REPO, `${REPO}/tests/test_api.py`);
        expect(result.baseUrl).toBeUndefined();
        expect(result.candidates).toHaveLength(0);
    });
    it("should disambiguate by language when multiple services share the same testDirectory", async () => {
        const { WorkspaceConfigManager } = await import("@skyramp/skyramp");
        WorkspaceConfigManager.mockImplementationOnce(() => ({
            exists: jest.fn().mockResolvedValue(true),
            read: jest.fn().mockResolvedValue({
                services: [
                    {
                        serviceName: "svc-python",
                        language: "python",
                        testDirectory: "shared/tests",
                        api: { baseUrl: "http://localhost:8000" },
                    },
                    {
                        serviceName: "svc-ts",
                        language: "typescript",
                        testDirectory: "shared/tests",
                        api: { baseUrl: "http://localhost:9000" },
                    },
                ],
            }),
        }));
        const result = await getWorkspaceBaseUrl(REPO, `${REPO}/shared/tests/test_api.py`, "python");
        expect(result.baseUrl).toBe("http://localhost:8000");
        expect(result.candidates).toHaveLength(0);
    });
    it("should return candidates when testDirectory and language are both shared across services", async () => {
        const { WorkspaceConfigManager } = await import("@skyramp/skyramp");
        WorkspaceConfigManager.mockImplementationOnce(() => ({
            exists: jest.fn().mockResolvedValue(true),
            read: jest.fn().mockResolvedValue({
                services: [
                    {
                        serviceName: "svc-a",
                        language: "python",
                        testDirectory: "shared/tests",
                        api: { baseUrl: "http://localhost:8000" },
                    },
                    {
                        serviceName: "svc-b",
                        language: "python",
                        testDirectory: "shared/tests",
                        api: { baseUrl: "http://localhost:9000" },
                    },
                ],
            }),
        }));
        const result = await getWorkspaceBaseUrl(REPO, `${REPO}/shared/tests/test_api.py`, "python");
        expect(result.baseUrl).toBeUndefined();
        expect(result.candidates).toHaveLength(2);
        expect(result.candidates.map((c) => c.serviceName)).toEqual(["svc-a", "svc-b"]);
        expect(result.candidates.map((c) => c.baseUrl)).toEqual([
            "http://localhost:8000",
            "http://localhost:9000",
        ]);
    });
    it("should match service using testDirectory as the workspace field for test file lookup", async () => {
        const { WorkspaceConfigManager } = await import("@skyramp/skyramp");
        WorkspaceConfigManager.mockImplementationOnce(() => ({
            exists: jest.fn().mockResolvedValue(true),
            read: jest.fn().mockResolvedValue({
                services: [
                    {
                        serviceName: "api",
                        testDirectory: "tests",
                        api: { baseUrl: "http://localhost:9000" },
                    },
                ],
            }),
        }));
        const result = await getWorkspaceBaseUrl(REPO, `${REPO}/tests/test_api.py`);
        expect(result.baseUrl).toBe("http://localhost:9000");
        expect(result.candidates).toHaveLength(0);
    });
    it("should return dockerNetwork when matched service has runtimeDetails.dockerNetwork", async () => {
        const { WorkspaceConfigManager } = await import("@skyramp/skyramp");
        WorkspaceConfigManager.mockImplementationOnce(() => ({
            exists: jest.fn().mockResolvedValue(true),
            read: jest.fn().mockResolvedValue({
                services: [
                    {
                        serviceName: "backend",
                        testDirectory: "tests",
                        api: { baseUrl: "http://localhost:8000" },
                        runtimeDetails: { dockerNetwork: "myapp_default" },
                    },
                ],
            }),
        }));
        const result = await getWorkspaceBaseUrl(REPO, `${REPO}/tests/test_api.py`);
        expect(result.baseUrl).toBe("http://localhost:8000");
        expect(result.dockerNetwork).toBe("myapp_default");
    });
    it("should return undefined dockerNetwork when service has no runtimeDetails", async () => {
        const result = await getWorkspaceBaseUrl(REPO, `${REPO}/backend/tests/test_api.py`);
        expect(result.baseUrl).toBe("http://localhost:8000");
        expect(result.dockerNetwork).toBeUndefined();
    });
    it("should return undefined dockerNetwork when ambiguous", async () => {
        const { WorkspaceConfigManager } = await import("@skyramp/skyramp");
        WorkspaceConfigManager.mockImplementationOnce(() => ({
            exists: jest.fn().mockResolvedValue(true),
            read: jest.fn().mockResolvedValue({
                services: [
                    {
                        serviceName: "svc-a",
                        language: "python",
                        testDirectory: "shared/tests",
                        api: { baseUrl: "http://localhost:8000" },
                        runtimeDetails: { dockerNetwork: "net-a" },
                    },
                    {
                        serviceName: "svc-b",
                        language: "python",
                        testDirectory: "shared/tests",
                        api: { baseUrl: "http://localhost:9000" },
                        runtimeDetails: { dockerNetwork: "net-b" },
                    },
                ],
            }),
        }));
        const result = await getWorkspaceBaseUrl(REPO, `${REPO}/shared/tests/test_api.py`, "python");
        expect(result.baseUrl).toBeUndefined();
        expect(result.dockerNetwork).toBeUndefined();
        expect(result.candidates).toHaveLength(2);
    });
    it("should return no match when matched service baseUrl is not a valid URL", async () => {
        const { WorkspaceConfigManager } = await import("@skyramp/skyramp");
        WorkspaceConfigManager.mockImplementationOnce(() => ({
            exists: jest.fn().mockResolvedValue(true),
            read: jest.fn().mockResolvedValue({
                services: [{ api: { baseUrl: "not-a-valid-url" }, testDirectory: "tests" }],
            }),
        }));
        const result = await getWorkspaceBaseUrl(REPO, `${REPO}/tests/test_api.py`);
        expect(result.baseUrl).toBeUndefined();
    });
});
