Public: anyone can read this; the crew can change it.
A test pins keeping a thought: signed out is refused, a body that is not text is refused, and nothing is stored #7122
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/7115-a-test-pins-keeping-a-thought-signed-out → main 1 commit fork 1 thread · 1 unresolved
1 file
changed +65
−0
test/thought-refusals.test.ts added +65
| @@ -0,0 +1,65 @@ | ||
| 1 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 2 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { createApp, type App } from "../src/app.ts"; | |
| 6 | import { keepThought } from "../src/actions/thought.ts"; | |
| 7 | import { createUser, type User } from "../src/core/users.ts"; | |
| 8 | ||
| 9 | describe("keepThought refusals", () => { | |
| 10 | let dataDir: string; | |
| 11 | let app: App; | |
| 12 | let user: User; | |
| 13 | ||
| 14 | beforeAll(async () => { | |
| 15 | dataDir = await mkdtemp(join(tmpdir(), "forge-thought-refusals-")); | |
| 16 | app = createApp({ dataDir, port: 0 }); | |
| 17 | const made = await createUser(app.db, { | |
| 18 | origin: "seed", | |
| 19 | trusted: true, | |
| 20 | username: "mira", | |
| 21 | email: "[email protected]", | |
| 22 | password: "a-good-passphrase", | |
| 23 | }); | |
| 24 | if (!made.ok) throw new Error("user"); | |
| 25 | user = made.value; | |
| 26 | }); | |
| 27 | ||
| 28 | afterAll(async () => { | |
| 29 | app.close(); | |
| 30 | await rm(dataDir, { recursive: true, force: true }); | |
| 31 | }); | |
| 32 | ||
| 33 | const count = () => | |
| 34 | app.db.query<{ n: number }, []>("SELECT COUNT(*) AS n FROM thought").get()?.n ?? -1; | |
| 35 | ||
| 36 | test("a text thought is kept and answered with an id and receivedAt", () => { | |
| 37 | const before = count(); | |
| 38 | const r = keepThought(app, user, "hello", "api"); | |
| 39 | expect(r.ok).toBe(true); | |
| 40 | if (!r.ok) return; | |
| 41 | expect(r.value.id.length).toBeGreaterThan(0); | |
| 42 | expect(Number.isNaN(Date.parse(r.value.receivedAt))).toBe(false); | |
| 43 | expect(count()).toBe(before + 1); | |
| 44 | }); | |
| 45 | ||
| 46 | test.each([ | |
| 47 | ["a number", 42], | |
| 48 | ["an object", { text: "x" }], | |
| 49 | ["null", null], | |
| 50 | ])("%s is not_text and nothing is stored", (_name, body) => { | |
| 51 | const before = count(); | |
| 52 | const r = keepThought(app, user, body, "api"); | |
| 53 | expect(r.ok).toBe(false); | |
| 54 | if (!r.ok) expect(r.error).toBe("not_text"); | |
| 55 | expect(count()).toBe(before); | |
| 56 | }); | |
| 57 | ||
| 58 | test("signed out is unauthorized and nothing is stored", () => { | |
| 59 | const before = count(); | |
| 60 | const r = keepThought(app, undefined, "hello", "api"); | |
| 61 | expect(r.ok).toBe(false); | |
| 62 | if (!r.ok) expect(r.error).toBe("unauthorized"); | |
| 63 | expect(count()).toBe(before); | |
| 64 | }); | |
| 65 | }); |
new above
offline
Add a sibling test for too_long (text of THOUGHT_MAX + 1 characters, import THOUGHT_MAX from the action) with the same unchanged-count check.