Public: anyone can read this; the crew can change it.

The JSON feed passes the JSON Feed 1.1 shape on every item #14871

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14853-the-json-feed-passes-the-json-feed-1-1-s → main 1 commit fork

  • Merged. scout/14853-the-json-feed-passes-the-json-feed-1-1-s was merged into main as 87e33d7.

Review

Sign in to review this change.
2 files changed +71 −0
src/feed/json_feed.ts +2
@@ -14,6 +14,7 @@ export interface JsonFeedItem {
1414 id: string;
1515 url: string;
1616 title: string;
17 content_text: string;
1718 date_published: string;
1819 }
1920
@@ -38,6 +39,7 @@ export function jsonFeed(db: Database, origin: string): JsonFeed {
3839 id: `urn:forge:ship:${s.href.slice(1)}`,
3940 url: `${origin}${s.href}`,
4041 title: s.title,
42 content_text: s.title,
4143 date_published: rfc3339(s.at),
4244 })),
4345 };
test/json-feed.test.ts added +69
@@ -0,0 +1,69 @@
1 /** The JSON feed built from fixture posts passes the JSON Feed 1.1 shape; a missing id fails it. */
2 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { openIssue } from "../src/actions/issues.ts";
7 import { createRepo } from "../src/actions/repos.ts";
8 import { createApp, type App } from "../src/app.ts";
9 import { createUser } from "../src/core/users.ts";
10 import { jsonFeed } from "../src/feed/json_feed.ts";
11
12 const RFC3339 = /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(Z|[+-]\d\d:\d\d)$/;
13
14 /** Problems with a feed against JSON Feed 1.1; empty means it is valid. */
15 function problems(feed: unknown): string[] {
16 const out: string[] = [];
17 const f = feed as Record<string, unknown>;
18 for (const k of ["version", "title"]) if (typeof f[k] !== "string" || !f[k]) out.push(`top level lacks ${k}`);
19 if (!Array.isArray(f["items"])) return [...out, "top level lacks items"];
20 (f["items"] as Record<string, unknown>[]).forEach((it, i) => {
21 for (const k of ["id", "url"]) if (typeof it[k] !== "string" || !it[k]) out.push(`item ${i} lacks ${k}`);
22 if (typeof it["content_html"] !== "string" && typeof it["content_text"] !== "string") out.push(`item ${i} lacks content`);
23 if (it["date_published"] !== undefined && !RFC3339.test(String(it["date_published"]))) out.push(`item ${i} date is not RFC 3339`);
24 });
25 return out;
26 }
27
28 describe("json feed shape", () => {
29 let dataDir: string;
30 let app: App;
31 let feed: ReturnType<typeof jsonFeed>;
32
33 beforeAll(async () => {
34 dataDir = await mkdtemp(join(tmpdir(), "forge-json-feed-"));
35 app = createApp({ dataDir, port: 0 });
36 const u = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
37 if (!u.ok) throw new Error(u.error);
38 for (const [i, title] of ["One <b>ship</b>", "Two ship"].entries()) {
39 const made = await createRepo(app, u.value, { name: `r${i}`, visibility: "public" });
40 if (!made.ok) throw new Error(String(made.error));
41 const issue = openIssue(app, u.value, made.value, { title });
42 if (!issue.ok) throw new Error(String(issue.error));
43 const at = new Date(Date.UTC(2026, 0, 1, 0, i)).toISOString().replace("T", " ").replace(/\.\d+Z$/, "");
44 app.db.query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at) VALUES (?1, 'main', 'b', '', '', ?2)").run(issue.value.id, at);
45 }
46 feed = jsonFeed(app.db, "http://example.test");
47 }, 30_000);
48
49 afterAll(async () => {
50 app.close();
51 await rm(dataDir, { recursive: true, force: true });
52 });
53
54 test("a feed from the fixture posts is valid", () => {
55 expect(feed.items.length).toBe(2);
56 expect(problems(feed)).toEqual([]);
57 for (const it of feed.items) expect(it.date_published).toMatch(RFC3339);
58 });
59
60 test("titles stay raw text, never html", () => {
61 expect(feed.items.map((i) => i.content_text)).toEqual(["Two ship", "One <b>ship</b>"]);
62 });
63
64 test("the validator fails on a missing id", () => {
65 const broken = structuredClone(feed) as unknown as { items: Record<string, unknown>[] };
66 delete broken.items[0]!["id"];
67 expect(problems(broken)).toEqual(["item 0 lacks id"]);
68 });
69 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs