Public: anyone can read this; the crew can change it.
A /talk conversation downloads as markdown #14873
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14861-a-talk-conversation-downloads-as-markdow → main 2 commits fork 3 threads · 3 unresolved
1 file
changed +78
−0
test/talk-export.test.ts added +78
| @@ -0,0 +1,78 @@ | ||
| 1 | /** | |
| 2 | * GET /talk/<id>.md (#14861): the conversation as Markdown under speaker headings, its owner's | |
| 3 | * alone, as an attachment with nosniff. Already served on main (#13606); this proves it. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { appendTurn, createConversation } from "../src/core/conversations.ts"; | |
| 11 | import { createSession } from "../src/core/sessions.ts"; | |
| 12 | import { createUser, type User } from "../src/core/users.ts"; | |
| 13 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 14 | import { promote } from "./helpers/members.ts"; | |
| 15 | import { quietLogs } from "./helpers/globals.ts"; | |
| 16 | ||
| 17 | quietLogs(); | |
| 18 | ||
| 19 | let dir: string; | |
| 20 | let app: App; | |
| 21 | let servers: Servers; | |
| 22 | let alice: User; | |
| 23 | let bob: User; | |
| 24 | let id: string; | |
| 25 | ||
| 26 | const get = (path: string, u?: User) => | |
| 27 | fetch(`${servers.url}${path}`, { redirect: "manual", headers: u === undefined ? {} : { cookie: `forge_session=${createSession(app.db, u.id).token}` } }); | |
| 28 | ||
| 29 | beforeAll(async () => { | |
| 30 | dir = await mkdtemp(join(tmpdir(), "forge-talk-export-")); | |
| 31 | app = createApp({ dataDir: dir, port: 0 }); | |
| 32 | servers = await serve(app); | |
| 33 | const mk = async (username: string, first = false) => { | |
| 34 | const u = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: first }); | |
| 35 | if (!u.ok) throw new Error(u.error); | |
| 36 | promote(app.db, u.value); | |
| 37 | return u.value; | |
| 38 | }; | |
| 39 | alice = await mk("alice", true); | |
| 40 | bob = await mk("bob"); | |
| 41 | const c = createConversation(app.db, alice.id); | |
| 42 | if (!c.ok) throw new Error(c.error); | |
| 43 | id = c.value.id; | |
| 44 | appendTurn(app.db, alice.id, id, { role: "person", text: "what is shipping?" }); | |
| 45 | appendTurn(app.db, alice.id, id, { role: "agent", text: "```\n# Forged heading\n```\nNothing yet." }); | |
| 46 | }, 30_000); | |
| 47 | ||
| 48 | afterAll(async () => { | |
| 49 | servers.stop(); | |
| 50 | app.close(); | |
| 51 | await rm(dir, { recursive: true, force: true }); | |
| 52 | }); | |
| 53 | ||
| 54 | describe("talk export", () => { | |
| 55 | test("the owner gets the .md alias as a Markdown attachment with nosniff, hostile text fenced", async () => { | |
| 56 | const r = await get(`/talk/${id}.md`, alice); | |
| 57 | expect(r.status).toBe(200); | |
| 58 | expect(r.headers.get("content-type")).toStartWith("text/markdown"); | |
| 59 | expect(r.headers.get("content-disposition")).toStartWith("attachment"); | |
| 60 | expect(r.headers.get("x-content-type-options")).toBe("nosniff"); | |
| 61 | const text = await r.text(); | |
| 62 | expect(text).toContain("what is shipping?"); | |
| 63 | // The forged heading sits inside a fence longer than the backticks in it. | |
| 64 | expect(text).toContain("````text\n```\n# Forged heading\n```\nNothing yet.\n````"); | |
| 65 | }); | |
| 66 | ||
| 67 | test("a stranger and an unknown id get 404 with nothing leaked; a signed-out reader is sent to sign in", async () => { | |
| 68 | const stranger = await get(`/talk/${id}.md`, bob); | |
| 69 | expect(stranger.status).toBe(404); | |
| 70 | expect(await stranger.text()).not.toContain("what is shipping?"); | |
| 71 | const unknown = await get(`/talk/nope.md`, alice); | |
outdated
Also assert the 404 body for bob does not contain 'what is shipping?'. |
||
| 72 | expect(unknown.status).toBe(404); | |
outdated
Too weak: a 200 HTML page passes this. Assert status 303, a location containing /signin, and that the body does not contain the turn text. |
||
| 73 | const out = await get(`/talk/${id}.md`); | |
| 74 | expect(out.status).toBe(303); | |
| 75 | expect(out.headers.get("location")).toContain("/signin"); | |
| 76 | expect(await out.text()).not.toContain("what is shipping?"); | |
| 77 | }); | |
| 78 | }); |
new above
offline
Any ```` anywhere satisfies this. Assert the forged heading line sits inside the fence, or that no heading line appears outside the fences.