A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.

9 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-browser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-browser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-browser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 toolbar: Back and Forward are greyed out when there's nowhere to go cc08c7a 1 week, 2 days ago History
cards.test.ts · 7.1 KB · 116 lines
1import { describe, expect, it } from "vitest";
2import { readFileSync } from "node:fs";
3import { buildCardsHtml, mergePosts, readerSource, shortStats, shortTime, validatePosts, validateViewRule, type Post, type ViewRule } from "../src/cards";
4
5// X's view rule, as published in the askrobots/x-cards plugin (no card view is built into the browser).
6const X_VIEW_RULE = JSON.parse(readFileSync(new URL("./fixtures/x-cards.json", import.meta.url), "utf8")) as ViewRule;
7
8const post = (over: Partial<Post> = {}): Post => ({
9 link: "https://x.com/a/status/1", author: "A", handle: "@a", avatar: "", time: "", timeText: "2h", text: "hi", images: [], stats: "", context: "", promoted: false, ...over,
10});
11
12describe("view rules are data", () => {
13 it("accepts X's rule", () => {
14 expect(validateViewRule(X_VIEW_RULE).ok).toBe(true);
15 });
16
17 it("refuses anything but selectors, known fields and plain takes", () => {
18 const bad = (fields: unknown) => validateViewRule({ ...X_VIEW_RULE, fields }) as { ok: boolean; error?: string };
19 expect(bad({ text: { sel: "div", take: "innerHTML" } }).ok).toBe(false);
20 expect(bad({ script: { sel: "div", take: "text" } }).error).toMatch(/unknown field script/);
21 expect(bad({ text: { sel: "div", take: "text", code: "alert(1)" } }).error).toMatch(/has code/);
22 expect(bad({ text: { sel: "div", take: "text", match: "(" } }).error).toMatch(/bad match/);
23 expect(validateViewRule({ ...X_VIEW_RULE, post: "" }).ok).toBe(false);
24 expect(validateViewRule({ ...X_VIEW_RULE, sites: ["evil site"] }).ok).toBe(false);
25 });
26
27 it("the reader carries the rule as JSON only", () => {
28 const src = readerSource(X_VIEW_RULE);
29 expect(src).toContain(JSON.stringify(X_VIEW_RULE));
30 expect(() => new Function(`return ${src.replace(/\)\(\{[\s\S]*\}\)$/, ")")}`)).not.toThrow();
31 });
32});
33
34describe("what the page returns is untrusted", () => {
35 it("keeps only posts linking to the rule's own sites over https, and plain https images", () => {
36 const got = validatePosts({ posts: [
37 { link: "https://x.com/a/status/1", text: "ok", images: ["https://pbs.twimg.com/media/a.jpg", "javascript:alert(1)", "http://pbs.twimg.com/b.jpg"] },
38 { link: "https://evil.test/a/status/2", text: "elsewhere" },
39 { link: "javascript:alert(1)", text: "script" },
40 { link: "http://x.com/a/status/3", text: "plain http" },
41 "not an object",
42 ] }, X_VIEW_RULE);
43 expect(got.map((p) => p.text)).toEqual(["ok"]);
44 expect(got[0]!.images).toEqual(["https://pbs.twimg.com/media/a.jpg"]);
45 });
46
47 it("clamps sizes and keeps a handle only when it looks like one", () => {
48 const [p] = validatePosts({ posts: [{ link: "https://x.com/a/status/1", text: "x".repeat(9000), handle: "@a <b>", author: "Name\n@a\n·", time: "2026-11-23T12:00:00.000Z" }] }, X_VIEW_RULE);
49 expect(p!.text.length).toBe(4000);
50 expect(p!.handle).toBe("");
51 expect(p!.author).toBe("Name");
52 expect(p!.time).toBe("2026-11-23T12:00:00.000Z");
53 });
54
55 it("merges by the post's address, keeping order and the fuller copy", () => {
56 const m = new Map<string, Post>();
57 expect(mergePosts(m, [post(), post({ link: "https://x.com/b/status/2" })])).toBe(2);
58 expect(mergePosts(m, [post({ link: "https://x.com/a/status/1?s=20", images: ["https://pbs.twimg.com/media/a.jpg"] })])).toBe(0);
59 expect([...m.values()].map((p) => p.images.length)).toEqual([1, 0]);
60 });
61});
62
63describe("the card page", () => {
64 const page = (posts: Post[]) => buildCardsHtml({ rule: X_VIEW_RULE, source: "https://x.com/home", title: "Home / X", posts, moreUrl: "dbb-cards://0123456789abcdef01/more", collectedAt: new Date("2026-11-23T15:00:00Z") });
65
66 it("escapes everything and has no scripts", () => {
67 const html = page([post({ text: `<script>alert(1)</script><img src=x onerror=alert(1)>`, author: `"><svg onload=alert(1)>` })]);
68 expect(html).not.toMatch(/<script|<svg|<img src=x/i); // (the words survive as escaped text, never as markup)
69 expect(html).toContain("&lt;script&gt;");
70 expect(html).toMatch(/Content-Security-Policy" content="default-src 'none'; img-src https:/);
71 });
72
73 it("hides promoted posts behind a line that says how many", () => {
74 const html = page([post(), post({ link: "https://x.com/ad/status/9", promoted: true })]);
75 expect(html).toMatch(/1 post from x\.com/);
76 expect(html).toMatch(/<details><summary>1 promoted post hidden/);
77 });
78
79 it("offers More posts only while the tab they came from is open", () => {
80 expect(page([post()])).toMatch(/class="more" href="dbb-cards:\/\/0123456789abcdef01\/more"/);
81 const closed = buildCardsHtml({ rule: X_VIEW_RULE, source: "https://x.com/home", title: "", posts: [post()], moreUrl: "", collectedAt: new Date() });
82 expect(closed).not.toMatch(/\/more"/);
83 expect(closed).toMatch(/tab these came from is closed/);
84 });
85
86 it("lays cards out as masonry: each to the shortest column, one layout per column count", async () => {
87 const { masonry, estimateHeight, COLUMN_COUNTS } = await import("../src/cards");
88 const cols = masonry([100, 300, 100, 100], 2, (x) => x);
89 expect(cols).toEqual([[100, 100, 100], [300]]);
90 expect(estimateHeight(post({ text: "x".repeat(2000) }))).toBeLessThan(estimateHeight(post({ text: "short", images: ["https://pbs.twimg.com/media/a.jpg"] })) + 200); // long text is cut to ten lines
91 const html = page([post(), post({ link: "https://x.com/b/status/2" })]);
92 for (const n of COLUMN_COUNTS) expect(html).toContain(`class="m m${n}"`);
93 });
94
95 it("puts each More posts read under its own line, the newest one the #new anchor", () => {
96 const html = buildCardsHtml({ rule: X_VIEW_RULE, source: "https://x.com/home", title: "", posts: [post(), post({ link: "https://x.com/b/status/2" }), post({ link: "https://x.com/c/status/3" })], moreUrl: "", batches: [{ from: 1, at: new Date() }, { from: 2, at: new Date() }], collectedAt: new Date() });
97 expect(html.match(/class="batch"/g)?.length).toBe(2);
98 expect(html.match(/id="new"/g)?.length).toBe(1);
99 expect(html.indexOf('id="new"')).toBeGreaterThan(html.lastIndexOf('class="batch"') - 1);
100 });
101
102 it("says what a More posts read found when nothing new came, at the #new anchor", () => {
103 const html = buildCardsHtml({ rule: X_VIEW_RULE, source: "https://x.com/home", title: "", posts: [post(), post({ link: "https://x.com/b/status/2" })], moreUrl: "", batches: [{ from: 1, at: new Date() }], note: "More posts scrolled 12 screens and read 30 posts, all already here <b>", collectedAt: new Date() });
104 expect(html).toMatch(/<p class="note" id="new">More posts scrolled 12 screens and read 30 posts, all already here &lt;b&gt;<\/p>/);
105 expect(html.match(/id="new"/g)?.length).toBe(1);
106 });
107
108 it("shortens counts and times the way X does", () => {
109 expect(shortStats("61 replies, 223 reposts, 1554 likes, 78 bookmarks, 254134 views")).toBe("61 replies · 223 reposts · 1.6K likes · 254K views");
110 const now = new Date("2026-11-23T15:00:00Z");
111 expect(shortTime("2026-11-23T14:59:30Z", now)).toBe("now");
112 expect(shortTime("2026-11-23T14:30:00Z", now)).toBe("30m");
113 expect(shortTime("2026-11-23T10:00:00Z", now)).toBe("5h");
114 expect(shortTime("2026-11-20T10:00:00Z", now)).toBe("Nov 20");
115 });
116});