Public: anyone can read this; the crew can change it.
Talk's origins answers tidily: repos finds the account without an id, a page of one-line rows with the total #16402
Check · Review · Land · Live done — Landed and live in production.
talk/origins-tidy → main 0 commits
4 files
changed +196
−43
docs/features/talk.md +3 −1
| @@ -182,7 +182,9 @@ accounts and what they hold; syncing one or changing its hosts stays on that | ||
| 182 | 182 | page. A repository brought in from one is a mirror, which is not the off-box |
| 183 | 183 | snapshot mirror of the forge's backups. `GET /api/v1/user/origins/:id/repos` |
| 184 | 184 | takes the same `q`, and the terminal's migrate filter matches the same way |
| 185 | (name or description). Test: `test/talk-origins.test.ts`. | |
| 185 | (name or description). `repos` answers `{ total, items, next_cursor }`: 50 one-line items a | |
| 186 | page (`owner/name · private · fork · here as you/name`), every field with `full: true`, and no | |
| 187 | account to name when you have one. Test: `test/talk-origins.test.ts`. | |
| 186 | 188 | |
| 187 | 189 | ## Receipts |
| 188 | 190 |
src/http/mcp/tools/origins.ts +79 −30
| @@ -7,6 +7,33 @@ import { REPO_GROUPS, type RepoGroup } from "../../../origins/types.ts"; | ||
| 7 | 7 | import type { Ctx } from "../../context.ts"; |
| 8 | 8 | import { trustedHosts } from "../trusted_hosts.ts"; |
| 9 | 9 | import { good, bad, type Args, type Tool } from "../shared.ts"; |
| 10 | import type { OriginRepoRow } from "../../../core/origins.ts"; | |
| 11 | import type { User } from "../../../core/users.ts"; | |
| 12 | ||
| 13 | /** Rows a `repos` answer holds at most: a whole account's list once flooded the conversation. */ | |
| 14 | const REPOS_PAGE = 50; | |
| 15 | ||
| 16 | /** One repository on one line: its name, who can see it, and its copy here when there is one. */ | |
| 17 | const repoLine = (ctx: Ctx, row: OriginRepoRow): string => { | |
| 18 | const here = remoteRepoJson(ctx, row).mirroredAs; | |
| 19 | return [row.fullName, row.private ? "private" : "public", ...(row.fork ? ["fork"] : []), ...(here === null ? [] : [`here as ${here.owner}/${here.name}`])].join(" · "); | |
| 20 | }; | |
| 21 | ||
| 22 | /** | |
| 23 | * The origin `repos` reads: the `id` given, or the one account a word names (`github`, a host), | |
| 24 | * or with none the person's only account. Several and no word: they are named, in one line. | |
| 25 | */ | |
| 26 | function originFor(ctx: Ctx, user: User, id: unknown): { id: string } | ReturnType<typeof bad> { | |
| 27 | const mine = listUserOrigins(ctx.app, user); | |
| 28 | const word = typeof id === "string" ? id.trim().toLowerCase() : ""; | |
| 29 | if (word !== "" && mine.some((o) => o.id === id)) return { id: id as string }; | |
| 30 | const host = (o: (typeof mine)[number]) => (URL.parse(o.webUrl)?.host ?? "").toLowerCase(); | |
| 31 | const named = word === "" ? mine : mine.filter((o) => o.kind === word || host(o) === word || host(o).split(".")[0] === word); | |
| 32 | if (named.length === 1) return { id: named[0]!.id }; | |
| 33 | if (mine.length === 0) return bad("not_found", "no account elsewhere is connected; connect one on /settings/origins"); | |
| 34 | const list = (named.length > 1 ? named : mine).map((o) => `${o.login} on ${host(o)} (${o.id})`).join(", "); | |
| 35 | return bad(word === "" || named.length > 1 ? "invalid" : "not_found", `say which account: \`id\` one of ${list}`); | |
| 36 | } | |
| 10 | 37 | |
| 11 | 38 | /** `list` and `repos` only read what the person connected. */ |
| 12 | 39 | const reads = (args: Args): boolean => args["action"] === "list" || args["action"] === "repos"; |
| @@ -24,7 +51,7 @@ const refusal = (ctx: Ctx, args: Args) => | ||
| 24 | 51 | |
| 25 | 52 | export const tool: Tool = { |
| 26 | 53 | name: "origins", |
| 27 | doc: "`{ action, id?, group?, q?, hosts? }`: your GitHub and other forge accounts (`/settings/origins`): `list` them, `repos` one holds (`id`; `q` words; `mirroredAs` is the copy here; with `group` yours, shared or public, one page and `next_cursor`, sent back as `cursor`), `inventory` what each holds and its migration state, `sync` one now, or `hosts`: with none, read an origin's internal hosts (a forge admin), with `hosts` (one per line) replace them; as `GET /api/v1/user/origins`, `…/:id/repos` and `POST …/:id/sync`, for you only, an admin token (Talk reads `list` and `repos`); never a credential", | |
| 54 | doc: "`{ action, id?, group?, q?, hosts? }`: your GitHub and other forge accounts (`/settings/origins`): `list` them, `repos` one holds (`id`, or a word like `github`, or none with one account; `q` words; 50 one-line rows a page, `full` for every field, `total` and `next_cursor`, sent back as `cursor`; `group` yours, shared or public, as the origin pages it), `inventory` what each holds and its migration state, `sync` one now, or `hosts`: with none, read an origin's internal hosts (a forge admin), with `hosts` (one per line) replace them; as `GET /api/v1/user/origins`, `…/:id/repos` and `POST …/:id/sync`, for you only, an admin token (Talk reads `list` and `repos`); never a credential", | |
| 28 | 55 | description: |
| 29 | 56 | "Your GitHub and other forge accounts, and their repos.", |
| 30 | 57 | inputSchema: { |
| @@ -35,6 +62,7 @@ export const tool: Tool = { | ||
| 35 | 62 | group: { type: "string", enum: [...REPO_GROUPS] }, |
| 36 | 63 | q: { type: "string" }, |
| 37 | 64 | cursor: { type: "string" }, |
| 65 | full: { type: "boolean" }, | |
| 38 | 66 | hosts: { type: "string" }, |
| 39 | 67 | }, |
| 40 | 68 | required: ["action"], |
| @@ -43,7 +71,7 @@ export const tool: Tool = { | ||
| 43 | 71 | // Its reads (the origins, what one holds, its inventory, the hosts with none given) are never |
| 44 | 72 | // held for a Do it: only a sync and a hosts change are. |
| 45 | 73 | heldNotWhen: (args) => reads(args) || args["action"] === "inventory" || (args["action"] === "hosts" && args["hosts"] === undefined), |
| 46 | readsWithout: ["id", "hosts", "cursor"], | |
| 74 | readsWithout: ["id", "hosts", "cursor", "full"], | |
| 47 | 75 | // Talk's agent is never offered a Do it the tool can only refuse. |
| 48 | 76 | refusesFirst: (ctx, _user, args) => refusal(ctx, args), |
| 49 | 77 | async run(ctx, user, args) { |
| @@ -52,43 +80,64 @@ export const tool: Tool = { | ||
| 52 | 80 | if (action === "hosts") return trustedHosts(ctx, user, args); |
| 53 | 81 | if (action !== "list" && action !== "repos" && action !== "inventory" && action !== "sync") |
| 54 | 82 | return bad("bad_action", "`action` is list, repos, inventory, sync or hosts"); |
| 55 | if (action !== "list" && (typeof id !== "string" || id === "")) | |
| 83 | if (action !== "list" && action !== "repos" && (typeof id !== "string" || id === "")) | |
| 56 | 84 | return bad("invalid", "send the origin's `id`, from list"); |
| 57 | 85 | const refused = refusal(ctx, args); |
| 58 | 86 | if (refused !== undefined) return refused; |
| 59 | 87 | if (action === "list") |
| 60 | 88 | return good(listUserOrigins(ctx.app, user).map(originJson)); |
| 89 | if (action === "repos") { | |
| 90 | const found = originFor(ctx, user, id); | |
| 91 | return "id" in found ? repos(ctx, user, found.id, args) : found; | |
| 92 | } | |
| 61 | 93 | const origin = id as string; |
| 62 | 94 | if (action === "sync") { |
| 63 | 95 | const synced = await syncOriginNow(ctx.app, user, origin); |
| 64 | 96 | return synced.ok ? good(synced.value) : bad("not_found", "no such origin"); |
| 65 | 97 | } |
| 66 | if (action === "inventory") { | |
| 67 | const rows = await inventoryFor(ctx.app, user, origin); | |
| 68 | return rows.ok | |
| 69 | ? good(rows.value) | |
| 70 | : rows.error === "unreachable" | |
| 71 | ? bad("origin_unreachable", ORIGIN_UNREACHABLE) | |
| 72 | : bad("not_found", "no such origin"); | |
| 73 | } | |
| 74 | const q = typeof args["q"] === "string" ? args["q"] : undefined; | |
| 75 | const unreachable = (error: string) => | |
| 76 | error === "unreachable" ? bad("origin_unreachable", ORIGIN_UNREACHABLE) : bad("not_found", "no such origin"); | |
| 77 | const group = args["group"]; | |
| 78 | if (typeof group === "string") { | |
| 79 | // One page of the group, as the origin searched it (a forge searches `q` itself): its rows are | |
| 80 | // never filtered again here, and `next_cursor` says there are more, sent back as `cursor`. | |
| 81 | const cursor = typeof args["cursor"] === "string" && args["cursor"] !== "" ? args["cursor"] : undefined; | |
| 82 | const page = await listOriginRepoGroup(ctx.app, user, origin, group as RepoGroup, { | |
| 83 | ...(q === undefined ? {} : { q }), | |
| 84 | ...(cursor === undefined ? {} : { cursor }), | |
| 85 | }); | |
| 86 | if (!page.ok) return unreachable(page.error); | |
| 87 | return good({ items: page.value.items.map((row) => remoteRepoJson(ctx, row)), next_cursor: page.value.next }); | |
| 88 | } | |
| 89 | // Every page, from the picker's cache: `q` narrows it here. | |
| 90 | const listed = await listRemoteRepos(ctx.app, user, origin); | |
| 91 | if (!listed.ok) return unreachable(listed.error); | |
| 92 | return good(listed.value.filter((row) => repoMatches(row, q)).map((row) => remoteRepoJson(ctx, row))); | |
| 98 | const rows = await inventoryFor(ctx.app, user, origin); | |
| 99 | return rows.ok | |
| 100 | ? good(rows.value) | |
| 101 | : rows.error === "unreachable" | |
| 102 | ? bad("origin_unreachable", ORIGIN_UNREACHABLE) | |
| 103 | : bad("not_found", "no such origin"); | |
| 93 | 104 | }, |
| 94 | 105 | }; |
| 106 | ||
| 107 | /** | |
| 108 | * What an origin holds, a page at a time: one line a repository (`full` for every field), at most | |
| 109 | * `REPOS_PAGE`, with `next_cursor` while more follow and, from the cache, `total`. | |
| 110 | */ | |
| 111 | async function repos(ctx: Ctx, user: User, origin: string, args: Args) { | |
| 112 | const q = typeof args["q"] === "string" ? args["q"] : undefined; | |
| 113 | const cursor = typeof args["cursor"] === "string" && args["cursor"] !== "" ? args["cursor"] : undefined; | |
| 114 | const shown = (rows: readonly OriginRepoRow[]) => rows.map((row) => (args["full"] === true ? remoteRepoJson(ctx, row) : repoLine(ctx, row))); | |
| 115 | const unreachable = (error: string) => | |
| 116 | error === "unreachable" ? bad("origin_unreachable", ORIGIN_UNREACHABLE) : bad("not_found", "no such origin"); | |
| 117 | // A list read whole is paged here, its cursor `o:<rows before>`; an origin's own page (a forge's, | |
| 118 | // at most 50) keeps the origin's cursor. | |
| 119 | const ours = cursor?.startsWith("o:") === true; | |
| 120 | const local = ours ? Math.max(0, Number.parseInt(cursor!.slice(2), 10) || 0) : 0; | |
| 121 | const pageOf = (rows: readonly OriginRepoRow[], total: boolean) => { | |
| 122 | const next = local + REPOS_PAGE < rows.length ? `o:${String(local + REPOS_PAGE)}` : null; | |
| 123 | return good({ ...(total ? { total: rows.length } : {}), items: shown(rows.slice(local, local + REPOS_PAGE)), next_cursor: next }); | |
| 124 | }; | |
| 125 | const group = args["group"]; | |
| 126 | if (typeof group === "string") { | |
| 127 | // One page of the group, as the origin searched it (a forge searches `q` itself): its rows are | |
| 128 | // never filtered again here, and `next_cursor` says there are more, sent back as `cursor`. | |
| 129 | const page = await listOriginRepoGroup(ctx.app, user, origin, group as RepoGroup, { | |
| 130 | ...(q === undefined ? {} : { q }), | |
| 131 | ...(cursor === undefined || ours ? {} : { cursor }), | |
| 132 | }); | |
| 133 | if (!page.ok) return unreachable(page.error); | |
| 134 | // No `next` from the origin: the group came back whole (GitHub's, cut from the cached list), | |
| 135 | // so it is paged here; a forge's own page already holds at most 50 and keeps its cursor. | |
| 136 | if (page.value.next === null) return pageOf(page.value.items, false); | |
| 137 | return good({ items: shown(page.value.items), next_cursor: page.value.next }); | |
| 138 | } | |
| 139 | // Every page, from the picker's cache: `q` narrows it here. | |
| 140 | const listed = await listRemoteRepos(ctx.app, user, origin); | |
| 141 | if (!listed.ok) return unreachable(listed.error); | |
| 142 | return pageOf(listed.value.filter((row) => repoMatches(row, q)), true); | |
| 143 | } |
test/mcp-origins.test.ts +3 −3
| @@ -103,9 +103,9 @@ describe("MCP origins", () => { | ||
| 103 | 103 | }); |
| 104 | 104 | |
| 105 | 105 | test("lists its repositories and starts a sync", async () => { |
| 106 | const repos = await tool(aliceAdmin, { action: "repos", id: originId }); | |
| 107 | expect(repos.value.map((x: any) => x.fullName)).toEqual(["octo/sprockets"]); | |
| 108 | expect(repos.value[0].mirroredAs).toBeNull(); | |
| 106 | const repos = await tool(aliceAdmin, { action: "repos", id: originId, full: true }); | |
| 107 | expect(repos.value.items.map((x: any) => x.fullName)).toEqual(["octo/sprockets"]); | |
| 108 | expect(repos.value.items[0].mirroredAs).toBeNull(); | |
| 109 | 109 | expect(repos.text).not.toContain(TOKEN); |
| 110 | 110 | const sync = await tool(aliceAdmin, { action: "sync", id: originId }); |
| 111 | 111 | expect(sync.value).toBeDefined(); |
test/talk-origins.test.ts +111 −9
| @@ -8,6 +8,7 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | ||
| 8 | 8 | import { mkdtemp, rm } from "node:fs/promises"; |
| 9 | 9 | import { tmpdir } from "node:os"; |
| 10 | 10 | import { join } from "node:path"; |
| 11 | import { createRepo } from "../src/actions/repos.ts"; | |
| 11 | 12 | import { createApp, type App } from "../src/app.ts"; |
| 12 | 13 | import { |
| 13 | 14 | appendTurn, |
| @@ -19,6 +20,7 @@ import { createUser, type User } from "../src/core/users.ts"; | ||
| 19 | 20 | import { mintViaToken } from "../src/core/via_token.ts"; |
| 20 | 21 | import { serve, type Servers } from "../src/http/server.ts"; |
| 21 | 22 | import { newFakeState, serveFakeForge, type FakeForge } from "./fake-origin.ts"; |
| 23 | import { promote } from "./helpers/members.ts"; | |
| 22 | 24 | |
| 23 | 25 | const LOGIN = "octo"; |
| 24 | 26 | const TOKEN = "octo-origin-secret-7c1e"; |
| @@ -92,6 +94,7 @@ beforeAll(async () => { | ||
| 92 | 94 | ? `acme/shared-${String(i)}` |
| 93 | 95 | : `octo/sprocket-${String(i)}`, |
| 94 | 96 | description: i === 42 ? "the answer" : "", |
| 97 | fork: i === 7, | |
| 95 | 98 | cloneUrl: `http://127.0.0.1:1/octo/r${String(i)}.git`, |
| 96 | 99 | defaultBranch: "main", |
| 97 | 100 | private: true, |
| @@ -155,7 +158,7 @@ describe("Talk's agent reads the person's own origins", () => { | ||
| 155 | 158 | expect(list.value.map((o: { id: string }) => o.id)).toEqual([originId]); |
| 156 | 159 | expect(list.text).not.toContain(TOKEN); |
| 157 | 160 | const repos = await call(t.token, { action: "repos", id: originId }); |
| 158 | expect(repos.value.length).toBeGreaterThan(0); | |
| 161 | expect(repos.value.items.length).toBeGreaterThan(0); | |
| 159 | 162 | expect(repos.text).not.toContain(TOKEN); |
| 160 | 163 | const turns = listTurns(app.db, alice.id, t.id, { limit: 50 }); |
| 161 | 164 | expect(turns.ok && turns.value.filter((x) => x.role === "confirm")).toEqual( |
| @@ -213,15 +216,15 @@ describe("repos is everything GitHub lists, narrowed by group and q", () => { | ||
| 213 | 216 | ).toBeUndefined(); |
| 214 | 217 | // Read with the admin token, so on the trunk this fails on the count alone. |
| 215 | 218 | const all = await call(admin, { action: "repos", id: originId }); |
| 216 | expect(all.value?.length).toBe(MANY); | |
| 219 | expect(all.value?.total).toBe(MANY); | |
| 217 | 220 | }); |
| 218 | 221 | |
| 219 | 222 | test("q narrows by name or description, and group splits yours from shared", async () => { |
| 220 | 223 | const t = talk(alice, "find sprocket-14 on GitHub").token; |
| 221 | 224 | expect( |
| 222 | 225 | names( |
| 223 | (await call(t, { action: "repos", id: originId, q: "sprocket-14" })) | |
| 224 | .value, | |
| 226 | (await call(t, { action: "repos", id: originId, q: "sprocket-14", full: true })) | |
| 227 | .value.items, | |
| 225 | 228 | ).sort(), |
| 226 | 229 | ).toEqual([ |
| 227 | 230 | "octo/sprocket-14", |
| @@ -237,12 +240,12 @@ describe("repos is everything GitHub lists, narrowed by group and q", () => { | ||
| 237 | 240 | ]); |
| 238 | 241 | expect( |
| 239 | 242 | names( |
| 240 | (await call(t, { action: "repos", id: originId, q: "the answer" })) | |
| 241 | .value, | |
| 243 | (await call(t, { action: "repos", id: originId, q: "the answer", full: true })) | |
| 244 | .value.items, | |
| 242 | 245 | ), |
| 243 | 246 | ).toEqual(["octo/sprocket-42"]); |
| 244 | 247 | const shared = names( |
| 245 | (await call(t, { action: "repos", id: originId, group: "shared" })).value | |
| 248 | (await call(t, { action: "repos", id: originId, group: "shared", full: true })).value | |
| 246 | 249 | .items, |
| 247 | 250 | ); |
| 248 | 251 | expect(shared.length).toBe(MANY / 10); |
| @@ -254,6 +257,7 @@ describe("repos is everything GitHub lists, narrowed by group and q", () => { | ||
| 254 | 257 | id: originId, |
| 255 | 258 | group: "yours", |
| 256 | 259 | q: "shared", |
| 260 | full: true, | |
| 257 | 261 | }) |
| 258 | 262 | ).value.items, |
| 259 | 263 | ); |
| @@ -304,7 +308,7 @@ describe("a forge origin's group is paged by the origin, and the agent is told t | ||
| 304 | 308 | let cursor: string | null = null; |
| 305 | 309 | let pages = 0; |
| 306 | 310 | do { |
| 307 | const page = (await call(t, { action: "repos", id, group: "yours", ...(cursor === null ? {} : { cursor }) })).value as { items: { fullName: string }[]; next_cursor: string | null }; | |
| 311 | const page = (await call(t, { action: "repos", id, group: "yours", full: true, ...(cursor === null ? {} : { cursor }) })).value as { items: { fullName: string }[]; next_cursor: string | null }; | |
| 308 | 312 | expect(page.items.length).toBeLessThanOrEqual(2); |
| 309 | 313 | seen.push(...names(page.items)); |
| 310 | 314 | cursor = page.next_cursor === null ? null : page.next_cursor.trim(); |
| @@ -313,7 +317,105 @@ describe("a forge origin's group is paged by the origin, and the agent is told t | ||
| 313 | 317 | expect(seen.sort()).toEqual(["ana/gear-1", "ana/gear-2", "ana/gear-3", "ana/gear-4", "ana/gear-5"]); |
| 314 | 318 | expect(pages).toBe(3); |
| 315 | 319 | // `q` is the origin's search: its answer is not filtered again. |
| 316 | const named = (await call(t, { action: "repos", id, group: "yours", q: "gear-4" })).value as { items: { fullName: string }[] }; | |
| 320 | const named = (await call(t, { action: "repos", id, group: "yours", q: "gear-4", full: true })).value as { items: { fullName: string }[] }; | |
| 317 | 321 | expect(names(named.items)).toEqual(["ana/gear-4"]); |
| 318 | 322 | }); |
| 319 | 323 | }); |
| 324 | ||
| 325 | describe("a listing never floods the conversation, and the account is found without an id", () => { | |
| 326 | test("50 one-line rows a page, the total, and next_cursor until every one is read", async () => { | |
| 327 | const t = talk(alice, "all my GitHub repos").token; | |
| 328 | const first = (await call(t, { action: "repos", id: originId })).value as { total: number; items: string[]; next_cursor: string | null }; | |
| 329 | expect(first.total).toBe(MANY); | |
| 330 | expect(first.items.length).toBe(50); | |
| 331 | expect(first.items.every((line) => typeof line === "string" && /^\S+\/\S+ · (private|public)/.test(line.trim()))).toBe(true); | |
| 332 | const seen = [...first.items]; | |
| 333 | let cursor = first.next_cursor?.trim() ?? null; | |
| 334 | while (cursor !== null) { | |
| 335 | const page = (await call(t, { action: "repos", id: originId, cursor })).value as { items: string[]; next_cursor: string | null }; | |
| 336 | seen.push(...page.items); | |
| 337 | cursor = page.next_cursor?.trim() ?? null; | |
| 338 | } | |
| 339 | expect(new Set(seen.map((l) => l.trim().split(" ")[0])).size).toBe(MANY); | |
| 340 | // A page's worth of lines is a small answer: a full list once came to about 92,000 characters. | |
| 341 | expect(JSON.stringify(first).length).toBeLessThan(5_000); | |
| 342 | }); | |
| 343 | ||
| 344 | test("with one account, no id reads it; a word names one of several; several and no word are named in one line", async () => { | |
| 345 | const t = talk(alice, "which repos can you see on GitHub?").token; | |
| 346 | // Alice has GitHub and, from the Forgejo test above, a second account: `github` names the first. | |
| 347 | const byWord = (await call(t, { action: "repos", id: "github", q: "sprocket-149" })).value as { total: number }; | |
| 348 | expect(byWord.total).toBe(1); | |
| 349 | const neither = await call(t, { action: "repos" }); | |
| 350 | // One line names them all (the fence around a via token's text adds the line breaks around it). | |
| 351 | expect(neither.error).toMatch(/say which account: `id` one of octo on [^\n]*, ana on [^\n]*/); | |
| 352 | // Bob has none: he is told where to connect one. | |
| 353 | expect((await call(talk(bob, "my GitHub").token, { action: "repos" })).error).toContain("/settings/origins"); | |
| 354 | // One account: no id needed. | |
| 355 | const carol = await createUser(app.db, { origin: "seed", trusted: true, username: "carol", email: "[email protected]", password: "a-good-passphrase" }); | |
| 356 | if (!carol.ok) throw new Error(carol.error); | |
| 357 | promote(app.db, carol.value); | |
| 358 | // Her own GitHub account: an account elsewhere is connected to one person here. | |
| 359 | const hers = newFakeState("github", "carol-gh", "carol-origin-secret-81c2"); | |
| 360 | hers.repos.push({ id: 900, fullName: "carol-gh/notes", cloneUrl: "http://127.0.0.1:1/carol-gh/notes.git", defaultBranch: "main" }); | |
| 361 | const carolForge = serveFakeForge(hers); | |
| 362 | try { | |
| 363 | const res = await fetch(`${servers.url}/api/v1/user/origins`, { | |
| 364 | method: "POST", | |
| 365 | headers: { "content-type": "application/json", authorization: `Bearer ${createToken(app.db, carol.value.id, "admin", "admin").token}` }, | |
| 366 | body: JSON.stringify({ host: carolForge.url, kind: "github", token: "carol-origin-secret-81c2" }), | |
| 367 | }); | |
| 368 | expect(res.status).toBe(201); | |
| 369 | const only = (await call(talk(carol.value, "my GitHub repos").token, { action: "repos" })).value as { total: number; items: string[] }; | |
| 370 | expect(only.total).toBe(1); | |
| 371 | expect(only.items[0]!.trim()).toStartWith("carol-gh/notes · public"); | |
| 372 | } finally { | |
| 373 | carolForge.stop(); | |
| 374 | } | |
| 375 | }); | |
| 376 | }); | |
| 377 | ||
| 378 | describe("the one-line rows and the pages, read as the agent reads them", () => { | |
| 379 | const lines = (v: unknown) => (v as { items: string[] }).items.map((l) => l.trim()); | |
| 380 | ||
| 381 | test("a line says fork, and here as owner/name for a repository mirrored here", async () => { | |
| 382 | const made = await createRepo(app, alice, { name: "sprocket-3", visibility: "private" }); | |
| 383 | if (!made.ok) throw new Error(String(made.error)); | |
| 384 | app.db.query("UPDATE repo SET origin_id = ?1, origin_remote_id = '3' WHERE id = ?2").run(originId, made.value.id); | |
| 385 | const t = talk(alice, "my GitHub repos").token; | |
| 386 | expect(lines((await call(t, { action: "repos", id: originId, q: "sprocket-7" })).value)).toContain("octo/sprocket-7 · private · fork"); | |
| 387 | expect(lines((await call(t, { action: "repos", id: originId, q: "sprocket-71" })).value)).toEqual(["octo/sprocket-71 · private"]); | |
| 388 | expect(lines((await call(t, { action: "repos", id: originId, q: "sprocket-3" })).value)).toContain("octo/sprocket-3 · private · here as alice/sprocket-3"); | |
| 389 | }); | |
| 390 | ||
| 391 | test("full: true without a group answers objects, a group without full answers strings", async () => { | |
| 392 | const t = talk(alice, "my GitHub repos").token; | |
| 393 | const full = (await call(t, { action: "repos", id: originId, q: "sprocket-149", full: true })).value as { items: { fullName: string }[] }; | |
| 394 | expect(full.items.map((r) => r.fullName.trim())).toEqual(["octo/sprocket-149"]); | |
| 395 | const shared = (await call(t, { action: "repos", id: originId, group: "shared" })).value as { items: unknown[] }; | |
| 396 | expect(shared.items.every((l) => typeof l === "string")).toBe(true); | |
| 397 | expect(shared.items.length).toBe(MANY / 10); | |
| 398 | }); | |
| 399 | ||
| 400 | test("a group read whole (GitHub's) is paged here with an o: cursor, to the end", async () => { | |
| 401 | const t = talk(alice, "my own GitHub repos").token; | |
| 402 | const seen: string[] = []; | |
| 403 | let cursor: string | null = null; | |
| 404 | let pages = 0; | |
| 405 | do { | |
| 406 | const page = (await call(t, { action: "repos", id: originId, group: "yours", ...(cursor === null ? {} : { cursor }) })).value as { items: string[]; next_cursor: string | null }; | |
| 407 | seen.push(...lines(page)); | |
| 408 | cursor = page.next_cursor === null ? null : page.next_cursor.trim(); | |
| 409 | if (cursor !== null) expect(cursor).toStartWith("o:"); | |
| 410 | pages++; | |
| 411 | } while (cursor !== null && pages < 10); | |
| 412 | expect(pages).toBe(3); | |
| 413 | expect(new Set(seen).size).toBe(MANY - MANY / 10); | |
| 414 | }); | |
| 415 | ||
| 416 | test("a word that names no account is not found, and the accounts are listed", async () => { | |
| 417 | const r = await call(talk(alice, "my gitlab repos").token, { action: "repos", id: "gitlab" }); | |
| 418 | expect(r.error).toContain("not_found"); | |
| 419 | expect(r.error).toMatch(/say which account: `id` one of octo on [^\n]*/); | |
| 420 | }); | |
| 421 | }); |
new above
offline