Open History to see the last commit
1import { test } from "node:test";
2import assert from "node:assert/strict";
3import { rowChanges, HIGHLIGHT_MS } from "../lib/changes";
4test("numbers pulse independently across successive snapshots", () => {
5 const before = [
6 { item_id: 1, score: 10, comments: 3 },
7 { item_id: 2, score: 8, comments: 1 },
8 ];
9 const score = [{ ...before[0], score: 11 }, before[1]];
10 const first = rowChanges(
11 before,
12 score,
13 ["score", "comments"],
14 new Map(),
15 1000,
16 );
17 assert.deepEqual(first.get(1), {
18 fields: { score: 1000 },
19 entered: undefined,
20 moved: undefined,
21 });
22 assert.equal(first.has(2), false);
23 const counts = [{ ...score[0], comments: 4 }, before[1]];
24 const second = rowChanges(score, counts, ["score", "comments"], first, 1500);
25 assert.deepEqual(second.get(1)?.fields, { score: 1000, comments: 1500 });
26 const expired = rowChanges(counts, counts, [], second, 1000 + HIGHLIGHT_MS);
27 assert.deepEqual(expired.get(1)?.fields, { comments: 1500 });
28 assert.equal(
29 rowChanges(counts, counts, [], expired, 1500 + HIGHLIGHT_MS).size,
30 0,
31 );
32});
33test("arrivals, moves and removed rows are distinguished", () => {
34 const before = [
35 { item_id: 1, score: 1 },
36 { item_id: 2, score: 2 },
37 ];
38 const after = [{ item_id: 3, score: 3 }, before[1], before[0]];
39 const changes = rowChanges(before, after, ["score"], new Map(), 1000);
40 assert.equal(changes.get(3)?.entered, 1000);
41 assert.deepEqual(changes.get(3)?.fields, {});
42 assert.equal(changes.has(2), false);
43 assert.equal(changes.get(1)?.moved, 1000);
44 const removed = rowChanges(after, [after[1]], ["score"], changes, 1100);
45 assert.equal(removed.has(1), false);
46 assert.equal(removed.has(3), false);
47});