Public: anyone can read this; the crew can change it.
/people lists people as rows by last activity, with a plain word for each one's role #13086
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/13011-people-lists-people-as-rows-by-last-acti → main 11 commits fork 3 threads · 3 unresolved
9 files
changed +306
−37
docs/features/README.md +1
| @@ -107,6 +107,7 @@ Read with the guide page [Tell the crew](../guide-tell-the-crew.md). | ||
| 107 | 107 | - [](flows.md) |
| 108 | 108 | - [](admin-flows.md) |
| 109 | 109 | - [](team-roles.md) |
| 110 | - [](people.md) | |
| 110 | 111 | - [](file-holds.md) |
| 111 | 112 | - [](maintain.md) |
| 112 | 113 | - [](asks-mute-undo.md) |
docs/features/people.md added +14
| @@ -0,0 +1,14 @@ | ||
| 1 | # People | |
| 2 | ||
| 3 | `/settings/people` (an owner's page) lists everyone with an account, the person seen most recently first and | |
| 4 | never-seen last. Each row has the name, a plain word for the role — owner, member, guest or crew member — the | |
| 5 | monthly allowance, when they were last seen (a browser session's last request or a token's last use) and their apps. | |
| 6 | One line of filters (`?role=owner|member|guest|crew`) narrows the list; a filter that names nobody says "Nobody here." | |
| 7 | ||
| 8 | Try it: open `/settings/people`, follow **guests**, then give a guest a starter allowance with **Make member**. | |
| 9 | ||
| 10 | The terminal (both doors): Palette › People shows the same rows in the same order, from the same query | |
| 11 | (`peopleByActivity` in `src/core/people_rows.ts`). `f` steps the filter through everyone, owner, member, guest and | |
| 12 | crew member; `r` changes a role, `s` suspends or lifts, `v` verifies an email, as before. | |
| 13 | ||
| 14 | Pinned by `test/people-rows.test.ts` and `test/tui-people-activity.test.ts`. |
src/core/people_rows.ts added +59
| @@ -0,0 +1,59 @@ | ||
| 1 | /** | |
| 2 | * People as rows by last activity: name, a plain word for the role, last seen. | |
| 3 | * "owner" is an admin, "crew member" an agent; no ids leave this module. Each | |
| 4 | * row also carries allowance, suspension and email standing, so a page or | |
| 5 | * screen needs this one query. Last seen is the latest of a browser session's last request | |
| 6 | * and a token's last use, else null ("never"). Pure data in, plain data out. | |
| 7 | */ | |
| 8 | ||
| 9 | import type { Database } from "bun:sqlite"; | |
| 10 | ||
| 11 | export const WORDS = ["owner", "member", "guest", "crew member"] as const; | |
| 12 | export type RoleWord = (typeof WORDS)[number]; | |
| 13 | ||
| 14 | export interface PersonRow { | |
| 15 | readonly username: string; | |
| 16 | readonly word: RoleWord; | |
| 17 | /** A model allowance in dollars a month; null for none, an owner or a crew member. */ | |
| 18 | readonly usdPerMonth: number | null; | |
| 19 | /** When the owner suspended the account; null while it is in good standing. */ | |
| 20 | readonly suspendedAt: string | null; | |
| 21 | /** Whether the email is proven (`core/verify.ts`). */ | |
| 22 | readonly verified: boolean; | |
| 23 | /** ISO instant of the latest sign of life, or null when there has been none. */ | |
| 24 | readonly lastSeen: string | null; | |
| 25 | } | |
| 26 | ||
| 27 | /** The filter a query value names, or null for everyone. */ | |
| 28 | export function wordFilter(raw: string | null | undefined): RoleWord | null { | |
| 29 | const v = (raw ?? "").trim().toLowerCase(); | |
| 30 | return v === "crew" ? "crew member" : (WORDS as readonly string[]).includes(v) ? (v as RoleWord) : null; | |
| 31 | } | |
| 32 | ||
| 33 | /** Every person and crew member, most recently seen first; never-seen last, newest first. */ | |
| 34 | export function peopleByActivity(db: Database, only: RoleWord | null = null): PersonRow[] { | |
| 35 | const rows = db | |
| 36 | .query<{ username: string; is_admin: number; is_agent: number; member: string | null; seen: string | null; usd: number | null; suspended_at: string | null; verified_at: string | null }, []>( | |
| 37 | `SELECT u.username, u.is_admin, u.is_agent, m.user_id AS member, g.usd_per_month AS usd, u.suspended_at, c.verified_at, | |
| 38 | MAX(COALESCE((SELECT MAX(s.last_seen_at) FROM session s WHERE s.user_id = u.id), ''), | |
| 39 | COALESCE((SELECT MAX(t.last_used_at) FROM token t WHERE t.user_id = u.id), '')) AS seen | |
| 40 | FROM user u LEFT JOIN member m ON m.user_id = u.id | |
| 41 | LEFT JOIN model_grant g ON g.user_id = u.id | |
| 42 | LEFT JOIN email_check c ON c.user_id = u.id | |
| 43 | WHERE u.kind = 'user' AND u.removed_at IS NULL | |
| 44 | ORDER BY (seen = '') , seen DESC, u.created_at DESC, u.username`, | |
| 45 | ) | |
| 46 | .all(); | |
| 47 | const out = rows.map((r): PersonRow => { | |
| 48 | const word: RoleWord = r.is_agent === 1 ? "crew member" : r.is_admin === 1 ? "owner" : r.member !== null ? "member" : "guest"; | |
| 49 | return { | |
| 50 | username: r.username, | |
| 51 | word, | |
| 52 | usdPerMonth: word === "member" || word === "guest" ? r.usd : null, | |
| 53 | suspendedAt: r.suspended_at, | |
| 54 | verified: r.verified_at !== null, | |
| 55 | lastSeen: r.seen === null || r.seen === "" ? null : r.seen, | |
| 56 | }; | |
| 57 | }); | |
| 58 | return only === null ? out : out.filter((p) => p.word === only); | |
| 59 | } |
src/tui/screens/people.ts +28 −10
| @@ -15,7 +15,7 @@ import { ADMIN_WORDS } from "../../actions/admin_words.ts"; | ||
| 15 | 15 | import { lastOwnerSays } from "../../core/orgs.ts"; |
| 16 | 16 | import { setRole } from "../../actions/roles.ts"; |
| 17 | 17 | import { AMOUNT_REFUSED } from "../../actions/model_words.ts"; |
| 18 | import { listPeople, type Person } from "../../core/roles.ts"; | |
| 18 | import { peopleByActivity, WORDS, type PersonRow, type RoleWord } from "../../core/people_rows.ts"; | |
| 19 | 19 | import type { Ctx, Screen } from "../app/app.ts"; |
| 20 | 20 | import { binding } from "../app/keys.ts"; |
| 21 | 21 | import type { Session } from "../session.ts"; |
| @@ -32,14 +32,22 @@ import type { PaletteEntry } from "./entries.ts"; | ||
| 32 | 32 | export const NO_MAIL_LINE = "Newcomers fork and open pulls unverified. v verifies, or set MAIL_SMTP_URL."; |
| 33 | 33 | |
| 34 | 34 | /** One person's row: name, role, allowance, and since when they are suspended. */ |
| 35 | function rowOf(p: Person): string { | |
| 35 | export function rowOf(p: PersonRow): string { | |
| 36 | 36 | const allowance = p.usdPerMonth === null ? "" : ` $${p.usdPerMonth.toFixed(2)} a month`; |
| 37 | const seen = p.lastSeen === null ? "never seen" : `seen ${p.lastSeen.slice(0, 10)}`; | |
| 37 | 38 | const suspended = p.suspendedAt === null ? "" : ` suspended ${p.suspendedAt.slice(0, 10)}`; |
outdated
Both doors: only the word changes. The terminal still shows no last seen, no activity order, no crew and no filter. Build the screen from peopleByActivity and add a test. |
||
| 38 | return `${p.username} ${p.role}${allowance}${suspended}`; | |
| 39 | return `${p.username} ${p.word}${allowance} ${seen}${suspended}`; | |
| 39 | 40 | } |
| 40 | 41 | |
| 42 | /** The page's filter line: `f` steps through everyone and each word. */ | |
| 43 | export const FILTERS: readonly (RoleWord | null)[] = [null, ...WORDS]; | |
| 44 | export const filterLine = (only: RoleWord | null): string => | |
| 45 | `Show: ${FILTERS.map((w) => (w === only ? `[${w ?? "everyone"}]` : (w ?? "everyone"))).join(" · ")} (f changes)`; | |
| 46 | ||
| 41 | 47 | export function peopleScreen(f: Session, ctx: Ctx): Screen { |
| 42 | 48 | const list = new ListState(); |
| 49 | let only: RoleWord | null = null; | |
| 50 | const people = (): PersonRow[] => peopleByActivity(f.app.db, only); | |
| 43 | 51 | // Read once per open: the answer lists the forge's secrets. |
| 44 | 52 | const noMail = !mailConfigured(f.app); |
| 45 | 53 | const verify = (name: string): void => { |
| @@ -83,9 +91,9 @@ export function peopleScreen(f: Session, ctx: Ctx): Screen { | ||
| 83 | 91 | }; |
| 84 | 92 | const screen: Screen = { |
| 85 | 93 | title: "People", |
| 86 | keys: [binding(["r"], "change a role"), binding(["s"], "suspend or lift"), binding(["v"], "verify"), binding(["esc"], "back")], | |
| 94 | keys: [binding(["f"], "filter"), binding(["r"], "change a role"), binding(["s"], "suspend or lift"), binding(["v"], "verify"), binding(["esc"], "back")], | |
| 87 | 95 | render(area, s) { |
| 88 | const rows: ListRow[] = listPeople(f.app.db).map((p) => ({ | |
| 96 | const rows: ListRow[] = people().map((p) => ({ | |
| 89 | 97 | title: rowOf(p), |
| 90 | 98 | ...(p.verified ? {} : { meta: "unverified" }), |
| 91 | 99 | })); |
| @@ -94,14 +102,24 @@ export function peopleScreen(f: Session, ctx: Ctx): Screen { | ||
| 94 | 102 | s.text(area.x, area.y, truncate(NO_MAIL_LINE, area.w), style(c.attn, c.bg), area.x + area.w); |
| 95 | 103 | area = { ...area, y: area.y + 2, h: Math.max(0, area.h - 2) }; |
| 96 | 104 | } |
| 105 | if (area.h > 1) { | |
| 106 | s.text(area.x, area.y, truncate(filterLine(only), area.w), style(c.mutedFg, c.bg), area.x + area.w); | |
| 107 | area = { ...area, y: area.y + 2, h: Math.max(0, area.h - 2) }; | |
| 108 | } | |
| 97 | 109 | drawList(s, area, c, list, rows); |
| 98 | 110 | }, |
| 99 | 111 | key(e) { |
| 100 | const people = listPeople(f.app.db); | |
| 101 | if (list.key(e, people.length)) return true; | |
| 112 | const shown = people(); | |
| 113 | if (list.key(e, shown.length)) return true; | |
| 102 | 114 | if (e.ctrl || e.alt) return false; |
| 115 | if (e.key === "f") { | |
| 116 | only = FILTERS[(FILTERS.indexOf(only) + 1) % FILTERS.length] ?? null; | |
| 117 | list.index = 0; | |
| 118 | list.offset = 0; | |
| 119 | return true; | |
| 120 | } | |
| 103 | 121 | if (e.key === "v") { |
| 104 | const p = people[list.index]; | |
| 122 | const p = shown[list.index]; | |
| 105 | 123 | if (f.user?.isAdmin !== true || f.user.kind !== "user") ctx.toast("Only a forge admin can verify an email.", "danger"); |
| 106 | 124 | else if (p === undefined) return true; |
| 107 | 125 | else if (p.verified) ctx.toast(`${p.username}'s email is already verified.`, "danger"); |
| @@ -127,8 +145,8 @@ export function peopleScreen(f: Session, ctx: Ctx): Screen { | ||
| 127 | 145 | label: "account name", |
| 128 | 146 | onSubmit: (name) => { |
| 129 | 147 | const username = name.trim(); |
| 130 | const person = listPeople(f.app.db).find((p) => p.username === username); | |
| 131 | if (person === undefined) return "No such account, or not one a suspension is for."; | |
| 148 | const person = peopleByActivity(f.app.db).find((p) => p.username === username); | |
| 149 | if (person === undefined || person.word === "crew member") return "No such account, or not one a suspension is for."; | |
| 132 | 150 | askSuspend(username, person.suspendedAt === null); |
| 133 | 151 | return undefined; |
| 134 | 152 | }, |
src/web/pages/people.ts +36 −19
| @@ -1,5 +1,8 @@ | ||
| 1 | 1 | /** |
| 2 | * People — every account and its role in one table: guest, member or admin. | |
| 2 | * People — rows by last activity: name, a plain word for the role (owner, | |
| 3 | * member, guest, crew member) and when they were last seen; no ids. One line | |
| 4 | * of filters (`?role=`) narrows it. One query (`peopleByActivity`) gives the | |
| 5 | * word, allowance and last seen. | |
| 3 | 6 | * An admin promotes a guest to member (with a starter model allowance, none |
| 4 | 7 | * by default) or demotes a member, in one same-origin form post. The guards |
| 5 | 8 | * refuse what `CAN` withholds; the card says so in `roleLimits` (`docs/ROLES.md`). |
| @@ -9,7 +12,8 @@ | ||
| 9 | 12 | |
| 10 | 13 | import { setRole } from "../../actions/roles.ts"; |
| 11 | 14 | import { GRANT_MAX_USD } from "../../core/model_grant.ts"; |
| 12 | import { listPeople, roleLimits, type Person } from "../../core/roles.ts"; | |
| 15 | import { peopleByActivity, wordFilter, type PersonRow } from "../../core/people_rows.ts"; | |
| 16 | import { roleLimits } from "../../core/roles.ts"; | |
| 13 | 17 | import { getUserByName, type User } from "../../core/users.ts"; |
| 14 | 18 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; |
| 15 | 19 | import type { Ctx } from "../../http/context.ts"; |
| @@ -18,7 +22,7 @@ import { field, readForm } from "../form.ts"; | ||
| 18 | 22 | import { isLocalPath } from "../flash.ts"; |
| 19 | 23 | import { html } from "../html.ts"; |
| 20 | 24 | import { flashTo, page } from "../layout.ts"; |
| 21 | import { badge, btn, empty, userLink } from "../ui.ts"; | |
| 25 | import { btn, empty, time, userLink } from "../ui.ts"; | |
| 22 | 26 | import { accountSections } from "./origins.ts"; |
| 23 | 27 | |
| 24 | 28 | /** |
| @@ -42,16 +46,29 @@ export function peoplePage(ctx: Ctx): Response { | ||
| 42 | 46 | if (!ctx.auth.user) return signInFirst(ctx); |
| 43 | 47 | if (!ctx.auth.user.isAdmin) return notFoundPage(ctx); |
| 44 | 48 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 45 | const rows = timePhase(ctx.request, "people", () => listPeople(ctx.app.db)); | |
| 49 | const only = wordFilter(ctx.url.searchParams.get("role")); | |
| 50 | const rows = timePhase(ctx.request, "people", () => peopleByActivity(ctx.app.db, only)); | |
| 46 | 51 | const made = appsMade(ctx); |
| 47 | 52 | const rendering = performance.now(); |
| 48 | const line = (p: Person) => { | |
| 49 | const role = p.role; | |
| 50 | const at = `/settings/people/${encodeURIComponent(p.username)}/role`; | |
| 53 | const filters = html`<p class="small" data-people-filters>Show: ${[ | |
| 54 | ["", "everyone"], | |
| 55 | ["owner", "owners"], | |
| 56 | ["member", "members"], | |
| 57 | ["guest", "guests"], | |
| 58 | ["crew", "crew"], | |
| 59 | ].flatMap(([v, label], i) => { | |
| 60 | const on = (only === null ? "" : only === "crew member" ? "crew" : only) === v; | |
| 61 | const link = on | |
| 62 | ? html`<strong aria-current="true">${label}</strong>` | |
| 63 | : html`<a href="${v === "" ? "/settings/people" : `/settings/people?role=${v}`}">${label}</a>`; | |
| 64 | return i === 0 ? [link] : [" · ", link]; | |
| 65 | })}</p>`; | |
| 66 | const line = (r: PersonRow) => { | |
| 67 | const at = `/settings/people/${encodeURIComponent(r.username)}/role`; | |
| 51 | 68 | const act = |
| 52 | role === "admin" | |
| 69 | r.word === "owner" || r.word === "crew member" | |
| 53 | 70 | ? null |
| 54 | : role === "guest" | |
| 71 | : r.word === "guest" | |
| 55 | 72 | ? html`<form class="row-fields" method="post" action="${at}"> |
| 56 | 73 | <input type="hidden" name="role" value="member" /> |
| 57 | 74 | <input class="input" name="usd" inputmode="decimal" autocomplete="off" |
| @@ -62,15 +79,15 @@ export function peoplePage(ctx: Ctx): Response { | ||
| 62 | 79 | <input type="hidden" name="role" value="guest" /> |
| 63 | 80 | ${btn("Make guest", { type: "submit", size: "sm" })} |
| 64 | 81 | </form>`; |
| 65 | const apps = made.get(p.username) ?? []; | |
| 82 | const apps = made.get(r.username) ?? []; | |
| 66 | 83 | const count = apps.length; |
| 67 | const shown = apps.slice(0, 3).map((n) => html`<a href="/${p.username}/${n}">${n}</a>`); | |
| 68 | const standing = p.usdPerMonth === null ? "no allowance" : `$${p.usdPerMonth.toFixed(2)} a month`; | |
| 84 | const shown = apps.slice(0, 3).map((n) => html`<a href="/${r.username}/${n}">${n}</a>`); | |
| 85 | const allowance = | |
| 86 | r.word === "owner" || r.word === "crew member" ? "" : r.usdPerMonth === null ? " · no allowance" : ` · $${r.usdPerMonth.toFixed(2)} a month`; | |
| 69 | 87 | return html`<tr> |
| 70 | <td>${userLink(p.username, { nav: true })}<div class="small muted">${count === 0 ? "No apps yet" : count === 1 ? "1 app" : `${count} apps`}${ | |
| 88 | <td>${userLink(r.username, { nav: true })}<div class="small"><strong>${r.word}</strong><span class="muted">${allowance}</span></div><div class="small muted">${count === 0 ? "No apps yet" : count === 1 ? "1 app" : `${count} apps`}${ | |
| 71 | 89 | count === 0 ? "" : html`: ${shown.flatMap((a, i) => (i === 0 ? [a] : [", ", a]))}${count > 3 ? ` and ${count - 3} more` : ""}` |
| 72 | }</div></td> | |
| 73 | <td>${badge(role, { tone: role === "guest" ? "default" : "success" })} <span class="small muted">${standing}</span></td> | |
| 90 | } · ${r.lastSeen === null ? "never seen" : html`seen ${time(r.lastSeen)}`}</div></td> | |
| 74 | 91 | <td>${act}</td> |
| 75 | 92 | </tr>`; |
| 76 | 93 | }; |
| @@ -81,14 +98,14 @@ export function peoplePage(ctx: Ctx): Response { | ||
| 81 | 98 | ${settingsLayout( |
| 82 | 99 | accountSections("people", { admin: true }), |
| 83 | 100 | html`<section class="settings-block"> |
| 84 | <h2>Guests, members and admins</h2> | |
| 85 | <p class="muted">Make a guest a member, with an allowance if you like.</p> | |
| 101 | <p>Most recently seen first.</p> | |
| 102 | ${filters} | |
| 86 | 103 | ${ |
| 87 | 104 | rows.length === 0 |
| 88 | ? empty({ icon: "users", title: "Nobody has joined yet." }) | |
| 105 | ? empty({ icon: "users", title: only === null ? "Nobody has joined yet." : "Nobody here." }) | |
| 89 | 106 | : html`<div class="table-scroll" data-people> |
| 90 | 107 | <table class="table" id="people"> |
| 91 | <thead><tr><th>Person</th><th>Standing</th><th></th></tr></thead> | |
| 108 | <thead><tr><th>Person</th><th></th></tr></thead> | |
| 92 | 109 | <tbody>${rows.map(line)}</tbody> |
| 93 | 110 | </table> |
| 94 | 111 | </div> |
test/keys-people-list.test.ts +11 −7
| @@ -1,4 +1,7 @@ | ||
| 1 | /** The people list joins the shell's j/k row traversal (#10353): one data-nav per person, none on the empty page. */ | |
| 1 | /** | |
| 2 | * The people list joins the shell's j/k row traversal (#10353): one data-nav per person, none on | |
| 3 | * the empty page. | |
| 4 | */ | |
| 2 | 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; |
| 3 | 6 | import { mkdtemp, rm } from "node:fs/promises"; |
| 4 | 7 | import { tmpdir } from "node:os"; |
| @@ -70,15 +73,16 @@ test("the empty page has no data-nav: nobody listed, nothing to traverse", async | ||
| 70 | 73 | body: new URLSearchParams({ username: "ada", password: "a-good-passphrase" }), |
| 71 | 74 | }); |
| 72 | 75 | const own = res0.headers.getSetCookie().map((c) => c.split(";")[0]).join("; "); |
| 73 | // The list hides agents; an admin session outlives the flag for this one request. | |
| 74 | fresh.db.run("UPDATE user SET is_agent = 1 WHERE username = 'ada'"); | |
| 75 | const res = await fetch(`${up.url}/settings/people`, { headers: { cookie: own } }); | |
| 76 | // Crew are listed now; a filter that names nobody leaves the page empty. | |
| 77 | const res = await fetch(`${up.url}/settings/people?role=guest`, { headers: { cookie: own } }); | |
| 76 | 78 | const text = await res.text(); |
| 77 | 79 | expect(res.status).toBe(200); |
outdated
The heading "Guests, members and admins" no longer exists, so indexOf returns -1 and this block is just the last character. The no-data-nav assertion passes vacuously. Anchor on id="people" or data-people. |
||
| 78 | expect(text).toContain("Nobody has joined yet."); | |
| 80 | expect(text).toContain("Nobody here."); | |
| 79 | 81 | // The settings sidebar has its own data-nav links; the people block is what follows its heading. |
| 80 | const block = text.slice(text.indexOf("Guests, members and admins")); | |
| 81 | expect(block.length).toBeLessThan(text.length); | |
| 82 | const at = text.indexOf("data-people-filters"); | |
| 83 | expect(at).toBeGreaterThan(-1); | |
| 84 | const block = text.slice(at); | |
| 85 | expect(block).toContain("Nobody here."); | |
| 82 | 86 | expect(block).not.toContain("data-nav"); |
| 83 | 87 | expect(block).not.toContain(`id="people"`); |
| 84 | 88 | } finally { |
test/people-rows.test.ts added +75
| @@ -0,0 +1,75 @@ | ||
| 1 | /** /settings/people: rows by last activity, a plain role word, a one-line filter (#13011). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { peopleByActivity, wordFilter } from "../src/core/people_rows.ts"; | |
| 8 | import { createUser } from "../src/core/users.ts"; | |
| 9 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 10 | import { quietLogs } from "./helpers/globals.ts"; | |
| 11 | ||
| 12 | quietLogs(); | |
| 13 | let root = ""; | |
| 14 | let app: App; | |
| 15 | let servers: Servers; | |
| 16 | let cookie = ""; | |
| 17 | const ids: Record<string, string> = {}; | |
| 18 | const id = (n: string): string => ids[n] ?? ""; | |
| 19 | ||
| 20 | const seed = async (username: string, admin = false) => { | |
| 21 | const u = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: admin }); | |
| 22 | if (!u.ok) throw new Error(u.error); | |
| 23 | ids[username] = u.value.id; | |
| 24 | }; | |
| 25 | const seen = (name: string, iso: string) => | |
| 26 | app.db.query("INSERT INTO session (token_hash, user_id, expires_at, last_seen_at) VALUES (?1, ?2, '2099-01-01T00:00:00Z', ?3)").run(`h-${name}`, id(name), iso); | |
| 27 | ||
| 28 | beforeAll(async () => { | |
| 29 | root = mkdtempSync(join(tmpdir(), "forge-people-rows-")); | |
| 30 | app = createApp({ dataDir: root, port: 0 }); | |
| 31 | await seed("alice", true); | |
| 32 | await seed("old-guest"); | |
| 33 | await seed("recent-member"); | |
| 34 | await seed("never-seen"); | |
| 35 | await seed("crewbot"); | |
| 36 | app.db.run("UPDATE user SET is_agent = 1 WHERE username = 'crewbot'"); | |
| 37 | app.db.run("INSERT INTO member (user_id, promoted_by, promoted_at) VALUES (?1, ?2, '2026-01-01T00:00:00Z')", [id("recent-member"), id("alice")]); | |
| 38 | seen("old-guest", "2026-01-01T00:00:00.000Z"); | |
| 39 | seen("recent-member", "2026-11-01T00:00:00.000Z"); | |
| 40 | servers = await serve(app); | |
| 41 | const signin = await fetch(`${servers.url}/signin`, { method: "POST", headers: { origin: servers.url }, body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), redirect: "manual" }); | |
| 42 | cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 43 | }); | |
| 44 | afterAll(() => { | |
| 45 | servers.stop(); | |
| 46 | app.close(); | |
| 47 | rmSync(root, { recursive: true, force: true }); | |
| 48 | }); | |
| 49 | ||
| 50 | test("rows run by last seen, never-seen last, with plain role words", () => { | |
| 51 | const rows = peopleByActivity(app.db); | |
| 52 | const names = rows.map((r) => r.username); | |
| 53 | expect(names.indexOf("recent-member")).toBeLessThan(names.indexOf("old-guest")); | |
| 54 | expect(names.indexOf("old-guest")).toBeLessThan(names.indexOf("never-seen")); | |
| 55 | const word = (n: string) => rows.find((r) => r.username === n)!.word; | |
| 56 | expect([word("alice"), word("recent-member"), word("old-guest"), word("crewbot")]).toEqual(["owner", "member", "guest", "crew member"]); | |
| 57 | expect(rows.find((r) => r.username === "never-seen")!.lastSeen).toBeNull(); | |
| 58 | }); | |
| 59 | ||
| 60 | test("the filter narrows to one word and ignores nonsense", () => { | |
| 61 | expect(peopleByActivity(app.db, wordFilter("crew")).map((r) => r.username)).toEqual(["crewbot"]); | |
| 62 | expect(wordFilter("<script>")).toBeNull(); | |
| 63 | }); | |
| 64 | ||
| 65 | test("the page shows the words, last seen and filter line, and no ids", async () => { | |
| 66 | const text = await (await fetch(`${servers.url}/settings/people`, { headers: { cookie } })).text(); | |
| 67 | expect(text).toContain("crew member"); | |
| 68 | expect(text).toContain("owner"); | |
| 69 | expect(text).toContain("never"); | |
| 70 | expect(text).toContain("data-people-filters"); | |
| 71 | for (const id of Object.values(ids)) expect(text).not.toContain(id); | |
| 72 | const guests = await (await fetch(`${servers.url}/settings/people?role=guest`, { headers: { cookie } })).text(); | |
| 73 | expect(guests).toContain("old-guest"); | |
| 74 | expect(guests.slice(guests.indexOf("id=\"people\"")).split("</table>")[0]).not.toContain("recent-member"); | |
| 75 | }); |
test/tui-people-activity.test.ts added +81
| @@ -0,0 +1,81 @@ | ||
| 1 | /** The terminal People screen lists by last seen, crew included; `f` filters (#13011). */ | |
| 2 | ||
| 3 | import { afterAll, beforeAll, 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 { createApp, type App as Forge } from "../src/app.ts"; | |
| 8 | import { createUser, type User } from "../src/core/users.ts"; | |
| 9 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 10 | import { filterLine, rowOf } from "../src/tui/screens/people.ts"; | |
| 11 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | import { settleApp, settlePalette, settleUntil } from "./helpers/tui.ts"; | |
| 14 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 15 | ||
| 16 | quietLogs(); | |
| 17 | ||
| 18 | let dataDir = ""; | |
| 19 | let forge: Forge; | |
| 20 | let alice: User; | |
| 21 | ||
| 22 | beforeAll(async () => { | |
| 23 | dataDir = await mkdtemp(join(tmpdir(), "forge-tui-people-activity-")); | |
| 24 | forge = createApp({ dataDir, port: 0 }); | |
| 25 | const a = await seedAdmin(forge, { username: "alice", email: "[email protected]", password: "a-long-passphrase" }); | |
| 26 | if (!a.ok) throw new Error("signup failed"); | |
| 27 | alice = a.value; | |
| 28 | const ids: Record<string, string> = {}; | |
| 29 | for (const n of ["quietone", "lately", "crewbot"]) { | |
| 30 | const u = await createUser(forge.db, { username: n, email: `${n}@forge.test`, password: "a-long-passphrase", origin: "seed" }); | |
| 31 | if (!u.ok) throw new Error("signup failed"); | |
| 32 | ids[n] = u.value.id; | |
| 33 | } | |
| 34 | forge.db.run("UPDATE user SET is_agent = 1 WHERE username = 'crewbot'"); | |
| 35 | const seen = (n: string, iso: string) => | |
| 36 | forge.db.query("INSERT INTO session (token_hash, user_id, expires_at, last_seen_at) VALUES (?1, ?2, '2099-01-01T00:00:00Z', ?3)").run(`h-${n}`, ids[n] ?? "", iso); | |
| 37 | seen("quietone", "2026-01-01T00:00:00.000Z"); | |
| 38 | seen("lately", "2026-11-01T00:00:00.000Z"); | |
| 39 | }); | |
| 40 | ||
| 41 | afterAll(async () => { | |
| 42 | await rm(dataDir, { recursive: true, force: true }); | |
| 43 | }); | |
| 44 | ||
| 45 | test("rows run by last seen with crew listed, and f narrows to one word", async () => { | |
| 46 | const terminal = headlessTerminal(120, 40); | |
| 47 | const { app } = createForgeApp(forge, { sshUsername: "alice", remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user: { ...alice } }, { terminal, exitHooks: false }); | |
| 48 | app.start(); | |
| 49 | const press = async (...keys: string[]): Promise<void> => { | |
| 50 | for (const key of keys) app.dispatch({ type: "key", key, ctrl: false, alt: false, shift: false }); | |
| 51 | await settleApp(app); | |
| 52 | }; | |
| 53 | try { | |
| 54 | await settleApp(app); | |
| 55 | await press("ctrl+k"); | |
| 56 | for (const ch of "People") await press(ch); | |
| 57 | await settlePalette(app); | |
| 58 | await press("enter"); | |
| 59 | await settleUntil(app, "lately"); | |
| 60 | const all = app.screen.toText(); | |
| 61 | expect(all).toContain("Show: [everyone]"); | |
| 62 | expect(all).toContain("crewbot crew member"); | |
| 63 | expect(all).toContain("lately guest"); | |
| 64 | expect(all).toContain("quietone guest"); | |
| 65 | expect(all).toContain("never seen"); | |
| 66 | expect(all.indexOf("lately")).toBeLessThan(all.indexOf("quietone")); | |
| 67 | await press("f", "f", "f", "f"); | |
| 68 | const crew = app.screen.toText(); | |
| 69 | expect(crew).toContain("[crew member]"); | |
| 70 | expect(crew).toContain("crewbot"); | |
| 71 | expect(crew).not.toContain("quietone"); | |
| 72 | } finally { | |
| 73 | app.dispatch({ type: "key", key: "ctrl+c", ctrl: true, alt: false, shift: false }); | |
| 74 | } | |
| 75 | }); | |
| 76 | ||
| 77 | test("a row and the filter line say the same words as the page", () => { | |
| 78 | const row = rowOf({ username: "ada", word: "member", usdPerMonth: 5, suspendedAt: null, verified: true, lastSeen: "2026-11-01T10:00:00.000Z" }); | |
| 79 | expect(row).toBe("ada member $5.00 a month seen 2026-11-01"); | |
| 80 | expect(filterLine("guest")).toBe("Show: everyone · owner · member · [guest] · crew member (f changes)"); | |
| 81 | }); |
test/tui-people-suspend.test.ts +1 −1
| @@ -98,7 +98,7 @@ describe("suspending from the terminal's People screen", () => { | ||
| 98 | 98 | await settleUntil(h.app, "bob is suspended."); |
| 99 | 99 | expect(suspendedAt("bob")).not.toBeNull(); |
| 100 | 100 | expect(sessionUser(forge.db, token).ok).toBe(false); |
| 101 | expect(h.text()).toMatch(/bob +guest +suspended 20\d\d-/); | |
| 101 | expect(h.text()).toMatch(/bob +guest +never seen +suspended 20\d\d-/); | |
| 102 | 102 | |
| 103 | 103 | await ask(h, "bob"); |
| 104 | 104 | expect(h.text()).toContain("can sign in again"); |
new above
offline
This duplicates the people listing in core/roles.ts, and the page has to query both and join them by name. Return the allowance and role from this one query instead.