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

An admin signed in at the terminal is asked once to work day to day as an everyday account, as the home card asks #8052

Parked opened this
Closed without merging

mason/forge:mason/8046-an-admin-signed-in-at-the-terminal-is-as → main 4 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +168 −0
src/tui/screens/dashboard.ts +39
@@ -28,6 +28,7 @@ import { agentsLine } from "../../web/studio/client_home.ts";
2828 import { lastVisit } from "../../core/home_visit.ts";
2929 import { sinceDigest } from "../../web/studio/since.ts";
3030 import { askForSomething } from "./tell.ts";
31 import { askForEveryday, everydayOf, EVERYDAY_WHY, keepAdmin } from "../../core/everyday.ts";
3132 import { mayView } from "../../core/sealed.ts";
3233 import { applyFleetIds, previewFleetIds, undoFleetOp } from "../../actions/fleet.ts";
3334 import { plain } from "./boards.ts";
@@ -210,6 +211,12 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen {
210211 let decider: string | null = null;
211212 /** Home's three figures, as the web's row says them; empty unless an admin looks (they count every repository). */
212213 let figures: string[] = [];
214 /**
215 * The web home's everyday ask, as one muted line at the head (#8046): why,
216 * and the two answers, while an admin has made no choice; once linked, the
217 * name of the account used day to day. Nobody else gets the row.
218 */
219 let everyday: { line: string; asking: boolean } | undefined;
213220 /** One line per fired flow alarm; an admin reads every repository, anyone else public ones alone. */
214221 let alarms: string[] = [];
215222 /** The panels this admin pinned to the web home, by title and board, in their order. */
@@ -295,10 +302,29 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen {
295302 ctx.invalidate();
296303 };
297304
305 const everydayHead = (): typeof everyday => {
306 if (f.user === undefined || !f.user.isAdmin || f.user.kind !== "user") return undefined;
307 if (askForEveryday(f.db, f.user))
308 return { line: `${EVERYDAY_WHY} E makes it on the web · K keeps using admin`, asking: true };
309 const daily = everydayOf(f.db, f.user);
310 return daily === undefined
311 ? undefined
312 : { line: `Day to day you are ${daily.username}; this is the admin account.`, asking: false };
313 };
314
315 /** `K`: the admin keeps working as admin, recorded for web and terminal alike. */
316 const keepAdminHere = (): void => {
317 if (f.user === undefined) return;
318 keepAdmin(f.db, f.user);
319 reload();
320 ctx.toast("kept admin; you will not be asked again", "success");
321 };
322
298323 const reload = (): void => {
299324 // The server remembers the *choice* and nothing about the laptop, so
300325 // this is a list of decisions — never a status that would be stale.
301326 pins = f.user ? listPins(f.db, f.user.id) : [];
327 everyday = everydayHead();
302328 pinned = f.user ? pinnedRepoIds(f.db, f.user.id) : new Set<string>();
303329 all = listReposVisibleTo(f.db, f.user, { limit: 200 });
304330 onYou = assignedToMe();
@@ -953,6 +979,9 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen {
953979 ...(focus === "repos" && f.user !== undefined && shown().some(mayArchive)
954980 ? [binding(["space"], "mark"), ...(markedRepos.size > 0 ? [binding(["a"], "archive marked")] : [])]
955981 : []),
982 ...(everyday?.asking === true
983 ? [binding(["E"], "everyday account"), binding(["K"], "keep admin")]
984 : []),
956985 binding(["r"], "refresh"),
957986 ];
958987 },
@@ -970,6 +999,11 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen {
970999 // A site admin's forge strip, as the board's: "All fine" or a line
9711000 // per cause. Anyone else gets no rows and no reads.
9721001 let body = area;
1002 if (everyday !== undefined) {
1003 const [strip, rest] = split(body, "v", [len(1), fill(1)]);
1004 s.text(strip!.x, strip!.y, truncate(everyday.line, strip!.w), style(c.mutedFg, c.bg), strip!.x + strip!.w);
1005 body = rest!;
1006 }
9731007 if (alarms.length > 0) {
9741008 const [strip, rest] = split(body, "v", [len(alarms.length), fill(1)]);
9751009 alarms.forEach((line, i) =>
@@ -1197,6 +1231,11 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen {
11971231 ctx.push(migrateScreen(f, ctx));
11981232 return true;
11991233 }
1234 if (e.key === "E" && everyday?.asking === true) {
1235 openOnWeb(f, ctx, "/settings", "Making your everyday account");
1236 return true;
1237 }
1238 if (e.key === "K" && everyday?.asking === true) return keepAdminHere(), true;
12001239 if (e.key === "A" && f.user) {
12011240 openOnWeb(f, ctx, "/settings", "Account settings");
12021241 return true;
test/egress-admin.test.ts +3
@@ -93,6 +93,9 @@ describe("egress grants in the terminal", () => {
9393 await settleUntil(h.tui, "granted by ada");
9494 expect(grants()).toEqual([ARM]);
9595 await h.press("x");
96 await settleUntil(h.tui, "Stop ada/arm reaching");
97 expect(grants()).toEqual([ARM]);
98 await h.press("y");
9699 await settleUntil(h.tui, "asked for, not granted");
97100 expect(grants()).toEqual([]);
98101 } finally {
test/tui-everyday.test.ts added +126
@@ -0,0 +1,126 @@
1 /** The terminal's dashboard asks an admin once to work day to day as an everyday account, as the web home does (#8046). */
2 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createAgent } from "../src/actions/crew.ts";
7 import { createApp, type App as Forge } from "../src/app.ts";
8 import { askForEveryday, keptAdmin, linkEveryday } from "../src/core/everyday.ts";
9 import { createUser, getUserByName, type User } from "../src/core/users.ts";
10 import { createForgeApp } from "../src/tui/forge.ts";
11 import { headlessTerminal } from "../src/tui/term/terminal.ts";
12 import { quietLogs } from "./helpers/globals.ts";
13 import { seedAdmin } from "./helpers/admin.ts";
14 import { settleApp } from "./helpers/tui.ts";
15
16 quietLogs();
17
18 const ASK = "K keeps using admin";
19 const WHY_START = "Admin can change everything here";
20
21 let dir: string;
22 let forge: Forge;
23 let alice: User;
24 let bob: User;
25
26 async function person(username: string, isAdmin = false): Promise<User> {
27 const made = await createUser(forge.db, {
28 origin: "seed",
29 trusted: true,
30 username,
31 email: `${username}@forge.test`,
32 password: "a-long-passphrase",
33 });
34 if (!made.ok) throw new Error(made.error);
35 if (isAdmin) forge.db.query("UPDATE user SET is_admin = 1 WHERE id = ?1").run(made.value.id);
36 return getUserByName(forge.db, username)!;
37 }
38
39 beforeAll(async () => {
40 dir = await mkdtemp(join(tmpdir(), "forge-tui-everyday-"));
41 forge = createApp({ dataDir: dir, port: 0 });
42 const a = await seedAdmin(forge, { username: "alice", email: "[email protected]", password: "a-long-passphrase" });
43 if (!a.ok) throw new Error("signup failed");
44 alice = a.value;
45 bob = await person("bob");
46 });
47
48 afterAll(async () => {
49 forge.close();
50 await rm(dir, { recursive: true, force: true });
51 });
52
53 function boot(user: User) {
54 const terminal = headlessTerminal(240, 32);
55 const { app } = createForgeApp(
56 forge,
57 { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:t", user },
58 { terminal, exitHooks: false },
59 );
60 app.start();
61 return {
62 text: () => app.screen.toText(),
63 settle: () => settleApp(app),
64 press: async (key: string) => {
65 app.dispatch({ type: "key", key, ctrl: false, alt: false, shift: false });
66 await settleApp(app);
67 },
68 stop: () => app.stop(),
69 };
70 }
71
72 async function firstScreen(user: User): Promise<string> {
73 const h = boot(user);
74 await h.settle();
75 const text = h.text();
76 h.stop();
77 return text;
78 }
79
80 test("an admin with no choice made sees the line at the head; K keeps admin and the line goes for good", async () => {
81 expect(askForEveryday(forge.db, alice)).toBe(true);
82 const h = boot(alice);
83 await h.settle();
84 const head = h.text().split("\n").find((line) => line.includes(WHY_START));
85 expect(head).toBeDefined();
86 expect(head).toContain("E makes it on the web · K keeps using admin");
87
88 // `k` still moves the cursor: it never makes the choice.
89 await h.press("k");
90 expect(keptAdmin(forge.db, alice)).toBe(false);
91 expect(h.text()).toContain(ASK);
92
93 await h.press("K");
94 expect(keptAdmin(forge.db, alice)).toBe(true);
95 expect(h.text()).not.toContain(WHY_START);
96 h.stop();
97
98 // The choice is the web's too: a fresh session is not asked again.
99 expect(askForEveryday(forge.db, alice)).toBe(false);
100 expect(await firstScreen(alice)).not.toContain(WHY_START);
101 });
102
103 test("an admin with an everyday account linked is told its name instead", async () => {
104 const carol = await person("carol", true);
105 const daily = await person("dana");
106 linkEveryday(forge.db, carol, daily);
107 const text = await firstScreen(carol);
108 expect(text).toContain("Day to day you are dana; this is the admin account.");
109 expect(text).not.toContain(ASK);
110 // The everyday account itself is not an admin, and is asked nothing.
111 expect(await firstScreen(daily)).not.toContain("Day to day");
112 });
113
114 test("a non-admin and an agent never see the line", async () => {
115 expect(await firstScreen(bob)).not.toContain(WHY_START);
116 const made = await createAgent(forge, alice, { username: "everydaybot" });
117 if (!made.ok) throw new Error(String(made.error));
118 // Even an agent made an admin is not asked: the everyday account is a person's.
119 forge.db.query("UPDATE user SET is_admin = 1 WHERE username = 'everydaybot'").run();
120 const agent = getUserByName(forge.db, "everydaybot")!;
121 expect(agent.isAdmin).toBe(true);
122 expect(agent.kind).toBe("agent");
123 const text = await firstScreen(agent);
124 expect(text).not.toContain(WHY_START);
125 expect(text).not.toContain("Day to day");
126 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs