Public: anyone can read this; the crew can change it.
Pinned repositories list only what the viewer may read, and a stale pin can be removed #15017
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14967-pinned-repositories-list-only-what-the-v → main 5 commits fork 1 thread · 1 unresolved
8 files
changed +309
−67
docs/features/pins-lost-access.md added +15
| @@ -0,0 +1,15 @@ | ||
| 1 | --- | |
| 2 | topic: What a forge is | |
| 3 | row: Pinned repositories: a pin to a repository you lost access to | |
| 4 | --- | |
| 5 | # Pinned repositories: a pin to a repository you lost access to | |
| 6 | ||
| 7 | A pin (the repositories `forge sync` keeps on your machine) outlives the access it was made with. When a repository you pinned goes private, or you are taken off it, the pin stays but is hidden: no list names it, so nothing you pinned can tell you about a repository you may no longer see. | |
| 8 | ||
| 9 | You can still take it off: | |
| 10 | ||
| 11 | - **Terminal**: the first screen's machine pane ends with one nameless row, "N pins no longer available · d clears". Move to it and press `d` (or `enter`), then choose **Unpin**: every such pin comes off at once. The row never names the repositories. | |
| 12 | - **API**: `DELETE /api/v1/user/pins/<owner>/<name>` answers 204 for your own pin, readable or not. A repository you never pinned answers 404 whether or not it exists. | |
| 13 | - **MCP**: `pins` with `{ action: "unpin", repo: "owner/name" }`, on the same terms. | |
| 14 | ||
| 15 | `GET /api/v1/user/pins` and the MCP `pins` list leave a hidden pin out. Get access back and it shows again. |
src/http/api/user.ts +13 −10
| @@ -18,7 +18,7 @@ import { listAssignedTo } from "../../core/issues.ts"; | ||
| 18 | 18 | import { nextStepsFor } from "../../core/next-steps.ts"; |
| 19 | 19 | import { listKeys, publicKey } from "../../core/keys.ts"; |
| 20 | 20 | import { listOrgsForUser } from "../../core/orgs.ts"; |
| 21 | import { canRead, getReposByIds } from "../../core/repos.ts"; | |
| 21 | import { canRead, getRepo, getReposByIds, readableRepoIds } from "../../core/repos.ts"; | |
| 22 | 22 | import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts"; |
| 23 | 23 | import { mayView } from "../../core/sealed.ts"; |
| 24 | 24 | import { emailVerified, RESEND_AFTER_MS } from "../../core/verify.ts"; |
| @@ -757,19 +757,19 @@ export function userRoutes(router: Router<Handler>): void { | ||
| 757 | 757 | const window = keyedWindow(ctx); |
| 758 | 758 | if (window instanceof Response) return window; |
| 759 | 759 | const user = ctx.auth.user; |
| 760 | const { pins, repos } = timePhase(ctx.request, "pins", () => { | |
| 760 | const { pins, repos, readable } = timePhase(ctx.request, "pins", () => { | |
| 761 | 761 | const pins = listPinned(ctx.app, user); |
| 762 | // One query for every pinned repo, not one per pin. | |
| 763 | const repos = getReposByIds( | |
| 764 | ctx.app.db, | |
| 765 | pins.map((pin) => pin.repoId), | |
| 766 | ); | |
| 767 | return { pins, repos }; | |
| 762 | const ids = pins.map((pin) => pin.repoId); | |
| 763 | // One query for every pinned repo, and one for which of them the | |
| 764 | // viewer may still read: a pin outlives the access it was made with. | |
| 765 | const repos = getReposByIds(ctx.app.db, ids); | |
| 766 | const readable = readableRepoIds(ctx.app.db, user, ids); | |
| 767 | return { pins, repos, readable }; | |
| 768 | 768 | }); |
| 769 | 769 | return timePhase(ctx.request, "render", () => { |
| 770 | 770 | const items = pins.flatMap((pin) => { |
| 771 | 771 | const repo = repos.get(pin.repoId); |
| 772 | if (!repo) return []; | |
| 772 | if (!repo || !readable.has(repo.id)) return []; | |
| 773 | 773 | const path = `${encodeURIComponent(repo.ownerName)}/${encodeURIComponent(repo.name)}`; |
| 774 | 774 | return [ |
| 775 | 775 | { |
| @@ -849,7 +849,10 @@ export function userRoutes(router: Router<Handler>): void { | ||
| 849 | 849 | router.add("DELETE", "/api/v1/user/pins/:owner/:name", (ctx) => { |
| 850 | 850 | const denied = requireScope(ctx, "rw"); |
| 851 | 851 | if (denied) return denied; |
| 852 | const repo = loadRepo(ctx); | |
| 852 | // Not `loadRepo`: a pin on a repository the caller can no longer read | |
| 853 | // must still come off. Only the caller's own pin is dropped, and a repo | |
| 854 | // they never pinned answers 404 whether or not it exists. | |
| 855 | const repo = getRepo(ctx.app.db, ctx.params.owner!, ctx.params.name!); | |
| 853 | 856 | if (!repo) return notFound(); |
| 854 | 857 | const removed = unpinRepo(ctx.app, ctx.auth.user, repo); |
| 855 | 858 | if (!removed.ok) return respondAccess(removed.error, 404); |
src/http/mcp/tools/pins.ts +16 −3
| @@ -1,11 +1,12 @@ | ||
| 1 | 1 | import { pageLimit } from "../../../core/issues.ts"; |
| 2 | 2 | import { listPinned, pinRepo, unpinRepo } from "../../../actions/origins.ts"; |
| 3 | import { canRead, getReposByIds } from "../../../core/repos.ts"; | |
| 3 | import { isPinned } from "../../../core/pins.ts"; | |
| 4 | import { canRead, getRepo, getReposByIds } from "../../../core/repos.ts"; | |
| 4 | 5 | import { good, bad, badLimit, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"; |
| 5 | 6 | |
| 6 | 7 | export const tool: Tool = { |
| 7 | 8 | name: "pins", |
| 8 | doc: "`{ action?, repo?, limit? (1 to 200), cursor? }`: `list` (the default) the caller's pinned repositories, paged by `owner/name` as `GET /api/v1/user/pins` is (`next_cursor`), leaving out a pin whose repository the caller can no longer read (the API's list still names it); `pin` or `unpin` `repo`, as `PUT`/`DELETE /api/v1/user/pins/:owner/:name` do. A repository the caller cannot see is not found", | |
| 9 | doc: "`{ action?, repo?, limit? (1 to 200), cursor? }`: `list` (the default) the caller's pinned repositories, paged by `owner/name` as `GET /api/v1/user/pins` is (`next_cursor`), leaving out a pin whose repository the caller can no longer read; `pin` or `unpin` `repo`, as `PUT`/`DELETE /api/v1/user/pins/:owner/:name` do. A repository the caller cannot see is not found, but their own pin on one still unpins", | |
| 9 | 10 | description: "Your pinned repos: list, pin or unpin `repo`.", |
| 10 | 11 | inputSchema: { |
| 11 | 12 | type: "object", |
| @@ -45,7 +46,11 @@ export const tool: Tool = { | ||
| 45 | 46 | next_cursor: after.length > size ? page[page.length - 1]!.repo : null, |
| 46 | 47 | }); |
| 47 | 48 | } |
| 48 | const repo = repoArg(ctx, user, args); | |
| 49 | // A pin outlives the access it was made with (#14967): the caller's own | |
| 50 | // pin comes off even when they can no longer read the repository. One | |
| 51 | // they never pinned still answers like a repository that does not exist. | |
| 52 | const stale = action === "unpin" && user !== undefined ? ownPin(ctx, user.id, args) : undefined; | |
| 53 | const repo = stale ?? repoArg(ctx, user, args); | |
| 49 | 54 | if (isAnswer(repo)) return repo; |
| 50 | 55 | const done = action === "pin" ? pinRepo(ctx.app, user, repo) : unpinRepo(ctx.app, user, repo); |
| 51 | 56 | return done.ok |
| @@ -53,3 +58,11 @@ export const tool: Tool = { | ||
| 53 | 58 | : bad(done.error, action === "pin" ? "that could not be pinned" : "that repository is not pinned"); |
| 54 | 59 | }, |
| 55 | 60 | }; |
| 61 | ||
| 62 | /** The repository `repo` names, only when this user has it pinned. */ | |
| 63 | function ownPin(ctx: Parameters<Tool["run"]>[0], userId: string, args: Record<string, unknown>) { | |
| 64 | const [owner, name, ...rest] = String(args["repo"] ?? "").split("/"); | |
| 65 | if (!owner || !name || rest.length > 0) return undefined; | |
| 66 | const repo = getRepo(ctx.app.db, owner, name); | |
| 67 | return repo !== undefined && isPinned(ctx.app.db, userId, repo.id) ? repo : undefined; | |
| 68 | } |
src/tui/screens/dashboard.ts +89 −35
| @@ -78,6 +78,7 @@ import { listOrgsForUser } from "../../core/orgs.ts"; | ||
| 78 | 78 | import { |
| 79 | 79 | canAdmin, |
| 80 | 80 | canRead, |
| 81 | readableRepoIds, | |
| 81 | 82 | getRepo, |
| 82 | 83 | getReposByIds, |
| 83 | 84 | listReposVisibleTo, |
| @@ -197,6 +198,12 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen { | ||
| 197 | 198 | /** What a typed filter found past `all`, for the query it was read for. */ |
| 198 | 199 | let found: { query: string; repos: Repo[] } | undefined; |
| 199 | 200 | let pins: Pin[] = []; |
| 201 | /** | |
| 202 | * Pins on repositories this account can no longer read (#14967): kept by | |
| 203 | * id only, drawn as one nameless row that clears them, so the terminal can | |
| 204 | * remove what it will not name. | |
| 205 | */ | |
| 206 | let stalePins: string[] = []; | |
| 200 | 207 | let pinned = new Set<string>(); |
| 201 | 208 | let onYou: OnYouItem[] = []; |
| 202 | 209 | /** Where each pull on you stands on the path (check, review, land, live), by issue id. */ |
| @@ -444,7 +451,12 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen { | ||
| 444 | 451 | const reload = (): void => { |
| 445 | 452 | // The server remembers the *choice* and nothing about the laptop, so |
| 446 | 453 | // this is a list of decisions — never a status that would be stale. |
| 447 | pins = f.user ? listPins(f.db, f.user.id) : []; | |
| 454 | // A pin outlives the access it was made with (#14967): list only what | |
| 455 | // this account may still read, in one query for every pin. | |
| 456 | const mine = f.user ? listPins(f.db, f.user.id) : []; | |
| 457 | const readable = readableRepoIds(f.db, f.user, mine.map((p) => p.repoId)); | |
| 458 | pins = mine.filter((p) => readable.has(p.repoId)); | |
| 459 | stalePins = mine.filter((p) => !readable.has(p.repoId)).map((p) => p.repoId); | |
| 448 | 460 | startedFrom = f.user === undefined ? null : startedFromText(f.app.config.dataDir); |
| 449 | 461 | firstWeek = firstWeekText(f.db, f.user); |
| 450 | 462 | pinned = f.user ? pinnedRepoIds(f.db, f.user.id) : new Set<string>(); |
| @@ -1036,6 +1048,8 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen { | ||
| 1036 | 1048 | const panes = (): PaneId[] => ORDER.filter((pane) => drawn.has(pane)); |
| 1037 | 1049 | |
| 1038 | 1050 | const at = (pane: PaneId): ListState => cursors[pane]; |
| 1051 | /** The machine pane's rows: each readable pin, then one for every stale pin. */ | |
| 1052 | const machineRows = (): number => pins.length + (stalePins.length > 0 ? 1 : 0); | |
| 1039 | 1053 | const size = (pane: PaneId): number => |
| 1040 | 1054 | pane === "repos" |
| 1041 | 1055 | ? exploring() |
| @@ -1043,7 +1057,7 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen { | ||
| 1043 | 1057 | : shown().length |
| 1044 | 1058 | : pane === "onyou" |
| 1045 | 1059 | ? onYou.length + waves.length |
| 1046 | : pins.length; | |
| 1060 | : machineRows(); | |
| 1047 | 1061 | |
| 1048 | 1062 | const cycle = (step: number): void => { |
| 1049 | 1063 | const list = panes(); |
| @@ -1068,6 +1082,51 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen { | ||
| 1068 | 1082 | ctx.toast(`${row.name} has no terminal page; the web has it at ${row.url}`, "attn"); |
| 1069 | 1083 | }; |
| 1070 | 1084 | |
| 1085 | /** The one confirm both unpins ask: Keep (default) or Unpin, then reload and say so. */ | |
| 1086 | const confirmUnpin = (title: string, body: string[], keep: string, act: (user: NonNullable<typeof f.user>) => void): void => { | |
| 1087 | if (f.user === undefined) return; | |
| 1088 | const user = f.user; | |
| 1089 | ctx.push( | |
| 1090 | confirmScreen(ctx, screen, { | |
| 1091 | title, | |
| 1092 | body, | |
| 1093 | actions: [ | |
| 1094 | { label: keep, variant: "ghost" }, | |
| 1095 | { label: "Unpin", variant: "danger" }, | |
| 1096 | ], | |
| 1097 | selected: 0, | |
| 1098 | onChoose: (index) => { | |
| 1099 | if (index !== 1) return; | |
| 1100 | act(user); | |
| 1101 | reload(); | |
| 1102 | ctx.toast("unpinned", "success"); | |
| 1103 | }, | |
| 1104 | }), | |
| 1105 | ); | |
| 1106 | }; | |
| 1107 | ||
| 1108 | const clearStale = (): void => { | |
| 1109 | if (stalePins.length === 0) return; | |
| 1110 | const ids = stalePins; | |
| 1111 | confirmUnpin( | |
| 1112 | "Clear pins", | |
| 1113 | [ | |
| 1114 | `Unpin ${plural(ids.length, "repository", "repositories")} you can no longer read?`, | |
| 1115 | "They stay hidden either way; this only takes them off your list.", | |
| 1116 | ], | |
| 1117 | "Keep them", | |
| 1118 | (user) => { | |
| 1119 | for (const repo of getReposByIds(f.db, ids).values()) unpinRepo(f.app, user, repo); | |
| 1120 | }, | |
| 1121 | ); | |
| 1122 | }; | |
| 1123 | ||
| 1124 | /** `enter` or `d` on the machine pane: a named pin, or the nameless stale row after them. */ | |
| 1125 | const actOnPin = (index: number, named: (pin: Pin | undefined) => void): void => { | |
| 1126 | if (index >= pins.length && stalePins.length > 0) return clearStale(); | |
| 1127 | named(pins[index]); | |
| 1128 | }; | |
| 1129 | ||
| 1071 | 1130 | const openPin = (pin: Pin | undefined): void => { |
| 1072 | 1131 | if (pin === undefined) return; |
| 1073 | 1132 | const repo = getRepo(f.db, pin.ownerName, pin.name); |
| @@ -1082,28 +1141,18 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen { | ||
| 1082 | 1141 | }; |
| 1083 | 1142 | |
| 1084 | 1143 | const unpin = (pin: Pin | undefined): void => { |
| 1085 | if (pin === undefined || f.user === undefined) return; | |
| 1086 | const user = f.user; | |
| 1087 | ctx.push( | |
| 1088 | confirmScreen(ctx, screen, { | |
| 1089 | title: "Unpin", | |
| 1090 | body: [ | |
| 1091 | `Stop keeping ${pin.ownerName}/${pin.name} on your machine?`, | |
| 1092 | "Nothing here is deleted; forge sync simply stops fetching it.", | |
| 1093 | ], | |
| 1094 | actions: [ | |
| 1095 | { label: "Keep it", variant: "ghost" }, | |
| 1096 | { label: "Unpin", variant: "danger" }, | |
| 1097 | ], | |
| 1098 | selected: 0, | |
| 1099 | onChoose: (index) => { | |
| 1100 | if (index !== 1) return; | |
| 1101 | const repo = getRepo(f.db, pin.ownerName, pin.name); | |
| 1102 | if (repo !== undefined) unpinRepo(f.app, user, repo); | |
| 1103 | reload(); | |
| 1104 | ctx.toast("unpinned", "success"); | |
| 1105 | }, | |
| 1106 | }), | |
| 1144 | if (pin === undefined) return; | |
| 1145 | confirmUnpin( | |
| 1146 | "Unpin", | |
| 1147 | [ | |
| 1148 | `Stop keeping ${pin.ownerName}/${pin.name} on your machine?`, | |
| 1149 | "Nothing here is deleted; forge sync simply stops fetching it.", | |
| 1150 | ], | |
| 1151 | "Keep it", | |
| 1152 | (user) => { | |
| 1153 | const repo = getRepo(f.db, pin.ownerName, pin.name); | |
| 1154 | if (repo !== undefined) unpinRepo(f.app, user, repo); | |
| 1155 | }, | |
| 1107 | 1156 | ); |
| 1108 | 1157 | }; |
| 1109 | 1158 | |
| @@ -1123,7 +1172,7 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen { | ||
| 1123 | 1172 | if (exploring()) openExplored(exploredRows()[at("repos").index]); |
| 1124 | 1173 | else openRepo(shown()[at("repos").index]); |
| 1125 | 1174 | } else if (focus === "onyou") openAssigned(onYou[at("onyou").index]); |
| 1126 | else openPin(pins[at("machine").index]); | |
| 1175 | else actOnPin(at("machine").index, openPin); | |
| 1127 | 1176 | }; |
| 1128 | 1177 | |
| 1129 | 1178 | const screen: Screen = { |
| @@ -1161,7 +1210,7 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen { | ||
| 1161 | 1210 | ...(silenced.size > 0 ? [binding(["L"], "listen again")] : []), |
| 1162 | 1211 | ...(hidden !== undefined ? [binding(["F"], SHOW_ALL_WORDS.toLowerCase())] : []), |
| 1163 | 1212 | binding(["d"], "unpin", { |
| 1164 | disabled: focus !== "machine" || pins.length === 0, | |
| 1213 | disabled: focus !== "machine" || machineRows() === 0, | |
| 1165 | 1214 | }), |
| 1166 | 1215 | binding(["C"], "decider", { disabled: !f.user }), |
| 1167 | 1216 | ...(firstTell |
| @@ -1410,7 +1459,7 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen { | ||
| 1410 | 1459 | if (f.user && e.key >= "1" && e.key <= String(KIND_CHIPS.length)) |
| 1411 | 1460 | return toggleChip(Number(e.key) - 1), true; |
| 1412 | 1461 | if (e.key === "d" && focus === "machine") { |
| 1413 | unpin(pins[at("machine").index]); | |
| 1462 | actOnPin(at("machine").index, unpin); | |
| 1414 | 1463 | return true; |
| 1415 | 1464 | } |
| 1416 | 1465 | if (e.key === "p" && f.user) { |
| @@ -1730,7 +1779,7 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen { | ||
| 1730 | 1779 | footer: "forge sync", |
| 1731 | 1780 | focused: on, |
| 1732 | 1781 | }); |
| 1733 | if (pins.length === 0) { | |
| 1782 | if (machineRows() === 0) { | |
| 1734 | 1783 | note(s, inner, c, [ |
| 1735 | 1784 | "Nothing pinned yet.", |
| 1736 | 1785 | "p pins the repository under the cursor;", |
| @@ -1743,20 +1792,25 @@ export function dashboardScreen(f: Session, ctx: Ctx): Screen { | ||
| 1743 | 1792 | inner, |
| 1744 | 1793 | c, |
| 1745 | 1794 | at("machine"), |
| 1746 | pins.map( | |
| 1747 | (pin): ListRow => ({ | |
| 1748 | title: `${repoMark(true, set)}${pin.ownerName}/${pin.name}`, | |
| 1749 | }), | |
| 1750 | ), | |
| 1795 | [ | |
| 1796 | ...pins.map( | |
| 1797 | (pin): ListRow => ({ | |
| 1798 | title: `${repoMark(true, set)}${pin.ownerName}/${pin.name}`, | |
| 1799 | }), | |
| 1800 | ), | |
| 1801 | ...(stalePins.length > 0 | |
| 1802 | ? [{ title: `${plural(stalePins.length, "pin")} no longer available · d clears` }] | |
| 1803 | : []), | |
| 1804 | ], | |
| 1751 | 1805 | on, |
| 1752 | 1806 | ); |
| 1753 | hitList(ctx, inner, at("machine"), pins.length, { | |
| 1807 | hitList(ctx, inner, at("machine"), machineRows(), { | |
| 1754 | 1808 | select: () => { |
| 1755 | 1809 | focus = "machine"; |
| 1756 | 1810 | }, |
| 1757 | 1811 | open: (index) => { |
| 1758 | 1812 | focus = "machine"; |
| 1759 | openPin(pins[index]); | |
| 1813 | actOnPin(index, openPin); | |
| 1760 | 1814 | }, |
| 1761 | 1815 | }); |
| 1762 | 1816 | } |
test/api-pins-revoked.test.ts added +130
| @@ -0,0 +1,130 @@ | ||
| 1 | /** | |
| 2 | * A pin outlives the access it was made with (#14967): the list shows only | |
| 3 | * what the viewer may still read, and the stale pin can still come off — | |
| 4 | * through the API and on the terminal's first screen alike. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { createRepo } from "../src/actions/repos.ts"; | |
| 12 | import { getRepo, updateRepoSettings } from "../src/core/repos.ts"; | |
| 13 | import { listPins } from "../src/core/pins.ts"; | |
| 14 | import { createSession } from "../src/core/sessions.ts"; | |
| 15 | import { createUser, type User } from "../src/core/users.ts"; | |
| 16 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 17 | import { driveTerminal } from "./helpers/tui-drive.ts"; | |
| 18 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 19 | import { quietLogs } from "./helpers/globals.ts"; | |
| 20 | ||
| 21 | quietLogs(); | |
| 22 | ||
| 23 | let dataDir: string; | |
| 24 | let app: App; | |
| 25 | let servers: Servers; | |
| 26 | let bobCookie = ""; | |
| 27 | let bobId = ""; | |
| 28 | let bob: User; | |
| 29 | ||
| 30 | const cells = process.env["FORGE_CELLS"]; | |
| 31 | ||
| 32 | beforeAll(async () => { | |
| 33 | process.env["FORGE_CELLS"] = "off"; | |
| 34 | dataDir = await mkdtemp(join(tmpdir(), "forge-api-pins-revoked-")); | |
| 35 | app = createApp({ dataDir, port: 0 }); | |
| 36 | servers = await serve(app); | |
| 37 | const a = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "a-good-passphrase" }); | |
| 38 | if (!a.ok) throw new Error("signup failed"); | |
| 39 | const alice = a.value; | |
| 40 | const b = await createUser(app.db, { | |
| 41 | origin: "seed", | |
| 42 | trusted: true, | |
| 43 | username: "bob", | |
| 44 | email: "[email protected]", | |
| 45 | password: "a-good-passphrase", | |
| 46 | }); | |
| 47 | if (!b.ok) throw new Error(String(b.error)); | |
| 48 | bob = b.value; | |
| 49 | bobId = bob.id; | |
| 50 | bobCookie = `forge_session=${createSession(app.db, bob.id).token}`; | |
| 51 | for (const name of ["kept", "closed"]) { | |
| 52 | const repo = await createRepo(app, alice, { name, visibility: "public" }); | |
| 53 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 54 | } | |
| 55 | }); | |
| 56 | afterAll(async () => { | |
| 57 | await servers.stop(); | |
| 58 | app.close(); | |
| 59 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 60 | else process.env["FORGE_CELLS"] = cells; | |
| 61 | await rm(dataDir, { recursive: true, force: true }); | |
| 62 | }); | |
| 63 | ||
| 64 | function asBob(path: string, method = "GET"): Promise<Response> { | |
| 65 | return fetch(`${servers.url}${path}`, { | |
| 66 | method, | |
| 67 | headers: { cookie: bobCookie, origin: servers.url }, | |
| 68 | }); | |
| 69 | } | |
| 70 | ||
| 71 | async function listed(): Promise<string[]> { | |
| 72 | const res = await asBob("/api/v1/user/pins"); | |
| 73 | expect(res.status).toBe(200); | |
| 74 | const body = (await res.json()) as { items: { owner: string; name: string }[] }; | |
| 75 | return body.items.map((p) => `${p.owner}/${p.name}`).sort(); | |
| 76 | } | |
| 77 | ||
| 78 | test("a pinned repo made private drops out of the list and can still be unpinned", async () => { | |
| 79 | expect((await asBob("/api/v1/user/pins/alice/kept", "PUT")).status).toBe(200); | |
| 80 | expect((await asBob("/api/v1/user/pins/alice/closed", "PUT")).status).toBe(200); | |
| 81 | expect(await listed()).toEqual(["alice/closed", "alice/kept"]); | |
| 82 | ||
| 83 | // Alice takes the repository private: bob's access is revoked. | |
| 84 | const closed = getRepo(app.db, "alice", "closed")!; | |
| 85 | updateRepoSettings(app.db, closed, { visibility: "private" }); | |
| 86 | ||
| 87 | expect(await listed()).toEqual(["alice/kept"]); | |
| 88 | const terminal = await firstScreen(); | |
| 89 | expect(terminal).toContain("alice/kept"); | |
| 90 | expect(terminal).not.toContain("alice/closed"); | |
| 91 | expect(terminal).toContain("1 pin no longer available"); | |
| 92 | ||
| 93 | // The stale pin still comes off, and only once. | |
| 94 | expect((await asBob("/api/v1/user/pins/alice/closed", "DELETE")).status).toBe(204); | |
| 95 | expect(listPins(app.db, bobId).map((p) => p.repoId)).toEqual([ | |
| 96 | getRepo(app.db, "alice", "kept")!.id, | |
| 97 | ]); | |
| 98 | expect((await asBob("/api/v1/user/pins/alice/closed", "DELETE")).status).toBe(404); | |
| 99 | }); | |
| 100 | ||
| 101 | // A regression guard, not proof of #14967: main answered this way too. | |
| 102 | test("unpinning a private repo never pinned answers like one that does not exist", async () => { | |
| 103 | const hidden = getRepo(app.db, "alice", "closed")!; | |
| 104 | expect(hidden.visibility).toBe("private"); | |
| 105 | const there = await asBob("/api/v1/user/pins/alice/closed", "DELETE"); | |
| 106 | const absent = await asBob("/api/v1/user/pins/alice/nothing", "DELETE"); | |
| 107 | expect(there.status).toBe(404); | |
| 108 | expect(absent.status).toBe(404); | |
| 109 | expect(await there.text()).toBe(await absent.text()); | |
| 110 | }); | |
| 111 | ||
| 112 | test("the terminal clears a stale pin without naming it", async () => { | |
| 113 | const closed = getRepo(app.db, "alice", "closed")!; | |
| 114 | updateRepoSettings(app.db, closed, { visibility: "public" }); | |
| 115 | expect((await asBob("/api/v1/user/pins/alice/closed", "PUT")).status).toBe(200); | |
| 116 | updateRepoSettings(app.db, closed, { visibility: "private" }); | |
| 117 | const before = await firstScreen(); | |
| 118 | expect(before).toContain("1 pin no longer available"); | |
| 119 | expect(before).not.toContain("alice/closed"); | |
| 120 | ||
| 121 | // Into the machine pane, past the readable pin, `d`, and choose Unpin. | |
| 122 | const after = await firstScreen(["\t", "\t", "j", "d", "\x1b[C", "\r"]); | |
| 123 | expect(listPins(app.db, bobId).map((p) => p.repoId)).toEqual([getRepo(app.db, "alice", "kept")!.id]); | |
| 124 | expect(after).toContain("alice/kept"); | |
| 125 | expect(after).not.toContain("no longer available"); | |
| 126 | }); | |
| 127 | ||
| 128 | function firstScreen(keys: string[] = []): Promise<string> { | |
| 129 | return driveTerminal(app, bob, keys, { cols: 200, rows: 48 }); | |
| 130 | } |
test/helpers/tui-drive.ts added +32
| @@ -0,0 +1,32 @@ | ||
| 1 | /** | |
| 2 | * Sign in to the terminal as a user, feed it keys one settle at a time, and | |
| 3 | * read the screen they leave behind: the shape several terminal tests share. | |
| 4 | */ | |
| 5 | import type { App } from "../../src/app.ts"; | |
| 6 | import type { User } from "../../src/core/users.ts"; | |
| 7 | import { createForgeApp } from "../../src/tui/forge.ts"; | |
| 8 | import { headlessTerminal } from "../../src/tui/term/terminal.ts"; | |
| 9 | import { settleApp } from "./tui.ts"; | |
| 10 | ||
| 11 | export async function driveTerminal( | |
| 12 | app: App, | |
| 13 | user: User, | |
| 14 | keys: string[], | |
| 15 | size: { cols: number; rows: number } = { cols: 120, rows: 30 }, | |
| 16 | ): Promise<string> { | |
| 17 | const terminal = headlessTerminal(size.cols, size.rows); | |
| 18 | const { app: tui } = createForgeApp( | |
| 19 | app, | |
| 20 | { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user }, | |
| 21 | { terminal, exitHooks: false }, | |
| 22 | ); | |
| 23 | tui.start(); | |
| 24 | await settleApp(tui); | |
| 25 | for (const key of keys) { | |
| 26 | terminal.feed(key); | |
| 27 | await settleApp(tui); | |
| 28 | } | |
| 29 | const text = tui.screen.toText(); | |
| 30 | tui.stop(); | |
| 31 | return text; | |
| 32 | } |
test/mcp-pins.test.ts +11
| @@ -106,6 +106,17 @@ describe("MCP pins", () => { | ||
| 106 | 106 | expect(await listed(aliceRw)).toContain("alice/later"); |
| 107 | 107 | }); |
| 108 | 108 | |
| 109 | test("the caller's own pin on a repository they can no longer read still unpins (#14967)", async () => { | |
| 110 | expect((await tool(bobRw, { action: "unpin", repo: "alice/later" })).value).toEqual({ | |
| 111 | repo: "alice/later", | |
| 112 | pinned: false, | |
| 113 | }); | |
| 114 | // Once it is off, it answers like a repository that does not exist. | |
| 115 | const gone = (await tool(bobRw, { action: "unpin", repo: "alice/later" })).error; | |
| 116 | expect(gone).toBe((await tool(bobRw, { action: "unpin", repo: "alice/nope" })).error); | |
| 117 | expect(gone).toContain("not_found"); | |
| 118 | }); | |
| 119 | ||
| 109 | 120 | test("list pages by owner/name with limit and cursor", async () => { |
| 110 | 121 | for (const n of ["pa", "pb", "pc"]) { |
| 111 | 122 | app.db.run( |
test/tui-talk-receipts.test.ts +3 −19
| @@ -13,9 +13,7 @@ import { openConversation } from "../src/core/conversations.ts"; | ||
| 13 | 13 | import { hold } from "../src/core/fence.ts"; |
| 14 | 14 | import { createIssue, getIssue } from "../src/core/issues.ts"; |
| 15 | 15 | import { createUser, type User } from "../src/core/users.ts"; |
| 16 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 17 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 18 | import { settleApp } from "./helpers/tui.ts"; | |
| 16 | import { driveTerminal } from "./helpers/tui-drive.ts"; | |
| 19 | 17 | import { proveAll } from "./helpers/verified.ts"; |
| 20 | 18 | |
| 21 | 19 | let dir: string; |
| @@ -26,22 +24,8 @@ let repoId: string; | ||
| 26 | 24 | let number: number; |
| 27 | 25 | const open = [":", "Receipts", "\r"]; |
| 28 | 26 | |
| 29 | async function drive(user: User, keys: string[]): Promise<string> { | |
| 30 | const terminal = headlessTerminal(120, 30); | |
| 31 | const { app: tui } = createForgeApp( | |
| 32 | app, | |
| 33 | { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user }, | |
| 34 | { terminal, exitHooks: false }, | |
| 35 | ); | |
| 36 | tui.start(); | |
| 37 | await settleApp(tui); | |
| 38 | for (const k of keys) { | |
| 39 | terminal.feed(k); | |
| 40 | await settleApp(tui); | |
| 41 | } | |
| 42 | const text = tui.screen.toText(); | |
| 43 | tui.stop?.(); | |
| 44 | return text; | |
| 27 | function drive(user: User, keys: string[]): Promise<string> { | |
| 28 | return driveTerminal(app, user, keys); | |
| 45 | 29 | } |
| 46 | 30 | |
| 47 | 31 | beforeAll(async () => { |
new above
offline
Filtering here leaves no way to unpin the stale row in the terminal. Keep an unlisted-but-removable path (for example a count with a 'clear stale pins' action) without exposing names.