Skip to content
Source
Sign inSign up
samwillis/hackernews-realtime
Open History to see the last commit

thread-load.test.ts

39 lines1.4 KB

1import { test } from "node:test";
2import assert from "node:assert/strict";
3import { firstComments } from "../ingest/thread-load";
4​
5test("first render reads one small parallel batch rather than a whole discussion", async () => {
6 const requests: string[] = [];
7 const snapshot = await firstComments(1, async (path) => {
8 requests.push(path);
9 const id = Number(path.slice(5));
10 return id === 1
11 ? {
12 id,
13 type: "story",
14 descendants: 200,
15 kids: Array.from({ length: 50 }, (_, i) => i + 2),
16 }
17 : { id, type: "comment", text: `Comment ${id}`, kids: [id + 1000] };
18 });
19 assert.equal(requests.length, 21);
20 assert.equal(snapshot.rows.length, 20);
21 assert.equal(snapshot.complete, false);
22 assert.equal(snapshot.rows[19].path, "000020");
23 assert.ok(!requests.includes("item/1002"));
24});
25​
26test("empty/small threads complete immediately, including deleted placeholders", async () => {
27 const empty = await firstComments(1, async () => ({ id: 1, type: "story" }));
28 assert.equal(empty.complete, true);
29 assert.deepEqual(empty.rows, []);
30 const small = await firstComments(1, async (path) =>
31 path === "item/1" ? { id: 1, type: "story", kids: [2] } : null,
32 );
33 assert.equal(small.complete, true);
34 assert.equal(small.rows[0].item.deleted, true);
35 await assert.rejects(
36 firstComments(1, async () => null),
37 /unavailable/,
38 );
39});