import { describe, it, expect, vi } from "vitest";
import { runAuthenticate } from "../tools/admin-identity-authenticate.js";
function fetchReturning(json) {
    return vi.fn(async () => ({ json: async () => json }));
}
describe("runAuthenticate", () => {
    it("returns the operator name and profile on match", async () => {
        const f = fetchReturning({ kind: "match", userId: "u-1", userName: "Ada", aboutOwner: { ok: true, body: "PROFILE" } });
        const res = await runAuthenticate("1234", { fetchImpl: f, uiPort: "1" });
        expect(res).toContain("Ada");
        expect(res).toContain("PROFILE");
    });
    it("returns Invalid PIN on miss", async () => {
        const f = fetchReturning({ kind: "miss" });
        const res = await runAuthenticate("9999", { fetchImpl: f, uiPort: "1" });
        expect(res).toBe("Invalid PIN.");
    });
    it("omits the profile when aboutOwner is unresolved but still authenticates", async () => {
        const f = fetchReturning({ kind: "match", userId: "u-1", userName: "Ada", aboutOwner: { ok: false, reason: "profile-not-found" } });
        const res = await runAuthenticate("1234", { fetchImpl: f, uiPort: "1" });
        expect(res).toBe("Authenticated as Ada.");
    });
    it("reports service-unavailable when the endpoint throws", async () => {
        const f = vi.fn(async () => { throw new Error("ECONNREFUSED"); });
        const res = await runAuthenticate("1234", { fetchImpl: f, uiPort: "1" });
        expect(res).toContain("unavailable");
    });
    it("posts the pin to the loopback validate endpoint", async () => {
        const f = vi.fn(async () => ({ json: async () => ({ kind: "miss" }) }));
        await runAuthenticate("1234", { fetchImpl: f, uiPort: "19200" });
        expect(f).toHaveBeenCalledWith("http://127.0.0.1:19200/api/admin/identity/validate", expect.objectContaining({ method: "POST", body: JSON.stringify({ pin: "1234" }) }));
    });
});
//# sourceMappingURL=admin-identity-authenticate.test.js.map