Public: anyone can read this; the crew can change it.
Flaky: test/audit.test.ts #2135
scout/forge:scout/2108-flaky-test-audit-test-ts → main 3 commits fork
2 files
changed +173
−126
scripts/gate.ts −5
| @@ -39,11 +39,6 @@ export const EXCLUDED: readonly Exclusion[] = [ | ||
| 39 | 39 | }, |
| 40 | 40 | { file: "test/apps-sleep.test.ts", reason: "not yet admitted", issue: 261 }, |
| 41 | 41 | { file: "test/apps-ws.test.ts", reason: "not yet admitted", issue: 261 }, |
| 42 | { | |
| 43 | file: "test/audit.test.ts", | |
| 44 | reason: "red in random order: the page: every app the viewer can read, each in its standing", | |
| 45 | issue: 2108, | |
| 46 | }, | |
| 47 | 42 | { |
| 48 | 43 | file: "test/board-windows.test.ts", |
| 49 | 44 | reason: |
test/audit.test.ts +173 −121
| @@ -26,6 +26,38 @@ import { openPull } from "../src/actions/pulls.ts"; | ||
| 26 | 26 | import { createUser, type User } from "../src/core/users.ts"; |
| 27 | 27 | import { serve, type Servers } from "../src/http/server.ts"; |
| 28 | 28 | |
| 29 | /** A forge of its own with alice signed in, for a test that changes what it reads. */ | |
| 30 | async function boot(prefix: string) { | |
| 31 | const dataDir = await mkdtemp(join(tmpdir(), prefix)); | |
| 32 | const app = createApp({ dataDir, port: 0 }); | |
| 33 | const servers = await serve(app); | |
| 34 | const a = await createUser(app.db, { | |
| 35 | origin: "seed", | |
| 36 | trusted: true, | |
| 37 | username: "alice", | |
| 38 | email: "[email protected]", | |
| 39 | password: "a-good-passphrase", | |
| 40 | adminIfFirst: true, | |
| 41 | }); | |
| 42 | if (!a.ok) throw new Error(a.error); | |
| 43 | const signedIn = await fetch(`${servers.url}/signin`, { | |
| 44 | method: "POST", | |
| 45 | body: new URLSearchParams({ | |
| 46 | username: "alice", | |
| 47 | password: "a-good-passphrase", | |
| 48 | }), | |
| 49 | redirect: "manual", | |
| 50 | }); | |
| 51 | const cookie = (signedIn.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 52 | expect(cookie).not.toBe(""); | |
| 53 | const stop = async () => { | |
| 54 | servers.stop(); | |
| 55 | app.close(); | |
| 56 | await rm(dataDir, { recursive: true, force: true }); | |
| 57 | }; | |
| 58 | return { app, servers, alice: a.value, cookie, stop }; | |
| 59 | } | |
| 60 | ||
| 29 | 61 | describe("the audit reads every app's policy from git", () => { |
| 30 | 62 | let dataDir: string; |
| 31 | 63 | let app: App; |
| @@ -37,7 +69,9 @@ describe("the audit reads every app's policy from git", () => { | ||
| 37 | 69 | dataDir = await mkdtemp(join(tmpdir(), "forge-audit-")); |
| 38 | 70 | app = createApp({ dataDir, port: 0 }); |
| 39 | 71 | servers = await serve(app); |
| 40 | const a = await createUser(app.db, { origin: "seed", trusted: true, | |
| 72 | const a = await createUser(app.db, { | |
| 73 | origin: "seed", | |
| 74 | trusted: true, | |
| 41 | 75 | username: "alice", |
| 42 | 76 | email: "[email protected]", |
| 43 | 77 | password: "a-good-passphrase", |
| @@ -192,131 +226,149 @@ describe("the audit reads every app's policy from git", () => { | ||
| 192 | 226 | }); |
| 193 | 227 | |
| 194 | 228 | test("what changed: contract commits, a crossing waiting, a verdict that flipped", async () => { |
| 195 | const notes = getRepo(app.db, "alice", "notes")!; | |
| 196 | const record = getAppByRepo(app.db, notes.id)!; | |
| 197 | // A commit that opens a door, then one that closes two. | |
| 198 | const text = ( | |
| 199 | (await ( | |
| 200 | await fetch( | |
| 201 | `${servers.url}/api/v1/repos/alice/notes/contents/contract.json`, | |
| 202 | ) | |
| 203 | ).json()) as { content: string } | |
| 204 | ).content; | |
| 205 | const contract = JSON.parse(text) as { | |
| 206 | resources: { note: { can: Record<string, string> } }; | |
| 207 | }; | |
| 208 | const wider = structuredClone(contract); | |
| 209 | wider.resources.note.can.create = "anon"; | |
| 210 | const opened = await commitToBranch(app, alice, notes, { | |
| 211 | message: "anyone may add", | |
| 212 | files: [{ path: "contract.json", content: JSON.stringify(wider) }], | |
| 213 | }); | |
| 214 | expect(opened.ok).toBe(true); | |
| 215 | const narrower = structuredClone(wider); | |
| 216 | narrower.resources.note.can.read = "user"; | |
| 217 | narrower.resources.note.can.list = "user"; | |
| 218 | const closed = await commitToBranch(app, alice, notes, { | |
| 219 | message: "only the signed-in read", | |
| 220 | files: [{ path: "contract.json", content: JSON.stringify(narrower) }], | |
| 221 | }); | |
| 222 | expect(closed.ok).toBe(true); | |
| 223 | const changes = await contractChanges(app.reposRoot, notes); | |
| 224 | expect(changes.map((c) => c.subject)).toEqual([ | |
| 225 | "only the signed-in read", | |
| 226 | "anyone may add", | |
| 227 | expect.any(String), | |
| 228 | ]); | |
| 229 | expect(changes[0]!.loosened).toEqual([]); | |
| 230 | expect( | |
| 231 | changes[0]!.tightened.map((t) => `${t.verb}.${t.as}`).sort(), | |
| 232 | ).toEqual(["list.anon", "read.anon"]); | |
| 233 | expect(changes[1]!.loosened.map((l) => `${l.verb}.${l.as}`)).toEqual([ | |
| 234 | "create.anon", | |
| 235 | ]); | |
| 236 | // The first commit, the scaffold's: the floor's open cells are what was decided. | |
| 237 | expect(changes[2]!.loosened.map((l) => `${l.verb}.${l.as}`).sort()).toEqual( | |
| 238 | ["list.anon", "read.anon"], | |
| 229 | // Its own forge: this test rewrites notes' contract, which the shared | |
| 230 | // forge's other tests read as the scaffold's. | |
| 231 | const { app, servers, alice, cookie, stop } = await boot( | |
| 232 | "forge-audit-changes-", | |
| 239 | 233 | ); |
| 234 | try { | |
| 235 | const made = await newApp(app, alice, { | |
| 236 | name: "notes", | |
| 237 | visibility: "public", | |
| 238 | }); | |
| 239 | if (!made.ok) throw new Error(String(made.error)); | |
| 240 | const notes = getRepo(app.db, "alice", "notes")!; | |
| 241 | const record = getAppByRepo(app.db, notes.id)!; | |
| 242 | // A commit that opens a door, then one that closes two. | |
| 243 | const text = ( | |
| 244 | (await ( | |
| 245 | await fetch( | |
| 246 | `${servers.url}/api/v1/repos/alice/notes/contents/contract.json`, | |
| 247 | ) | |
| 248 | ).json()) as { content: string } | |
| 249 | ).content; | |
| 250 | const contract = JSON.parse(text) as { | |
| 251 | resources: { note: { can: Record<string, string> } }; | |
| 252 | }; | |
| 253 | const wider = structuredClone(contract); | |
| 254 | wider.resources.note.can.create = "anon"; | |
| 255 | const opened = await commitToBranch(app, alice, notes, { | |
| 256 | message: "anyone may add", | |
| 257 | files: [{ path: "contract.json", content: JSON.stringify(wider) }], | |
| 258 | }); | |
| 259 | expect(opened.ok).toBe(true); | |
| 260 | const narrower = structuredClone(wider); | |
| 261 | narrower.resources.note.can.read = "user"; | |
| 262 | narrower.resources.note.can.list = "user"; | |
| 263 | const closed = await commitToBranch(app, alice, notes, { | |
| 264 | message: "only the signed-in read", | |
| 265 | files: [{ path: "contract.json", content: JSON.stringify(narrower) }], | |
| 266 | }); | |
| 267 | expect(closed.ok).toBe(true); | |
| 268 | const changes = await contractChanges(app.reposRoot, notes); | |
| 269 | expect(changes.map((c) => c.subject)).toEqual([ | |
| 270 | "only the signed-in read", | |
| 271 | "anyone may add", | |
| 272 | expect.any(String), | |
| 273 | ]); | |
| 274 | expect(changes[0]!.loosened).toEqual([]); | |
| 275 | expect( | |
| 276 | changes[0]!.tightened.map((t) => `${t.verb}.${t.as}`).sort(), | |
| 277 | ).toEqual(["list.anon", "read.anon"]); | |
| 278 | expect(changes[1]!.loosened.map((l) => `${l.verb}.${l.as}`)).toEqual([ | |
| 279 | "create.anon", | |
| 280 | ]); | |
| 281 | // The first commit, the scaffold's: the floor's open cells are what was decided. | |
| 282 | expect( | |
| 283 | changes[2]!.loosened.map((l) => `${l.verb}.${l.as}`).sort(), | |
| 284 | ).toEqual(["list.anon", "read.anon"]); | |
| 240 | 285 | |
| 241 | // A pull at the wall, as the crew would leave it. | |
| 242 | const branch = await commitToBranch(app, alice, notes, { | |
| 243 | branch: "wider-still", | |
| 244 | message: "anyone may delete", | |
| 245 | files: [ | |
| 246 | { | |
| 247 | path: "contract.json", | |
| 248 | content: JSON.stringify({ | |
| 249 | ...narrower, | |
| 250 | resources: { | |
| 251 | note: { can: { ...narrower.resources.note.can, delete: "anon" } }, | |
| 252 | }, | |
| 253 | }), | |
| 286 | // A pull at the wall, as the crew would leave it. | |
| 287 | const branch = await commitToBranch(app, alice, notes, { | |
| 288 | branch: "wider-still", | |
| 289 | message: "anyone may delete", | |
| 290 | files: [ | |
| 291 | { | |
| 292 | path: "contract.json", | |
| 293 | content: JSON.stringify({ | |
| 294 | ...narrower, | |
| 295 | resources: { | |
| 296 | note: { | |
| 297 | can: { ...narrower.resources.note.can, delete: "anon" }, | |
| 298 | }, | |
| 299 | }, | |
| 300 | }), | |
| 301 | }, | |
| 302 | ], | |
| 303 | }); | |
| 304 | expect(branch.ok).toBe(true); | |
| 305 | const pull = await openPull(app, alice, notes, { | |
| 306 | title: "Anyone may delete", | |
| 307 | baseBranch: "main", | |
| 308 | headBranch: "wider-still", | |
| 309 | }); | |
| 310 | if (!pull.ok) throw new Error(JSON.stringify(pull.error)); | |
| 311 | recordDecision(app.db, { | |
| 312 | kind: "land", | |
| 313 | repoId: notes.id, | |
| 314 | issueId: pull.value.id, | |
| 315 | state: { | |
| 316 | head: pull.value.headOid, | |
| 317 | cells: ["note.delete as anon: was no, now yes"], | |
| 254 | 318 | }, |
| 255 | ], | |
| 256 | }); | |
| 257 | expect(branch.ok).toBe(true); | |
| 258 | const pull = await openPull(app, alice, notes, { | |
| 259 | title: "Anyone may delete", | |
| 260 | baseBranch: "main", | |
| 261 | headBranch: "wider-still", | |
| 262 | }); | |
| 263 | if (!pull.ok) throw new Error(JSON.stringify(pull.error)); | |
| 264 | recordDecision(app.db, { | |
| 265 | kind: "land", | |
| 266 | repoId: notes.id, | |
| 267 | issueId: pull.value.id, | |
| 268 | state: { | |
| 269 | head: pull.value.headOid, | |
| 270 | cells: ["note.delete as anon: was no, now yes"], | |
| 271 | }, | |
| 272 | answer: "wall", | |
| 273 | p: 1, | |
| 274 | by: "wall", | |
| 275 | ms: 0, | |
| 276 | }); | |
| 277 | const waiting = crossingsWaiting(app.db, [notes]); | |
| 278 | expect(waiting.map((c) => c.pull.title)).toEqual(["Anyone may delete"]); | |
| 279 | expect(waiting[0]!.cells).toEqual(["note.delete as anon: was no, now yes"]); | |
| 319 | answer: "wall", | |
| 320 | p: 1, | |
| 321 | by: "wall", | |
| 322 | ms: 0, | |
| 323 | }); | |
| 324 | const waiting = crossingsWaiting(app.db, [notes]); | |
| 325 | expect(waiting.map((c) => c.pull.title)).toEqual(["Anyone may delete"]); | |
| 326 | expect(waiting[0]!.cells).toEqual([ | |
| 327 | "note.delete as anon: was no, now yes", | |
| 328 | ]); | |
| 280 | 329 | |
| 281 | // Two probes, the second disagreeing with the first. | |
| 282 | recordProbe(app.db, record.id, { | |
| 283 | repo: notes, | |
| 284 | commitOid: "a".repeat(40), | |
| 285 | at: "", | |
| 286 | verdict: "ok", | |
| 287 | knocks: [], | |
| 288 | said: [], | |
| 289 | }); | |
| 290 | recordProbe(app.db, record.id, { | |
| 291 | repo: notes, | |
| 292 | commitOid: "b".repeat(40), | |
| 293 | at: "", | |
| 294 | verdict: "mismatch", | |
| 295 | knocks: [], | |
| 296 | said: ["note.delete as anon: the table says no, the app said 404"], | |
| 297 | }); | |
| 298 | const flips = verdictChanges(app.db, [{ repo: notes, appId: record.id }]); | |
| 299 | expect(flips.map((f) => [f.from, f.to])).toEqual([ | |
| 300 | ["ok", "mismatch"], | |
| 301 | [undefined, "ok"], | |
| 302 | ]); | |
| 330 | // Two probes, the second disagreeing with the first. | |
| 331 | recordProbe(app.db, record.id, { | |
| 332 | repo: notes, | |
| 333 | commitOid: "a".repeat(40), | |
| 334 | at: "", | |
| 335 | verdict: "ok", | |
| 336 | knocks: [], | |
| 337 | said: [], | |
| 338 | }); | |
| 339 | recordProbe(app.db, record.id, { | |
| 340 | repo: notes, | |
| 341 | commitOid: "b".repeat(40), | |
| 342 | at: "", | |
| 343 | verdict: "mismatch", | |
| 344 | knocks: [], | |
| 345 | said: ["note.delete as anon: the table says no, the app said 404"], | |
| 346 | }); | |
| 347 | const flips = verdictChanges(app.db, [{ repo: notes, appId: record.id }]); | |
| 348 | expect(flips.map((f) => [f.from, f.to])).toEqual([ | |
| 349 | ["ok", "mismatch"], | |
| 350 | [undefined, "ok"], | |
| 351 | ]); | |
| 303 | 352 | |
| 304 | const body = await ( | |
| 305 | await fetch(`${servers.url}/audit`, { headers: { cookie } }) | |
| 306 | ).text(); | |
| 307 | expect(body).toContain("What changed"); | |
| 308 | expect(body).toContain("1 pull waits"); | |
| 309 | expect(body).toContain("Anyone may delete"); | |
| 310 | expect(body).toContain("only the signed-in read"); | |
| 311 | expect(body).toContain("2 doors narrower"); | |
| 312 | expect(body).toContain("1 door wider"); | |
| 313 | expect(body).toContain("Doors, over time"); | |
| 314 | expect(body).toContain("alice/notes disobeys its table"); | |
| 315 | // A stranger sees the public app's history too; the ledger row is about | |
| 316 | // a public pull, so the crossing is as public as the pull. | |
| 317 | const anon = await (await fetch(`${servers.url}/audit`)).text(); | |
| 318 | expect(anon).toContain("only the signed-in read"); | |
| 319 | }); | |
| 353 | const body = await ( | |
| 354 | await fetch(`${servers.url}/audit`, { headers: { cookie } }) | |
| 355 | ).text(); | |
| 356 | expect(body).toContain("What changed"); | |
| 357 | expect(body).toContain("1 pull waits"); | |
| 358 | expect(body).toContain("Anyone may delete"); | |
| 359 | expect(body).toContain("only the signed-in read"); | |
| 360 | expect(body).toContain("2 doors narrower"); | |
| 361 | expect(body).toContain("1 door wider"); | |
| 362 | expect(body).toContain("Doors, over time"); | |
| 363 | expect(body).toContain("alice/notes disobeys its table"); | |
| 364 | // A stranger sees the public app's history too; the ledger row is about | |
| 365 | // a public pull, so the crossing is as public as the pull. | |
| 366 | const anon = await (await fetch(`${servers.url}/audit`)).text(); | |
| 367 | expect(anon).toContain("only the signed-in read"); | |
| 368 | } finally { | |
| 369 | await stop(); | |
| 370 | } | |
| 371 | }, 30_000); | |
| 320 | 372 | |
| 321 | 373 | test("the table file: one row per cell, with what the door said", async () => { |
| 322 | 374 | const res = await fetch(`${servers.url}/audit.csv`, { |
new above
offline