Open History to see the last commit
1import { test } from "node:test";
2import assert from "node:assert/strict";
3import { Pool } from "pg";
4import { spreadChanges, type PendingChange } from "../ingest/smoothing";
5import { itemRecord } from "../ingest/store";
6import { commentRecords } from "../ingest/thread-store";
7import { runSession } from "../ingest/session";
8
9const item = (id: number, due_at = -1): PendingChange => ({
10 key: `item:${id}`,
11 due_at,
12 received_at: 1000,
13 revision: 1,
14 payload: {
15 kind: "item",
16 record: itemRecord({ id, title: `Story ${id}` }, ""),
17 },
18});
19
20test("smoothing spreads a burst across its window without postponing pending work", () => {
21 const pending = item(9, 2500);
22 const changes = spreadChanges(
23 [item(4), item(2), pending, item(1), item(3)],
24 1000,
25 28000,
26 );
27 assert.deepEqual(
28 changes
29 .filter((job) => job.key !== "item:9")
30 .sort((a, b) => a.key.localeCompare(b.key))
31 .map((job) => job.due_at),
32 [6600, 12200, 17800, 23400],
33 );
34 assert.equal(pending.due_at, 2500);
35 assert.equal(spreadChanges([item(1), item(2)], 1000, 0)[0].due_at, 1000);
36});
37
38test("new reply edits wait for their tree; existing comment edits are independent", () => {
39 const thread = {
40 story: { id: 1, title: "One" },
41 receivedAt: 1000,
42 truncated: false,
43 rows: [
44 {
45 item: { id: 11, text: "Reply" },
46 parentId: 1,
47 path: "000001",
48 depth: 0,
49 },
50 ],
51 };
52 const edit: PendingChange = {
53 key: "comment:1:11",
54 due_at: -1,
55 received_at: 1000,
56 revision: 1,
57 payload: {
58 kind: "comment",
59 record: commentRecords(thread, "")[0],
60 afterStructure: true,
61 },
62 };
63 const existing = {
64 ...edit,
65 key: "comment:1:12",
66 payload: { ...edit.payload, afterStructure: false },
67 } as PendingChange;
68 const tree: PendingChange = {
69 key: "thread:1",
70 due_at: 20000,
71 received_at: 1000,
72 revision: 1,
73 payload: { kind: "thread", thread },
74 };
75 spreadChanges([edit, existing, tree], 1000, 28000);
76 assert.equal(edit.due_at, 20000);
77 assert.ok(existing.due_at < 20000);
78 assert.equal(tree.due_at, 20000);
79});
80
81test("invalid smoothing config fails before acquiring the database", async () => {
82 const pool = new Pool();
83 try {
84 for (const smoothingMs of [NaN, Infinity, -1, 1.5, 60001])
85 await assert.rejects(
86 runSession(pool, { smoothingMs }),
87 /Invalid ingestion smoothing/,
88 );
89 } finally {
90 await pool.end();
91 }
92});