~james/core

core/web/src/lib/server/lookupEvents.test.ts -rw-r--r-- 5.9 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createBobbinClient } from "#lib/api/client.ts";
import { resolveMiniDoc, type MiniDoc } from "#lib/api/identity.ts";
import { lookups } from "#lib/api/lookupCache.ts";
import { resolveRepoByName } from "#lib/api/repo.ts";
import { subscribeLookupEvents, subscribeUrl } from "#lib/server/lookupEvents.ts";
import type { Fetch } from "#lib/fetch.ts";

const ALICE: MiniDoc = {
	did: "did:plc:alice",
	handle: "alice.test",
	pds: "https://pds.example",
	signing_key: "zQ3shSiLsnqpyQ4SfDTT1D8qzFEoeYT8rSDXW6o8pVY7VcRBJ"
};

const REPO = {
	uri: "at://did:plc:repo/org.tangled.repo.manifest/self",
	value: { declaration: { owner: ALICE.did, name: "kernel" } },
	git: { defaultBranch: "main", isEmpty: false }
};

const json = (body: unknown, status = 200) =>
	new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });

class FakeSocket {
	onopen: (() => void) | null = null;
	onmessage: ((message: { data: unknown }) => void) | null = null;
	onclose: (() => void) | null = null;
	constructor(readonly url: string) {}
	close() {
		this.onclose?.();
	}
	send(event: unknown) {
		this.onmessage?.({ data: JSON.stringify(event) });
	}
}

const follow = () => {
	const sockets: FakeSocket[] = [];
	const events = subscribeLookupEvents({
		url: "wss://hydrant.test/subscribe",
		connect: (url) => {
			const socket = new FakeSocket(url);
			sockets.push(socket);
			return socket;
		}
	});
	sockets[0].onopen?.();
	return { events, sockets };
};

const counting = (...docs: MiniDoc[]) => {
	const fetchMock = vi.fn<Fetch>(async (input) => {
		const identifier = new URL(String(input)).searchParams.get("identifier");
		const doc = docs.find((d) => d.did === identifier || d.handle === identifier);
		return doc ? json(doc) : json({ error: "InvalidRequest", message: "unknown" }, 400);
	});
	return {
		fetchMock,
		ctx: createBobbinClient({ serviceUrl: "https://bobbin.test", fetch: fetchMock })
	};
};

describe("lookup events", () => {
	beforeEach(() => {
		lookups.configure(60_000);
		vi.spyOn(console, "info").mockImplementation(() => {});
		vi.spyOn(console, "warn").mockImplementation(() => {});
	});
	afterEach(() => {
		lookups.configure(0);
		vi.useRealTimers();
		vi.restoreAllMocks();
	});

	it("asks hydrant for identity events and the repo records", () => {
		const url = new URL(subscribeUrl("wss://hydrant.test/subscribe"));
		expect(url.searchParams.getAll("wantedCollections")).toEqual([
			"org.tangled.repo.manifest",
			"org.tangled.git.refs"
		]);
	});

	it("drops the did and both handles when a handle moves", async () => {
		const { events, sockets } = follow();
		const previous: MiniDoc = { ...ALICE, did: "did:plc:previous", handle: "alice.example" };
		const { fetchMock, ctx } = counting(ALICE, previous);
		await resolveMiniDoc(ctx, ALICE.did);
		await resolveMiniDoc(ctx, "alice.test");
		await expect(resolveMiniDoc(ctx, "alice.example")).resolves.toMatchObject(previous);
		await resolveMiniDoc(ctx, ALICE.did);
		await resolveMiniDoc(ctx, "alice.example");
		expect(fetchMock).toHaveBeenCalledTimes(3);

		const renamed = { ...ALICE, handle: "alice.example" };
		fetchMock.mockImplementation(async () => json(renamed));
		sockets[0].send({
			did: ALICE.did,
			kind: "identity",
			identity: { did: ALICE.did, handle: "alice.example" }
		});

		await expect(resolveMiniDoc(ctx, ALICE.did)).resolves.toMatchObject(renamed);
		await expect(resolveMiniDoc(ctx, "alice.test")).resolves.toMatchObject(renamed);
		await expect(resolveMiniDoc(ctx, "alice.example")).resolves.toMatchObject(renamed);
		expect(fetchMock).toHaveBeenCalledTimes(6);
		events.close();
	});

	it("drops an account's lookups when its status changes", async () => {
		const { events, sockets } = follow();
		const { fetchMock, ctx } = counting(ALICE);
		await resolveMiniDoc(ctx, "alice.test");
		sockets[0].send({ did: ALICE.did, kind: "account", account: { active: false } });
		await resolveMiniDoc(ctx, "alice.test");
		expect(fetchMock).toHaveBeenCalledTimes(2);
		events.close();
	});

	it("drops a repo when its manifest or refs change, not on other records", async () => {
		const { events, sockets } = follow();
		const fetchMock = vi.fn<Fetch>(async () => json(REPO));
		const ctx = createBobbinClient({ serviceUrl: "https://bobbin.test", fetch: fetchMock });
		await resolveRepoByName(ctx, ALICE.did, "kernel");

		const commit = (collection: string) =>
			sockets[0].send({ did: "did:plc:repo", kind: "commit", commit: { collection } });
		commit("org.tangled.track.ticket");
		await resolveRepoByName(ctx, ALICE.did, "kernel");
		expect(fetchMock).toHaveBeenCalledTimes(1);

		commit("org.tangled.git.refs");
		await resolveRepoByName(ctx, ALICE.did, "kernel");
		expect(fetchMock).toHaveBeenCalledTimes(2);
		events.close();
	});

	it("drops a lookup again once bobbin has had time to see the same event", async () => {
		vi.useFakeTimers();
		const { events, sockets } = follow();
		const { fetchMock, ctx } = counting(ALICE);
		await resolveMiniDoc(ctx, "alice.test");
		sockets[0].send({ did: ALICE.did, kind: "identity", identity: { handle: "alice.test" } });
		await resolveMiniDoc(ctx, "alice.test");
		expect(fetchMock).toHaveBeenCalledTimes(2);

		vi.advanceTimersByTime(10_000);
		await resolveMiniDoc(ctx, "alice.test");
		expect(fetchMock).toHaveBeenCalledTimes(3);
		events.close();
	});

	it("starts from an empty cache after a reconnect", async () => {
		vi.useFakeTimers();
		const { events, sockets } = follow();
		const { fetchMock, ctx } = counting(ALICE);
		await resolveMiniDoc(ctx, "alice.test");
		expect(events.connected).toBe(true);

		sockets[0].onclose?.();
		expect(events.connected).toBe(false);
		await resolveMiniDoc(ctx, "alice.test");
		expect(fetchMock).toHaveBeenCalledTimes(1);

		vi.advanceTimersByTime(1_000);
		expect(sockets).toHaveLength(2);
		sockets[1].onopen?.();
		expect(events.connected).toBe(true);
		await resolveMiniDoc(ctx, "alice.test");
		expect(fetchMock).toHaveBeenCalledTimes(2);
		events.close();
	});
});