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

test/studio-runs.test.ts and test/studio-shipping.test.ts are split by concept so no test file is over 800 lines #14012

Merged opened this

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

Merged

mason/forge:mason/14011-test-studio-runs-test-ts-and-test-studio → main 2 commits fork

  • Merged. mason/14011-test-studio-runs-test-ts-and-test-studio was merged into main as 5a5ee84.

Review

Sign in to review this change.
16 files changed +4727 −4696
scripts/test-times.json −2
@@ -58,9 +58,7 @@
5858 "test/steer.test.ts": 16.2,
5959 "test/studio-changes.test.ts": 2.3,
6060 "test/studio-door.test.ts": 2.1,
61 "test/studio-runs.test.ts": 2.5,
6261 "test/studio-search.test.ts": 2.3,
63 "test/studio-shipping.test.ts": 3.0,
6462 "test/studio-words.test.ts": 17,
6563 "test/studio.test.ts": 35.3,
6664 "test/usage.test.ts": 17.5,
test/helpers/studio-signin.ts added +18
@@ -0,0 +1,18 @@
1 /**
2 * Signs a seeded user in through the forge's own `/signin` form and returns
3 * the session cookie as a `Cookie` header value (`name=value`). The studio's
4 * test files (test/studio-*.test.ts) all sign in this way; it lives here so
5 * each file owns its own forge and fixtures but none copies the form post.
6 */
7 export async function signInCookie(
8 base: string,
9 username: string,
10 password = "a-good-passphrase",
11 ): Promise<string> {
12 const res = await fetch(`${base}/signin`, {
13 method: "POST",
14 body: new URLSearchParams({ username, password }),
15 redirect: "manual",
16 });
17 return (res.headers.get("set-cookie") ?? "").split(";")[0]!;
18 }
test/studio-crew-panel.test.ts added +147
@@ -0,0 +1,147 @@
1 /**
2 * The studio's crew panel on a forge with no agents yet.
3 *
4 * One of the files the studio's suite was split into so no one file sets the
5 * gate's floor (#2038) or passes 800 lines (#14011); the rest are
6 * test/studio*.test.ts, and they sign in through test/helpers/studio-signin.ts.
7 */
8
9 import { signInCookie } from "./helpers/studio-signin.ts";
10 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
11 import { mkdtemp, rm } from "node:fs/promises";
12 import { tmpdir } from "node:os";
13 import { join } from "node:path";
14 import { createApp, type App } from "../src/app.ts";
15 import { enableApp } from "../src/actions/apps.ts";
16 import { addTeamMember, createOrg, createTeam } from "../src/actions/orgs.ts";
17 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
18 import { createUser } from "../src/core/users.ts";
19 import { serve, type Servers } from "../src/http/server.ts";
20
21 describe("the studio's crew panel on a forge with no agents yet", () => {
22 let dataDir: string;
23 let app: App;
24 let servers: Servers;
25 let base: string;
26 let ownerCookie: string;
27 let teamAdminCookie: string;
28
29 const signIn = async (username: string): Promise<string> =>
30 await signInCookie(base, username);
31
32 const get = (path: string, cookie: string) =>
33 fetch(`${base}${path}`, {
34 headers: { cookie: `${cookie}; forge_mode=studio` },
35 redirect: "manual",
36 });
37
38 beforeAll(async () => {
39 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-no-agents-"));
40 app = createApp({ dataDir, port: 0 });
41 servers = await serve(app);
42 base = servers.url;
43 const a = await createUser(app.db, { origin: "seed", trusted: true,
44 username: "alice",
45 email: "[email protected]",
46 password: "a-good-passphrase",
47 adminIfFirst: true,
48 });
49 if (!a.ok) throw new Error(a.error);
50 const alice = a.value;
51 const b = await createUser(app.db, { origin: "seed", trusted: true,
52 username: "bob",
53 email: "[email protected]",
54 password: "a-good-passphrase",
55 });
56 if (!b.ok) throw new Error(b.error);
57 ownerCookie = await signIn("alice");
58 teamAdminCookie = await signIn("bob");
59
60 // alice's own app: she is both the repo's admin and the forge's.
61 const pad = await createRepo(app, alice, {
62 name: "pad",
63 visibility: "public",
64 });
65 if (!pad.ok) throw new Error(String(pad.error));
66 const padSeeded = await commitToBranch(app, alice, pad.value, {
67 message: "Start",
68 files: [{ path: "README.md", content: "pad\n" }],
69 });
70 if (!padSeeded.ok) throw new Error(JSON.stringify(padSeeded.error));
71 const padEnabled = await enableApp(app, alice, pad.value, { slug: "pad" });
72 if (!padEnabled.ok) throw new Error(String(padEnabled.error));
73
74 // an org app whose team admin, bob, is not a forge admin.
75 const org = await createOrg(app, alice, "acme");
76 if (!org.ok) throw new Error(org.error);
77 const team = await createTeam(app, alice, org.value, "leads", "admin");
78 if (!team.ok) throw new Error(String(team.error));
79 const added = await addTeamMember(app, alice, org.value, team.value, "bob");
80 if (!added.ok) throw new Error(String(added.error));
81 const vault = await createRepo(app, alice, {
82 name: "vault",
83 owner: "acme",
84 visibility: "public",
85 });
86 if (!vault.ok) throw new Error(String(vault.error));
87 const vaultSeeded = await commitToBranch(app, alice, vault.value, {
88 message: "Start",
89 files: [{ path: "README.md", content: "vault\n" }],
90 });
91 if (!vaultSeeded.ok) throw new Error(JSON.stringify(vaultSeeded.error));
92 const vaultEnabled = await enableApp(app, alice, vault.value, {
93 slug: "vault",
94 });
95 if (!vaultEnabled.ok) throw new Error(String(vaultEnabled.error));
96 });
97
98 afterAll(async () => {
99 servers.stop();
100 app.close();
101 await rm(dataDir, { recursive: true, force: true });
102 });
103
104 test("a forge admin sees the crew panel name the door an agent is made through, and reaches it in one click without leaving studio mode", async () => {
105 const settings = await (
106 await get("/alice/pad/settings", ownerCookie)
107 ).text();
108 expect(settings).toContain("No agents exist on this forge yet.");
109 expect(settings).not.toContain("Add an agent below");
110 expect(settings).toContain('href="/settings/crew#crew-new"');
111
112 const doorResponse = await get("/settings/crew", ownerCookie);
113 expect(doorResponse.status).toBe(200);
114 const crewPage = await doorResponse.text();
115 expect(crewPage).toContain("New agent");
116 expect(crewPage).toContain('action="/settings/crew"');
117 });
118
119 test("a repository admin who is not a forge admin is told who can add an agent, not sent to a page that refuses them", async () => {
120 const settings = await (
121 await get("/acme/vault/settings", teamAdminCookie)
122 ).text();
123 expect(settings).toContain(
124 "No agents exist on this forge yet. An admin of this forge can add one.",
125 );
126 expect(settings).not.toContain('href="/settings/crew#crew-new"');
127
128 const refused = await get("/settings/crew", teamAdminCookie);
129 expect(refused.status).toBe(403);
130 });
131
132 test("the app page's way to add a crew member leads unbroken to the door an agent is made through, with nothing dev-only in the way", async () => {
133 const appPage = await (await get("/alice/pad", ownerCookie)).text();
134 expect(appPage).toContain("Nobody on the crew yet");
135 expect(appPage).toContain('href="/alice/pad/settings#crew"');
136
137 const settings = await (
138 await get("/alice/pad/settings", ownerCookie)
139 ).text();
140 expect(settings).toContain('href="/settings/crew#crew-new"');
141
142 const door = await get("/settings/crew", ownerCookie);
143 expect(door.status).toBe(200);
144 const doorBody = await door.text();
145 expect(doorBody).toContain("New agent");
146 });
147 });
test/studio-now.test.ts added +367
@@ -0,0 +1,367 @@
1 /**
2 * What \"Now\" says when no agent run is going: production coming up, a tell
3 * nobody on the crew can take, a tell a person holds.
4 *
5 * One of the files the studio's suite was split into so no one file sets the
6 * gate's floor (#2038) or passes 800 lines (#14011); the rest are
7 * test/studio*.test.ts, and they sign in through test/helpers/studio-signin.ts.
8 */
9
10 import { signInCookie } from "./helpers/studio-signin.ts";
11 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
12 import { mkdtemp, rm } from "node:fs/promises";
13 import { tmpdir } from "node:os";
14 import { join } from "node:path";
15 import { createApp, type App } from "../src/app.ts";
16 import { enableApp } from "../src/actions/apps.ts";
17 import type { EnvName } from "../src/apps/data.ts";
18 import { AppsRuntime } from "../src/apps/runtime.ts";
19 import { getAppByRepo, type AppRecord } from "../src/core/apps.ts";
20 import { addToCrew, createAgent } from "../src/actions/crew.ts";
21 import { assignIssue, openIssue } from "../src/actions/issues.ts";
22 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
23 import type { Issue } from "../src/core/issues.ts";
24 import type { Repo } from "../src/core/repos.ts";
25 import { claimRun, createRun, finishRun } from "../src/core/runs.ts";
26 import { createUser, type User } from "../src/core/users.ts";
27 import { serve, type Servers } from "../src/http/server.ts";
28 import { appendRunLog } from "../src/runs/log.ts";
29
30 /**
31 * Issue #163: "Now" is built from agent runs alone, so a fresh app with a
32 * deploy going and nothing else read as if nothing were happening — right
33 * beside the head card tailing that very deploy. "Now"'s empty state now
34 * says otherwise when the head card is tailing a deploy, and keeps its
35 * ordinary wording when nothing at all is going.
36 */
37 describe(`"Now" while production is coming up and nothing else is`, () => {
38 let dataDir: string;
39 let app: App;
40 let servers: Servers;
41 let base: string;
42 let runtime: AppsRuntime;
43 let repo: Repo;
44 let record: AppRecord;
45 let owner: string;
46
47 interface PreviewSlot {
48 readonly app: AppRecord;
49 readonly env: EnvName;
50 readonly chain: Promise<void>;
51 readonly restarts: number[];
52 deploying?: unknown;
53 live?: unknown;
54 lastError?: string;
55 lastRunId?: string;
56 }
57 const putEnv = (
58 env: EnvName,
59 slot: Omit<PreviewSlot, "app" | "env" | "chain" | "restarts">,
60 ): void => {
61 (runtime as unknown as { envs: Map<string, PreviewSlot> }).envs.set(
62 `${record.id}/${env}`,
63 { app: record, env, chain: Promise.resolve(), restarts: [], ...slot },
64 );
65 };
66
67 const signIn = async (username: string): Promise<string> =>
68 await signInCookie(base, username);
69
70 const get = (path: string, cookie: string) =>
71 fetch(`${base}${path}`, {
72 headers: { cookie: `${cookie}; forge_mode=studio` },
73 redirect: "manual",
74 });
75
76 beforeAll(async () => {
77 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-now-empty-"));
78 app = createApp({ dataDir, port: 0 });
79 servers = await serve(app);
80 base = servers.url;
81 runtime = new AppsRuntime(app, {
82 domain: "nowempty.test",
83 scheme: "http",
84 publicPort: servers.port,
85 forgeUrl: servers.url,
86 concurrency: 1,
87 });
88 const a = await createUser(app.db, { origin: "seed", trusted: true,
89 username: "alice",
90 email: "[email protected]",
91 password: "a-good-passphrase",
92 adminIfFirst: true,
93 });
94 if (!a.ok) throw new Error(a.error);
95 owner = await signIn("alice");
96 const made = await createRepo(app, a.value, {
97 name: "ember",
98 visibility: "private",
99 description: "a new app",
100 });
101 if (!made.ok) throw new Error(String(made.error));
102 repo = made.value;
103 const seeded = await commitToBranch(app, a.value, repo, {
104 message: "Start",
105 files: [{ path: "README.md", content: "ember\n" }],
106 });
107 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
108 const enabled = await enableApp(app, a.value, repo, { slug: "ember" });
109 if (!enabled.ok) throw new Error(String(enabled.error));
110 record = getAppByRepo(app.db, repo.id)!;
111 });
112
113 afterAll(async () => {
114 servers.stop();
115 app.close();
116 await rm(dataDir, { recursive: true, force: true });
117 });
118
119 test('a running deploy and no agent run: "Now" does not say nothing is happening, and opens no second tail', async () => {
120 const deployRun = createRun(app.db, {
121 repoId: repo.id,
122 kind: "deploy",
123 env: "production",
124 commitOid: "eae580a",
125 reason: "boot",
126 });
127 claimRun(app.db, deployRun.id);
128 appendRunLog(app.config.dataDir, deployRun.id, "deploy in progress");
129 putEnv("production", {
130 deploying: {
131 run: { id: deployRun.id, commitOid: "eae580a" },
132 abort: new AbortController(),
133 },
134 });
135
136 const page = await (await get("/alice/ember", owner)).text();
137 expect(page).not.toContain("Nothing yet");
138 expect(page).toContain("Production is coming up");
139 // Exactly one tail on the page — the head card's, not a second one in
140 // "Now".
141 expect(page.match(/id="app-run-tail"/g)?.length).toBe(1);
142
143 finishRun(app.db, deployRun.id, { state: "ok", summary: "deploy" });
144 });
145
146 test('with nothing going at all, "Now" reads exactly as it does today — production live and silent says its own quiet line above, not through "Now"', async () => {
147 putEnv("production", {
148 live: {
149 commitOid: "eae580a",
150 handle: { port: 4502 },
151 runId: "irrelevant",
152 since: new Date().toISOString(),
153 manifest: { tty: null },
154 },
155 });
156
157 const page = await (await get("/alice/ember", owner)).text();
158 expect(page).toContain("Nothing yet. Tell the crew something.");
159 // The head card's own quiet line (#284) is not the deploy-in-progress
160 // wording "Now" uses — production here is live, not coming up.
161 expect(page).not.toContain("Production is coming up");
162 expect(page).toContain("Production is quiet");
163 });
164 });
165
166 /**
167 * Issue #183: `tell` cannot route a tell to anyone with nobody on the
168 * crew — the issue it opens stays unassigned forever, not merely
169 * unpicked-up like an agent-held tell. Both surfaces that used to call this
170 * "quiet" or "nothing yet" must say the honest reason instead, and both
171 * failed before this change.
172 */
173 describe('a tell nobody on the crew can take is not "quiet"', () => {
174 let dataDir: string;
175 let app: App;
176 let servers: Servers;
177 let base: string;
178 let repo: Repo;
179 let alice: User;
180 let cookie: string;
181
182 const get = (path: string) =>
183 fetch(`${base}${path}`, {
184 headers: { cookie: `${cookie}; forge_mode=studio` },
185 redirect: "manual",
186 });
187 const post = (path: string, body: Record<string, string>) =>
188 fetch(`${base}${path}`, {
189 method: "POST",
190 headers: { cookie, origin: base },
191 body: new URLSearchParams(body),
192 redirect: "manual",
193 });
194
195 beforeAll(async () => {
196 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-untaken-"));
197 app = createApp({ dataDir, port: 0 });
198 servers = await serve(app);
199 base = servers.url;
200 const a = await createUser(app.db, { origin: "seed", trusted: true,
201 username: "alice",
202 email: "[email protected]",
203 password: "a-good-passphrase",
204 adminIfFirst: true,
205 });
206 if (!a.ok) throw new Error(a.error);
207 alice = a.value;
208 cookie = await signInCookie(base, "alice");
209 const made = await createRepo(app, alice, {
210 name: "pad",
211 visibility: "private",
212 description: "a pad for notes",
213 });
214 if (!made.ok) throw new Error(String(made.error));
215 repo = made.value;
216 const seeded = await commitToBranch(app, alice, repo, {
217 message: "Start",
218 files: [{ path: "README.md", content: "pad\n" }],
219 });
220 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
221 const enabled = await enableApp(app, alice, repo, { slug: "pad" });
222 if (!enabled.ok) throw new Error(String(enabled.error));
223 // No crew at all — the exact state `tell` cannot route out of. Every
224 // test below reads the one tell told into it here.
225 const told = await post("/alice/pad/tell", {
226 text: "Add a dark mode toggle.",
227 });
228 if (told.status !== 303) throw new Error(`tell: ${told.status}`);
229 });
230
231 afterAll(async () => {
232 servers.stop();
233 app.close();
234 await rm(dataDir, { recursive: true, force: true });
235 });
236
237 test('a crewless tell renders a "Now" row, not the empty state, and links to where a crew member is added', async () => {
238 const page = await (await get("/alice/pad")).text();
239 expect(page).not.toContain("Nothing yet. Tell the crew something.");
240 expect(page).toContain("Add a dark mode toggle.");
241 expect(page).toContain("nobody on the crew to take it");
242 expect(page).toContain(`href="/alice/pad/settings"`);
243 });
244
245 test('the shelf row for that app is not "quiet"', async () => {
246 const home = await (await get("/apps")).text();
247 const at = home.indexOf('href="/alice/pad"');
248 expect(at).toBeGreaterThan(-1);
249 const start = home.lastIndexOf("<li>", at);
250 const shelfRow = home.slice(start, home.indexOf("</li>", at) + 5);
251 expect(shelfRow).not.toContain("quiet — tell the crew something");
252 expect(shelfRow).toContain("nobody on the crew to take it");
253 });
254
255 test("a reader of the public app sees the fact, not the owner's invitation or a link to settings", async () => {
256 app.db.run("UPDATE repo SET visibility = 'public' WHERE id = ?", [
257 repo.id,
258 ]);
259 const b = await createUser(app.db, { origin: "seed", trusted: true,
260 username: "bob",
261 email: "[email protected]",
262 password: "a-good-passphrase",
263 });
264 if (!b.ok) throw new Error(b.error);
265 const bobCookie = await signInCookie(base, "bob");
266 const read = await (
267 await fetch(`${base}/alice/pad`, {
268 headers: { cookie: `${bobCookie}; forge_mode=studio` },
269 })
270 ).text();
271 expect(read).toContain("Add a dark mode toggle.");
272 expect(read).toContain("nobody on the crew to take it");
273 expect(read).not.toContain("add someone");
274 expect(read).not.toContain(`href="/alice/pad/settings"`);
275 // The writer still gets the invitation, link included.
276 const write = await (await get("/alice/pad")).text();
277 expect(write).toContain("add someone");
278 expect(write).toContain(`href="/alice/pad/settings"`);
279 });
280 });
281
282 /**
283 * Issue #226: taking a tell yourself — assigning it to a person, not an
284 * agent — used to drop it clean off "Now": the panel's filter only ever
285 * let an agent-held or unassigned tell through, so a person holding it
286 * counted as nobody. `tellWord`'s own fallback said "not picked up yet"
287 * regardless, so the tell's own page agreed with nothing true either.
288 * Both failed before this change.
289 */
290 describe("a tell held by a person still shows on the front door (#226)", () => {
291 let dataDir: string;
292 let app: App;
293 let servers: Servers;
294 let base: string;
295 let repo: Repo;
296 let alice: User;
297 let cookie: string;
298 let tell: Issue;
299
300 const get = (path: string) =>
301 fetch(`${base}${path}`, {
302 headers: { cookie: `${cookie}; forge_mode=studio` },
303 redirect: "manual",
304 });
305
306 beforeAll(async () => {
307 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-person-held-"));
308 app = createApp({ dataDir, port: 0 });
309 servers = await serve(app);
310 base = servers.url;
311 const a = await createUser(app.db, { origin: "seed", trusted: true,
312 username: "alice",
313 email: "[email protected]",
314 password: "a-good-passphrase",
315 adminIfFirst: true,
316 });
317 if (!a.ok) throw new Error(a.error);
318 alice = a.value;
319 cookie = await signInCookie(base, "alice");
320 const made = await createRepo(app, alice, {
321 name: "pad",
322 visibility: "private",
323 description: "a pad for notes",
324 });
325 if (!made.ok) throw new Error(String(made.error));
326 repo = made.value;
327 const seeded = await commitToBranch(app, alice, repo, {
328 message: "Start",
329 files: [{ path: "README.md", content: "pad\n" }],
330 });
331 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
332 const enabled = await enableApp(app, alice, repo, { slug: "pad" });
333 if (!enabled.ok) throw new Error(String(enabled.error));
334 const scout = await createAgent(app, alice, { username: "scout" });
335 if (!scout.ok) throw new Error(String(scout.error));
336 expect(addToCrew(app, alice, repo, "scout").ok).toBe(true);
337
338 const told = openIssue(app, alice, repo, {
339 title: "Give the header a subtitle",
340 });
341 if (!told.ok) throw new Error(String(told.error));
342 tell = told.value;
343 // Taken by a person — the owner herself — not routed to the crew.
344 expect(assignIssue(app, alice, repo, tell, alice).ok).toBe(true);
345 });
346
347 afterAll(async () => {
348 servers.stop();
349 app.close();
350 await rm(dataDir, { recursive: true, force: true });
351 });
352
353 test('"Now" names the person who holds it, not "Nothing yet"', async () => {
354 const page = await (await get("/alice/pad")).text();
355 expect(page).not.toContain("Nothing yet. Tell the crew something.");
356 expect(page).toContain("Give the header a subtitle");
357 expect(page).toContain("alice");
358 expect(page).not.toContain("not picked up yet");
359 });
360
361 test("the tell's own page agrees with the panel: held, not unpicked", async () => {
362 const page = await (await get(`/alice/pad/issues/${tell.number}`)).text();
363 expect(page).not.toContain("not picked up yet");
364 expect(page).toContain(">alice</a>");
365 expect(page).toContain("has it");
366 });
367 });
test/studio-preview-speed.test.ts added +582
@@ -0,0 +1,582 @@
1 /**
2 * How fast a change reaches its preview: `servedPhrase`, `previewLatency`,
3 * and the studio's own words for it.
4 *
5 * One of the files the studio's suite was split into so no one file sets the
6 * gate's floor (#2038) or passes 800 lines (#14011); the rest are
7 * test/studio*.test.ts, and they sign in through test/helpers/studio-signin.ts.
8 */
9
10 import { signInCookie } from "./helpers/studio-signin.ts";
11 import {
12 afterAll,
13 beforeAll,
14 beforeEach,
15 describe,
16 expect,
17 test,
18 } from "bun:test";
19 import { mkdtemp, rm } from "node:fs/promises";
20 import { tmpdir } from "node:os";
21 import { join } from "node:path";
22 import { createApp, type App } from "../src/app.ts";
23 import { enableApp } from "../src/actions/apps.ts";
24 import type { EnvStatus } from "../src/apps/runtime.ts";
25 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
26 import { type Repo } from "../src/core/repos.ts";
27 import {
28 claimRun,
29 createRun,
30 finishRun,
31 markServing,
32 previewLatency,
33 STOPPED_SUMMARY,
34 } from "../src/core/runs.ts";
35 import { createUser, type User } from "../src/core/users.ts";
36 import { serve, type Servers } from "../src/http/server.ts";
37 import { servedPhrase } from "../src/web/ui.ts";
38
39 /**
40 * The one function every page words a deployment's speed with. The pages
41 * above prove it reaches the studio; these prove what it says at the edges,
42 * where a real deploy is awkward to arrange.
43 */
44 describe("servedPhrase", () => {
45 const env = (extra: Partial<EnvStatus>): EnvStatus => ({
46 env: "production",
47 state: "running",
48 commitOid: "cafebabe",
49 url: "http://vault.apps.test",
50 ...extra,
51 });
52
53 test("says nothing at all until the deployment serves", () => {
54 expect(
55 servedPhrase(
56 env({
57 createdAt: "2026-01-01T00:00:00.000Z",
58 deployStartedAt: "2026-01-01T00:00:02.000Z",
59 }),
60 ),
61 ).toBeUndefined();
62 });
63
64 test("clocks that disagree clamp to zero rather than printing a negative", () => {
65 expect(
66 servedPhrase(
67 env({
68 createdAt: "2026-01-01T00:00:00.300Z",
69 deployStartedAt: "2026-01-01T00:00:00.300Z",
70 servingAt: "2026-01-01T00:00:00.000Z",
71 }),
72 ),
73 ).toBe("0.0s after the push");
74 // A start before the push it answers is skew too: no negative wait.
75 expect(
76 servedPhrase(
77 env({
78 createdAt: "2026-01-01T00:00:05.000Z",
79 deployStartedAt: "2026-01-01T00:00:00.000Z",
80 servingAt: "2026-01-01T00:00:06.000Z",
81 }),
82 ),
83 ).toBe("1.0s after the push");
84 });
85
86 test("a wait under a second is noise: the phrase reads as it always has", () => {
87 expect(
88 servedPhrase(
89 env({
90 createdAt: "2026-01-01T00:00:00.000Z",
91 deployStartedAt: "2026-01-01T00:00:00.900Z",
92 servingAt: "2026-01-01T00:00:04.300Z",
93 }),
94 ),
95 ).toBe("4.3s after the push");
96 });
97
98 test("a push with no start recorded is still measured from the push", () => {
99 expect(
100 servedPhrase(
101 env({
102 createdAt: "2026-01-01T00:00:00.000Z",
103 servingAt: "2026-01-01T00:00:02.500Z",
104 }),
105 ),
106 ).toBe("2.5s after the push");
107 });
108
109 test("a deployment with neither instant, and an unreadable one, say nothing", () => {
110 expect(
111 servedPhrase(env({ servingAt: "2026-01-01T00:00:02.500Z" })),
112 ).toBeUndefined();
113 expect(
114 servedPhrase(
115 env({ createdAt: "not a time", servingAt: "2026-01-01T00:00:02.500Z" }),
116 ),
117 ).toBeUndefined();
118 });
119 });
120
121 describe("previewLatency: the median seconds from push to serving", () => {
122 let dataDir: string;
123 let app: App;
124 let alice: User;
125 let repo: Repo;
126
127 beforeAll(async () => {
128 dataDir = await mkdtemp(join(tmpdir(), "forge-preview-latency-"));
129 app = createApp({ dataDir, port: 0 });
130 const a = await createUser(app.db, { origin: "seed", trusted: true,
131 username: "alice",
132 email: "[email protected]",
133 password: "a-good-passphrase",
134 adminIfFirst: true,
135 });
136 if (!a.ok) throw new Error(a.error);
137 alice = a.value;
138 const made = await createRepo(app, alice, {
139 name: "latency",
140 visibility: "private",
141 description: "",
142 });
143 if (!made.ok) throw new Error(String(made.error));
144 repo = made.value;
145 });
146
147 afterAll(async () => {
148 app.close();
149 await rm(dataDir, { recursive: true, force: true });
150 });
151
152 // Each test reads only the deploys it seeds: the window has no upper
153 // bound, so another test's deploys would otherwise land in it.
154 beforeEach(() => {
155 app.db.query("DELETE FROM run WHERE repo_id = ?1").run(repo.id);
156 });
157
158 /**
159 * A deploy run stamped with a known push instant and, unless null, known started/serving
160 * instants.
161 */
162 const seedDeploy = (
163 createdAt: string,
164 startedAt: string | null,
165 servingAt: string | null,
166 ): void => {
167 const deploy = createRun(app.db, { repoId: repo.id, kind: "deploy" });
168 app.db
169 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
170 .run(deploy.id, createdAt);
171 if (startedAt !== null) {
172 app.db
173 .query("UPDATE run SET started_at = ?2 WHERE id = ?1")
174 .run(deploy.id, startedAt);
175 }
176 if (servingAt !== null) {
177 app.db
178 .query("UPDATE run SET serving_at = ?2 WHERE id = ?1")
179 .run(deploy.id, servingAt);
180 }
181 };
182
183 test("no deploys, a never-served one, and the median of a growing set — all against one window", () => {
184 const now = Date.now();
185 const since = new Date(now - 60_000).toISOString();
186
187 // Nothing seeded yet: undefined, never a zero.
188 expect(previewLatency(app.db, repo.id, since)).toBeUndefined();
189
190 // Before the window: a very slow deploy that must not pull the median up.
191 seedDeploy(
192 new Date(now - 10 * 24 * 60 * 60_000).toISOString(),
193 null,
194 new Date(now - 10 * 24 * 60 * 60_000 + 600_000).toISOString(),
195 );
196 expect(previewLatency(app.db, repo.id, since)).toBeUndefined();
197
198 // A deploy that never served is left out, not counted as a zero.
199 seedDeploy(new Date(now).toISOString(), null, null);
200 expect(previewLatency(app.db, repo.id, since)).toBeUndefined();
201
202 // Three served deploys, seeded out of order: an odd-count median.
203 const durations = [2, 6, 4]; // seconds
204 durations.forEach((d, i) => {
205 const createdAt = new Date(now + i * 1_000).toISOString();
206 seedDeploy(
207 createdAt,
208 null,
209 new Date(Date.parse(createdAt) + d * 1_000).toISOString(),
210 );
211 });
212 const odd = previewLatency(app.db, repo.id, since)!;
213 expect(odd.n).toBe(3);
214 expect(odd.medianSeconds).toBeCloseTo(4, 2);
215
216 // A fourth deploy, 10s: the even-count median is the mean of the middle two.
217 const createdAt = new Date(now + 3_000).toISOString();
218 seedDeploy(
219 createdAt,
220 null,
221 new Date(Date.parse(createdAt) + 10_000).toISOString(),
222 );
223 const even = previewLatency(app.db, repo.id, since)!;
224 expect(even.n).toBe(4);
225 expect(even.medianSeconds).toBeCloseTo(5, 2);
226
227 // None of the above carried a `started_at`: the split has nothing to say.
228 expect(even.queueMedianSeconds).toBeUndefined();
229 expect(even.bootMedianSeconds).toBeUndefined();
230 expect(even.splitN).toBe(0);
231 });
232
233 test("the split between waiting to be picked up and coming up, seeded beside a total", () => {
234 const now = Date.now();
235 const since = new Date(now - 60_000).toISOString();
236
237 // Three deploys with known queue and boot segments: (queue, boot) in seconds.
238 const segments: readonly (readonly [number, number])[] = [
239 [1, 3],
240 [2, 6],
241 [3, 9],
242 ];
243 segments.forEach(([queue, boot], i) => {
244 const createdAt = now + i * 1_000;
245 const startedAt = createdAt + queue * 1_000;
246 const servingAt = startedAt + boot * 1_000;
247 seedDeploy(
248 new Date(createdAt).toISOString(),
249 new Date(startedAt).toISOString(),
250 new Date(servingAt).toISOString(),
251 );
252 });
253 const full = previewLatency(app.db, repo.id, since)!;
254 expect(full.n).toBe(3);
255 expect(full.splitN).toBe(3);
256 expect(full.queueMedianSeconds).toBeCloseTo(2, 2);
257 expect(full.bootMedianSeconds).toBeCloseTo(6, 2);
258
259 // A fourth, served deploy with no `started_at`: it feeds the total but
260 // is left out of the split, never counted as a zero-length queue or boot.
261 const createdAt = now + 3_000;
262 seedDeploy(
263 new Date(createdAt).toISOString(),
264 null,
265 new Date(createdAt + 100_000).toISOString(),
266 );
267 const withGap = previewLatency(app.db, repo.id, since)!;
268 expect(withGap.n).toBe(4);
269 expect(withGap.splitN).toBe(3);
270 expect(withGap.queueMedianSeconds).toBeCloseTo(2, 2);
271 expect(withGap.bootMedianSeconds).toBeCloseTo(6, 2);
272 });
273
274 test("a deploy that finished without ever serving is counted apart, never in the median; one still running is counted as neither", () => {
275 const now = Date.now();
276 const since = new Date(now - 60_000).toISOString();
277
278 // Two served deploys, 4s and 6s.
279 [4, 6].forEach((d, i) => {
280 const createdAt = new Date(now + i * 1_000).toISOString();
281 seedDeploy(
282 createdAt,
283 null,
284 new Date(Date.parse(createdAt) + d * 1_000).toISOString(),
285 );
286 });
287 const clean = previewLatency(app.db, repo.id, since)!;
288 expect(clean.n).toBe(2);
289 expect(clean.failedN).toBe(0);
290 expect(clean.medianSeconds).toBeCloseTo(5, 2);
291
292 // A deploy still queued: neither served nor finished. Moves neither number.
293 const inFlight = createRun(app.db, { repoId: repo.id, kind: "deploy" });
294 app.db
295 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
296 .run(inFlight.id, new Date(now + 2_000).toISOString());
297 const withInFlight = previewLatency(app.db, repo.id, since)!;
298 expect(withInFlight.n).toBe(2);
299 expect(withInFlight.failedN).toBe(0);
300 expect(withInFlight.medianSeconds).toBeCloseTo(5, 2);
301
302 // A deploy that finished but never served — counted as failed, never a
303 // zero-second deploy pulling the median down.
304 const failed = createRun(app.db, { repoId: repo.id, kind: "deploy" });
305 app.db
306 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
307 .run(failed.id, new Date(now + 3_000).toISOString());
308 claimRun(app.db, failed.id);
309 finishRun(app.db, failed.id, { state: "failed", summary: "build failed" });
310 const withFailed = previewLatency(app.db, repo.id, since)!;
311 expect(withFailed.n).toBe(2);
312 expect(withFailed.failedN).toBe(1);
313 expect(withFailed.medianSeconds).toBeCloseTo(5, 2);
314
315 // A cancelled deploy that never served also counts as failed.
316 const cancelled = createRun(app.db, { repoId: repo.id, kind: "deploy" });
317 app.db
318 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
319 .run(cancelled.id, new Date(now + 4_000).toISOString());
320 claimRun(app.db, cancelled.id);
321 app.db
322 .query(
323 "UPDATE run SET state = 'cancelled', finished_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?1",
324 )
325 .run(cancelled.id);
326 const withCancelled = previewLatency(app.db, repo.id, since)!;
327 expect(withCancelled.n).toBe(2);
328 expect(withCancelled.failedN).toBe(2);
329 expect(withCancelled.medianSeconds).toBeCloseTo(5, 2);
330 });
331
332 test("a week with only never-served deploys still reports the failed count, with no median to give", () => {
333 const now = Date.now();
334 const since = new Date(now - 60_000).toISOString();
335
336 const failed = createRun(app.db, { repoId: repo.id, kind: "deploy" });
337 app.db
338 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
339 .run(failed.id, new Date(now).toISOString());
340 claimRun(app.db, failed.id);
341 finishRun(app.db, failed.id, { state: "failed", summary: "build failed" });
342
343 const latency = previewLatency(app.db, repo.id, since)!;
344 expect(latency).toBeDefined();
345 expect(latency.n).toBe(0);
346 expect(latency.failedN).toBe(1);
347 expect(latency.medianSeconds).toBeUndefined();
348 });
349
350 test("a deploy the forge's own restart cancelled is never counted as failed, nor in the total it is 'of' (#255, #274)", () => {
351 const now = Date.now();
352 const since = new Date(now - 60_000).toISOString();
353
354 // A real failure: counted.
355 const failed = createRun(app.db, { repoId: repo.id, kind: "deploy" });
356 app.db
357 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
358 .run(failed.id, new Date(now).toISOString());
359 claimRun(app.db, failed.id);
360 finishRun(app.db, failed.id, { state: "failed", summary: "build failed" });
361
362 // A deploy the forge's own restart cancelled: not this commit's fault,
363 // and not counted at all — neither as a failure nor in the total.
364 const stopped = createRun(app.db, { repoId: repo.id, kind: "deploy" });
365 app.db
366 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
367 .run(stopped.id, new Date(now + 1_000).toISOString());
368 claimRun(app.db, stopped.id);
369 app.db
370 .query(
371 "UPDATE run SET state = 'cancelled', summary = ?2, finished_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?1",
372 )
373 .run(stopped.id, STOPPED_SUMMARY);
374
375 const latency = previewLatency(app.db, repo.id, since)!;
376 expect(latency.n).toBe(0);
377 expect(latency.failedN).toBe(1);
378
379 // A person's own Cancel, carrying a different summary, still counts —
380 // only the forge's own reason is left out.
381 const byPerson = createRun(app.db, { repoId: repo.id, kind: "deploy" });
382 app.db
383 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
384 .run(byPerson.id, new Date(now + 2_000).toISOString());
385 claimRun(app.db, byPerson.id);
386 app.db
387 .query(
388 "UPDATE run SET state = 'cancelled', summary = 'cancelled by alice', finished_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?1",
389 )
390 .run(byPerson.id);
391 const withPerson = previewLatency(app.db, repo.id, since)!;
392 expect(withPerson.failedN).toBe(2);
393 });
394 });
395
396 describe("the studio's own words for how fast a change reaches its preview", () => {
397 let dataDir: string;
398 let app: App;
399 let servers: Servers;
400 let base: string;
401 let alice: User;
402 let repo: Repo;
403 let ownerCookie: string;
404
405 beforeAll(async () => {
406 dataDir = await mkdtemp(join(tmpdir(), "forge-preview-latency-page-"));
407 app = createApp({ dataDir, port: 0 });
408 servers = await serve(app);
409 base = servers.url;
410 const a = await createUser(app.db, { origin: "seed", trusted: true,
411 username: "alice",
412 email: "[email protected]",
413 password: "a-good-passphrase",
414 adminIfFirst: true,
415 });
416 if (!a.ok) throw new Error(a.error);
417 alice = a.value;
418 ownerCookie = await signInCookie(base, "alice");
419 const made = await createRepo(app, alice, {
420 name: "trickle",
421 visibility: "private",
422 description: "",
423 });
424 if (!made.ok) throw new Error(String(made.error));
425 repo = made.value;
426 const seeded = await commitToBranch(app, alice, repo, {
427 message: "Start",
428 files: [{ path: "README.md", content: "start\n" }],
429 });
430 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
431 const enabled = await enableApp(app, alice, repo, { slug: "trickle" });
432 if (!enabled.ok) throw new Error(String(enabled.error));
433 // Enough history behind it that these tests exercise the preview
434 // wording, not the day-one line (#274) — a finished agent run from
435 // well outside this week's own window.
436 const seedRun = createRun(app.db, { repoId: repo.id, kind: "agent" });
437 app.db
438 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
439 .run(
440 seedRun.id,
441 new Date(Date.now() - 30 * 24 * 60 * 60_000).toISOString(),
442 );
443 claimRun(app.db, seedRun.id);
444 finishRun(app.db, seedRun.id, { state: "ok", summary: "seed" });
445 });
446
447 afterAll(async () => {
448 servers.stop();
449 app.close();
450 await rm(dataDir, { recursive: true, force: true });
451 });
452
453 // Each test reads only the deploys it seeds; the agent run from
454 // beforeAll stays, so the page always has history behind it.
455 beforeEach(() => {
456 app.db
457 .query("DELETE FROM run WHERE repo_id = ?1 AND kind = 'deploy'")
458 .run(repo.id);
459 });
460
461 const get = (path: string) =>
462 fetch(`${base}${path}`, {
463 headers: { cookie: `${ownerCookie}; forge_mode=studio` },
464 redirect: "manual",
465 });
466
467 /**
468 * A deploy run stamped with a known push instant and serving instant, and unless null a known
469 * started instant.
470 */
471 const seedDeploy = (
472 createdAt: string,
473 startedAt: string | null,
474 servingAt: string,
475 ): void => {
476 const deploy = createRun(app.db, { repoId: repo.id, kind: "deploy" });
477 app.db
478 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
479 .run(deploy.id, createdAt);
480 if (startedAt !== null) {
481 app.db
482 .query("UPDATE run SET started_at = ?2 WHERE id = ?1")
483 .run(deploy.id, startedAt);
484 }
485 app.db
486 .query("UPDATE run SET serving_at = ?2 WHERE id = ?1")
487 .run(deploy.id, servingAt);
488 };
489
490 test("fewer than three served deploys this week says so plainly rather than a median of one", async () => {
491 const before = await (await get("/alice/trickle")).text();
492 expect(before).toContain("not enough deploys this week to say");
493 expect(before).not.toContain("reaches its preview in");
494
495 const deploy = createRun(app.db, {
496 repoId: repo.id,
497 kind: "deploy",
498 env: "production",
499 reason: "push",
500 });
501 markServing(app.db, deploy.id);
502 const one = await (await get("/alice/trickle")).text();
503 expect(one).toContain("not enough deploys this week to say");
504 expect(one).not.toContain("reaches its preview in");
505 });
506
507 test("the split reads as the total's explanation, and says when it is drawn from fewer deploys", async () => {
508 // Three deploys with a known queue and boot each.
509 const now = Date.now();
510 const segments: readonly (readonly [number, number])[] = [
511 [1, 3],
512 [2, 6],
513 [3, 9],
514 ];
515 segments.forEach(([queue, boot], i) => {
516 const createdAt = now + i * 1_000;
517 const startedAt = createdAt + queue * 1_000;
518 const servingAt = startedAt + boot * 1_000;
519 seedDeploy(
520 new Date(createdAt).toISOString(),
521 new Date(startedAt).toISOString(),
522 new Date(servingAt).toISOString(),
523 );
524 });
525 // A fourth, served deploy with no `started_at`: it feeds the total but
526 // is left out of the split.
527 seedDeploy(
528 new Date(now + 3_000).toISOString(),
529 null,
530 new Date(now + 100_000).toISOString(),
531 );
532 const page = await (await get("/alice/trickle")).text();
533 expect(page).toContain("4 deploys");
534 expect(page).toContain("2.0s waiting");
535 // Only 3 of the 4 served deploys carried a `started_at`: the split says so.
536 expect(page).toContain("6.0s coming up (3 of 4)");
537
538 // A clean week — every deploy served — reads exactly as it always has:
539 // no mention of failures, no empty clause left dangling.
540 expect(page).not.toContain("never reached a preview");
541 expect(page).not.toContain("0 failed");
542 });
543
544 test("a deploy that finished without ever serving is counted and worded, and links to the runs list", async () => {
545 // Four served deploys first, so the failed one is the fifth.
546 const now = Date.now();
547 for (let i = 0; i < 4; i++) {
548 seedDeploy(
549 new Date(now + i * 1_000).toISOString(),
550 null,
551 new Date(now + i * 1_000 + 5_000).toISOString(),
552 );
553 }
554 const before = await (await get("/alice/trickle")).text();
555
556 const failed = createRun(app.db, {
557 repoId: repo.id,
558 kind: "deploy",
559 env: "production",
560 reason: "push",
561 });
562 claimRun(app.db, failed.id);
563 finishRun(app.db, failed.id, { state: "failed", summary: "build failed" });
564
565 const after = await (await get("/alice/trickle")).text();
566 expect(after).toContain("1 of 5 never reached a preview");
567 expect(after).toContain(`href="/alice/trickle/runs?kind=deploy"`);
568
569 // A deploy still running moves neither the served count nor the failed
570 // count: it is neither served nor finished.
571 const inFlight = createRun(app.db, {
572 repoId: repo.id,
573 kind: "deploy",
574 env: "production",
575 reason: "push",
576 });
577 claimRun(app.db, inFlight.id);
578 const withInFlight = await (await get("/alice/trickle")).text();
579 expect(withInFlight).toContain("1 of 5 never reached a preview");
580 expect(withInFlight).not.toBe(before);
581 });
582 });
test/studio-run-page.test.ts added +763
@@ -0,0 +1,763 @@
1 /**
2 * A run's own page in the studio: its head, transcript, steer box, cancel,
3 * the one live stream, and a check's step rows.
4 *
5 * One of the files the studio's suite was split into so no one file sets the
6 * gate's floor (#2038) or passes 800 lines (#14011); the rest are
7 * test/studio*.test.ts, and they sign in through test/helpers/studio-signin.ts.
8 */
9
10 import { signInCookie } from "./helpers/studio-signin.ts";
11 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
12 import { writeFileSync } from "node:fs";
13 import { mkdtemp, rm } from "node:fs/promises";
14 import { looksPath } from "../src/checks/looks.ts";
15 import { stepsLine } from "../src/checks/progress.ts";
16 import { tmpdir } from "node:os";
17 import { join } from "node:path";
18 import { createApp, type App } from "../src/app.ts";
19 import { enableApp } from "../src/actions/apps.ts";
20 import { addToCrew, createAgent } from "../src/actions/crew.ts";
21 import { openIssue } from "../src/actions/issues.ts";
22 import { openPull } from "../src/actions/pulls.ts";
23 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
24 import type { Repo } from "../src/core/repos.ts";
25 import {
26 claimRun,
27 createRun,
28 cancelRun,
29 finishRun,
30 noteRunProgress,
31 } from "../src/core/runs.ts";
32 import { createUser, type User } from "../src/core/users.ts";
33 import { serve, type Servers } from "../src/http/server.ts";
34 import { appendRunLog } from "../src/runs/log.ts";
35 import { workBegins, workEnds } from "../src/runs/ending.ts";
36 import { withScripts } from "./helpers/page-script.ts";
37
38 /**
39 * Working, on its own page: `/:owner/:name/runs/:id` in the studio names
40 * the tell, the cost, and offers the steer box — same routes and checks as
41 * dev's transcript, one click away at `?mode=dev`.
42 */
43 describe("working: a run's own page in the studio", () => {
44 let dataDir: string;
45 let app: App;
46 let servers: Servers;
47 let base: string;
48 let repo: Repo;
49 let alice: User;
50 let scoutId: string;
51 let writerCookie: string;
52 let readerCookie: string;
53
54 const signIn = async (username: string): Promise<string> =>
55 await signInCookie(base, username);
56
57 const get = (path: string, cookie: string) =>
58 fetch(`${base}${path}`, {
59 headers: { cookie: `${cookie}; forge_mode=studio` },
60 redirect: "manual",
61 });
62
63 beforeAll(async () => {
64 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-run-"));
65 app = createApp({ dataDir, port: 0 });
66 servers = await serve(app);
67 base = servers.url;
68 const a = await createUser(app.db, { origin: "seed", trusted: true,
69 username: "alice",
70 email: "[email protected]",
71 password: "a-good-passphrase",
72 adminIfFirst: true,
73 });
74 if (!a.ok) throw new Error(a.error);
75 alice = a.value;
76 const b = await createUser(app.db, { origin: "seed", trusted: true,
77 username: "bob",
78 email: "[email protected]",
79 password: "a-good-passphrase",
80 });
81 if (!b.ok) throw new Error(b.error);
82 writerCookie = await signIn("alice");
83 readerCookie = await signIn("bob");
84 const made = await createRepo(app, a.value, {
85 name: "engine",
86 visibility: "private",
87 description: "an app that builds",
88 });
89 if (!made.ok) throw new Error(String(made.error));
90 repo = made.value;
91 const seeded = await commitToBranch(app, a.value, repo, {
92 message: "Start",
93 files: [{ path: "README.md", content: "engine\n" }],
94 });
95 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
96 const enabled = await enableApp(app, a.value, repo, { slug: "engine" });
97 if (!enabled.ok) throw new Error(String(enabled.error));
98 const scout = await createAgent(app, a.value, { username: "scout" });
99 if (!scout.ok) throw new Error(String(scout.error));
100 scoutId = scout.value.user.id;
101 expect(addToCrew(app, a.value, repo, "scout").ok).toBe(true);
102 });
103
104 afterAll(async () => {
105 servers.stop();
106 app.close();
107 await rm(dataDir, { recursive: true, force: true });
108 });
109
110 test("a run's and a tell's heads draw §5's page head and meta row, not .issue-head or .issue-meta", async () => {
111 const tell = openIssue(app, alice, repo, { title: "Head check" });
112 if (!tell.ok) throw new Error(String(tell.error));
113 const run = createRun(app.db, {
114 repoId: repo.id,
115 kind: "agent",
116 issueId: tell.value.id,
117 actorId: scoutId,
118 reason: "assigned",
119 });
120 const runPage = await (
121 await get(`/alice/engine/runs/${run.id}`, writerCookie)
122 ).text();
123 const tellPage = await (
124 await get(`/alice/engine/issues/${tell.value.number}`, writerCookie)
125 ).text();
126 for (const page of [runPage, tellPage]) {
127 expect(page).not.toContain("issue-head");
128 expect(page).not.toContain("issue-meta");
129 expect(page).toContain('class="page-head"');
130 expect(page).toContain('class="page-head-sub bar muted small"');
131 expect(page).not.toContain('class="sub');
132 }
133 });
134
135 test("the studio transcript names the tell, the cost, and the steer box", async () => {
136 const tell = openIssue(app, alice, repo, {
137 title: "Fix the flicker on load",
138 body: "It flashes white before the page renders.",
139 });
140 if (!tell.ok) throw new Error(String(tell.error));
141 const run = createRun(app.db, {
142 repoId: repo.id,
143 kind: "agent",
144 issueId: tell.value.id,
145 actorId: scoutId,
146 reason: "assigned",
147 });
148 claimRun(app.db, run.id);
149 noteRunProgress(app.db, run.id, { costUsd: 0.42 });
150
151 const page = await (
152 await get(`/alice/engine/runs/${run.id}`, writerCookie)
153 ).text();
154 expect(page).toContain("Fix the flicker on load");
155 // The tell it came from, in the studio's own noun (#158).
156 expect(page).toContain(`href="/alice/engine/issues/${tell.value.number}"`);
157 expect(page).toContain("$0.42 spent");
158 expect(page).toContain('data-word="working"');
159 // An agent's own run still speaks in the agent's name (#195).
160 expect(page).toContain('data-who="scout"');
161 expect(page).toContain('id="run-steer"');
162 expect(page).toContain("Steer scout");
163 // Jump, live and steer draw §5's btn, bar and card (#4263).
164 for (const old of ["run-jump", "run-live", "run-steer"]) {
165 expect(page).not.toMatch(new RegExp(`class="[^"]*\\b${old}\\b`));
166 }
167 expect(page).toContain("newer below");
168 expect(page).toMatch(/<button[^>]*class="btn"[^>]*id="run-jump"/);
169 expect(page).toMatch(
170 /<form[^>]*class="stack"[^>]*id="run-steer"[^>]*action="\/alice\/engine\/runs\/\w+\/steer"/s,
171 );
172 // Plain like the home page's tell box, not a boxed card (#9973).
173 expect(page).not.toMatch(/<form[^>]*class="[^"]*\bcard\b[^"]*"[^>]*id="run-steer"/s);
174 expect(page).toMatch(/<form[^>]*id="run-steer"[^>]*data-api="[^"]+"[^>]*data-running="1"/s);
175 expect(page).toContain('for="steer-text"');
176 // Not shown without an issue to speak on.
177 const bare = createRun(app.db, {
178 repoId: repo.id,
179 kind: "agent",
180 actorId: scoutId,
181 reason: "assigned",
182 });
183 claimRun(app.db, bare.id);
184 const barePage = await (await get(`/alice/engine/runs/${bare.id}`, writerCookie)).text();
185 expect(barePage).not.toContain('id="run-steer"');
186 expect(page).toContain('name="text"');
187 expect(page).toContain('formaction="/alice/engine/runs/');
188 expect(page).toContain('id="run-stop"');
189 // The transcript is on the page, and there is no second reading of it.
190 expect(page).toContain('id="run-log"');
191 expect(page).not.toContain("?mode=dev");
192 // The summary is a plain paragraph, not a page-part class (#3960).
193 expect(page).toMatch(/<p class="mt" id="run-summary">/);
194 expect(page).not.toContain('class="run-summary"');
195 expect(page).not.toContain("run-actions");
196 });
197
198 test("the steer box is a writer's, on an agent run with an issue, never for a reader or a signed-out visitor", async () => {
199 const made = await createRepo(app, alice, {
200 name: "open",
201 visibility: "public",
202 description: "a public app",
203 });
204 if (!made.ok) throw new Error(String(made.error));
205 const open = made.value;
206 const seeded = await commitToBranch(app, alice, open, {
207 message: "Start",
208 files: [{ path: "README.md", content: "open\n" }],
209 });
210 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
211 const tell = openIssue(app, alice, open, { title: "Steer me" });
212 if (!tell.ok) throw new Error(String(tell.error));
213 const mk = (issueId?: string) => {
214 const run = createRun(app.db, {
215 repoId: open.id,
216 kind: "agent",
217 issueId,
218 actorId: scoutId,
219 reason: "assigned",
220 });
221 claimRun(app.db, run.id);
222 return run;
223 };
224 const page = async (id: string, cookie?: string) => {
225 const res = await fetch(`${base}/alice/open/runs/${id}`, {
226 headers: { cookie: `${cookie ?? ""}; forge_mode=studio` },
227 redirect: "manual",
228 });
229 return { status: res.status, text: await res.text() };
230 };
231 const live = mk(tell.value.id);
232 const asWriter = await page(live.id, writerCookie);
233 expect(asWriter.status).toBe(200);
234 expect(asWriter.text).toContain('id="steer-text"');
235 expect(asWriter.text).toContain('id="run-steer"');
236 // A reader of a public repository sees the run, not the box.
237 const asReader = await page(live.id, readerCookie);
238 expect(asReader.status).toBe(200);
239 expect(asReader.text).toContain('data-word=');
240 expect(asReader.text).not.toContain('id="steer-text"');
241 expect(asReader.text).not.toContain('id="run-steer"');
242 const signedOut = await page(live.id);
243 expect(signedOut.status).toBe(200);
244 expect(signedOut.text).toContain('data-word=');
245 expect(signedOut.text).not.toContain('id="steer-text"');
246 expect(signedOut.text).not.toContain('id="run-steer"');
247 // No issue to speak on: no box, even to a writer.
248 const bare = mk();
249 const bareWriter = await page(bare.id, writerCookie);
250 expect(bareWriter.status).toBe(200);
251 expect(bareWriter.text).toContain('data-word=');
252 expect(bareWriter.text).not.toContain('id="steer-text"');
253 // A finished run keeps the box for a writer only, as "tell it for its
254 // next run" (the comment starts a run); a reader never gets it.
255 finishRun(app.db, live.id, { state: "ok", summary: "done" });
256 const over = await page(live.id, writerCookie);
257 expect(over.text).toContain('data-running="0"');
258 expect(over.text).toContain("The run is over");
259 for (const cookie of [readerCookie, undefined]) {
260 const seen = await page(live.id, cookie);
261 expect(seen.status).toBe(200);
262 expect(seen.text).not.toContain('id="steer-text"');
263 expect(seen.text).not.toContain('id="run-steer"');
264 }
265 });
266
267 test("the cancel form sits in a .bar.mt row for an issue-less agent run and a non-agent run", async () => {
268 const agent = createRun(app.db, {
269 repoId: repo.id,
270 kind: "agent",
271 actorId: scoutId,
272 reason: "assigned",
273 });
274 claimRun(app.db, agent.id);
275 const check = createRun(app.db, {
276 repoId: repo.id,
277 kind: "check",
278 reason: "push",
279 });
280 for (const [run, label] of [
281 [agent, "Stop"],
282 [check, "Cancel run"],
283 ] as const) {
284 const page = await (
285 await get(`/alice/engine/runs/${run.id}`, writerCookie)
286 ).text();
287 expect(page).toMatch(
288 new RegExp(
289 `<div class="bar mt" id="run-cancel">\\s*<form method="post" action="/alice/engine/runs/${run.id}/cancel">[\\s\\S]*?${label}[\\s\\S]*?</form>`,
290 ),
291 );
292 }
293 });
294
295 test("a running run's page reads its log from the one /events stream and opens no other", async () => {
296 const run = createRun(app.db, {
297 repoId: repo.id,
298 kind: "agent",
299 actorId: scoutId,
300 reason: "assigned",
301 });
302 claimRun(app.db, run.id);
303 appendRunLog(app.config.dataDir, run.id, "first");
304 appendRunLog(app.config.dataDir, run.id, "second");
305
306 const page = await withScripts(
307 base,
308 await (await get(`/alice/engine/runs/${run.id}`, writerCookie)).text(),
309 );
310 // Every EventSource the page's HTML and script open is /events.
311 const opened = [...page.matchAll(/new EventSource\(([^,;]*)/g)].map(
312 (m) => m[1]!,
313 );
314 expect(opened.length).toBeGreaterThan(0);
315 for (const arg of opened) expect(arg).toStartWith("'/events");
316 // The connection is really opened: liveRegionsJs only connects when the
317 // page has a [data-live] element, and a script's text proves nothing.
318 expect(page).toMatch(/<div[^>]*id="run-head"[^>]*data-live="run\.\*"/);
319 expect(page).toContain('id="run-conn"');
320 expect(page).not.toContain("follow=1");
321 expect(page).not.toContain("data-stream=");
322 // The run's frames arrive on that connection; the file's lines are
323 // counted so none is drawn twice.
324 expect(page).toContain("'run.log'");
325 expect(page).toContain("forge:run-log");
326 expect(page).toContain(`data-run="${run.id}"`);
327 expect(page).toContain('data-seen="2"');
328 expect(page).toContain(">second<");
329 });
330
331 test("a running run's page never reloads itself: its head is the live region, and once over it shows the final state", async () => {
332 const run = createRun(app.db, {
333 repoId: repo.id,
334 kind: "agent",
335 actorId: scoutId,
336 reason: "assigned",
337 });
338 claimRun(app.db, run.id);
339 noteRunProgress(app.db, run.id, { costUsd: 0.25 });
340 const path = `/alice/engine/runs/${run.id}`;
341 const head = (html: string): string =>
342 html.slice(html.indexOf('id="run-head"'), html.indexOf('id="run-live"'));
343
344 const going = await (await get(path, writerCookie)).text();
345 expect(going).not.toMatch(/<meta\s+http-equiv="refresh"/);
346 // Word, cost and clock sit in one region, swapped in place on any
347 // run.* event about this repository and no other.
348 expect(going).toMatch(
349 /<div\s+class="page-head"\s+id="run-head"\s+data-live="run\.\*"\s+data-live-repo="alice\/engine"/,
350 );
351 expect(going).not.toContain("issue-head");
352 expect(going).not.toContain("issue-meta");
353 expect(head(going)).toContain('data-word="working"');
354 expect(head(going)).toContain("$0.25 spent");
355 expect(head(going)).toContain("running for");
356
357 finishRun(app.db, run.id, { state: "ok", summary: "planned" });
358 const over = await (await get(path, writerCookie)).text();
359 expect(over).not.toMatch(/<meta\s+http-equiv="refresh"/);
360 // The same region by id, so the swap on run.finished finds its match,
361 // carrying the final word, cost and summary; nothing is listening now.
362 expect(over).toContain('id="run-head"');
363 expect(over).not.toMatch(/id="run-head"[^>]*data-live=/);
364 expect(head(over)).toContain('data-word="planned"');
365 expect(head(over)).toContain("$0.25 spent");
366 expect(head(over)).toContain("took");
367 expect(head(over)).toContain(">planned<");
368 expect(over).not.toContain('id="run-conn"');
369 expect(over).not.toContain('id="run-stop"');
370 });
371
372 test("a cancelled run still ending its processes keeps its head live and says stopping, then cancelled (#13291)", async () => {
373 const run = createRun(app.db, { repoId: repo.id, kind: "agent", actorId: scoutId, reason: "assigned" });
374 claimRun(app.db, run.id);
375 workBegins(app.db, run.id);
376 cancelRun(app.db, run.id);
377 const path = `/alice/engine/runs/${run.id}`;
378 const head = (html: string): string =>
379 html.slice(html.indexOf('id="run-head"'), html.indexOf('id="run-live"'));
380 try {
381 const ending = await (await get(path, writerCookie)).text();
382 expect(ending).toMatch(/<div\s+class="page-head"\s+id="run-head"\s+data-live="run\.\*"/);
383 expect(head(ending)).toContain('data-word="stopping"');
384 } finally {
385 workEnds(app.db, run.id);
386 }
387 const ended = await (await get(path, writerCookie)).text();
388 expect(ended).not.toMatch(/id="run-head"[^>]*data-live=/);
389 expect(head(ended)).toContain('data-word="cancelled"');
390 });
391
392 test("the runs list files a stopping run under working, not stopped, until its processes have ended (#13291)", async () => {
393 const run = createRun(app.db, { repoId: repo.id, kind: "agent", actorId: scoutId, reason: "assigned" });
394 claimRun(app.db, run.id);
395 workBegins(app.db, run.id);
396 cancelRun(app.db, run.id);
397 const link = `/alice/engine/runs/${run.id}`;
398 const list = async (query: string): Promise<string> =>
399 (await get(`/alice/engine/runs?${query}`, writerCookie)).text();
400 try {
401 // The default agent list shows what is open and moving: a stopping run is.
402 expect(await list("kind=agent")).toContain(link);
403 expect(await list("kind=agent&word=stopped")).not.toContain(link);
404 } finally {
405 workEnds(app.db, run.id);
406 }
407 expect(await list("kind=agent")).not.toContain(link);
408 expect(await list("kind=agent&word=stopped")).toContain(link);
409 });
410
411 test("a long transcript holds its last window, and the whole of it is one step deeper at ?all=1", async () => {
412 const run = createRun(app.db, {
413 repoId: repo.id,
414 kind: "agent",
415 actorId: scoutId,
416 reason: "assigned",
417 });
418 claimRun(app.db, run.id);
419 for (let n = 0; n < 450; n++)
420 appendRunLog(app.config.dataDir, run.id, `line number ${n}`);
421 finishRun(app.db, run.id, { state: "ok", summary: "done" });
422
423 const page = await (
424 await get(`/alice/engine/runs/${run.id}`, writerCookie)
425 ).text();
426 expect(page).not.toContain("line number 0<");
427 expect(page).toContain("line number 449");
428 expect(page).toContain(`href="/alice/engine/runs/${run.id}?all=1"`);
429
430 const all = await (
431 await get(`/alice/engine/runs/${run.id}?all=1`, writerCookie)
432 ).text();
433 expect(all).toContain("line number 0<");
434 expect(all).toContain("line number 449");
435
436 // The depth step is refused exactly as the run is.
437 const anon = await fetch(`${base}/alice/engine/runs/${run.id}?all=1`, {
438 redirect: "manual",
439 });
440 expect(anon.status).toBe(404);
441 });
442
443 test("a reader without access gets the forge's not-found, exactly as dev", async () => {
444 const tell = openIssue(app, alice, repo, { title: "Private work" });
445 if (!tell.ok) throw new Error(String(tell.error));
446 const run = createRun(app.db, {
447 repoId: repo.id,
448 kind: "agent",
449 issueId: tell.value.id,
450 actorId: scoutId,
451 reason: "assigned",
452 });
453 claimRun(app.db, run.id);
454
455 const asReader = await get(`/alice/engine/runs/${run.id}`, readerCookie);
456 expect(asReader.status).toBe(404);
457 });
458
459 test("a finished run renders in the studio's own word, with no Stop", async () => {
460 const tell = openIssue(app, alice, repo, { title: "Tidy the footer" });
461 if (!tell.ok) throw new Error(String(tell.error));
462 const branch = await commitToBranch(app, alice, repo, {
463 branch: "scout/tidy-the-footer",
464 base: "main",
465 message: "Tidy the footer",
466 files: [{ path: "FOOTER.md", content: "tidy\n" }],
467 });
468 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
469 const opened = await openPull(app, alice, repo, {
470 title: "Tidy the footer",
471 body: `Closes #${tell.value.number}`,
472 baseBranch: "main",
473 headBranch: "scout/tidy-the-footer",
474 });
475 if (!opened.ok) throw new Error(String(opened.error));
476 // The word comes from the pull this run actually produced (found by
477 // its branch), not from parsing the summary below.
478 const run = createRun(app.db, {
479 repoId: repo.id,
480 kind: "agent",
481 issueId: tell.value.id,
482 branch: "scout/tidy-the-footer",
483 actorId: scoutId,
484 reason: "assigned",
485 });
486 claimRun(app.db, run.id);
487 finishRun(app.db, run.id, {
488 state: "ok",
489 summary: `opened pull #${opened.value.number}`,
490 costUsd: 0.1,
491 turns: 4,
492 });
493
494 const page = await (
495 await get(`/alice/engine/runs/${run.id}`, writerCookie)
496 ).text();
497 // The badge itself, not a stylesheet that happens to mention the word:
498 // this read `data-word="look"` until the studio's rules moved out of the
499 // page, and matched `.studio-list li[data-word="look"]` in the inline
500 // <style>, never the badge. The pull has no deploy of this head, so its
501 // word is `waiting` — and the point stands either way: the word came
502 // from the pull found by branch, not from the summary (`planned` is what
503 // a run with nothing to show would say).
504 expect(page).toMatch(/data-word="waiting"\s+id="run-word"/);
505 expect(page).not.toContain('data-word="planned"');
506 expect(page).not.toContain('id="run-stop"');
507 expect(page).not.toContain(">Stop<");
508 expect(page).not.toContain(">Cancel run<");
509 });
510
511 test("a deploy's own run page speaks as the deploy, reads as done rather than planned, and prints a tenth for a sub-second run (#195)", async () => {
512 const run = createRun(app.db, {
513 repoId: repo.id,
514 kind: "deploy",
515 env: "production",
516 commitOid: "c740479",
517 reason: "boot",
518 });
519 claimRun(app.db, run.id);
520 appendRunLog(app.config.dataDir, run.id, "$ bun test");
521 appendRunLog(app.config.dataDir, run.id, "bun test v1.3.14 (0d9b296a)");
522 appendRunLog(
523 app.config.dataDir,
524 run.id,
525 "(pass) the page knows who you are and offers the right door [3.61ms]",
526 );
527 finishRun(app.db, run.id, { state: "ok", summary: "deploy" });
528 // A run that took 300ms — today's whole-second rounding reads "took 0s".
529 app.db
530 .query("UPDATE run SET started_at = ?2, finished_at = ?3 WHERE id = ?1")
531 .run(run.id, "2024-01-01T00:00:00.000Z", "2024-01-01T00:00:00.300Z");
532
533 const page = await (
534 await get(`/alice/engine/runs/${run.id}`, writerCookie)
535 ).text();
536 // The deploy speaks for itself: the box, and every line in it — never
537 // "an agent", which is nobody that was actually there.
538 expect(page).toContain('data-who="the deploy"');
539 expect(page).toContain('>the deploy<');
540 expect(page).not.toContain("an agent");
541 // A deploy that finished with nothing else to show reads as done, not
542 // as a run that has not started yet.
543 expect(page).toContain('data-word="done"');
544 expect(page).not.toContain('data-word="planned"');
545 // Sub-second, in tenths — not rounded away to "took 0s".
546 expect(page).toContain(">took 0.3s<");
547 });
548
549 const stepStates = (page: string) =>
550 [...page.matchAll(/<li data-step="(\w+)" data-state="(\w+)"/g)].map(
551 (m) => `${m[1]}:${m[2]}`,
552 );
553
554 test("a running check's page has its three step rows from the first paint: install done, tests running", async () => {
555 const run = createRun(app.db, {
556 repoId: repo.id,
557 kind: "check",
558 commitOid: "c740479",
559 reason: "push",
560 });
561 claimRun(app.db, run.id);
562 for (const line of [
563 "$ bun install",
564 "$ bun scripts/gate.ts && tsc && bun scripts/test-shards.ts",
565 "test-shards: 12 files",
566 "── shard 1/2: test/a.test.ts",
567 ])
568 appendRunLog(app.config.dataDir, run.id, line);
569 const page = await (
570 await get(`/alice/engine/runs/${run.id}`, writerCookie)
571 ).text();
572 expect(stepStates(page)).toEqual([
573 "install:ok",
574 "typecheck:ok",
575 "tests:running",
576 ]);
577 });
578
579 test("a running check's page always has the bar and words, pins the failure outside the live region, and hides all from a reader without the log", async () => {
580 const mk = (lines: string[]) => {
581 const r = createRun(app.db, {
582 repoId: repo.id,
583 kind: "check",
584 commitOid: "c740479",
585 reason: "push",
586 });
587 claimRun(app.db, r.id);
588 for (const l of lines) appendRunLog(app.config.dataDir, r.id, l);
589 return r;
590 };
591 const run = mk([
592 "$ bun install",
593 "$ gate && tsc && bun scripts/test-shards.ts",
594 "test-shards: 9 files in 3 shards",
595 "── shard 1/3: test/a.test.ts",
596 "error: <b>boom</b>",
597 "(fail) a breaks",
598 " 4 pass",
599 " 1 fail",
600 "── shard 1/3 done (exit 1, 2.0s)",
601 "── shard 2/3: test/b.test.ts",
602 ]);
603 // A recorded look is on the page, escaped, and in the terminal's line.
604 const noLooks = await (await get(`/alice/engine/runs/${run.id}`, writerCookie)).text();
605 expect(noLooks).not.toContain("vitals");
606 writeFileSync(
607 looksPath(app.config.dataDir, run.id),
608 JSON.stringify([
609 { look: "<b>home</b>", entries: { layoutShift: [{ startTime: 1, value: 0.3 }] } },
610 { look: "mobile", skipped: "no browser" },
611 ]),
612 );
613 const seen = await (await get(`/alice/engine/runs/${run.id}`, writerCookie)).text();
614 expect(seen).toContain("<td>CLS</td><td>0.3</td>");
615 expect(seen).toContain("&lt;b&gt;home&lt;/b&gt;");
616 expect(seen).toContain("skipped: no browser");
617 expect(stepsLine(app.config.dataDir, { state: "running", run }, true)).toContain(
618 "vitals <b>home</b>: LCP n/a, CLS 0.3 poor, TBT 0 ms good; mobile: skipped: no browser",
619 );
620 expect(stepsLine(app.config.dataDir, { state: "running", run }, false)).toBe("");
621 const page = await (await get(`/alice/engine/runs/${run.id}`, writerCookie)).text();
622 expect(page).toContain('<progress aria-label="Test files started" max="9" value="2"');
623 expect(page).toContain("2 of 9 files started · 4 passed, 1 failed · running test/b.test.ts");
624 expect(page).toContain('<strong class="break">a breaks</strong>');
625 expect(page).toContain("error: &lt;b&gt;boom&lt;/b&gt;");
626 // The pins sit after the live region closes.
627 const live = page.indexOf('role="status"');
628 expect(live).toBeGreaterThan(-1);
629 expect(page.indexOf('id="check-pins"')).toBeGreaterThan(page.indexOf("Test files"));
630 expect(page.slice(live, page.indexOf('id="check-pins"'))).not.toContain("a breaks");
631 // The steps are bare rows, not a boxed card (#9727).
632 expect(page.slice(page.indexOf('id="check-steps"'), page.indexOf('id="check-pins"'))).toMatch(
633 /<div\s+class="card bare"[^>]*>\s*<ul class="list">/,
634 );
635 // The replaced element (#check-steps) closes before the pin slot, which is
636 // patched in place so focus and scroll survive a refetch.
637 const at = page.indexOf('id="check-steps"');
638 expect(page.slice(at, page.indexOf('id="check-pins"'))).toContain("</div>");
639 expect(page.slice(at, page.indexOf('id="check-pins"')).match(/<div/g)?.length).toBe(
640 page.slice(at, page.indexOf('id="check-pins"')).match(/<\/div>/g)?.length,
641 );
642 // Nothing logged yet: the bar and the line are still there, empty.
643 const fresh = mk([]);
644 const quiet = await (await get(`/alice/engine/runs/${fresh.id}`, writerCookie)).text();
645 expect(quiet).toContain('<progress aria-label="Test files started"></progress>');
646 const block = quiet.slice(quiet.indexOf('id="check-steps"'), quiet.indexOf('id="run-live"'));
647 expect(block).not.toContain("value=");
648 expect(block).not.toContain("tabindex");
649 expect(block).not.toContain('role="region"');
650 // With pins, the slot is focusable and labelled; the live region holds no pin.
651 expect(page).toContain('data-pins id="check-pins" role="region" tabindex="0" aria-label="Failed tests"');
652 expect(page).not.toMatch(/data-pins[^>]*class="[^"]*stack/);
653 expect(page).not.toContain("pinned-failures");
654 expect(page).not.toContain("pinned-slot");
655 expect(page.slice(page.indexOf('id="check-pins"'))).toMatch(/class="card"\s+data-tone="danger"/);
656 expect(page.slice(live, page.indexOf(`id="check-pins"`, live))).toContain("2 of 9 files started");
657 expect(quiet).toContain('<p class="check-words small muted break"></p>'); // empty, but still rendered
658 for (const old of ["tests-bar", "tests-words"]) {
659 expect(page).not.toContain(old);
660 expect(quiet).not.toContain(old);
661 }
662 // A finished run with no known count shows no bar at all.
663 const done = mk(["$ bun install"]);
664 finishRun(app.db, done.id, { state: "failed", summary: "x" });
665 const ended = await (await get(`/alice/engine/runs/${done.id}`, writerCookie)).text();
666 expect(ended).toContain("check-progress");
667 // Drawn from the design system: the old bespoke classes are gone.
668 for (const old of ['class="check-steps"', 'class="check-progress"', "step-state", "step-name"]) {
669 expect(ended).not.toContain(old);
670 expect(page).not.toContain(old);
671 }
672 expect(page).toContain('data-step="');
673 expect(page).toContain("data-check-progress");
674 expect(ended).not.toContain("<progress");
675 // No look has recorded vitals yet: the run page renders no table, not an empty one.
676 expect(ended).not.toContain("vitals");
677 // A reader the log is hidden from gets none of it.
678 const plain = await createRepo(app, alice, { name: "plainb", visibility: "public", description: "x" });
679 if (!plain.ok) throw new Error(String(plain.error));
680 const hidden = createRun(app.db, { repoId: plain.value.id, kind: "check", commitOid: "c740479", reason: "push" });
681 claimRun(app.db, hidden.id);
682 appendRunLog(app.config.dataDir, hidden.id, "(fail) secret");
683 const hid = await (await get(`/alice/plainb/runs/${hidden.id}`, readerCookie)).text();
684 expect(hid).not.toContain("tests-bar");
685 expect(hid).not.toContain("secret");
686 });
687
688 test("the page re-reads its rows as the run goes, and a reader who cannot see the log gets none", async () => {
689 const script = await (
690 await get(
691 `/alice/engine/runs/${
692 (
693 createRun(app.db, {
694 repoId: repo.id,
695 kind: "check",
696 commitOid: "c740479",
697 reason: "push",
698 })
699 ).id
700 }`,
701 writerCookie,
702 )
703 ).text();
704 const scripts = await withScripts(base, script);
705 expect(scripts).toContain("getElementById('check-steps')");
706 expect(scripts).toContain("\\(fail\\) ");
707 expect(scripts).toContain("['role', 'tabindex', 'aria-label']");
708 const plain = await createRepo(app, alice, {
709 name: "plain",
710 visibility: "public",
711 description: "no app",
712 });
713 if (!plain.ok) throw new Error(String(plain.error));
714 const hidden = createRun(app.db, {
715 repoId: plain.value.id,
716 kind: "check",
717 commitOid: "c740479",
718 reason: "push",
719 });
720 claimRun(app.db, hidden.id);
721 appendRunLog(app.config.dataDir, hidden.id, "$ bun install");
722 const page = await (
723 await get(`/alice/plain/runs/${hidden.id}`, readerCookie)
724 ).text();
725 expect(page).toContain('id="run-log-hidden"');
726 expect(page).not.toContain('id="check-steps"');
727 });
728
729 test("a finished red check's page shows tests failed, and a fresh one still has three rows", async () => {
730 const run = createRun(app.db, {
731 repoId: repo.id,
732 kind: "check",
733 commitOid: "c740479",
734 reason: "push",
735 });
736 claimRun(app.db, run.id);
737 for (const line of ["$ bun install", "$ bun test", "test-shards: 3 files"])
738 appendRunLog(app.config.dataDir, run.id, line);
739 finishRun(app.db, run.id, { state: "failed", summary: "1 of 3 tests" });
740 const page = await (
741 await get(`/alice/engine/runs/${run.id}`, writerCookie)
742 ).text();
743 expect(stepStates(page)).toEqual([
744 "install:ok",
745 "typecheck:ok",
746 "tests:failed",
747 ]);
748 const fresh = createRun(app.db, {
749 repoId: repo.id,
750 kind: "check",
751 commitOid: "c740479",
752 reason: "push",
753 });
754 const queued = await (
755 await get(`/alice/engine/runs/${fresh.id}`, writerCookie)
756 ).text();
757 expect(stepStates(queued)).toEqual([
758 "install:pending",
759 "typecheck:pending",
760 "tests:pending",
761 ]);
762 });
763 });
test/studio-run-styles.test.ts added +40
@@ -0,0 +1,40 @@
1 /**
2 * The run page's stylesheet: the pin slot's rules, and the old run-* classes
3 * left behind.
4 *
5 * One of the files the studio's suite was split into so no one file sets the
6 * gate's floor (#2038) or passes 800 lines (#14011); the rest are
7 * test/studio*.test.ts, and they sign in through test/helpers/studio-signin.ts.
8 */
9
10 import { describe, expect, test } from "bun:test";
11
12 describe("the pin slot's stylesheet", () => {
13 test("caps [data-pins] with scroll and reserves it while a check is open", async () => {
14 const { css } = await import("../src/web/style.ts");
15 expect(css).toContain("[data-pins] { max-height: 9em; overflow-y: auto; }");
16 expect(css).toContain('[data-check-progress][data-state="running"] [data-pins] { height: 9em; }');
17 expect(css).toContain('[data-check-progress][data-state="queued"] [data-pins]');
18 expect(css).not.toContain(".pinned-");
19 // Cards stack as blocks (a flex column would squash them into 9em), and output wraps.
20 expect(css).toContain("[data-pins] .card + .card { margin-top: 12px; }");
21 expect(css).toContain("[data-pins] pre { margin: 4px 0 0; white-space: pre-wrap; font-size: var(--fs-min); }");
22 });
23 test("the slot is not a flex column", async () => {
24 const { css } = await import("../src/web/style.ts");
25 expect(css).not.toMatch(/\[data-pins\][^{]*\{[^}]*display:\s*(flex|grid)/);
26 });
27 });
28
29 describe("run page controls leave the old classes behind (#4263)", () => {
30 test("no run-jump, run-live or run-steer class selector is defined, and [hidden] still beats .btn", async () => {
31 const { css } = await import("../src/web/style.ts");
32 for (const old of ["run-jump", "run-live", "run-steer"]) {
33 expect(css).not.toMatch(new RegExp(`\\.${old}(?![\\w-])`));
34 }
35 // The layout is kept under the ids the client script already uses.
36 expect(css).toMatch(/#run-steer \{[^}]*position: sticky/);
37 expect(css).toMatch(/#run-jump \{[^}]*position: sticky/);
38 expect(css).toMatch(/\[hidden\] \{ display: none !important; \}/);
39 });
40 });
test/studio-run-words.test.ts added +278
@@ -0,0 +1,278 @@
1 /**
2 * The studio's words for a run and a pull (`runWord`, `pullWord`) come from
3 * facts, never from `run.summary`: shipped means production serves it.
4 *
5 * One of the files the studio's suite was split into so no one file sets the
6 * gate's floor (#2038) or passes 800 lines (#14011); the rest are
7 * test/studio*.test.ts, and they sign in through test/helpers/studio-signin.ts.
8 */
9
10 import { afterAll, beforeAll, describe, expect, spyOn, test } from "bun:test";
11 import { mkdtemp, rm } from "node:fs/promises";
12 import { tmpdir } from "node:os";
13 import { join } from "node:path";
14 import { createApp, type App } from "../src/app.ts";
15 import { enableApp } from "../src/actions/apps.ts";
16 import { AppsRuntime } from "../src/apps/runtime.ts";
17 import { getAppByRepo, type AppRecord } from "../src/core/apps.ts";
18 import { openIssue } from "../src/actions/issues.ts";
19 import { mergePull, openPull } from "../src/actions/pulls.ts";
20 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
21 import { getPull, type Pull } from "../src/core/pulls.ts";
22 import type { Repo } from "../src/core/repos.ts";
23 import {
24 claimRun,
25 createRun,
26 finishRun,
27 markServing,
28 } from "../src/core/runs.ts";
29 import { createUser, type User } from "../src/core/users.ts";
30 import { serve, type Servers } from "../src/http/server.ts";
31 import {
32 pullWord,
33 runWord,
34 servingHistory,
35 type WordCtx,
36 } from "../src/web/studio/words.ts";
37 import { repoDir } from "../src/git/repos.ts";
38
39 describe("words come from facts, not from run.summary", () => {
40 let dataDir: string;
41 let app: App;
42 let servers: Servers;
43 let runtime: AppsRuntime;
44 let repo: Repo;
45 let record: AppRecord;
46 let alice: User;
47 let wordCtx: WordCtx;
48 let seededCommit: string;
49
50 const put = (live?: { commitOid: string; state?: "failed" }): void => {
51 const envs = (runtime as unknown as { envs: Map<string, unknown> }).envs;
52 envs.set(`${record.id}/production`, {
53 app: record,
54 env: "production",
55 chain: Promise.resolve(),
56 restarts: [],
57 ...(live?.state === "failed"
58 ? { lastError: "the deploy failed" }
59 : live
60 ? {
61 live: {
62 commitOid: live.commitOid,
63 handle: { port: 4321 },
64 runId: "fake",
65 since: new Date().toISOString(),
66 manifest: { tty: null },
67 },
68 }
69 : {}),
70 });
71 };
72
73 beforeAll(async () => {
74 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-words-"));
75 app = createApp({ dataDir, port: 0 });
76 servers = await serve(app);
77 runtime = new AppsRuntime(app, {
78 domain: "words.test",
79 scheme: "http",
80 publicPort: servers.port,
81 forgeUrl: servers.url,
82 concurrency: 1,
83 });
84 const a = await createUser(app.db, { origin: "seed", trusted: true,
85 username: "alice",
86 email: "[email protected]",
87 password: "a-good-passphrase",
88 adminIfFirst: true,
89 });
90 if (!a.ok) throw new Error(a.error);
91 alice = a.value;
92 const made = await createRepo(app, alice, {
93 name: "wordsapp",
94 visibility: "private",
95 description: "",
96 });
97 if (!made.ok) throw new Error(String(made.error));
98 repo = made.value;
99 const seeded = await commitToBranch(app, alice, repo, {
100 message: "Start",
101 files: [{ path: "README.md", content: "start\n" }],
102 });
103 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
104 seededCommit = seeded.value.commitOid;
105 const enabled = await enableApp(app, alice, repo, { slug: "wordsapp" });
106 if (!enabled.ok) throw new Error(String(enabled.error));
107 record = getAppByRepo(app.db, repo.id)!;
108 wordCtx = {
109 db: app.db,
110 dir: repoDir(app.reposRoot, repo.ownerName, repo.name),
111 runtime,
112 record,
113 history: servingHistory(),
114 // The owner reads it: production's facts are theirs to see.
115 viewer: alice,
116 };
117 });
118
119 afterAll(async () => {
120 servers.stop();
121 app.close();
122 await rm(dataDir, { recursive: true, force: true });
123 });
124
125 test("a merged pull whose production deploy has not yet served does not say shipped, and says so once it has", async () => {
126 const branch = await commitToBranch(app, alice, repo, {
127 branch: "feature/word",
128 base: "main",
129 message: "Add a word",
130 files: [{ path: "WORD.md", content: "word\n" }],
131 });
132 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
133 const opened = await openPull(app, alice, repo, {
134 title: "Add a word",
135 body: "",
136 baseBranch: "main",
137 headBranch: "feature/word",
138 });
139 if (!opened.ok) throw new Error(String(opened.error));
140 const merged = await mergePull(app, alice, repo, opened.value, "squash");
141 if (!merged.ok) throw new Error(String(merged.error));
142 const pull = getPull(app.db, repo.id, opened.value.number)!;
143 expect(pull.mergedOid).not.toBeNull();
144
145 // Production is still on the commit before the merge: not shipped.
146 put({ commitOid: seededCommit });
147 const notYet = await pullWord(wordCtx, pull);
148 expect(notYet.word).not.toBe("shipped");
149
150 // Production has caught up to the merge commit: shipped, live.
151 put({ commitOid: pull.mergedOid! });
152 const now = await pullWord(wordCtx, pull);
153 expect(now.word).toBe("shipped");
154 });
155
156 test("a page of merged pulls asks git about production once, however many rows (#285)", async () => {
157 const pulls: Pull[] = [];
158 for (let i = 0; i < 5; i++) {
159 const branch = await commitToBranch(app, alice, repo, {
160 branch: `feature/many-${i}`,
161 base: "main",
162 message: `Many ${i}`,
163 files: [{ path: `MANY-${i}.md`, content: `${i}\n` }],
164 });
165 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
166 const opened = await openPull(app, alice, repo, {
167 title: `Many ${i}`,
168 body: "",
169 baseBranch: "main",
170 headBranch: `feature/many-${i}`,
171 });
172 if (!opened.ok) throw new Error(String(opened.error));
173 const merged = await mergePull(app, alice, repo, opened.value, "squash");
174 if (!merged.ok) throw new Error(String(merged.error));
175 pulls.push(getPull(app.db, repo.id, opened.value.number)!);
176 }
177
178 // Every git process spawned while one page's words are decided.
179 const spawned: string[] = [];
180 const original = Bun.spawn.bind(Bun);
181 const spy = spyOn(Bun, "spawn").mockImplementation(((
182 cmd: string[],
183 options: never,
184 ) => {
185 if (cmd[0] === "git") spawned.push(cmd.slice(1).join(" "));
186 return original(cmd, options);
187 }) as unknown as typeof Bun.spawn);
188 const wordsOfOnePage = async (): Promise<string[]> => {
189 const page: WordCtx = { ...wordCtx, history: servingHistory() };
190 const words = await Promise.all(pulls.map((p) => pullWord(page, p)));
191 return words.map((w) => w.word);
192 };
193 try {
194 // Production at the second merge: that one and the one before it are
195 // shipped (ancestry, not equality), the three after it are not.
196 put({ commitOid: pulls[1]!.mergedOid! });
197 expect(await wordsOfOnePage()).toEqual([
198 "shipped",
199 "shipped",
200 "waiting",
201 "waiting",
202 "waiting",
203 ]);
204 expect(spawned).toHaveLength(1);
205
206 // A new page is a new question: the deploy that just landed is seen.
207 spawned.length = 0;
208 put({ commitOid: pulls[4]!.mergedOid! });
209 expect(new Set(await wordsOfOnePage())).toEqual(new Set(["shipped"]));
210 expect(spawned).toHaveLength(1);
211 } finally {
212 spy.mockRestore();
213 }
214 });
215
216 test("an agent run that opened no pull and pushed nothing is not shipped", async () => {
217 const told = await openIssue(app, alice, repo, {
218 title: "Write a plan",
219 body: "",
220 });
221 if (!told.ok) throw new Error(String(told.error));
222 const run = createRun(app.db, {
223 repoId: repo.id,
224 kind: "agent",
225 issueId: told.value.id,
226 branch: "scout/999-a-branch-nothing-used",
227 actorId: alice.id,
228 reason: "assigned",
229 });
230 claimRun(app.db, run.id);
231 const finished = finishRun(app.db, run.id, {
232 state: "ok",
233 summary: "wrote docs/PLAN.md",
234 });
235 const word = await runWord(wordCtx, finished!);
236 expect(word.word).not.toBe("shipped");
237 expect(word.word).toBe("planned");
238 expect(word.note).toBe("wrote docs/PLAN.md");
239 });
240
241 test("a deploy serving with failed tests is not shipped", async () => {
242 const branch = await commitToBranch(app, alice, repo, {
243 branch: "feature/preview",
244 base: "main",
245 message: "Preview",
246 files: [{ path: "PREVIEW.md", content: "preview\n" }],
247 });
248 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
249 const opened = await openPull(app, alice, repo, {
250 title: "Preview",
251 body: "",
252 baseBranch: "main",
253 headBranch: "feature/preview",
254 });
255 if (!opened.ok) throw new Error(String(opened.error));
256 const pull = opened.value;
257 const deploy = createRun(app.db, {
258 repoId: repo.id,
259 kind: "check",
260 issueId: pull.id,
261 env: `pull-${pull.number}`,
262 commitOid: pull.headOid,
263 reason: "pull",
264 });
265 claimRun(app.db, deploy.id);
266 markServing(app.db, deploy.id);
267 finishRun(app.db, deploy.id, {
268 state: "failed",
269 summary: "test: 1 of 3 tests failed",
270 });
271 const word = await pullWord(
272 wordCtx,
273 getPull(app.db, repo.id, pull.number)!,
274 );
275 expect(word.word).not.toBe("shipped");
276 expect(word.note).toBe("serving, but the check failed");
277 });
278 });
test/studio-runs.test.ts deleted −2503
@@ -1,2503 +0,0 @@
1 /**
2 * The studio: slot counts, words from facts, a run's page, the tails, and
3 * what the front door calls news. One of the files the studio's suite was
4 * split into so no one file sets the gate's floor (#2038); the rest are
5 * test/studio*.test.ts. It runs in about 2.5 s with no waits (#3578).
6 */
7
8 import { seatAs } from "./helpers/seat.ts";
9 import {
10 afterAll,
11 afterEach,
12 beforeAll,
13 beforeEach,
14 describe,
15 expect,
16 spyOn,
17 test,
18 } from "bun:test";
19 import { writeFileSync } from "node:fs";
20 import { mkdtemp, rm } from "node:fs/promises";
21 import { looksPath } from "../src/checks/looks.ts";
22 import { stepsLine } from "../src/checks/progress.ts";
23 import { tmpdir } from "node:os";
24 import { join } from "node:path";
25 import { createApp, type App } from "../src/app.ts";
26 import { enableApp } from "../src/actions/apps.ts";
27 import type { EnvName } from "../src/apps/data.ts";
28 import { AppsRuntime } from "../src/apps/runtime.ts";
29 import { getAppByRepo, type AppRecord } from "../src/core/apps.ts";
30 import { addToCrew, createAgent } from "../src/actions/crew.ts";
31 import { assignIssue, openIssue } from "../src/actions/issues.ts";
32 import { mergePull, openPull } from "../src/actions/pulls.ts";
33 import {
34 commitToBranch,
35 createRepo,
36 updateRepoSettings,
37 } from "../src/actions/repos.ts";
38 import {} from "../src/core/agents.ts";
39 import type { Issue } from "../src/core/issues.ts";
40 import { getPull, type Pull } from "../src/core/pulls.ts";
41 import type { Repo } from "../src/core/repos.ts";
42 import {
43 claimRun,
44 createRun,
45 cancelRun,
46 finishRun,
47 getRun,
48 markServing,
49 noteRunProgress,
50 type Run,
51 } from "../src/core/runs.ts";
52 import { createUser, getUserByName, type User } from "../src/core/users.ts";
53 import { serve, type Servers } from "../src/http/server.ts";
54 import {
55 pullWord,
56 runWord,
57 servingHistory,
58 type WordCtx,
59 } from "../src/web/studio/words.ts";
60 import { repoDir } from "../src/git/repos.ts";
61 import { appendRunLog } from "../src/runs/log.ts";
62 import { workBegins, workEnds } from "../src/runs/ending.ts";
63 import { withScripts } from "./helpers/page-script.ts";
64
65 describe("the studio's slot count", () => {
66 let dataDir: string;
67 let app: App;
68 let servers: Servers;
69 let base: string;
70 let cookie: string;
71
72 beforeAll(async () => {
73 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-slots-"));
74 app = createApp({ dataDir, port: 0 });
75 servers = await serve(app);
76 base = servers.url;
77 const a = await createUser(app.db, { origin: "seed", trusted: true,
78 username: "alice",
79 email: "[email protected]",
80 password: "a-good-passphrase",
81 adminIfFirst: true,
82 });
83 if (!a.ok) throw new Error(a.error);
84 const alice = a.value;
85 cookie = (
86 (
87 await fetch(`${base}/signin`, {
88 method: "POST",
89 body: new URLSearchParams({
90 username: "alice",
91 password: "a-good-passphrase",
92 }),
93 redirect: "manual",
94 })
95 ).headers.get("set-cookie") ?? ""
96 ).split(";")[0]!;
97 const made = await createRepo(app, alice, {
98 name: "loom",
99 visibility: "private",
100 description: "a place for cloth",
101 });
102 if (!made.ok) throw new Error(String(made.error));
103 const repo = made.value;
104 const seeded = await commitToBranch(app, alice, repo, {
105 message: "Start",
106 files: [{ path: "README.md", content: "# loom\n" }],
107 });
108 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
109 const enabled = await enableApp(app, alice, repo, { slug: "loom" });
110 if (!enabled.ok) throw new Error(String(enabled.error));
111 for (const name of ["brick", "quill", "chart"]) {
112 const agent = await createAgent(app, alice, { username: name });
113 if (!agent.ok) throw new Error(String(agent.error));
114 expect(addToCrew(app, alice, repo, name).ok).toBe(true);
115 }
116 const reviewer = getUserByName(app.db, "quill")!;
117 const planner = getUserByName(app.db, "chart")!;
118 seatAs(app.db, repo.id, reviewer.id, "reviewer");
119 seatAs(app.db, repo.id, planner.id, "planner");
120 const on = await updateRepoSettings(app, alice, repo, {
121 autonomy: "autonomous",
122 autonomyDirection: "",
123 autonomySlots: 2,
124 autonomyBy: alice.id,
125 });
126 expect(on.ok).toBe(true);
127 // One builder building, one reviewer reviewing, one planner planning.
128 const inFlight: [string, User, string][] = [
129 ["Warp the loom", getUserByName(app.db, "brick")!, "assigned"],
130 ["Look at the shuttle", reviewer, "review"],
131 ["Plan the next cloth", planner, "assigned"],
132 ];
133 for (const [title, actor, reason] of inFlight) {
134 const issue = openIssue(app, alice, repo, { title });
135 if (!issue.ok) throw new Error(String(issue.error));
136 const run = createRun(app.db, {
137 repoId: repo.id,
138 kind: "agent",
139 issueId: issue.value.id,
140 actorId: actor.id,
141 reason,
142 });
143 claimRun(app.db, run.id);
144 }
145 });
146
147 afterAll(async () => {
148 servers.stop();
149 app.close();
150 await rm(dataDir, { recursive: true, force: true });
151 });
152
153 test("reads 1 of 2 in use with three runs in flight, and Now still lists all three", async () => {
154 const page = await (
155 await fetch(`${base}/alice/loom`, {
156 headers: { cookie: `${cookie}; forge_mode=studio` },
157 redirect: "manual",
158 })
159 ).text();
160 expect(page).toContain("1 of 2 in use");
161 // The crew card says the governor's reason in one line (#7644).
162 expect(page).toContain("Builders 2: a quiet hour, 0 of 0 open pulls moving");
163 expect(page).not.toContain("3 of 2");
164 expect(page).not.toContain("2 of 2");
165 // The runs that take no slot are not hidden: Now has all of them.
166 expect(page).toContain("Warp the loom");
167 expect(page).toContain("Look at the shuttle");
168 expect(page).toContain("Plan the next cloth");
169 });
170 });
171
172 describe("words come from facts, not from run.summary", () => {
173 let dataDir: string;
174 let app: App;
175 let servers: Servers;
176 let runtime: AppsRuntime;
177 let repo: Repo;
178 let record: AppRecord;
179 let alice: User;
180 let wordCtx: WordCtx;
181 let seededCommit: string;
182
183 const put = (live?: { commitOid: string; state?: "failed" }): void => {
184 const envs = (runtime as unknown as { envs: Map<string, unknown> }).envs;
185 envs.set(`${record.id}/production`, {
186 app: record,
187 env: "production",
188 chain: Promise.resolve(),
189 restarts: [],
190 ...(live?.state === "failed"
191 ? { lastError: "the deploy failed" }
192 : live
193 ? {
194 live: {
195 commitOid: live.commitOid,
196 handle: { port: 4321 },
197 runId: "fake",
198 since: new Date().toISOString(),
199 manifest: { tty: null },
200 },
201 }
202 : {}),
203 });
204 };
205
206 beforeAll(async () => {
207 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-words-"));
208 app = createApp({ dataDir, port: 0 });
209 servers = await serve(app);
210 runtime = new AppsRuntime(app, {
211 domain: "words.test",
212 scheme: "http",
213 publicPort: servers.port,
214 forgeUrl: servers.url,
215 concurrency: 1,
216 });
217 const a = await createUser(app.db, { origin: "seed", trusted: true,
218 username: "alice",
219 email: "[email protected]",
220 password: "a-good-passphrase",
221 adminIfFirst: true,
222 });
223 if (!a.ok) throw new Error(a.error);
224 alice = a.value;
225 const made = await createRepo(app, alice, {
226 name: "wordsapp",
227 visibility: "private",
228 description: "",
229 });
230 if (!made.ok) throw new Error(String(made.error));
231 repo = made.value;
232 const seeded = await commitToBranch(app, alice, repo, {
233 message: "Start",
234 files: [{ path: "README.md", content: "start\n" }],
235 });
236 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
237 seededCommit = seeded.value.commitOid;
238 const enabled = await enableApp(app, alice, repo, { slug: "wordsapp" });
239 if (!enabled.ok) throw new Error(String(enabled.error));
240 record = getAppByRepo(app.db, repo.id)!;
241 wordCtx = {
242 db: app.db,
243 dir: repoDir(app.reposRoot, repo.ownerName, repo.name),
244 runtime,
245 record,
246 history: servingHistory(),
247 // The owner reads it: production's facts are theirs to see.
248 viewer: alice,
249 };
250 });
251
252 afterAll(async () => {
253 servers.stop();
254 app.close();
255 await rm(dataDir, { recursive: true, force: true });
256 });
257
258 test("a merged pull whose production deploy has not yet served does not say shipped, and says so once it has", async () => {
259 const branch = await commitToBranch(app, alice, repo, {
260 branch: "feature/word",
261 base: "main",
262 message: "Add a word",
263 files: [{ path: "WORD.md", content: "word\n" }],
264 });
265 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
266 const opened = await openPull(app, alice, repo, {
267 title: "Add a word",
268 body: "",
269 baseBranch: "main",
270 headBranch: "feature/word",
271 });
272 if (!opened.ok) throw new Error(String(opened.error));
273 const merged = await mergePull(app, alice, repo, opened.value, "squash");
274 if (!merged.ok) throw new Error(String(merged.error));
275 const pull = getPull(app.db, repo.id, opened.value.number)!;
276 expect(pull.mergedOid).not.toBeNull();
277
278 // Production is still on the commit before the merge: not shipped.
279 put({ commitOid: seededCommit });
280 const notYet = await pullWord(wordCtx, pull);
281 expect(notYet.word).not.toBe("shipped");
282
283 // Production has caught up to the merge commit: shipped, live.
284 put({ commitOid: pull.mergedOid! });
285 const now = await pullWord(wordCtx, pull);
286 expect(now.word).toBe("shipped");
287 });
288
289 test("a page of merged pulls asks git about production once, however many rows (#285)", async () => {
290 const pulls: Pull[] = [];
291 for (let i = 0; i < 5; i++) {
292 const branch = await commitToBranch(app, alice, repo, {
293 branch: `feature/many-${i}`,
294 base: "main",
295 message: `Many ${i}`,
296 files: [{ path: `MANY-${i}.md`, content: `${i}\n` }],
297 });
298 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
299 const opened = await openPull(app, alice, repo, {
300 title: `Many ${i}`,
301 body: "",
302 baseBranch: "main",
303 headBranch: `feature/many-${i}`,
304 });
305 if (!opened.ok) throw new Error(String(opened.error));
306 const merged = await mergePull(app, alice, repo, opened.value, "squash");
307 if (!merged.ok) throw new Error(String(merged.error));
308 pulls.push(getPull(app.db, repo.id, opened.value.number)!);
309 }
310
311 // Every git process spawned while one page's words are decided.
312 const spawned: string[] = [];
313 const original = Bun.spawn.bind(Bun);
314 const spy = spyOn(Bun, "spawn").mockImplementation(((
315 cmd: string[],
316 options: never,
317 ) => {
318 if (cmd[0] === "git") spawned.push(cmd.slice(1).join(" "));
319 return original(cmd, options);
320 }) as unknown as typeof Bun.spawn);
321 const wordsOfOnePage = async (): Promise<string[]> => {
322 const page: WordCtx = { ...wordCtx, history: servingHistory() };
323 const words = await Promise.all(pulls.map((p) => pullWord(page, p)));
324 return words.map((w) => w.word);
325 };
326 try {
327 // Production at the second merge: that one and the one before it are
328 // shipped (ancestry, not equality), the three after it are not.
329 put({ commitOid: pulls[1]!.mergedOid! });
330 expect(await wordsOfOnePage()).toEqual([
331 "shipped",
332 "shipped",
333 "waiting",
334 "waiting",
335 "waiting",
336 ]);
337 expect(spawned).toHaveLength(1);
338
339 // A new page is a new question: the deploy that just landed is seen.
340 spawned.length = 0;
341 put({ commitOid: pulls[4]!.mergedOid! });
342 expect(new Set(await wordsOfOnePage())).toEqual(new Set(["shipped"]));
343 expect(spawned).toHaveLength(1);
344 } finally {
345 spy.mockRestore();
346 }
347 });
348
349 test("an agent run that opened no pull and pushed nothing is not shipped", async () => {
350 const told = await openIssue(app, alice, repo, {
351 title: "Write a plan",
352 body: "",
353 });
354 if (!told.ok) throw new Error(String(told.error));
355 const run = createRun(app.db, {
356 repoId: repo.id,
357 kind: "agent",
358 issueId: told.value.id,
359 branch: "scout/999-a-branch-nothing-used",
360 actorId: alice.id,
361 reason: "assigned",
362 });
363 claimRun(app.db, run.id);
364 const finished = finishRun(app.db, run.id, {
365 state: "ok",
366 summary: "wrote docs/PLAN.md",
367 });
368 const word = await runWord(wordCtx, finished!);
369 expect(word.word).not.toBe("shipped");
370 expect(word.word).toBe("planned");
371 expect(word.note).toBe("wrote docs/PLAN.md");
372 });
373
374 test("a deploy serving with failed tests is not shipped", async () => {
375 const branch = await commitToBranch(app, alice, repo, {
376 branch: "feature/preview",
377 base: "main",
378 message: "Preview",
379 files: [{ path: "PREVIEW.md", content: "preview\n" }],
380 });
381 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
382 const opened = await openPull(app, alice, repo, {
383 title: "Preview",
384 body: "",
385 baseBranch: "main",
386 headBranch: "feature/preview",
387 });
388 if (!opened.ok) throw new Error(String(opened.error));
389 const pull = opened.value;
390 const deploy = createRun(app.db, {
391 repoId: repo.id,
392 kind: "check",
393 issueId: pull.id,
394 env: `pull-${pull.number}`,
395 commitOid: pull.headOid,
396 reason: "pull",
397 });
398 claimRun(app.db, deploy.id);
399 markServing(app.db, deploy.id);
400 finishRun(app.db, deploy.id, {
401 state: "failed",
402 summary: "test: 1 of 3 tests failed",
403 });
404 const word = await pullWord(
405 wordCtx,
406 getPull(app.db, repo.id, pull.number)!,
407 );
408 expect(word.word).not.toBe("shipped");
409 expect(word.note).toBe("serving, but the check failed");
410 });
411 });
412
413 /**
414 * Working, on its own page: `/:owner/:name/runs/:id` in the studio names
415 * the tell, the cost, and offers the steer box — same routes and checks as
416 * dev's transcript, one click away at `?mode=dev`.
417 */
418 describe("working: a run's own page in the studio", () => {
419 let dataDir: string;
420 let app: App;
421 let servers: Servers;
422 let base: string;
423 let repo: Repo;
424 let alice: User;
425 let scoutId: string;
426 let writerCookie: string;
427 let readerCookie: string;
428
429 const signIn = async (username: string): Promise<string> =>
430 (
431 (
432 await fetch(`${base}/signin`, {
433 method: "POST",
434 body: new URLSearchParams({
435 username,
436 password: "a-good-passphrase",
437 }),
438 redirect: "manual",
439 })
440 ).headers.get("set-cookie") ?? ""
441 ).split(";")[0]!;
442
443 const get = (path: string, cookie: string) =>
444 fetch(`${base}${path}`, {
445 headers: { cookie: `${cookie}; forge_mode=studio` },
446 redirect: "manual",
447 });
448
449 beforeAll(async () => {
450 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-run-"));
451 app = createApp({ dataDir, port: 0 });
452 servers = await serve(app);
453 base = servers.url;
454 const a = await createUser(app.db, { origin: "seed", trusted: true,
455 username: "alice",
456 email: "[email protected]",
457 password: "a-good-passphrase",
458 adminIfFirst: true,
459 });
460 if (!a.ok) throw new Error(a.error);
461 alice = a.value;
462 const b = await createUser(app.db, { origin: "seed", trusted: true,
463 username: "bob",
464 email: "[email protected]",
465 password: "a-good-passphrase",
466 });
467 if (!b.ok) throw new Error(b.error);
468 writerCookie = await signIn("alice");
469 readerCookie = await signIn("bob");
470 const made = await createRepo(app, a.value, {
471 name: "engine",
472 visibility: "private",
473 description: "an app that builds",
474 });
475 if (!made.ok) throw new Error(String(made.error));
476 repo = made.value;
477 const seeded = await commitToBranch(app, a.value, repo, {
478 message: "Start",
479 files: [{ path: "README.md", content: "engine\n" }],
480 });
481 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
482 const enabled = await enableApp(app, a.value, repo, { slug: "engine" });
483 if (!enabled.ok) throw new Error(String(enabled.error));
484 const scout = await createAgent(app, a.value, { username: "scout" });
485 if (!scout.ok) throw new Error(String(scout.error));
486 scoutId = scout.value.user.id;
487 expect(addToCrew(app, a.value, repo, "scout").ok).toBe(true);
488 });
489
490 afterAll(async () => {
491 servers.stop();
492 app.close();
493 await rm(dataDir, { recursive: true, force: true });
494 });
495
496 test("a run's and a tell's heads draw §5's page head and meta row, not .issue-head or .issue-meta", async () => {
497 const tell = openIssue(app, alice, repo, { title: "Head check" });
498 if (!tell.ok) throw new Error(String(tell.error));
499 const run = createRun(app.db, {
500 repoId: repo.id,
501 kind: "agent",
502 issueId: tell.value.id,
503 actorId: scoutId,
504 reason: "assigned",
505 });
506 const runPage = await (
507 await get(`/alice/engine/runs/${run.id}`, writerCookie)
508 ).text();
509 const tellPage = await (
510 await get(`/alice/engine/issues/${tell.value.number}`, writerCookie)
511 ).text();
512 for (const page of [runPage, tellPage]) {
513 expect(page).not.toContain("issue-head");
514 expect(page).not.toContain("issue-meta");
515 expect(page).toContain('class="page-head"');
516 expect(page).toContain('class="page-head-sub bar muted small"');
517 expect(page).not.toContain('class="sub');
518 }
519 });
520
521 test("the studio transcript names the tell, the cost, and the steer box", async () => {
522 const tell = openIssue(app, alice, repo, {
523 title: "Fix the flicker on load",
524 body: "It flashes white before the page renders.",
525 });
526 if (!tell.ok) throw new Error(String(tell.error));
527 const run = createRun(app.db, {
528 repoId: repo.id,
529 kind: "agent",
530 issueId: tell.value.id,
531 actorId: scoutId,
532 reason: "assigned",
533 });
534 claimRun(app.db, run.id);
535 noteRunProgress(app.db, run.id, { costUsd: 0.42 });
536
537 const page = await (
538 await get(`/alice/engine/runs/${run.id}`, writerCookie)
539 ).text();
540 expect(page).toContain("Fix the flicker on load");
541 // The tell it came from, in the studio's own noun (#158).
542 expect(page).toContain(`href="/alice/engine/issues/${tell.value.number}"`);
543 expect(page).toContain("$0.42 spent");
544 expect(page).toContain('data-word="working"');
545 // An agent's own run still speaks in the agent's name (#195).
546 expect(page).toContain('data-who="scout"');
547 expect(page).toContain('id="run-steer"');
548 expect(page).toContain("Steer scout");
549 // Jump, live and steer draw §5's btn, bar and card (#4263).
550 for (const old of ["run-jump", "run-live", "run-steer"]) {
551 expect(page).not.toMatch(new RegExp(`class="[^"]*\\b${old}\\b`));
552 }
553 expect(page).toContain("newer below");
554 expect(page).toMatch(/<button[^>]*class="btn"[^>]*id="run-jump"/);
555 expect(page).toMatch(
556 /<form[^>]*class="stack"[^>]*id="run-steer"[^>]*action="\/alice\/engine\/runs\/\w+\/steer"/s,
557 );
558 // Plain like the home page's tell box, not a boxed card (#9973).
559 expect(page).not.toMatch(/<form[^>]*class="[^"]*\bcard\b[^"]*"[^>]*id="run-steer"/s);
560 expect(page).toMatch(/<form[^>]*id="run-steer"[^>]*data-api="[^"]+"[^>]*data-running="1"/s);
561 expect(page).toContain('for="steer-text"');
562 // Not shown without an issue to speak on.
563 const bare = createRun(app.db, {
564 repoId: repo.id,
565 kind: "agent",
566 actorId: scoutId,
567 reason: "assigned",
568 });
569 claimRun(app.db, bare.id);
570 const barePage = await (await get(`/alice/engine/runs/${bare.id}`, writerCookie)).text();
571 expect(barePage).not.toContain('id="run-steer"');
572 expect(page).toContain('name="text"');
573 expect(page).toContain('formaction="/alice/engine/runs/');
574 expect(page).toContain('id="run-stop"');
575 // The transcript is on the page, and there is no second reading of it.
576 expect(page).toContain('id="run-log"');
577 expect(page).not.toContain("?mode=dev");
578 // The summary is a plain paragraph, not a page-part class (#3960).
579 expect(page).toMatch(/<p class="mt" id="run-summary">/);
580 expect(page).not.toContain('class="run-summary"');
581 expect(page).not.toContain("run-actions");
582 });
583
584 test("the steer box is a writer's, on an agent run with an issue, never for a reader or a signed-out visitor", async () => {
585 const made = await createRepo(app, alice, {
586 name: "open",
587 visibility: "public",
588 description: "a public app",
589 });
590 if (!made.ok) throw new Error(String(made.error));
591 const open = made.value;
592 const seeded = await commitToBranch(app, alice, open, {
593 message: "Start",
594 files: [{ path: "README.md", content: "open\n" }],
595 });
596 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
597 const tell = openIssue(app, alice, open, { title: "Steer me" });
598 if (!tell.ok) throw new Error(String(tell.error));
599 const mk = (issueId?: string) => {
600 const run = createRun(app.db, {
601 repoId: open.id,
602 kind: "agent",
603 issueId,
604 actorId: scoutId,
605 reason: "assigned",
606 });
607 claimRun(app.db, run.id);
608 return run;
609 };
610 const page = async (id: string, cookie?: string) => {
611 const res = await fetch(`${base}/alice/open/runs/${id}`, {
612 headers: { cookie: `${cookie ?? ""}; forge_mode=studio` },
613 redirect: "manual",
614 });
615 return { status: res.status, text: await res.text() };
616 };
617 const live = mk(tell.value.id);
618 const asWriter = await page(live.id, writerCookie);
619 expect(asWriter.status).toBe(200);
620 expect(asWriter.text).toContain('id="steer-text"');
621 expect(asWriter.text).toContain('id="run-steer"');
622 // A reader of a public repository sees the run, not the box.
623 const asReader = await page(live.id, readerCookie);
624 expect(asReader.status).toBe(200);
625 expect(asReader.text).toContain('data-word=');
626 expect(asReader.text).not.toContain('id="steer-text"');
627 expect(asReader.text).not.toContain('id="run-steer"');
628 const signedOut = await page(live.id);
629 expect(signedOut.status).toBe(200);
630 expect(signedOut.text).toContain('data-word=');
631 expect(signedOut.text).not.toContain('id="steer-text"');
632 expect(signedOut.text).not.toContain('id="run-steer"');
633 // No issue to speak on: no box, even to a writer.
634 const bare = mk();
635 const bareWriter = await page(bare.id, writerCookie);
636 expect(bareWriter.status).toBe(200);
637 expect(bareWriter.text).toContain('data-word=');
638 expect(bareWriter.text).not.toContain('id="steer-text"');
639 // A finished run keeps the box for a writer only, as "tell it for its
640 // next run" (the comment starts a run); a reader never gets it.
641 finishRun(app.db, live.id, { state: "ok", summary: "done" });
642 const over = await page(live.id, writerCookie);
643 expect(over.text).toContain('data-running="0"');
644 expect(over.text).toContain("The run is over");
645 for (const cookie of [readerCookie, undefined]) {
646 const seen = await page(live.id, cookie);
647 expect(seen.status).toBe(200);
648 expect(seen.text).not.toContain('id="steer-text"');
649 expect(seen.text).not.toContain('id="run-steer"');
650 }
651 });
652
653 test("the cancel form sits in a .bar.mt row for an issue-less agent run and a non-agent run", async () => {
654 const agent = createRun(app.db, {
655 repoId: repo.id,
656 kind: "agent",
657 actorId: scoutId,
658 reason: "assigned",
659 });
660 claimRun(app.db, agent.id);
661 const check = createRun(app.db, {
662 repoId: repo.id,
663 kind: "check",
664 reason: "push",
665 });
666 for (const [run, label] of [
667 [agent, "Stop"],
668 [check, "Cancel run"],
669 ] as const) {
670 const page = await (
671 await get(`/alice/engine/runs/${run.id}`, writerCookie)
672 ).text();
673 expect(page).toMatch(
674 new RegExp(
675 `<div class="bar mt" id="run-cancel">\\s*<form method="post" action="/alice/engine/runs/${run.id}/cancel">[\\s\\S]*?${label}[\\s\\S]*?</form>`,
676 ),
677 );
678 }
679 });
680
681 test("a running run's page reads its log from the one /events stream and opens no other", async () => {
682 const run = createRun(app.db, {
683 repoId: repo.id,
684 kind: "agent",
685 actorId: scoutId,
686 reason: "assigned",
687 });
688 claimRun(app.db, run.id);
689 appendRunLog(app.config.dataDir, run.id, "first");
690 appendRunLog(app.config.dataDir, run.id, "second");
691
692 const page = await withScripts(
693 base,
694 await (await get(`/alice/engine/runs/${run.id}`, writerCookie)).text(),
695 );
696 // Every EventSource the page's HTML and script open is /events.
697 const opened = [...page.matchAll(/new EventSource\(([^,;]*)/g)].map(
698 (m) => m[1]!,
699 );
700 expect(opened.length).toBeGreaterThan(0);
701 for (const arg of opened) expect(arg).toStartWith("'/events");
702 // The connection is really opened: liveRegionsJs only connects when the
703 // page has a [data-live] element, and a script's text proves nothing.
704 expect(page).toMatch(/<div[^>]*id="run-head"[^>]*data-live="run\.\*"/);
705 expect(page).toContain('id="run-conn"');
706 expect(page).not.toContain("follow=1");
707 expect(page).not.toContain("data-stream=");
708 // The run's frames arrive on that connection; the file's lines are
709 // counted so none is drawn twice.
710 expect(page).toContain("'run.log'");
711 expect(page).toContain("forge:run-log");
712 expect(page).toContain(`data-run="${run.id}"`);
713 expect(page).toContain('data-seen="2"');
714 expect(page).toContain(">second<");
715 });
716
717 test("a running run's page never reloads itself: its head is the live region, and once over it shows the final state", async () => {
718 const run = createRun(app.db, {
719 repoId: repo.id,
720 kind: "agent",
721 actorId: scoutId,
722 reason: "assigned",
723 });
724 claimRun(app.db, run.id);
725 noteRunProgress(app.db, run.id, { costUsd: 0.25 });
726 const path = `/alice/engine/runs/${run.id}`;
727 const head = (html: string): string =>
728 html.slice(html.indexOf('id="run-head"'), html.indexOf('id="run-live"'));
729
730 const going = await (await get(path, writerCookie)).text();
731 expect(going).not.toMatch(/<meta\s+http-equiv="refresh"/);
732 // Word, cost and clock sit in one region, swapped in place on any
733 // run.* event about this repository and no other.
734 expect(going).toMatch(
735 /<div\s+class="page-head"\s+id="run-head"\s+data-live="run\.\*"\s+data-live-repo="alice\/engine"/,
736 );
737 expect(going).not.toContain("issue-head");
738 expect(going).not.toContain("issue-meta");
739 expect(head(going)).toContain('data-word="working"');
740 expect(head(going)).toContain("$0.25 spent");
741 expect(head(going)).toContain("running for");
742
743 finishRun(app.db, run.id, { state: "ok", summary: "planned" });
744 const over = await (await get(path, writerCookie)).text();
745 expect(over).not.toMatch(/<meta\s+http-equiv="refresh"/);
746 // The same region by id, so the swap on run.finished finds its match,
747 // carrying the final word, cost and summary; nothing is listening now.
748 expect(over).toContain('id="run-head"');
749 expect(over).not.toMatch(/id="run-head"[^>]*data-live=/);
750 expect(head(over)).toContain('data-word="planned"');
751 expect(head(over)).toContain("$0.25 spent");
752 expect(head(over)).toContain("took");
753 expect(head(over)).toContain(">planned<");
754 expect(over).not.toContain('id="run-conn"');
755 expect(over).not.toContain('id="run-stop"');
756 });
757
758 test("a cancelled run still ending its processes keeps its head live and says stopping, then cancelled (#13291)", async () => {
759 const run = createRun(app.db, { repoId: repo.id, kind: "agent", actorId: scoutId, reason: "assigned" });
760 claimRun(app.db, run.id);
761 workBegins(app.db, run.id);
762 cancelRun(app.db, run.id);
763 const path = `/alice/engine/runs/${run.id}`;
764 const head = (html: string): string =>
765 html.slice(html.indexOf('id="run-head"'), html.indexOf('id="run-live"'));
766 try {
767 const ending = await (await get(path, writerCookie)).text();
768 expect(ending).toMatch(/<div\s+class="page-head"\s+id="run-head"\s+data-live="run\.\*"/);
769 expect(head(ending)).toContain('data-word="stopping"');
770 } finally {
771 workEnds(app.db, run.id);
772 }
773 const ended = await (await get(path, writerCookie)).text();
774 expect(ended).not.toMatch(/id="run-head"[^>]*data-live=/);
775 expect(head(ended)).toContain('data-word="cancelled"');
776 });
777
778 test("the runs list files a stopping run under working, not stopped, until its processes have ended (#13291)", async () => {
779 const run = createRun(app.db, { repoId: repo.id, kind: "agent", actorId: scoutId, reason: "assigned" });
780 claimRun(app.db, run.id);
781 workBegins(app.db, run.id);
782 cancelRun(app.db, run.id);
783 const link = `/alice/engine/runs/${run.id}`;
784 const list = async (query: string): Promise<string> =>
785 (await get(`/alice/engine/runs?${query}`, writerCookie)).text();
786 try {
787 // The default agent list shows what is open and moving: a stopping run is.
788 expect(await list("kind=agent")).toContain(link);
789 expect(await list("kind=agent&word=stopped")).not.toContain(link);
790 } finally {
791 workEnds(app.db, run.id);
792 }
793 expect(await list("kind=agent")).not.toContain(link);
794 expect(await list("kind=agent&word=stopped")).toContain(link);
795 });
796
797 test("a long transcript holds its last window, and the whole of it is one step deeper at ?all=1", async () => {
798 const run = createRun(app.db, {
799 repoId: repo.id,
800 kind: "agent",
801 actorId: scoutId,
802 reason: "assigned",
803 });
804 claimRun(app.db, run.id);
805 for (let n = 0; n < 450; n++)
806 appendRunLog(app.config.dataDir, run.id, `line number ${n}`);
807 finishRun(app.db, run.id, { state: "ok", summary: "done" });
808
809 const page = await (
810 await get(`/alice/engine/runs/${run.id}`, writerCookie)
811 ).text();
812 expect(page).not.toContain("line number 0<");
813 expect(page).toContain("line number 449");
814 expect(page).toContain(`href="/alice/engine/runs/${run.id}?all=1"`);
815
816 const all = await (
817 await get(`/alice/engine/runs/${run.id}?all=1`, writerCookie)
818 ).text();
819 expect(all).toContain("line number 0<");
820 expect(all).toContain("line number 449");
821
822 // The depth step is refused exactly as the run is.
823 const anon = await fetch(`${base}/alice/engine/runs/${run.id}?all=1`, {
824 redirect: "manual",
825 });
826 expect(anon.status).toBe(404);
827 });
828
829 test("a reader without access gets the forge's not-found, exactly as dev", async () => {
830 const tell = openIssue(app, alice, repo, { title: "Private work" });
831 if (!tell.ok) throw new Error(String(tell.error));
832 const run = createRun(app.db, {
833 repoId: repo.id,
834 kind: "agent",
835 issueId: tell.value.id,
836 actorId: scoutId,
837 reason: "assigned",
838 });
839 claimRun(app.db, run.id);
840
841 const asReader = await get(`/alice/engine/runs/${run.id}`, readerCookie);
842 expect(asReader.status).toBe(404);
843 });
844
845 test("a finished run renders in the studio's own word, with no Stop", async () => {
846 const tell = openIssue(app, alice, repo, { title: "Tidy the footer" });
847 if (!tell.ok) throw new Error(String(tell.error));
848 const branch = await commitToBranch(app, alice, repo, {
849 branch: "scout/tidy-the-footer",
850 base: "main",
851 message: "Tidy the footer",
852 files: [{ path: "FOOTER.md", content: "tidy\n" }],
853 });
854 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
855 const opened = await openPull(app, alice, repo, {
856 title: "Tidy the footer",
857 body: `Closes #${tell.value.number}`,
858 baseBranch: "main",
859 headBranch: "scout/tidy-the-footer",
860 });
861 if (!opened.ok) throw new Error(String(opened.error));
862 // The word comes from the pull this run actually produced (found by
863 // its branch), not from parsing the summary below.
864 const run = createRun(app.db, {
865 repoId: repo.id,
866 kind: "agent",
867 issueId: tell.value.id,
868 branch: "scout/tidy-the-footer",
869 actorId: scoutId,
870 reason: "assigned",
871 });
872 claimRun(app.db, run.id);
873 finishRun(app.db, run.id, {
874 state: "ok",
875 summary: `opened pull #${opened.value.number}`,
876 costUsd: 0.1,
877 turns: 4,
878 });
879
880 const page = await (
881 await get(`/alice/engine/runs/${run.id}`, writerCookie)
882 ).text();
883 // The badge itself, not a stylesheet that happens to mention the word:
884 // this read `data-word="look"` until the studio's rules moved out of the
885 // page, and matched `.studio-list li[data-word="look"]` in the inline
886 // <style>, never the badge. The pull has no deploy of this head, so its
887 // word is `waiting` — and the point stands either way: the word came
888 // from the pull found by branch, not from the summary (`planned` is what
889 // a run with nothing to show would say).
890 expect(page).toMatch(/data-word="waiting"\s+id="run-word"/);
891 expect(page).not.toContain('data-word="planned"');
892 expect(page).not.toContain('id="run-stop"');
893 expect(page).not.toContain(">Stop<");
894 expect(page).not.toContain(">Cancel run<");
895 });
896
897 test("a deploy's own run page speaks as the deploy, reads as done rather than planned, and prints a tenth for a sub-second run (#195)", async () => {
898 const run = createRun(app.db, {
899 repoId: repo.id,
900 kind: "deploy",
901 env: "production",
902 commitOid: "c740479",
903 reason: "boot",
904 });
905 claimRun(app.db, run.id);
906 appendRunLog(app.config.dataDir, run.id, "$ bun test");
907 appendRunLog(app.config.dataDir, run.id, "bun test v1.3.14 (0d9b296a)");
908 appendRunLog(
909 app.config.dataDir,
910 run.id,
911 "(pass) the page knows who you are and offers the right door [3.61ms]",
912 );
913 finishRun(app.db, run.id, { state: "ok", summary: "deploy" });
914 // A run that took 300ms — today's whole-second rounding reads "took 0s".
915 app.db
916 .query("UPDATE run SET started_at = ?2, finished_at = ?3 WHERE id = ?1")
917 .run(run.id, "2024-01-01T00:00:00.000Z", "2024-01-01T00:00:00.300Z");
918
919 const page = await (
920 await get(`/alice/engine/runs/${run.id}`, writerCookie)
921 ).text();
922 // The deploy speaks for itself: the box, and every line in it — never
923 // "an agent", which is nobody that was actually there.
924 expect(page).toContain('data-who="the deploy"');
925 expect(page).toContain('>the deploy<');
926 expect(page).not.toContain("an agent");
927 // A deploy that finished with nothing else to show reads as done, not
928 // as a run that has not started yet.
929 expect(page).toContain('data-word="done"');
930 expect(page).not.toContain('data-word="planned"');
931 // Sub-second, in tenths — not rounded away to "took 0s".
932 expect(page).toContain(">took 0.3s<");
933 });
934
935 const stepStates = (page: string) =>
936 [...page.matchAll(/<li data-step="(\w+)" data-state="(\w+)"/g)].map(
937 (m) => `${m[1]}:${m[2]}`,
938 );
939
940 test("a running check's page has its three step rows from the first paint: install done, tests running", async () => {
941 const run = createRun(app.db, {
942 repoId: repo.id,
943 kind: "check",
944 commitOid: "c740479",
945 reason: "push",
946 });
947 claimRun(app.db, run.id);
948 for (const line of [
949 "$ bun install",
950 "$ bun scripts/gate.ts && tsc && bun scripts/test-shards.ts",
951 "test-shards: 12 files",
952 "── shard 1/2: test/a.test.ts",
953 ])
954 appendRunLog(app.config.dataDir, run.id, line);
955 const page = await (
956 await get(`/alice/engine/runs/${run.id}`, writerCookie)
957 ).text();
958 expect(stepStates(page)).toEqual([
959 "install:ok",
960 "typecheck:ok",
961 "tests:running",
962 ]);
963 });
964
965 test("a running check's page always has the bar and words, pins the failure outside the live region, and hides all from a reader without the log", async () => {
966 const mk = (lines: string[]) => {
967 const r = createRun(app.db, {
968 repoId: repo.id,
969 kind: "check",
970 commitOid: "c740479",
971 reason: "push",
972 });
973 claimRun(app.db, r.id);
974 for (const l of lines) appendRunLog(app.config.dataDir, r.id, l);
975 return r;
976 };
977 const run = mk([
978 "$ bun install",
979 "$ gate && tsc && bun scripts/test-shards.ts",
980 "test-shards: 9 files in 3 shards",
981 "── shard 1/3: test/a.test.ts",
982 "error: <b>boom</b>",
983 "(fail) a breaks",
984 " 4 pass",
985 " 1 fail",
986 "── shard 1/3 done (exit 1, 2.0s)",
987 "── shard 2/3: test/b.test.ts",
988 ]);
989 // A recorded look is on the page, escaped, and in the terminal's line.
990 const noLooks = await (await get(`/alice/engine/runs/${run.id}`, writerCookie)).text();
991 expect(noLooks).not.toContain("vitals");
992 writeFileSync(
993 looksPath(app.config.dataDir, run.id),
994 JSON.stringify([
995 { look: "<b>home</b>", entries: { layoutShift: [{ startTime: 1, value: 0.3 }] } },
996 { look: "mobile", skipped: "no browser" },
997 ]),
998 );
999 const seen = await (await get(`/alice/engine/runs/${run.id}`, writerCookie)).text();
1000 expect(seen).toContain("<td>CLS</td><td>0.3</td>");
1001 expect(seen).toContain("&lt;b&gt;home&lt;/b&gt;");
1002 expect(seen).toContain("skipped: no browser");
1003 expect(stepsLine(app.config.dataDir, { state: "running", run }, true)).toContain(
1004 "vitals <b>home</b>: LCP n/a, CLS 0.3 poor, TBT 0 ms good; mobile: skipped: no browser",
1005 );
1006 expect(stepsLine(app.config.dataDir, { state: "running", run }, false)).toBe("");
1007 const page = await (await get(`/alice/engine/runs/${run.id}`, writerCookie)).text();
1008 expect(page).toContain('<progress aria-label="Test files started" max="9" value="2"');
1009 expect(page).toContain("2 of 9 files started · 4 passed, 1 failed · running test/b.test.ts");
1010 expect(page).toContain('<strong class="break">a breaks</strong>');
1011 expect(page).toContain("error: &lt;b&gt;boom&lt;/b&gt;");
1012 // The pins sit after the live region closes.
1013 const live = page.indexOf('role="status"');
1014 expect(live).toBeGreaterThan(-1);
1015 expect(page.indexOf('id="check-pins"')).toBeGreaterThan(page.indexOf("Test files"));
1016 expect(page.slice(live, page.indexOf('id="check-pins"'))).not.toContain("a breaks");
1017 // The steps are bare rows, not a boxed card (#9727).
1018 expect(page.slice(page.indexOf('id="check-steps"'), page.indexOf('id="check-pins"'))).toMatch(
1019 /<div\s+class="card bare"[^>]*>\s*<ul class="list">/,
1020 );
1021 // The replaced element (#check-steps) closes before the pin slot, which is
1022 // patched in place so focus and scroll survive a refetch.
1023 const at = page.indexOf('id="check-steps"');
1024 expect(page.slice(at, page.indexOf('id="check-pins"'))).toContain("</div>");
1025 expect(page.slice(at, page.indexOf('id="check-pins"')).match(/<div/g)?.length).toBe(
1026 page.slice(at, page.indexOf('id="check-pins"')).match(/<\/div>/g)?.length,
1027 );
1028 // Nothing logged yet: the bar and the line are still there, empty.
1029 const fresh = mk([]);
1030 const quiet = await (await get(`/alice/engine/runs/${fresh.id}`, writerCookie)).text();
1031 expect(quiet).toContain('<progress aria-label="Test files started"></progress>');
1032 const block = quiet.slice(quiet.indexOf('id="check-steps"'), quiet.indexOf('id="run-live"'));
1033 expect(block).not.toContain("value=");
1034 expect(block).not.toContain("tabindex");
1035 expect(block).not.toContain('role="region"');
1036 // With pins, the slot is focusable and labelled; the live region holds no pin.
1037 expect(page).toContain('data-pins id="check-pins" role="region" tabindex="0" aria-label="Failed tests"');
1038 expect(page).not.toMatch(/data-pins[^>]*class="[^"]*stack/);
1039 expect(page).not.toContain("pinned-failures");
1040 expect(page).not.toContain("pinned-slot");
1041 expect(page.slice(page.indexOf('id="check-pins"'))).toMatch(/class="card"\s+data-tone="danger"/);
1042 expect(page.slice(live, page.indexOf(`id="check-pins"`, live))).toContain("2 of 9 files started");
1043 expect(quiet).toContain('<p class="check-words small muted break"></p>'); // empty, but still rendered
1044 for (const old of ["tests-bar", "tests-words"]) {
1045 expect(page).not.toContain(old);
1046 expect(quiet).not.toContain(old);
1047 }
1048 // A finished run with no known count shows no bar at all.
1049 const done = mk(["$ bun install"]);
1050 finishRun(app.db, done.id, { state: "failed", summary: "x" });
1051 const ended = await (await get(`/alice/engine/runs/${done.id}`, writerCookie)).text();
1052 expect(ended).toContain("check-progress");
1053 // Drawn from the design system: the old bespoke classes are gone.
1054 for (const old of ['class="check-steps"', 'class="check-progress"', "step-state", "step-name"]) {
1055 expect(ended).not.toContain(old);
1056 expect(page).not.toContain(old);
1057 }
1058 expect(page).toContain('data-step="');
1059 expect(page).toContain("data-check-progress");
1060 expect(ended).not.toContain("<progress");
1061 // No look has recorded vitals yet: the run page renders no table, not an empty one.
1062 expect(ended).not.toContain("vitals");
1063 // A reader the log is hidden from gets none of it.
1064 const plain = await createRepo(app, alice, { name: "plainb", visibility: "public", description: "x" });
1065 if (!plain.ok) throw new Error(String(plain.error));
1066 const hidden = createRun(app.db, { repoId: plain.value.id, kind: "check", commitOid: "c740479", reason: "push" });
1067 claimRun(app.db, hidden.id);
1068 appendRunLog(app.config.dataDir, hidden.id, "(fail) secret");
1069 const hid = await (await get(`/alice/plainb/runs/${hidden.id}`, readerCookie)).text();
1070 expect(hid).not.toContain("tests-bar");
1071 expect(hid).not.toContain("secret");
1072 });
1073
1074 test("the page re-reads its rows as the run goes, and a reader who cannot see the log gets none", async () => {
1075 const script = await (
1076 await get(
1077 `/alice/engine/runs/${
1078 (
1079 createRun(app.db, {
1080 repoId: repo.id,
1081 kind: "check",
1082 commitOid: "c740479",
1083 reason: "push",
1084 })
1085 ).id
1086 }`,
1087 writerCookie,
1088 )
1089 ).text();
1090 const scripts = await withScripts(base, script);
1091 expect(scripts).toContain("getElementById('check-steps')");
1092 expect(scripts).toContain("\\(fail\\) ");
1093 expect(scripts).toContain("['role', 'tabindex', 'aria-label']");
1094 const plain = await createRepo(app, alice, {
1095 name: "plain",
1096 visibility: "public",
1097 description: "no app",
1098 });
1099 if (!plain.ok) throw new Error(String(plain.error));
1100 const hidden = createRun(app.db, {
1101 repoId: plain.value.id,
1102 kind: "check",
1103 commitOid: "c740479",
1104 reason: "push",
1105 });
1106 claimRun(app.db, hidden.id);
1107 appendRunLog(app.config.dataDir, hidden.id, "$ bun install");
1108 const page = await (
1109 await get(`/alice/plain/runs/${hidden.id}`, readerCookie)
1110 ).text();
1111 expect(page).toContain('id="run-log-hidden"');
1112 expect(page).not.toContain('id="check-steps"');
1113 });
1114
1115 test("a finished red check's page shows tests failed, and a fresh one still has three rows", async () => {
1116 const run = createRun(app.db, {
1117 repoId: repo.id,
1118 kind: "check",
1119 commitOid: "c740479",
1120 reason: "push",
1121 });
1122 claimRun(app.db, run.id);
1123 for (const line of ["$ bun install", "$ bun test", "test-shards: 3 files"])
1124 appendRunLog(app.config.dataDir, run.id, line);
1125 finishRun(app.db, run.id, { state: "failed", summary: "1 of 3 tests" });
1126 const page = await (
1127 await get(`/alice/engine/runs/${run.id}`, writerCookie)
1128 ).text();
1129 expect(stepStates(page)).toEqual([
1130 "install:ok",
1131 "typecheck:ok",
1132 "tests:failed",
1133 ]);
1134 const fresh = createRun(app.db, {
1135 repoId: repo.id,
1136 kind: "check",
1137 commitOid: "c740479",
1138 reason: "push",
1139 });
1140 const queued = await (
1141 await get(`/alice/engine/runs/${fresh.id}`, writerCookie)
1142 ).text();
1143 expect(stepStates(queued)).toEqual([
1144 "install:pending",
1145 "typecheck:pending",
1146 "tests:pending",
1147 ]);
1148 });
1149 });
1150
1151 /**
1152 * Issue #88: the app's page tails the crew's log while it works — the last
1153 * handful of lines beside the running row, live, bounded, and with a
1154 * no-script fallback that behaves like the run page's own. Its own repo and
1155 * app, so no other test's still-running run ever counts as "the active run"
1156 * here.
1157 */
1158 describe("the app's page tails the crew's log while it works", () => {
1159 let dataDir: string;
1160 let app: App;
1161 let servers: Servers;
1162 let base: string;
1163 let repo: Repo;
1164 let alice: User;
1165 let scoutId: string;
1166 let writerCookie: string;
1167 let readerCookie: string;
1168
1169 const signIn = async (username: string): Promise<string> =>
1170 (
1171 (
1172 await fetch(`${base}/signin`, {
1173 method: "POST",
1174 body: new URLSearchParams({
1175 username,
1176 password: "a-good-passphrase",
1177 }),
1178 redirect: "manual",
1179 })
1180 ).headers.get("set-cookie") ?? ""
1181 ).split(";")[0]!;
1182
1183 const get = (path: string, cookie: string) =>
1184 fetch(`${base}${path}`, {
1185 headers: { cookie: `${cookie}; forge_mode=studio` },
1186 redirect: "manual",
1187 });
1188
1189 beforeAll(async () => {
1190 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-tail-"));
1191 app = createApp({ dataDir, port: 0 });
1192 servers = await serve(app);
1193 base = servers.url;
1194 const a = await createUser(app.db, { origin: "seed", trusted: true,
1195 username: "alice",
1196 email: "[email protected]",
1197 password: "a-good-passphrase",
1198 adminIfFirst: true,
1199 });
1200 if (!a.ok) throw new Error(a.error);
1201 alice = a.value;
1202 const b = await createUser(app.db, { origin: "seed", trusted: true,
1203 username: "bob",
1204 email: "[email protected]",
1205 password: "a-good-passphrase",
1206 });
1207 if (!b.ok) throw new Error(b.error);
1208 writerCookie = await signIn("alice");
1209 readerCookie = await signIn("bob");
1210 const made = await createRepo(app, a.value, {
1211 name: "engine",
1212 visibility: "private",
1213 description: "an app that builds",
1214 });
1215 if (!made.ok) throw new Error(String(made.error));
1216 repo = made.value;
1217 const seeded = await commitToBranch(app, a.value, repo, {
1218 message: "Start",
1219 files: [{ path: "README.md", content: "engine\n" }],
1220 });
1221 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
1222 const enabled = await enableApp(app, a.value, repo, { slug: "engine" });
1223 if (!enabled.ok) throw new Error(String(enabled.error));
1224 const scout = await createAgent(app, a.value, { username: "scout" });
1225 if (!scout.ok) throw new Error(String(scout.error));
1226 scoutId = scout.value.user.id;
1227 expect(addToCrew(app, a.value, repo, "scout").ok).toBe(true);
1228 });
1229
1230 afterAll(async () => {
1231 servers.stop();
1232 app.close();
1233 await rm(dataDir, { recursive: true, force: true });
1234 });
1235
1236 test("the app's page carries the tail of the active run's log, bounded, and never reloads itself", async () => {
1237 const tell = openIssue(app, alice, repo, { title: "Speed up boot" });
1238 if (!tell.ok) throw new Error(String(tell.error));
1239 const run = createRun(app.db, {
1240 repoId: repo.id,
1241 kind: "agent",
1242 issueId: tell.value.id,
1243 actorId: scoutId,
1244 reason: "assigned",
1245 });
1246 claimRun(app.db, run.id);
1247 for (let i = 0; i < 9; i++)
1248 appendRunLog(app.config.dataDir, run.id, `line ${i}`);
1249
1250 const page = await (await get("/alice/engine", writerCookie)).text();
1251 expect(page).toContain('id="app-run-tail"');
1252 expect(page).toContain(
1253 `data-stream="/api/v1/repos/alice/engine/runs/${run.id}/log?follow=1"`,
1254 );
1255 // The retired one-off class is gone from the HTML.
1256 expect(page).not.toContain("log-tail-row");
1257 // The block-level log box is a sibling <li> padded by §5's .card-body,
1258 // never inside a row's ellipsizing, nowrap .row-title, so its lines wrap.
1259 expect(page).toMatch(
1260 /<li class="card-body">\s*<details[^>]*>\s*<summary[^>]*>Run log<\/summary>\s*<[^>]*id="app-run-tail"/,
1261 );
1262 expect(page).not.toMatch(
1263 /<span\s+class="row-title"[^>]*>(?:(?!<\/span>)[\s\S])*id="app-run-tail"/,
1264 );
1265 // Bounded on the first paint too: only the last handful made it, and
1266 // the box says so.
1267 expect(page).not.toContain(">line 0<");
1268 expect(page).toContain(">line 8<");
1269 expect(page).toContain("Holding the last 6 lines here");
1270 // The page never reloads itself (#2873): the Now card is a live region
1271 // for this repository's runs, and the tail rides the one /events stream.
1272 expect(page).not.toMatch(/<meta\s+http-equiv="refresh"/);
1273 expect(page).toMatch(
1274 /id="live-now"\s+data-live="run\.queued,run\.started,run\.finished[^"]*"\s+data-live-repo="alice\/engine"/,
1275 );
1276
1277 const stream = await get(
1278 `/api/v1/repos/alice/engine/runs/${run.id}/log?follow=1`,
1279 writerCookie,
1280 );
1281 expect(stream.status).toBe(200);
1282
1283 finishRun(app.db, run.id, { state: "ok", summary: "planned" });
1284 const settled = await (await get("/alice/engine", writerCookie)).text();
1285 expect(settled).not.toContain('id="app-run-tail"');
1286 });
1287
1288 test("a log far larger than the tail's cap still ends the page at its true last line", async () => {
1289 const tell = openIssue(app, alice, repo, { title: "Long-running build" });
1290 if (!tell.ok) throw new Error(String(tell.error));
1291 const run = createRun(app.db, {
1292 repoId: repo.id,
1293 kind: "agent",
1294 issueId: tell.value.id,
1295 actorId: scoutId,
1296 reason: "assigned",
1297 });
1298 claimRun(app.db, run.id);
1299 // Far past a byte cap sized for a handful of lines (old default: 512KB).
1300 for (let i = 0; i < 2000; i++)
1301 appendRunLog(app.config.dataDir, run.id, `line ${i}`);
1302
1303 const page = await (await get("/alice/engine", writerCookie)).text();
1304 expect(page).not.toContain(">line 1993<");
1305 expect(page).toContain(">line 1999<");
1306 expect(page).toContain("Holding the last 6 lines here");
1307
1308 finishRun(app.db, run.id, { state: "ok", summary: "planned" });
1309 });
1310
1311 test("a reader without access gets the forge's not-found from the page and from the tail's own stream", async () => {
1312 const tell = openIssue(app, alice, repo, { title: "Private boot work" });
1313 if (!tell.ok) throw new Error(String(tell.error));
1314 const run = createRun(app.db, {
1315 repoId: repo.id,
1316 kind: "agent",
1317 issueId: tell.value.id,
1318 actorId: scoutId,
1319 reason: "assigned",
1320 });
1321 claimRun(app.db, run.id);
1322 appendRunLog(app.config.dataDir, run.id, "line 0");
1323
1324 const page = await get("/alice/engine", readerCookie);
1325 expect(page.status).toBe(404);
1326 const stream = await get(
1327 `/api/v1/repos/alice/engine/runs/${run.id}/log?follow=1`,
1328 readerCookie,
1329 );
1330 expect(stream.status).toBe(404);
1331 finishRun(app.db, run.id, { state: "ok", summary: "planned" });
1332 });
1333 });
1334
1335 /**
1336 * Issue #145: the run a brand-new app is waiting on is its first deploy, not
1337 * an agent's — the head card tails production's own deploy the same way a
1338 * running row is tailed, and yields to an agent run's own tail when one is
1339 * also going, so the page never opens two streams for one repo.
1340 */
1341 describe("the app's page tails the deploy that is bringing it alive", () => {
1342 let dataDir: string;
1343 let app: App;
1344 let servers: Servers;
1345 let base: string;
1346 let runtime: AppsRuntime;
1347 let repo: Repo;
1348 let record: AppRecord;
1349 let owner: string;
1350 let scoutId: string;
1351
1352 interface PreviewSlot {
1353 readonly app: AppRecord;
1354 readonly env: EnvName;
1355 readonly chain: Promise<void>;
1356 readonly restarts: number[];
1357 deploying?: unknown;
1358 live?: unknown;
1359 lastError?: string;
1360 lastRunId?: string;
1361 }
1362 const putEnv = (
1363 env: EnvName,
1364 slot: Omit<PreviewSlot, "app" | "env" | "chain" | "restarts">,
1365 ): void => {
1366 (runtime as unknown as { envs: Map<string, PreviewSlot> }).envs.set(
1367 `${record.id}/${env}`,
1368 { app: record, env, chain: Promise.resolve(), restarts: [], ...slot },
1369 );
1370 };
1371
1372 const signIn = async (username: string): Promise<string> =>
1373 (
1374 (
1375 await fetch(`${base}/signin`, {
1376 method: "POST",
1377 body: new URLSearchParams({
1378 username,
1379 password: "a-good-passphrase",
1380 }),
1381 redirect: "manual",
1382 })
1383 ).headers.get("set-cookie") ?? ""
1384 ).split(";")[0]!;
1385
1386 const get = (path: string, cookie: string) =>
1387 fetch(`${base}${path}`, {
1388 headers: { cookie: `${cookie}; forge_mode=studio` },
1389 redirect: "manual",
1390 });
1391
1392 // A fresh app for each test: one test's tell or run would otherwise fill
1393 // "Now" for the next, and its production slot stand in for the next's.
1394 beforeEach(async () => {
1395 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-deploy-tail-"));
1396 app = createApp({ dataDir, port: 0 });
1397 servers = await serve(app);
1398 base = servers.url;
1399 runtime = new AppsRuntime(app, {
1400 domain: "deploytail.test",
1401 scheme: "http",
1402 publicPort: servers.port,
1403 forgeUrl: servers.url,
1404 concurrency: 1,
1405 });
1406 const a = await createUser(app.db, { origin: "seed", trusted: true,
1407 username: "alice",
1408 email: "[email protected]",
1409 password: "a-good-passphrase",
1410 adminIfFirst: true,
1411 });
1412 if (!a.ok) throw new Error(a.error);
1413 owner = await signIn("alice");
1414 const made = await createRepo(app, a.value, {
1415 name: "kindling",
1416 visibility: "private",
1417 description: "a new app",
1418 });
1419 if (!made.ok) throw new Error(String(made.error));
1420 repo = made.value;
1421 const seeded = await commitToBranch(app, a.value, repo, {
1422 message: "Start",
1423 files: [{ path: "README.md", content: "kindling\n" }],
1424 });
1425 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
1426 const enabled = await enableApp(app, a.value, repo, { slug: "kindling" });
1427 if (!enabled.ok) throw new Error(String(enabled.error));
1428 record = getAppByRepo(app.db, repo.id)!;
1429 const scout = await createAgent(app, a.value, { username: "scout" });
1430 if (!scout.ok) throw new Error(String(scout.error));
1431 scoutId = scout.value.user.id;
1432 expect(addToCrew(app, a.value, repo, "scout").ok).toBe(true);
1433 });
1434
1435 afterEach(async () => {
1436 servers.stop();
1437 app.close();
1438 await rm(dataDir, { recursive: true, force: true });
1439 });
1440
1441 test("a production deploy under way, with no agent run going, is tailed in the head card and named as the deploy's own stream", async () => {
1442 const deployRun = createRun(app.db, {
1443 repoId: repo.id,
1444 kind: "deploy",
1445 env: "production",
1446 commitOid: "f00dcafe",
1447 reason: "boot",
1448 });
1449 claimRun(app.db, deployRun.id);
1450 appendRunLog(app.config.dataDir, deployRun.id, "checking out f00dcafe");
1451 appendRunLog(app.config.dataDir, deployRun.id, "install …");
1452 putEnv("production", {
1453 deploying: {
1454 run: { id: deployRun.id, commitOid: "f00dcafe" },
1455 abort: new AbortController(),
1456 },
1457 });
1458
1459 const page = await (await get("/alice/kindling", owner)).text();
1460 expect(page).toContain('id="app-run-tail"');
1461 expect(page).toContain('data-who="the deploy"');
1462 expect(page).toContain(
1463 `data-stream="/api/v1/repos/alice/kindling/runs/${deployRun.id}/log?follow=1"`,
1464 );
1465 expect(page).toContain("install …");
1466 // The page never reloads itself while the deploy goes (#2873): the head
1467 // card swaps in place on the deploy's run events.
1468 expect(page).not.toMatch(/<meta\s+http-equiv="refresh"/);
1469 // Issue #163: the deploy is plainly going, in the head card above — "Now"
1470 // does not also claim nothing is happening, and it does not open a
1471 // second stream of the same lines to say so.
1472 expect(page).not.toContain("Nothing yet");
1473 expect(page).toContain("Production is coming up");
1474 expect(page.match(/id="app-run-tail"/g)?.length).toBe(1);
1475
1476 finishRun(app.db, deployRun.id, { state: "ok", summary: "deploy" });
1477 });
1478
1479 test("an agent run at work outranks a background deploy: the page follows the crew, never both", async () => {
1480 const deployRun = createRun(app.db, {
1481 repoId: repo.id,
1482 kind: "deploy",
1483 env: "production",
1484 commitOid: "d0d0d0d0",
1485 reason: "push",
1486 });
1487 claimRun(app.db, deployRun.id);
1488 appendRunLog(app.config.dataDir, deployRun.id, "deploying d0d0d0d0");
1489 putEnv("production", {
1490 deploying: {
1491 run: { id: deployRun.id, commitOid: "d0d0d0d0" },
1492 abort: new AbortController(),
1493 },
1494 });
1495
1496 const tell = openIssue(app, getUserByName(app.db, "alice")!, repo, {
1497 title: "Add a footer",
1498 });
1499 if (!tell.ok) throw new Error(String(tell.error));
1500 const agentRun = createRun(app.db, {
1501 repoId: repo.id,
1502 kind: "agent",
1503 issueId: tell.value.id,
1504 actorId: scoutId,
1505 reason: "assigned",
1506 });
1507 claimRun(app.db, agentRun.id);
1508 appendRunLog(app.config.dataDir, agentRun.id, "scout is on it");
1509
1510 const page = await (await get("/alice/kindling", owner)).text();
1511 // Exactly one tail, and it is the crew's own run — never the deploy's.
1512 expect(page.match(/id="app-run-tail"/g)?.length).toBe(1);
1513 expect(page).not.toContain('data-who="the deploy"');
1514 expect(page).toContain(
1515 `data-stream="/api/v1/repos/alice/kindling/runs/${agentRun.id}/log?follow=1"`,
1516 );
1517
1518 finishRun(app.db, deployRun.id, { state: "ok", summary: "deploy" });
1519 finishRun(app.db, agentRun.id, { state: "ok", summary: "planned" });
1520 });
1521
1522 // Issue #284: production stays "live" long after its deploy finishes, and
1523 // a fresh live process that has written nothing to its log yet is not an
1524 // empty black box — the card says so in a line instead.
1525 test("with no agent run going and production live but silent, the head card says so in a line, not an empty box", async () => {
1526 putEnv("production", {
1527 live: {
1528 commitOid: "d0d0d0d0",
1529 handle: { port: 4501 },
1530 runId: "irrelevant",
1531 since: new Date().toISOString(),
1532 manifest: { tty: null },
1533 },
1534 });
1535 const page = await (await get("/alice/kindling", owner)).text();
1536 expect(page).toContain('id="app-run-tail"');
1537 expect(page).not.toContain('class="run-log');
1538 expect(page).toContain("Production is quiet");
1539 // Still nothing to fetch, and the page does not reload in case that
1540 // changes (#2873): the head card swaps in place when production moves.
1541 expect(page).not.toMatch(/<meta\s+http-equiv="refresh"/);
1542 });
1543 });
1544
1545 /**
1546 * Issue #284: production keeps writing to the same run's log for as long as
1547 * its process runs, long after the deploy that produced it is finished —
1548 * the head card's tail follows that, not just a deploy in flight.
1549 */
1550 describe("the app's page tails production's own output while it runs (#284)", () => {
1551 let dataDir: string;
1552 let app: App;
1553 let servers: Servers;
1554 let base: string;
1555 let runtime: AppsRuntime;
1556 let repo: Repo;
1557 let record: AppRecord;
1558 let owner: string;
1559 let strangerCookie: string;
1560 let scoutId: string;
1561
1562 interface PreviewSlot {
1563 readonly app: AppRecord;
1564 readonly env: EnvName;
1565 readonly chain: Promise<void>;
1566 readonly restarts: number[];
1567 deploying?: unknown;
1568 live?: unknown;
1569 lastError?: string;
1570 lastRunId?: string;
1571 }
1572 const putEnv = (
1573 env: EnvName,
1574 slot: Omit<PreviewSlot, "app" | "env" | "chain" | "restarts">,
1575 ): void => {
1576 (runtime as unknown as { envs: Map<string, PreviewSlot> }).envs.set(
1577 `${record.id}/${env}`,
1578 { app: record, env, chain: Promise.resolve(), restarts: [], ...slot },
1579 );
1580 };
1581
1582 const signIn = async (username: string): Promise<string> =>
1583 (
1584 (
1585 await fetch(`${base}/signin`, {
1586 method: "POST",
1587 body: new URLSearchParams({
1588 username,
1589 password: "a-good-passphrase",
1590 }),
1591 redirect: "manual",
1592 })
1593 ).headers.get("set-cookie") ?? ""
1594 ).split(";")[0]!;
1595
1596 const get = (path: string, cookie: string) =>
1597 fetch(`${base}${path}`, {
1598 headers: { cookie: `${cookie}; forge_mode=studio` },
1599 redirect: "manual",
1600 });
1601
1602 beforeAll(async () => {
1603 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-live-tail-"));
1604 app = createApp({ dataDir, port: 0 });
1605 servers = await serve(app);
1606 base = servers.url;
1607 runtime = new AppsRuntime(app, {
1608 domain: "livetail.test",
1609 scheme: "http",
1610 publicPort: servers.port,
1611 forgeUrl: servers.url,
1612 concurrency: 1,
1613 });
1614 const a = await createUser(app.db, { origin: "seed", trusted: true,
1615 username: "alice",
1616 email: "[email protected]",
1617 password: "a-good-passphrase",
1618 adminIfFirst: true,
1619 });
1620 if (!a.ok) throw new Error(a.error);
1621 const c = await createUser(app.db, { origin: "seed", trusted: true,
1622 username: "carol",
1623 email: "[email protected]",
1624 password: "a-good-passphrase",
1625 });
1626 if (!c.ok) throw new Error(c.error);
1627 owner = await signIn("alice");
1628 strangerCookie = await signIn("carol");
1629 const made = await createRepo(app, a.value, {
1630 name: "furnace",
1631 visibility: "public",
1632 description: "an app that keeps a fire going",
1633 });
1634 if (!made.ok) throw new Error(String(made.error));
1635 repo = made.value;
1636 const seeded = await commitToBranch(app, a.value, repo, {
1637 message: "Start",
1638 files: [{ path: "README.md", content: "furnace\n" }],
1639 });
1640 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
1641 const enabled = await enableApp(app, a.value, repo, { slug: "furnace" });
1642 if (!enabled.ok) throw new Error(String(enabled.error));
1643 record = getAppByRepo(app.db, repo.id)!;
1644 const scout = await createAgent(app, a.value, { username: "scout" });
1645 if (!scout.ok) throw new Error(String(scout.error));
1646 scoutId = scout.value.user.id;
1647 expect(addToCrew(app, a.value, repo, "scout").ok).toBe(true);
1648 });
1649
1650 afterAll(async () => {
1651 servers.stop();
1652 app.close();
1653 await rm(dataDir, { recursive: true, force: true });
1654 });
1655
1656 test("production running with lines in its log carries the tail, named as the app's own", async () => {
1657 const deployRun = createRun(app.db, {
1658 repoId: repo.id,
1659 kind: "deploy",
1660 env: "production",
1661 commitOid: "b16b00b5",
1662 reason: "push",
1663 });
1664 claimRun(app.db, deployRun.id);
1665 for (let i = 0; i < 9; i++)
1666 appendRunLog(app.config.dataDir, deployRun.id, `serving request ${i}`);
1667 finishRun(app.db, deployRun.id, { state: "ok", summary: "deploy" });
1668 putEnv("production", {
1669 live: {
1670 commitOid: "b16b00b5",
1671 handle: { port: 4503 },
1672 runId: deployRun.id,
1673 since: new Date().toISOString(),
1674 manifest: { tty: null },
1675 },
1676 });
1677
1678 const page = await (await get("/alice/furnace", owner)).text();
1679 expect(page).toContain('id="app-run-tail"');
1680 expect(page).toContain('data-who="the app"');
1681 expect(page).toContain(
1682 `data-stream="/api/v1/repos/alice/furnace/runs/${deployRun.id}/log?follow=1"`,
1683 );
1684 expect(page).not.toContain(">serving request 0<");
1685 expect(page).toContain(">serving request 8<");
1686 expect(page).toContain("Holding the last 6 lines here");
1687 expect(page).not.toMatch(/<meta\s+http-equiv="refresh"/);
1688
1689 // Issue #284's own point: the run this reads from is long finished —
1690 // the tail follows the env that points at it, not the run's own state.
1691 expect(getRun(app.db, deployRun.id)?.state).toBe("ok");
1692 });
1693
1694 test("a viewer who cannot see the owner's facts gets no output at all", async () => {
1695 const response = await get("/alice/furnace", strangerCookie);
1696 expect(response.status).toBe(200);
1697 const page = await response.text();
1698 expect(page).not.toContain('id="app-run-tail"');
1699 expect(page).not.toContain("serving request");
1700 });
1701
1702 test("an agent run at work still outranks production's own tail", async () => {
1703 const tell = openIssue(app, getUserByName(app.db, "alice")!, repo, {
1704 title: "Add a chimney",
1705 });
1706 if (!tell.ok) throw new Error(String(tell.error));
1707 const agentRun = createRun(app.db, {
1708 repoId: repo.id,
1709 kind: "agent",
1710 issueId: tell.value.id,
1711 actorId: scoutId,
1712 reason: "assigned",
1713 });
1714 claimRun(app.db, agentRun.id);
1715 appendRunLog(app.config.dataDir, agentRun.id, "scout is on it");
1716
1717 const page = await (await get("/alice/furnace", owner)).text();
1718 expect(page.match(/id="app-run-tail"/g)?.length).toBe(1);
1719 expect(page).not.toContain('data-who="the app"');
1720 expect(page).toContain(
1721 `data-stream="/api/v1/repos/alice/furnace/runs/${agentRun.id}/log?follow=1"`,
1722 );
1723
1724 finishRun(app.db, agentRun.id, { state: "ok", summary: "planned" });
1725 });
1726 });
1727
1728 /**
1729 * Issue #163: "Now" is built from agent runs alone, so a fresh app with a
1730 * deploy going and nothing else read as if nothing were happening — right
1731 * beside the head card tailing that very deploy. "Now"'s empty state now
1732 * says otherwise when the head card is tailing a deploy, and keeps its
1733 * ordinary wording when nothing at all is going.
1734 */
1735 describe(`"Now" while production is coming up and nothing else is`, () => {
1736 let dataDir: string;
1737 let app: App;
1738 let servers: Servers;
1739 let base: string;
1740 let runtime: AppsRuntime;
1741 let repo: Repo;
1742 let record: AppRecord;
1743 let owner: string;
1744
1745 interface PreviewSlot {
1746 readonly app: AppRecord;
1747 readonly env: EnvName;
1748 readonly chain: Promise<void>;
1749 readonly restarts: number[];
1750 deploying?: unknown;
1751 live?: unknown;
1752 lastError?: string;
1753 lastRunId?: string;
1754 }
1755 const putEnv = (
1756 env: EnvName,
1757 slot: Omit<PreviewSlot, "app" | "env" | "chain" | "restarts">,
1758 ): void => {
1759 (runtime as unknown as { envs: Map<string, PreviewSlot> }).envs.set(
1760 `${record.id}/${env}`,
1761 { app: record, env, chain: Promise.resolve(), restarts: [], ...slot },
1762 );
1763 };
1764
1765 const signIn = async (username: string): Promise<string> =>
1766 (
1767 (
1768 await fetch(`${base}/signin`, {
1769 method: "POST",
1770 body: new URLSearchParams({
1771 username,
1772 password: "a-good-passphrase",
1773 }),
1774 redirect: "manual",
1775 })
1776 ).headers.get("set-cookie") ?? ""
1777 ).split(";")[0]!;
1778
1779 const get = (path: string, cookie: string) =>
1780 fetch(`${base}${path}`, {
1781 headers: { cookie: `${cookie}; forge_mode=studio` },
1782 redirect: "manual",
1783 });
1784
1785 beforeAll(async () => {
1786 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-now-empty-"));
1787 app = createApp({ dataDir, port: 0 });
1788 servers = await serve(app);
1789 base = servers.url;
1790 runtime = new AppsRuntime(app, {
1791 domain: "nowempty.test",
1792 scheme: "http",
1793 publicPort: servers.port,
1794 forgeUrl: servers.url,
1795 concurrency: 1,
1796 });
1797 const a = await createUser(app.db, { origin: "seed", trusted: true,
1798 username: "alice",
1799 email: "[email protected]",
1800 password: "a-good-passphrase",
1801 adminIfFirst: true,
1802 });
1803 if (!a.ok) throw new Error(a.error);
1804 owner = await signIn("alice");
1805 const made = await createRepo(app, a.value, {
1806 name: "ember",
1807 visibility: "private",
1808 description: "a new app",
1809 });
1810 if (!made.ok) throw new Error(String(made.error));
1811 repo = made.value;
1812 const seeded = await commitToBranch(app, a.value, repo, {
1813 message: "Start",
1814 files: [{ path: "README.md", content: "ember\n" }],
1815 });
1816 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
1817 const enabled = await enableApp(app, a.value, repo, { slug: "ember" });
1818 if (!enabled.ok) throw new Error(String(enabled.error));
1819 record = getAppByRepo(app.db, repo.id)!;
1820 });
1821
1822 afterAll(async () => {
1823 servers.stop();
1824 app.close();
1825 await rm(dataDir, { recursive: true, force: true });
1826 });
1827
1828 test('a running deploy and no agent run: "Now" does not say nothing is happening, and opens no second tail', async () => {
1829 const deployRun = createRun(app.db, {
1830 repoId: repo.id,
1831 kind: "deploy",
1832 env: "production",
1833 commitOid: "eae580a",
1834 reason: "boot",
1835 });
1836 claimRun(app.db, deployRun.id);
1837 appendRunLog(app.config.dataDir, deployRun.id, "deploy in progress");
1838 putEnv("production", {
1839 deploying: {
1840 run: { id: deployRun.id, commitOid: "eae580a" },
1841 abort: new AbortController(),
1842 },
1843 });
1844
1845 const page = await (await get("/alice/ember", owner)).text();
1846 expect(page).not.toContain("Nothing yet");
1847 expect(page).toContain("Production is coming up");
1848 // Exactly one tail on the page — the head card's, not a second one in
1849 // "Now".
1850 expect(page.match(/id="app-run-tail"/g)?.length).toBe(1);
1851
1852 finishRun(app.db, deployRun.id, { state: "ok", summary: "deploy" });
1853 });
1854
1855 test('with nothing going at all, "Now" reads exactly as it does today — production live and silent says its own quiet line above, not through "Now"', async () => {
1856 putEnv("production", {
1857 live: {
1858 commitOid: "eae580a",
1859 handle: { port: 4502 },
1860 runId: "irrelevant",
1861 since: new Date().toISOString(),
1862 manifest: { tty: null },
1863 },
1864 });
1865
1866 const page = await (await get("/alice/ember", owner)).text();
1867 expect(page).toContain("Nothing yet. Tell the crew something.");
1868 // The head card's own quiet line (#284) is not the deploy-in-progress
1869 // wording "Now" uses — production here is live, not coming up.
1870 expect(page).not.toContain("Production is coming up");
1871 expect(page).toContain("Production is quiet");
1872 });
1873 });
1874
1875 /**
1876 * Issue #183: `tell` cannot route a tell to anyone with nobody on the
1877 * crew — the issue it opens stays unassigned forever, not merely
1878 * unpicked-up like an agent-held tell. Both surfaces that used to call this
1879 * "quiet" or "nothing yet" must say the honest reason instead, and both
1880 * failed before this change.
1881 */
1882 describe('a tell nobody on the crew can take is not "quiet"', () => {
1883 let dataDir: string;
1884 let app: App;
1885 let servers: Servers;
1886 let base: string;
1887 let repo: Repo;
1888 let alice: User;
1889 let cookie: string;
1890
1891 const get = (path: string) =>
1892 fetch(`${base}${path}`, {
1893 headers: { cookie: `${cookie}; forge_mode=studio` },
1894 redirect: "manual",
1895 });
1896 const post = (path: string, body: Record<string, string>) =>
1897 fetch(`${base}${path}`, {
1898 method: "POST",
1899 headers: { cookie, origin: base },
1900 body: new URLSearchParams(body),
1901 redirect: "manual",
1902 });
1903
1904 beforeAll(async () => {
1905 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-untaken-"));
1906 app = createApp({ dataDir, port: 0 });
1907 servers = await serve(app);
1908 base = servers.url;
1909 const a = await createUser(app.db, { origin: "seed", trusted: true,
1910 username: "alice",
1911 email: "[email protected]",
1912 password: "a-good-passphrase",
1913 adminIfFirst: true,
1914 });
1915 if (!a.ok) throw new Error(a.error);
1916 alice = a.value;
1917 cookie = (
1918 (
1919 await fetch(`${base}/signin`, {
1920 method: "POST",
1921 body: new URLSearchParams({
1922 username: "alice",
1923 password: "a-good-passphrase",
1924 }),
1925 redirect: "manual",
1926 })
1927 ).headers.get("set-cookie") ?? ""
1928 ).split(";")[0]!;
1929 const made = await createRepo(app, alice, {
1930 name: "pad",
1931 visibility: "private",
1932 description: "a pad for notes",
1933 });
1934 if (!made.ok) throw new Error(String(made.error));
1935 repo = made.value;
1936 const seeded = await commitToBranch(app, alice, repo, {
1937 message: "Start",
1938 files: [{ path: "README.md", content: "pad\n" }],
1939 });
1940 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
1941 const enabled = await enableApp(app, alice, repo, { slug: "pad" });
1942 if (!enabled.ok) throw new Error(String(enabled.error));
1943 // No crew at all — the exact state `tell` cannot route out of. Every
1944 // test below reads the one tell told into it here.
1945 const told = await post("/alice/pad/tell", {
1946 text: "Add a dark mode toggle.",
1947 });
1948 if (told.status !== 303) throw new Error(`tell: ${told.status}`);
1949 });
1950
1951 afterAll(async () => {
1952 servers.stop();
1953 app.close();
1954 await rm(dataDir, { recursive: true, force: true });
1955 });
1956
1957 test('a crewless tell renders a "Now" row, not the empty state, and links to where a crew member is added', async () => {
1958 const page = await (await get("/alice/pad")).text();
1959 expect(page).not.toContain("Nothing yet. Tell the crew something.");
1960 expect(page).toContain("Add a dark mode toggle.");
1961 expect(page).toContain("nobody on the crew to take it");
1962 expect(page).toContain(`href="/alice/pad/settings"`);
1963 });
1964
1965 test('the shelf row for that app is not "quiet"', async () => {
1966 const home = await (await get("/apps")).text();
1967 const at = home.indexOf('href="/alice/pad"');
1968 expect(at).toBeGreaterThan(-1);
1969 const start = home.lastIndexOf("<li>", at);
1970 const shelfRow = home.slice(start, home.indexOf("</li>", at) + 5);
1971 expect(shelfRow).not.toContain("quiet — tell the crew something");
1972 expect(shelfRow).toContain("nobody on the crew to take it");
1973 });
1974
1975 test("a reader of the public app sees the fact, not the owner's invitation or a link to settings", async () => {
1976 app.db.run("UPDATE repo SET visibility = 'public' WHERE id = ?", [
1977 repo.id,
1978 ]);
1979 const b = await createUser(app.db, { origin: "seed", trusted: true,
1980 username: "bob",
1981 email: "[email protected]",
1982 password: "a-good-passphrase",
1983 });
1984 if (!b.ok) throw new Error(b.error);
1985 const bobCookie = (
1986 (
1987 await fetch(`${base}/signin`, {
1988 method: "POST",
1989 body: new URLSearchParams({
1990 username: "bob",
1991 password: "a-good-passphrase",
1992 }),
1993 redirect: "manual",
1994 })
1995 ).headers.get("set-cookie") ?? ""
1996 ).split(";")[0]!;
1997 const read = await (
1998 await fetch(`${base}/alice/pad`, {
1999 headers: { cookie: `${bobCookie}; forge_mode=studio` },
2000 })
2001 ).text();
2002 expect(read).toContain("Add a dark mode toggle.");
2003 expect(read).toContain("nobody on the crew to take it");
2004 expect(read).not.toContain("add someone");
2005 expect(read).not.toContain(`href="/alice/pad/settings"`);
2006 // The writer still gets the invitation, link included.
2007 const write = await (await get("/alice/pad")).text();
2008 expect(write).toContain("add someone");
2009 expect(write).toContain(`href="/alice/pad/settings"`);
2010 });
2011 });
2012
2013 /**
2014 * Issue #226: taking a tell yourself — assigning it to a person, not an
2015 * agent — used to drop it clean off "Now": the panel's filter only ever
2016 * let an agent-held or unassigned tell through, so a person holding it
2017 * counted as nobody. `tellWord`'s own fallback said "not picked up yet"
2018 * regardless, so the tell's own page agreed with nothing true either.
2019 * Both failed before this change.
2020 */
2021 describe("a tell held by a person still shows on the front door (#226)", () => {
2022 let dataDir: string;
2023 let app: App;
2024 let servers: Servers;
2025 let base: string;
2026 let repo: Repo;
2027 let alice: User;
2028 let cookie: string;
2029 let tell: Issue;
2030
2031 const get = (path: string) =>
2032 fetch(`${base}${path}`, {
2033 headers: { cookie: `${cookie}; forge_mode=studio` },
2034 redirect: "manual",
2035 });
2036
2037 beforeAll(async () => {
2038 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-person-held-"));
2039 app = createApp({ dataDir, port: 0 });
2040 servers = await serve(app);
2041 base = servers.url;
2042 const a = await createUser(app.db, { origin: "seed", trusted: true,
2043 username: "alice",
2044 email: "[email protected]",
2045 password: "a-good-passphrase",
2046 adminIfFirst: true,
2047 });
2048 if (!a.ok) throw new Error(a.error);
2049 alice = a.value;
2050 cookie = (
2051 (
2052 await fetch(`${base}/signin`, {
2053 method: "POST",
2054 body: new URLSearchParams({
2055 username: "alice",
2056 password: "a-good-passphrase",
2057 }),
2058 redirect: "manual",
2059 })
2060 ).headers.get("set-cookie") ?? ""
2061 ).split(";")[0]!;
2062 const made = await createRepo(app, alice, {
2063 name: "pad",
2064 visibility: "private",
2065 description: "a pad for notes",
2066 });
2067 if (!made.ok) throw new Error(String(made.error));
2068 repo = made.value;
2069 const seeded = await commitToBranch(app, alice, repo, {
2070 message: "Start",
2071 files: [{ path: "README.md", content: "pad\n" }],
2072 });
2073 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
2074 const enabled = await enableApp(app, alice, repo, { slug: "pad" });
2075 if (!enabled.ok) throw new Error(String(enabled.error));
2076 const scout = await createAgent(app, alice, { username: "scout" });
2077 if (!scout.ok) throw new Error(String(scout.error));
2078 expect(addToCrew(app, alice, repo, "scout").ok).toBe(true);
2079
2080 const told = openIssue(app, alice, repo, {
2081 title: "Give the header a subtitle",
2082 });
2083 if (!told.ok) throw new Error(String(told.error));
2084 tell = told.value;
2085 // Taken by a person — the owner herself — not routed to the crew.
2086 expect(assignIssue(app, alice, repo, tell, alice).ok).toBe(true);
2087 });
2088
2089 afterAll(async () => {
2090 servers.stop();
2091 app.close();
2092 await rm(dataDir, { recursive: true, force: true });
2093 });
2094
2095 test('"Now" names the person who holds it, not "Nothing yet"', async () => {
2096 const page = await (await get("/alice/pad")).text();
2097 expect(page).not.toContain("Nothing yet. Tell the crew something.");
2098 expect(page).toContain("Give the header a subtitle");
2099 expect(page).toContain("alice");
2100 expect(page).not.toContain("not picked up yet");
2101 });
2102
2103 test("the tell's own page agrees with the panel: held, not unpicked", async () => {
2104 const page = await (await get(`/alice/pad/issues/${tell.number}`)).text();
2105 expect(page).not.toContain("not picked up yet");
2106 expect(page).toContain(">alice</a>");
2107 expect(page).toContain("has it");
2108 });
2109 });
2110
2111 /**
2112 * Issue #191: `appShelf`/`shelfRows` used to derive a row from agent runs
2113 * alone, so a change a person opened by hand — no run anywhere near it —
2114 * fell to "quiet — tell the crew something" even while it sat green and
2115 * ready for a look, and "Happening now" said nobody was working while that
2116 * same change waited. Both read from `openPullsForRepos` now, grouped once
2117 * for the whole shelf, so a person's own change counts exactly as an
2118 * agent's does. Failed before this change.
2119 */
2120 describe("an open change is never quiet on the front door", () => {
2121 let dataDir: string;
2122 let app: App;
2123 let servers: Servers;
2124 let base: string;
2125 let repo: Repo;
2126 let alice: User;
2127 let cookie: string;
2128 let pull: Pull;
2129
2130 const get = (path: string) =>
2131 fetch(`${base}${path}`, {
2132 headers: { cookie: `${cookie}; forge_mode=studio` },
2133 redirect: "manual",
2134 });
2135
2136 beforeAll(async () => {
2137 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-open-change-"));
2138 app = createApp({ dataDir, port: 0 });
2139 servers = await serve(app);
2140 base = servers.url;
2141 const a = await createUser(app.db, { origin: "seed", trusted: true,
2142 username: "alice",
2143 email: "[email protected]",
2144 password: "a-good-passphrase",
2145 adminIfFirst: true,
2146 });
2147 if (!a.ok) throw new Error(a.error);
2148 alice = a.value;
2149 cookie = (
2150 (
2151 await fetch(`${base}/signin`, {
2152 method: "POST",
2153 body: new URLSearchParams({
2154 username: "alice",
2155 password: "a-good-passphrase",
2156 }),
2157 redirect: "manual",
2158 })
2159 ).headers.get("set-cookie") ?? ""
2160 ).split(";")[0]!;
2161 const made = await createRepo(app, alice, {
2162 name: "pad",
2163 visibility: "private",
2164 description: "a pad for notes",
2165 });
2166 if (!made.ok) throw new Error(String(made.error));
2167 repo = made.value;
2168 const seeded = await commitToBranch(app, alice, repo, {
2169 message: "Start",
2170 files: [{ path: "README.md", content: "pad\n" }],
2171 });
2172 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
2173 const enabled = await enableApp(app, alice, repo, { slug: "pad" });
2174 if (!enabled.ok) throw new Error(String(enabled.error));
2175
2176 // Alice opens the change herself, through the editor — no agent run
2177 // anywhere near it, the exact case `latestAgentRunsForRepos` misses.
2178 const branch = await commitToBranch(app, alice, repo, {
2179 branch: "note",
2180 base: "main",
2181 message: "A note",
2182 files: [{ path: "NOTE.md", content: "note\n" }],
2183 });
2184 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
2185 const opened = await openPull(app, alice, repo, {
2186 title: "A note",
2187 body: "",
2188 baseBranch: "main",
2189 headBranch: "note",
2190 });
2191 if (!opened.ok) throw new Error(String(opened.error));
2192 pull = opened.value;
2193 const check = createRun(app.db, {
2194 repoId: repo.id,
2195 kind: "check",
2196 issueId: pull.id,
2197 env: `pull-${pull.number}`,
2198 commitOid: pull.headOid,
2199 reason: "pull",
2200 });
2201 claimRun(app.db, check.id);
2202 finishRun(app.db, check.id, { state: "ok", summary: "check passed in 1s" });
2203
2204 // A second app, otherwise identical, with nothing open at all — the
2205 // control that must keep reading "quiet".
2206 const madeQuiet = await createRepo(app, alice, {
2207 name: "quiet",
2208 visibility: "private",
2209 description: "says nothing",
2210 });
2211 if (!madeQuiet.ok) throw new Error(String(madeQuiet.error));
2212 const seededQuiet = await commitToBranch(app, alice, madeQuiet.value, {
2213 message: "Start",
2214 files: [{ path: "README.md", content: "quiet\n" }],
2215 });
2216 if (!seededQuiet.ok) throw new Error(JSON.stringify(seededQuiet.error));
2217 const enabledQuiet = await enableApp(app, alice, madeQuiet.value, {
2218 slug: "quiet",
2219 });
2220 if (!enabledQuiet.ok) throw new Error(String(enabledQuiet.error));
2221 });
2222
2223 afterAll(async () => {
2224 servers.stop();
2225 app.close();
2226 await rm(dataDir, { recursive: true, force: true });
2227 });
2228
2229 test("the shelf row carries the change's own word, its badge and dot kept", async () => {
2230 const home = await (await get("/apps")).text();
2231 const at = home.indexOf('href="/alice/pad"');
2232 expect(at).toBeGreaterThan(-1);
2233 const start = home.lastIndexOf("<li>", at);
2234 const shelfRow = home.slice(start, home.indexOf("</li>", at) + 5);
2235 expect(shelfRow).not.toContain("quiet — tell the crew something");
2236 expect(shelfRow).toContain('data-word="look"');
2237 expect(shelfRow).toContain('class="dot"');
2238 expect(shelfRow).toContain('class="badge"');
2239 });
2240
2241 test("an app with nothing open still reads quiet", async () => {
2242 const home = await (await get("/apps")).text();
2243 const at = home.indexOf('href="/alice/quiet"');
2244 expect(at).toBeGreaterThan(-1);
2245 const start = home.lastIndexOf("<li>", at);
2246 const shelfRow = home.slice(start, home.indexOf("</li>", at) + 5);
2247 expect(shelfRow).toContain("quiet — tell the crew something");
2248 });
2249 });
2250
2251 /**
2252 * Issue #215: once a merged change closes, it drops out of
2253 * `openPullsForRepos`, and before this the shelf and the rail had nothing
2254 * left to say about it but "quiet" — the row's *happening* half spoke only
2255 * for an agent run, never a deploy. `productionFact` (`words.ts`) reads
2256 * production's own status for the fact a merge or a push leaves behind:
2257 * the deploy that landed, or one that failed behind what still serves.
2258 */
2259 describe("a deploy that just landed is news on the front door", () => {
2260 let dataDir: string;
2261 let app: App;
2262 let servers: Servers;
2263 let base: string;
2264 let runtime: AppsRuntime;
2265 let repo: Repo;
2266 let record: AppRecord;
2267 let quietRepo: Repo;
2268 let alice: User;
2269 let cookie: string;
2270 let deployRun: Run;
2271
2272 interface PreviewSlot {
2273 readonly app: AppRecord;
2274 readonly env: EnvName;
2275 readonly chain: Promise<void>;
2276 readonly restarts: number[];
2277 deploying?: unknown;
2278 live?: unknown;
2279 lastError?: string;
2280 lastRunId?: string;
2281 }
2282 const putEnv = (
2283 appRecord: AppRecord,
2284 env: EnvName,
2285 slot: Omit<PreviewSlot, "app" | "env" | "chain" | "restarts">,
2286 ): void => {
2287 (runtime as unknown as { envs: Map<string, PreviewSlot> }).envs.set(
2288 `${appRecord.id}/${env}`,
2289 { app: appRecord, env, chain: Promise.resolve(), restarts: [], ...slot },
2290 );
2291 };
2292
2293 const get = (path: string) =>
2294 fetch(`${base}${path}`, {
2295 headers: { cookie: `${cookie}; forge_mode=studio` },
2296 redirect: "manual",
2297 });
2298
2299 const shelfRowFor = (home: string, href: string): string => {
2300 const at = home.indexOf(`href="${href}"`);
2301 expect(at).toBeGreaterThan(-1);
2302 const start = home.lastIndexOf("<li>", at);
2303 return home.slice(start, home.indexOf("</li>", at) + 5);
2304 };
2305
2306 beforeAll(async () => {
2307 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-shipped-"));
2308 app = createApp({ dataDir, port: 0 });
2309 servers = await serve(app);
2310 base = servers.url;
2311 // Uncalled `.start()`: this runtime never subscribes to the bus, so
2312 // nothing here spins a real process — its `envs` are only ever what
2313 // `putEnv` puts there, the same fiction the live-fragment tests use.
2314 runtime = new AppsRuntime(app, {
2315 domain: "shipped.test",
2316 scheme: "http",
2317 publicPort: servers.port,
2318 forgeUrl: servers.url,
2319 concurrency: 1,
2320 });
2321 const a = await createUser(app.db, { origin: "seed", trusted: true,
2322 username: "alice",
2323 email: "[email protected]",
2324 password: "a-good-passphrase",
2325 adminIfFirst: true,
2326 });
2327 if (!a.ok) throw new Error(a.error);
2328 alice = a.value;
2329 cookie = (
2330 (
2331 await fetch(`${base}/signin`, {
2332 method: "POST",
2333 body: new URLSearchParams({
2334 username: "alice",
2335 password: "a-good-passphrase",
2336 }),
2337 redirect: "manual",
2338 })
2339 ).headers.get("set-cookie") ?? ""
2340 ).split(";")[0]!;
2341
2342 const made = await createRepo(app, alice, {
2343 name: "sap",
2344 visibility: "private",
2345 description: "a sap for notes",
2346 });
2347 if (!made.ok) throw new Error(String(made.error));
2348 repo = made.value;
2349 const seeded = await commitToBranch(app, alice, repo, {
2350 message: "Start",
2351 files: [{ path: "README.md", content: "sap\n" }],
2352 });
2353 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
2354 const enabled = await enableApp(app, alice, repo, { slug: "sap" });
2355 if (!enabled.ok) throw new Error(String(enabled.error));
2356 record = getAppByRepo(app.db, repo.id)!;
2357
2358 deployRun = createRun(app.db, {
2359 repoId: repo.id,
2360 kind: "deploy",
2361 env: "production",
2362 commitOid: "6c99218",
2363 reason: "merge",
2364 actorId: alice.id,
2365 });
2366 claimRun(app.db, deployRun.id);
2367 deployRun =
2368 finishRun(app.db, deployRun.id, { state: "ok", summary: "deploy" }) ??
2369 deployRun;
2370
2371 // A second app, otherwise identical, that never shipped — the control
2372 // that must keep reading quiet.
2373 const madeQuiet = await createRepo(app, alice, {
2374 name: "quiet2",
2375 visibility: "private",
2376 description: "says nothing",
2377 });
2378 if (!madeQuiet.ok) throw new Error(String(madeQuiet.error));
2379 const seededQuiet = await commitToBranch(app, alice, madeQuiet.value, {
2380 message: "Start",
2381 files: [{ path: "README.md", content: "quiet\n" }],
2382 });
2383 if (!seededQuiet.ok) throw new Error(JSON.stringify(seededQuiet.error));
2384 const enabledQuiet = await enableApp(app, alice, madeQuiet.value, {
2385 slug: "quiet2",
2386 });
2387 if (!enabledQuiet.ok) throw new Error(String(enabledQuiet.error));
2388 quietRepo = madeQuiet.value;
2389 });
2390
2391 // Each test starts from the merge's deploy serving just now; a test that
2392 // wants another production slot puts its own.
2393 beforeEach(() => {
2394 const servingAt = new Date().toISOString();
2395 putEnv(record, "production", {
2396 live: {
2397 commitOid: "6c99218",
2398 handle: { port: 4321 },
2399 runId: deployRun.id,
2400 since: servingAt,
2401 servingAt,
2402 createdAt: servingAt,
2403 manifest: { tty: null },
2404 },
2405 });
2406 });
2407
2408 afterAll(async () => {
2409 servers.stop();
2410 app.close();
2411 await rm(dataDir, { recursive: true, force: true });
2412 });
2413
2414 test("the shelf row reads shipped, with who and when, not quiet", async () => {
2415 const home = await (await get("/apps")).text();
2416 const shelfRow = shelfRowFor(home, "/alice/sap");
2417 expect(shelfRow).not.toContain("quiet — tell the crew something");
2418 expect(shelfRow).toContain('data-word="shipped"');
2419 expect(shelfRow).toContain("alice");
2420 });
2421
2422 test("an app with no recent deploy still reads quiet", async () => {
2423 const home = await (await get("/apps")).text();
2424 const shelfRow = shelfRowFor(home, `/alice/${quietRepo.name}`);
2425 expect(shelfRow).toContain("quiet — tell the crew something");
2426 });
2427
2428 test("a deploy no push asked for is not news: a redeploy or the boot reconcile does not read shipped", async () => {
2429 const restartedAt = new Date().toISOString();
2430 putEnv(record, "production", {
2431 live: {
2432 commitOid: "6c99218",
2433 handle: { port: 4321 },
2434 runId: deployRun.id,
2435 since: restartedAt,
2436 servingAt: restartedAt,
2437 // No `createdAt`: nothing asked a push for this one — a redeploy
2438 // someone pressed, or the boot reconcile every restart runs.
2439 manifest: { tty: null },
2440 },
2441 });
2442 const home = await (await get("/apps")).text();
2443 const shelfRow = shelfRowFor(home, "/alice/sap");
2444 expect(shelfRow).not.toContain('data-word="shipped"');
2445 expect(shelfRow).toContain("quiet — tell the crew something");
2446 });
2447
2448 test("a deploy older than the news window reads quiet again, not shipped forever", async () => {
2449 const old = new Date(Date.now() - 25 * 60 * 60_000).toISOString();
2450 putEnv(record, "production", {
2451 live: {
2452 commitOid: "6c99218",
2453 handle: { port: 4321 },
2454 runId: deployRun.id,
2455 since: old,
2456 servingAt: old,
2457 createdAt: old,
2458 manifest: { tty: null },
2459 },
2460 });
2461 const home = await (await get("/apps")).text();
2462 const shelfRow = shelfRowFor(home, "/alice/sap");
2463 expect(shelfRow).not.toContain('data-word="shipped"');
2464 expect(shelfRow).toContain("quiet — tell the crew something");
2465 });
2466 });
2467
2468 /**
2469 * Issue #65: a public app's studio page is the app, not the owner's books.
2470 * `canSeeOwnerFacts` (`src/core/repos.ts`) is the one predicate — the same
2471 * as `canWrite` today — that every surface carrying money, the crew or the
2472 * decision ledger must ask before it computes or renders one of those.
2473 */
2474
2475 describe("the pin slot's stylesheet", () => {
2476 test("caps [data-pins] with scroll and reserves it while a check is open", async () => {
2477 const { css } = await import("../src/web/style.ts");
2478 expect(css).toContain("[data-pins] { max-height: 9em; overflow-y: auto; }");
2479 expect(css).toContain('[data-check-progress][data-state="running"] [data-pins] { height: 9em; }');
2480 expect(css).toContain('[data-check-progress][data-state="queued"] [data-pins]');
2481 expect(css).not.toContain(".pinned-");
2482 // Cards stack as blocks (a flex column would squash them into 9em), and output wraps.
2483 expect(css).toContain("[data-pins] .card + .card { margin-top: 12px; }");
2484 expect(css).toContain("[data-pins] pre { margin: 4px 0 0; white-space: pre-wrap; font-size: var(--fs-min); }");
2485 });
2486 test("the slot is not a flex column", async () => {
2487 const { css } = await import("../src/web/style.ts");
2488 expect(css).not.toMatch(/\[data-pins\][^{]*\{[^}]*display:\s*(flex|grid)/);
2489 });
2490 });
2491
2492 describe("run page controls leave the old classes behind (#4263)", () => {
2493 test("no run-jump, run-live or run-steer class selector is defined, and [hidden] still beats .btn", async () => {
2494 const { css } = await import("../src/web/style.ts");
2495 for (const old of ["run-jump", "run-live", "run-steer"]) {
2496 expect(css).not.toMatch(new RegExp(`\\.${old}(?![\\w-])`));
2497 }
2498 // The layout is kept under the ids the client script already uses.
2499 expect(css).toMatch(/#run-steer \{[^}]*position: sticky/);
2500 expect(css).toMatch(/#run-jump \{[^}]*position: sticky/);
2501 expect(css).toMatch(/\[hidden\] \{ display: none !important; \}/);
2502 });
2503 });
test/studio-shelf-news.test.ts added +368
@@ -0,0 +1,368 @@
1 /**
2 * What the front door's shelf calls news rather than quiet: an open change,
3 * and a deploy that just landed.
4 *
5 * One of the files the studio's suite was split into so no one file sets the
6 * gate's floor (#2038) or passes 800 lines (#14011); the rest are
7 * test/studio*.test.ts, and they sign in through test/helpers/studio-signin.ts.
8 */
9
10 import { signInCookie } from "./helpers/studio-signin.ts";
11 import {
12 afterAll,
13 beforeAll,
14 beforeEach,
15 describe,
16 expect,
17 test,
18 } from "bun:test";
19 import { mkdtemp, rm } from "node:fs/promises";
20 import { tmpdir } from "node:os";
21 import { join } from "node:path";
22 import { createApp, type App } from "../src/app.ts";
23 import { enableApp } from "../src/actions/apps.ts";
24 import type { EnvName } from "../src/apps/data.ts";
25 import { AppsRuntime } from "../src/apps/runtime.ts";
26 import { getAppByRepo, type AppRecord } from "../src/core/apps.ts";
27 import { openPull } from "../src/actions/pulls.ts";
28 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
29 import { type Pull } from "../src/core/pulls.ts";
30 import type { Repo } from "../src/core/repos.ts";
31 import { claimRun, createRun, finishRun, type Run } from "../src/core/runs.ts";
32 import { createUser, type User } from "../src/core/users.ts";
33 import { serve, type Servers } from "../src/http/server.ts";
34
35 /**
36 * Issue #191: `appShelf`/`shelfRows` used to derive a row from agent runs
37 * alone, so a change a person opened by hand — no run anywhere near it —
38 * fell to "quiet — tell the crew something" even while it sat green and
39 * ready for a look, and "Happening now" said nobody was working while that
40 * same change waited. Both read from `openPullsForRepos` now, grouped once
41 * for the whole shelf, so a person's own change counts exactly as an
42 * agent's does. Failed before this change.
43 */
44 describe("an open change is never quiet on the front door", () => {
45 let dataDir: string;
46 let app: App;
47 let servers: Servers;
48 let base: string;
49 let repo: Repo;
50 let alice: User;
51 let cookie: string;
52 let pull: Pull;
53
54 const get = (path: string) =>
55 fetch(`${base}${path}`, {
56 headers: { cookie: `${cookie}; forge_mode=studio` },
57 redirect: "manual",
58 });
59
60 beforeAll(async () => {
61 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-open-change-"));
62 app = createApp({ dataDir, port: 0 });
63 servers = await serve(app);
64 base = servers.url;
65 const a = await createUser(app.db, { origin: "seed", trusted: true,
66 username: "alice",
67 email: "[email protected]",
68 password: "a-good-passphrase",
69 adminIfFirst: true,
70 });
71 if (!a.ok) throw new Error(a.error);
72 alice = a.value;
73 cookie = await signInCookie(base, "alice");
74 const made = await createRepo(app, alice, {
75 name: "pad",
76 visibility: "private",
77 description: "a pad for notes",
78 });
79 if (!made.ok) throw new Error(String(made.error));
80 repo = made.value;
81 const seeded = await commitToBranch(app, alice, repo, {
82 message: "Start",
83 files: [{ path: "README.md", content: "pad\n" }],
84 });
85 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
86 const enabled = await enableApp(app, alice, repo, { slug: "pad" });
87 if (!enabled.ok) throw new Error(String(enabled.error));
88
89 // Alice opens the change herself, through the editor — no agent run
90 // anywhere near it, the exact case `latestAgentRunsForRepos` misses.
91 const branch = await commitToBranch(app, alice, repo, {
92 branch: "note",
93 base: "main",
94 message: "A note",
95 files: [{ path: "NOTE.md", content: "note\n" }],
96 });
97 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
98 const opened = await openPull(app, alice, repo, {
99 title: "A note",
100 body: "",
101 baseBranch: "main",
102 headBranch: "note",
103 });
104 if (!opened.ok) throw new Error(String(opened.error));
105 pull = opened.value;
106 const check = createRun(app.db, {
107 repoId: repo.id,
108 kind: "check",
109 issueId: pull.id,
110 env: `pull-${pull.number}`,
111 commitOid: pull.headOid,
112 reason: "pull",
113 });
114 claimRun(app.db, check.id);
115 finishRun(app.db, check.id, { state: "ok", summary: "check passed in 1s" });
116
117 // A second app, otherwise identical, with nothing open at all — the
118 // control that must keep reading "quiet".
119 const madeQuiet = await createRepo(app, alice, {
120 name: "quiet",
121 visibility: "private",
122 description: "says nothing",
123 });
124 if (!madeQuiet.ok) throw new Error(String(madeQuiet.error));
125 const seededQuiet = await commitToBranch(app, alice, madeQuiet.value, {
126 message: "Start",
127 files: [{ path: "README.md", content: "quiet\n" }],
128 });
129 if (!seededQuiet.ok) throw new Error(JSON.stringify(seededQuiet.error));
130 const enabledQuiet = await enableApp(app, alice, madeQuiet.value, {
131 slug: "quiet",
132 });
133 if (!enabledQuiet.ok) throw new Error(String(enabledQuiet.error));
134 });
135
136 afterAll(async () => {
137 servers.stop();
138 app.close();
139 await rm(dataDir, { recursive: true, force: true });
140 });
141
142 test("the shelf row carries the change's own word, its badge and dot kept", async () => {
143 const home = await (await get("/apps")).text();
144 const at = home.indexOf('href="/alice/pad"');
145 expect(at).toBeGreaterThan(-1);
146 const start = home.lastIndexOf("<li>", at);
147 const shelfRow = home.slice(start, home.indexOf("</li>", at) + 5);
148 expect(shelfRow).not.toContain("quiet — tell the crew something");
149 expect(shelfRow).toContain('data-word="look"');
150 expect(shelfRow).toContain('class="dot"');
151 expect(shelfRow).toContain('class="badge"');
152 });
153
154 test("an app with nothing open still reads quiet", async () => {
155 const home = await (await get("/apps")).text();
156 const at = home.indexOf('href="/alice/quiet"');
157 expect(at).toBeGreaterThan(-1);
158 const start = home.lastIndexOf("<li>", at);
159 const shelfRow = home.slice(start, home.indexOf("</li>", at) + 5);
160 expect(shelfRow).toContain("quiet — tell the crew something");
161 });
162 });
163
164 /**
165 * Issue #215: once a merged change closes, it drops out of
166 * `openPullsForRepos`, and before this the shelf and the rail had nothing
167 * left to say about it but "quiet" — the row's *happening* half spoke only
168 * for an agent run, never a deploy. `productionFact` (`words.ts`) reads
169 * production's own status for the fact a merge or a push leaves behind:
170 * the deploy that landed, or one that failed behind what still serves.
171 */
172 describe("a deploy that just landed is news on the front door", () => {
173 let dataDir: string;
174 let app: App;
175 let servers: Servers;
176 let base: string;
177 let runtime: AppsRuntime;
178 let repo: Repo;
179 let record: AppRecord;
180 let quietRepo: Repo;
181 let alice: User;
182 let cookie: string;
183 let deployRun: Run;
184
185 interface PreviewSlot {
186 readonly app: AppRecord;
187 readonly env: EnvName;
188 readonly chain: Promise<void>;
189 readonly restarts: number[];
190 deploying?: unknown;
191 live?: unknown;
192 lastError?: string;
193 lastRunId?: string;
194 }
195 const putEnv = (
196 appRecord: AppRecord,
197 env: EnvName,
198 slot: Omit<PreviewSlot, "app" | "env" | "chain" | "restarts">,
199 ): void => {
200 (runtime as unknown as { envs: Map<string, PreviewSlot> }).envs.set(
201 `${appRecord.id}/${env}`,
202 { app: appRecord, env, chain: Promise.resolve(), restarts: [], ...slot },
203 );
204 };
205
206 const get = (path: string) =>
207 fetch(`${base}${path}`, {
208 headers: { cookie: `${cookie}; forge_mode=studio` },
209 redirect: "manual",
210 });
211
212 const shelfRowFor = (home: string, href: string): string => {
213 const at = home.indexOf(`href="${href}"`);
214 expect(at).toBeGreaterThan(-1);
215 const start = home.lastIndexOf("<li>", at);
216 return home.slice(start, home.indexOf("</li>", at) + 5);
217 };
218
219 beforeAll(async () => {
220 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-shipped-"));
221 app = createApp({ dataDir, port: 0 });
222 servers = await serve(app);
223 base = servers.url;
224 // Uncalled `.start()`: this runtime never subscribes to the bus, so
225 // nothing here spins a real process — its `envs` are only ever what
226 // `putEnv` puts there, the same fiction the live-fragment tests use.
227 runtime = new AppsRuntime(app, {
228 domain: "shipped.test",
229 scheme: "http",
230 publicPort: servers.port,
231 forgeUrl: servers.url,
232 concurrency: 1,
233 });
234 const a = await createUser(app.db, { origin: "seed", trusted: true,
235 username: "alice",
236 email: "[email protected]",
237 password: "a-good-passphrase",
238 adminIfFirst: true,
239 });
240 if (!a.ok) throw new Error(a.error);
241 alice = a.value;
242 cookie = await signInCookie(base, "alice");
243
244 const made = await createRepo(app, alice, {
245 name: "sap",
246 visibility: "private",
247 description: "a sap for notes",
248 });
249 if (!made.ok) throw new Error(String(made.error));
250 repo = made.value;
251 const seeded = await commitToBranch(app, alice, repo, {
252 message: "Start",
253 files: [{ path: "README.md", content: "sap\n" }],
254 });
255 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
256 const enabled = await enableApp(app, alice, repo, { slug: "sap" });
257 if (!enabled.ok) throw new Error(String(enabled.error));
258 record = getAppByRepo(app.db, repo.id)!;
259
260 deployRun = createRun(app.db, {
261 repoId: repo.id,
262 kind: "deploy",
263 env: "production",
264 commitOid: "6c99218",
265 reason: "merge",
266 actorId: alice.id,
267 });
268 claimRun(app.db, deployRun.id);
269 deployRun =
270 finishRun(app.db, deployRun.id, { state: "ok", summary: "deploy" }) ??
271 deployRun;
272
273 // A second app, otherwise identical, that never shipped — the control
274 // that must keep reading quiet.
275 const madeQuiet = await createRepo(app, alice, {
276 name: "quiet2",
277 visibility: "private",
278 description: "says nothing",
279 });
280 if (!madeQuiet.ok) throw new Error(String(madeQuiet.error));
281 const seededQuiet = await commitToBranch(app, alice, madeQuiet.value, {
282 message: "Start",
283 files: [{ path: "README.md", content: "quiet\n" }],
284 });
285 if (!seededQuiet.ok) throw new Error(JSON.stringify(seededQuiet.error));
286 const enabledQuiet = await enableApp(app, alice, madeQuiet.value, {
287 slug: "quiet2",
288 });
289 if (!enabledQuiet.ok) throw new Error(String(enabledQuiet.error));
290 quietRepo = madeQuiet.value;
291 });
292
293 // Each test starts from the merge's deploy serving just now; a test that
294 // wants another production slot puts its own.
295 beforeEach(() => {
296 const servingAt = new Date().toISOString();
297 putEnv(record, "production", {
298 live: {
299 commitOid: "6c99218",
300 handle: { port: 4321 },
301 runId: deployRun.id,
302 since: servingAt,
303 servingAt,
304 createdAt: servingAt,
305 manifest: { tty: null },
306 },
307 });
308 });
309
310 afterAll(async () => {
311 servers.stop();
312 app.close();
313 await rm(dataDir, { recursive: true, force: true });
314 });
315
316 test("the shelf row reads shipped, with who and when, not quiet", async () => {
317 const home = await (await get("/apps")).text();
318 const shelfRow = shelfRowFor(home, "/alice/sap");
319 expect(shelfRow).not.toContain("quiet — tell the crew something");
320 expect(shelfRow).toContain('data-word="shipped"');
321 expect(shelfRow).toContain("alice");
322 });
323
324 test("an app with no recent deploy still reads quiet", async () => {
325 const home = await (await get("/apps")).text();
326 const shelfRow = shelfRowFor(home, `/alice/${quietRepo.name}`);
327 expect(shelfRow).toContain("quiet — tell the crew something");
328 });
329
330 test("a deploy no push asked for is not news: a redeploy or the boot reconcile does not read shipped", async () => {
331 const restartedAt = new Date().toISOString();
332 putEnv(record, "production", {
333 live: {
334 commitOid: "6c99218",
335 handle: { port: 4321 },
336 runId: deployRun.id,
337 since: restartedAt,
338 servingAt: restartedAt,
339 // No `createdAt`: nothing asked a push for this one — a redeploy
340 // someone pressed, or the boot reconcile every restart runs.
341 manifest: { tty: null },
342 },
343 });
344 const home = await (await get("/apps")).text();
345 const shelfRow = shelfRowFor(home, "/alice/sap");
346 expect(shelfRow).not.toContain('data-word="shipped"');
347 expect(shelfRow).toContain("quiet — tell the crew something");
348 });
349
350 test("a deploy older than the news window reads quiet again, not shipped forever", async () => {
351 const old = new Date(Date.now() - 25 * 60 * 60_000).toISOString();
352 putEnv(record, "production", {
353 live: {
354 commitOid: "6c99218",
355 handle: { port: 4321 },
356 runId: deployRun.id,
357 since: old,
358 servingAt: old,
359 createdAt: old,
360 manifest: { tty: null },
361 },
362 });
363 const home = await (await get("/apps")).text();
364 const shelfRow = shelfRowFor(home, "/alice/sap");
365 expect(shelfRow).not.toContain('data-word="shipped"');
366 expect(shelfRow).toContain("quiet — tell the crew something");
367 });
368 });
test/studio-ship-take-back.test.ts added +439
@@ -0,0 +1,439 @@
1 /**
2 * Shipping and taking back a change from the studio: a person's own change
3 * ships from its page, and a shipped one offers Take it back.
4 *
5 * One of the files the studio's suite was split into so no one file sets the
6 * gate's floor (#2038) or passes 800 lines (#14011); the rest are
7 * test/studio*.test.ts, and they sign in through test/helpers/studio-signin.ts.
8 */
9
10 import { signInCookie } from "./helpers/studio-signin.ts";
11 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
12 import { mkdtemp, rm } from "node:fs/promises";
13 import { tmpdir } from "node:os";
14 import { join } from "node:path";
15 import { createApp, type App } from "../src/app.ts";
16 import { enableApp } from "../src/actions/apps.ts";
17 import { mergePull, openPull } from "../src/actions/pulls.ts";
18 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
19 import { getPull, type Pull } from "../src/core/pulls.ts";
20 import { type Repo } from "../src/core/repos.ts";
21 import { claimRun, createRun, finishRun } from "../src/core/runs.ts";
22 import { createUser, type User } from "../src/core/users.ts";
23 import { serve, type Servers } from "../src/http/server.ts";
24
25 /**
26 * Issue #211: the studio's own Decide card ships a person's own change too,
27 * not only the crew's — the same `/ship` route, the same conflict wording
28 * (`change.ts:145-157` before this), offered to a writer, whoever opened it.
29 */
30 describe("a person's own change ships from its own studio page", () => {
31 let dataDir: string;
32 let app: App;
33 let servers: Servers;
34 let base: string;
35 let alice: User;
36 let repo: Repo;
37 let ownerCookie: string;
38 let readerCookie: string;
39
40 const signIn = async (username: string): Promise<string> =>
41 await signInCookie(base, username);
42
43 const get = (path: string, cookie: string) =>
44 fetch(`${base}${path}`, {
45 headers: { cookie: `${cookie}; forge_mode=studio` },
46 redirect: "manual",
47 });
48 const post = (path: string, cookie: string) =>
49 fetch(`${base}${path}`, {
50 method: "POST",
51 headers: { cookie, origin: base },
52 redirect: "manual",
53 });
54
55 beforeAll(async () => {
56 dataDir = await mkdtemp(join(tmpdir(), "forge-own-ship-"));
57 app = createApp({ dataDir, port: 0 });
58 servers = await serve(app);
59 base = servers.url;
60 const a = await createUser(app.db, { origin: "seed", trusted: true,
61 username: "alice",
62 email: "[email protected]",
63 password: "a-good-passphrase",
64 adminIfFirst: true,
65 });
66 if (!a.ok) throw new Error(a.error);
67 alice = a.value;
68 const r = await createUser(app.db, { origin: "seed", trusted: true,
69 username: "eve",
70 email: "[email protected]",
71 password: "a-good-passphrase",
72 });
73 if (!r.ok) throw new Error(r.error);
74 ownerCookie = await signIn("alice");
75 readerCookie = await signIn("eve");
76 const made = await createRepo(app, alice, {
77 name: "garden",
78 visibility: "public",
79 description: "grown by hand, not by the crew",
80 });
81 if (!made.ok) throw new Error(String(made.error));
82 repo = made.value;
83 const seeded = await commitToBranch(app, alice, repo, {
84 message: "Start",
85 files: [
86 {
87 path: "package.json",
88 content:
89 '{"name":"garden","scripts":{"start":"bun run server.ts"}}\n',
90 },
91 {
92 path: "server.ts",
93 content:
94 "Bun.serve({ port: Number(process.env.PORT), fetch: () => new Response('ok') });\n",
95 },
96 ],
97 });
98 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
99 const enabled = await enableApp(app, alice, repo, { slug: "garden" });
100 if (!enabled.ok) throw new Error(String(enabled.error));
101 });
102
103 afterAll(async () => {
104 servers.stop();
105 app.close();
106 await rm(dataDir, { recursive: true, force: true });
107 });
108
109 test("a green change alice opened herself offers Ship to a writer, and nothing to a reader; shipping lands it and the page reads shipped", async () => {
110 const branch = await commitToBranch(app, alice, repo, {
111 branch: "feature/bench",
112 base: "main",
113 message: "Add a bench",
114 files: [{ path: "BENCH.md", content: "a place to sit\n" }],
115 });
116 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
117 const opened = await openPull(app, alice, repo, {
118 title: "Add a bench",
119 body: "For sitting.",
120 baseBranch: "main",
121 headBranch: "feature/bench",
122 });
123 if (!opened.ok) throw new Error(String(opened.error));
124 const pull = opened.value;
125 const run = createRun(app.db, {
126 repoId: repo.id,
127 kind: "check",
128 issueId: pull.id,
129 env: `pull-${pull.number}`,
130 commitOid: pull.headOid,
131 reason: "pull",
132 });
133 claimRun(app.db, run.id);
134 finishRun(app.db, run.id, { state: "ok", summary: "check passed in 1s" });
135
136 const readerPage = await (
137 await get(`/alice/garden/pulls/${pull.number}`, readerCookie)
138 ).text();
139 expect(readerPage).toContain('data-word="look"');
140 expect(readerPage).not.toContain(`id="decide"`);
141 expect(readerPage).not.toContain("Ship");
142
143 const ownerPage = await (
144 await get(`/alice/garden/pulls/${pull.number}`, ownerCookie)
145 ).text();
146 expect(ownerPage).toContain(`id="decide"`);
147 expect(ownerPage).toContain("Ship");
148 // The words a person's own green change is offered in are `asks.ts`'s
149 // (#224: a green change asks to be looked at whoever opened it, and the
150 // card says so without pretending somebody finished it for them). The
151 // `own` fallback in `studio/change.ts` — "The check is green." — is what
152 // an autonomous repository's own pull still gets, where the asks panel
153 // offers nothing.
154 expect(ownerPage).toContain(
155 "is green — ready for a look. Ship it, or say what is off.",
156 );
157
158 const shipped = await post(
159 `/alice/garden/pulls/${pull.number}/ship`,
160 ownerCookie,
161 );
162 expect(shipped.status).toBe(303);
163 expect(getPull(app.db, repo.id, pull.number)?.mergedAt).not.toBeNull();
164 const after = await (
165 await get(`/alice/garden/pulls/${pull.number}`, ownerCookie)
166 ).text();
167 expect(after).toContain('data-word="shipped"');
168 });
169
170 test("a conflicted change alice opened herself offers the bring-up-to-date wording, never a Ship that would be refused", async () => {
171 const base1 = await commitToBranch(app, alice, repo, {
172 message: "Add a file two branches will fight over",
173 files: [{ path: "CONFLICT.md", content: "base\nline\n" }],
174 });
175 if (!base1.ok) throw new Error(JSON.stringify(base1.error));
176 const branch = await commitToBranch(app, alice, repo, {
177 branch: "feature/own-conflict",
178 base: "main",
179 message: "Alice's edit",
180 files: [{ path: "CONFLICT.md", content: "base\nalice\n" }],
181 });
182 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
183 const onMain = await commitToBranch(app, alice, repo, {
184 message: "Main's edit",
185 files: [{ path: "CONFLICT.md", content: "base\nmain\n" }],
186 });
187 if (!onMain.ok) throw new Error(JSON.stringify(onMain.error));
188
189 const opened = await openPull(app, alice, repo, {
190 title: "Alice's conflicting change",
191 body: "This will not merge cleanly.",
192 baseBranch: "main",
193 headBranch: "feature/own-conflict",
194 });
195 if (!opened.ok) throw new Error(String(opened.error));
196 const pull = opened.value;
197 const run = createRun(app.db, {
198 repoId: repo.id,
199 kind: "check",
200 issueId: pull.id,
201 env: `pull-${pull.number}`,
202 commitOid: pull.headOid,
203 reason: "pull",
204 });
205 claimRun(app.db, run.id);
206 finishRun(app.db, run.id, { state: "ok", summary: "check passed in 1s" });
207
208 const studio = await (
209 await get(`/alice/garden/pulls/${pull.number}`, ownerCookie)
210 ).text();
211 expect(studio).toContain(`id="decide"`);
212 expect(studio).toContain("conflicts with main");
213 expect(studio).toContain("bring the branch up to date");
214 expect(studio).toContain("Bring up to date");
215 expect(studio).not.toContain("Ship");
216 });
217 });
218
219 /**
220 * Issue #190: a shipped change's studio page offers Take it back to a
221 * writer, behind the studio's existing confirm, and pressing it opens a new
222 * change that undoes it.
223 */
224 describe("take it back: the studio's verb on a shipped change", () => {
225 let dataDir: string;
226 let app: App;
227 let servers: Servers;
228 let base: string;
229 let alice: User;
230 let repo: Repo;
231 let ownerCookie: string;
232 let readerCookie: string;
233
234 const signIn = async (username: string): Promise<string> =>
235 await signInCookie(base, username);
236
237 const get = (path: string, cookie: string) =>
238 fetch(`${base}${path}`, {
239 headers: { cookie: `${cookie}; forge_mode=studio` },
240 redirect: "manual",
241 });
242 const post = (path: string, cookie: string, origin = base) =>
243 fetch(`${base}${path}`, {
244 method: "POST",
245 headers: { cookie, origin },
246 redirect: "manual",
247 });
248
249 const shipChange = async (
250 branch: string,
251 title: string,
252 content: string,
253 ): Promise<Pull> => {
254 const committed = await commitToBranch(app, alice, repo, {
255 branch,
256 base: "main",
257 message: title,
258 files: [{ path: "code.txt", content }],
259 });
260 if (!committed.ok) throw new Error(JSON.stringify(committed.error));
261 const opened = await openPull(app, alice, repo, {
262 title,
263 baseBranch: "main",
264 headBranch: branch,
265 });
266 if (!opened.ok) throw new Error(String(opened.error));
267 const merged = await mergePull(app, alice, repo, opened.value, "squash");
268 if (!merged.ok) throw new Error(JSON.stringify(merged.error));
269 return getPull(app.db, repo.id, opened.value.number)!;
270 };
271
272 beforeAll(async () => {
273 dataDir = await mkdtemp(join(tmpdir(), "forge-take-back-"));
274 app = createApp({ dataDir, port: 0 });
275 servers = await serve(app);
276 base = servers.url;
277 const a = await createUser(app.db, { origin: "seed", trusted: true,
278 username: "alice",
279 email: "[email protected]",
280 password: "a-good-passphrase",
281 adminIfFirst: true,
282 });
283 if (!a.ok) throw new Error(a.error);
284 alice = a.value;
285 const r = await createUser(app.db, { origin: "seed", trusted: true,
286 username: "eve",
287 email: "[email protected]",
288 password: "a-good-passphrase",
289 });
290 if (!r.ok) throw new Error(r.error);
291 ownerCookie = await signIn("alice");
292 readerCookie = await signIn("eve");
293 const made = await createRepo(app, alice, {
294 name: "orchard",
295 visibility: "public",
296 });
297 if (!made.ok) throw new Error(String(made.error));
298 repo = made.value;
299 const seeded = await commitToBranch(app, alice, repo, {
300 message: "Start",
301 files: [
302 {
303 path: "package.json",
304 content:
305 '{"name":"orchard","scripts":{"start":"bun run server.ts"}}\n',
306 },
307 {
308 path: "server.ts",
309 content:
310 "Bun.serve({ port: Number(process.env.PORT), fetch: () => new Response('ok') });\n",
311 },
312 { path: "code.txt", content: "alpha\n" },
313 ],
314 });
315 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
316 const enabled = await enableApp(app, alice, repo, { slug: "orchard" });
317 if (!enabled.ok) throw new Error(String(enabled.error));
318 });
319
320 afterAll(async () => {
321 servers.stop();
322 app.close();
323 await rm(dataDir, { recursive: true, force: true });
324 });
325
326 test("a shipped change offers Take it back to a writer, not to a reader, and not while it is still open", async () => {
327 const branch = await commitToBranch(app, alice, repo, {
328 branch: "feature/loud",
329 base: "main",
330 message: "Make it loud",
331 files: [{ path: "code.txt", content: "alpha\nBETA\n" }],
332 });
333 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
334 const opened = await openPull(app, alice, repo, {
335 title: "Make it loud",
336 baseBranch: "main",
337 headBranch: "feature/loud",
338 });
339 if (!opened.ok) throw new Error(String(opened.error));
340 const pull = opened.value;
341
342 const whileOpen = await (
343 await get(`/alice/orchard/pulls/${pull.number}`, ownerCookie)
344 ).text();
345 expect(whileOpen).not.toContain("Take it back");
346
347 const merged = await mergePull(app, alice, repo, pull, "squash");
348 if (!merged.ok) throw new Error(JSON.stringify(merged.error));
349
350 const ownerPage = await (
351 await get(`/alice/orchard/pulls/${pull.number}`, ownerCookie)
352 ).text();
353 expect(ownerPage).toContain("Take it back");
354 expect(ownerPage).toContain(
355 `/-/confirm?to=%2Falice%2Forchard%2Fpulls%2F${pull.number}%2Ftake-back`,
356 );
357
358 const readerPage = await (
359 await get(`/alice/orchard/pulls/${pull.number}`, readerCookie)
360 ).text();
361 expect(readerPage).not.toContain("Take it back");
362
363 const confirm = await (
364 await get(
365 `/-/confirm?to=${encodeURIComponent(`/alice/orchard/pulls/${pull.number}/take-back`)}`,
366 ownerCookie,
367 )
368 ).text();
369 expect(confirm).toContain("Take back");
370 expect(confirm).toContain("Make it loud");
371 expect(confirm).toContain(
372 "opens a new change that undoes exactly what it shipped",
373 );
374 });
375
376 test("pressing Take it back opens a new change and lands on its studio page", async () => {
377 const shipped = await shipChange(
378 "feature/gamma",
379 "Add gamma",
380 "alpha\nGAMMA\n",
381 );
382
383 const taken = await post(
384 `/alice/orchard/pulls/${shipped.number}/take-back`,
385 ownerCookie,
386 );
387 expect(taken.status).toBe(303);
388 const location = taken.headers.get("location")!;
389 expect(location).toBe(`/alice/orchard/pulls/${shipped.number + 1}`);
390
391 const revertPage = await (await get(location, ownerCookie)).text();
392 expect(revertPage).toContain(`Revert &quot;Add gamma&quot;`);
393 });
394
395 test("the POST is refused from another origin, and nothing is created", async () => {
396 const shipped = await shipChange(
397 "feature/delta",
398 "Add delta",
399 "alpha\nDELTA\n",
400 );
401
402 const forged = await post(
403 `/alice/orchard/pulls/${shipped.number}/take-back`,
404 ownerCookie,
405 "https://evil.example",
406 );
407 expect(forged.status).toBe(403);
408 expect(getPull(app.db, repo.id, shipped.number + 1)).toBeUndefined();
409 });
410
411 test("a refusal is said in the studio's own words, and takes nothing back twice", async () => {
412 const shipped = await shipChange(
413 "feature/epsilon",
414 "Add epsilon",
415 "alpha\nEPSILON\n",
416 );
417 const first = await post(
418 `/alice/orchard/pulls/${shipped.number}/take-back`,
419 ownerCookie,
420 );
421 expect(first.status).toBe(303);
422
423 const again = await post(
424 `/alice/orchard/pulls/${shipped.number}/take-back`,
425 ownerCookie,
426 );
427 expect(again.status).toBe(303);
428 const location = again.headers.get("location")!;
429 expect(location).toBe(`/alice/orchard/pulls/${shipped.number}`);
430 const flash = decodeURIComponent(again.headers.get("set-cookie") ?? "");
431 expect(flash).toContain("already been taken back");
432
433 // Only one revert pull exists — the second attempt created nothing.
434 expect(getPull(app.db, repo.id, shipped.number + 1)?.title).toBe(
435 'Revert "Add epsilon"',
436 );
437 expect(getPull(app.db, repo.id, shipped.number + 2)).toBeUndefined();
438 });
439 });
test/studio-shipping.test.ts deleted −2191
@@ -1,2191 +0,0 @@
1 /**
2 * The studio: what a stranger sees, shipping and taking back a change, the
3 * crew panel, and the week's counts. One of the files the studio's suite
4 * was split into so no one file sets the gate's floor (#2038); the rest are
5 * test/studio*.test.ts.
6 */
7
8 import {
9 afterAll,
10 beforeAll,
11 beforeEach,
12 describe,
13 expect,
14 test,
15 } from "bun:test";
16 import { mkdtemp, rm } from "node:fs/promises";
17 import { tmpdir } from "node:os";
18 import { join } from "node:path";
19 import { createApp, type App } from "../src/app.ts";
20 import { enableApp } from "../src/actions/apps.ts";
21 import type { EnvStatus } from "../src/apps/runtime.ts";
22 import { addToCrew, createAgent } from "../src/actions/crew.ts";
23 import { addTeamMember, createOrg, createTeam } from "../src/actions/orgs.ts";
24 import { openIssue } from "../src/actions/issues.ts";
25 import { mergePull, openPull } from "../src/actions/pulls.ts";
26 import {
27 commitToBranch,
28 createRepo,
29 updateRepoSettings,
30 } from "../src/actions/repos.ts";
31 import { recordDecision } from "../src/core/decisions.ts";
32 import { getPull, mergedPullCount, type Pull } from "../src/core/pulls.ts";
33 import { getRepo, type Repo } from "../src/core/repos.ts";
34 import {
35 agentRunTotals,
36 claimRun,
37 createRun,
38 finishRun,
39 markServing,
40 previewLatency,
41 STOPPED_SUMMARY,
42 type Run,
43 } from "../src/core/runs.ts";
44 import { createUser, getUserByName, type User } from "../src/core/users.ts";
45 import { serve, type Servers } from "../src/http/server.ts";
46 import { NOBODY_ON_CREW } from "../src/web/studio/words.ts";
47 import { servedPhrase } from "../src/web/ui.ts";
48 import { ageApp } from "./helpers/history.ts";
49
50 describe("a stranger sees the app, not the owner's books", () => {
51 let dataDir: string;
52 let app: App;
53 let servers: Servers;
54 let base: string;
55 let repo: Repo;
56 let alice: User;
57 let ownerCookie: string;
58 let strangerCookie: string;
59 let run: Run;
60
61 const signIn = async (username: string): Promise<string> =>
62 (
63 (
64 await fetch(`${base}/signin`, {
65 method: "POST",
66 body: new URLSearchParams({
67 username,
68 password: "a-good-passphrase",
69 }),
70 redirect: "manual",
71 })
72 ).headers.get("set-cookie") ?? ""
73 ).split(";")[0]!;
74
75 const get = (path: string, cookie?: string) =>
76 fetch(`${base}${path}`, {
77 headers: {
78 cookie:
79 cookie === undefined
80 ? "forge_mode=studio"
81 : `${cookie}; forge_mode=studio`,
82 },
83 redirect: "manual",
84 });
85
86 beforeAll(async () => {
87 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-public-"));
88 app = createApp({ dataDir, port: 0 });
89 servers = await serve(app);
90 base = servers.url;
91 const a = await createUser(app.db, { origin: "seed", trusted: true,
92 username: "alice",
93 email: "[email protected]",
94 password: "a-good-passphrase",
95 adminIfFirst: true,
96 });
97 if (!a.ok) throw new Error(a.error);
98 alice = a.value;
99 const c = await createUser(app.db, { origin: "seed", trusted: true,
100 username: "carol",
101 email: "[email protected]",
102 password: "a-good-passphrase",
103 });
104 if (!c.ok) throw new Error(c.error);
105 ownerCookie = await signIn("alice");
106 strangerCookie = await signIn("carol");
107 const made = await createRepo(app, alice, {
108 name: "beacon",
109 visibility: "public",
110 description: "a light that stays on",
111 });
112 if (!made.ok) throw new Error(String(made.error));
113 repo = made.value;
114 const seeded = await commitToBranch(app, alice, repo, {
115 message: "Start",
116 files: [{ path: "README.md", content: "beacon\n" }],
117 });
118 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
119 const enabled = await enableApp(app, alice, repo, { slug: "beacon" });
120 if (!enabled.ok) throw new Error(String(enabled.error));
121 const scout = await createAgent(app, alice, { username: "keeper" });
122 if (!scout.ok) throw new Error(String(scout.error));
123 expect(addToCrew(app, alice, repo, "keeper").ok).toBe(true);
124 const on = await updateRepoSettings(app, alice, repo, {
125 autonomy: "assisted",
126 autonomyDirection: "",
127 autonomySlots: 2,
128 autonomyBy: alice.id,
129 });
130 expect(on.ok).toBe(true);
131
132 const tell = openIssue(app, alice, repo, { title: "Trim the wick" });
133 if (!tell.ok) throw new Error(String(tell.error));
134 const branch = await commitToBranch(app, alice, repo, {
135 branch: "keeper/trim-the-wick",
136 base: "main",
137 message: "Trim the wick",
138 files: [{ path: "WICK.md", content: "trim\n" }],
139 });
140 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
141 const opened = await openPull(app, alice, repo, {
142 title: "Trim the wick",
143 body: `Closes #${tell.value.number}`,
144 baseBranch: "main",
145 headBranch: "keeper/trim-the-wick",
146 });
147 if (!opened.ok) throw new Error(String(opened.error));
148 const keeperId = getUserByName(app.db, "keeper")!.id;
149 run = createRun(app.db, {
150 repoId: repo.id,
151 kind: "agent",
152 issueId: tell.value.id,
153 branch: "keeper/trim-the-wick",
154 actorId: keeperId,
155 reason: "assigned",
156 });
157 claimRun(app.db, run.id);
158 run =
159 finishRun(app.db, run.id, {
160 state: "ok",
161 summary: `opened pull #${opened.value.number}`,
162 costUsd: 42.5,
163 turns: 7,
164 }) ?? run;
165 const merged = await mergePull(app, alice, repo, opened.value, "squash");
166 if (!merged.ok) throw new Error(String(merged.error));
167 recordDecision(app.db, {
168 kind: "land",
169 repoId: repo.id,
170 issueId: tell.value.id,
171 state: { card: "ask", head: opened.value.headOid },
172 answer: "ship",
173 p: 0.9,
174 by: "rule",
175 ms: 3,
176 });
177
178 // Three served deploys this week, median 4.0s, and a fourth that never
179 // answered — excluded, not a zero that would drag the median down.
180 const now = Date.now();
181 for (const [offsetMs, durationMs] of [
182 [0, 2_000],
183 [1_000, 4_000],
184 [2_000, 6_000],
185 [3_000, null],
186 ] as const) {
187 const deploy = createRun(app.db, {
188 repoId: repo.id,
189 kind: "deploy",
190 env: "production",
191 commitOid: "cafefeed",
192 reason: "push",
193 });
194 app.db
195 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
196 .run(deploy.id, new Date(now + offsetMs).toISOString());
197 if (durationMs !== null) {
198 app.db
199 .query("UPDATE run SET serving_at = ?2 WHERE id = ?1")
200 .run(deploy.id, new Date(now + offsetMs + durationMs).toISOString());
201 }
202 }
203 });
204
205 afterAll(async () => {
206 servers.stop();
207 app.close();
208 await rm(dataDir, { recursive: true, force: true });
209 });
210
211 test("anonymous and read-only visitors get the app, not the owner's numbers; the owner still gets everything", async () => {
212 const anon = await (await get("/alice/beacon")).text();
213 const stranger = await (await get("/alice/beacon", strangerCookie)).text();
214 const owner = await (await get("/alice/beacon", ownerCookie)).text();
215
216 for (const page of [anon, stranger]) {
217 expect(page).not.toMatch(/\$\d/);
218 expect(page).not.toContain("keeper");
219 expect(page).not.toContain(">Decided<");
220 expect(page).not.toContain(">On its own<");
221 expect(page).not.toContain("/fleet/graph");
222 expect(page).not.toContain(">Slots<");
223 expect(page).not.toContain("Nobody yet");
224 // The app itself is still there: what it is, that it shipped, a way in.
225 expect(page).toContain("a light that stays on");
226 expect(page).toContain(">Shipped<");
227 expect(page).toContain("1 pull request");
228 expect(page).toContain("Open the app");
229 // The direction's own number is the same owner's-books fact as spend
230 // and the ledger: a stranger and an anonymous visitor see neither.
231 expect(page).not.toContain(">Preview<");
232 expect(page).not.toContain("reaches its preview");
233 }
234
235 expect(owner).toContain("$42.50");
236 expect(owner).toContain("keeper");
237 expect(owner).toContain(">On its own<");
238 // One click from the app's spend to the fleet graph, scoped to this app (#578).
239 expect(owner).toContain('href="/fleet/graph?repo=alice%2Fbeacon"');
240 expect(owner).toContain(">Decided<");
241 // Three served deploys this week — 2.0s, 4.0s and 6.0s — median 4.0s;
242 // the fourth, never served, does not drag it toward zero.
243 expect(owner).toContain("reaches its preview in 4.0s");
244 expect(owner).toContain("· 3 deploys");
245 });
246
247 test("the app's live fragment carries no owner fact either", async () => {
248 const fragment = await (await get("/alice/beacon/app/state")).text();
249 expect(fragment).not.toMatch(/\$\d/);
250 expect(fragment).not.toContain("keeper");
251 });
252
253 test("a run's own page hides cost, turns and the crew's name from a stranger and from an anonymous reader; the JSON API redacts the run the same way", async () => {
254 const anonPage = await (await get(`/alice/beacon/runs/${run.id}`)).text();
255 const strangerPage = await (
256 await get(`/alice/beacon/runs/${run.id}`, strangerCookie)
257 ).text();
258 for (const page of [anonPage, strangerPage]) {
259 expect(page).not.toContain("$42.50");
260 expect(page).not.toContain("7 turns");
261 expect(page).not.toContain("keeper");
262 // The log box, which carried data-who, is not drawn for them (#1075).
263 expect(page).toContain("<title>the crew on #");
264 expect(page).not.toContain('id="run-log"');
265 }
266 const ownerPage = await (
267 await get(`/alice/beacon/runs/${run.id}`, ownerCookie)
268 ).text();
269 expect(ownerPage).toContain("$42.50");
270 expect(ownerPage).toContain("7 turns");
271 expect(ownerPage).toContain('data-who="keeper"');
272
273 const asStranger = (await (
274 await get(`/api/v1/repos/alice/beacon/runs/${run.id}`, strangerCookie)
275 ).json()) as { costUsd: number; turns: number };
276 expect(asStranger.costUsd).toBe(0);
277 expect(asStranger.turns).toBe(0);
278 const asOwner = (await (
279 await get(`/api/v1/repos/alice/beacon/runs/${run.id}`, ownerCookie)
280 ).json()) as { costUsd: number; turns: number };
281 expect(asOwner.costUsd).toBe(42.5);
282 expect(asOwner.turns).toBe(7);
283
284 const listAsStranger = (await (
285 await get(`/api/v1/repos/alice/beacon/runs`, strangerCookie)
286 ).json()) as { items: { costUsd: number }[] };
287 expect(listAsStranger.items.every((r) => r.costUsd === 0)).toBe(true);
288
289 // The log is the writers' and crew's (#1075): a stranger's tail is
290 // refused, and carries neither the cost nor the transcript.
291 const tailAsStranger = await get(
292 `/api/v1/repos/alice/beacon/runs/${run.id}/log?follow=1`,
293 strangerCookie,
294 );
295 expect(tailAsStranger.status).toBe(404);
296 expect(await tailAsStranger.text()).not.toContain("42.5");
297 const tailAsOwner = await (
298 await get(
299 `/api/v1/repos/alice/beacon/runs/${run.id}/log?follow=1`,
300 ownerCookie,
301 )
302 ).text();
303 expect(tailAsOwner).toContain("42.5");
304 });
305
306 test("the studio home names the crew for the owner, but only 'the crew' for a signed-in reader of the same public app", async () => {
307 const asStranger = await (await get("/apps", strangerCookie)).text();
308 expect(asStranger.toLowerCase()).not.toContain("keeper");
309 expect(asStranger).toContain("beacon");
310 const asOwner = await (await get("/apps", ownerCookie)).text();
311 expect(asOwner.toLowerCase()).toContain("keeper");
312 expect(asOwner).toContain("beacon");
313 });
314
315 const post = (path: string, body: Record<string, string>, cookie: string) =>
316 fetch(`${base}${path}`, {
317 method: "POST",
318 headers: {
319 cookie: `${cookie}; forge_mode=studio`,
320 origin: base,
321 },
322 body: new URLSearchParams(body),
323 redirect: "manual",
324 });
325
326 test("an autonomous app with nobody on its crew says so beside the mode, and stops saying it once a builder joins", async () => {
327 const made = await createRepo(app, alice, { name: "lamp", visibility: "public" });
328 if (!made.ok) throw new Error(String(made.error));
329 const lamp = made.value;
330 const seeded = await commitToBranch(app, alice, lamp, {
331 message: "Start",
332 files: [{ path: "README.md", content: "lamp\n" }],
333 });
334 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
335 const enabled = await enableApp(app, alice, lamp, { slug: "lamp" });
336 if (!enabled.ok) throw new Error(String(enabled.error));
337 const on = await updateRepoSettings(app, alice, lamp, {
338 autonomy: "autonomous",
339 autonomyDirection: "",
340 autonomySlots: 1,
341 autonomyBy: alice.id,
342 });
343 expect(on.ok).toBe(true);
344
345 // An older app: a new one shows only its progress (#10299).
346 ageApp(app.db, lamp.id);
347 const empty = await (await get("/alice/lamp", ownerCookie)).text();
348 expect(empty).toContain("By itself");
349 expect(empty).not.toContain("Autonomous");
350 expect(empty).toContain("nobody on the crew");
351 expect(empty).toContain('href="/alice/lamp/settings">Put someone on the crew');
352 // The shelf's stuck word and this card share one phrase.
353 expect(NOBODY_ON_CREW).toBe("nobody on the crew");
354
355 const crewed = await (await get("/alice/beacon", ownerCookie)).text();
356 expect(crewed).not.toContain("nobody on the crew");
357
358 const agent = await createAgent(app, alice, { username: "wick" });
359 if (!agent.ok) throw new Error(String(agent.error));
360 expect(addToCrew(app, alice, lamp, "wick").ok).toBe(true);
361 const joined = await (await get("/alice/lamp", ownerCookie)).text();
362 expect(joined).not.toContain("nobody on the crew");
363 });
364
365 test("the studio's settings page renders the crew and the week's budget for a writer, with no dollar ceiling field, with the same numbers the app page shows", async () => {
366 const owner = await (
367 await get("/alice/beacon/settings", ownerCookie)
368 ).text();
369 expect(owner).toContain("keeper");
370 // The crew card itself: the account menu's Crew row, which once answered for it, is gone.
371 expect(owner).toContain('id="crew"');
372 expect(owner).toContain(">On its own<");
373 expect(owner).toMatch(/<dt>Today<\/dt>\s*<dd>\$42\.50 spent<\/dd>/);
374 // The week as a share and a pace; no daily dollar ceiling to read or to set (#10623).
375 expect(owner).toMatch(/<dt>Week<\/dt>\s*<dd>/);
376 expect(owner).not.toMatch(/name="ceiling"/);
377 expect(owner).not.toContain("Ceiling per day");
378 // The crew card says the governor's reason in one line (#7644).
379 expect(owner).toMatch(/<dt>Governor<\/dt>\s*<dd>Builders \d+: /);
380 expect(owner).toContain("Remove");
381 expect(owner).toContain("Save");
382 // The rest is beneath, on the same page and in the same frame.
383 expect(owner).toContain("<h2>Branch and gates</h2>");
384 expect(owner.indexOf(">On its own<")).toBeLessThan(
385 owner.indexOf("<h2>Branch and gates</h2>"),
386 );
387 expect(owner).toContain('href="/alice/beacon/settings/labels"');
388 expect(owner).not.toContain("?mode=dev");
389 });
390
391 test("a reader without write sees no crew and no controls on the settings page; one who cannot read gets not-found", async () => {
392 const stranger = await (
393 await get("/alice/beacon/settings", strangerCookie)
394 ).text();
395 expect(stranger).not.toContain("keeper");
396 expect(stranger).not.toMatch(/\$\d/);
397 expect(stranger).not.toContain("Add to crew");
398 expect(stranger).not.toContain("<select");
399
400 const anon = await get("/alice/beacon/settings");
401 expect(anon.status).toBe(200);
402 const anonText = await anon.text();
403 expect(anonText).not.toContain("keeper");
404
405 const noSuchRepo = await get("/alice/no-such-app/settings", strangerCookie);
406 expect(noSuchRepo.status).toBe(404);
407 });
408
409 test("a reader's post to the crew action is refused, exactly as dev's page refuses it", async () => {
410 const refused = await post(
411 "/alice/beacon/settings/crew",
412 { agent: "keeper" },
413 strangerCookie,
414 );
415 expect(refused.status).toBe(404);
416 });
417
418 test("each settings card saves only its own fields: About never turns a gate or the outsiders switch off, and Branch and gates never touches the words", async () => {
419 const before = getRepo(app.db, "alice", "beacon")!;
420 const on = await post(
421 "/alice/beacon/settings",
422 {
423 card: "gates",
424 defaultBranch: before.defaultBranch,
425 protect: "on",
426 requireReview: "on",
427 // The settings page sends this marker beside the switch; without it a
428 // save leaves the switch as it is (#822).
429 crewOutsidersShown: "1",
430 crewOutsiders: "on",
431 },
432 ownerCookie,
433 );
434 expect(on.status).toBe(303);
435 let now = getRepo(app.db, "alice", "beacon")!;
436 expect(now.crewOutsiders).toBe(true);
437 expect(now.description).toBe(before.description);
438
439 const about = await post(
440 "/alice/beacon/settings",
441 { card: "about", description: "a lamp", visibility: now.visibility },
442 ownerCookie,
443 );
444 expect(about.status).toBe(303);
445 now = getRepo(app.db, "alice", "beacon")!;
446 expect(now.description).toBe("a lamp");
447 expect(now.crewOutsiders).toBe(true);
448 expect(now.protectDefault).toBe(true);
449 expect(now.requireReview).toBe(true);
450
451 // Put it back for the tests that follow.
452 await post(
453 "/alice/beacon/settings",
454 {
455 card: "gates",
456 defaultBranch: before.defaultBranch,
457 crewOutsidersShown: "1",
458 },
459 ownerCookie,
460 );
461 await post(
462 "/alice/beacon/settings",
463 { card: "about", description: before.description, visibility: before.visibility },
464 ownerCookie,
465 );
466 });
467
468 test("a role change made from the studio settings page lands exactly as dev's page does", async () => {
469 const roleChange = await post(
470 "/alice/beacon/settings/crew/keeper/role",
471 { role: "reviewer" },
472 ownerCookie,
473 );
474 expect(roleChange.status).toBe(303);
475 expect(roleChange.headers.get("location")).toBe(
476 "/alice/beacon/settings#crew",
477 );
478 const after = await (
479 await get("/alice/beacon/settings", ownerCookie)
480 ).text();
481 // Keeper declares only "builder", so the repository cannot widen it to
482 // reviewer: it is drawn once, under the seat it holds — the team lists
483 // are the crew, there is no second Crew card listing it again.
484 const keeperRows = after.match(/<li[^>]*data-member="keeper"[^>]*>/g) ?? [];
485 expect(keeperRows).toEqual(['<li data-member="keeper" data-seat="builder">']);
486 expect(after).not.toContain("<h2>Crew</h2>");
487 });
488
489 test("?mode=dev on the settings route is the same one page; dev's Crew and Autonomy addresses land on its cards", async () => {
490 const dev = await (
491 await get("/alice/beacon/settings", ownerCookie)
492 ).text();
493 expect(dev).toContain(">General<");
494 expect(dev).toContain('id="crew"');
495 expect(dev).toContain("Description");
496 expect(dev).toContain("<h2>Branch and gates</h2>");
497 expect(dev).toContain('id="f-direction"');
498
499 for (const [old, to] of [
500 ["/alice/beacon/settings/crew", "/alice/beacon/settings#crew"],
501 ["/alice/beacon/settings/autonomy", "/alice/beacon/settings#autonomy"],
502 ]) {
503 const res = await get(old!, ownerCookie);
504 expect(res.status).toBe(303);
505 expect(res.headers.get("location")).toBe(to!);
506 }
507 // A reader who cannot see the repository gets not-found, not a hint.
508 const hidden = await get("/alice/no-such-app/settings/crew", ownerCookie);
509 expect(hidden.status).toBe(404);
510 });
511 });
512
513 /**
514 * Issue #211: the studio's own Decide card ships a person's own change too,
515 * not only the crew's — the same `/ship` route, the same conflict wording
516 * (`change.ts:145-157` before this), offered to a writer, whoever opened it.
517 */
518 describe("a person's own change ships from its own studio page", () => {
519 let dataDir: string;
520 let app: App;
521 let servers: Servers;
522 let base: string;
523 let alice: User;
524 let repo: Repo;
525 let ownerCookie: string;
526 let readerCookie: string;
527
528 const signIn = async (username: string): Promise<string> =>
529 (
530 (
531 await fetch(`${base}/signin`, {
532 method: "POST",
533 body: new URLSearchParams({
534 username,
535 password: "a-good-passphrase",
536 }),
537 redirect: "manual",
538 })
539 ).headers.get("set-cookie") ?? ""
540 ).split(";")[0]!;
541
542 const get = (path: string, cookie: string) =>
543 fetch(`${base}${path}`, {
544 headers: { cookie: `${cookie}; forge_mode=studio` },
545 redirect: "manual",
546 });
547 const post = (path: string, cookie: string) =>
548 fetch(`${base}${path}`, {
549 method: "POST",
550 headers: { cookie, origin: base },
551 redirect: "manual",
552 });
553
554 beforeAll(async () => {
555 dataDir = await mkdtemp(join(tmpdir(), "forge-own-ship-"));
556 app = createApp({ dataDir, port: 0 });
557 servers = await serve(app);
558 base = servers.url;
559 const a = await createUser(app.db, { origin: "seed", trusted: true,
560 username: "alice",
561 email: "[email protected]",
562 password: "a-good-passphrase",
563 adminIfFirst: true,
564 });
565 if (!a.ok) throw new Error(a.error);
566 alice = a.value;
567 const r = await createUser(app.db, { origin: "seed", trusted: true,
568 username: "eve",
569 email: "[email protected]",
570 password: "a-good-passphrase",
571 });
572 if (!r.ok) throw new Error(r.error);
573 ownerCookie = await signIn("alice");
574 readerCookie = await signIn("eve");
575 const made = await createRepo(app, alice, {
576 name: "garden",
577 visibility: "public",
578 description: "grown by hand, not by the crew",
579 });
580 if (!made.ok) throw new Error(String(made.error));
581 repo = made.value;
582 const seeded = await commitToBranch(app, alice, repo, {
583 message: "Start",
584 files: [
585 {
586 path: "package.json",
587 content:
588 '{"name":"garden","scripts":{"start":"bun run server.ts"}}\n',
589 },
590 {
591 path: "server.ts",
592 content:
593 "Bun.serve({ port: Number(process.env.PORT), fetch: () => new Response('ok') });\n",
594 },
595 ],
596 });
597 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
598 const enabled = await enableApp(app, alice, repo, { slug: "garden" });
599 if (!enabled.ok) throw new Error(String(enabled.error));
600 });
601
602 afterAll(async () => {
603 servers.stop();
604 app.close();
605 await rm(dataDir, { recursive: true, force: true });
606 });
607
608 test("a green change alice opened herself offers Ship to a writer, and nothing to a reader; shipping lands it and the page reads shipped", async () => {
609 const branch = await commitToBranch(app, alice, repo, {
610 branch: "feature/bench",
611 base: "main",
612 message: "Add a bench",
613 files: [{ path: "BENCH.md", content: "a place to sit\n" }],
614 });
615 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
616 const opened = await openPull(app, alice, repo, {
617 title: "Add a bench",
618 body: "For sitting.",
619 baseBranch: "main",
620 headBranch: "feature/bench",
621 });
622 if (!opened.ok) throw new Error(String(opened.error));
623 const pull = opened.value;
624 const run = createRun(app.db, {
625 repoId: repo.id,
626 kind: "check",
627 issueId: pull.id,
628 env: `pull-${pull.number}`,
629 commitOid: pull.headOid,
630 reason: "pull",
631 });
632 claimRun(app.db, run.id);
633 finishRun(app.db, run.id, { state: "ok", summary: "check passed in 1s" });
634
635 const readerPage = await (
636 await get(`/alice/garden/pulls/${pull.number}`, readerCookie)
637 ).text();
638 expect(readerPage).toContain('data-word="look"');
639 expect(readerPage).not.toContain(`id="decide"`);
640 expect(readerPage).not.toContain("Ship");
641
642 const ownerPage = await (
643 await get(`/alice/garden/pulls/${pull.number}`, ownerCookie)
644 ).text();
645 expect(ownerPage).toContain(`id="decide"`);
646 expect(ownerPage).toContain("Ship");
647 // The words a person's own green change is offered in are `asks.ts`'s
648 // (#224: a green change asks to be looked at whoever opened it, and the
649 // card says so without pretending somebody finished it for them). The
650 // `own` fallback in `studio/change.ts` — "The check is green." — is what
651 // an autonomous repository's own pull still gets, where the asks panel
652 // offers nothing.
653 expect(ownerPage).toContain(
654 "is green — ready for a look. Ship it, or say what is off.",
655 );
656
657 const shipped = await post(
658 `/alice/garden/pulls/${pull.number}/ship`,
659 ownerCookie,
660 );
661 expect(shipped.status).toBe(303);
662 expect(getPull(app.db, repo.id, pull.number)?.mergedAt).not.toBeNull();
663 const after = await (
664 await get(`/alice/garden/pulls/${pull.number}`, ownerCookie)
665 ).text();
666 expect(after).toContain('data-word="shipped"');
667 });
668
669 test("a conflicted change alice opened herself offers the bring-up-to-date wording, never a Ship that would be refused", async () => {
670 const base1 = await commitToBranch(app, alice, repo, {
671 message: "Add a file two branches will fight over",
672 files: [{ path: "CONFLICT.md", content: "base\nline\n" }],
673 });
674 if (!base1.ok) throw new Error(JSON.stringify(base1.error));
675 const branch = await commitToBranch(app, alice, repo, {
676 branch: "feature/own-conflict",
677 base: "main",
678 message: "Alice's edit",
679 files: [{ path: "CONFLICT.md", content: "base\nalice\n" }],
680 });
681 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
682 const onMain = await commitToBranch(app, alice, repo, {
683 message: "Main's edit",
684 files: [{ path: "CONFLICT.md", content: "base\nmain\n" }],
685 });
686 if (!onMain.ok) throw new Error(JSON.stringify(onMain.error));
687
688 const opened = await openPull(app, alice, repo, {
689 title: "Alice's conflicting change",
690 body: "This will not merge cleanly.",
691 baseBranch: "main",
692 headBranch: "feature/own-conflict",
693 });
694 if (!opened.ok) throw new Error(String(opened.error));
695 const pull = opened.value;
696 const run = createRun(app.db, {
697 repoId: repo.id,
698 kind: "check",
699 issueId: pull.id,
700 env: `pull-${pull.number}`,
701 commitOid: pull.headOid,
702 reason: "pull",
703 });
704 claimRun(app.db, run.id);
705 finishRun(app.db, run.id, { state: "ok", summary: "check passed in 1s" });
706
707 const studio = await (
708 await get(`/alice/garden/pulls/${pull.number}`, ownerCookie)
709 ).text();
710 expect(studio).toContain(`id="decide"`);
711 expect(studio).toContain("conflicts with main");
712 expect(studio).toContain("bring the branch up to date");
713 expect(studio).toContain("Bring up to date");
714 expect(studio).not.toContain("Ship");
715 });
716 });
717
718 /**
719 * Issue #190: a shipped change's studio page offers Take it back to a
720 * writer, behind the studio's existing confirm, and pressing it opens a new
721 * change that undoes it.
722 */
723 describe("take it back: the studio's verb on a shipped change", () => {
724 let dataDir: string;
725 let app: App;
726 let servers: Servers;
727 let base: string;
728 let alice: User;
729 let repo: Repo;
730 let ownerCookie: string;
731 let readerCookie: string;
732
733 const signIn = async (username: string): Promise<string> =>
734 (
735 (
736 await fetch(`${base}/signin`, {
737 method: "POST",
738 body: new URLSearchParams({
739 username,
740 password: "a-good-passphrase",
741 }),
742 redirect: "manual",
743 })
744 ).headers.get("set-cookie") ?? ""
745 ).split(";")[0]!;
746
747 const get = (path: string, cookie: string) =>
748 fetch(`${base}${path}`, {
749 headers: { cookie: `${cookie}; forge_mode=studio` },
750 redirect: "manual",
751 });
752 const post = (path: string, cookie: string, origin = base) =>
753 fetch(`${base}${path}`, {
754 method: "POST",
755 headers: { cookie, origin },
756 redirect: "manual",
757 });
758
759 const shipChange = async (
760 branch: string,
761 title: string,
762 content: string,
763 ): Promise<Pull> => {
764 const committed = await commitToBranch(app, alice, repo, {
765 branch,
766 base: "main",
767 message: title,
768 files: [{ path: "code.txt", content }],
769 });
770 if (!committed.ok) throw new Error(JSON.stringify(committed.error));
771 const opened = await openPull(app, alice, repo, {
772 title,
773 baseBranch: "main",
774 headBranch: branch,
775 });
776 if (!opened.ok) throw new Error(String(opened.error));
777 const merged = await mergePull(app, alice, repo, opened.value, "squash");
778 if (!merged.ok) throw new Error(JSON.stringify(merged.error));
779 return getPull(app.db, repo.id, opened.value.number)!;
780 };
781
782 beforeAll(async () => {
783 dataDir = await mkdtemp(join(tmpdir(), "forge-take-back-"));
784 app = createApp({ dataDir, port: 0 });
785 servers = await serve(app);
786 base = servers.url;
787 const a = await createUser(app.db, { origin: "seed", trusted: true,
788 username: "alice",
789 email: "[email protected]",
790 password: "a-good-passphrase",
791 adminIfFirst: true,
792 });
793 if (!a.ok) throw new Error(a.error);
794 alice = a.value;
795 const r = await createUser(app.db, { origin: "seed", trusted: true,
796 username: "eve",
797 email: "[email protected]",
798 password: "a-good-passphrase",
799 });
800 if (!r.ok) throw new Error(r.error);
801 ownerCookie = await signIn("alice");
802 readerCookie = await signIn("eve");
803 const made = await createRepo(app, alice, {
804 name: "orchard",
805 visibility: "public",
806 });
807 if (!made.ok) throw new Error(String(made.error));
808 repo = made.value;
809 const seeded = await commitToBranch(app, alice, repo, {
810 message: "Start",
811 files: [
812 {
813 path: "package.json",
814 content:
815 '{"name":"orchard","scripts":{"start":"bun run server.ts"}}\n',
816 },
817 {
818 path: "server.ts",
819 content:
820 "Bun.serve({ port: Number(process.env.PORT), fetch: () => new Response('ok') });\n",
821 },
822 { path: "code.txt", content: "alpha\n" },
823 ],
824 });
825 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
826 const enabled = await enableApp(app, alice, repo, { slug: "orchard" });
827 if (!enabled.ok) throw new Error(String(enabled.error));
828 });
829
830 afterAll(async () => {
831 servers.stop();
832 app.close();
833 await rm(dataDir, { recursive: true, force: true });
834 });
835
836 test("a shipped change offers Take it back to a writer, not to a reader, and not while it is still open", async () => {
837 const branch = await commitToBranch(app, alice, repo, {
838 branch: "feature/loud",
839 base: "main",
840 message: "Make it loud",
841 files: [{ path: "code.txt", content: "alpha\nBETA\n" }],
842 });
843 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
844 const opened = await openPull(app, alice, repo, {
845 title: "Make it loud",
846 baseBranch: "main",
847 headBranch: "feature/loud",
848 });
849 if (!opened.ok) throw new Error(String(opened.error));
850 const pull = opened.value;
851
852 const whileOpen = await (
853 await get(`/alice/orchard/pulls/${pull.number}`, ownerCookie)
854 ).text();
855 expect(whileOpen).not.toContain("Take it back");
856
857 const merged = await mergePull(app, alice, repo, pull, "squash");
858 if (!merged.ok) throw new Error(JSON.stringify(merged.error));
859
860 const ownerPage = await (
861 await get(`/alice/orchard/pulls/${pull.number}`, ownerCookie)
862 ).text();
863 expect(ownerPage).toContain("Take it back");
864 expect(ownerPage).toContain(
865 `/-/confirm?to=%2Falice%2Forchard%2Fpulls%2F${pull.number}%2Ftake-back`,
866 );
867
868 const readerPage = await (
869 await get(`/alice/orchard/pulls/${pull.number}`, readerCookie)
870 ).text();
871 expect(readerPage).not.toContain("Take it back");
872
873 const confirm = await (
874 await get(
875 `/-/confirm?to=${encodeURIComponent(`/alice/orchard/pulls/${pull.number}/take-back`)}`,
876 ownerCookie,
877 )
878 ).text();
879 expect(confirm).toContain("Take back");
880 expect(confirm).toContain("Make it loud");
881 expect(confirm).toContain(
882 "opens a new change that undoes exactly what it shipped",
883 );
884 });
885
886 test("pressing Take it back opens a new change and lands on its studio page", async () => {
887 const shipped = await shipChange(
888 "feature/gamma",
889 "Add gamma",
890 "alpha\nGAMMA\n",
891 );
892
893 const taken = await post(
894 `/alice/orchard/pulls/${shipped.number}/take-back`,
895 ownerCookie,
896 );
897 expect(taken.status).toBe(303);
898 const location = taken.headers.get("location")!;
899 expect(location).toBe(`/alice/orchard/pulls/${shipped.number + 1}`);
900
901 const revertPage = await (await get(location, ownerCookie)).text();
902 expect(revertPage).toContain(`Revert &quot;Add gamma&quot;`);
903 });
904
905 test("the POST is refused from another origin, and nothing is created", async () => {
906 const shipped = await shipChange(
907 "feature/delta",
908 "Add delta",
909 "alpha\nDELTA\n",
910 );
911
912 const forged = await post(
913 `/alice/orchard/pulls/${shipped.number}/take-back`,
914 ownerCookie,
915 "https://evil.example",
916 );
917 expect(forged.status).toBe(403);
918 expect(getPull(app.db, repo.id, shipped.number + 1)).toBeUndefined();
919 });
920
921 test("a refusal is said in the studio's own words, and takes nothing back twice", async () => {
922 const shipped = await shipChange(
923 "feature/epsilon",
924 "Add epsilon",
925 "alpha\nEPSILON\n",
926 );
927 const first = await post(
928 `/alice/orchard/pulls/${shipped.number}/take-back`,
929 ownerCookie,
930 );
931 expect(first.status).toBe(303);
932
933 const again = await post(
934 `/alice/orchard/pulls/${shipped.number}/take-back`,
935 ownerCookie,
936 );
937 expect(again.status).toBe(303);
938 const location = again.headers.get("location")!;
939 expect(location).toBe(`/alice/orchard/pulls/${shipped.number}`);
940 const flash = decodeURIComponent(again.headers.get("set-cookie") ?? "");
941 expect(flash).toContain("already been taken back");
942
943 // Only one revert pull exists — the second attempt created nothing.
944 expect(getPull(app.db, repo.id, shipped.number + 1)?.title).toBe(
945 'Revert "Add epsilon"',
946 );
947 expect(getPull(app.db, repo.id, shipped.number + 2)).toBeUndefined();
948 });
949 });
950
951 describe("the studio's crew panel on a forge with no agents yet", () => {
952 let dataDir: string;
953 let app: App;
954 let servers: Servers;
955 let base: string;
956 let ownerCookie: string;
957 let teamAdminCookie: string;
958
959 const signIn = async (username: string): Promise<string> =>
960 (
961 (
962 await fetch(`${base}/signin`, {
963 method: "POST",
964 body: new URLSearchParams({
965 username,
966 password: "a-good-passphrase",
967 }),
968 redirect: "manual",
969 })
970 ).headers.get("set-cookie") ?? ""
971 ).split(";")[0]!;
972
973 const get = (path: string, cookie: string) =>
974 fetch(`${base}${path}`, {
975 headers: { cookie: `${cookie}; forge_mode=studio` },
976 redirect: "manual",
977 });
978
979 beforeAll(async () => {
980 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-no-agents-"));
981 app = createApp({ dataDir, port: 0 });
982 servers = await serve(app);
983 base = servers.url;
984 const a = await createUser(app.db, { origin: "seed", trusted: true,
985 username: "alice",
986 email: "[email protected]",
987 password: "a-good-passphrase",
988 adminIfFirst: true,
989 });
990 if (!a.ok) throw new Error(a.error);
991 const alice = a.value;
992 const b = await createUser(app.db, { origin: "seed", trusted: true,
993 username: "bob",
994 email: "[email protected]",
995 password: "a-good-passphrase",
996 });
997 if (!b.ok) throw new Error(b.error);
998 ownerCookie = await signIn("alice");
999 teamAdminCookie = await signIn("bob");
1000
1001 // alice's own app: she is both the repo's admin and the forge's.
1002 const pad = await createRepo(app, alice, {
1003 name: "pad",
1004 visibility: "public",
1005 });
1006 if (!pad.ok) throw new Error(String(pad.error));
1007 const padSeeded = await commitToBranch(app, alice, pad.value, {
1008 message: "Start",
1009 files: [{ path: "README.md", content: "pad\n" }],
1010 });
1011 if (!padSeeded.ok) throw new Error(JSON.stringify(padSeeded.error));
1012 const padEnabled = await enableApp(app, alice, pad.value, { slug: "pad" });
1013 if (!padEnabled.ok) throw new Error(String(padEnabled.error));
1014
1015 // an org app whose team admin, bob, is not a forge admin.
1016 const org = await createOrg(app, alice, "acme");
1017 if (!org.ok) throw new Error(org.error);
1018 const team = await createTeam(app, alice, org.value, "leads", "admin");
1019 if (!team.ok) throw new Error(String(team.error));
1020 const added = await addTeamMember(app, alice, org.value, team.value, "bob");
1021 if (!added.ok) throw new Error(String(added.error));
1022 const vault = await createRepo(app, alice, {
1023 name: "vault",
1024 owner: "acme",
1025 visibility: "public",
1026 });
1027 if (!vault.ok) throw new Error(String(vault.error));
1028 const vaultSeeded = await commitToBranch(app, alice, vault.value, {
1029 message: "Start",
1030 files: [{ path: "README.md", content: "vault\n" }],
1031 });
1032 if (!vaultSeeded.ok) throw new Error(JSON.stringify(vaultSeeded.error));
1033 const vaultEnabled = await enableApp(app, alice, vault.value, {
1034 slug: "vault",
1035 });
1036 if (!vaultEnabled.ok) throw new Error(String(vaultEnabled.error));
1037 });
1038
1039 afterAll(async () => {
1040 servers.stop();
1041 app.close();
1042 await rm(dataDir, { recursive: true, force: true });
1043 });
1044
1045 test("a forge admin sees the crew panel name the door an agent is made through, and reaches it in one click without leaving studio mode", async () => {
1046 const settings = await (
1047 await get("/alice/pad/settings", ownerCookie)
1048 ).text();
1049 expect(settings).toContain("No agents exist on this forge yet.");
1050 expect(settings).not.toContain("Add an agent below");
1051 expect(settings).toContain('href="/settings/crew#crew-new"');
1052
1053 const doorResponse = await get("/settings/crew", ownerCookie);
1054 expect(doorResponse.status).toBe(200);
1055 const crewPage = await doorResponse.text();
1056 expect(crewPage).toContain("New agent");
1057 expect(crewPage).toContain('action="/settings/crew"');
1058 });
1059
1060 test("a repository admin who is not a forge admin is told who can add an agent, not sent to a page that refuses them", async () => {
1061 const settings = await (
1062 await get("/acme/vault/settings", teamAdminCookie)
1063 ).text();
1064 expect(settings).toContain(
1065 "No agents exist on this forge yet. An admin of this forge can add one.",
1066 );
1067 expect(settings).not.toContain('href="/settings/crew#crew-new"');
1068
1069 const refused = await get("/settings/crew", teamAdminCookie);
1070 expect(refused.status).toBe(403);
1071 });
1072
1073 test("the app page's way to add a crew member leads unbroken to the door an agent is made through, with nothing dev-only in the way", async () => {
1074 const appPage = await (await get("/alice/pad", ownerCookie)).text();
1075 expect(appPage).toContain("Nobody on the crew yet");
1076 expect(appPage).toContain('href="/alice/pad/settings#crew"');
1077
1078 const settings = await (
1079 await get("/alice/pad/settings", ownerCookie)
1080 ).text();
1081 expect(settings).toContain('href="/settings/crew#crew-new"');
1082
1083 const door = await get("/settings/crew", ownerCookie);
1084 expect(door.status).toBe(200);
1085 const doorBody = await door.text();
1086 expect(doorBody).toContain("New agent");
1087 });
1088 });
1089
1090 /**
1091 * The one function every page words a deployment's speed with. The pages
1092 * above prove it reaches the studio; these prove what it says at the edges,
1093 * where a real deploy is awkward to arrange.
1094 */
1095 describe("servedPhrase", () => {
1096 const env = (extra: Partial<EnvStatus>): EnvStatus => ({
1097 env: "production",
1098 state: "running",
1099 commitOid: "cafebabe",
1100 url: "http://vault.apps.test",
1101 ...extra,
1102 });
1103
1104 test("says nothing at all until the deployment serves", () => {
1105 expect(
1106 servedPhrase(
1107 env({
1108 createdAt: "2026-01-01T00:00:00.000Z",
1109 deployStartedAt: "2026-01-01T00:00:02.000Z",
1110 }),
1111 ),
1112 ).toBeUndefined();
1113 });
1114
1115 test("clocks that disagree clamp to zero rather than printing a negative", () => {
1116 expect(
1117 servedPhrase(
1118 env({
1119 createdAt: "2026-01-01T00:00:00.300Z",
1120 deployStartedAt: "2026-01-01T00:00:00.300Z",
1121 servingAt: "2026-01-01T00:00:00.000Z",
1122 }),
1123 ),
1124 ).toBe("0.0s after the push");
1125 // A start before the push it answers is skew too: no negative wait.
1126 expect(
1127 servedPhrase(
1128 env({
1129 createdAt: "2026-01-01T00:00:05.000Z",
1130 deployStartedAt: "2026-01-01T00:00:00.000Z",
1131 servingAt: "2026-01-01T00:00:06.000Z",
1132 }),
1133 ),
1134 ).toBe("1.0s after the push");
1135 });
1136
1137 test("a wait under a second is noise: the phrase reads as it always has", () => {
1138 expect(
1139 servedPhrase(
1140 env({
1141 createdAt: "2026-01-01T00:00:00.000Z",
1142 deployStartedAt: "2026-01-01T00:00:00.900Z",
1143 servingAt: "2026-01-01T00:00:04.300Z",
1144 }),
1145 ),
1146 ).toBe("4.3s after the push");
1147 });
1148
1149 test("a push with no start recorded is still measured from the push", () => {
1150 expect(
1151 servedPhrase(
1152 env({
1153 createdAt: "2026-01-01T00:00:00.000Z",
1154 servingAt: "2026-01-01T00:00:02.500Z",
1155 }),
1156 ),
1157 ).toBe("2.5s after the push");
1158 });
1159
1160 test("a deployment with neither instant, and an unreadable one, say nothing", () => {
1161 expect(
1162 servedPhrase(env({ servingAt: "2026-01-01T00:00:02.500Z" })),
1163 ).toBeUndefined();
1164 expect(
1165 servedPhrase(
1166 env({ createdAt: "not a time", servingAt: "2026-01-01T00:00:02.500Z" }),
1167 ),
1168 ).toBeUndefined();
1169 });
1170 });
1171
1172 describe("previewLatency: the median seconds from push to serving", () => {
1173 let dataDir: string;
1174 let app: App;
1175 let alice: User;
1176 let repo: Repo;
1177
1178 beforeAll(async () => {
1179 dataDir = await mkdtemp(join(tmpdir(), "forge-preview-latency-"));
1180 app = createApp({ dataDir, port: 0 });
1181 const a = await createUser(app.db, { origin: "seed", trusted: true,
1182 username: "alice",
1183 email: "[email protected]",
1184 password: "a-good-passphrase",
1185 adminIfFirst: true,
1186 });
1187 if (!a.ok) throw new Error(a.error);
1188 alice = a.value;
1189 const made = await createRepo(app, alice, {
1190 name: "latency",
1191 visibility: "private",
1192 description: "",
1193 });
1194 if (!made.ok) throw new Error(String(made.error));
1195 repo = made.value;
1196 });
1197
1198 afterAll(async () => {
1199 app.close();
1200 await rm(dataDir, { recursive: true, force: true });
1201 });
1202
1203 // Each test reads only the deploys it seeds: the window has no upper
1204 // bound, so another test's deploys would otherwise land in it.
1205 beforeEach(() => {
1206 app.db.query("DELETE FROM run WHERE repo_id = ?1").run(repo.id);
1207 });
1208
1209 /**
1210 * A deploy run stamped with a known push instant and, unless null, known started/serving
1211 * instants.
1212 */
1213 const seedDeploy = (
1214 createdAt: string,
1215 startedAt: string | null,
1216 servingAt: string | null,
1217 ): void => {
1218 const deploy = createRun(app.db, { repoId: repo.id, kind: "deploy" });
1219 app.db
1220 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
1221 .run(deploy.id, createdAt);
1222 if (startedAt !== null) {
1223 app.db
1224 .query("UPDATE run SET started_at = ?2 WHERE id = ?1")
1225 .run(deploy.id, startedAt);
1226 }
1227 if (servingAt !== null) {
1228 app.db
1229 .query("UPDATE run SET serving_at = ?2 WHERE id = ?1")
1230 .run(deploy.id, servingAt);
1231 }
1232 };
1233
1234 test("no deploys, a never-served one, and the median of a growing set — all against one window", () => {
1235 const now = Date.now();
1236 const since = new Date(now - 60_000).toISOString();
1237
1238 // Nothing seeded yet: undefined, never a zero.
1239 expect(previewLatency(app.db, repo.id, since)).toBeUndefined();
1240
1241 // Before the window: a very slow deploy that must not pull the median up.
1242 seedDeploy(
1243 new Date(now - 10 * 24 * 60 * 60_000).toISOString(),
1244 null,
1245 new Date(now - 10 * 24 * 60 * 60_000 + 600_000).toISOString(),
1246 );
1247 expect(previewLatency(app.db, repo.id, since)).toBeUndefined();
1248
1249 // A deploy that never served is left out, not counted as a zero.
1250 seedDeploy(new Date(now).toISOString(), null, null);
1251 expect(previewLatency(app.db, repo.id, since)).toBeUndefined();
1252
1253 // Three served deploys, seeded out of order: an odd-count median.
1254 const durations = [2, 6, 4]; // seconds
1255 durations.forEach((d, i) => {
1256 const createdAt = new Date(now + i * 1_000).toISOString();
1257 seedDeploy(
1258 createdAt,
1259 null,
1260 new Date(Date.parse(createdAt) + d * 1_000).toISOString(),
1261 );
1262 });
1263 const odd = previewLatency(app.db, repo.id, since)!;
1264 expect(odd.n).toBe(3);
1265 expect(odd.medianSeconds).toBeCloseTo(4, 2);
1266
1267 // A fourth deploy, 10s: the even-count median is the mean of the middle two.
1268 const createdAt = new Date(now + 3_000).toISOString();
1269 seedDeploy(
1270 createdAt,
1271 null,
1272 new Date(Date.parse(createdAt) + 10_000).toISOString(),
1273 );
1274 const even = previewLatency(app.db, repo.id, since)!;
1275 expect(even.n).toBe(4);
1276 expect(even.medianSeconds).toBeCloseTo(5, 2);
1277
1278 // None of the above carried a `started_at`: the split has nothing to say.
1279 expect(even.queueMedianSeconds).toBeUndefined();
1280 expect(even.bootMedianSeconds).toBeUndefined();
1281 expect(even.splitN).toBe(0);
1282 });
1283
1284 test("the split between waiting to be picked up and coming up, seeded beside a total", () => {
1285 const now = Date.now();
1286 const since = new Date(now - 60_000).toISOString();
1287
1288 // Three deploys with known queue and boot segments: (queue, boot) in seconds.
1289 const segments: readonly (readonly [number, number])[] = [
1290 [1, 3],
1291 [2, 6],
1292 [3, 9],
1293 ];
1294 segments.forEach(([queue, boot], i) => {
1295 const createdAt = now + i * 1_000;
1296 const startedAt = createdAt + queue * 1_000;
1297 const servingAt = startedAt + boot * 1_000;
1298 seedDeploy(
1299 new Date(createdAt).toISOString(),
1300 new Date(startedAt).toISOString(),
1301 new Date(servingAt).toISOString(),
1302 );
1303 });
1304 const full = previewLatency(app.db, repo.id, since)!;
1305 expect(full.n).toBe(3);
1306 expect(full.splitN).toBe(3);
1307 expect(full.queueMedianSeconds).toBeCloseTo(2, 2);
1308 expect(full.bootMedianSeconds).toBeCloseTo(6, 2);
1309
1310 // A fourth, served deploy with no `started_at`: it feeds the total but
1311 // is left out of the split, never counted as a zero-length queue or boot.
1312 const createdAt = now + 3_000;
1313 seedDeploy(
1314 new Date(createdAt).toISOString(),
1315 null,
1316 new Date(createdAt + 100_000).toISOString(),
1317 );
1318 const withGap = previewLatency(app.db, repo.id, since)!;
1319 expect(withGap.n).toBe(4);
1320 expect(withGap.splitN).toBe(3);
1321 expect(withGap.queueMedianSeconds).toBeCloseTo(2, 2);
1322 expect(withGap.bootMedianSeconds).toBeCloseTo(6, 2);
1323 });
1324
1325 test("a deploy that finished without ever serving is counted apart, never in the median; one still running is counted as neither", () => {
1326 const now = Date.now();
1327 const since = new Date(now - 60_000).toISOString();
1328
1329 // Two served deploys, 4s and 6s.
1330 [4, 6].forEach((d, i) => {
1331 const createdAt = new Date(now + i * 1_000).toISOString();
1332 seedDeploy(
1333 createdAt,
1334 null,
1335 new Date(Date.parse(createdAt) + d * 1_000).toISOString(),
1336 );
1337 });
1338 const clean = previewLatency(app.db, repo.id, since)!;
1339 expect(clean.n).toBe(2);
1340 expect(clean.failedN).toBe(0);
1341 expect(clean.medianSeconds).toBeCloseTo(5, 2);
1342
1343 // A deploy still queued: neither served nor finished. Moves neither number.
1344 const inFlight = createRun(app.db, { repoId: repo.id, kind: "deploy" });
1345 app.db
1346 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
1347 .run(inFlight.id, new Date(now + 2_000).toISOString());
1348 const withInFlight = previewLatency(app.db, repo.id, since)!;
1349 expect(withInFlight.n).toBe(2);
1350 expect(withInFlight.failedN).toBe(0);
1351 expect(withInFlight.medianSeconds).toBeCloseTo(5, 2);
1352
1353 // A deploy that finished but never served — counted as failed, never a
1354 // zero-second deploy pulling the median down.
1355 const failed = createRun(app.db, { repoId: repo.id, kind: "deploy" });
1356 app.db
1357 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
1358 .run(failed.id, new Date(now + 3_000).toISOString());
1359 claimRun(app.db, failed.id);
1360 finishRun(app.db, failed.id, { state: "failed", summary: "build failed" });
1361 const withFailed = previewLatency(app.db, repo.id, since)!;
1362 expect(withFailed.n).toBe(2);
1363 expect(withFailed.failedN).toBe(1);
1364 expect(withFailed.medianSeconds).toBeCloseTo(5, 2);
1365
1366 // A cancelled deploy that never served also counts as failed.
1367 const cancelled = createRun(app.db, { repoId: repo.id, kind: "deploy" });
1368 app.db
1369 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
1370 .run(cancelled.id, new Date(now + 4_000).toISOString());
1371 claimRun(app.db, cancelled.id);
1372 app.db
1373 .query(
1374 "UPDATE run SET state = 'cancelled', finished_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?1",
1375 )
1376 .run(cancelled.id);
1377 const withCancelled = previewLatency(app.db, repo.id, since)!;
1378 expect(withCancelled.n).toBe(2);
1379 expect(withCancelled.failedN).toBe(2);
1380 expect(withCancelled.medianSeconds).toBeCloseTo(5, 2);
1381 });
1382
1383 test("a week with only never-served deploys still reports the failed count, with no median to give", () => {
1384 const now = Date.now();
1385 const since = new Date(now - 60_000).toISOString();
1386
1387 const failed = createRun(app.db, { repoId: repo.id, kind: "deploy" });
1388 app.db
1389 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
1390 .run(failed.id, new Date(now).toISOString());
1391 claimRun(app.db, failed.id);
1392 finishRun(app.db, failed.id, { state: "failed", summary: "build failed" });
1393
1394 const latency = previewLatency(app.db, repo.id, since)!;
1395 expect(latency).toBeDefined();
1396 expect(latency.n).toBe(0);
1397 expect(latency.failedN).toBe(1);
1398 expect(latency.medianSeconds).toBeUndefined();
1399 });
1400
1401 test("a deploy the forge's own restart cancelled is never counted as failed, nor in the total it is 'of' (#255, #274)", () => {
1402 const now = Date.now();
1403 const since = new Date(now - 60_000).toISOString();
1404
1405 // A real failure: counted.
1406 const failed = createRun(app.db, { repoId: repo.id, kind: "deploy" });
1407 app.db
1408 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
1409 .run(failed.id, new Date(now).toISOString());
1410 claimRun(app.db, failed.id);
1411 finishRun(app.db, failed.id, { state: "failed", summary: "build failed" });
1412
1413 // A deploy the forge's own restart cancelled: not this commit's fault,
1414 // and not counted at all — neither as a failure nor in the total.
1415 const stopped = createRun(app.db, { repoId: repo.id, kind: "deploy" });
1416 app.db
1417 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
1418 .run(stopped.id, new Date(now + 1_000).toISOString());
1419 claimRun(app.db, stopped.id);
1420 app.db
1421 .query(
1422 "UPDATE run SET state = 'cancelled', summary = ?2, finished_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?1",
1423 )
1424 .run(stopped.id, STOPPED_SUMMARY);
1425
1426 const latency = previewLatency(app.db, repo.id, since)!;
1427 expect(latency.n).toBe(0);
1428 expect(latency.failedN).toBe(1);
1429
1430 // A person's own Cancel, carrying a different summary, still counts —
1431 // only the forge's own reason is left out.
1432 const byPerson = createRun(app.db, { repoId: repo.id, kind: "deploy" });
1433 app.db
1434 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
1435 .run(byPerson.id, new Date(now + 2_000).toISOString());
1436 claimRun(app.db, byPerson.id);
1437 app.db
1438 .query(
1439 "UPDATE run SET state = 'cancelled', summary = 'cancelled by alice', finished_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?1",
1440 )
1441 .run(byPerson.id);
1442 const withPerson = previewLatency(app.db, repo.id, since)!;
1443 expect(withPerson.failedN).toBe(2);
1444 });
1445 });
1446
1447 describe("the studio's own words for how fast a change reaches its preview", () => {
1448 let dataDir: string;
1449 let app: App;
1450 let servers: Servers;
1451 let base: string;
1452 let alice: User;
1453 let repo: Repo;
1454 let ownerCookie: string;
1455
1456 beforeAll(async () => {
1457 dataDir = await mkdtemp(join(tmpdir(), "forge-preview-latency-page-"));
1458 app = createApp({ dataDir, port: 0 });
1459 servers = await serve(app);
1460 base = servers.url;
1461 const a = await createUser(app.db, { origin: "seed", trusted: true,
1462 username: "alice",
1463 email: "[email protected]",
1464 password: "a-good-passphrase",
1465 adminIfFirst: true,
1466 });
1467 if (!a.ok) throw new Error(a.error);
1468 alice = a.value;
1469 ownerCookie = (
1470 (
1471 await fetch(`${base}/signin`, {
1472 method: "POST",
1473 body: new URLSearchParams({
1474 username: "alice",
1475 password: "a-good-passphrase",
1476 }),
1477 redirect: "manual",
1478 })
1479 ).headers.get("set-cookie") ?? ""
1480 ).split(";")[0]!;
1481 const made = await createRepo(app, alice, {
1482 name: "trickle",
1483 visibility: "private",
1484 description: "",
1485 });
1486 if (!made.ok) throw new Error(String(made.error));
1487 repo = made.value;
1488 const seeded = await commitToBranch(app, alice, repo, {
1489 message: "Start",
1490 files: [{ path: "README.md", content: "start\n" }],
1491 });
1492 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
1493 const enabled = await enableApp(app, alice, repo, { slug: "trickle" });
1494 if (!enabled.ok) throw new Error(String(enabled.error));
1495 // Enough history behind it that these tests exercise the preview
1496 // wording, not the day-one line (#274) — a finished agent run from
1497 // well outside this week's own window.
1498 const seedRun = createRun(app.db, { repoId: repo.id, kind: "agent" });
1499 app.db
1500 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
1501 .run(
1502 seedRun.id,
1503 new Date(Date.now() - 30 * 24 * 60 * 60_000).toISOString(),
1504 );
1505 claimRun(app.db, seedRun.id);
1506 finishRun(app.db, seedRun.id, { state: "ok", summary: "seed" });
1507 });
1508
1509 afterAll(async () => {
1510 servers.stop();
1511 app.close();
1512 await rm(dataDir, { recursive: true, force: true });
1513 });
1514
1515 // Each test reads only the deploys it seeds; the agent run from
1516 // beforeAll stays, so the page always has history behind it.
1517 beforeEach(() => {
1518 app.db
1519 .query("DELETE FROM run WHERE repo_id = ?1 AND kind = 'deploy'")
1520 .run(repo.id);
1521 });
1522
1523 const get = (path: string) =>
1524 fetch(`${base}${path}`, {
1525 headers: { cookie: `${ownerCookie}; forge_mode=studio` },
1526 redirect: "manual",
1527 });
1528
1529 /**
1530 * A deploy run stamped with a known push instant and serving instant, and unless null a known
1531 * started instant.
1532 */
1533 const seedDeploy = (
1534 createdAt: string,
1535 startedAt: string | null,
1536 servingAt: string,
1537 ): void => {
1538 const deploy = createRun(app.db, { repoId: repo.id, kind: "deploy" });
1539 app.db
1540 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
1541 .run(deploy.id, createdAt);
1542 if (startedAt !== null) {
1543 app.db
1544 .query("UPDATE run SET started_at = ?2 WHERE id = ?1")
1545 .run(deploy.id, startedAt);
1546 }
1547 app.db
1548 .query("UPDATE run SET serving_at = ?2 WHERE id = ?1")
1549 .run(deploy.id, servingAt);
1550 };
1551
1552 test("fewer than three served deploys this week says so plainly rather than a median of one", async () => {
1553 const before = await (await get("/alice/trickle")).text();
1554 expect(before).toContain("not enough deploys this week to say");
1555 expect(before).not.toContain("reaches its preview in");
1556
1557 const deploy = createRun(app.db, {
1558 repoId: repo.id,
1559 kind: "deploy",
1560 env: "production",
1561 reason: "push",
1562 });
1563 markServing(app.db, deploy.id);
1564 const one = await (await get("/alice/trickle")).text();
1565 expect(one).toContain("not enough deploys this week to say");
1566 expect(one).not.toContain("reaches its preview in");
1567 });
1568
1569 test("the split reads as the total's explanation, and says when it is drawn from fewer deploys", async () => {
1570 // Three deploys with a known queue and boot each.
1571 const now = Date.now();
1572 const segments: readonly (readonly [number, number])[] = [
1573 [1, 3],
1574 [2, 6],
1575 [3, 9],
1576 ];
1577 segments.forEach(([queue, boot], i) => {
1578 const createdAt = now + i * 1_000;
1579 const startedAt = createdAt + queue * 1_000;
1580 const servingAt = startedAt + boot * 1_000;
1581 seedDeploy(
1582 new Date(createdAt).toISOString(),
1583 new Date(startedAt).toISOString(),
1584 new Date(servingAt).toISOString(),
1585 );
1586 });
1587 // A fourth, served deploy with no `started_at`: it feeds the total but
1588 // is left out of the split.
1589 seedDeploy(
1590 new Date(now + 3_000).toISOString(),
1591 null,
1592 new Date(now + 100_000).toISOString(),
1593 );
1594 const page = await (await get("/alice/trickle")).text();
1595 expect(page).toContain("4 deploys");
1596 expect(page).toContain("2.0s waiting");
1597 // Only 3 of the 4 served deploys carried a `started_at`: the split says so.
1598 expect(page).toContain("6.0s coming up (3 of 4)");
1599
1600 // A clean week — every deploy served — reads exactly as it always has:
1601 // no mention of failures, no empty clause left dangling.
1602 expect(page).not.toContain("never reached a preview");
1603 expect(page).not.toContain("0 failed");
1604 });
1605
1606 test("a deploy that finished without ever serving is counted and worded, and links to the runs list", async () => {
1607 // Four served deploys first, so the failed one is the fifth.
1608 const now = Date.now();
1609 for (let i = 0; i < 4; i++) {
1610 seedDeploy(
1611 new Date(now + i * 1_000).toISOString(),
1612 null,
1613 new Date(now + i * 1_000 + 5_000).toISOString(),
1614 );
1615 }
1616 const before = await (await get("/alice/trickle")).text();
1617
1618 const failed = createRun(app.db, {
1619 repoId: repo.id,
1620 kind: "deploy",
1621 env: "production",
1622 reason: "push",
1623 });
1624 claimRun(app.db, failed.id);
1625 finishRun(app.db, failed.id, { state: "failed", summary: "build failed" });
1626
1627 const after = await (await get("/alice/trickle")).text();
1628 expect(after).toContain("1 of 5 never reached a preview");
1629 expect(after).toContain(`href="/alice/trickle/runs?kind=deploy"`);
1630
1631 // A deploy still running moves neither the served count nor the failed
1632 // count: it is neither served nor finished.
1633 const inFlight = createRun(app.db, {
1634 repoId: repo.id,
1635 kind: "deploy",
1636 env: "production",
1637 reason: "push",
1638 });
1639 claimRun(app.db, inFlight.id);
1640 const withInFlight = await (await get("/alice/trickle")).text();
1641 expect(withInFlight).toContain("1 of 5 never reached a preview");
1642 expect(withInFlight).not.toBe(before);
1643 });
1644 });
1645
1646 describe("a low deploy count never reads as a failure ratio (#274)", () => {
1647 let dataDir: string;
1648 let app: App;
1649 let servers: Servers;
1650 let base: string;
1651 let alice: User;
1652 let repo: Repo;
1653 let ownerCookie: string;
1654
1655 beforeAll(async () => {
1656 dataDir = await mkdtemp(join(tmpdir(), "forge-low-count-ratio-"));
1657 app = createApp({ dataDir, port: 0 });
1658 servers = await serve(app);
1659 base = servers.url;
1660 const a = await createUser(app.db, { origin: "seed", trusted: true,
1661 username: "alice",
1662 email: "[email protected]",
1663 password: "a-good-passphrase",
1664 adminIfFirst: true,
1665 });
1666 if (!a.ok) throw new Error(a.error);
1667 alice = a.value;
1668 ownerCookie = (
1669 (
1670 await fetch(`${base}/signin`, {
1671 method: "POST",
1672 body: new URLSearchParams({
1673 username: "alice",
1674 password: "a-good-passphrase",
1675 }),
1676 redirect: "manual",
1677 })
1678 ).headers.get("set-cookie") ?? ""
1679 ).split(";")[0]!;
1680 const made = await createRepo(app, alice, {
1681 name: "tworuns",
1682 visibility: "private",
1683 description: "",
1684 });
1685 if (!made.ok) throw new Error(String(made.error));
1686 repo = made.value;
1687 const seeded = await commitToBranch(app, alice, repo, {
1688 message: "Start",
1689 files: [{ path: "README.md", content: "start\n" }],
1690 });
1691 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
1692 const enabled = await enableApp(app, alice, repo, { slug: "tworuns" });
1693 if (!enabled.ok) throw new Error(String(enabled.error));
1694
1695 // History behind it, so the full panel renders — but only two deploys
1696 // this week, both never served, neither the forge's own doing.
1697 const seedRun = createRun(app.db, { repoId: repo.id, kind: "agent" });
1698 app.db
1699 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
1700 .run(
1701 seedRun.id,
1702 new Date(Date.now() - 30 * 24 * 60 * 60_000).toISOString(),
1703 );
1704 claimRun(app.db, seedRun.id);
1705 finishRun(app.db, seedRun.id, { state: "ok", summary: "seed" });
1706
1707 for (let i = 0; i < 2; i++) {
1708 const failed = createRun(app.db, {
1709 repoId: repo.id,
1710 kind: "deploy",
1711 env: "production",
1712 reason: "push",
1713 });
1714 claimRun(app.db, failed.id);
1715 finishRun(app.db, failed.id, {
1716 state: "failed",
1717 summary: "build failed",
1718 });
1719 }
1720 });
1721
1722 afterAll(async () => {
1723 servers.stop();
1724 app.close();
1725 await rm(dataDir, { recursive: true, force: true });
1726 });
1727
1728 const get = (path: string) =>
1729 fetch(`${base}${path}`, {
1730 headers: { cookie: `${ownerCookie}; forge_mode=studio` },
1731 redirect: "manual",
1732 });
1733
1734 test("2 of 2 never reads as a ratio — a handful of deploys needs to be behind it first", async () => {
1735 const page = await (await get("/alice/tworuns")).text();
1736 expect(page).toContain("not enough deploys this week to say");
1737 expect(page).not.toContain("never reached a preview");
1738 });
1739 });
1740
1741 describe("this week's runs and spend are counted by the database, not read off a page of runs", () => {
1742 let dataDir: string;
1743 let app: App;
1744 let servers: Servers;
1745 let base: string;
1746 let alice: User;
1747 let busy: Repo;
1748 let quiet: Repo;
1749 let ownerCookie: string;
1750
1751 /** How many agent runs this week — more than `listRuns` would ever hand back. */
1752 const RUNS = 250;
1753 /** What each of them cost, in the float dollars the column holds. */
1754 const COST = 0.13;
1755
1756 beforeAll(async () => {
1757 dataDir = await mkdtemp(join(tmpdir(), "forge-week-totals-"));
1758 app = createApp({ dataDir, port: 0 });
1759 servers = await serve(app);
1760 base = servers.url;
1761 const a = await createUser(app.db, { origin: "seed", trusted: true,
1762 username: "alice",
1763 email: "[email protected]",
1764 password: "a-good-passphrase",
1765 adminIfFirst: true,
1766 });
1767 if (!a.ok) throw new Error(a.error);
1768 alice = a.value;
1769 ownerCookie = (
1770 (
1771 await fetch(`${base}/signin`, {
1772 method: "POST",
1773 body: new URLSearchParams({
1774 username: "alice",
1775 password: "a-good-passphrase",
1776 }),
1777 redirect: "manual",
1778 })
1779 ).headers.get("set-cookie") ?? ""
1780 ).split(";")[0]!;
1781
1782 for (const name of ["busy", "quiet"] as const) {
1783 const made = await createRepo(app, alice, {
1784 name,
1785 visibility: "private",
1786 description: "",
1787 });
1788 if (!made.ok) throw new Error(String(made.error));
1789 const seeded = await commitToBranch(app, alice, made.value, {
1790 message: "Start",
1791 files: [{ path: "README.md", content: "start\n" }],
1792 });
1793 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
1794 const enabled = await enableApp(app, alice, made.value, { slug: name });
1795 if (!enabled.ok) throw new Error(String(enabled.error));
1796 if (name === "busy") busy = made.value;
1797 if (name === "quiet") quiet = made.value;
1798 }
1799
1800 // A real week behind "quiet", just not this one — an old finished run,
1801 // so a week with nothing in it still reads as a quiet week (#274), not
1802 // as an app with no history at all.
1803 const oldQuiet = createRun(app.db, { repoId: quiet.id, kind: "agent" });
1804 claimRun(app.db, oldQuiet.id);
1805 finishRun(app.db, oldQuiet.id, { state: "ok", summary: "long ago" });
1806 app.db
1807 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
1808 .run(
1809 oldQuiet.id,
1810 new Date(Date.now() - 30 * 24 * 60 * 60_000).toISOString(),
1811 );
1812
1813 const keeper = await createAgent(app, alice, { username: "keeper" });
1814 if (!keeper.ok) throw new Error(String(keeper.error));
1815 expect(addToCrew(app, alice, busy, "keeper").ok).toBe(true);
1816 const keeperId = getUserByName(app.db, "keeper")!.id;
1817
1818 // 250 agent runs inside the week — past the 200 `listRuns` will return —
1819 // and one fat run ten days back that the window must leave out.
1820 for (let i = 0; i < RUNS; i++) {
1821 const r = createRun(app.db, {
1822 repoId: busy.id,
1823 kind: "agent",
1824 actorId: keeperId,
1825 reason: "assigned",
1826 });
1827 claimRun(app.db, r.id);
1828 finishRun(app.db, r.id, {
1829 state: "ok",
1830 summary: "did a thing",
1831 costUsd: COST,
1832 });
1833 }
1834 const old = createRun(app.db, {
1835 repoId: busy.id,
1836 kind: "agent",
1837 actorId: keeperId,
1838 reason: "assigned",
1839 });
1840 claimRun(app.db, old.id);
1841 finishRun(app.db, old.id, {
1842 state: "ok",
1843 summary: "last month",
1844 costUsd: 99,
1845 });
1846 app.db
1847 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
1848 .run(old.id, new Date(Date.now() - 10 * 24 * 60 * 60_000).toISOString());
1849 });
1850
1851 afterAll(async () => {
1852 servers.stop();
1853 app.close();
1854 await rm(dataDir, { recursive: true, force: true });
1855 });
1856
1857 const get = (path: string) =>
1858 fetch(`${base}${path}`, {
1859 headers: { cookie: `${ownerCookie}; forge_mode=studio` },
1860 redirect: "manual",
1861 });
1862
1863 test("the aggregate counts every run in the window, skips the one outside it, and is 0 for a week with none", () => {
1864 const week = new Date(Date.now() - 7 * 24 * 60 * 60_000).toISOString();
1865 const totals = agentRunTotals(app.db, busy.id, week);
1866 expect(totals.runs).toBe(RUNS);
1867 expect(totals.spentUsd).toBeCloseTo(RUNS * COST, 6);
1868
1869 // The whole ledger, window opened wide: the old run is there after all.
1870 const ever = agentRunTotals(app.db, busy.id, "2000-01-01T00:00:00.000Z");
1871 expect(ever.runs).toBe(RUNS + 1);
1872 expect(ever.spentUsd).toBeCloseTo(RUNS * COST + 99, 6);
1873
1874 // SUM over no rows is NULL; a repository with no runs reads zero.
1875 const none = agentRunTotals(app.db, "no-such-repo", week);
1876 expect(none).toEqual({ runs: 0, spentUsd: 0 });
1877 });
1878
1879 test("the app page says 250 runs and the sum of all 250, not the last 200", async () => {
1880 const page = await (await get("/alice/busy")).text();
1881 expect(page).toMatch(/Runs<\/dt>\s*<dd>250<\/dd>/);
1882 expect(page).toContain(`$${(RUNS * COST).toFixed(2)}`);
1883 // What the 200-row read would have said, and the run outside the window.
1884 expect(page).not.toContain(`$${(200 * COST).toFixed(2)}`);
1885 expect(page).not.toContain("$99.00");
1886 });
1887
1888 test("an app with real history but no runs this week reads 0 and $0.00, never blank", async () => {
1889 const page = await (await get("/alice/quiet")).text();
1890 expect(page).toMatch(/Runs<\/dt>\s*<dd>0<\/dd>/);
1891 expect(page).toContain("$0.00");
1892 });
1893 });
1894
1895 describe("this week's shipping is counted by the database, not read off a page of closed pulls", () => {
1896 let dataDir: string;
1897 let app: App;
1898 let servers: Servers;
1899 let base: string;
1900 let alice: User;
1901 let busy: Repo;
1902 let quiet: Repo;
1903 let ownerCookie: string;
1904
1905 /** How many changes landed this week — past the page of closed pulls the
1906 * count used to be filtered out of. */
1907 const SHIPPED = 150;
1908 /** One agent run's cost, so the page prints a cost per shipped change. */
1909 const SPENT = 30;
1910
1911 /** A pull row on a closed issue: `mergedAt` null is closed without a merge. */
1912 const seedPull = (repo: Repo, title: string, mergedAt: string | null) => {
1913 const opened = openIssue(app, alice, repo, { title });
1914 if (!opened.ok) throw new Error(String(opened.error));
1915 app.db
1916 .query(
1917 `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at)
1918 VALUES (?1, 'main', ?2, 'a', 'b', ?3)`,
1919 )
1920 .run(opened.value.id, `f${opened.value.number}`, mergedAt);
1921 app.db
1922 .query("UPDATE issue SET kind = 'pull', state = 'closed' WHERE id = ?1")
1923 .run(opened.value.id);
1924 };
1925
1926 beforeAll(async () => {
1927 dataDir = await mkdtemp(join(tmpdir(), "forge-week-shipped-"));
1928 app = createApp({ dataDir, port: 0 });
1929 servers = await serve(app);
1930 base = servers.url;
1931 const a = await createUser(app.db, { origin: "seed", trusted: true,
1932 username: "alice",
1933 email: "[email protected]",
1934 password: "a-good-passphrase",
1935 adminIfFirst: true,
1936 });
1937 if (!a.ok) throw new Error(a.error);
1938 alice = a.value;
1939 ownerCookie = (
1940 (
1941 await fetch(`${base}/signin`, {
1942 method: "POST",
1943 body: new URLSearchParams({
1944 username: "alice",
1945 password: "a-good-passphrase",
1946 }),
1947 redirect: "manual",
1948 })
1949 ).headers.get("set-cookie") ?? ""
1950 ).split(";")[0]!;
1951
1952 for (const name of ["busy", "quiet"] as const) {
1953 const made = await createRepo(app, alice, {
1954 name,
1955 visibility: "private",
1956 description: "",
1957 });
1958 if (!made.ok) throw new Error(String(made.error));
1959 const seeded = await commitToBranch(app, alice, made.value, {
1960 message: "Start",
1961 files: [{ path: "README.md", content: "start\n" }],
1962 });
1963 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
1964 const enabled = await enableApp(app, alice, made.value, { slug: name });
1965 if (!enabled.ok) throw new Error(String(enabled.error));
1966 if (name === "busy") busy = made.value;
1967 if (name === "quiet") quiet = made.value;
1968 }
1969
1970 // 150 changes landed inside the week — past the hundred closed pulls the
1971 // page used to read — plus two that must not count: one closed without a
1972 // merge, and one merged ten days back.
1973 const now = Date.now();
1974 for (let i = 0; i < SHIPPED; i++)
1975 seedPull(busy, `landed ${i}`, new Date(now - i * 60_000).toISOString());
1976 seedPull(busy, "abandoned", null);
1977 seedPull(
1978 busy,
1979 "landed last week",
1980 new Date(now - 10 * 24 * 60 * 60_000).toISOString(),
1981 );
1982
1983 // A real week behind "quiet", just not this one — a change shipped a
1984 // month back, so a week with nothing new still reads as a quiet week
1985 // (#274), not as an app with no history at all.
1986 seedPull(
1987 quiet,
1988 "shipped a month ago",
1989 new Date(now - 30 * 24 * 60 * 60_000).toISOString(),
1990 );
1991
1992 const keeper = await createAgent(app, alice, { username: "keeper" });
1993 if (!keeper.ok) throw new Error(String(keeper.error));
1994 expect(addToCrew(app, alice, busy, "keeper").ok).toBe(true);
1995 const run = createRun(app.db, {
1996 repoId: busy.id,
1997 kind: "agent",
1998 actorId: getUserByName(app.db, "keeper")!.id,
1999 reason: "assigned",
2000 });
2001 claimRun(app.db, run.id);
2002 finishRun(app.db, run.id, {
2003 state: "ok",
2004 summary: "a week of work",
2005 costUsd: SPENT,
2006 });
2007 });
2008
2009 afterAll(async () => {
2010 servers.stop();
2011 app.close();
2012 await rm(dataDir, { recursive: true, force: true });
2013 });
2014
2015 const get = (path: string) =>
2016 fetch(`${base}${path}`, {
2017 headers: { cookie: `${ownerCookie}; forge_mode=studio` },
2018 redirect: "manual",
2019 });
2020
2021 test("the aggregate counts the merged pulls in the window and nothing else", () => {
2022 const week = new Date(Date.now() - 7 * 24 * 60 * 60_000).toISOString();
2023 expect(mergedPullCount(app.db, busy.id, week)).toBe(SHIPPED);
2024
2025 // Window opened wide: the older merge is there after all, the pull closed
2026 // without a merge still is not.
2027 expect(mergedPullCount(app.db, busy.id, "2000-01-01T00:00:00.000Z")).toBe(
2028 SHIPPED + 1,
2029 );
2030
2031 // COUNT over no rows is 0, and another repo's merges are never this one's.
2032 expect(mergedPullCount(app.db, "no-such-repo", week)).toBe(0);
2033 });
2034
2035 test("a merged pull on a sealed report is counted only for a reader who may see it", async () => {
2036 const week = new Date(Date.now() - 7 * 24 * 60 * 60_000).toISOString();
2037 const row = app.db
2038 .query<{ id: string; author_id: string }, [string, string]>(
2039 `SELECT issue.id, issue.author_id FROM issue JOIN pull ON pull.issue_id = issue.id
2040 WHERE issue.repo_id = ?1 AND pull.merged_at >= ?2 LIMIT 1`,
2041 )
2042 .get(busy.id, week)!;
2043 app.db.query("UPDATE issue SET sealed = 1 WHERE id = ?1").run(row.id);
2044 try {
2045 expect(mergedPullCount(app.db, busy.id, week)).toBe(SHIPPED);
2046 expect(mergedPullCount(app.db, busy.id, week, { sealed: true })).toBe(SHIPPED);
2047 expect(mergedPullCount(app.db, busy.id, week, {})).toBe(SHIPPED - 1);
2048 expect(mergedPullCount(app.db, busy.id, week, { sealedBy: "someone-else" })).toBe(SHIPPED - 1);
2049 expect(mergedPullCount(app.db, busy.id, week, { sealedBy: row.author_id })).toBe(SHIPPED);
2050
2051 // The page applies the viewer's scope: a stranger is not told the
2052 // sealed pull landed, the writer is.
2053 app.db.query("UPDATE repo SET visibility = 'public' WHERE id = ?1").run(busy.id);
2054 const out = await (
2055 await fetch(`${base}/alice/busy`, {
2056 headers: { cookie: "forge_mode=studio" },
2057 redirect: "manual",
2058 })
2059 ).text();
2060 expect(out).toMatch(/Shipped<\/dt>\s*<dd>149 pull requests<\/dd>/);
2061 const own = await (await get("/alice/busy")).text();
2062 expect(own).toMatch(/Shipped<\/dt>\s*<dd>150 pull requests<\/dd>/);
2063 } finally {
2064 app.db.query("UPDATE repo SET visibility = 'private' WHERE id = ?1").run(busy.id);
2065 app.db.query("UPDATE issue SET sealed = 0 WHERE id = ?1").run(row.id);
2066 }
2067 });
2068
2069 test("the app page says 150 changes shipped, not the 100 a page of closed pulls holds", async () => {
2070 const page = await (await get("/alice/busy")).text();
2071 expect(page).toMatch(/Shipped<\/dt>\s*<dd>150 pull requests<\/dd>/);
2072 expect(page).not.toContain("100 pull requests");
2073 // And the cost of a shipped change divides by all 150.
2074 expect(page).toContain(
2075 `$${(SPENT / SHIPPED).toFixed(2)} per shipped pull request`,
2076 );
2077 });
2078
2079 test("an app with real history but nothing shipped this week reads 0 changes and no cost per change", async () => {
2080 const page = await (await get("/alice/quiet")).text();
2081 expect(page).toMatch(/Shipped<\/dt>\s*<dd>0 pull requests<\/dd>/);
2082 expect(page).not.toContain("per shipped pull request");
2083 });
2084 });
2085
2086 describe("a one-minute-old app's front door does not lead with a week of zeros (#274)", () => {
2087 let dataDir: string;
2088 let app: App;
2089 let servers: Servers;
2090 let base: string;
2091 let alice: User;
2092 let repo: Repo;
2093 let ownerCookie: string;
2094
2095 beforeAll(async () => {
2096 dataDir = await mkdtemp(join(tmpdir(), "forge-day-one-"));
2097 app = createApp({ dataDir, port: 0 });
2098 servers = await serve(app);
2099 base = servers.url;
2100 const a = await createUser(app.db, { origin: "seed", trusted: true,
2101 username: "alice",
2102 email: "[email protected]",
2103 password: "a-good-passphrase",
2104 adminIfFirst: true,
2105 });
2106 if (!a.ok) throw new Error(a.error);
2107 alice = a.value;
2108 ownerCookie = (
2109 (
2110 await fetch(`${base}/signin`, {
2111 method: "POST",
2112 body: new URLSearchParams({
2113 username: "alice",
2114 password: "a-good-passphrase",
2115 }),
2116 redirect: "manual",
2117 })
2118 ).headers.get("set-cookie") ?? ""
2119 ).split(";")[0]!;
2120 const made = await createRepo(app, alice, {
2121 name: "newborn",
2122 visibility: "public",
2123 description: "",
2124 });
2125 if (!made.ok) throw new Error(String(made.error));
2126 repo = made.value;
2127 const seeded = await commitToBranch(app, alice, repo, {
2128 message: "Start",
2129 files: [{ path: "README.md", content: "start\n" }],
2130 });
2131 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
2132 const enabled = await enableApp(app, alice, repo, { slug: "newborn" });
2133 if (!enabled.ok) throw new Error(String(enabled.error));
2134
2135 // Exactly the shape of a one-minute-old app: its own first two
2136 // production deploys, one of which the forge's own restart cancelled —
2137 // never a shipped change, never an agent run.
2138 const cancelledByForge = createRun(app.db, {
2139 repoId: repo.id,
2140 kind: "deploy",
2141 env: "production",
2142 reason: "boot",
2143 });
2144 claimRun(app.db, cancelledByForge.id);
2145 app.db
2146 .query(
2147 "UPDATE run SET state = 'cancelled', summary = ?2, finished_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?1",
2148 )
2149 .run(cancelledByForge.id, STOPPED_SUMMARY);
2150 const secondBoot = createRun(app.db, {
2151 repoId: repo.id,
2152 kind: "deploy",
2153 env: "production",
2154 reason: "push",
2155 });
2156 claimRun(app.db, secondBoot.id);
2157 });
2158
2159 afterAll(async () => {
2160 servers.stop();
2161 app.close();
2162 await rm(dataDir, { recursive: true, force: true });
2163 });
2164
2165 const get = (path: string) =>
2166 fetch(`${base}${path}`, {
2167 headers: { cookie: `${ownerCookie}; forge_mode=studio` },
2168 redirect: "manual",
2169 });
2170
2171 test("the day-one page a person lands on from New app says one true line, not a week of zeros", async () => {
2172 const page = await (await get("/alice/newborn")).text();
2173 // A new app shows only its progress: no week, no rail (#10299).
2174 expect(page).not.toContain("This week");
2175 expect(page).not.toContain("Nothing to show yet");
2176 expect(page).toContain("Tell the crew");
2177 // Not a single zero from the old panel, and not the scary ratio: the
2178 // forge's own cancelled boot is never counted, and two deploys is well
2179 // under the threshold a ratio needs anyway.
2180 expect(page).not.toMatch(/Runs<\/dt>/);
2181 expect(page).not.toMatch(/Runs cost<\/dt>/);
2182 expect(page).not.toContain("never reached a preview");
2183 expect(page).not.toContain("$0.00");
2184 });
2185
2186 test("a stranger sees the same quiet line, not the owner's zeros", async () => {
2187 const page = await (await fetch(`${base}/alice/newborn`)).text();
2188 expect(page).not.toContain("Nothing to show yet");
2189 expect(page).not.toMatch(/Shipped<\/dt>\s*<dd>0 pull requests<\/dd>/);
2190 });
2191 });
test/studio-slots.test.ts added +123
@@ -0,0 +1,123 @@
1 /**
2 * The studio's slot count: how many of the crew's slots are in use, and that
3 * \"Now\" lists every run in flight however many slots there are.
4 *
5 * One of the files the studio's suite was split into so no one file sets the
6 * gate's floor (#2038) or passes 800 lines (#14011); the rest are
7 * test/studio*.test.ts, and they sign in through test/helpers/studio-signin.ts.
8 */
9
10 import { signInCookie } from "./helpers/studio-signin.ts";
11 import { seatAs } from "./helpers/seat.ts";
12 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
13 import { mkdtemp, rm } from "node:fs/promises";
14 import { tmpdir } from "node:os";
15 import { join } from "node:path";
16 import { createApp, type App } from "../src/app.ts";
17 import { enableApp } from "../src/actions/apps.ts";
18 import { addToCrew, createAgent } from "../src/actions/crew.ts";
19 import { openIssue } from "../src/actions/issues.ts";
20 import {
21 commitToBranch,
22 createRepo,
23 updateRepoSettings,
24 } from "../src/actions/repos.ts";
25 import { claimRun, createRun } from "../src/core/runs.ts";
26 import { createUser, getUserByName, type User } from "../src/core/users.ts";
27 import { serve, type Servers } from "../src/http/server.ts";
28
29 describe("the studio's slot count", () => {
30 let dataDir: string;
31 let app: App;
32 let servers: Servers;
33 let base: string;
34 let cookie: string;
35
36 beforeAll(async () => {
37 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-slots-"));
38 app = createApp({ dataDir, port: 0 });
39 servers = await serve(app);
40 base = servers.url;
41 const a = await createUser(app.db, { origin: "seed", trusted: true,
42 username: "alice",
43 email: "[email protected]",
44 password: "a-good-passphrase",
45 adminIfFirst: true,
46 });
47 if (!a.ok) throw new Error(a.error);
48 const alice = a.value;
49 cookie = await signInCookie(base, "alice");
50 const made = await createRepo(app, alice, {
51 name: "loom",
52 visibility: "private",
53 description: "a place for cloth",
54 });
55 if (!made.ok) throw new Error(String(made.error));
56 const repo = made.value;
57 const seeded = await commitToBranch(app, alice, repo, {
58 message: "Start",
59 files: [{ path: "README.md", content: "# loom\n" }],
60 });
61 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
62 const enabled = await enableApp(app, alice, repo, { slug: "loom" });
63 if (!enabled.ok) throw new Error(String(enabled.error));
64 for (const name of ["brick", "quill", "chart"]) {
65 const agent = await createAgent(app, alice, { username: name });
66 if (!agent.ok) throw new Error(String(agent.error));
67 expect(addToCrew(app, alice, repo, name).ok).toBe(true);
68 }
69 const reviewer = getUserByName(app.db, "quill")!;
70 const planner = getUserByName(app.db, "chart")!;
71 seatAs(app.db, repo.id, reviewer.id, "reviewer");
72 seatAs(app.db, repo.id, planner.id, "planner");
73 const on = await updateRepoSettings(app, alice, repo, {
74 autonomy: "autonomous",
75 autonomyDirection: "",
76 autonomySlots: 2,
77 autonomyBy: alice.id,
78 });
79 expect(on.ok).toBe(true);
80 // One builder building, one reviewer reviewing, one planner planning.
81 const inFlight: [string, User, string][] = [
82 ["Warp the loom", getUserByName(app.db, "brick")!, "assigned"],
83 ["Look at the shuttle", reviewer, "review"],
84 ["Plan the next cloth", planner, "assigned"],
85 ];
86 for (const [title, actor, reason] of inFlight) {
87 const issue = openIssue(app, alice, repo, { title });
88 if (!issue.ok) throw new Error(String(issue.error));
89 const run = createRun(app.db, {
90 repoId: repo.id,
91 kind: "agent",
92 issueId: issue.value.id,
93 actorId: actor.id,
94 reason,
95 });
96 claimRun(app.db, run.id);
97 }
98 });
99
100 afterAll(async () => {
101 servers.stop();
102 app.close();
103 await rm(dataDir, { recursive: true, force: true });
104 });
105
106 test("reads 1 of 2 in use with three runs in flight, and Now still lists all three", async () => {
107 const page = await (
108 await fetch(`${base}/alice/loom`, {
109 headers: { cookie: `${cookie}; forge_mode=studio` },
110 redirect: "manual",
111 })
112 ).text();
113 expect(page).toContain("1 of 2 in use");
114 // The crew card says the governor's reason in one line (#7644).
115 expect(page).toContain("Builders 2: a quiet hour, 0 of 0 open pulls moving");
116 expect(page).not.toContain("3 of 2");
117 expect(page).not.toContain("2 of 2");
118 // The runs that take no slot are not hidden: Now has all of them.
119 expect(page).toContain("Warp the loom");
120 expect(page).toContain("Look at the shuttle");
121 expect(page).toContain("Plan the next cloth");
122 });
123 });
test/studio-stranger.test.ts added +489
@@ -0,0 +1,489 @@
1 /**
2 * What a stranger sees on a public app's studio page: the app, never the
3 * owner's books, crew or decisions.
4 *
5 * One of the files the studio's suite was split into so no one file sets the
6 * gate's floor (#2038) or passes 800 lines (#14011); the rest are
7 * test/studio*.test.ts, and they sign in through test/helpers/studio-signin.ts.
8 */
9
10 import { signInCookie } from "./helpers/studio-signin.ts";
11 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
12 import { mkdtemp, rm } from "node:fs/promises";
13 import { tmpdir } from "node:os";
14 import { join } from "node:path";
15 import { createApp, type App } from "../src/app.ts";
16 import { enableApp } from "../src/actions/apps.ts";
17 import { addToCrew, createAgent } from "../src/actions/crew.ts";
18 import { openIssue } from "../src/actions/issues.ts";
19 import { mergePull, openPull } from "../src/actions/pulls.ts";
20 import {
21 commitToBranch,
22 createRepo,
23 updateRepoSettings,
24 } from "../src/actions/repos.ts";
25 import { recordDecision } from "../src/core/decisions.ts";
26 import { getRepo, type Repo } from "../src/core/repos.ts";
27 import { claimRun, createRun, finishRun, type Run } from "../src/core/runs.ts";
28 import { createUser, getUserByName, type User } from "../src/core/users.ts";
29 import { serve, type Servers } from "../src/http/server.ts";
30 import { NOBODY_ON_CREW } from "../src/web/studio/words.ts";
31 import { ageApp } from "./helpers/history.ts";
32
33 /**
34 * Issue #65: a public app's studio page is the app, not the owner's books.
35 * `canSeeOwnerFacts` (`src/core/repos.ts`) is the one predicate — the same
36 * as `canWrite` today — that every surface carrying money, the crew or the
37 * decision ledger must ask before it computes or renders one of those.
38 */
39 describe("a stranger sees the app, not the owner's books", () => {
40 let dataDir: string;
41 let app: App;
42 let servers: Servers;
43 let base: string;
44 let repo: Repo;
45 let alice: User;
46 let ownerCookie: string;
47 let strangerCookie: string;
48 let run: Run;
49
50 const signIn = async (username: string): Promise<string> =>
51 await signInCookie(base, username);
52
53 const get = (path: string, cookie?: string) =>
54 fetch(`${base}${path}`, {
55 headers: {
56 cookie:
57 cookie === undefined
58 ? "forge_mode=studio"
59 : `${cookie}; forge_mode=studio`,
60 },
61 redirect: "manual",
62 });
63
64 beforeAll(async () => {
65 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-public-"));
66 app = createApp({ dataDir, port: 0 });
67 servers = await serve(app);
68 base = servers.url;
69 const a = await createUser(app.db, { origin: "seed", trusted: true,
70 username: "alice",
71 email: "[email protected]",
72 password: "a-good-passphrase",
73 adminIfFirst: true,
74 });
75 if (!a.ok) throw new Error(a.error);
76 alice = a.value;
77 const c = await createUser(app.db, { origin: "seed", trusted: true,
78 username: "carol",
79 email: "[email protected]",
80 password: "a-good-passphrase",
81 });
82 if (!c.ok) throw new Error(c.error);
83 ownerCookie = await signIn("alice");
84 strangerCookie = await signIn("carol");
85 const made = await createRepo(app, alice, {
86 name: "beacon",
87 visibility: "public",
88 description: "a light that stays on",
89 });
90 if (!made.ok) throw new Error(String(made.error));
91 repo = made.value;
92 const seeded = await commitToBranch(app, alice, repo, {
93 message: "Start",
94 files: [{ path: "README.md", content: "beacon\n" }],
95 });
96 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
97 const enabled = await enableApp(app, alice, repo, { slug: "beacon" });
98 if (!enabled.ok) throw new Error(String(enabled.error));
99 const scout = await createAgent(app, alice, { username: "keeper" });
100 if (!scout.ok) throw new Error(String(scout.error));
101 expect(addToCrew(app, alice, repo, "keeper").ok).toBe(true);
102 const on = await updateRepoSettings(app, alice, repo, {
103 autonomy: "assisted",
104 autonomyDirection: "",
105 autonomySlots: 2,
106 autonomyBy: alice.id,
107 });
108 expect(on.ok).toBe(true);
109
110 const tell = openIssue(app, alice, repo, { title: "Trim the wick" });
111 if (!tell.ok) throw new Error(String(tell.error));
112 const branch = await commitToBranch(app, alice, repo, {
113 branch: "keeper/trim-the-wick",
114 base: "main",
115 message: "Trim the wick",
116 files: [{ path: "WICK.md", content: "trim\n" }],
117 });
118 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
119 const opened = await openPull(app, alice, repo, {
120 title: "Trim the wick",
121 body: `Closes #${tell.value.number}`,
122 baseBranch: "main",
123 headBranch: "keeper/trim-the-wick",
124 });
125 if (!opened.ok) throw new Error(String(opened.error));
126 const keeperId = getUserByName(app.db, "keeper")!.id;
127 run = createRun(app.db, {
128 repoId: repo.id,
129 kind: "agent",
130 issueId: tell.value.id,
131 branch: "keeper/trim-the-wick",
132 actorId: keeperId,
133 reason: "assigned",
134 });
135 claimRun(app.db, run.id);
136 run =
137 finishRun(app.db, run.id, {
138 state: "ok",
139 summary: `opened pull #${opened.value.number}`,
140 costUsd: 42.5,
141 turns: 7,
142 }) ?? run;
143 const merged = await mergePull(app, alice, repo, opened.value, "squash");
144 if (!merged.ok) throw new Error(String(merged.error));
145 recordDecision(app.db, {
146 kind: "land",
147 repoId: repo.id,
148 issueId: tell.value.id,
149 state: { card: "ask", head: opened.value.headOid },
150 answer: "ship",
151 p: 0.9,
152 by: "rule",
153 ms: 3,
154 });
155
156 // Three served deploys this week, median 4.0s, and a fourth that never
157 // answered — excluded, not a zero that would drag the median down.
158 const now = Date.now();
159 for (const [offsetMs, durationMs] of [
160 [0, 2_000],
161 [1_000, 4_000],
162 [2_000, 6_000],
163 [3_000, null],
164 ] as const) {
165 const deploy = createRun(app.db, {
166 repoId: repo.id,
167 kind: "deploy",
168 env: "production",
169 commitOid: "cafefeed",
170 reason: "push",
171 });
172 app.db
173 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
174 .run(deploy.id, new Date(now + offsetMs).toISOString());
175 if (durationMs !== null) {
176 app.db
177 .query("UPDATE run SET serving_at = ?2 WHERE id = ?1")
178 .run(deploy.id, new Date(now + offsetMs + durationMs).toISOString());
179 }
180 }
181 });
182
183 afterAll(async () => {
184 servers.stop();
185 app.close();
186 await rm(dataDir, { recursive: true, force: true });
187 });
188
189 test("anonymous and read-only visitors get the app, not the owner's numbers; the owner still gets everything", async () => {
190 const anon = await (await get("/alice/beacon")).text();
191 const stranger = await (await get("/alice/beacon", strangerCookie)).text();
192 const owner = await (await get("/alice/beacon", ownerCookie)).text();
193
194 for (const page of [anon, stranger]) {
195 expect(page).not.toMatch(/\$\d/);
196 expect(page).not.toContain("keeper");
197 expect(page).not.toContain(">Decided<");
198 expect(page).not.toContain(">On its own<");
199 expect(page).not.toContain("/fleet/graph");
200 expect(page).not.toContain(">Slots<");
201 expect(page).not.toContain("Nobody yet");
202 // The app itself is still there: what it is, that it shipped, a way in.
203 expect(page).toContain("a light that stays on");
204 expect(page).toContain(">Shipped<");
205 expect(page).toContain("1 pull request");
206 expect(page).toContain("Open the app");
207 // The direction's own number is the same owner's-books fact as spend
208 // and the ledger: a stranger and an anonymous visitor see neither.
209 expect(page).not.toContain(">Preview<");
210 expect(page).not.toContain("reaches its preview");
211 }
212
213 expect(owner).toContain("$42.50");
214 expect(owner).toContain("keeper");
215 expect(owner).toContain(">On its own<");
216 // One click from the app's spend to the fleet graph, scoped to this app (#578).
217 expect(owner).toContain('href="/fleet/graph?repo=alice%2Fbeacon"');
218 expect(owner).toContain(">Decided<");
219 // Three served deploys this week — 2.0s, 4.0s and 6.0s — median 4.0s;
220 // the fourth, never served, does not drag it toward zero.
221 expect(owner).toContain("reaches its preview in 4.0s");
222 expect(owner).toContain("· 3 deploys");
223 });
224
225 test("the app's live fragment carries no owner fact either", async () => {
226 const fragment = await (await get("/alice/beacon/app/state")).text();
227 expect(fragment).not.toMatch(/\$\d/);
228 expect(fragment).not.toContain("keeper");
229 });
230
231 test("a run's own page hides cost, turns and the crew's name from a stranger and from an anonymous reader; the JSON API redacts the run the same way", async () => {
232 const anonPage = await (await get(`/alice/beacon/runs/${run.id}`)).text();
233 const strangerPage = await (
234 await get(`/alice/beacon/runs/${run.id}`, strangerCookie)
235 ).text();
236 for (const page of [anonPage, strangerPage]) {
237 expect(page).not.toContain("$42.50");
238 expect(page).not.toContain("7 turns");
239 expect(page).not.toContain("keeper");
240 // The log box, which carried data-who, is not drawn for them (#1075).
241 expect(page).toContain("<title>the crew on #");
242 expect(page).not.toContain('id="run-log"');
243 }
244 const ownerPage = await (
245 await get(`/alice/beacon/runs/${run.id}`, ownerCookie)
246 ).text();
247 expect(ownerPage).toContain("$42.50");
248 expect(ownerPage).toContain("7 turns");
249 expect(ownerPage).toContain('data-who="keeper"');
250
251 const asStranger = (await (
252 await get(`/api/v1/repos/alice/beacon/runs/${run.id}`, strangerCookie)
253 ).json()) as { costUsd: number; turns: number };
254 expect(asStranger.costUsd).toBe(0);
255 expect(asStranger.turns).toBe(0);
256 const asOwner = (await (
257 await get(`/api/v1/repos/alice/beacon/runs/${run.id}`, ownerCookie)
258 ).json()) as { costUsd: number; turns: number };
259 expect(asOwner.costUsd).toBe(42.5);
260 expect(asOwner.turns).toBe(7);
261
262 const listAsStranger = (await (
263 await get(`/api/v1/repos/alice/beacon/runs`, strangerCookie)
264 ).json()) as { items: { costUsd: number }[] };
265 expect(listAsStranger.items.every((r) => r.costUsd === 0)).toBe(true);
266
267 // The log is the writers' and crew's (#1075): a stranger's tail is
268 // refused, and carries neither the cost nor the transcript.
269 const tailAsStranger = await get(
270 `/api/v1/repos/alice/beacon/runs/${run.id}/log?follow=1`,
271 strangerCookie,
272 );
273 expect(tailAsStranger.status).toBe(404);
274 expect(await tailAsStranger.text()).not.toContain("42.5");
275 const tailAsOwner = await (
276 await get(
277 `/api/v1/repos/alice/beacon/runs/${run.id}/log?follow=1`,
278 ownerCookie,
279 )
280 ).text();
281 expect(tailAsOwner).toContain("42.5");
282 });
283
284 test("the studio home names the crew for the owner, but only 'the crew' for a signed-in reader of the same public app", async () => {
285 const asStranger = await (await get("/apps", strangerCookie)).text();
286 expect(asStranger.toLowerCase()).not.toContain("keeper");
287 expect(asStranger).toContain("beacon");
288 const asOwner = await (await get("/apps", ownerCookie)).text();
289 expect(asOwner.toLowerCase()).toContain("keeper");
290 expect(asOwner).toContain("beacon");
291 });
292
293 const post = (path: string, body: Record<string, string>, cookie: string) =>
294 fetch(`${base}${path}`, {
295 method: "POST",
296 headers: {
297 cookie: `${cookie}; forge_mode=studio`,
298 origin: base,
299 },
300 body: new URLSearchParams(body),
301 redirect: "manual",
302 });
303
304 test("an autonomous app with nobody on its crew says so beside the mode, and stops saying it once a builder joins", async () => {
305 const made = await createRepo(app, alice, { name: "lamp", visibility: "public" });
306 if (!made.ok) throw new Error(String(made.error));
307 const lamp = made.value;
308 const seeded = await commitToBranch(app, alice, lamp, {
309 message: "Start",
310 files: [{ path: "README.md", content: "lamp\n" }],
311 });
312 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
313 const enabled = await enableApp(app, alice, lamp, { slug: "lamp" });
314 if (!enabled.ok) throw new Error(String(enabled.error));
315 const on = await updateRepoSettings(app, alice, lamp, {
316 autonomy: "autonomous",
317 autonomyDirection: "",
318 autonomySlots: 1,
319 autonomyBy: alice.id,
320 });
321 expect(on.ok).toBe(true);
322
323 // An older app: a new one shows only its progress (#10299).
324 ageApp(app.db, lamp.id);
325 const empty = await (await get("/alice/lamp", ownerCookie)).text();
326 expect(empty).toContain("By itself");
327 expect(empty).not.toContain("Autonomous");
328 expect(empty).toContain("nobody on the crew");
329 expect(empty).toContain('href="/alice/lamp/settings">Put someone on the crew');
330 // The shelf's stuck word and this card share one phrase.
331 expect(NOBODY_ON_CREW).toBe("nobody on the crew");
332
333 const crewed = await (await get("/alice/beacon", ownerCookie)).text();
334 expect(crewed).not.toContain("nobody on the crew");
335
336 const agent = await createAgent(app, alice, { username: "wick" });
337 if (!agent.ok) throw new Error(String(agent.error));
338 expect(addToCrew(app, alice, lamp, "wick").ok).toBe(true);
339 const joined = await (await get("/alice/lamp", ownerCookie)).text();
340 expect(joined).not.toContain("nobody on the crew");
341 });
342
343 test("the studio's settings page renders the crew and the week's budget for a writer, with no dollar ceiling field, with the same numbers the app page shows", async () => {
344 const owner = await (
345 await get("/alice/beacon/settings", ownerCookie)
346 ).text();
347 expect(owner).toContain("keeper");
348 // The crew card itself: the account menu's Crew row, which once answered for it, is gone.
349 expect(owner).toContain('id="crew"');
350 expect(owner).toContain(">On its own<");
351 expect(owner).toMatch(/<dt>Today<\/dt>\s*<dd>\$42\.50 spent<\/dd>/);
352 // The week as a share and a pace; no daily dollar ceiling to read or to set (#10623).
353 expect(owner).toMatch(/<dt>Week<\/dt>\s*<dd>/);
354 expect(owner).not.toMatch(/name="ceiling"/);
355 expect(owner).not.toContain("Ceiling per day");
356 // The crew card says the governor's reason in one line (#7644).
357 expect(owner).toMatch(/<dt>Governor<\/dt>\s*<dd>Builders \d+: /);
358 expect(owner).toContain("Remove");
359 expect(owner).toContain("Save");
360 // The rest is beneath, on the same page and in the same frame.
361 expect(owner).toContain("<h2>Branch and gates</h2>");
362 expect(owner.indexOf(">On its own<")).toBeLessThan(
363 owner.indexOf("<h2>Branch and gates</h2>"),
364 );
365 expect(owner).toContain('href="/alice/beacon/settings/labels"');
366 expect(owner).not.toContain("?mode=dev");
367 });
368
369 test("a reader without write sees no crew and no controls on the settings page; one who cannot read gets not-found", async () => {
370 const stranger = await (
371 await get("/alice/beacon/settings", strangerCookie)
372 ).text();
373 expect(stranger).not.toContain("keeper");
374 expect(stranger).not.toMatch(/\$\d/);
375 expect(stranger).not.toContain("Add to crew");
376 expect(stranger).not.toContain("<select");
377
378 const anon = await get("/alice/beacon/settings");
379 expect(anon.status).toBe(200);
380 const anonText = await anon.text();
381 expect(anonText).not.toContain("keeper");
382
383 const noSuchRepo = await get("/alice/no-such-app/settings", strangerCookie);
384 expect(noSuchRepo.status).toBe(404);
385 });
386
387 test("a reader's post to the crew action is refused, exactly as dev's page refuses it", async () => {
388 const refused = await post(
389 "/alice/beacon/settings/crew",
390 { agent: "keeper" },
391 strangerCookie,
392 );
393 expect(refused.status).toBe(404);
394 });
395
396 test("each settings card saves only its own fields: About never turns a gate or the outsiders switch off, and Branch and gates never touches the words", async () => {
397 const before = getRepo(app.db, "alice", "beacon")!;
398 const on = await post(
399 "/alice/beacon/settings",
400 {
401 card: "gates",
402 defaultBranch: before.defaultBranch,
403 protect: "on",
404 requireReview: "on",
405 // The settings page sends this marker beside the switch; without it a
406 // save leaves the switch as it is (#822).
407 crewOutsidersShown: "1",
408 crewOutsiders: "on",
409 },
410 ownerCookie,
411 );
412 expect(on.status).toBe(303);
413 let now = getRepo(app.db, "alice", "beacon")!;
414 expect(now.crewOutsiders).toBe(true);
415 expect(now.description).toBe(before.description);
416
417 const about = await post(
418 "/alice/beacon/settings",
419 { card: "about", description: "a lamp", visibility: now.visibility },
420 ownerCookie,
421 );
422 expect(about.status).toBe(303);
423 now = getRepo(app.db, "alice", "beacon")!;
424 expect(now.description).toBe("a lamp");
425 expect(now.crewOutsiders).toBe(true);
426 expect(now.protectDefault).toBe(true);
427 expect(now.requireReview).toBe(true);
428
429 // Put it back for the tests that follow.
430 await post(
431 "/alice/beacon/settings",
432 {
433 card: "gates",
434 defaultBranch: before.defaultBranch,
435 crewOutsidersShown: "1",
436 },
437 ownerCookie,
438 );
439 await post(
440 "/alice/beacon/settings",
441 { card: "about", description: before.description, visibility: before.visibility },
442 ownerCookie,
443 );
444 });
445
446 test("a role change made from the studio settings page lands exactly as dev's page does", async () => {
447 const roleChange = await post(
448 "/alice/beacon/settings/crew/keeper/role",
449 { role: "reviewer" },
450 ownerCookie,
451 );
452 expect(roleChange.status).toBe(303);
453 expect(roleChange.headers.get("location")).toBe(
454 "/alice/beacon/settings#crew",
455 );
456 const after = await (
457 await get("/alice/beacon/settings", ownerCookie)
458 ).text();
459 // Keeper declares only "builder", so the repository cannot widen it to
460 // reviewer: it is drawn once, under the seat it holds — the team lists
461 // are the crew, there is no second Crew card listing it again.
462 const keeperRows = after.match(/<li[^>]*data-member="keeper"[^>]*>/g) ?? [];
463 expect(keeperRows).toEqual(['<li data-member="keeper" data-seat="builder">']);
464 expect(after).not.toContain("<h2>Crew</h2>");
465 });
466
467 test("?mode=dev on the settings route is the same one page; dev's Crew and Autonomy addresses land on its cards", async () => {
468 const dev = await (
469 await get("/alice/beacon/settings", ownerCookie)
470 ).text();
471 expect(dev).toContain(">General<");
472 expect(dev).toContain('id="crew"');
473 expect(dev).toContain("Description");
474 expect(dev).toContain("<h2>Branch and gates</h2>");
475 expect(dev).toContain('id="f-direction"');
476
477 for (const [old, to] of [
478 ["/alice/beacon/settings/crew", "/alice/beacon/settings#crew"],
479 ["/alice/beacon/settings/autonomy", "/alice/beacon/settings#autonomy"],
480 ]) {
481 const res = await get(old!, ownerCookie);
482 expect(res.status).toBe(303);
483 expect(res.headers.get("location")).toBe(to!);
484 }
485 // A reader who cannot see the repository gets not-found, not a hint.
486 const hidden = await get("/alice/no-such-app/settings/crew", ownerCookie);
487 expect(hidden.status).toBe(404);
488 });
489 });
test/studio-tails.test.ts added +579
@@ -0,0 +1,579 @@
1 /**
2 * The app's page tails one log at a time: the crew's run, the deploy that is
3 * bringing it alive, or production's own output.
4 *
5 * One of the files the studio's suite was split into so no one file sets the
6 * gate's floor (#2038) or passes 800 lines (#14011); the rest are
7 * test/studio*.test.ts, and they sign in through test/helpers/studio-signin.ts.
8 */
9
10 import { signInCookie } from "./helpers/studio-signin.ts";
11 import {
12 afterAll,
13 afterEach,
14 beforeAll,
15 beforeEach,
16 describe,
17 expect,
18 test,
19 } from "bun:test";
20 import { mkdtemp, rm } from "node:fs/promises";
21 import { tmpdir } from "node:os";
22 import { join } from "node:path";
23 import { createApp, type App } from "../src/app.ts";
24 import { enableApp } from "../src/actions/apps.ts";
25 import type { EnvName } from "../src/apps/data.ts";
26 import { AppsRuntime } from "../src/apps/runtime.ts";
27 import { getAppByRepo, type AppRecord } from "../src/core/apps.ts";
28 import { addToCrew, createAgent } from "../src/actions/crew.ts";
29 import { openIssue } from "../src/actions/issues.ts";
30 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
31 import type { Repo } from "../src/core/repos.ts";
32 import { claimRun, createRun, finishRun, getRun } from "../src/core/runs.ts";
33 import { createUser, getUserByName, type User } from "../src/core/users.ts";
34 import { serve, type Servers } from "../src/http/server.ts";
35 import { appendRunLog } from "../src/runs/log.ts";
36
37 /**
38 * Issue #88: the app's page tails the crew's log while it works — the last
39 * handful of lines beside the running row, live, bounded, and with a
40 * no-script fallback that behaves like the run page's own. Its own repo and
41 * app, so no other test's still-running run ever counts as "the active run"
42 * here.
43 */
44 describe("the app's page tails the crew's log while it works", () => {
45 let dataDir: string;
46 let app: App;
47 let servers: Servers;
48 let base: string;
49 let repo: Repo;
50 let alice: User;
51 let scoutId: string;
52 let writerCookie: string;
53 let readerCookie: string;
54
55 const signIn = async (username: string): Promise<string> =>
56 await signInCookie(base, username);
57
58 const get = (path: string, cookie: string) =>
59 fetch(`${base}${path}`, {
60 headers: { cookie: `${cookie}; forge_mode=studio` },
61 redirect: "manual",
62 });
63
64 beforeAll(async () => {
65 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-tail-"));
66 app = createApp({ dataDir, port: 0 });
67 servers = await serve(app);
68 base = servers.url;
69 const a = await createUser(app.db, { origin: "seed", trusted: true,
70 username: "alice",
71 email: "[email protected]",
72 password: "a-good-passphrase",
73 adminIfFirst: true,
74 });
75 if (!a.ok) throw new Error(a.error);
76 alice = a.value;
77 const b = await createUser(app.db, { origin: "seed", trusted: true,
78 username: "bob",
79 email: "[email protected]",
80 password: "a-good-passphrase",
81 });
82 if (!b.ok) throw new Error(b.error);
83 writerCookie = await signIn("alice");
84 readerCookie = await signIn("bob");
85 const made = await createRepo(app, a.value, {
86 name: "engine",
87 visibility: "private",
88 description: "an app that builds",
89 });
90 if (!made.ok) throw new Error(String(made.error));
91 repo = made.value;
92 const seeded = await commitToBranch(app, a.value, repo, {
93 message: "Start",
94 files: [{ path: "README.md", content: "engine\n" }],
95 });
96 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
97 const enabled = await enableApp(app, a.value, repo, { slug: "engine" });
98 if (!enabled.ok) throw new Error(String(enabled.error));
99 const scout = await createAgent(app, a.value, { username: "scout" });
100 if (!scout.ok) throw new Error(String(scout.error));
101 scoutId = scout.value.user.id;
102 expect(addToCrew(app, a.value, repo, "scout").ok).toBe(true);
103 });
104
105 afterAll(async () => {
106 servers.stop();
107 app.close();
108 await rm(dataDir, { recursive: true, force: true });
109 });
110
111 test("the app's page carries the tail of the active run's log, bounded, and never reloads itself", async () => {
112 const tell = openIssue(app, alice, repo, { title: "Speed up boot" });
113 if (!tell.ok) throw new Error(String(tell.error));
114 const run = createRun(app.db, {
115 repoId: repo.id,
116 kind: "agent",
117 issueId: tell.value.id,
118 actorId: scoutId,
119 reason: "assigned",
120 });
121 claimRun(app.db, run.id);
122 for (let i = 0; i < 9; i++)
123 appendRunLog(app.config.dataDir, run.id, `line ${i}`);
124
125 const page = await (await get("/alice/engine", writerCookie)).text();
126 expect(page).toContain('id="app-run-tail"');
127 expect(page).toContain(
128 `data-stream="/api/v1/repos/alice/engine/runs/${run.id}/log?follow=1"`,
129 );
130 // The retired one-off class is gone from the HTML.
131 expect(page).not.toContain("log-tail-row");
132 // The block-level log box is a sibling <li> padded by §5's .card-body,
133 // never inside a row's ellipsizing, nowrap .row-title, so its lines wrap.
134 expect(page).toMatch(
135 /<li class="card-body">\s*<details[^>]*>\s*<summary[^>]*>Run log<\/summary>\s*<[^>]*id="app-run-tail"/,
136 );
137 expect(page).not.toMatch(
138 /<span\s+class="row-title"[^>]*>(?:(?!<\/span>)[\s\S])*id="app-run-tail"/,
139 );
140 // Bounded on the first paint too: only the last handful made it, and
141 // the box says so.
142 expect(page).not.toContain(">line 0<");
143 expect(page).toContain(">line 8<");
144 expect(page).toContain("Holding the last 6 lines here");
145 // The page never reloads itself (#2873): the Now card is a live region
146 // for this repository's runs, and the tail rides the one /events stream.
147 expect(page).not.toMatch(/<meta\s+http-equiv="refresh"/);
148 expect(page).toMatch(
149 /id="live-now"\s+data-live="run\.queued,run\.started,run\.finished[^"]*"\s+data-live-repo="alice\/engine"/,
150 );
151
152 const stream = await get(
153 `/api/v1/repos/alice/engine/runs/${run.id}/log?follow=1`,
154 writerCookie,
155 );
156 expect(stream.status).toBe(200);
157
158 finishRun(app.db, run.id, { state: "ok", summary: "planned" });
159 const settled = await (await get("/alice/engine", writerCookie)).text();
160 expect(settled).not.toContain('id="app-run-tail"');
161 });
162
163 test("a log far larger than the tail's cap still ends the page at its true last line", async () => {
164 const tell = openIssue(app, alice, repo, { title: "Long-running build" });
165 if (!tell.ok) throw new Error(String(tell.error));
166 const run = createRun(app.db, {
167 repoId: repo.id,
168 kind: "agent",
169 issueId: tell.value.id,
170 actorId: scoutId,
171 reason: "assigned",
172 });
173 claimRun(app.db, run.id);
174 // Far past a byte cap sized for a handful of lines (old default: 512KB).
175 for (let i = 0; i < 2000; i++)
176 appendRunLog(app.config.dataDir, run.id, `line ${i}`);
177
178 const page = await (await get("/alice/engine", writerCookie)).text();
179 expect(page).not.toContain(">line 1993<");
180 expect(page).toContain(">line 1999<");
181 expect(page).toContain("Holding the last 6 lines here");
182
183 finishRun(app.db, run.id, { state: "ok", summary: "planned" });
184 });
185
186 test("a reader without access gets the forge's not-found from the page and from the tail's own stream", async () => {
187 const tell = openIssue(app, alice, repo, { title: "Private boot work" });
188 if (!tell.ok) throw new Error(String(tell.error));
189 const run = createRun(app.db, {
190 repoId: repo.id,
191 kind: "agent",
192 issueId: tell.value.id,
193 actorId: scoutId,
194 reason: "assigned",
195 });
196 claimRun(app.db, run.id);
197 appendRunLog(app.config.dataDir, run.id, "line 0");
198
199 const page = await get("/alice/engine", readerCookie);
200 expect(page.status).toBe(404);
201 const stream = await get(
202 `/api/v1/repos/alice/engine/runs/${run.id}/log?follow=1`,
203 readerCookie,
204 );
205 expect(stream.status).toBe(404);
206 finishRun(app.db, run.id, { state: "ok", summary: "planned" });
207 });
208 });
209
210 /**
211 * Issue #145: the run a brand-new app is waiting on is its first deploy, not
212 * an agent's — the head card tails production's own deploy the same way a
213 * running row is tailed, and yields to an agent run's own tail when one is
214 * also going, so the page never opens two streams for one repo.
215 */
216 describe("the app's page tails the deploy that is bringing it alive", () => {
217 let dataDir: string;
218 let app: App;
219 let servers: Servers;
220 let base: string;
221 let runtime: AppsRuntime;
222 let repo: Repo;
223 let record: AppRecord;
224 let owner: string;
225 let scoutId: string;
226
227 interface PreviewSlot {
228 readonly app: AppRecord;
229 readonly env: EnvName;
230 readonly chain: Promise<void>;
231 readonly restarts: number[];
232 deploying?: unknown;
233 live?: unknown;
234 lastError?: string;
235 lastRunId?: string;
236 }
237 const putEnv = (
238 env: EnvName,
239 slot: Omit<PreviewSlot, "app" | "env" | "chain" | "restarts">,
240 ): void => {
241 (runtime as unknown as { envs: Map<string, PreviewSlot> }).envs.set(
242 `${record.id}/${env}`,
243 { app: record, env, chain: Promise.resolve(), restarts: [], ...slot },
244 );
245 };
246
247 const signIn = async (username: string): Promise<string> =>
248 await signInCookie(base, username);
249
250 const get = (path: string, cookie: string) =>
251 fetch(`${base}${path}`, {
252 headers: { cookie: `${cookie}; forge_mode=studio` },
253 redirect: "manual",
254 });
255
256 // A fresh app for each test: one test's tell or run would otherwise fill
257 // "Now" for the next, and its production slot stand in for the next's.
258 beforeEach(async () => {
259 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-deploy-tail-"));
260 app = createApp({ dataDir, port: 0 });
261 servers = await serve(app);
262 base = servers.url;
263 runtime = new AppsRuntime(app, {
264 domain: "deploytail.test",
265 scheme: "http",
266 publicPort: servers.port,
267 forgeUrl: servers.url,
268 concurrency: 1,
269 });
270 const a = await createUser(app.db, { origin: "seed", trusted: true,
271 username: "alice",
272 email: "[email protected]",
273 password: "a-good-passphrase",
274 adminIfFirst: true,
275 });
276 if (!a.ok) throw new Error(a.error);
277 owner = await signIn("alice");
278 const made = await createRepo(app, a.value, {
279 name: "kindling",
280 visibility: "private",
281 description: "a new app",
282 });
283 if (!made.ok) throw new Error(String(made.error));
284 repo = made.value;
285 const seeded = await commitToBranch(app, a.value, repo, {
286 message: "Start",
287 files: [{ path: "README.md", content: "kindling\n" }],
288 });
289 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
290 const enabled = await enableApp(app, a.value, repo, { slug: "kindling" });
291 if (!enabled.ok) throw new Error(String(enabled.error));
292 record = getAppByRepo(app.db, repo.id)!;
293 const scout = await createAgent(app, a.value, { username: "scout" });
294 if (!scout.ok) throw new Error(String(scout.error));
295 scoutId = scout.value.user.id;
296 expect(addToCrew(app, a.value, repo, "scout").ok).toBe(true);
297 });
298
299 afterEach(async () => {
300 servers.stop();
301 app.close();
302 await rm(dataDir, { recursive: true, force: true });
303 });
304
305 test("a production deploy under way, with no agent run going, is tailed in the head card and named as the deploy's own stream", async () => {
306 const deployRun = createRun(app.db, {
307 repoId: repo.id,
308 kind: "deploy",
309 env: "production",
310 commitOid: "f00dcafe",
311 reason: "boot",
312 });
313 claimRun(app.db, deployRun.id);
314 appendRunLog(app.config.dataDir, deployRun.id, "checking out f00dcafe");
315 appendRunLog(app.config.dataDir, deployRun.id, "install …");
316 putEnv("production", {
317 deploying: {
318 run: { id: deployRun.id, commitOid: "f00dcafe" },
319 abort: new AbortController(),
320 },
321 });
322
323 const page = await (await get("/alice/kindling", owner)).text();
324 expect(page).toContain('id="app-run-tail"');
325 expect(page).toContain('data-who="the deploy"');
326 expect(page).toContain(
327 `data-stream="/api/v1/repos/alice/kindling/runs/${deployRun.id}/log?follow=1"`,
328 );
329 expect(page).toContain("install …");
330 // The page never reloads itself while the deploy goes (#2873): the head
331 // card swaps in place on the deploy's run events.
332 expect(page).not.toMatch(/<meta\s+http-equiv="refresh"/);
333 // Issue #163: the deploy is plainly going, in the head card above — "Now"
334 // does not also claim nothing is happening, and it does not open a
335 // second stream of the same lines to say so.
336 expect(page).not.toContain("Nothing yet");
337 expect(page).toContain("Production is coming up");
338 expect(page.match(/id="app-run-tail"/g)?.length).toBe(1);
339
340 finishRun(app.db, deployRun.id, { state: "ok", summary: "deploy" });
341 });
342
343 test("an agent run at work outranks a background deploy: the page follows the crew, never both", async () => {
344 const deployRun = createRun(app.db, {
345 repoId: repo.id,
346 kind: "deploy",
347 env: "production",
348 commitOid: "d0d0d0d0",
349 reason: "push",
350 });
351 claimRun(app.db, deployRun.id);
352 appendRunLog(app.config.dataDir, deployRun.id, "deploying d0d0d0d0");
353 putEnv("production", {
354 deploying: {
355 run: { id: deployRun.id, commitOid: "d0d0d0d0" },
356 abort: new AbortController(),
357 },
358 });
359
360 const tell = openIssue(app, getUserByName(app.db, "alice")!, repo, {
361 title: "Add a footer",
362 });
363 if (!tell.ok) throw new Error(String(tell.error));
364 const agentRun = createRun(app.db, {
365 repoId: repo.id,
366 kind: "agent",
367 issueId: tell.value.id,
368 actorId: scoutId,
369 reason: "assigned",
370 });
371 claimRun(app.db, agentRun.id);
372 appendRunLog(app.config.dataDir, agentRun.id, "scout is on it");
373
374 const page = await (await get("/alice/kindling", owner)).text();
375 // Exactly one tail, and it is the crew's own run — never the deploy's.
376 expect(page.match(/id="app-run-tail"/g)?.length).toBe(1);
377 expect(page).not.toContain('data-who="the deploy"');
378 expect(page).toContain(
379 `data-stream="/api/v1/repos/alice/kindling/runs/${agentRun.id}/log?follow=1"`,
380 );
381
382 finishRun(app.db, deployRun.id, { state: "ok", summary: "deploy" });
383 finishRun(app.db, agentRun.id, { state: "ok", summary: "planned" });
384 });
385
386 // Issue #284: production stays "live" long after its deploy finishes, and
387 // a fresh live process that has written nothing to its log yet is not an
388 // empty black box — the card says so in a line instead.
389 test("with no agent run going and production live but silent, the head card says so in a line, not an empty box", async () => {
390 putEnv("production", {
391 live: {
392 commitOid: "d0d0d0d0",
393 handle: { port: 4501 },
394 runId: "irrelevant",
395 since: new Date().toISOString(),
396 manifest: { tty: null },
397 },
398 });
399 const page = await (await get("/alice/kindling", owner)).text();
400 expect(page).toContain('id="app-run-tail"');
401 expect(page).not.toContain('class="run-log');
402 expect(page).toContain("Production is quiet");
403 // Still nothing to fetch, and the page does not reload in case that
404 // changes (#2873): the head card swaps in place when production moves.
405 expect(page).not.toMatch(/<meta\s+http-equiv="refresh"/);
406 });
407 });
408
409 /**
410 * Issue #284: production keeps writing to the same run's log for as long as
411 * its process runs, long after the deploy that produced it is finished —
412 * the head card's tail follows that, not just a deploy in flight.
413 */
414 describe("the app's page tails production's own output while it runs (#284)", () => {
415 let dataDir: string;
416 let app: App;
417 let servers: Servers;
418 let base: string;
419 let runtime: AppsRuntime;
420 let repo: Repo;
421 let record: AppRecord;
422 let owner: string;
423 let strangerCookie: string;
424 let scoutId: string;
425
426 interface PreviewSlot {
427 readonly app: AppRecord;
428 readonly env: EnvName;
429 readonly chain: Promise<void>;
430 readonly restarts: number[];
431 deploying?: unknown;
432 live?: unknown;
433 lastError?: string;
434 lastRunId?: string;
435 }
436 const putEnv = (
437 env: EnvName,
438 slot: Omit<PreviewSlot, "app" | "env" | "chain" | "restarts">,
439 ): void => {
440 (runtime as unknown as { envs: Map<string, PreviewSlot> }).envs.set(
441 `${record.id}/${env}`,
442 { app: record, env, chain: Promise.resolve(), restarts: [], ...slot },
443 );
444 };
445
446 const signIn = async (username: string): Promise<string> =>
447 await signInCookie(base, username);
448
449 const get = (path: string, cookie: string) =>
450 fetch(`${base}${path}`, {
451 headers: { cookie: `${cookie}; forge_mode=studio` },
452 redirect: "manual",
453 });
454
455 beforeAll(async () => {
456 dataDir = await mkdtemp(join(tmpdir(), "forge-studio-live-tail-"));
457 app = createApp({ dataDir, port: 0 });
458 servers = await serve(app);
459 base = servers.url;
460 runtime = new AppsRuntime(app, {
461 domain: "livetail.test",
462 scheme: "http",
463 publicPort: servers.port,
464 forgeUrl: servers.url,
465 concurrency: 1,
466 });
467 const a = await createUser(app.db, { origin: "seed", trusted: true,
468 username: "alice",
469 email: "[email protected]",
470 password: "a-good-passphrase",
471 adminIfFirst: true,
472 });
473 if (!a.ok) throw new Error(a.error);
474 const c = await createUser(app.db, { origin: "seed", trusted: true,
475 username: "carol",
476 email: "[email protected]",
477 password: "a-good-passphrase",
478 });
479 if (!c.ok) throw new Error(c.error);
480 owner = await signIn("alice");
481 strangerCookie = await signIn("carol");
482 const made = await createRepo(app, a.value, {
483 name: "furnace",
484 visibility: "public",
485 description: "an app that keeps a fire going",
486 });
487 if (!made.ok) throw new Error(String(made.error));
488 repo = made.value;
489 const seeded = await commitToBranch(app, a.value, repo, {
490 message: "Start",
491 files: [{ path: "README.md", content: "furnace\n" }],
492 });
493 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
494 const enabled = await enableApp(app, a.value, repo, { slug: "furnace" });
495 if (!enabled.ok) throw new Error(String(enabled.error));
496 record = getAppByRepo(app.db, repo.id)!;
497 const scout = await createAgent(app, a.value, { username: "scout" });
498 if (!scout.ok) throw new Error(String(scout.error));
499 scoutId = scout.value.user.id;
500 expect(addToCrew(app, a.value, repo, "scout").ok).toBe(true);
501 });
502
503 afterAll(async () => {
504 servers.stop();
505 app.close();
506 await rm(dataDir, { recursive: true, force: true });
507 });
508
509 test("production running with lines in its log carries the tail, named as the app's own", async () => {
510 const deployRun = createRun(app.db, {
511 repoId: repo.id,
512 kind: "deploy",
513 env: "production",
514 commitOid: "b16b00b5",
515 reason: "push",
516 });
517 claimRun(app.db, deployRun.id);
518 for (let i = 0; i < 9; i++)
519 appendRunLog(app.config.dataDir, deployRun.id, `serving request ${i}`);
520 finishRun(app.db, deployRun.id, { state: "ok", summary: "deploy" });
521 putEnv("production", {
522 live: {
523 commitOid: "b16b00b5",
524 handle: { port: 4503 },
525 runId: deployRun.id,
526 since: new Date().toISOString(),
527 manifest: { tty: null },
528 },
529 });
530
531 const page = await (await get("/alice/furnace", owner)).text();
532 expect(page).toContain('id="app-run-tail"');
533 expect(page).toContain('data-who="the app"');
534 expect(page).toContain(
535 `data-stream="/api/v1/repos/alice/furnace/runs/${deployRun.id}/log?follow=1"`,
536 );
537 expect(page).not.toContain(">serving request 0<");
538 expect(page).toContain(">serving request 8<");
539 expect(page).toContain("Holding the last 6 lines here");
540 expect(page).not.toMatch(/<meta\s+http-equiv="refresh"/);
541
542 // Issue #284's own point: the run this reads from is long finished —
543 // the tail follows the env that points at it, not the run's own state.
544 expect(getRun(app.db, deployRun.id)?.state).toBe("ok");
545 });
546
547 test("a viewer who cannot see the owner's facts gets no output at all", async () => {
548 const response = await get("/alice/furnace", strangerCookie);
549 expect(response.status).toBe(200);
550 const page = await response.text();
551 expect(page).not.toContain('id="app-run-tail"');
552 expect(page).not.toContain("serving request");
553 });
554
555 test("an agent run at work still outranks production's own tail", async () => {
556 const tell = openIssue(app, getUserByName(app.db, "alice")!, repo, {
557 title: "Add a chimney",
558 });
559 if (!tell.ok) throw new Error(String(tell.error));
560 const agentRun = createRun(app.db, {
561 repoId: repo.id,
562 kind: "agent",
563 issueId: tell.value.id,
564 actorId: scoutId,
565 reason: "assigned",
566 });
567 claimRun(app.db, agentRun.id);
568 appendRunLog(app.config.dataDir, agentRun.id, "scout is on it");
569
570 const page = await (await get("/alice/furnace", owner)).text();
571 expect(page.match(/id="app-run-tail"/g)?.length).toBe(1);
572 expect(page).not.toContain('data-who="the app"');
573 expect(page).toContain(
574 `data-stream="/api/v1/repos/alice/furnace/runs/${agentRun.id}/log?follow=1"`,
575 );
576
577 finishRun(app.db, agentRun.id, { state: "ok", summary: "planned" });
578 });
579 });
test/studio-week-counts.test.ts added +534
@@ -0,0 +1,534 @@
1 /**
2 * The week's counts on the studio: runs, spend and shipping counted by the
3 * database, a low deploy count never read as a ratio, and a new app's front
4 * door without a week of zeros.
5 *
6 * One of the files the studio's suite was split into so no one file sets the
7 * gate's floor (#2038) or passes 800 lines (#14011); the rest are
8 * test/studio*.test.ts, and they sign in through test/helpers/studio-signin.ts.
9 */
10
11 import { signInCookie } from "./helpers/studio-signin.ts";
12 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
13 import { mkdtemp, rm } from "node:fs/promises";
14 import { tmpdir } from "node:os";
15 import { join } from "node:path";
16 import { createApp, type App } from "../src/app.ts";
17 import { enableApp } from "../src/actions/apps.ts";
18 import { addToCrew, createAgent } from "../src/actions/crew.ts";
19 import { openIssue } from "../src/actions/issues.ts";
20 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
21 import { mergedPullCount } from "../src/core/pulls.ts";
22 import { type Repo } from "../src/core/repos.ts";
23 import {
24 agentRunTotals,
25 claimRun,
26 createRun,
27 finishRun,
28 STOPPED_SUMMARY,
29 } from "../src/core/runs.ts";
30 import { createUser, getUserByName, type User } from "../src/core/users.ts";
31 import { serve, type Servers } from "../src/http/server.ts";
32
33 describe("a low deploy count never reads as a failure ratio (#274)", () => {
34 let dataDir: string;
35 let app: App;
36 let servers: Servers;
37 let base: string;
38 let alice: User;
39 let repo: Repo;
40 let ownerCookie: string;
41
42 beforeAll(async () => {
43 dataDir = await mkdtemp(join(tmpdir(), "forge-low-count-ratio-"));
44 app = createApp({ dataDir, port: 0 });
45 servers = await serve(app);
46 base = servers.url;
47 const a = await createUser(app.db, { origin: "seed", trusted: true,
48 username: "alice",
49 email: "[email protected]",
50 password: "a-good-passphrase",
51 adminIfFirst: true,
52 });
53 if (!a.ok) throw new Error(a.error);
54 alice = a.value;
55 ownerCookie = await signInCookie(base, "alice");
56 const made = await createRepo(app, alice, {
57 name: "tworuns",
58 visibility: "private",
59 description: "",
60 });
61 if (!made.ok) throw new Error(String(made.error));
62 repo = made.value;
63 const seeded = await commitToBranch(app, alice, repo, {
64 message: "Start",
65 files: [{ path: "README.md", content: "start\n" }],
66 });
67 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
68 const enabled = await enableApp(app, alice, repo, { slug: "tworuns" });
69 if (!enabled.ok) throw new Error(String(enabled.error));
70
71 // History behind it, so the full panel renders — but only two deploys
72 // this week, both never served, neither the forge's own doing.
73 const seedRun = createRun(app.db, { repoId: repo.id, kind: "agent" });
74 app.db
75 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
76 .run(
77 seedRun.id,
78 new Date(Date.now() - 30 * 24 * 60 * 60_000).toISOString(),
79 );
80 claimRun(app.db, seedRun.id);
81 finishRun(app.db, seedRun.id, { state: "ok", summary: "seed" });
82
83 for (let i = 0; i < 2; i++) {
84 const failed = createRun(app.db, {
85 repoId: repo.id,
86 kind: "deploy",
87 env: "production",
88 reason: "push",
89 });
90 claimRun(app.db, failed.id);
91 finishRun(app.db, failed.id, {
92 state: "failed",
93 summary: "build failed",
94 });
95 }
96 });
97
98 afterAll(async () => {
99 servers.stop();
100 app.close();
101 await rm(dataDir, { recursive: true, force: true });
102 });
103
104 const get = (path: string) =>
105 fetch(`${base}${path}`, {
106 headers: { cookie: `${ownerCookie}; forge_mode=studio` },
107 redirect: "manual",
108 });
109
110 test("2 of 2 never reads as a ratio — a handful of deploys needs to be behind it first", async () => {
111 const page = await (await get("/alice/tworuns")).text();
112 expect(page).toContain("not enough deploys this week to say");
113 expect(page).not.toContain("never reached a preview");
114 });
115 });
116
117 describe("this week's runs and spend are counted by the database, not read off a page of runs", () => {
118 let dataDir: string;
119 let app: App;
120 let servers: Servers;
121 let base: string;
122 let alice: User;
123 let busy: Repo;
124 let quiet: Repo;
125 let ownerCookie: string;
126
127 /** How many agent runs this week — more than `listRuns` would ever hand back. */
128 const RUNS = 250;
129 /** What each of them cost, in the float dollars the column holds. */
130 const COST = 0.13;
131
132 beforeAll(async () => {
133 dataDir = await mkdtemp(join(tmpdir(), "forge-week-totals-"));
134 app = createApp({ dataDir, port: 0 });
135 servers = await serve(app);
136 base = servers.url;
137 const a = await createUser(app.db, { origin: "seed", trusted: true,
138 username: "alice",
139 email: "[email protected]",
140 password: "a-good-passphrase",
141 adminIfFirst: true,
142 });
143 if (!a.ok) throw new Error(a.error);
144 alice = a.value;
145 ownerCookie = await signInCookie(base, "alice");
146
147 for (const name of ["busy", "quiet"] as const) {
148 const made = await createRepo(app, alice, {
149 name,
150 visibility: "private",
151 description: "",
152 });
153 if (!made.ok) throw new Error(String(made.error));
154 const seeded = await commitToBranch(app, alice, made.value, {
155 message: "Start",
156 files: [{ path: "README.md", content: "start\n" }],
157 });
158 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
159 const enabled = await enableApp(app, alice, made.value, { slug: name });
160 if (!enabled.ok) throw new Error(String(enabled.error));
161 if (name === "busy") busy = made.value;
162 if (name === "quiet") quiet = made.value;
163 }
164
165 // A real week behind "quiet", just not this one — an old finished run,
166 // so a week with nothing in it still reads as a quiet week (#274), not
167 // as an app with no history at all.
168 const oldQuiet = createRun(app.db, { repoId: quiet.id, kind: "agent" });
169 claimRun(app.db, oldQuiet.id);
170 finishRun(app.db, oldQuiet.id, { state: "ok", summary: "long ago" });
171 app.db
172 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
173 .run(
174 oldQuiet.id,
175 new Date(Date.now() - 30 * 24 * 60 * 60_000).toISOString(),
176 );
177
178 const keeper = await createAgent(app, alice, { username: "keeper" });
179 if (!keeper.ok) throw new Error(String(keeper.error));
180 expect(addToCrew(app, alice, busy, "keeper").ok).toBe(true);
181 const keeperId = getUserByName(app.db, "keeper")!.id;
182
183 // 250 agent runs inside the week — past the 200 `listRuns` will return —
184 // and one fat run ten days back that the window must leave out.
185 for (let i = 0; i < RUNS; i++) {
186 const r = createRun(app.db, {
187 repoId: busy.id,
188 kind: "agent",
189 actorId: keeperId,
190 reason: "assigned",
191 });
192 claimRun(app.db, r.id);
193 finishRun(app.db, r.id, {
194 state: "ok",
195 summary: "did a thing",
196 costUsd: COST,
197 });
198 }
199 const old = createRun(app.db, {
200 repoId: busy.id,
201 kind: "agent",
202 actorId: keeperId,
203 reason: "assigned",
204 });
205 claimRun(app.db, old.id);
206 finishRun(app.db, old.id, {
207 state: "ok",
208 summary: "last month",
209 costUsd: 99,
210 });
211 app.db
212 .query("UPDATE run SET created_at = ?2 WHERE id = ?1")
213 .run(old.id, new Date(Date.now() - 10 * 24 * 60 * 60_000).toISOString());
214 });
215
216 afterAll(async () => {
217 servers.stop();
218 app.close();
219 await rm(dataDir, { recursive: true, force: true });
220 });
221
222 const get = (path: string) =>
223 fetch(`${base}${path}`, {
224 headers: { cookie: `${ownerCookie}; forge_mode=studio` },
225 redirect: "manual",
226 });
227
228 test("the aggregate counts every run in the window, skips the one outside it, and is 0 for a week with none", () => {
229 const week = new Date(Date.now() - 7 * 24 * 60 * 60_000).toISOString();
230 const totals = agentRunTotals(app.db, busy.id, week);
231 expect(totals.runs).toBe(RUNS);
232 expect(totals.spentUsd).toBeCloseTo(RUNS * COST, 6);
233
234 // The whole ledger, window opened wide: the old run is there after all.
235 const ever = agentRunTotals(app.db, busy.id, "2000-01-01T00:00:00.000Z");
236 expect(ever.runs).toBe(RUNS + 1);
237 expect(ever.spentUsd).toBeCloseTo(RUNS * COST + 99, 6);
238
239 // SUM over no rows is NULL; a repository with no runs reads zero.
240 const none = agentRunTotals(app.db, "no-such-repo", week);
241 expect(none).toEqual({ runs: 0, spentUsd: 0 });
242 });
243
244 test("the app page says 250 runs and the sum of all 250, not the last 200", async () => {
245 const page = await (await get("/alice/busy")).text();
246 expect(page).toMatch(/Runs<\/dt>\s*<dd>250<\/dd>/);
247 expect(page).toContain(`$${(RUNS * COST).toFixed(2)}`);
248 // What the 200-row read would have said, and the run outside the window.
249 expect(page).not.toContain(`$${(200 * COST).toFixed(2)}`);
250 expect(page).not.toContain("$99.00");
251 });
252
253 test("an app with real history but no runs this week reads 0 and $0.00, never blank", async () => {
254 const page = await (await get("/alice/quiet")).text();
255 expect(page).toMatch(/Runs<\/dt>\s*<dd>0<\/dd>/);
256 expect(page).toContain("$0.00");
257 });
258 });
259
260 describe("this week's shipping is counted by the database, not read off a page of closed pulls", () => {
261 let dataDir: string;
262 let app: App;
263 let servers: Servers;
264 let base: string;
265 let alice: User;
266 let busy: Repo;
267 let quiet: Repo;
268 let ownerCookie: string;
269
270 /** How many changes landed this week — past the page of closed pulls the
271 * count used to be filtered out of. */
272 const SHIPPED = 150;
273 /** One agent run's cost, so the page prints a cost per shipped change. */
274 const SPENT = 30;
275
276 /** A pull row on a closed issue: `mergedAt` null is closed without a merge. */
277 const seedPull = (repo: Repo, title: string, mergedAt: string | null) => {
278 const opened = openIssue(app, alice, repo, { title });
279 if (!opened.ok) throw new Error(String(opened.error));
280 app.db
281 .query(
282 `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at)
283 VALUES (?1, 'main', ?2, 'a', 'b', ?3)`,
284 )
285 .run(opened.value.id, `f${opened.value.number}`, mergedAt);
286 app.db
287 .query("UPDATE issue SET kind = 'pull', state = 'closed' WHERE id = ?1")
288 .run(opened.value.id);
289 };
290
291 beforeAll(async () => {
292 dataDir = await mkdtemp(join(tmpdir(), "forge-week-shipped-"));
293 app = createApp({ dataDir, port: 0 });
294 servers = await serve(app);
295 base = servers.url;
296 const a = await createUser(app.db, { origin: "seed", trusted: true,
297 username: "alice",
298 email: "[email protected]",
299 password: "a-good-passphrase",
300 adminIfFirst: true,
301 });
302 if (!a.ok) throw new Error(a.error);
303 alice = a.value;
304 ownerCookie = await signInCookie(base, "alice");
305
306 for (const name of ["busy", "quiet"] as const) {
307 const made = await createRepo(app, alice, {
308 name,
309 visibility: "private",
310 description: "",
311 });
312 if (!made.ok) throw new Error(String(made.error));
313 const seeded = await commitToBranch(app, alice, made.value, {
314 message: "Start",
315 files: [{ path: "README.md", content: "start\n" }],
316 });
317 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
318 const enabled = await enableApp(app, alice, made.value, { slug: name });
319 if (!enabled.ok) throw new Error(String(enabled.error));
320 if (name === "busy") busy = made.value;
321 if (name === "quiet") quiet = made.value;
322 }
323
324 // 150 changes landed inside the week — past the hundred closed pulls the
325 // page used to read — plus two that must not count: one closed without a
326 // merge, and one merged ten days back.
327 const now = Date.now();
328 for (let i = 0; i < SHIPPED; i++)
329 seedPull(busy, `landed ${i}`, new Date(now - i * 60_000).toISOString());
330 seedPull(busy, "abandoned", null);
331 seedPull(
332 busy,
333 "landed last week",
334 new Date(now - 10 * 24 * 60 * 60_000).toISOString(),
335 );
336
337 // A real week behind "quiet", just not this one — a change shipped a
338 // month back, so a week with nothing new still reads as a quiet week
339 // (#274), not as an app with no history at all.
340 seedPull(
341 quiet,
342 "shipped a month ago",
343 new Date(now - 30 * 24 * 60 * 60_000).toISOString(),
344 );
345
346 const keeper = await createAgent(app, alice, { username: "keeper" });
347 if (!keeper.ok) throw new Error(String(keeper.error));
348 expect(addToCrew(app, alice, busy, "keeper").ok).toBe(true);
349 const run = createRun(app.db, {
350 repoId: busy.id,
351 kind: "agent",
352 actorId: getUserByName(app.db, "keeper")!.id,
353 reason: "assigned",
354 });
355 claimRun(app.db, run.id);
356 finishRun(app.db, run.id, {
357 state: "ok",
358 summary: "a week of work",
359 costUsd: SPENT,
360 });
361 });
362
363 afterAll(async () => {
364 servers.stop();
365 app.close();
366 await rm(dataDir, { recursive: true, force: true });
367 });
368
369 const get = (path: string) =>
370 fetch(`${base}${path}`, {
371 headers: { cookie: `${ownerCookie}; forge_mode=studio` },
372 redirect: "manual",
373 });
374
375 test("the aggregate counts the merged pulls in the window and nothing else", () => {
376 const week = new Date(Date.now() - 7 * 24 * 60 * 60_000).toISOString();
377 expect(mergedPullCount(app.db, busy.id, week)).toBe(SHIPPED);
378
379 // Window opened wide: the older merge is there after all, the pull closed
380 // without a merge still is not.
381 expect(mergedPullCount(app.db, busy.id, "2000-01-01T00:00:00.000Z")).toBe(
382 SHIPPED + 1,
383 );
384
385 // COUNT over no rows is 0, and another repo's merges are never this one's.
386 expect(mergedPullCount(app.db, "no-such-repo", week)).toBe(0);
387 });
388
389 test("a merged pull on a sealed report is counted only for a reader who may see it", async () => {
390 const week = new Date(Date.now() - 7 * 24 * 60 * 60_000).toISOString();
391 const row = app.db
392 .query<{ id: string; author_id: string }, [string, string]>(
393 `SELECT issue.id, issue.author_id FROM issue JOIN pull ON pull.issue_id = issue.id
394 WHERE issue.repo_id = ?1 AND pull.merged_at >= ?2 LIMIT 1`,
395 )
396 .get(busy.id, week)!;
397 app.db.query("UPDATE issue SET sealed = 1 WHERE id = ?1").run(row.id);
398 try {
399 expect(mergedPullCount(app.db, busy.id, week)).toBe(SHIPPED);
400 expect(mergedPullCount(app.db, busy.id, week, { sealed: true })).toBe(SHIPPED);
401 expect(mergedPullCount(app.db, busy.id, week, {})).toBe(SHIPPED - 1);
402 expect(mergedPullCount(app.db, busy.id, week, { sealedBy: "someone-else" })).toBe(SHIPPED - 1);
403 expect(mergedPullCount(app.db, busy.id, week, { sealedBy: row.author_id })).toBe(SHIPPED);
404
405 // The page applies the viewer's scope: a stranger is not told the
406 // sealed pull landed, the writer is.
407 app.db.query("UPDATE repo SET visibility = 'public' WHERE id = ?1").run(busy.id);
408 const out = await (
409 await fetch(`${base}/alice/busy`, {
410 headers: { cookie: "forge_mode=studio" },
411 redirect: "manual",
412 })
413 ).text();
414 expect(out).toMatch(/Shipped<\/dt>\s*<dd>149 pull requests<\/dd>/);
415 const own = await (await get("/alice/busy")).text();
416 expect(own).toMatch(/Shipped<\/dt>\s*<dd>150 pull requests<\/dd>/);
417 } finally {
418 app.db.query("UPDATE repo SET visibility = 'private' WHERE id = ?1").run(busy.id);
419 app.db.query("UPDATE issue SET sealed = 0 WHERE id = ?1").run(row.id);
420 }
421 });
422
423 test("the app page says 150 changes shipped, not the 100 a page of closed pulls holds", async () => {
424 const page = await (await get("/alice/busy")).text();
425 expect(page).toMatch(/Shipped<\/dt>\s*<dd>150 pull requests<\/dd>/);
426 expect(page).not.toContain("100 pull requests");
427 // And the cost of a shipped change divides by all 150.
428 expect(page).toContain(
429 `$${(SPENT / SHIPPED).toFixed(2)} per shipped pull request`,
430 );
431 });
432
433 test("an app with real history but nothing shipped this week reads 0 changes and no cost per change", async () => {
434 const page = await (await get("/alice/quiet")).text();
435 expect(page).toMatch(/Shipped<\/dt>\s*<dd>0 pull requests<\/dd>/);
436 expect(page).not.toContain("per shipped pull request");
437 });
438 });
439
440 describe("a one-minute-old app's front door does not lead with a week of zeros (#274)", () => {
441 let dataDir: string;
442 let app: App;
443 let servers: Servers;
444 let base: string;
445 let alice: User;
446 let repo: Repo;
447 let ownerCookie: string;
448
449 beforeAll(async () => {
450 dataDir = await mkdtemp(join(tmpdir(), "forge-day-one-"));
451 app = createApp({ dataDir, port: 0 });
452 servers = await serve(app);
453 base = servers.url;
454 const a = await createUser(app.db, { origin: "seed", trusted: true,
455 username: "alice",
456 email: "[email protected]",
457 password: "a-good-passphrase",
458 adminIfFirst: true,
459 });
460 if (!a.ok) throw new Error(a.error);
461 alice = a.value;
462 ownerCookie = await signInCookie(base, "alice");
463 const made = await createRepo(app, alice, {
464 name: "newborn",
465 visibility: "public",
466 description: "",
467 });
468 if (!made.ok) throw new Error(String(made.error));
469 repo = made.value;
470 const seeded = await commitToBranch(app, alice, repo, {
471 message: "Start",
472 files: [{ path: "README.md", content: "start\n" }],
473 });
474 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
475 const enabled = await enableApp(app, alice, repo, { slug: "newborn" });
476 if (!enabled.ok) throw new Error(String(enabled.error));
477
478 // Exactly the shape of a one-minute-old app: its own first two
479 // production deploys, one of which the forge's own restart cancelled —
480 // never a shipped change, never an agent run.
481 const cancelledByForge = createRun(app.db, {
482 repoId: repo.id,
483 kind: "deploy",
484 env: "production",
485 reason: "boot",
486 });
487 claimRun(app.db, cancelledByForge.id);
488 app.db
489 .query(
490 "UPDATE run SET state = 'cancelled', summary = ?2, finished_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?1",
491 )
492 .run(cancelledByForge.id, STOPPED_SUMMARY);
493 const secondBoot = createRun(app.db, {
494 repoId: repo.id,
495 kind: "deploy",
496 env: "production",
497 reason: "push",
498 });
499 claimRun(app.db, secondBoot.id);
500 });
501
502 afterAll(async () => {
503 servers.stop();
504 app.close();
505 await rm(dataDir, { recursive: true, force: true });
506 });
507
508 const get = (path: string) =>
509 fetch(`${base}${path}`, {
510 headers: { cookie: `${ownerCookie}; forge_mode=studio` },
511 redirect: "manual",
512 });
513
514 test("the day-one page a person lands on from New app says one true line, not a week of zeros", async () => {
515 const page = await (await get("/alice/newborn")).text();
516 // A new app shows only its progress: no week, no rail (#10299).
517 expect(page).not.toContain("This week");
518 expect(page).not.toContain("Nothing to show yet");
519 expect(page).toContain("Tell the crew");
520 // Not a single zero from the old panel, and not the scary ratio: the
521 // forge's own cancelled boot is never counted, and two deploys is well
522 // under the threshold a ratio needs anyway.
523 expect(page).not.toMatch(/Runs<\/dt>/);
524 expect(page).not.toMatch(/Runs cost<\/dt>/);
525 expect(page).not.toContain("never reached a preview");
526 expect(page).not.toContain("$0.00");
527 });
528
529 test("a stranger sees the same quiet line, not the owner's zeros", async () => {
530 const page = await (await fetch(`${base}/alice/newborn`)).text();
531 expect(page).not.toContain("Nothing to show yet");
532 expect(page).not.toMatch(/Shipped<\/dt>\s*<dd>0 pull requests<\/dd>/);
533 });
534 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs