Public: anyone can read this; the crew can change it.
Every repository and app page says who can see it, in a sentence that names the people #955
steward/809-audience → main 4 commits
8 files
changed +507
−0
docs/MCP.md +1
| @@ -28,6 +28,7 @@ for a tunnel and a forge that sleeps. | ||
| 28 | 28 | | tool | does | writes | |
| 29 | 29 | | -------------- | ------------------------------------------------------------------------------------------------- | ------ | |
| 30 | 30 | | `apps` | the apps you can see: live or not, where served, how each runs itself, what is working | | |
| 31 | | `here` | a page's manifest: what it is, who you are there, what you can do with its route and tool, and who can see it (`audience`, the sentence the page shows) | | | |
| 31 | 32 | | `tell` | a sentence becomes an issue, a builder is picked, a run starts on the crew's next tick | yes | |
| 32 | 33 | | `say` | speak on an issue or a pull; a working builder hears it as its next step | yes | |
| 33 | 34 | | `edit` | a new title or body for an issue or a pull: its author's, or a writer's; an agent edits only what it wrote | yes | |
scripts/gate.ts +1
| @@ -69,6 +69,7 @@ export const GATE: readonly string[] = [ | ||
| 69 | 69 | "test/checks.test.ts", |
| 70 | 70 | "test/dependencies.test.ts", |
| 71 | 71 | "test/map-closing.test.ts", |
| 72 | "test/audience.test.ts", | |
| 72 | 73 | "test/autonomy.test.ts", |
| 73 | 74 | "test/studio.test.ts", |
| 74 | 75 | "test/studio-words.test.ts", |
src/actions/here.ts +4
| @@ -16,6 +16,7 @@ import type { App } from "../app.ts"; | ||
| 16 | 16 | import { appsRuntime } from "../apps/runtime.ts"; |
| 17 | 17 | import { isCrew } from "../core/agents.ts"; |
| 18 | 18 | import { getAppByRepo } from "../core/apps.ts"; |
| 19 | import { repoAudience, type Audience } from "../core/audience.ts"; | |
| 19 | 20 | import { |
| 20 | 21 | canAdmin, |
| 21 | 22 | canEnterApp, |
| @@ -54,6 +55,8 @@ export interface Manifest { | ||
| 54 | 55 | readonly kind: "repository"; |
| 55 | 56 | readonly name: string; |
| 56 | 57 | readonly visibility: "public" | "private"; |
| 58 | /** Who can see it, in the words its page uses (#809). */ | |
| 59 | readonly audience: Audience; | |
| 57 | 60 | readonly app?: { |
| 58 | 61 | readonly access: "public" | "private"; |
| 59 | 62 | readonly state?: string; |
| @@ -181,6 +184,7 @@ function repoHere( | ||
| 181 | 184 | kind: "repository" as const, |
| 182 | 185 | name: `${repo.ownerName}/${repo.name}`, |
| 183 | 186 | visibility: repo.visibility, |
| 187 | audience: repoAudience(app.db, repo, viewer), | |
| 184 | 188 | }; |
| 185 | 189 | // A private app the viewer may not enter is not there, to them. |
| 186 | 190 | if (record === undefined || !canEnterApp(app.db, repo, record, viewer)) |
src/core/audience.ts added +203
| @@ -0,0 +1,203 @@ | ||
| 1 | /** | |
| 2 | * Who can see a repository, and who an app answers to, said in a sentence | |
| 3 | * that names the people (#809): the Private/Public badge, made plain. A | |
| 4 | * person on the page should never have to guess whether "private" means | |
| 5 | * them alone, their organization, or also the crew — a wrong guess tells | |
| 6 | * them their work is safer than it is. | |
| 7 | * | |
| 8 | * The counts come from the rules that are enforced, not from a parallel | |
| 9 | * query: `REPO_VISIBLE` and `REPO_WRITABLE` (`core/visibility.ts`), asked | |
| 10 | * for every active person at once, and for an app the same clauses | |
| 11 | * `canEnterApp` asks. The sentence names only the viewer ("you") and the | |
| 12 | * owner the page already shows; everyone else is a number, so a reader | |
| 13 | * learns how many can change a thing and never who. | |
| 14 | */ | |
| 15 | import type { Database } from "bun:sqlite"; | |
| 16 | import { orgAccess } from "./orgs.ts"; | |
| 17 | import type { Repo } from "./repos.ts"; | |
| 18 | import { getUserById, type User } from "./users.ts"; | |
| 19 | import { REPO_VISIBLE, REPO_WRITABLE } from "./visibility.ts"; | |
| 20 | ||
| 21 | export interface Audience { | |
| 22 | readonly level: "public" | "private"; | |
| 23 | readonly sentence: string; | |
| 24 | } | |
| 25 | ||
| 26 | /** The plain facts a sentence is made from. */ | |
| 27 | export interface AudienceFacts { | |
| 28 | readonly visibility: "public" | "private"; | |
| 29 | readonly ownerKind: "user" | "org"; | |
| 30 | readonly ownerName: string; | |
| 31 | /** Whether the viewer is among those who can read it. */ | |
| 32 | readonly viewerReads: boolean; | |
| 33 | /** Whether the viewer is the owner (a personal repository). */ | |
| 34 | readonly viewerOwns: boolean; | |
| 35 | /** Active people other than the viewer who can read it. */ | |
| 36 | readonly otherReaders: number; | |
| 37 | /** Active people, the viewer included, who can change it. */ | |
| 38 | readonly writers: number; | |
| 39 | /** Whether an agent crew works here. */ | |
| 40 | readonly crew: boolean; | |
| 41 | /** | |
| 42 | * Whether the viewer may be told numbers: someone who could see the list | |
| 43 | * behind them — who can write it, or a member of the organization that | |
| 44 | * owns it. Anyone else gets the sentence without counts, so polling it | |
| 45 | * never shows a known person being added. | |
| 46 | */ | |
| 47 | readonly counted: boolean; | |
| 48 | } | |
| 49 | ||
| 50 | const people = (n: number): string => `${n} ${n === 1 ? "person" : "people"}`; | |
| 51 | ||
| 52 | /** A list read aloud: "a", "a and b", "a, b, and c". */ | |
| 53 | function spoken(parts: readonly string[]): string { | |
| 54 | if (parts.length <= 1) return parts[0] ?? ""; | |
| 55 | if (parts.length === 2) return `${parts[0]} and ${parts[1]}`; | |
| 56 | return `${parts.slice(0, -1).join(", ")}, and ${parts.at(-1)}`; | |
| 57 | } | |
| 58 | ||
| 59 | /** The sentence for a repository, from its facts. Pure. */ | |
| 60 | export function audienceOf(f: AudienceFacts): Audience { | |
| 61 | if (f.visibility === "public") | |
| 62 | return { | |
| 63 | level: "public", | |
| 64 | sentence: f.counted | |
| 65 | ? `Public: anyone on the internet can read this; ${people(f.writers)} can change it.` | |
| 66 | : "Public: anyone on the internet can read this; its owners and their team can change it.", | |
| 67 | }; | |
| 68 | const others = (where: string): string => | |
| 69 | `${f.otherReaders}${f.viewerReads ? " other" : ""} ${f.otherReaders === 1 ? "person" : "people"}${where}`; | |
| 70 | const who: string[] = []; | |
| 71 | if (f.viewerReads) who.push("you"); | |
| 72 | if (f.ownerKind === "user" && !f.viewerOwns) who.push(f.ownerName); | |
| 73 | if (f.otherReaders > 0) | |
| 74 | who.push(others(f.ownerKind === "org" ? ` in ${f.ownerName}` : "")); | |
| 75 | if (f.crew) who.push("the crew"); | |
| 76 | const onlyYou = who[0] === "you" && who.filter((w) => w !== "the crew").length === 1; | |
| 77 | return { | |
| 78 | level: "private", | |
| 79 | sentence: `Private: ${onlyYou ? "only " : ""}${spoken(who)}.`, | |
| 80 | }; | |
| 81 | } | |
| 82 | ||
| 83 | /** `fragment`, which reads its viewer as `?1`, asked of `u.id` instead. */ | |
| 84 | const forEveryone = (fragment: string): string => | |
| 85 | fragment.replaceAll("?1", "u.id"); | |
| 86 | ||
| 87 | const ACTIVE_PEOPLE = `u.kind = 'user' AND u.is_agent = 0 | |
| 88 | AND u.removed_at IS NULL AND u.suspended_at IS NULL`; | |
| 89 | ||
| 90 | /** The active people (not agents) who can read, and who can write, `repo`. */ | |
| 91 | function counts( | |
| 92 | db: Database, | |
| 93 | repoId: string, | |
| 94 | ): { readers: string[]; writers: string[] } { | |
| 95 | const ask = (rule: string): string[] => | |
| 96 | ( | |
| 97 | db | |
| 98 | .query<{ id: string }, [string]>( | |
| 99 | // The rule is rewritten to ask about `u.id` before it is placed, | |
| 100 | // so the one parameter left, `?1`, is the repository. | |
| 101 | `SELECT u.id FROM user u WHERE ${ACTIVE_PEOPLE} | |
| 102 | AND EXISTS (SELECT 1 FROM repo WHERE repo.id = ?1 AND ${forEveryone(rule)})`, | |
| 103 | ) | |
| 104 | .all(repoId) as { id: string }[] | |
| 105 | ).map((r) => r.id); | |
| 106 | return { readers: ask(REPO_VISIBLE), writers: ask(REPO_WRITABLE) }; | |
| 107 | } | |
| 108 | ||
| 109 | const hasCrew = (db: Database, repoId: string): boolean => | |
| 110 | db | |
| 111 | .query( | |
| 112 | `SELECT 1 FROM repo_crew c JOIN user a ON a.id = c.user_id | |
| 113 | WHERE c.repo_id = ?1 AND a.is_agent = 1`, | |
| 114 | ) | |
| 115 | .get(repoId) !== null; | |
| 116 | ||
| 117 | /** Who can see `repo`, said to `viewer`. */ | |
| 118 | export function repoAudience( | |
| 119 | db: Database, | |
| 120 | repo: Repo, | |
| 121 | viewer: User | undefined, | |
| 122 | ): Audience { | |
| 123 | const ownerKind = getUserById(db, repo.ownerId)?.kind === "org" ? "org" : "user"; | |
| 124 | const { readers, writers } = counts(db, repo.id); | |
| 125 | const viewerReads = viewer !== undefined && readers.includes(viewer.id); | |
| 126 | return audienceOf({ | |
| 127 | visibility: repo.visibility, | |
| 128 | ownerKind, | |
| 129 | ownerName: repo.ownerName, | |
| 130 | viewerReads, | |
| 131 | viewerOwns: viewer !== undefined && viewer.id === repo.ownerId, | |
| 132 | // Everyone else who can read it: not the viewer, and for a personal | |
| 133 | // repository not its owner, who is named. | |
| 134 | otherReaders: readers.filter( | |
| 135 | (id) => id !== viewer?.id && (ownerKind === "org" || id !== repo.ownerId), | |
| 136 | ).length, | |
| 137 | writers: writers.length, | |
| 138 | crew: hasCrew(db, repo.id), | |
| 139 | counted: mayCount(db, repo, writers, viewer), | |
| 140 | }); | |
| 141 | } | |
| 142 | ||
| 143 | /** | |
| 144 | * Whether `viewer` could see the lists the numbers come from: a person who | |
| 145 | * can write the repository, or a member of the organization that owns it. | |
| 146 | * On a private repository everyone who can read it is one of these already. | |
| 147 | */ | |
| 148 | function mayCount( | |
| 149 | db: Database, | |
| 150 | repo: Repo, | |
| 151 | writers: readonly string[], | |
| 152 | viewer: User | undefined, | |
| 153 | ): boolean { | |
| 154 | if (viewer === undefined) return false; | |
| 155 | if (writers.includes(viewer.id)) return true; | |
| 156 | return orgAccess(db, repo.ownerId, viewer) !== "none"; | |
| 157 | } | |
| 158 | ||
| 159 | /** | |
| 160 | * Who a repository's app answers to, said to `viewer`: anyone, for a public | |
| 161 | * app; for a private one, the people who can change the repository, the | |
| 162 | * people the app names, and the crew — `canEnterApp`'s clauses, counted. | |
| 163 | */ | |
| 164 | export function appAudience( | |
| 165 | db: Database, | |
| 166 | repo: Repo, | |
| 167 | app: { readonly id: string; readonly access: "public" | "private" }, | |
| 168 | viewer: User | undefined, | |
| 169 | ): Audience { | |
| 170 | if (app.access === "public") | |
| 171 | return { | |
| 172 | level: "public", | |
| 173 | sentence: "The app is open to anyone with its link.", | |
| 174 | }; | |
| 175 | const { writers } = counts(db, repo.id); | |
| 176 | const lead = | |
| 177 | repo.visibility === "public" | |
| 178 | ? "The code is public; the app answers only to" | |
| 179 | : "The app answers only to"; | |
| 180 | // Who it names is its writers' to see: anyone else is told the rule, not | |
| 181 | // a number that moves when a friend is added (#786). | |
| 182 | if (!mayCount(db, repo, writers, viewer)) | |
| 183 | return { level: "private", sentence: `${lead} the people its owners let in.` }; | |
| 184 | const named = ( | |
| 185 | db | |
| 186 | .query<{ id: string }, [string]>( | |
| 187 | `SELECT u.id FROM app_member m JOIN user u ON u.id = m.user_id | |
| 188 | WHERE m.app_id = ?1 AND ${ACTIVE_PEOPLE}`, | |
| 189 | ) | |
| 190 | .all(app.id) as { id: string }[] | |
| 191 | ).map((r) => r.id); | |
| 192 | const enter = new Set([...writers, ...named]); | |
| 193 | const you = viewer !== undefined && enter.has(viewer.id); | |
| 194 | const others = [...enter].filter((id) => id !== viewer?.id).length; | |
| 195 | const who = [ | |
| 196 | ...(you ? ["you"] : []), | |
| 197 | ...(others > 0 | |
| 198 | ? [you ? `${others} other ${others === 1 ? "person" : "people"}` : people(others)] | |
| 199 | : []), | |
| 200 | ...(hasCrew(db, repo.id) ? ["the crew"] : []), | |
| 201 | ]; | |
| 202 | return { level: "private", sentence: `${lead} ${spoken(who)}.` }; | |
| 203 | } |
src/http/api.ts +3
| @@ -334,6 +334,7 @@ import { | ||
| 334 | 334 | import { frontInput, frontJson, setFront } from "../actions/front.ts"; |
| 335 | 335 | import { COPIES_PER_DAY, makeItYours } from "../actions/make_it_yours.ts"; |
| 336 | 336 | import type { Router } from "./router.ts"; |
| 337 | import { repoAudience } from "../core/audience.ts"; | |
| 337 | 338 | import { clientAddress, clientBucket } from "./peer.ts"; |
| 338 | 339 | import { manifestFor } from "../actions/here.ts"; |
| 339 | 340 | import { |
| @@ -453,6 +454,8 @@ function repoJson(ctx: Ctx, repo: Repo) { | ||
| 453 | 454 | pinned: |
| 454 | 455 | ctx.auth.user !== undefined && |
| 455 | 456 | isPinned(ctx.app.db, ctx.auth.user.id, repo.id), |
| 457 | // Who can see it, in the words its page uses (#809). | |
| 458 | audience: repoAudience(ctx.app.db, repo, ctx.auth.user), | |
| 456 | 459 | }; |
| 457 | 460 | } |
| 458 | 461 |
src/web/chrome.ts +25
| @@ -12,6 +12,7 @@ import { sshCommand } from "../ssh/command.ts"; | ||
| 12 | 12 | import { mayDirectCrew } from "../crew/direct.ts"; |
| 13 | 13 | import { mayReadRuns } from "../actions/runs.ts"; |
| 14 | 14 | import { getAppByRepo } from "../core/apps.ts"; |
| 15 | import { appAudience, repoAudience } from "../core/audience.ts"; | |
| 15 | 16 | import { edgeLoginUrl } from "../http/edge.ts"; |
| 16 | 17 | import { |
| 17 | 18 | canAdmin, |
| @@ -142,6 +143,29 @@ export function repoHeader( | ||
| 142 | 143 | return scopeHeader(ctx, localScope(ctx, repo), active, options); |
| 143 | 144 | } |
| 144 | 145 | |
| 146 | /** | |
| 147 | * Who can see this repository, in a sentence beside its badge (#809), and on | |
| 148 | * the app's own page who the app answers to. One line, cut short with the | |
| 149 | * whole sentence in its title; for whoever may change the settings it is the | |
| 150 | * way to them. | |
| 151 | */ | |
| 152 | function audienceLine( | |
| 153 | ctx: Ctx, | |
| 154 | repo: Repo, | |
| 155 | active: RepoTab, | |
| 156 | base: string, | |
| 157 | admin: boolean, | |
| 158 | ): Html { | |
| 159 | const said = [repoAudience(ctx.app.db, repo, ctx.auth.user).sentence]; | |
| 160 | const record = active === "app" ? getAppByRepo(ctx.app.db, repo.id) : undefined; | |
| 161 | if (record !== undefined) | |
| 162 | said.push(appAudience(ctx.app.db, repo, record, ctx.auth.user).sentence); | |
| 163 | const sentence = said.join(" "); | |
| 164 | return html`<p class="repo-audience" title="${sentence}"> | |
| 165 | ${admin ? html`<a href="${base}/settings">${sentence}</a>` : sentence} | |
| 166 | </p>`; | |
| 167 | } | |
| 168 | ||
| 145 | 169 | /** |
| 146 | 170 | * The one repo header, for either side of the world. Identity, the where |
| 147 | 171 | * strip, and the tab strip with live counts. Settings appears only for an |
| @@ -321,6 +345,7 @@ export function scopeHeader( | ||
| 321 | 345 | !options.compact && facts.description !== "", |
| 322 | 346 | () => html`<p class="repo-desc">${facts.description}</p>`, |
| 323 | 347 | )} |
| 348 | ${when(repo, (r) => audienceLine(ctx, r, active, base, admin))} | |
| 324 | 349 | ${when( |
| 325 | 350 | parent, |
| 326 | 351 | (p) => |
src/web/style/content.ts +5
| @@ -26,6 +26,11 @@ export const content = /* css */ ` | ||
| 26 | 26 | .repo-title .owner { font-weight: 400; color: var(--muted-fg); } |
| 27 | 27 | .repo-title .slash { color: var(--faint); font-weight: 400; } |
| 28 | 28 | .repo-desc { margin-top: 4px; color: var(--muted-fg); font-size: 13.5px; text-wrap: pretty; } |
| 29 | .repo-audience { | |
| 30 | margin-top: 2px; color: var(--muted-fg); font-size: 12.5px; | |
| 31 | white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; | |
| 32 | } | |
| 33 | .repo-audience a { color: inherit; } | |
| 29 | 34 | .repo-lineage { margin-top: 3px; font-size: 12.5px; color: var(--faint); } |
| 30 | 35 | .repo-head .tabs { margin-top: 12px; } |
| 31 | 36 |
test/audience.test.ts added +265
| @@ -0,0 +1,265 @@ | ||
| 1 | /** | |
| 2 | * Who can see a repository, in a sentence that names the people (#809): | |
| 3 | * counted from the rules that are enforced, said on the repository's head, | |
| 4 | * the app page, the API and the MCP, and never naming anyone a reader | |
| 5 | * could not already see. | |
| 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 { newApp, updateAppSettings } from "../src/actions/apps.ts"; | |
| 12 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 13 | import { createRepo } from "../src/actions/repos.ts"; | |
| 14 | import { createApp, type App } from "../src/app.ts"; | |
| 15 | import { appAudience, audienceOf, repoAudience } from "../src/core/audience.ts"; | |
| 16 | import { getAppByRepo } from "../src/core/apps.ts"; | |
| 17 | import { addTeamMember, createOrg, createTeam } from "../src/core/orgs.ts"; | |
| 18 | import { canRead, canWrite, getRepo } from "../src/core/repos.ts"; | |
| 19 | import { createToken } from "../src/core/tokens.ts"; | |
| 20 | import { createUser, type User } from "../src/core/users.ts"; | |
| 21 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 22 | ||
| 23 | describe("the sentence, from its facts", () => { | |
| 24 | const base = { | |
| 25 | ownerKind: "user" as const, | |
| 26 | ownerName: "alice", | |
| 27 | viewerReads: true, | |
| 28 | viewerOwns: true, | |
| 29 | otherReaders: 0, | |
| 30 | writers: 1, | |
| 31 | crew: true, | |
| 32 | counted: true, | |
| 33 | }; | |
| 34 | test("private and personal: only you, and the crew when it works here", () => { | |
| 35 | expect(audienceOf({ ...base, visibility: "private" }).sentence).toBe( | |
| 36 | "Private: only you and the crew.", | |
| 37 | ); | |
| 38 | expect( | |
| 39 | audienceOf({ ...base, visibility: "private", crew: false }).sentence, | |
| 40 | ).toBe("Private: only you."); | |
| 41 | }); | |
| 42 | test("private and personal, seen by someone else who may read it", () => { | |
| 43 | expect( | |
| 44 | audienceOf({ | |
| 45 | ...base, | |
| 46 | visibility: "private", | |
| 47 | viewerOwns: false, | |
| 48 | otherReaders: 1, | |
| 49 | }).sentence, | |
| 50 | ).toBe("Private: you, alice, 1 other person, and the crew."); | |
| 51 | }); | |
| 52 | test("private, in an organization", () => { | |
| 53 | expect( | |
| 54 | audienceOf({ | |
| 55 | ...base, | |
| 56 | visibility: "private", | |
| 57 | ownerKind: "org", | |
| 58 | ownerName: "acme", | |
| 59 | viewerOwns: false, | |
| 60 | otherReaders: 3, | |
| 61 | }).sentence, | |
| 62 | ).toBe("Private: you, 3 other people in acme, and the crew."); | |
| 63 | }); | |
| 64 | test("public, to someone who could not see the list, gives no number", () => { | |
| 65 | expect(audienceOf({ ...base, visibility: "public", writers: 2, counted: false }).sentence).toBe( | |
| 66 | "Public: anyone on the internet can read this; its owners and their team can change it.", | |
| 67 | ); | |
| 68 | }); | |
| 69 | test("public says who can change it, as a number", () => { | |
| 70 | expect(audienceOf({ ...base, visibility: "public", writers: 2 })).toEqual({ | |
| 71 | level: "public", | |
| 72 | sentence: | |
| 73 | "Public: anyone on the internet can read this; 2 people can change it.", | |
| 74 | }); | |
| 75 | }); | |
| 76 | }); | |
| 77 | ||
| 78 | describe("the sentence on a forge", () => { | |
| 79 | let dataDir: string; | |
| 80 | let app: App; | |
| 81 | let servers: Servers; | |
| 82 | let base: string; | |
| 83 | const users: Record<string, User> = {}; | |
| 84 | const tokens: Record<string, string> = {}; | |
| 85 | ||
| 86 | beforeAll(async () => { | |
| 87 | dataDir = await mkdtemp(join(tmpdir(), "forge-audience-")); | |
| 88 | app = createApp({ dataDir, port: 0 }); | |
| 89 | servers = await serve(app); | |
| 90 | base = servers.url; | |
| 91 | for (const [name, first] of [ | |
| 92 | ["alice", true], | |
| 93 | ["wes", false], | |
| 94 | ["rita", false], | |
| 95 | ["carol", false], | |
| 96 | ] as const) { | |
| 97 | const made = await createUser(app.db, { | |
| 98 | trusted: true, | |
| 99 | username: name, | |
| 100 | email: `${name}@forge.local`, | |
| 101 | password: "a-good-passphrase", | |
| 102 | adminIfFirst: first, | |
| 103 | }); | |
| 104 | if (!made.ok) throw new Error(made.error); | |
| 105 | users[name] = made.value; | |
| 106 | tokens[name] = createToken(app.db, made.value.id, "t", "rw").token; | |
| 107 | } | |
| 108 | const org = createOrg(app.db, "acme", users["alice"]!.id); | |
| 109 | if (!org.ok) throw new Error(org.error); | |
| 110 | const writers = createTeam(app.db, org.value.id, "Writers", "write"); | |
| 111 | const readers = createTeam(app.db, org.value.id, "Readers", "read"); | |
| 112 | if (!writers.ok || !readers.ok) throw new Error("teams"); | |
| 113 | addTeamMember(app.db, writers.value, users["wes"]!.id); | |
| 114 | addTeamMember(app.db, readers.value, users["rita"]!.id); | |
| 115 | const vault = await createRepo(app, users["alice"]!, { | |
| 116 | name: "vault", | |
| 117 | owner: "acme", | |
| 118 | visibility: "private", | |
| 119 | }); | |
| 120 | if (!vault.ok) throw new Error(String(vault.error)); | |
| 121 | const bot = await createAgent(app, users["alice"]!, { username: "bot" }); | |
| 122 | if (!bot.ok) throw new Error(String(bot.error)); | |
| 123 | expect(addToCrew(app, users["alice"]!, vault.value, "bot").ok).toBe(true); | |
| 124 | // A public repository whose app is private. | |
| 125 | const game = await newApp(app, users["alice"]!, { | |
| 126 | name: "game", | |
| 127 | owner: "acme", | |
| 128 | visibility: "public", | |
| 129 | }); | |
| 130 | if (!game.ok) throw new Error(String(game.error)); | |
| 131 | expect( | |
| 132 | updateAppSettings(app, users["alice"]!, game.value.repo, { | |
| 133 | access: "private", | |
| 134 | }).ok, | |
| 135 | ).toBe(true); | |
| 136 | }, 60_000); | |
| 137 | ||
| 138 | afterAll(async () => { | |
| 139 | servers.stop(); | |
| 140 | app.close(); | |
| 141 | await rm(dataDir, { recursive: true, force: true }); | |
| 142 | }); | |
| 143 | ||
| 144 | test("the counts are the enforced rule's, person by person", () => { | |
| 145 | const vault = getRepo(app.db, "acme", "vault")!; | |
| 146 | const people = ["alice", "wes", "rita", "carol"].map((n) => users[n]!); | |
| 147 | const readers = people.filter((u) => canRead(app.db, vault, u)).length; | |
| 148 | const writers = people.filter((u) => canWrite(app.db, vault, u)).length; | |
| 149 | expect([readers, writers]).toEqual([3, 2]); | |
| 150 | // Wes reads it, with two others in acme; the crew works here. | |
| 151 | expect(repoAudience(app.db, vault, users["wes"]).sentence).toBe( | |
| 152 | "Private: you, 2 other people in acme, and the crew.", | |
| 153 | ); | |
| 154 | }); | |
| 155 | ||
| 156 | test("the repository's head says it beside the badge, and names nobody it should not", async () => { | |
| 157 | const page = await fetch(`${base}/acme/vault`, { | |
| 158 | headers: { | |
| 159 | authorization: `Bearer ${tokens["rita"]}`, | |
| 160 | cookie: "forge_mode=dev", | |
| 161 | }, | |
| 162 | }).then((r) => r.text()); | |
| 163 | expect(page).toContain( | |
| 164 | "Private: you, 2 other people in acme, and the crew.", | |
| 165 | ); | |
| 166 | expect(page).not.toContain(">wes<"); | |
| 167 | expect(page).not.toMatch(/Private:[^<]*wes/); | |
| 168 | }); | |
| 169 | ||
| 170 | test("the API and the MCP say the same words", async () => { | |
| 171 | const api = (await ( | |
| 172 | await fetch(`${base}/api/v1/repos/acme/vault`, { | |
| 173 | headers: { authorization: `Bearer ${tokens["rita"]}` }, | |
| 174 | }) | |
| 175 | ).json()) as any; | |
| 176 | expect(api.audience).toEqual({ | |
| 177 | level: "private", | |
| 178 | sentence: "Private: you, 2 other people in acme, and the crew.", | |
| 179 | }); | |
| 180 | const mcp = (await ( | |
| 181 | await fetch(`${base}/mcp`, { | |
| 182 | method: "POST", | |
| 183 | headers: { | |
| 184 | "content-type": "application/json", | |
| 185 | authorization: `Bearer ${tokens["rita"]}`, | |
| 186 | }, | |
| 187 | body: JSON.stringify({ | |
| 188 | jsonrpc: "2.0", | |
| 189 | id: 1, | |
| 190 | method: "tools/call", | |
| 191 | params: { name: "here", arguments: { url: `${base}/acme/vault` } }, | |
| 192 | }), | |
| 193 | }) | |
| 194 | ).json()) as any; | |
| 195 | expect(JSON.parse(mcp.result.content[0].text).here.audience).toEqual( | |
| 196 | api.audience, | |
| 197 | ); | |
| 198 | // A stranger still learns nothing of a private repository. | |
| 199 | const hidden = await fetch(`${base}/api/v1/repos/acme/vault`, { | |
| 200 | headers: { authorization: `Bearer ${tokens["carol"]}` }, | |
| 201 | }); | |
| 202 | expect(hidden.status).toBe(404); | |
| 203 | }); | |
| 204 | ||
| 205 | test("a public repository says it plainly; its private app says who it answers to", async () => { | |
| 206 | const game = getRepo(app.db, "acme", "game")!; | |
| 207 | // An outsider learns the rule; a member of acme learns the number. | |
| 208 | expect(repoAudience(app.db, game, users["carol"]).sentence).toBe( | |
| 209 | "Public: anyone on the internet can read this; its owners and their team can change it.", | |
| 210 | ); | |
| 211 | expect(repoAudience(app.db, game, users["rita"]).sentence).toBe( | |
| 212 | "Public: anyone on the internet can read this; 2 people can change it.", | |
| 213 | ); | |
| 214 | const record = getAppByRepo(app.db, game.id)!; | |
| 215 | expect(appAudience(app.db, game, record, users["carol"]).sentence).toBe( | |
| 216 | "The code is public; the app answers only to the people its owners let in.", | |
| 217 | ); | |
| 218 | expect(appAudience(app.db, game, record, users["alice"]).sentence).toBe( | |
| 219 | "The code is public; the app answers only to you and 1 other person.", | |
| 220 | ); | |
| 221 | const page = await fetch(`${base}/acme/game`, { | |
| 222 | headers: { authorization: `Bearer ${tokens["carol"]}` }, | |
| 223 | }).then((r) => r.text()); | |
| 224 | expect(page).toContain( | |
| 225 | "The code is public; the app answers only to the people its owners let in.", | |
| 226 | ); | |
| 227 | }); | |
| 228 | ||
| 229 | test("an outsider's sentence does not move when a writer or an app member is added", async () => { | |
| 230 | const game = getRepo(app.db, "acme", "game")!; | |
| 231 | const record = getAppByRepo(app.db, game.id)!; | |
| 232 | const look = async () => { | |
| 233 | const api = (await (await fetch(`${base}/api/v1/repos/acme/game`)).json()) as any; | |
| 234 | const page = await fetch(`${base}/acme/game`).then((r) => r.text()); | |
| 235 | const said = page.match(/class="repo-audience"[^>]*>\s*([^<]*)</)?.[1]?.trim(); | |
| 236 | return { | |
| 237 | api: api.audience, | |
| 238 | page: said, | |
| 239 | carolApp: appAudience(app.db, game, record, users["carol"]).sentence, | |
| 240 | }; | |
| 241 | }; | |
| 242 | const before = await look(); | |
| 243 | expect(before.page).toBeDefined(); | |
| 244 | // A new writer in acme, and a person the private app names. | |
| 245 | const newcomer = await createUser(app.db, { | |
| 246 | trusted: true, | |
| 247 | username: "nell", | |
| 248 | email: "[email protected]", | |
| 249 | password: "a-good-passphrase", | |
| 250 | }); | |
| 251 | if (!newcomer.ok) throw new Error(newcomer.error); | |
| 252 | const team = createTeam(app.db, getRepo(app.db, "acme", "vault")!.ownerId, "More", "write"); | |
| 253 | if (!team.ok) throw new Error(String(team.error)); | |
| 254 | addTeamMember(app.db, team.value, newcomer.value.id); | |
| 255 | app.db.run("INSERT INTO app_member (app_id, user_id) VALUES (?1, ?2)", [ | |
| 256 | record.id, | |
| 257 | users["carol"]!.id, | |
| 258 | ]); | |
| 259 | const after = await look(); | |
| 260 | expect(after.api).toEqual(before.api); | |
| 261 | expect(after.page).toBe(before.page); | |
| 262 | // Carol is now named, so she may enter; she is still not told numbers. | |
| 263 | expect(after.carolApp).toBe(before.carolApp); | |
| 264 | }); | |
| 265 | }); |
new above
offline