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

The terminal lists the teams of your organizations and who is in each, as the organization's Teams page does #8070

Parked opened this
Closed without merging

scout/forge:scout/7997-the-terminal-lists-the-teams-of-your-org → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +236 −1
src/tui/screens/entries.ts +2
@@ -23,6 +23,7 @@ import { entry as forges } from "./forges.ts";
2323 import { entry as invites } from "./invites.ts";
2424 import { entry as limits } from "./limits.ts";
2525 import { modelCeilingEntry, modelGrantEntry } from "./model.ts";
26 import { entry as orgTeams } from "./orgteams.ts";
2627 import { entry as owner } from "./owner.ts";
2728 import { entry as people } from "./people.ts";
2829 import { entry as quotas } from "./quotas.ts";
@@ -69,6 +70,7 @@ export const ENTRIES: readonly PaletteEntry[] = [
6970 quotas,
7071 limits,
7172 teams,
73 orgTeams,
7274 agents,
7375 invites,
7476 bulk,
src/tui/screens/orgteams.ts added +93
@@ -0,0 +1,93 @@
1 /**
2 * Organization teams in the terminal (#7997): for each organization the
3 * viewer belongs to, the teams they may see with a member count, as
4 * /<org>/-/teams shows them. Enter on a team lists its members, `@username`
5 * and display name; esc goes back. Which teams show is `listTeamsVisibleTo`'s
6 * rule, the page's: an org admin sees every team, a member only their own.
7 * Read-only: no key here adds or removes anyone.
8 */
9
10 import { listOrgsForUser, listTeamMembers, listTeamsVisibleTo, type Team } from "../../core/orgs.ts";
11 import type { User } from "../../core/users.ts";
12 import type { Ctx, Screen } from "../app/app.ts";
13 import { binding } from "../app/keys.ts";
14 import type { Session } from "../session.ts";
15 import { ListState, drawList, type ListRow } from "./common.ts";
16 import type { PaletteEntry } from "./entries.ts";
17
18 export const NO_TEAMS = "No teams you can see.";
19
20 /** One line of the list: a team, or an organization with none to show. */
21 type Line = { readonly org: User; readonly team?: Team; readonly members: number };
22
23 /** Per organization the viewer is in, the teams they may see, in name order. */
24 export function orgTeamLines(f: Session): Line[] {
25 const user = f.user;
26 if (user === undefined) return [];
27 const lines: Line[] = [];
28 const orgs = listOrgsForUser(f.app.db, user.id).sort((a, b) => a.username.localeCompare(b.username));
29 for (const org of orgs) {
30 const teams = listTeamsVisibleTo(f.app.db, org.id, user);
31 if (teams.length === 0) lines.push({ org, members: 0 });
32 for (const team of teams) lines.push({ org, team, members: listTeamMembers(f.app.db, team.id).length });
33 }
34 return lines;
35 }
36
37 function teamMembersScreen(ctx: Ctx, f: Session, org: User, team: Team): Screen {
38 const state = new ListState();
39 const rows = (): ListRow[] =>
40 listTeamMembers(f.app.db, team.id)
41 .sort((a, b) => a.username.localeCompare(b.username))
42 .map((u) => ({ title: `@${u.username}`, ...(u.displayName === "" ? {} : { secondary: u.displayName }) }));
43 return {
44 title: `${org.username} / ${team.name}`,
45 keys: [binding(["j", "k"], "move cursor")],
46 render(area, s) {
47 drawList(s, area, ctx.theme.c, state, rows());
48 },
49 key(e) {
50 return state.key(e, rows().length);
51 },
52 };
53 }
54
55 export function orgTeamsScreen(f: Session, ctx: Ctx): Screen {
56 const state = new ListState();
57 return {
58 title: "Organization teams",
59 keys: [binding(["j", "k"], "move cursor"), binding(["enter"], "members")],
60 render(area, s) {
61 const rows = orgTeamLines(f).map((l): ListRow =>
62 l.team === undefined
63 ? { title: NO_TEAMS, secondary: `@${l.org.username}` }
64 : {
65 title: l.team.name,
66 secondary: `@${l.org.username}`,
67 meta: `${l.members} ${l.members === 1 ? "member" : "members"} · ${l.team.permission}`,
68 },
69 );
70 drawList(s, area, ctx.theme.c, state, rows);
71 },
72 key(e) {
73 const lines = orgTeamLines(f);
74 if (state.key(e, lines.length)) return true;
75 if (e.ctrl || e.alt || e.key !== "enter") return false;
76 const line = lines[state.index];
77 if (line?.team === undefined) return false;
78 ctx.push(teamMembersScreen(ctx, f, line.org, line.team));
79 return true;
80 },
81 };
82 }
83
84 /** Organization teams, as /<org>/-/teams has them: anyone signed in who belongs to an organization. */
85 export const entry: PaletteEntry = {
86 id: "org-teams",
87 title: "Organization teams",
88 hint: "who is in each team of your organizations",
89 after: ["teams", "limits", "quotas", "people"],
90 offered: (session) =>
91 session.user !== undefined && listOrgsForUser(session.app.db, session.user.id).length > 0,
92 run: (session, ctx) => ctx.push(orgTeamsScreen(session, ctx)),
93 };
test/egress-admin.test.ts +1 −1
@@ -93,7 +93,7 @@ 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, "Revoke");
96 await settleUntil(h.tui, "Stop ada/arm reaching");
9797 expect(grants()).toEqual([ARM]);
9898 await h.press("y");
9999 await settleUntil(h.tui, "asked for, not granted");
test/tui-org-teams.test.ts added +140
@@ -0,0 +1,140 @@
1 /**
2 * Organization teams in the terminal (#7997): an org admin sees every team of
3 * the organization, a member only the teams they are in (`listTeamsVisibleTo`,
4 * the Teams page's rule); Enter lists the team's members; a person in no
5 * organization is not offered the entry.
6 */
7 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
8 import { mkdtemp, rm } from "node:fs/promises";
9 import { tmpdir } from "node:os";
10 import { join } from "node:path";
11 import { createApp, type App as Forge } from "../src/app.ts";
12 import { addTeamMember, createOrg, createTeam } from "../src/core/orgs.ts";
13 import { createUser, getUserByName } from "../src/core/users.ts";
14 import { createForgeApp } from "../src/tui/forge.ts";
15 import { headlessTerminal } from "../src/tui/term/terminal.ts";
16 import { quietLogs } from "./helpers/globals.ts";
17 import { settleApp } from "./helpers/tui.ts";
18
19 quietLogs();
20
21 describe("organization teams in the terminal", () => {
22 let dataDir: string;
23 let forge: Forge;
24
25 function boot(username: string) {
26 const terminal = headlessTerminal(140, 32);
27 const user = getUserByName(forge.db, username)!;
28 const { app } = createForgeApp(
29 forge,
30 { sshUsername: "t", remoteAddress: "127.0.0.1", user },
31 { terminal, exitHooks: false },
32 );
33 app.start();
34 const key = (k: string, ctrl = false) => app.dispatch({ type: "key", key: k, ctrl, alt: false, shift: false });
35 return {
36 text: () => app.screen.toText(),
37 press: async (...keys: string[]) => {
38 keys.forEach((k) => key(k));
39 await settleApp(app);
40 },
41 ctrl: async (k: string) => {
42 key(k, true);
43 await settleApp(app);
44 },
45 type: async (t: string) => {
46 for (const ch of t) key(ch);
47 await settleApp(app);
48 },
49 stop: () => app.stop(),
50 };
51 }
52
53 const open = async (h: ReturnType<typeof boot>) => {
54 await h.ctrl("k");
55 await h.type("Organization teams");
56 await h.press("enter");
57 };
58
59 beforeAll(async () => {
60 dataDir = await mkdtemp(join(tmpdir(), "forge-tui-org-teams-"));
61 forge = createApp({ dataDir, port: 0 });
62 for (const username of ["rooty", "olive", "mark", "dana", "erin"]) {
63 const made = await createUser(forge.db, {
64 origin: "seed",
65 trusted: true,
66 username,
67 email: `${username}@forge.local`,
68 password: "a-good-passphrase",
69 adminIfFirst: username === "rooty",
70 ...(username === "mark" ? { displayName: "Mark Member" } : {}),
71 });
72 if (!made.ok) throw new Error(made.error);
73 }
74 const id = (n: string) => getUserByName(forge.db, n)!.id;
75 const org = createOrg(forge.db, "acme", id("olive"));
76 if (!org.ok) throw new Error(org.error);
77 const writers = createTeam(forge.db, org.value.id, "Writers", "write");
78 const readers = createTeam(forge.db, org.value.id, "Readers", "read");
79 if (!writers.ok || !readers.ok) throw new Error("team");
80 addTeamMember(forge.db, writers.value, id("mark"));
81 addTeamMember(forge.db, readers.value, id("dana"));
82 });
83 afterAll(async () => {
84 forge.close();
85 await rm(dataDir, { recursive: true, force: true });
86 });
87
88 test("an org admin sees every team with its member count", async () => {
89 const h = boot("olive");
90 await open(h);
91 const text = h.text();
92 expect(text).toContain("Organization teams");
93 expect(text).toContain("Owners");
94 expect(text).toContain("Writers");
95 expect(text).toContain("Readers");
96 expect(text).toContain("@acme");
97 expect(text).toContain("1 member");
98 h.stop();
99 });
100
101 test("a member sees only the teams they are in", async () => {
102 const h = boot("mark");
103 await open(h);
104 const text = h.text();
105 expect(text).toContain("Writers");
106 expect(text).not.toContain("Readers");
107 expect(text).not.toContain("Owners");
108 h.stop();
109 });
110
111 test("Enter lists the team's members, and esc goes back", async () => {
112 const h = boot("mark");
113 await open(h);
114 await h.press("enter");
115 let text = h.text();
116 expect(text).toContain("acme / Writers");
117 expect(text).toContain("@mark");
118 expect(text).toContain("Mark Member");
119 expect(text).not.toContain("@dana");
120 expect(text).not.toContain("@olive");
121 await h.press("esc");
122 text = h.text();
123 expect(text).not.toContain("acme / Writers");
124 expect(text).toContain("Organization teams");
125 h.stop();
126 });
127
128 test("a person in no organization is not offered the entry", async () => {
129 const h = boot("erin");
130 await h.ctrl("k");
131 await h.type("Organization teams");
132 expect(h.text()).not.toContain("who is in each team of your organizations");
133 h.stop();
134 const m = boot("mark");
135 await m.ctrl("k");
136 await m.type("Organization teams");
137 expect(m.text()).toContain("who is in each team of your organizations");
138 m.stop();
139 });
140 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs