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

The terminal's resilience screen tests where snapshots go with `t`: reached or not, in words, never a key, as MCP `backups_test` does #7312

Parked opened this
Closed without merging

scout/forge:scout/7297-the-terminal-s-resilience-screen-tests-w → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
2 files changed +165 −1
src/tui/screens/resilience.ts +25 −1
@@ -1,13 +1,16 @@
11 /**
22 * The Resilience command's dialog, a forge admin's: one line per THREATS.md
33 * protection with its standing in the last gate, as /resilience says it.
4 * Enter, Esc or q closes it. A forge admin who is a person also sees where the
4 * Enter, Esc or q closes it. `t` (a person who is a forge admin) tests the
5 * snapshot destination in effect and says what came of it in the page's
6 * words. A forge admin who is a person also sees where the
57 * off-box snapshot mirror is, `M` seals it (five prompts, the secret masked,
68 * a blank keeps what was sealed) and `F` forgets it after a confirm; the
79 * rules are `actions/mirror.ts`, the Backups page's and MCP `mirror_set`'s.
810 * No key is ever drawn or read back.
911 */
1012
13 import { testDestination } from "../../actions/destination.ts";
1114 import { forgetMirror, saveMirror } from "../../actions/mirror.ts";
1215 import type { DestinationInput } from "../../actions/destination.ts";
1316 import { snapshotsOf } from "../../snapshot/service.ts";
@@ -44,9 +47,11 @@ const FIELDS = [
4447 export function resilienceScreen(f: Session, ctx: Ctx): Screen {
4548 const mayMirror = f.user?.isAdmin === true && f.user.kind === "user";
4649 let note = "";
50 let testing = false;
4751 const screen: Screen = {
4852 title: "Resilience",
4953 keys: [
54 ...(mayMirror ? [binding(["t"], "test where snapshots go")] : []),
5055 ...(mayMirror ? [binding(["m"], "set the mirror"), binding(["f"], "forget the mirror")] : []),
5156 binding(["enter", "esc", "q"], "close"),
5257 ],
@@ -68,6 +73,25 @@ export function resilienceScreen(f: Session, ctx: Ctx): Screen {
6873 },
6974 key(e) {
7075 if (e.ctrl || e.alt) return true;
76 if (e.key === "t" && mayMirror && f.user) {
77 if (testing) return true;
78 testing = true;
79 note = "Testing…";
80 ctx.invalidate();
81 const actor = f.user;
82 const blank = { endpoint: "", bucket: "", region: "", accessKey: "", secretKey: "" };
83 void ctx.track(
84 testDestination(f.app, actor, blank)
85 .then((said) => (said.ok ? said.value : "Not found."))
86 .catch(() => "The test could not run.")
87 .then((answer) => {
88 note = answer;
89 testing = false;
90 ctx.invalidate();
91 }),
92 );
93 return true;
94 }
7195 if (e.key === "enter" || e.key === "esc" || e.key === "q") ctx.pop();
7296 else if (mayMirror && e.key === "m") ctx.push(ask(0, {}));
7397 else if (mayMirror && e.key === "f")
test/tui-backups-test.test.ts added +140
@@ -0,0 +1,140 @@
1 /**
2 * `t` on the terminal's Resilience screen tests where snapshots go, as MCP
3 * `backups_test` and the Backups page do: reached or not, in words, no key.
4 */
5 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { createApp, type App as Forge } from "../src/app.ts";
10 import { saveDestination } from "../src/actions/destination.ts";
11 import { createUser, getUserByName } from "../src/core/users.ts";
12 import { createForgeApp } from "../src/tui/forge.ts";
13 import type { TuiIdentity } from "../src/tui/session.ts";
14 import { headlessTerminal } from "../src/tui/term/terminal.ts";
15 import { quietLogs } from "./helpers/globals.ts";
16 import { settleApp } from "./helpers/tui.ts";
17
18 quietLogs();
19
20 const SECRET = "s3cr3t-value-xyz";
21 const ACCESS = "AKIAEXAMPLE";
22
23 describe("the terminal tests the snapshot destination", () => {
24 let dataDir: string;
25 let forge: Forge;
26 let hits = 0;
27 let refuse = false;
28 const store = Bun.serve({
29 port: 0,
30 fetch() {
31 hits++;
32 if (refuse) return new Response("<Error><Code>AccessDenied</Code></Error>", { status: 403 });
33 return new Response(
34 `<?xml version="1.0" encoding="UTF-8"?><ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Name>my-bucket</Name><Prefix></Prefix><KeyCount>0</KeyCount><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated></ListBucketResult>`,
35 { headers: { "content-type": "application/xml" } },
36 );
37 },
38 });
39
40 function boot(username: string) {
41 const terminal = headlessTerminal(140, 32);
42 const user = getUserByName(forge.db, username)!;
43 const identity: TuiIdentity = { sshUsername: "t", remoteAddress: "127.0.0.1", user };
44 const { app } = createForgeApp(forge, identity, { terminal, exitHooks: false });
45 app.start();
46 const key = (k: string, ctrl = false) =>
47 app.dispatch({ type: "key", key: k, ctrl, alt: false, shift: false });
48 return {
49 text: () => app.screen.toText(),
50 async open() {
51 key("k", true);
52 for (const ch of "Resilience") key(ch);
53 await settleApp(app);
54 key("enter");
55 await settleApp(app);
56 },
57 async t() {
58 key("t");
59 await settleApp(app);
60 for (let i = 0; i < 100 && !/Reached|Could not|no S3|could not run/.test(app.screen.toText()); i++) {
61 await Bun.sleep(50);
62 await settleApp(app);
63 }
64 },
65 stop: () => app.stop(),
66 };
67 }
68
69 beforeAll(async () => {
70 dataDir = await mkdtemp(join(tmpdir(), "forge-tui-backups-test-"));
71 forge = createApp({ dataDir, port: 0 });
72 for (const [username, adminIfFirst] of [["alice", true], ["bob", false]] as const) {
73 const made = await createUser(forge.db, {
74 origin: "seed",
75 trusted: true,
76 username,
77 email: `${username}@forge.local`,
78 password: "a-good-passphrase",
79 adminIfFirst,
80 });
81 if (!made.ok) throw new Error(made.error);
82 }
83 });
84
85 afterAll(async () => {
86 store.stop(true);
87 forge.close();
88 await rm(dataDir, { recursive: true, force: true });
89 });
90
91 const seal = (endpoint: string) =>
92 saveDestination(forge, getUserByName(forge.db, "alice")!, {
93 endpoint,
94 bucket: "my-bucket",
95 region: "",
96 accessKey: ACCESS,
97 secretKey: SECRET,
98 });
99
100 test("with no destination set it says so and calls out to nothing", async () => {
101 const h = boot("alice");
102 await h.open();
103 await h.t();
104 expect(h.text()).toContain("no S3 destination to test");
105 expect(hits).toBe(0);
106 h.stop();
107 });
108
109 test("a reachable store reads reached, with no key on the frame", async () => {
110 expect(seal(`http://127.0.0.1:${store.port}`).ok).toBe(true);
111 const h = boot("alice");
112 await h.open();
113 expect(h.text()).toContain("test where snapshots go");
114 await h.t();
115 expect(h.text()).toContain("Reached");
116 expect(h.text()).toContain("0 objects listed");
117 expect(h.text()).not.toContain(SECRET);
118 expect(h.text()).not.toContain(ACCESS);
119 h.stop();
120 });
121
122 test("an unreachable store reads its reason, with no key on the frame", async () => {
123 refuse = true;
124 expect(seal(`http://127.0.0.1:${store.port}`).ok).toBe(true);
125 const h = boot("alice");
126 await h.open();
127 await h.t();
128 expect(h.text()).toContain("Could not list");
129 expect(h.text()).not.toContain(SECRET);
130 expect(h.text()).not.toContain(ACCESS);
131 h.stop();
132 });
133
134 test("a non-admin has no Resilience command, so no t", async () => {
135 const h = boot("bob");
136 await h.open();
137 expect(h.text()).not.toContain("test where snapshots go");
138 h.stop();
139 });
140 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs