import { describe, expect, it, vi } from "vitest";
import { resolveEmailToDid } from "#lib/api/emailDid.ts";
describe("resolveEmailToDid", () => {
it("short-circuits on an empty list without fetching", async () => {
const fetchFn = vi.fn();
await expect(resolveEmailToDid(fetchFn, [])).resolves.toEqual(new Map());
expect(fetchFn).not.toHaveBeenCalled();
});
it("requests each email as an encoded repeated param", async () => {
const fetchFn = vi.fn(async () => new Response("{}"));
await resolveEmailToDid(fetchFn, ["alice@example.com", "a+b@example.com"]);
expect(fetchFn).toHaveBeenCalledWith(
"/_internal/email-did?emails=alice%40example.com&emails=a%2Bb%40example.com"
);
});
it("keeps only emails that resolved to a did", async () => {
const fetchFn = vi.fn(
async () =>
new Response(
JSON.stringify({
"alice@example.com": "did:plc:alice",
"bob@example.com": null
})
)
);
await expect(
resolveEmailToDid(fetchFn, ["alice@example.com", "bob@example.com"])
).resolves.toEqual(new Map([["alice@example.com", "did:plc:alice"]]));
});
it("returns an empty map when the route errors", async () => {
const fetchFn = vi.fn(async () => new Response("nope", { status: 500 }));
await expect(resolveEmailToDid(fetchFn, ["alice@example.com"])).resolves.toEqual(new Map());
});
it("returns an empty map when the fetch rejects", async () => {
const fetchFn = vi.fn(async () => {
throw new TypeError("offline");
});
await expect(resolveEmailToDid(fetchFn, ["alice@example.com"])).resolves.toEqual(new Map());
});
it("returns an empty map for a malformed body", async () => {
const fetchFn = vi.fn(async () => new Response("not json"));
await expect(resolveEmailToDid(fetchFn, ["alice@example.com"])).resolves.toEqual(new Map());
});
});