Public: anyone can read this; the crew can change it.
Archive, part 1: an admin archives or wakes one repository from Settings and the API, and its page says "archived on <date>" with a Wake button #1844
scout/forge:scout/1838-archive-part-1-an-admin-archives-or-wake → main 10 commits fork 4 threads · 4 unresolved
README.md +10
| @@ -193,6 +193,16 @@ taking a release is not done here. This forge publishes its own | ||
| 193 | 193 | `/releases.json` (the version names, each linking to that document), so any |
| 194 | 194 | forge can be a parent. The terminal shows the count, not the release names. |
| 195 | 195 | |
| 196 | ## Archive | |
| 197 | ||
| 198 | An admin puts a repository away from **Settings → Archive**, with | |
| 199 | `PATCH /api/v1/repos/:owner/:name {"archived":true|false}` (admins only; anyone | |
| 200 | else gets 403), or in the terminal with `a` on the About card (`I`). The | |
| 201 | repository page and the card then say when it was archived, and an admin gets | |
| 202 | a Wake button or the same key to undo it. Archiving twice keeps the first date. | |
| 203 | It does not yet block anything: clone, push and links keep working, and the | |
| 204 | repository stays in every list. | |
| 205 | ||
| 196 | 206 | ## Health |
| 197 | 207 | |
| 198 | 208 | `GET /api/v1/health` (and the MCP `health` tool) answer a site admin with |
scripts/gate.ts +2
| @@ -27,6 +27,8 @@ export type Exclusion = { | ||
| 27 | 27 | }; |
| 28 | 28 | |
| 29 | 29 | export const GATE: readonly string[] = [ |
| 30 | "test/archive.test.ts", | |
| 31 | "test/tui-archive.test.ts", | |
| 30 | 32 | "test/domain-drill.test.ts", |
| 31 | 33 | "test/accounts-retire.test.ts", |
| 32 | 34 | "test/client-home.test.ts", |
src/actions/repos.ts +3 −1
| @@ -266,7 +266,7 @@ export async function updateRepoSettings( | ||
| 266 | 266 | if (!moved.ok) return err("unknown_branch"); |
| 267 | 267 | } |
| 268 | 268 | const updated = writeSettings(app.db, repo, settings); |
| 269 | const changed = ( | |
| 269 | const changed: string[] = ( | |
| 270 | 270 | [ |
| 271 | 271 | "visibility", |
| 272 | 272 | "description", |
| @@ -278,6 +278,8 @@ export async function updateRepoSettings( | ||
| 278 | 278 | "decisionsBy", |
| 279 | 279 | ] as const |
| 280 | 280 | ).filter((k) => settings[k] !== undefined && settings[k] !== repo[k]); |
| 281 | if (settings.archived !== undefined && settings.archived !== (repo.archivedAt !== null)) | |
| 282 | changed.push("archived"); | |
| 281 | 283 | app.bus.append({ |
| 282 | 284 | kind: "repo.settings_updated", |
| 283 | 285 | repoId: repo.id, |
src/core/migrations/20261015090000-a-repository-put-away.ts added +5
| @@ -0,0 +1,5 @@ | ||
| 1 | // Archive (#1838): when an admin put a repository away. Null is the | |
| 2 | // ordinary state; nothing is deleted and clone and push go on working. | |
| 3 | export default ` | |
| 4 | ALTER TABLE repo ADD COLUMN archived_at TEXT; | |
| 5 | `; |
src/core/releases.json +5 −1
| @@ -3,7 +3,11 @@ | ||
| 3 | 3 | "releases": [ |
| 4 | 4 | { |
| 5 | 5 | "version": "0.7.123", |
| 6 | "migrations": 93 | |
| 6 | "migrations": 93, | |
| 7 | "ids": [ | |
| 8 | "20261015090000-feed-chips", | |
| 9 | "20261015230000-a-domain-drill" | |
| 10 | ] | |
| 7 | 11 | }, |
| 8 | 12 | { |
| 9 | 13 | "version": "0.7.122", |
src/core/repos.ts +13 −1
| @@ -128,6 +128,8 @@ export interface Repo { | ||
| 128 | 128 | * read only by the base's writers and crew. Null for every other one. |
| 129 | 129 | */ |
| 130 | 130 | readonly sealedFor: string | null; |
| 131 | /** When an admin archived it (ISO time), or null: put away, not deleted. */ | |
| 132 | readonly archivedAt: string | null; | |
| 131 | 133 | /** The origin this mirrors, or null for a native repo. */ |
| 132 | 134 | readonly origin: RepoOrigin | null; |
| 133 | 135 | /** |
| @@ -166,6 +168,7 @@ interface RepoRow { | ||
| 166 | 168 | direction_issue_id: string | null; |
| 167 | 169 | fork_parent_id: string | null; |
| 168 | 170 | sealed_for: string | null; |
| 171 | archived_at: string | null; | |
| 169 | 172 | origin_id: string | null; |
| 170 | 173 | origin_kind: "github" | "forgejo" | "gitea" | null; |
| 171 | 174 | origin_full_name: string | null; |
| @@ -204,6 +207,7 @@ function toRepo(row: RepoRow): Repo { | ||
| 204 | 207 | directionIssueId: row.direction_issue_id, |
| 205 | 208 | forkParentId: row.fork_parent_id, |
| 206 | 209 | sealedFor: row.sealed_for, |
| 210 | archivedAt: row.archived_at, | |
| 207 | 211 | origin: |
| 208 | 212 | row.origin_id !== null && row.origin_kind !== null |
| 209 | 213 | ? { |
| @@ -1036,6 +1040,8 @@ export interface RepoSettings { | ||
| 1036 | 1040 | /** Whether someone who cannot write here may start or steer a run. */ |
| 1037 | 1041 | crewOutsiders?: boolean; |
| 1038 | 1042 | decisionsBy?: DecisionsBy; |
| 1043 | /** Archive (true) or wake (false); the time is stamped here. */ | |
| 1044 | archived?: boolean; | |
| 1039 | 1045 | } |
| 1040 | 1046 | |
| 1041 | 1047 | /** Update the mutable repo fields, leaving unspecified ones untouched. */ |
| @@ -1048,7 +1054,8 @@ export function updateRepoSettings( | ||
| 1048 | 1054 | `UPDATE repo SET visibility = ?1, description = ?2, protect_default = ?3, |
| 1049 | 1055 | default_branch = ?4, require_review = ?5, require_checks = ?7, |
| 1050 | 1056 | autonomy = ?8, autonomy_direction = ?9, autonomy_ceiling_usd = ?10, |
| 1051 | autonomy_slots = ?11, autonomy_by = ?12, crew_outsiders = ?13, decisions_by = ?14 | |
| 1057 | autonomy_slots = ?11, autonomy_by = ?12, crew_outsiders = ?13, decisions_by = ?14, | |
| 1058 | archived_at = ?15 | |
| 1052 | 1059 | WHERE id = ?6`, |
| 1053 | 1060 | ).run( |
| 1054 | 1061 | settings.visibility ?? repo.visibility, |
| @@ -1065,6 +1072,11 @@ export function updateRepoSettings( | ||
| 1065 | 1072 | settings.autonomyBy === undefined ? repo.autonomyBy : settings.autonomyBy, |
| 1066 | 1073 | (settings.crewOutsiders ?? repo.crewOutsiders) ? 1 : 0, |
| 1067 | 1074 | settings.decisionsBy ?? repo.decisionsBy, |
| 1075 | settings.archived === undefined | |
| 1076 | ? repo.archivedAt | |
| 1077 | : settings.archived | |
| 1078 | ? (repo.archivedAt ?? new Date().toISOString()) | |
| 1079 | : null, | |
| 1068 | 1080 | ); |
| 1069 | 1081 | return getRepoById(db, repo.id)!; |
| 1070 | 1082 | } |
src/http/api.ts +7
| @@ -1814,6 +1814,9 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1814 | 1814 | const repo = loadRepo(ctx); |
| 1815 | 1815 | if (!repo) return notFound(); |
| 1816 | 1816 | const input = await body(ctx.request); |
| 1817 | // The caller can already read this repository, so saying no is no leak. | |
| 1818 | if (input.archived !== undefined && !canAdmin(ctx.app.db, repo, ctx.auth.user)) | |
| 1819 | return apiError("forbidden", 403, "only an admin archives or wakes"); | |
| 1817 | 1820 | if (input.decisionsBy !== undefined && !isDecisionsBy(input.decisionsBy)) |
| 1818 | 1821 | return apiError( |
| 1819 | 1822 | "bad_decisions_by", |
| @@ -1847,6 +1850,10 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1847 | 1850 | ...(isDecisionsBy(input.decisionsBy) |
| 1848 | 1851 | ? { decisionsBy: input.decisionsBy } |
| 1849 | 1852 | : {}), |
| 1853 | // Archive or wake (#1838): any admin's call, and only a real boolean. | |
| 1854 | ...(typeof input.archived === "boolean" | |
| 1855 | ? { archived: input.archived } | |
| 1856 | : {}), | |
| 1850 | 1857 | }); |
| 1851 | 1858 | if (!updated.ok) return respondAccess(updated.error, 422); |
| 1852 | 1859 | return json(repoJson(ctx, updated.value)); |
src/http/server.ts +2
| @@ -326,6 +326,7 @@ import { | ||
| 326 | 326 | settingsPage, |
| 327 | 327 | settingsUpdate, |
| 328 | 328 | waveCut, |
| 329 | archiveSubmit, | |
| 329 | 330 | webhookCreate, |
| 330 | 331 | webhookDelete, |
| 331 | 332 | } from "../web/pages/settings.ts"; |
| @@ -1139,6 +1140,7 @@ export function buildRouter(): Router<Handler> { | ||
| 1139 | 1140 | router.add("GET", "/:owner/:name/settings", settingsPage); |
| 1140 | 1141 | router.add("POST", "/:owner/:name/settings", settingsUpdate); |
| 1141 | 1142 | router.add("POST", "/:owner/:name/settings/wave", waveCut); |
| 1143 | router.add("POST", "/:owner/:name/settings/archive", archiveSubmit); | |
| 1142 | 1144 | router.add("GET", "/:owner/:name/settings/app", appSettingsPage); |
| 1143 | 1145 | router.add("POST", "/:owner/:name/settings/app", appSettingsUpdate); |
| 1144 | 1146 | router.add("POST", "/:owner/:name/settings/app/disable", appDisable); |
src/tui/screens/about.ts +48 −5
| @@ -18,7 +18,8 @@ | ||
| 18 | 18 | import { isPinned } from "../../core/pins.ts"; |
| 19 | 19 | import { parentReleases, releasesSentence } from "../../core/parent_releases.ts"; |
| 20 | 20 | import { running } from "../../kernel/version.ts"; |
| 21 | import type { Repo } from "../../core/repos.ts"; | |
| 21 | import { updateRepoSettings } from "../../actions/repos.ts"; | |
| 22 | import { canAdmin, getRepoById, type Repo } from "../../core/repos.ts"; | |
| 22 | 23 | import type { RepoFacts, RepoSource } from "../../view/source.ts"; |
| 23 | 24 | import type { Ctx, Screen } from "../app/app.ts"; |
| 24 | 25 | import { binding } from "../app/keys.ts"; |
| @@ -43,15 +44,37 @@ export function aboutScreen( | ||
| 43 | 44 | facts: RepoFacts | undefined, |
| 44 | 45 | ref: string, |
| 45 | 46 | ): Screen { |
| 46 | const repo = localRepo(src); | |
| 47 | const rows = aboutFacts(f, src, facts, ref); | |
| 47 | const held = localRepo(src); | |
| 48 | // Archiving changes the row under this card, so read it at use. | |
| 49 | let repo = held; | |
| 50 | const may = (): boolean => | |
| 51 | repo !== undefined && | |
| 52 | f.user !== undefined && | |
| 53 | f.user.kind === "user" && | |
| 54 | canAdmin(f.db, repo, f.user); | |
| 55 | const factRows = (): [string, string][] => [ | |
| 56 | ...archivedRow(repo), | |
| 57 | ...aboutFacts(f, src, facts, ref), | |
| 58 | ]; | |
| 59 | let rows = factRows(); | |
| 48 | 60 | const cloneUrl = rows.find(([label]) => label.startsWith("clone"))?.[1] ?? ""; |
| 49 | 61 | return { |
| 50 | 62 | title: "About", |
| 51 | keys: [ | |
| 63 | get keys() { | |
| 64 | return [ | |
| 52 | 65 | binding(["y"], "copy the clone URL", { disabled: cloneUrl === "" }), |
| 66 | // Only an admin is offered the key, and only an admin's press counts. | |
| 67 | ...(may() | |
| 68 | ? [ | |
| 69 | binding( | |
| 70 | ["a"], | |
| 71 | repo?.archivedAt === null ? "archive" : "wake", | |
| 72 | ), | |
| 73 | ] | |
| 74 | : []), | |
| 53 | 75 | binding(["esc"], "close"), |
| 54 | ], | |
| 76 | ]; | |
| 77 | }, | |
| 55 | 78 | render(area, s) { |
| 56 | 79 | parent.render(area, s); |
| 57 | 80 | const c = ctx.theme.c; |
| @@ -128,6 +151,20 @@ export function aboutScreen( | ||
| 128 | 151 | ctx.toast("copied the clone URL", "success"); |
| 129 | 152 | return true; |
| 130 | 153 | } |
| 154 | if (e.key === "a" && repo !== undefined && may()) { | |
| 155 | const waking = repo.archivedAt !== null; | |
| 156 | void updateRepoSettings(f.app, f.user, repo, { | |
| 157 | archived: !waking, | |
| 158 | }).then((done) => { | |
| 159 | if (done.ok) { | |
| 160 | repo = getRepoById(f.db, done.value.id) ?? done.value; | |
| 161 | rows = factRows(); | |
| 162 | ctx.toast(waking ? "awake again" : "archived", "success"); | |
| 163 | } else ctx.toast("that could not be changed", "danger"); | |
| 164 | ctx.invalidate(); | |
| 165 | }); | |
| 166 | return true; | |
| 167 | } | |
| 131 | 168 | if (e.key === "esc" || e.key === "q" || e.key === "I") { |
| 132 | 169 | ctx.pop(); |
| 133 | 170 | return true; |
| @@ -137,6 +174,12 @@ export function aboutScreen( | ||
| 137 | 174 | }; |
| 138 | 175 | } |
| 139 | 176 | |
| 177 | /** One line when an admin has put the repository away. */ | |
| 178 | function archivedRow(repo: Repo | undefined): [string, string][] { | |
| 179 | if (repo === undefined || repo.archivedAt === null) return []; | |
| 180 | return [["archived", repo.archivedAt.slice(0, 10)]]; | |
| 181 | } | |
| 182 | ||
| 140 | 183 | /** |
| 141 | 184 | * The web following card's releases sentence, for a fork on a forge that |
| 142 | 185 | * names a parent forge: the same words, from the same cache. |
src/web/pages/repo.ts +24 −2
| @@ -920,6 +920,28 @@ export async function repoFiles(ctx: Ctx): Promise<Response> { | ||
| 920 | 920 | return await codeHome(ctx, repo); |
| 921 | 921 | } |
| 922 | 922 | |
| 923 | /** | |
| 924 | * "Archived on <date>" with, for an admin, the Wake button. A repository put | |
| 925 | * away still clones, pushes and links as before; this only says so. | |
| 926 | */ | |
| 927 | function archivedLine(ctx: Ctx, repo: Repo): Html | null { | |
Fine on the page. The terminal needs the same thing: the About screen should show the archived line, and an admin should get a key that wakes the repository through |
||
| 928 | if (repo.archivedAt === null) return null; | |
| 929 | return html`<div class="mt archived"> | |
| 930 | <p> | |
| 931 | Archived on | |
| 932 | <time datetime="${repo.archivedAt}">${repo.archivedAt.slice(0, 10)}</time> | |
| 933 | </p> | |
| 934 | ${when( | |
| 935 | canAdmin(ctx.app.db, repo, ctx.auth.user), | |
| 936 | () => html`<form method="post" action="${repoBase(repo)}/settings/archive"> | |
| 937 | <input type="hidden" name="archived" value="0" /> | |
| 938 | <input type="hidden" name="to" value="repo" /> | |
| 939 | ${btn("Wake", { size: "sm" })} | |
| 940 | </form>`, | |
| 941 | )} | |
| 942 | </div>` | |
| 943 | } | |
| 944 | ||
| 923 | 945 | /** The code at a ref: the tree, the README and the About rail — with, in |
| 924 | 946 | * the studio, the offer to make a repository that is not an app one. */ |
| 925 | 947 | async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> { |
| @@ -930,7 +952,7 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> { | ||
| 930 | 952 | const refs = await refSetFrom(scope.source); |
| 931 | 953 | |
| 932 | 954 | if (refs.branches.length === 0) { |
| 933 | const body = html`${scopeHeader(ctx, scope, "code")}${noApp} | |
| 955 | const body = html`${scopeHeader(ctx, scope, "code")}${archivedLine(ctx, repo)}${noApp} | |
| 934 | 956 | <div class="mt"> |
| 935 | 957 | ${empty({ |
| 936 | 958 | icon: "repo", |
| @@ -989,7 +1011,7 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> { | ||
| 989 | 1011 | const following = await followingOf(ctx, repo); |
| 990 | 1012 | const body = html`${scopeHeader(ctx, scope, "code", { |
| 991 | 1013 | branchCount: refs.branches.length, |
| 992 | })}${noApp} | |
| 1014 | })}${archivedLine(ctx, repo)}${noApp} | |
| 993 | 1015 | ${frozenBlock(ctx, repo)} ${appStrip(ctx, repo)} |
| 994 | 1016 | ${following === undefined |
| 995 | 1017 | ? forkBar(ctx, repo) |
src/web/pages/settings.ts +45 −1
| @@ -320,12 +320,28 @@ export async function settingsPage(ctx: Ctx): Promise<Response> { | ||
| 320 | 320 | </form>`, |
| 321 | 321 | { head: html`${icon("rocket")}<h2>Cut a wave</h2>` }, |
| 322 | 322 | ); |
| 323 | const archive = card( | |
| 324 | html`<form method="post" action="${base}/settings/archive"> | |
| 325 | <input type="hidden" name="archived" value="${repo.archivedAt === null ? "1" : "0"}" /> | |
| 326 | <p class="muted"> | |
| 327 | ${repo.archivedAt === null | |
| 328 | ? "Put a finished repository away without deleting it. Its page says when it was archived; links, clone and push keep working." | |
| 329 | : html`Archived on ${repo.archivedAt.slice(0, 10)}. Wake it to take the label off.`} | |
| 330 | </p> | |
| 331 | <div class="form-actions"> | |
| 332 | ${btn(repo.archivedAt === null ? "Archive" : "Wake", { | |
| 333 | variant: "primary", | |
| 334 | })} | |
| 335 | </div> | |
| 336 | </form>`, | |
| 337 | { head: html`${icon("alert")}<h2>Archive</h2>` }, | |
| 338 | ); | |
| 323 | 339 | return settingsPageResponse( |
| 324 | 340 | ctx, |
| 325 | 341 | repo, |
| 326 | 342 | "general", |
| 327 | 343 | "Settings", |
| 328 | html`${lead}<div class="mt">${body}</div><div class="mt">${wave}</div>${secrets | |
| 344 | html`${lead}<div class="mt">${body}</div><div class="mt">${wave}</div><div class="mt">${archive}</div>${secrets | |
| 329 | 345 | ? html`<div class="mt">${secrets}</div>` |
| 330 | 346 | : null}`, |
| 331 | 347 | ); |
| @@ -381,6 +397,34 @@ export async function settingsUpdate(ctx: Ctx): Promise<Response> { | ||
| 381 | 397 | return flashTo(ctx, `${base}/settings`, flash); |
| 382 | 398 | } |
| 383 | 399 | |
| 400 | /** | |
| 401 | * POST /:owner/:name/settings/archive — archive or wake. The state asked | |
| 402 | * for travels in the form, so a double submit is idempotent. | |
| 403 | */ | |
| 404 | export async function archiveSubmit(ctx: Ctx): Promise<Response> { | |
| 405 | const gate = await adminForm(ctx); | |
| 406 | if (gate instanceof Response) return gate; | |
| 407 | const { repo, form } = gate; | |
| 408 | const asked = field(form, "archived"); | |
outdated
Any value other than "1" counts as a wake. That works because the form only ever sends "0" or "1". Validating at the boundary means refusing anything else, as the API does with its boolean check: |
||
| 409 | if (asked !== "0" && asked !== "1") | |
| 410 | return flashTo(ctx, `${repoBase(repo)}/settings`, { | |
| 411 | tone: "danger", | |
| 412 | message: "That could not be changed.", | |
| 413 | }); | |
| 414 | const archived = asked === "1"; | |
| 415 | const updated = await updateRepoSettings(ctx.app, ctx.auth.user, repo, { | |
| 416 | archived, | |
| 417 | }); | |
| 418 | const back = field(form, "to") === "repo" ? repoBase(repo) : `${repoBase(repo)}/settings`; | |
| 419 | return flashTo( | |
| 420 | ctx, | |
| 421 | back, | |
| 422 | updated.ok | |
| 423 | ? { message: archived ? "Archived." : "Awake again." } | |
| 424 | : { tone: "danger", message: "That could not be changed." }, | |
| 425 | ); | |
| 426 | } | |
| 427 | ||
| 384 | 428 | /** POST /:owner/:name/settings/wave — open the draft of the next wave. */ |
| 385 | 429 | export async function waveCut(ctx: Ctx): Promise<Response> { |
| 386 | 430 | const gate = await adminForm(ctx); |
test/archive.test.ts added +132
| @@ -0,0 +1,132 @@ | ||
| 1 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 2 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { createApp, type App } from "../src/app.ts"; | |
| 6 | import { getRepo } from "../src/core/repos.ts"; | |
| 7 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 8 | ||
| 9 | type Row = { archivedAt: string | null }; | |
| 10 | ||
| 11 | describe("archive and wake", () => { | |
| 12 | let dataDir: string; | |
| 13 | let app: App; | |
| 14 | let servers: Servers; | |
| 15 | let base: string; | |
| 16 | let owner = ""; | |
| 17 | let stranger = ""; | |
| 18 | ||
| 19 | const form = (fields: Record<string, string>) => new URLSearchParams(fields); | |
| 20 | ||
| 21 | async function signup(username: string): Promise<string> { | |
| 22 | const res = await fetch(`${base}/signup`, { | |
| 23 | method: "POST", | |
| 24 | body: form({ | |
| 25 | username, | |
| 26 | email: `${username}@f.dev`, | |
| 27 | password: "archive-password", | |
| 28 | }), | |
| 29 | redirect: "manual", | |
| 30 | }); | |
| 31 | return (res.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 32 | } | |
| 33 | ||
| 34 | const patch = (cookie: string, archived: boolean) => | |
| 35 | fetch(`${base}/api/v1/repos/ada/old`, { | |
| 36 | method: "PATCH", | |
| 37 | headers: { cookie, "content-type": "application/json", origin: base }, | |
| 38 | body: JSON.stringify({ archived }), | |
| 39 | }); | |
| 40 | ||
| 41 | beforeAll(async () => { | |
| 42 | dataDir = await mkdtemp(join(tmpdir(), "forge-archive-")); | |
| 43 | app = createApp({ dataDir, port: 0 }); | |
| 44 | servers = await serve(app); | |
| 45 | base = servers.url; | |
| 46 | owner = await signup("ada"); | |
| 47 | stranger = await signup("bob"); | |
| 48 | await fetch(`${base}/new`, { | |
| 49 | method: "POST", | |
| 50 | headers: { cookie: owner }, | |
| 51 | body: form({ name: "old", visibility: "public", description: "done" }), | |
| 52 | redirect: "manual", | |
| 53 | }); | |
| 54 | }); | |
| 55 | ||
| 56 | afterAll(async () => { | |
| 57 | servers.stop(); | |
| 58 | app.close(); | |
| 59 | await rm(dataDir, { recursive: true, force: true }); | |
| 60 | }); | |
| 61 | ||
| 62 | test("a repository starts unarchived, and the JSON says so", async () => { | |
| 63 | const res = await fetch(`${base}/api/v1/repos/ada/old`, { | |
| 64 | headers: { cookie: owner }, | |
| 65 | }); | |
| 66 | expect(((await res.json()) as Row).archivedAt).toBeNull(); | |
| 67 | }); | |
| 68 | ||
| 69 | test("the API archives and wakes it", async () => { | |
| 70 | const archived = await patch(owner, true); | |
| 71 | expect(archived.status).toBe(200); | |
| 72 | const stamp = ((await archived.json()) as Row).archivedAt; | |
| 73 | expect(Date.parse(stamp ?? "")).not.toBeNaN(); | |
| 74 | expect(getRepo(app.db, "ada", "old")?.archivedAt).toBe(stamp); | |
| 75 | ||
| 76 | // Archiving again keeps the first date. | |
| 77 | expect(((await (await patch(owner, true)).json()) as Row).archivedAt).toBe(stamp); | |
| 78 | ||
| 79 | const woke = await patch(owner, false); | |
| 80 | expect(((await woke.json()) as Row).archivedAt).toBeNull(); | |
| 81 | expect(getRepo(app.db, "ada", "old")?.archivedAt).toBeNull(); | |
| 82 | }); | |
| 83 | ||
| 84 | test("a non-admin is refused with 403 and nothing changes", async () => { | |
| 85 | const res = await patch(stranger, true); | |
| 86 | expect(res.status).toBe(403); | |
| 87 | expect(getRepo(app.db, "ada", "old")?.archivedAt).toBeNull(); | |
| 88 | }); | |
| 89 | ||
| 90 | test("the Settings form archives, the page says so, and Wake clears it", async () => { | |
| 91 | const post = (cookie: string, fields: Record<string, string>, origin = base) => | |
| 92 | fetch(`${base}/ada/old/settings/archive`, { | |
| 93 | method: "POST", | |
| 94 | headers: { cookie, origin }, | |
| 95 | body: form(fields), | |
| 96 | redirect: "manual", | |
| 97 | }); | |
| 98 | ||
| 99 | // Cross-origin: refused, unchanged. | |
| 100 | const cross = await post(owner, { archived: "1" }, "http://evil.example"); | |
| 101 | expect(cross.status).toBe(403); | |
| 102 | expect(getRepo(app.db, "ada", "old")?.archivedAt).toBeNull(); | |
| 103 | ||
| 104 | // A stranger's form is not an admin's. | |
| 105 | expect((await post(stranger, { archived: "1" })).status).toBeGreaterThanOrEqual(400); | |
| 106 | expect(getRepo(app.db, "ada", "old")?.archivedAt).toBeNull(); | |
| 107 | ||
| 108 | expect((await post(owner, { archived: "1" })).status).toBe(303); | |
| 109 | const at = getRepo(app.db, "ada", "old")!.archivedAt!; | |
| 110 | const day = at.slice(0, 10); | |
| 111 | ||
| 112 | const asOwner = await ( | |
| 113 | await fetch(`${base}/ada/old`, { headers: { cookie: owner } }) | |
| 114 | ).text(); | |
| 115 | expect(asOwner).toContain(`Archived on`); | |
| 116 | expect(asOwner).toContain(day); | |
| 117 | expect(asOwner).toContain("Wake"); | |
| 118 | ||
| 119 | const asStranger = await ( | |
| 120 | await fetch(`${base}/ada/old`, { headers: { cookie: stranger } }) | |
| 121 | ).text(); | |
| 122 | expect(asStranger).toContain("Archived on"); | |
| 123 | expect(asStranger).not.toContain("/settings/archive"); | |
| 124 | ||
| 125 | expect((await post(owner, { archived: "0", to: "repo" })).status).toBe(303); | |
| 126 | expect(getRepo(app.db, "ada", "old")?.archivedAt).toBeNull(); | |
| 127 | const woken = await ( | |
| 128 | await fetch(`${base}/ada/old`, { headers: { cookie: owner } }) | |
| 129 | ).text(); | |
| 130 | expect(woken).not.toContain("Archived on"); | |
| 131 | }); | |
| 132 | }); |
test/tui-archive.test.ts added +114
| @@ -0,0 +1,114 @@ | ||
| 1 | /** Archive in the terminal (#1838): About shows it, an admin's `a` flips it. */ | |
| 2 | ||
| 3 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 4 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 5 | import { tmpdir } from "node:os"; | |
| 6 | import { join } from "node:path"; | |
| 7 | import { updateRepoSettings } from "../src/actions/repos.ts"; | |
| 8 | import { createRepo } from "../src/actions/repos.ts"; | |
| 9 | import { signUp } from "../src/actions/users.ts"; | |
| 10 | import { createApp, type App as Forge } from "../src/app.ts"; | |
| 11 | import { getRepo, type Repo } from "../src/core/repos.ts"; | |
| 12 | import type { User } from "../src/core/users.ts"; | |
| 13 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 14 | import type { TuiIdentity } from "../src/tui/session.ts"; | |
| 15 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 16 | import { quietLogs } from "./helpers/globals.ts"; | |
| 17 | import { settleApp, settleUntil } from "./helpers/tui.ts"; | |
| 18 | ||
| 19 | quietLogs(); | |
| 20 | ||
| 21 | describe("archive in the terminal", () => { | |
| 22 | let dataDir: string; | |
| 23 | let forge: Forge; | |
| 24 | let alice: User; | |
| 25 | let repo: Repo; | |
| 26 | ||
| 27 | function boot(identity: TuiIdentity) { | |
| 28 | const terminal = headlessTerminal(120, 40); | |
| 29 | const { app } = createForgeApp(forge, identity, { | |
| 30 | terminal, | |
| 31 | exitHooks: false, | |
| 32 | }); | |
| 33 | app.start(); | |
| 34 | const press = async (...keys: string[]): Promise<void> => { | |
| 35 | for (const key of keys) | |
| 36 | app.dispatch({ type: "key", key, ctrl: false, alt: false, shift: false }); | |
| 37 | await settleApp(app); | |
| 38 | }; | |
| 39 | return { app, press, text: () => app.screen.toText() }; | |
| 40 | } | |
| 41 | ||
| 42 | async function openAbout(h: ReturnType<typeof boot>): Promise<void> { | |
| 43 | await settleApp(h.app); | |
| 44 | await h.press("/"); | |
| 45 | for (const ch of "beacon") await h.press(ch); | |
| 46 | await h.press("enter"); | |
| 47 | await h.press("enter"); | |
| 48 | await settleUntil(h.app, "alice/beacon"); | |
| 49 | await h.press("I"); | |
| 50 | await settleUntil(h.app, "clone (http)"); | |
| 51 | } | |
| 52 | ||
| 53 | beforeAll(async () => { | |
| 54 | dataDir = await mkdtemp(join(tmpdir(), "forge-tui-archive-")); | |
| 55 | forge = createApp({ dataDir, port: 0 }); | |
| 56 | const a = await signUp(forge, { | |
| 57 | username: "alice", | |
| 58 | email: "[email protected]", | |
| 59 | password: "a-long-passphrase", | |
| 60 | }); | |
| 61 | if (!a.ok) throw new Error("signup failed"); | |
| 62 | alice = a.value; | |
| 63 | const made = await createRepo(forge, alice, { | |
| 64 | name: "beacon", | |
| 65 | visibility: "public", | |
| 66 | }); | |
| 67 | if (!made.ok) throw new Error("repo failed"); | |
| 68 | repo = made.value; | |
| 69 | }); | |
| 70 | ||
| 71 | afterAll(async () => { | |
| 72 | await rm(dataDir, { recursive: true, force: true }); | |
| 73 | }); | |
| 74 | ||
| 75 | test("an admin archives and wakes from About with `a`", async () => { | |
| 76 | const h = boot({ | |
| 77 | sshUsername: "alice", | |
| 78 | remoteAddress: "127.0.0.1", | |
| 79 | fingerprint: "SHA256:test", | |
| 80 | user: alice, | |
| 81 | }); | |
| 82 | try { | |
| 83 | await openAbout(h); | |
| 84 | expect(h.text()).not.toContain("archived"); | |
| 85 | await h.press("a"); | |
| 86 | expect(getRepo(forge.db, "alice", "beacon")?.archivedAt).not.toBeNull(); | |
| 87 | expect(h.text()).toMatch(/archived +20\d\d-/); | |
| 88 | await h.press("a"); | |
| 89 | expect(getRepo(forge.db, "alice", "beacon")?.archivedAt).toBeNull(); | |
| 90 | expect(h.text()).not.toMatch(/archived +20\d\d-/); | |
| 91 | } finally { | |
| 92 | h.app.stop(); | |
| 93 | } | |
| 94 | }); | |
| 95 | ||
| 96 | test("a guest sees the archived line but `a` does nothing", async () => { | |
| 97 | const done = await updateRepoSettings(forge, alice, repo, { archived: true }); | |
| 98 | expect(done.ok).toBe(true); | |
| 99 | const h = boot({ | |
| 100 | sshUsername: "git", | |
| 101 | remoteAddress: "127.0.0.1", | |
| 102 | fingerprint: "SHA256:unknown", | |
| 103 | }); | |
| 104 | try { | |
| 105 | await openAbout(h); | |
| 106 | expect(h.text()).toMatch(/archived +20\d\d-/); | |
| 107 | expect(h.text()).not.toMatch(/\ba +(wake|archive)\b/); | |
| 108 | await h.press("a"); | |
| 109 | expect(getRepo(forge.db, "alice", "beacon")?.archivedAt).not.toBeNull(); | |
| 110 | } finally { | |
| 111 | h.app.stop(); | |
| 112 | } | |
| 113 | }); | |
| 114 | }); |
Drop this. It rewrites the record of a release that has already shipped, from a guess, and that record belongs to the release flow, not to an archive feature.