Public: anyone can read this; the crew can change it.
Close account in the terminal: Your profile shows the closing day, c asks to close, k keeps #15759
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15714-close-account-in-the-terminal-your-profi → main 0 commits fork 1 thread · 1 unresolved
3 files
changed +166
−2
docs/features/account-close.md added +16
| @@ -0,0 +1,16 @@ | ||
| 1 | --- | |
| 2 | topic: Every setting, by topic | |
| 3 | row: Close my account | |
| 4 | --- | |
| 5 | # Close my account | |
| 6 | ||
| 7 | Asking to close records a request; the account closes 7 days later (`GRACE_DAYS`), and **Keep my account** clears it | |
| 8 | before then. Nothing is deleted by asking. Both doors call `askToClose` in `src/actions/account_close.ts` and refuse in | |
| 9 | the same words: a wrong username or password, being the forge's only admin, being an organization's only admin while | |
| 10 | others write to it, or still paying for a forge. | |
| 11 | ||
| 12 | - Web: Settings › Account, the close block (password or passkey, and the username), and **Keep my account**. | |
| 13 | - Terminal: **Your profile** shows "closes on <date>" while a request stands. `c` asks for the password (masked), then the | |
| 14 | username; `k` keeps the account. Asking from the terminal ends every browser session. | |
| 15 | ||
| 16 | Pinned by `test/account-close.test.ts` and `test/tui-account-close.test.ts`. |
src/tui/screens/profile.ts +45 −2
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | import { emailSavedWords, PROFILE_FALLBACK, PROFILE_REFUSALS } from "../../actions/profile_words.ts"; |
| 12 | import { askToClose, CLOSE_WORDS, closeRequestedAt, closesOn, closingSays, GRACE_DAYS, keepMyAccount, keptSays } from "../../actions/account_close.ts"; | |
| 12 | 13 | import { updateProfile } from "../../actions/users.ts"; |
| 13 | 14 | import { EXPORT_EXEC, EXPORT_PATH, EXPORT_WORDS, exportCounts, exportFileName } from "../../core/account_export.ts"; |
| 14 | 15 | import { sshCommand } from "../../ssh/command.ts"; |
| @@ -19,7 +20,7 @@ import { binding } from "../app/keys.ts"; | ||
| 19 | 20 | import type { Session } from "../session.ts"; |
| 20 | 21 | import { style } from "../term/style.ts"; |
| 21 | 22 | import { truncate } from "../term/width.ts"; |
| 22 | import { confirmScreen, promptScreen } from "./common.ts"; | |
| 23 | import { confirmScreen, later, promptScreen } from "./common.ts"; | |
| 23 | 24 | import type { PaletteEntry } from "./entries.ts"; |
| 24 | 25 | |
| 25 | 26 | /** What the export holds, counted with small queries (#15617). */ |
| @@ -52,20 +53,53 @@ export function profileScreen(f: Session, ctx: Ctx): Screen { | ||
| 52 | 53 | return undefined; |
| 53 | 54 | }; |
| 54 | 55 | |
| 56 | /** `c`: the password (masked), then the username; the same asking and words as the web. */ | |
| 57 | const askClose = (): void => { | |
| 58 | const user = f.user; | |
| 59 | if (user === undefined) return; | |
| 60 | ctx.push( | |
| 61 | promptScreen(ctx, screen, { | |
| 62 | title: "Close account", | |
| 63 | label: "password", | |
| 64 | hint: `Closes ${GRACE_DAYS} days after you ask; k keeps it.`, | |
| 65 | mask: true, | |
| 66 | onSubmit: (password) => | |
| 67 | later(ctx, () => | |
| 68 | promptScreen(ctx, screen, { | |
| 69 | title: "Close account", | |
| 70 | label: `type ${user.username} to confirm`, | |
| 71 | onSubmit: async (typed) => { | |
| 72 | const r = await askToClose(f.app, user, { username: typed, proof: { password, source: f.identity.remoteAddress } }); | |
| 73 | if (r.ok) { | |
| 74 | ctx.toast(closingSays(r.at), "success"); | |
| 75 | } else { | |
| 76 | const words = r.error === "refused" ? r.refusal.says : r.error === "username" ? CLOSE_WORDS.username(user.username) : CLOSE_WORDS[r.error]; | |
| 77 | ctx.toast(words, "danger"); | |
| 78 | } | |
| 79 | ctx.invalidate(); | |
| 80 | }, | |
| 81 | }), | |
| 82 | ), | |
| 83 | }), | |
| 84 | ); | |
| 85 | }; | |
| 86 | ||
| 55 | 87 | const held = exportWords(f.app.db, f.user); |
| 56 | 88 | const screen: Screen = { |
| 57 | 89 | title: "Your profile", |
| 58 | keys: [binding(["n"], "set display name"), binding(["e"], "set email"), binding(["x"], "take my data")], | |
| 90 | keys: [binding(["n"], "set display name"), binding(["e"], "set email"), binding(["x"], "take my data"), binding(["c"], "close account"), binding(["k"], "keep account")], | |
| 59 | 91 | render(area, s) { |
| 60 | 92 | const c = ctx.theme.c; |
| 61 | 93 | const u = f.user; |
| 62 | 94 | if (u === undefined) return; |
| 63 | 95 | const email = isStandInEmail(u.email) ? "none" : u.email; |
| 96 | const closing = closeRequestedAt(f.app.db, u.id); | |
| 64 | 97 | const rows: [string, string][] = [ |
| 65 | 98 | ["Username", u.username], |
| 66 | 99 | ["Display name", u.displayName], |
| 67 | 100 | ["Email", email], |
| 68 | 101 | ["Your data", held], |
| 102 | ...(closing === null ? [] : [["Closing", `closes on ${closesOn(closing)}; k keeps it`] as [string, string]]), | |
| 69 | 103 | ]; |
| 70 | 104 | rows.forEach(([k, v], i) => { |
| 71 | 105 | const y = area.y + i; |
| @@ -86,6 +120,15 @@ export function profileScreen(f: Session, ctx: Ctx): Screen { | ||
| 86 | 120 | ); |
| 87 | 121 | return true; |
| 88 | 122 | } |
| 123 | if (e.key === "c") { | |
| 124 | askClose(); | |
| 125 | return true; | |
| 126 | } | |
| 127 | if (e.key === "k") { | |
| 128 | ctx.toast(keptSays(keepMyAccount(f.app, f.user)), "success"); | |
| 129 | ctx.invalidate(); | |
| 130 | return true; | |
| 131 | } | |
| 89 | 132 | if (e.key === "x") { |
| 90 | 133 | ctx.push(confirmScreen(ctx, screen, { title: "Your data", body: exportHow(f.app, f.user), actions: [{ label: "Close" }], onChoose: () => ctx.invalidate(), width: 76 })); |
| 91 | 134 | return true; |
test/tui-account-close.test.ts added +105
| @@ -0,0 +1,105 @@ | ||
| 1 | /** Close account in the terminal (#15714): `c` asks, `k` keeps, refusals in the web's words. */ | |
| 2 | import { afterAll, beforeAll, describe, 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 { closeRequestedAt, closesOn, CLOSE_WORDS } from "../src/actions/account_close.ts"; | |
| 7 | import { createApp, type App as Forge } from "../src/app.ts"; | |
| 8 | import { createUser, getUserByName } from "../src/core/users.ts"; | |
| 9 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 10 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 11 | import { quietLogs } from "./helpers/globals.ts"; | |
| 12 | import { settleApp } from "./helpers/tui.ts"; | |
| 13 | ||
| 14 | quietLogs(); | |
| 15 | ||
| 16 | describe("close account in the terminal", () => { | |
| 17 | let dataDir: string; | |
| 18 | let forge: Forge; | |
| 19 | ||
| 20 | function boot(username: string) { | |
| 21 | const user = getUserByName(forge.db, username)!; | |
| 22 | const { app } = createForgeApp( | |
| 23 | forge, | |
| 24 | { sshUsername: "t", remoteAddress: "127.0.0.1", user }, | |
| 25 | { terminal: headlessTerminal(140, 32), exitHooks: false }, | |
| 26 | ); | |
| 27 | app.start(); | |
| 28 | const feed = async (keys: string[], ctrl = false) => { | |
| 29 | for (const key of keys) app.dispatch({ type: "key", key, ctrl, alt: false, shift: false }); | |
| 30 | await settleApp(app); | |
| 31 | }; | |
| 32 | return { | |
| 33 | text: () => app.screen.toText(), | |
| 34 | press: (...keys: string[]) => feed(keys), | |
| 35 | type: (t: string) => feed([...t]), | |
| 36 | open: async () => { | |
| 37 | await feed(["k"], true); | |
| 38 | await feed([..."Your profile", "enter"]); | |
| 39 | }, | |
| 40 | stop: () => app.stop(), | |
| 41 | }; | |
| 42 | } | |
| 43 | ||
| 44 | beforeAll(async () => { | |
| 45 | dataDir = await mkdtemp(join(tmpdir(), "forge-tui-close-")); | |
| 46 | forge = createApp({ dataDir, port: 0 }); | |
| 47 | for (const [username, adminIfFirst] of [["alice", true], ["bob", false]] as const) { | |
| 48 | const made = await createUser(forge.db, { | |
| 49 | origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst, | |
| 50 | }); | |
| 51 | if (!made.ok) throw new Error(made.error); | |
| 52 | } | |
| 53 | }); | |
| 54 | afterAll(async () => { | |
| 55 | forge.close(); | |
| 56 | await rm(dataDir, { recursive: true, force: true }); | |
| 57 | }); | |
| 58 | ||
| 59 | const ask = async (h: ReturnType<typeof boot>, password: string, username: string) => { | |
| 60 | await h.press("c"); | |
| 61 | for (const answer of [password, username]) { | |
| 62 | await h.type(answer); | |
| 63 | await h.press("enter"); | |
| 64 | } | |
| 65 | }; | |
| 66 | ||
| 67 | test("a wrong username or password is refused and records nothing", async () => { | |
| 68 | const h = boot("bob"); | |
| 69 | await h.open(); | |
| 70 | await ask(h, "a-good-passphrase", "robert"); | |
| 71 | expect(h.text()).toContain(CLOSE_WORDS.username("bob")); | |
| 72 | await ask(h, "not-the-password", "bob"); | |
| 73 | expect(h.text()).toContain(CLOSE_WORDS.password); | |
| 74 | expect(closeRequestedAt(forge.db, getUserByName(forge.db, "bob")!.id)).toBeNull(); | |
| 75 | h.stop(); | |
| 76 | }); | |
| 77 | ||
| 78 | test("the only admin is refused in the web's words", async () => { | |
| 79 | const h = boot("alice"); | |
| 80 | await h.open(); | |
| 81 | await ask(h, "a-good-passphrase", "alice"); | |
| 82 | expect(h.text()).toContain("You are this forge's only admin"); | |
| 83 | expect(closeRequestedAt(forge.db, getUserByName(forge.db, "alice")!.id)).toBeNull(); | |
| 84 | h.stop(); | |
| 85 | }); | |
| 86 | ||
| 87 | test("c asks, the profile shows the closing day, k keeps", async () => { | |
| 88 | const bob = getUserByName(forge.db, "bob")!; | |
| 89 | const h = boot("bob"); | |
| 90 | await h.open(); | |
| 91 | await h.press("c"); | |
| 92 | await h.type("a-good-passphrase"); | |
| 93 | expect(h.text()).not.toContain("a-good-passphrase"); | |
| 94 | await h.press("enter"); | |
| 95 | await h.type("bob"); | |
| 96 | await h.press("enter"); | |
| 97 | const at = closeRequestedAt(forge.db, bob.id)!; | |
| 98 | expect(at).not.toBeNull(); | |
| 99 | expect(h.text()).toContain(`closes on ${closesOn(at)}`); | |
| 100 | await h.press("k"); | |
| 101 | expect(closeRequestedAt(forge.db, bob.id)).toBeNull(); | |
| 102 | expect(h.text()).not.toContain("closes on"); | |
| 103 | h.stop(); | |
| 104 | }); | |
| 105 | }); |
new above
offline