~james/core

ref: 452f17b75181d62d1d92016edecd38e1d70fecab core/web/src/lib/api/patchStream.test.ts -rw-r--r-- 3.2 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 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
import type { FileDiffMetadata } from "@pierre/diffs";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { getPatchStream, startPatchStream } from "#lib/api/patchStream.ts";

const streamPatchInWorker = vi.hoisted(() => vi.fn());

vi.mock("#lib/components/repo/diffRpc.ts", () => ({ streamPatchInWorker }));

const file = (name: string, prevName?: string) =>
	({ name, prevName, hunks: [] }) as unknown as FileDiffMetadata;

describe("startPatchStream", () => {
	beforeEach(() => {
		streamPatchInWorker.mockReset();
	});

	it("indexes every landed file by its name and by the name it had before", async () => {
		streamPatchInWorker.mockImplementation(
			async (_url: string, land: (files: FileDiffMetadata[]) => void) => {
				land([file("src/new.ts", "src/old.ts")]);
			}
		);

		const stream = startPatchStream("/patch");

		await expect(stream.done).resolves.toBe(true);
		expect(stream.byName.get("src/new.ts")).toBe(stream.byName.get("src/old.ts"));
	});

	it("reports a failed patch instead of throwing at whoever waits for it", async () => {
		streamPatchInWorker.mockRejectedValue(new Error("mirror is down"));

		await expect(startPatchStream("/patch").done).resolves.toBe(false);
	});

	it("drops batches that land after the reader walked away", async () => {
		let land: (files: FileDiffMetadata[]) => void = () => undefined;
		streamPatchInWorker.mockImplementation(
			(_url: string, listener: (files: FileDiffMetadata[]) => void) => {
				land = listener;
				return new Promise<void>(() => undefined);
			}
		);

		const stream = startPatchStream("/patch");
		const seen: FileDiffMetadata[][] = [];
		stream.subscribe((files) => seen.push(files));

		stream.stop();
		land([file("src/late.ts")]);

		expect(seen).toEqual([]);
		expect(stream.byName.size).toBe(0);
	});
});

describe("getPatchStream", () => {
	beforeEach(() => {
		streamPatchInWorker.mockReset();
		streamPatchInWorker.mockImplementation(() => new Promise<void>(() => undefined));
	});

	it("shares one download between every caller asking for the same patch", () => {
		expect(getPatchStream("/shared")).toBe(getPatchStream("/shared"));
	});

	it("keeps the last couple of patches and stops the ones before them", () => {
		const first = getPatchStream("/first");
		const stopped = vi.spyOn(first, "stop");

		getPatchStream("/second");
		getPatchStream("/third");

		expect(stopped).toHaveBeenCalled();
		expect(getPatchStream("/first")).not.toBe(first);
	});

	it("drops an oversized patch once a newer one is requested, unless a view listens to it", async () => {
		const sized = (name: string, lines: number) =>
			({ ...file(name), unifiedLineCount: lines }) as FileDiffMetadata;
		let landHuge: (files: FileDiffMetadata[]) => void = () => undefined;
		streamPatchInWorker.mockImplementationOnce(
			(_url: string, land: (files: FileDiffMetadata[]) => void) => {
				landHuge = land;
				return new Promise<void>(() => undefined);
			}
		);
		const huge = getPatchStream("/huge");
		landHuge([sized("big.lock", 50_000)]);
		const listened = getPatchStream("/listened");
		listened.subscribe(() => undefined);
		getPatchStream("/small");

		expect(getPatchStream("/huge")).not.toBe(huge);
		expect(getPatchStream("/listened")).toBe(listened);
	});
});