Public: anyone can read this; the crew can change it.
A tell that is refused for length or emptiness keeps what was typed and names the limit #13610
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13587-a-tell-that-is-refused-for-length-or-emp → main 4 commits fork 3 threads · 3 unresolved
10 files
changed +245
−35
src/web/studio/actions.ts +9 −2
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | import type { Routed } from "../../crew/route.ts"; |
| 12 | import { COMPOSER_FIELDS, claimShare, releaseShare } from "../share_target.ts"; | |
| 12 | import { COMPOSER_FIELDS, claimShare, refuseTell, releaseShare } from "../share_target.ts"; | |
| 13 | 13 | import { mayView } from "../../core/sealed.ts"; |
| 14 | 14 | import { holdPull, releasePull } from "../../actions/holds.ts"; |
| 15 | 15 | import { addComment } from "../../actions/issues.ts"; |
| @@ -30,10 +30,11 @@ import { crumbsFor, loadRepo, notFoundPage, pageHead, repoBase, signInFirst } fr | ||
| 30 | 30 | import { teamHoldRefusal } from "../../core/teams_words.ts"; |
| 31 | 31 | import { guardedRefusal } from "../../crew/stranger.ts"; |
| 32 | 32 | import type { Guarded } from "../../core/pulls.ts"; |
| 33 | import { backTo, field, readForm, type Form } from "../form.ts"; | |
| 33 | import { backTo, field, prefersJson, readForm, type Form } from "../form.ts"; | |
| 34 | 34 | import { isLocalPath } from "../flash.ts"; |
| 35 | 35 | import { html } from "../html.ts"; |
| 36 | 36 | import { flashOrJson, flashTo, page } from "../layout.ts"; |
| 37 | import { tellRefusedPage } from "./tell_refused.ts"; | |
| 37 | 38 | import { crewCanRun, NO_CREDIT_WORDS } from "./crew_key.ts"; |
| 38 | 39 | import { FORGE_TARGET, NEW_APP_TARGET } from "./feed.ts"; |
| 39 | 40 | import { AUTONOMY_FAILED_WORDS, firstAsk } from "../../actions/first_ask.ts"; |
| @@ -232,6 +233,12 @@ export async function composerSubmit(ctx: Ctx): Promise<Response> { | ||
| 232 | 233 | // given back unless the tell was made (so a failed post can be retried). |
| 233 | 234 | const once = field(form, COMPOSER_FIELDS.once); |
| 234 | 235 | const userId = ctx.auth.user.id; |
| 236 | // Empty or over the limit: said before anything is claimed or made, the words kept in the box. | |
| 237 | const words = refuseTell(field(form, COMPOSER_FIELDS.text)); | |
| 238 | if (words !== undefined) { | |
| 239 | if (prefersJson(ctx)) return flashOrJson(ctx, home, { tone: "danger", message: words }); | |
| 240 | return tellRefusedPage(ctx, { text: field(form, COMPOSER_FIELDS.text), app: target, back: home, once, from: field(form, "from") }, words); | |
| 241 | } | |
| 235 | 242 | if (once !== "") { |
| 236 | 243 | const claim = claimShare(userId, once); |
| 237 | 244 | if (claim === "bad") return flashOrJson(ctx, home, { tone: "danger", message: "That share could not be sent." }, { href: home }); |
src/web/studio/tell_form.ts added +22
| @@ -0,0 +1,22 @@ | ||
| 1 | /** | |
| 2 | * The tell form, written once: the words in the box, the hidden fields and the target the caller | |
| 3 | * brings, and, when the tell was refused (#13587), the one sentence naming the limit. That sentence | |
| 4 | * has an id the box points at (aria-describedby, aria-invalid) and the box takes focus, because a | |
| 5 | * role=alert present on load is not reliably announced. | |
| 6 | */ | |
| 7 | import { html, type Html } from "../html.ts"; | |
| 8 | import { btn } from "../ui.ts"; | |
| 9 | import { COMPOSER_FIELDS, TELL_MAX } from "../share_target.ts"; | |
| 10 | ||
| 11 | export function tellForm(opts: { text: string; hidden: Html; aside?: Html; refusal?: string | undefined }): Html { | |
| 12 | const refused = opts.refusal !== undefined; | |
| 13 | const limits = refused ? html`` : html`maxlength="${String(TELL_MAX)}" required`; | |
| 14 | const flags = refused ? html`autofocus aria-invalid="true" aria-describedby="tell-refusal"` : html``; | |
| 15 | const sentence = refused ? html`<p id="tell-refusal" role="alert" data-tell-refused>${opts.refusal ?? ""}</p>` : html``; | |
| 16 | return html`${sentence} | |
| 17 | <form class="stack" method="post" action="/tell"> | |
| 18 | ${opts.hidden} | |
| 19 | <textarea class="input" name="${COMPOSER_FIELDS.text}" rows="6" ${limits} ${flags} aria-label="Tell the crew" data-keep>${opts.text}</textarea> | |
| 20 | <div class="bar">${opts.aside ?? html``}<span class="spacer"></span>${btn("Send", { variant: "primary" })}</div> | |
| 21 | </form>`; | |
| 22 | } |
src/web/studio/tell_refused.ts added +32
| @@ -0,0 +1,32 @@ | ||
| 1 | /** | |
| 2 | * A tell refused for being empty or over TELL_MAX (#13587): the form again, what was typed still in | |
| 3 | * the box, and one sentence naming the limit, with status 422. The words are output, so they are | |
| 4 | * escaped by `html`; the target, back path and share token are carried as hidden fields. | |
| 5 | */ | |
| 6 | import type { Ctx } from "../../http/context.ts"; | |
| 7 | import { html } from "../html.ts"; | |
| 8 | import { page } from "../layout.ts"; | |
| 9 | import { keepJs } from "../keep.ts"; | |
| 10 | import { COMPOSER_FIELDS } from "../share_target.ts"; | |
| 11 | import { tellForm } from "./tell_form.ts"; | |
| 12 | import { isLocalPath } from "../flash.ts"; | |
| 13 | ||
| 14 | export function tellRefusedPage(ctx: Ctx, kept: { text: string; app: string; back: string; once: string; from: string }, words: string): Response { | |
| 15 | const back = isLocalPath(kept.back) ? kept.back : "/"; | |
| 16 | return page( | |
| 17 | { | |
| 18 | ctx, | |
| 19 | title: "Tell not sent", | |
| 20 | scriptJs: keepJs, | |
| 21 | body: html`<h1>Tell the crew</h1>${tellForm({ | |
| 22 | text: kept.text, | |
| 23 | refusal: words, | |
| 24 | hidden: html`<input type="hidden" name="${COMPOSER_FIELDS.back}" value="${back}" /> | |
| 25 | ${kept.app === "" ? "" : html`<input type="hidden" name="${COMPOSER_FIELDS.app}" value="${kept.app}" />`} | |
| 26 | ${kept.once === "" ? "" : html`<input type="hidden" name="${COMPOSER_FIELDS.once}" value="${kept.once}" />`} | |
| 27 | ${kept.from === "" ? "" : html`<input type="hidden" name="from" value="${kept.from}" />`}`, | |
| 28 | })}`, | |
outdated
Add autofocus so the keyboard lands in the kept words, plus aria-describedby/aria-invalid. |
||
| 29 | }, | |
outdated
Principle 1: this repeats the limit the alert already names. Remove it. |
||
| 30 | 422, | |
| 31 | ); | |
| 32 | } |
test/first-ask-new-app.test.ts +7 −7
| @@ -6,8 +6,7 @@ | ||
| 6 | 6 | |
| 7 | 7 | import { afterAll, beforeAll, expect, test } from "bun:test"; |
| 8 | 8 | import { listIssues, MAX_BODY } from "../src/core/issues.ts"; |
| 9 | import { TOO_LONG_WORDS } from "../src/actions/tell.ts"; | |
| 10 | import { FORGE_REPO_WORDS, SAY_SOMETHING_WORDS } from "../src/actions/tell_words.ts"; | |
| 9 | import { FORGE_REPO_WORDS } from "../src/actions/tell_words.ts"; | |
| 11 | 10 | import { listReposOwnedBy } from "../src/core/repos.ts"; |
| 12 | 11 | import { autonomyStatus } from "../src/crew/autonomy.ts"; |
| 13 | 12 | import { listCrewRoles } from "../src/core/agents.ts"; |
| @@ -52,7 +51,8 @@ test("the first tell makes one app, files one open issue on it, keeps no thought | ||
| 52 | 51 | // The forge's included credit lets the crew run: the new app starts autonomous (box 2). |
| 53 | 52 | expect(crewCanRun(forge.child)).toBe(true); |
| 54 | 53 | expect(repo.autonomy).toBe("autonomous"); |
| 55 | // Autonomy is on as the asker and a builder and reviewer are seated: the tick does not pause (#11508). | |
| 54 | // Autonomy is on as the asker and a builder and reviewer are seated: the tick does not pause | |
| 55 | // (#11508). | |
| 56 | 56 | expect(repo.autonomyBy).toBe(maria.id); |
| 57 | 57 | expect(autonomyStatus(forge.child, repo).paused).toBeNull(); |
| 58 | 58 | expect(listCrewRoles(db, repo.id).map((c) => c.role).sort()).toEqual(["builder", "reviewer"]); |
| @@ -89,8 +89,8 @@ test("an empty ask makes no app and says so", async () => { | ||
| 89 | 89 | const db = forge.child.db; |
| 90 | 90 | const before = listReposOwnedBy(db, getUserByName(db, "maria")!.id).length; |
| 91 | 91 | const res = await forge.post(forge.owner, "/tell", { app: "new-app", text: " " }); |
| 92 | expect(res.status).toBe(303); | |
| 93 | expect(flashOf(res)).toContain(SAY_SOMETHING_WORDS); | |
| 92 | expect(res.status).toBe(422); | |
| 93 | expect(decodeURIComponent(await res.text()).replace(/'|'/g, "'")).toContain("Nothing to tell yet"); | |
| 94 | 94 | expect(listReposOwnedBy(db, getUserByName(db, "maria")!.id).length).toBe(before); |
| 95 | 95 | }); |
| 96 | 96 | |
| @@ -98,8 +98,8 @@ test("an over-long ask makes no app and says so", async () => { | ||
| 98 | 98 | const db = forge.child.db; |
| 99 | 99 | const before = listReposOwnedBy(db, getUserByName(db, "maria")!.id).length; |
| 100 | 100 | const res = await forge.post(forge.owner, "/tell", { app: "new-app", text: "zebra ".repeat(MAX_BODY) }); |
| 101 | expect(res.status).toBe(303); | |
| 102 | expect(flashOf(res)).toContain(TOO_LONG_WORDS); | |
| 101 | expect(res.status).toBe(422); | |
| 102 | expect(decodeURIComponent(await res.text()).replace(/'|'/g, "'")).toContain("That is too long: a tell takes at most"); | |
| 103 | 103 | expect(listReposOwnedBy(db, getUserByName(db, "maria")!.id).length).toBe(before); |
| 104 | 104 | }); |
| 105 | 105 |
test/tell-refused.test.ts added +127
| @@ -0,0 +1,127 @@ | ||
| 1 | /** A tell refused for emptiness or length keeps what was typed and names the limit (#13587). */ | |
| 2 | import { afterAll, beforeAll, 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 { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createUser } from "../src/core/users.ts"; | |
| 8 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 9 | import { TELL_MAX, forgetShares } from "../src/web/share_target.ts"; | |
| 10 | import { quietLogs } from "./helpers/globals.ts"; | |
| 11 | ||
| 12 | quietLogs(); | |
| 13 | ||
| 14 | let dataDir: string; | |
| 15 | let app: App; | |
| 16 | let servers: Servers; | |
| 17 | let cookie = ""; | |
| 18 | ||
| 19 | beforeAll(async () => { | |
| 20 | dataDir = await mkdtemp(join(tmpdir(), "forge-tell-refused-")); | |
| 21 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 22 | servers = await serve(app); | |
| 23 | const made = await createUser(app.db, { | |
| 24 | origin: "seed", trusted: true, username: "alice", email: "[email protected]", | |
| 25 | password: "a-good-passphrase", adminIfFirst: true, | |
| 26 | }); | |
| 27 | if (!made.ok) throw new Error(made.error); | |
| 28 | const res = await fetch(`${servers.url}/signin`, { | |
| 29 | method: "POST", | |
| 30 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), | |
| 31 | redirect: "manual", | |
| 32 | }); | |
| 33 | cookie = (res.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 34 | }, 120_000); | |
| 35 | ||
| 36 | afterAll(async () => { | |
| 37 | servers.stop(); | |
| 38 | app.close(); | |
| 39 | await rm(dataDir, { recursive: true, force: true }); | |
| 40 | }); | |
| 41 | ||
| 42 | const thoughts = () => app.db.query<{ n: number }, []>("SELECT count(*) AS n FROM thought").get()!.n; | |
| 43 | ||
| 44 | const post = (fields: Record<string, string>) => | |
| 45 | fetch(`${servers.url}/tell`, { | |
| 46 | method: "POST", | |
| 47 | headers: { cookie, origin: servers.url }, | |
| 48 | body: new URLSearchParams({ app: "forge", back: "/feed", ...fields }), | |
| 49 | redirect: "manual", | |
| 50 | }); | |
| 51 | ||
| 52 | test("an empty tell answers 422, names the limit and files nothing", async () => { | |
| 53 | const before = thoughts(); | |
| 54 | for (const text of ["", " \n "]) { | |
| 55 | const res = await post({ text }); | |
| 56 | expect(res.status).toBe(422); | |
| 57 | const out = await res.text(); | |
| 58 | expect(out).toContain("Nothing to tell yet"); | |
| 59 | expect(out).toContain("2,000 characters"); | |
| 60 | expect(out).toContain('<textarea'); | |
| 61 | } | |
| 62 | expect(thoughts()).toBe(before); | |
| 63 | }); | |
| 64 | ||
| 65 | test("a tell over the limit keeps the words, escaped, and names the limit", async () => { | |
| 66 | const before = thoughts(); | |
| 67 | const text = `<script>alert(1)</script>${"x".repeat(TELL_MAX)}`; | |
| 68 | const res = await post({ text, once: "0123456789abcdef01234567" }); | |
| 69 | expect(res.status).toBe(422); | |
| 70 | const out = await res.text(); | |
| 71 | expect(out).toContain("at most 2,000 characters"); | |
| 72 | expect(out).toContain("<script>alert(1)</script>" + "x".repeat(TELL_MAX)); | |
| 73 | expect(out).not.toContain("<script>alert(1)"); | |
| 74 | expect(out).toContain('name="app" value="forge"'); | |
| 75 | expect(out).toContain('name="once" value="0123456789abcdef01234567"'); | |
| 76 | expect(thoughts()).toBe(before); | |
| 77 | }); | |
| 78 | ||
| 79 | test("exactly the limit is told", async () => { | |
| 80 | const before = thoughts(); | |
| 81 | const res = await post({ text: "y".repeat(TELL_MAX) }); | |
| 82 | expect(res.status).toBe(303); | |
| 83 | expect(thoughts()).toBe(before + 1); | |
| 84 | }); | |
| 85 | ||
| 86 | test("a refused post does not use up its share token", async () => { | |
| 87 | forgetShares(); | |
| 88 | const once = "abcdef0123456789abcdef01"; | |
| 89 | expect((await post({ text: "", once })).status).toBe(422); | |
| 90 | expect((await post({ text: "", once })).status).toBe(422); | |
| 91 | const before = thoughts(); | |
| 92 | const retry = await post({ text: "now with words", once }); | |
| 93 | expect(retry.status).toBe(303); | |
| 94 | expect(thoughts()).toBe(before + 1); | |
| 95 | }); | |
| 96 | ||
| 97 | test("the refusal is announced and the box is pointed at it", async () => { | |
| 98 | const out = await (await post({ text: "" })).text(); | |
| 99 | expect(out).toContain('id="tell-refusal"'); | |
| 100 | expect(out).toContain('aria-describedby="tell-refusal"'); | |
| 101 | expect(out).toContain('aria-invalid="true"'); | |
| 102 | expect(out).toContain("autofocus"); | |
| 103 | expect(out).not.toContain("At most 2,000 characters."); | |
| 104 | }); | |
| 105 | ||
| 106 | test("the share-sheet path does the same: 422, the draft kept, the limit named", async () => { | |
| 107 | const long = await fetch(`${servers.url}/tell?text=${encodeURIComponent("<b>" + "z".repeat(TELL_MAX))}`, { headers: { cookie } }); | |
| 108 | expect(long.status).toBe(422); | |
| 109 | const out = await long.text(); | |
| 110 | expect(out).toContain("at most 2,000 characters"); | |
| 111 | expect(out).toContain("<b>" + "z".repeat(TELL_MAX)); | |
| 112 | const empty = await fetch(`${servers.url}/tell?title=%20`, { headers: { cookie } }); | |
| 113 | expect(empty.status).toBe(422); | |
| 114 | expect(await empty.text()).toContain("Nothing to tell yet"); | |
| 115 | const exact = await fetch(`${servers.url}/tell?text=${"z".repeat(TELL_MAX)}`, { headers: { cookie } }); | |
| 116 | expect(exact.status).toBe(200); | |
| 117 | }); | |
| 118 | ||
| 119 | test("a JSON asker still gets a 422 with the sentence", async () => { | |
| 120 | const res = await fetch(`${servers.url}/tell`, { | |
| 121 | method: "POST", | |
| 122 | headers: { cookie, origin: servers.url, accept: "application/json" }, | |
| 123 | body: new URLSearchParams({ app: "forge", text: "" }), | |
| 124 | }); | |
| 125 | expect(res.status).toBe(422); | |
| 126 | expect(((await res.json()) as { error: string }).error).toContain("2,000"); | |
| 127 | }); |
new above
offline
Give this an id and point the textarea at it with aria-describedby and aria-invalid; role=alert on load is not reliably read.