Public: anyone can read this; the crew can change it.
A public repository's ships and a person's public posts each have an Atom feed, linked from the page #12451
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/12407-a-public-repository-s-ships-and-a-person → main 5 commits fork
11 files
changed +395
−0
docs/features/atom.md added +10
| @@ -0,0 +1,10 @@ | ||
| 1 | # Atom feeds | |
| 2 | ||
| 3 | Follow with a reader, no account needed. Each page that has a feed advertises it with `<link rel="alternate" type="application/atom+xml">`. | |
| 4 | ||
| 5 | - `/changelog.atom`, `/explore.atom`: what went live; the ranked public apps. | |
| 6 | - `/{owner}/{name}/activity.atom`: a repository's activity. | |
| 7 | - `/{owner}/{name}/ships.atom`: the newest 30 merged pulls of a repository (title, link, merge time, author). Linked from the repository page. | |
| 8 | - `/{owner}.atom`: the newest 30 public posts of a person (issues they opened on repositories a stranger may read). Linked from their profile. | |
| 9 | ||
| 10 | The two newest feeds are what a stranger sees: a private repository's ships are 404 to anyone who cannot read it, and a private repository's posts are never listed. Text is XML-escaped, answers carry an `ETag` (`If-None-Match` gets 304) and the usual security headers. Code: `src/http/atom_routes.ts`. The person feed reads `issue_author_created`. |
src/core/migration-pins/20261114120000-posts-by-author.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | 5c5f3930e7c5130c09cfea2e839b347ce43a211a3a642334d68feb894cc26e62 |
src/core/migrations/20261114120000-posts-by-author.ts added +5
| @@ -0,0 +1,5 @@ | ||
| 1 | // A person's Atom feed (/:owner.atom) reads the newest issues one person | |
| 2 | // opened; issue_author alone would sort them. Additive: one index build. | |
| 3 | export default ` | |
| 4 | CREATE INDEX IF NOT EXISTS issue_author_created ON issue(author_id, kind, created_at DESC); | |
| 5 | `; |
src/http/atom_routes.ts added +148
| @@ -0,0 +1,148 @@ | ||
| 1 | /** | |
| 2 | * Two more Atom feeds, for people who follow with a reader: | |
| 3 | * `GET /:owner/:name/ships.atom` (a repository's merged pulls) and | |
| 4 | * `GET /:owner.atom` (a person's public posts: the issues they opened on | |
| 5 | * repositories a stranger may read). Both are what a stranger sees, whoever | |
| 6 | * asks, so a private repository is 404 and a private post is never listed. | |
| 7 | */ | |
| 8 | ||
| 9 | import type { Database } from "bun:sqlite"; | |
| 10 | import { getIssuesByIds, type Issue } from "../core/issues.ts"; | |
| 11 | import { canRead, getRepo, getRepoById, type Repo } from "../core/repos.ts"; | |
| 12 | import { notePhase, takeSessionTime, timePhase } from "../core/route_timing.ts"; | |
| 13 | import { getUserByName, hiddenFrom } from "../core/users.ts"; | |
| 14 | import { plainTitle } from "../story/gather.ts"; | |
| 15 | import { holds, stamp, xml } from "../web/feeds.ts"; | |
| 16 | import { STARTED } from "../web/started.ts"; | |
| 17 | import { ownerProfile } from "../web/pages/org.ts"; | |
| 18 | import type { Ctx, Handler } from "./context.ts"; | |
| 19 | import { notFound } from "./respond.ts"; | |
| 20 | import type { Router } from "./router.ts"; | |
| 21 | ||
| 22 | export const ATOM_ENTRIES = 30; | |
| 23 | const HEADERS = { "content-type": "application/atom+xml; charset=utf-8", "cache-control": "public, max-age=300" }; | |
| 24 | ||
| 25 | interface Item { | |
| 26 | id: string; | |
| 27 | title: string; | |
| 28 | at: string; | |
| 29 | author: string; | |
| 30 | link: string; | |
| 31 | } | |
| 32 | ||
| 33 | /** The newest merged pulls of one repository, newest first; a sealed report's pull is never one. */ | |
| 34 | export function shippedPulls(db: Database, repoId: string, limit = ATOM_ENTRIES): { issue: Issue; at: string }[] { | |
| 35 | const rows = db | |
| 36 | .query<{ issue_id: string; merged_at: string }, [string, number]>( | |
| 37 | `SELECT pull.issue_id, pull.merged_at FROM pull JOIN issue ON issue.id = pull.issue_id | |
| 38 | WHERE issue.repo_id = ?1 AND pull.merged_at IS NOT NULL AND issue.sealed = 0 | |
| 39 | ORDER BY pull.merged_at DESC LIMIT ?2`, | |
| 40 | ) | |
| 41 | .all(repoId, limit); | |
| 42 | const issues = getIssuesByIds(db, rows.map((r) => r.issue_id)); | |
| 43 | return rows.flatMap((r) => { | |
| 44 | const issue = issues.get(r.issue_id); | |
| 45 | return issue === undefined ? [] : [{ issue, at: r.merged_at }]; | |
| 46 | }); | |
| 47 | } | |
| 48 | ||
| 49 | /** The newest unsealed issues one person opened on public repositories; callers check `canRead`. */ | |
| 50 | export function postsBy(db: Database, authorId: string, limit: number): Issue[] { | |
| 51 | const ids = db | |
| 52 | .query<{ id: string }, [string, number]>( | |
| 53 | `SELECT issue.id FROM issue JOIN repo ON repo.id = issue.repo_id | |
| 54 | WHERE issue.author_id = ?1 AND issue.kind = 'issue' AND issue.sealed = 0 AND repo.visibility = 'public' | |
| 55 | ORDER BY issue.created_at DESC LIMIT ?2`, | |
| 56 | ) | |
| 57 | .all(authorId, limit) | |
| 58 | .map((r) => r.id); | |
| 59 | const issues = getIssuesByIds(db, ids); | |
| 60 | return ids.flatMap((id) => (issues.has(id) ? [issues.get(id)!] : [])); | |
| 61 | } | |
| 62 | ||
| 63 | function render(ctx: Ctx, f: { cache?: string | undefined; vary?: boolean; path: string; html: string; title: string; subtitle: string; items: Item[] }): Response { | |
| 64 | const origin = ctx.url.origin; | |
| 65 | const etag = `W/"${Bun.hash(JSON.stringify([f.title, f.subtitle, f.items])).toString(36)}"`; | |
| 66 | const headers: Record<string, string> = { ...HEADERS, etag }; | |
| 67 | if (f.cache !== undefined) headers["cache-control"] = f.cache; | |
| 68 | if (f.vary === true) headers["vary"] = "authorization, cookie"; | |
| 69 | if (holds(ctx.request.headers.get("if-none-match"), etag)) return new Response(null, { status: 304, headers }); | |
| 70 | const entries = f.items.map( | |
| 71 | (i) => ` <entry> | |
| 72 | <id>${xml(i.id)}</id> | |
| 73 | <title>${xml(i.title)}</title> | |
| 74 | <updated>${xml(stamp(i.at))}</updated> | |
| 75 | <author><name>${xml(i.author)}</name></author> | |
| 76 | <link rel="alternate" type="text/html" href="${xml(i.link)}"/> | |
| 77 | </entry>`, | |
| 78 | ); | |
| 79 | const body = `<?xml version="1.0" encoding="utf-8"?> | |
| 80 | <feed xmlns="http://www.w3.org/2005/Atom"> | |
| 81 | <id>${xml(origin + f.path)}</id> | |
| 82 | <title>${xml(f.title)}</title> | |
| 83 | <subtitle>${xml(f.subtitle)}</subtitle> | |
| 84 | <updated>${xml(stamp(f.items[0]?.at ?? STARTED))}</updated> | |
| 85 | <link rel="self" type="application/atom+xml" href="${xml(origin + f.path)}"/> | |
| 86 | <link rel="alternate" type="text/html" href="${xml(origin + f.html)}"/> | |
| 87 | <generator>forge</generator> | |
| 88 | ${entries.join("\n")} | |
| 89 | </feed> | |
| 90 | `; | |
| 91 | return new Response(body, { headers }); | |
| 92 | } | |
| 93 | ||
| 94 | /** `GET /:owner/:name/ships.atom`. Read as the asker: a private repository is 404 to a stranger. */ | |
| 95 | export function shipsAtom(ctx: Ctx): Response { | |
| 96 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 97 | const repo = getRepo(ctx.app.db, ctx.params["owner"]!, ctx.params["name"]!); | |
| 98 | if (!repo || !canRead(ctx.app.db, repo, ctx.auth.user)) return notFound(); | |
| 99 | const slug = `${repo.ownerName}/${repo.name}`; | |
| 100 | const items = timePhase(ctx.request, "ships", () => shippedPulls(ctx.app.db, repo.id)).map(({ issue, at }) => ({ | |
| 101 | id: `urn:forge:ship:${slug}#${issue.number}`, | |
| 102 | title: plainTitle(issue.title), | |
| 103 | at, | |
| 104 | author: issue.originAuthor ?? issue.authorName, | |
| 105 | link: `${ctx.url.origin}/${slug}/pulls/${issue.number}`, | |
| 106 | })); | |
| 107 | return render(ctx, { cache: repo.visibility === "public" ? undefined : "private, no-store", vary: true, path: `/${slug}/ships.atom`, html: `/${slug}`, title: `${slug} ships`, subtitle: repo.description === "" ? "What shipped" : repo.description, items }); | |
| 108 | } | |
| 109 | ||
| 110 | /** `GET /:owner.atom`. Only what a stranger could read; the asker changes nothing. */ | |
| 111 | export async function personAtom(ctx: Ctx): Promise<Response> { | |
| 112 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 113 | // An account literally named "x.atom" keeps its profile; the feed is for names without one. | |
| 114 | const whole = getUserByName(ctx.app.db, `${ctx.params["owner"]}.atom`); | |
| 115 | if (whole !== undefined) return ownerProfile({ ...ctx, params: { ...ctx.params, owner: whole.username } }); | |
| 116 | const owner = getUserByName(ctx.app.db, ctx.params["owner"]!); | |
| 117 | if (owner === undefined) return notFound(); | |
| 118 | if (hiddenFrom(owner, undefined)) return notFound(); | |
| 119 | const repos = new Map<string, Repo | undefined>(); | |
| 120 | const readable = (id: string): Repo | undefined => { | |
| 121 | if (!repos.has(id)) { | |
| 122 | const r = getRepoById(ctx.app.db, id); | |
| 123 | repos.set(id, r !== undefined && canRead(ctx.app.db, r, undefined) ? r : undefined); | |
| 124 | } | |
| 125 | return repos.get(id); | |
| 126 | }; | |
| 127 | const items = timePhase(ctx.request, "posts", () => | |
| 128 | postsBy(ctx.app.db, owner.id, ATOM_ENTRIES) | |
| 129 | .flatMap((p) => { | |
| 130 | const repo = readable(p.repoId); | |
| 131 | return repo === undefined ? [] : [{ p, repo }]; | |
| 132 | }) | |
| 133 | .slice(0, ATOM_ENTRIES) | |
| 134 | .map(({ p, repo }) => ({ | |
| 135 | id: `urn:forge:post:${repo.ownerName}/${repo.name}#${p.number}`, | |
| 136 | title: p.title, | |
| 137 | at: p.createdAt, | |
| 138 | author: owner.username, | |
| 139 | link: `${ctx.url.origin}/${repo.ownerName}/${repo.name}/issues/${p.number}`, | |
| 140 | })), | |
| 141 | ); | |
| 142 | return render(ctx, { path: `/${owner.username}.atom`, html: `/${owner.username}`, title: `${owner.displayName || owner.username} (@${owner.username})`, subtitle: "Public posts", items }); | |
| 143 | } | |
| 144 | ||
| 145 | export function atomRoutes(router: Router<Handler>): void { | |
| 146 | router.add("GET", "/:owner.atom", personAtom); | |
| 147 | router.add("GET", "/:owner/:name/ships.atom", shipsAtom); | |
| 148 | } |
src/http/route_marks.ts +1
| @@ -22,6 +22,7 @@ export const HEADS: Readonly<Record<string, Mark | "mixed">> = { | ||
| 22 | 22 | "/-": "open", |
| 23 | 23 | "/.well-known": "open", |
| 24 | 24 | "/:owner": "open", |
| 25 | "/:owner.atom": "open", | |
| 25 | 26 | "/__forge": "open", |
| 26 | 27 | "/_funnel": "open", |
| 27 | 28 | "/_measure": "open", |
src/http/server.ts +4
| @@ -47,6 +47,7 @@ import { scriptErrorSubmit } from "./script_errors.ts"; | ||
| 47 | 47 | import { stripeHook } from "./stripe_hook.ts"; |
| 48 | 48 | import { settingsRoutes } from "./settings_routes.ts"; |
| 49 | 49 | import { pluginRoutes } from "./plugin_routes.ts"; |
| 50 | import { atomRoutes } from "./atom_routes.ts"; | |
| 50 | 51 | import { pinnedRoutes } from "./pinned_routes.ts"; |
| 51 | 52 | import { boardsAsk, boardsPage } from "../web/pages/boards.ts"; |
| 52 | 53 | import { boardNew, boardPin, boardPost, pinPost } from "../web/pages/boards-edit.ts"; |
| @@ -1531,6 +1532,9 @@ export function buildRouter(): Router<Handler> { | ||
| 1531 | 1532 | router.add("POST", "/:owner/-/teams/:team/delete", teamDelete); |
| 1532 | 1533 | router.add("POST", "/:owner/-/subscriptions", orgSubscriptionsSave); |
| 1533 | 1534 | |
| 1535 | // Atom feeds of a repository's ships and a person's posts, before `/:owner` can take them. | |
| 1536 | atomRoutes(router); | |
| 1537 | ||
| 1534 | 1538 | // Owner profile (a person or org) is a single segment — after all literals. |
| 1535 | 1539 | router.add("GET", "/:owner", ownerProfile); |
| 1536 | 1540 |
src/web/layout.ts +5
| @@ -122,6 +122,8 @@ export interface PageOptions { | ||
| 122 | 122 | siteName?: string; |
| 123 | 123 | /** This page's feed, advertised in the head so a reader can find it. */ |
| 124 | 124 | feed?: { href: string; title: string }; |
| 125 | /** Further feeds of this page (a repository's ships beside its activity). */ | |
| 126 | moreFeeds?: { href: string; title: string }[]; | |
| 125 | 127 | /** This page's JSON twin, advertised in the head for agents. */ |
| 126 | 128 | json?: { href: string; title: string }; |
| 127 | 129 | /** Structured data (JSON-LD) for the head, already escaped by `safeJson`. */ |
| @@ -852,6 +854,9 @@ export function page(options: PageOptions, status = 200): Response { | ||
| 852 | 854 | href="${f.href}" |
| 853 | 855 | />`, |
| 854 | 856 | )} |
| 857 | ${(options.moreFeeds ?? []).map( | |
| 858 | (f) => html`<link rel="alternate" type="application/atom+xml" title="${f.title}" href="${f.href}" />`, | |
| 859 | )} | |
| 855 | 860 | ${when( |
| 856 | 861 | options.json, |
| 857 | 862 | (j) => html`<link rel="alternate" type="application/json" title="${j.title}" href="${j.href}" />`, |
src/web/pages/org.ts +1
| @@ -470,6 +470,7 @@ export async function ownerProfile(ctx: Ctx): Promise<Response> { | ||
| 470 | 470 | const res = page({ |
| 471 | 471 | ctx, |
| 472 | 472 | title: owner.username, |
| 473 | feed: { href: `/${owner.username}.atom`, title: `${owner.username} posts` }, | |
| 473 | 474 | description: ownerLine({ |
| 474 | 475 | org: isOrg, |
| 475 | 476 | name: owner.username, |
src/web/pages/repo.ts +2
| @@ -1435,6 +1435,7 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> { | ||
| 1435 | 1435 | href: `${repoBase(repo)}/activity.atom`, |
| 1436 | 1436 | title: `${repo.ownerName}/${repo.name} activity`, |
| 1437 | 1437 | }, |
| 1438 | moreFeeds: [{ href: `${repoBase(repo)}/ships.atom`, title: `${repo.ownerName}/${repo.name} ships` }], | |
| 1438 | 1439 | crumbs: scope.crumbs, |
| 1439 | 1440 | palette: scope.palette, |
| 1440 | 1441 | body, |
| @@ -1524,6 +1525,7 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> { | ||
| 1524 | 1525 | href: `${repoBase(repo)}/activity.atom`, |
| 1525 | 1526 | title: `${repo.ownerName}/${repo.name} activity`, |
| 1526 | 1527 | }, |
| 1528 | moreFeeds: [{ href: `${repoBase(repo)}/ships.atom`, title: `${repo.ownerName}/${repo.name} ships` }], | |
| 1527 | 1529 | crumbs: scope.crumbs, |
| 1528 | 1530 | palette: scope.palette, |
| 1529 | 1531 | body, |
test/repo-atom-edges.test.ts added +111
| @@ -0,0 +1,111 @@ | ||
| 1 | /** The edges of the Atom feeds: name clash, cap and order, sealed leftovers, private readers. */ | |
| 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 { createRepo } from "../src/actions/repos.ts"; | |
| 7 | import { createApp, type App } from "../src/app.ts"; | |
| 8 | import { createIssue } from "../src/core/issues.ts"; | |
| 9 | import { createSession } from "../src/core/sessions.ts"; | |
| 10 | import { createUser } from "../src/core/users.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | import { checkAtom } from "./helpers/atom.ts"; | |
| 13 | ||
| 14 | describe("atom edges", () => { | |
| 15 | let dataDir: string; | |
| 16 | let app: App; | |
| 17 | let servers: Servers; | |
| 18 | let cookie = ""; | |
| 19 | const ids: Record<string, string> = {}; | |
| 20 | ||
| 21 | const ship = (repo: string, title: string, at: string, sealed = false) => { | |
| 22 | const i = createIssue(app.db, { repoId: ids[repo]!, kind: "pull", title, authorId: ids["alice"]!, sealed }); | |
| 23 | if (!i.ok) throw new Error(String(i.error)); | |
| 24 | app.db.query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at, head_repo_id) VALUES (?1,'main','x','a','b',?2,?3)").run(i.value.id, at, ids[repo]!); | |
| 25 | }; | |
| 26 | ||
| 27 | beforeAll(async () => { | |
| 28 | dataDir = await mkdtemp(join(tmpdir(), "forge-atom-edges-")); | |
| 29 | app = createApp({ dataDir, port: 0 }); | |
| 30 | servers = await serve(app); | |
| 31 | const mk = async (username: string, first = false) => { | |
| 32 | const u = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@f.dev`, password: "a-good-passphrase", adminIfFirst: first, allowReserved: !first }); | |
| 33 | if (!u.ok) throw new Error(u.error); | |
| 34 | return u.value; | |
| 35 | }; | |
| 36 | const alice = await mk("alice", true); | |
| 37 | await mk("bob"); | |
| 38 | await mk("bob.atom"); | |
| 39 | ids["alice"] = alice.id; | |
| 40 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 41 | for (const [name, visibility] of [["many", "public"], ["secret", "private"]] as const) { | |
| 42 | const r = await createRepo(app, alice, { name, visibility, description: "" }); | |
| 43 | if (!r.ok) throw new Error(String(r.error)); | |
| 44 | ids[name] = app.db.query<{ id: string }, [string]>("SELECT id FROM repo WHERE name = ?1").get(name)!.id; | |
| 45 | } | |
| 46 | for (let n = 1; n <= 35; n++) ship("many", `ship ${n}`, `2026-10-${String(n).padStart(2, "0")}T10:00:00.000Z`); | |
| 47 | ship("many", "sealed ship", "2026-11-20T10:00:00.000Z", true); | |
| 48 | ship("secret", "private ship", "2026-11-01T10:00:00.000Z"); | |
| 49 | createIssue(app.db, { repoId: ids["many"]!, kind: "issue", title: "sealed report", authorId: alice.id, sealed: true }); | |
| 50 | createIssue(app.db, { repoId: ids["many"]!, kind: "issue", title: "open post", authorId: alice.id }); | |
| 51 | // Older public posts must survive 100+ newer private ones. | |
| 52 | for (let n = 0; n < 105; n++) createIssue(app.db, { repoId: ids["secret"]!, kind: "issue", title: `hidden ${n}`, authorId: alice.id }); | |
| 53 | }, 60_000); | |
| 54 | ||
| 55 | afterAll(async () => { | |
| 56 | servers.stop(); | |
| 57 | app.close(); | |
| 58 | await rm(dataDir, { recursive: true, force: true }); | |
| 59 | }); | |
| 60 | ||
| 61 | test("ships are capped at 30, newest first, and leave sealed pulls out", async () => { | |
| 62 | const text = await (await fetch(`${servers.url}/alice/many/ships.atom`)).text(); | |
| 63 | const { titles } = checkAtom(text, `${servers.url}/alice/many/ships.atom`); | |
| 64 | expect(titles.length).toBe(30); | |
| 65 | expect(titles[0]).toBe("Ship 35"); | |
| 66 | expect(titles[29]).toBe("Ship 6"); | |
| 67 | expect(text.toLowerCase()).not.toContain("sealed ship"); | |
| 68 | expect(text.toLowerCase()).toContain("ship 35"); | |
| 69 | }); | |
| 70 | ||
| 71 | test("a sealed issue is left out of a person's feed", async () => { | |
| 72 | const text = await (await fetch(`${servers.url}/alice.atom`)).text(); | |
| 73 | expect(text.toLowerCase()).not.toContain("sealed report"); | |
| 74 | expect(text.toLowerCase()).toContain("open post"); | |
| 75 | }); | |
| 76 | ||
| 77 | test("older public posts are not crowded out by newer private ones", async () => { | |
| 78 | const text = await (await fetch(`${servers.url}/alice.atom`)).text(); | |
| 79 | expect(text.toLowerCase()).toContain("open post"); | |
| 80 | expect(text.toLowerCase()).not.toContain("hidden"); | |
| 81 | }); | |
| 82 | ||
| 83 | test("an account named bob.atom keeps its profile, beside bob's own feed", async () => { | |
| 84 | const res = await fetch(`${servers.url}/bob.atom`); | |
| 85 | expect(res.headers.get("content-type")).toContain("text/html"); | |
| 86 | expect(await res.text()).toContain("@bob.atom"); | |
| 87 | // The feed of bob.atom is one suffix further; bob's own is unreachable by that name. | |
| 88 | const feed = await fetch(`${servers.url}/bob.atom.atom`); | |
| 89 | expect(feed.headers.get("content-type")).toContain("application/atom+xml"); | |
| 90 | expect(await feed.text()).toContain("bob.atom (@bob.atom)"); | |
| 91 | }); | |
| 92 | ||
| 93 | test("with no x.atom account, x.atom is the feed of x; unknown names are 404", async () => { | |
| 94 | const feed = await fetch(`${servers.url}/alice.atom`); | |
| 95 | expect(feed.headers.get("content-type")).toContain("application/atom+xml"); | |
| 96 | expect((await fetch(`${servers.url}/nobody.atom`)).status).toBe(404); | |
| 97 | }); | |
| 98 | ||
| 99 | test("a signed-in reader of a private repository gets it, uncached", async () => { | |
| 100 | const res = await fetch(`${servers.url}/alice/secret/ships.atom`, { headers: { cookie } }); | |
| 101 | expect(res.status).toBe(200); | |
| 102 | expect(res.headers.get("cache-control")).toBe("private, no-store"); | |
| 103 | expect(res.headers.get("vary")).toContain("cookie"); | |
| 104 | expect(await res.text()).toContain("Private ship"); | |
| 105 | // The next stranger is not handed what the reader was. | |
| 106 | expect((await fetch(`${servers.url}/alice/secret/ships.atom`)).status).toBe(404); | |
| 107 | const pub = await fetch(`${servers.url}/alice/many/ships.atom`, { headers: { cookie } }); | |
| 108 | expect(pub.headers.get("cache-control")).not.toContain("public"); | |
| 109 | expect((await fetch(`${servers.url}/alice/many/ships.atom`)).headers.get("cache-control")).toBe("public, max-age=300"); | |
| 110 | }); | |
| 111 | }); |
test/repo-atom.test.ts added +107
| @@ -0,0 +1,107 @@ | ||
| 1 | /** /:owner/:name/ships.atom and /:owner.atom: Atom for merged pulls and a person's public posts. */ | |
| 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 { createRepo } from "../src/actions/repos.ts"; | |
| 7 | import { createApp, type App } from "../src/app.ts"; | |
| 8 | import { createIssue } from "../src/core/issues.ts"; | |
| 9 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 10 | import { createUser } from "../src/core/users.ts"; | |
| 11 | import { checkAtom } from "./helpers/atom.ts"; | |
| 12 | import { shippedPulls, postsBy } from "../src/http/atom_routes.ts"; | |
| 13 | ||
| 14 | describe("repo and person atom", () => { | |
| 15 | let dataDir: string; | |
| 16 | let app: App; | |
| 17 | let servers: Servers; | |
| 18 | let aliceId = ""; | |
| 19 | ||
| 20 | beforeAll(async () => { | |
| 21 | dataDir = await mkdtemp(join(tmpdir(), "forge-repo-atom-")); | |
| 22 | app = createApp({ dataDir, port: 0 }); | |
| 23 | servers = await serve(app); | |
| 24 | const u = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 25 | if (!u.ok) throw new Error(u.error); | |
| 26 | aliceId = u.value.id; | |
| 27 | for (const [name, visibility] of [["shown", "public"], ["secret", "private"]] as const) { | |
| 28 | const r = await createRepo(app, u.value, { name, visibility, description: "" }); | |
| 29 | if (!r.ok) throw new Error(String(r.error)); | |
| 30 | } | |
| 31 | const repoId = (n: string) => app.db.query<{ id: string }, [string]>("SELECT id FROM repo WHERE name = ?1").get(n)!.id; | |
| 32 | for (const [name, title] of [["shown", "Fish & <chips> \"fresh\""], ["secret", "Private plan"]] as const) { | |
| 33 | const i = createIssue(app.db, { repoId: repoId(name), kind: "pull", title, authorId: aliceId }); | |
| 34 | if (!i.ok) throw new Error(String(i.error)); | |
| 35 | app.db.query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at, head_repo_id) VALUES (?1,'main','x','a','b','2026-11-01T10:00:00.000Z',?2)").run(i.value.id, repoId(name)); | |
| 36 | } | |
| 37 | for (const [name, title] of [["shown", "A public <post>"], ["secret", "A private post"]] as const) { | |
| 38 | const i = createIssue(app.db, { repoId: repoId(name), kind: "issue", title, authorId: aliceId }); | |
| 39 | if (!i.ok) throw new Error(String(i.error)); | |
| 40 | } | |
| 41 | }, 30_000); | |
| 42 | ||
| 43 | afterAll(async () => { | |
| 44 | servers.stop(); | |
| 45 | app.close(); | |
| 46 | await rm(dataDir, { recursive: true, force: true }); | |
| 47 | }); | |
| 48 | ||
| 49 | test("ships.atom lists merged pulls, escaped, with the security floor", async () => { | |
| 50 | const res = await fetch(`${servers.url}/alice/shown/ships.atom`); | |
| 51 | const text = await res.text(); | |
| 52 | expect(res.status).toBe(200); | |
| 53 | expect(res.headers.get("content-type")).toContain("application/atom+xml"); | |
| 54 | expect(res.headers.get("x-content-type-options")).toBe("nosniff"); | |
| 55 | expect(res.headers.get("content-security-policy")).toBeTruthy(); | |
| 56 | expect(res.headers.get("referrer-policy")).toBeTruthy(); | |
| 57 | const { titles } = checkAtom(text, `${servers.url}/alice/shown/ships.atom`); | |
| 58 | expect(titles).toEqual(["Fish & <chips> \"fresh\""]); | |
| 59 | expect(text).toContain("Fish & <chips>"); | |
| 60 | expect(text).toContain("/alice/shown/pulls/1"); | |
| 61 | expect(text).toContain("<name>alice</name>"); | |
| 62 | }); | |
| 63 | ||
| 64 | test("a private repository's ships are 404 to a stranger", async () => { | |
| 65 | expect((await fetch(`${servers.url}/alice/secret/ships.atom`)).status).toBe(404); | |
| 66 | expect((await fetch(`${servers.url}/alice/nothing/ships.atom`)).status).toBe(404); | |
| 67 | }); | |
| 68 | ||
| 69 | test("an ETag answers 304 and changes with the content", async () => { | |
| 70 | const first = await fetch(`${servers.url}/alice/shown/ships.atom`); | |
| 71 | const etag = first.headers.get("etag")!; | |
| 72 | expect(etag).toBeTruthy(); | |
| 73 | const again = await fetch(`${servers.url}/alice/shown/ships.atom`, { headers: { "if-none-match": etag } }); | |
| 74 | expect(again.status).toBe(304); | |
| 75 | const p = await fetch(`${servers.url}/alice.atom`); | |
| 76 | const pe = p.headers.get("etag")!; | |
| 77 | expect((await fetch(`${servers.url}/alice.atom`, { headers: { "if-none-match": pe } })).status).toBe(304); | |
| 78 | expect(pe).not.toBe(etag); | |
| 79 | }); | |
| 80 | ||
| 81 | test("a person's feed lists public posts only", async () => { | |
| 82 | const res = await fetch(`${servers.url}/alice.atom`); | |
| 83 | const text = await res.text(); | |
| 84 | expect(res.status).toBe(200); | |
| 85 | const { titles } = checkAtom(text, `${servers.url}/alice.atom`); | |
| 86 | expect(titles).toEqual(["A public <post>"]); | |
| 87 | expect(text).toContain("<post>"); | |
| 88 | expect(text).not.toContain("private"); | |
| 89 | expect((await fetch(`${servers.url}/nobody.atom`)).status).toBe(404); | |
| 90 | }); | |
| 91 | ||
| 92 | test("the pages carry the alternate links", async () => { | |
| 93 | const repo = await (await fetch(`${servers.url}/alice/shown`)).text(); | |
| 94 | expect(repo).toMatch(/<link\s+rel="alternate"\s+type="application\/atom\+xml"[^>]*href="\/alice\/shown\/ships\.atom"/); | |
| 95 | const person = await (await fetch(`${servers.url}/alice`)).text(); | |
| 96 | expect(person).toMatch(/<link\s+rel="alternate"\s+type="application\/atom\+xml"[^>]*href="\/alice\.atom"/); | |
| 97 | }); | |
| 98 | ||
| 99 | test("the reads walk indexes", () => { | |
| 100 | const plan = (sql: string) => app.db.query(`EXPLAIN QUERY PLAN ${sql}`).all("x", 30).map((r) => (r as { detail: string }).detail); | |
| 101 | const posts = plan(`SELECT issue.id FROM issue JOIN repo ON repo.id = issue.repo_id WHERE issue.author_id = ?1 AND issue.kind = 'issue' AND issue.sealed = 0 AND repo.visibility = 'public' ORDER BY issue.created_at DESC LIMIT ?2`); | |
| 102 | for (const l of posts) expect(l).not.toContain("TEMP B-TREE"); | |
| 103 | expect(posts.join()).toContain("issue_author_created"); | |
| 104 | expect(shippedPulls(app.db, "none")).toEqual([]); | |
| 105 | expect(postsBy(app.db, "none", 5)).toEqual([]); | |
| 106 | }); | |
| 107 | }); |
new above
offline