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

Press every moved web key in its own place and hear where it went #17230

opened this

Merged 44h 57m ago, live in production

oldkeys-browser → main 0 commits

  • Merged. oldkeys-browser was merged into main as 8192e6e.

Review

Sign in to review this change.
1 file changed +165 −0
test/old-keys-pressed.test.ts added +165
@@ -0,0 +1,165 @@
1 /**
2 * Every web key that moved (keymap.ts OLD_KEYS) says where it went, pressed in a browser on its own
3 * page and in its own context: a pull's Files for its verdicts, /review and /next on a focused row,
4 * a repository's files for the finder, /system for its sort. One Chromium for the file. A new moved
5 * key on a page this table has no place for fails here, so a hint that can never show cannot ship.
6 */
7 import { afterAll, beforeAll, expect, test } from "bun:test";
8 import { mkdtemp, rm } from "node:fs/promises";
9 import { tmpdir } from "node:os";
10 import { join } from "node:path";
11 import { openIssue } from "../src/actions/issues.ts";
12 import { openPull } from "../src/actions/pulls.ts";
13 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
14 import { createApp, type App } from "../src/app.ts";
15 import { getPull } from "../src/core/pulls.ts";
16 import { claimRun, createRun, finishRun } from "../src/core/runs.ts";
17 import { createSession } from "../src/core/sessions.ts";
18 import { createUser, type User } from "../src/core/users.ts";
19 import { serve, type Servers } from "../src/http/server.ts";
20 import { findChromium } from "../src/looks/capture.ts";
21 import { OLD_KEYS, OLD_KEYS_UNTIL, oldKeys, provideOldKeysClockForTest, type OldKey } from "../src/web/keymap.ts";
22 import { chromiumRuns, sharedBrowser, type SharedBrowser } from "./helpers/chrome.ts";
23 import { quietLogs } from "./helpers/globals.ts";
24
25 quietLogs();
26 const found = findChromium();
27 const chromium = found !== undefined && chromiumRuns(found) ? found : undefined;
28 const skip = chromium === undefined && (process.env["FORGE_CHECK"] ?? "") === "";
29 if (skip) console.log("SKIPPING: no chromium that starts on this machine, so the moved keys are not pressed");
30
31 /** The terminal's pages: their moved keys are pressed by the terminal's own tests. */
32 const TERMINAL: readonly OldKey["page"][] = ["terminal", "runs", "cockpit"];
33
34 let dir: string;
35 let app: App;
36 let servers: Servers;
37 let alice: User;
38 let pullNumber = 0;
39 let browser: SharedBrowser | undefined;
40
41 beforeAll(async () => {
42 dir = await mkdtemp(join(tmpdir(), "forge-old-keys-"));
43 app = createApp({ dataDir: dir, port: 0 });
44 servers = await serve(app);
45 const made = await createUser(app.db, {
46 origin: "seed",
47 trusted: true,
48 username: "alice",
49 email: "[email protected]",
50 password: "a-good-passphrase",
51 adminIfFirst: true,
52 });
53 if (!made.ok) throw new Error(made.error);
54 alice = made.value;
55 const bob = await createUser(app.db, {
56 origin: "seed",
57 trusted: true,
58 username: "bob",
59 email: "[email protected]",
60 password: "a-good-passphrase",
61 });
62 if (!bob.ok) throw new Error(bob.error);
63 const r = await createRepo(app, alice, { name: "keys", visibility: "public" });
64 if (!r.ok) throw new Error(JSON.stringify(r.error));
65 const repo = r.value;
66 const seeded = await commitToBranch(app, alice, repo, { message: "Start", files: [{ path: "README.md", content: "# keys\n" }] });
67 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
68 // /next's rows.
69 for (const t of ["Later gate: not now", "Row A", "Row B"]) {
70 const o = openIssue(app, alice, repo, { title: t });
71 if (!o.ok) throw new Error(String(o.error));
72 }
73 // A green pull: alice's ask on /review. Its author is then bob, so alice may approve it on Files.
74 const branch = await commitToBranch(app, alice, repo, {
75 branch: "feature/x",
76 base: "main",
77 message: "x",
78 files: [{ path: "x.ts", content: "export const x = 1;\n" }],
79 });
80 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
81 const opened = await openPull(app, alice, repo, { title: "A change", baseBranch: "main", headBranch: "feature/x" });
82 if (!opened.ok) throw new Error(String(opened.error));
83 const pull = getPull(app.db, repo.id, opened.value.number)!;
84 const run = createRun(app.db, {
85 repoId: repo.id,
86 kind: "check",
87 issueId: pull.id,
88 env: `pull-${pull.number}`,
89 commitOid: pull.headOid,
90 reason: "pull",
91 });
92 claimRun(app.db, run.id);
93 finishRun(app.db, run.id, { state: "ok", summary: "ok" });
94 pullNumber = pull.number;
95 if (chromium !== undefined) browser = await sharedBrowser(chromium);
96 }, 60_000);
97
98 afterAll(async () => {
99 await browser?.close();
100 servers.stop();
101 app.close();
102 await rm(dir, { recursive: true, force: true });
103 });
104
105 /**
106 * Where each page's moved keys are pressed, and what has the focus first ("" for the page itself).
107 */
108 const PLACES: Record<Exclude<OldKey["page"], "terminal" | "runs" | "cockpit">, { path: () => string; focus: string }> = {
109 pull: { path: () => `/alice/keys/pulls/${String(pullNumber)}/files`, focus: "" },
110 review: { path: () => "/review", focus: "li[data-ask] [data-nav]" },
111 files: { path: () => "/alice/keys/tree/main", focus: "" },
112 next: { path: () => "/alice/keys/next", focus: "li[data-number] a" },
113 system: { path: () => "/system", focus: "" },
114 };
115
116 test("every web page with a moved key has a place in this table", () => {
117 const pages = [...new Set(OLD_KEYS.map((o) => o.page))].filter((p) => !TERMINAL.includes(p));
118 expect(pages.filter((p) => !(p in PLACES))).toEqual([]);
119 });
120
121 // One page load per page: its moved keys are pressed in turn in the same context, each read before
122 // the next (a key's hint goes when the next key is pressed). One test each keeps every test quick.
123 let ready = false;
124 const setUp = (): void => {
125 if (ready) return;
126 ready = true;
127 // The pull's author is bob from here, so alice is offered every verdict on its Files.
128 app.db.query("UPDATE issue SET author_id = (SELECT id FROM user WHERE username = 'bob') WHERE kind = 'pull'").run();
129 };
130
131 for (const [page, place] of Object.entries(PLACES) as [keyof typeof PLACES, (typeof PLACES)[keyof typeof PLACES]][]) {
132 test.skipIf(skip)(`${page}: each moved key, pressed in its place, says where it went`, async () => {
133 if (browser === undefined) throw new Error("no Chromium starts in this check, so the moved keys are not pressed: the check's image must carry one (FORGE_CHROMIUM)");
134 setUp();
135 provideOldKeysClockForTest(new Date(Date.parse(OLD_KEYS_UNTIL) - 86_400_000));
136 try {
137 const moved = oldKeys(page);
138 const cookie = [{ name: "forge_session", value: createSession(app.db, alice.id).token }];
139 const said = JSON.parse(
140 await browser.evaluate(
141 `${servers.url}${place.path()}`,
142 { width: 1280, height: 900 },
143 cookie,
144 `(async () => {
145 const focus = ${JSON.stringify(place.focus)};
146 const heard = [];
147 for (const keys of ${JSON.stringify(moved.map((o) => o.was.split(" ")))}) {
148 const at = focus === "" ? document : document.querySelector(focus);
149 if (at === null) { heard.push("nothing to focus: " + focus); continue; }
150 if (at !== document) at.focus();
151 for (const k of keys) at.dispatchEvent(new KeyboardEvent("keydown", { key: k, bubbles: true, cancelable: true }));
152 await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
153 heard.push(document.querySelector(".toast[data-key-says]")?.textContent.trim() ?? null);
154 }
155 return JSON.stringify(heard);
156 })()`,
157 ),
158 ) as (string | null)[];
159 expect(moved.length).toBeGreaterThan(0);
160 expect(said).toEqual(moved.map((o) => o.says));
161 } finally {
162 provideOldKeysClockForTest(undefined);
163 }
164 }, 30_000);
165 }

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs