~james/core

core/web/src/lib/api/emailDid.test.ts -rw-r--r-- 1.8 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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());
	});
});