Public: anyone can read this; the crew can change it.

Tell the crew from a toolbar carries the page it was sent from, proven in a test and visible on the issue #16000

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/15975-tell-the-crew-from-a-toolbar-carries-the → main 0 commits fork 1 thread · 1 unresolved

  • Merged. scout/15975-tell-the-crew-from-a-toolbar-carries-the was merged into main as 23aedcb.

Review

Sign in to review this change.
3 files changed +46 −5
src/actions/tell_context.ts +13 −2
@@ -18,6 +18,15 @@ export interface TellContext {
1818 }
1919
2020 const SAID_MAX = 2000;
21
22 /** The most characters one line of a tell's context may carry; longer is trimmed, never refused. */
23 export const CONTEXT_LINE_MAX = 300;
24
25 /** A context value cut to `max` characters, never inside a `%XX` escape, so it still reads. */
26 export function trimContext(value: string, max: number = CONTEXT_LINE_MAX): string {
27 if (value.length <= max) return value;
28 return value.slice(0, max).replace(/%[0-9A-F]?$/i, "");
29 }

Nit: move trimContext below the RUN and COMMIT constants so the constants stay together.

2130 const RUN = /^[A-Za-z0-9_-]{1,64}$/;
2231 const COMMIT = /^[0-9a-f]{7,64}$/;
2332
@@ -157,7 +166,7 @@ export function contextNote(c: TellContext | undefined, opts: { preview?: boolea
157166 const app = opts.app ?? c.app;
158167 const preview = opts.preview === true ? previewLine(c.pull, c.page, app) : "";
159168 const lines = [
160 preview !== "" || c.page === undefined ? "" : `- page: ${c.page}`,
169 preview !== "" || c.page === undefined ? "" : `- page: ${trimContext(c.page, CONTEXT_LINE_MAX - 8)}`,
161170 preview !== "" || c.pull === undefined ? "" : `- pull: #${c.pull}`,
162171 preview !== "" || app === undefined || !APP_SLUG.test(app)
163172 ? ""
@@ -184,5 +193,7 @@ function fromLine(page: string | undefined, app: string | undefined): string {
184193 /[\\\s\x00-\x1f\x7f<>()\[\]`"'*_~|!#&]/g,
185194 (ch) => `%${ch.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0")}`,
186195 );
187 return parseFrom(path) === path ? `From: ${app} ${path}` : "";
196 const head = `From: ${app} `;
197 const kept = trimContext(path, CONTEXT_LINE_MAX - head.length);
198 return parseFrom(kept) === kept ? `${head}${kept}` : "";
188199 }
src/actions/tell_from.ts +5 −3
@@ -3,7 +3,7 @@
33 * what the server would before it spends a round trip.
44 */
55
6 import { scrubQuery } from "./tell_context.ts";
6 import { CONTEXT_LINE_MAX, scrubQuery, trimContext } from "./tell_context.ts";
77
88 /** The most characters a tell's words may have; every door refuses more. */
99 export const TELL_TEXT_MAX = 4000;
@@ -66,7 +66,9 @@ export function previewLine(
6666 /[\\\s\x00-\x1f\x7f<>()\[\]`"'*_~|!#&]/g,
6767 (c) => `%${c.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0")}`,
6868 );
69 if (parseFrom(path) !== path) return "";
7069 const named = app !== undefined && APP_SLUG.test(app) ? `${app} ` : "";
71 return `From: ${named}${path} (preview of #${pull})`;
70 const tail = ` (preview of #${pull})`;
71 const kept = trimContext(path, CONTEXT_LINE_MAX - `From: ${named}`.length - tail.length);
72 if (parseFrom(kept) !== kept) return "";
73 return `From: ${named}${kept}${tail}`;
7274 }
test/apps-toolbar-tell.test.ts +28
@@ -175,6 +175,34 @@ describe("tell the crew from the pill", () => {
175175 expect(issue.body).not.toContain("- pull:");
176176 });
177177
178 test("a tell from a preview's toolbar reads back through the API with its app, path and pull on one line", async () => {
179 const res = await post(
180 { text: "Read me back", page: "https://notes-pull-3.apps.test/deep/page?q=1", pull: "3" },
181 { origin: servers.url },
182 );
183 expect(res.status).toBe(200);
184 const n = listIssues(app.db, repoId, {}).find((i) => i.title === "Read me back")!.number;
185 const api = await fetch(`${servers.url}/api/v1/repos/alice/notes/issues/${n}`, { headers: auth });
186 expect(api.status).toBe(200);
187 const body = ((await api.json()) as { body: string }).body;
188 expect(body.split("\n").filter((l) => l.startsWith("From:"))).toEqual(["From: notes /deep/page (preview of #3)"]);
189 });
190
191 test("a context longer than 300 characters is trimmed, not refused", async () => {
192 const long = "/" + "a".repeat(900);
193 const res = await post({ text: "Long context", page: `https://notes-pull-3.apps.test${long}`, pull: "3" });
194 expect(res.status).toBe(200);
195 const body = listIssues(app.db, repoId, {}).find((i) => i.title === "Long context")!.body;
196 const line = body.split("\n").find((l) => l.startsWith("From:"))!;
197 expect(line.length).toBeLessThanOrEqual(300);
198 expect(line).toContain("(preview of #3)");
199 for (const l of body.split("\n")) expect(l.length).toBeLessThanOrEqual(300);
200 const prod = await post({ text: "Long prod", page: `https://notes.apps.test/${"%41".repeat(200)}` });
201 expect(prod.status).toBe(200);
202 for (const l of listIssues(app.db, repoId, {}).find((i) => i.title === "Long prod")!.body.split("\n"))
203 expect(l.length).toBeLessThanOrEqual(300);
204 });
205
178206 test("a hostile page path is encoded in the filed line, and a hostile pull is refused", async () => {
179207 const res = await post({
180208 text: "Hostile path",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs