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

comments.test.ts

106 lines3.3 KB

1import { test } from "node:test";
2import assert from "node:assert/strict";
3import { createElement } from "react";
4import { renderToStaticMarkup } from "react-dom/server";
5import { commentContent } from "../lib/comment-content";
6import {
7 parseStoryId,
8 sortComments,
9 THREAD_LIMIT,
10 type Comment,
11} from "../lib/comments";
12import { orderedComments, selectComments } from "../ingest/threads";
13const render = (html: string) =>
14 renderToStaticMarkup(createElement("div", null, commentContent(html)));
15test("HN comment formatting decodes entities and drops unsafe elements and links", () => {
16 const markup = render(
17 '<p>Hello &amp; <i>world</i><br><a href="https://example.com/?a=1&amp;b=2" onclick="alert(1)">link</a></p><pre><code>&lt;tag&gt;</code></pre>',
18 );
19 assert.match(markup, /Hello &amp; <i>world<\/i>/);
20 assert.match(markup, /href="https:\/\/example.com\/\?a=1&amp;b=2"/);
21 assert.match(markup, /&lt;tag&gt;/);
22 const hostile = render(
23 '<script>alert(1)</script><style>bad</style><img src=x onerror=bad><a href="javascript:alert(1)">bad link</a><a href="data:text/html,bad">data</a><a href="https://user:pass@example.com">credential</a><p style="color:red">text</p>',
24 );
25 assert.doesNotMatch(hostile, /script|style|img|href=|onerror|onclick|alert/);
26 assert.match(hostile, /<span>bad link<\/span>/);
27 assert.match(
28 render('<a href="item?id=1">local</a>'),
29 /href="https:\/\/news.ycombinator.com\/item\?id=1"/,
30 );
31});
32test("comment tree preserves HN sibling order, depth and deleted parent placeholders", () => {
33 const root = { id: 1, kids: [3, 2] };
34 const items = new Map([
35 [2, { id: 2 }],
36 [3, { id: 3, deleted: true, kids: [4] }],
37 [4, { id: 4, kids: [1, 3] }],
38 ]);
39 const rows = orderedComments(root, items);
40 assert.deepEqual(
41 rows.map((row) => [row.item.id, row.parentId, row.depth, row.path]),
42 [
43 [3, 1, 0, "000001"],
44 [4, 3, 1, "000001.000001"],
45 [2, 1, 0, "000002"],
46 ],
47 );
48 const reordered = orderedComments({ ...root, kids: [2, 3] }, items);
49 assert.deepEqual(
50 reordered.map((row) => row.item.id),
51 [2, 3, 4],
52 );
53});
54test("thread traversal is bounded, handles cycles and rejects malformed story IDs", () => {
55 const root = {
56 id: 1,
57 kids: Array.from({ length: THREAD_LIMIT + 10 }, (_, i) => i + 2),
58 };
59 assert.equal(selectComments(root, new Map()).ids.length, THREAD_LIMIT);
60 assert.equal(selectComments(root, new Map()).truncated, true);
61 assert.deepEqual(
62 selectComments(
63 { id: 1, kids: [1, 2, 2] },
64 new Map([[2, { id: 2, kids: [1, 2, 3] }]]),
65 ).ids,
66 [2, 3],
67 );
68 for (const bad of [
69 null,
70 undefined,
71 "01",
72 "-1",
73 "0",
74 "1e3",
75 "1;select",
76 "2147483648",
77 ["1"],
78 ])
79 assert.throws(() => parseStoryId(bad));
80 assert.equal(parseStoryId("2147483647"), 2147483647);
81 assert.deepEqual(
82 sortComments(
83 [
84 [3, "000002"],
85 [2, "000001.000001"],
86 [1, "000001"],
87 ].map(
88 ([id, path]) =>
89 ({
90 item_id: Number(id),
91 path: String(path),
92 story_id: 1,
93 parent_id: 1,
94 depth: 0,
95 author: null,
96 created: 0,
97 text: "",
98 deleted: false,
99 dead: false,
100 committed_at: "",
101 }) satisfies Comment,
102 ),
103 ).map((row) => row.item_id),
104 [1, 2, 3],
105 );
106});