Public: anyone can read this; the crew can change it.
/api/v1/explore rows carry the numbers the page's why-line shows: forks, ships, likes and age #13613
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13600-api-v1-explore-rows-carry-the-numbers-th → main 3 commits fork
4 files
changed +93
−4
docs/features/explore.md +1 −1
| @@ -22,7 +22,7 @@ Each row's meta line starts with one phrase for the signal that most explains it | ||
| 22 | 22 | |
| 23 | 23 | ## The figures line on an app row (#13483) |
| 24 | 24 | |
| 25 | An app's meta line opens with its numbers in full: `3 forks, 5 ships, 2 likes, shipped 2d ago · description`. Forks and ships are always said, even at zero; likes appear when there are some, or when the sort is `likes`; `sort:use` adds `N h in use`. So the figure the active sort ranks by is always on the line. The age is `shipped <time>` on the page (the exact date is the `<time>` element's title), `never shipped` when there is none. The meta stays one ellipsised line at 360 px (`.row-meta`, asserted in `test/explorer-why.test.ts`). `GET /api/v1/explore` and the MCP `explore` tool carry the same line as `figures` on each app row (the age worded by `shippedWords`: `shipped 2 days ago`), `figuresOf` in `actions/explore.ts`; the terminal's explorer rows say it too. People and forges have no `figures`; they keep the `why` phrase. | |
| 25 | An app's meta line opens with its numbers in full: `3 forks, 5 ships, 2 likes, shipped 2d ago · description`. Forks and ships are always said, even at zero; likes appear when there are some, or when the sort is `likes`; `sort:use` adds `N h in use`. So the figure the active sort ranks by is always on the line. The age is `shipped <time>` on the page (the exact date is the `<time>` element's title), `never shipped` when there is none. The meta stays one ellipsised line at 360 px (`.row-meta`, asserted in `test/explorer-why.test.ts`). `GET /api/v1/explore` and the MCP `explore` tool carry the same line as `figures` on each app row (the age worded by `shippedWords`: `shipped 2 days ago`), `figuresOf` in `actions/explore.ts`; the terminal's explorer rows say it too. People and forges have no `figures`; they keep the `why` phrase. Each app row also carries `forks`, `ships`, `likes` and `updatedAt` (the last ship, null when none) at the top level, copied from the same `numbers` and `shippedAt` the line is made of, by `rowsWithWhy` for both doors (`test/explore-api-numbers.test.ts`). | |
| 26 | 26 | |
| 27 | 27 | ## How likes count |
| 28 | 28 |
src/actions/explore.ts +4 −2
| @@ -527,13 +527,15 @@ export async function explore( | ||
| 527 | 527 | * The rows as the API and the MCP send them: each with its `why` line when the ranking has one to |
| 528 | 528 | * say (omitted otherwise). |
| 529 | 529 | */ |
| 530 | export function rowsWithWhy(app: App, query: ExploreQuery, items: readonly ExploreRow[]): (ExploreRow & { why?: string; figures?: string })[] { | |
| 530 | export function rowsWithWhy(app: App, query: ExploreQuery, items: readonly ExploreRow[]): (ExploreRow & { why?: string; figures?: string; forks?: number; ships?: number; likes?: number; updatedAt?: string | null })[] { | |
| 531 | 531 | const weights = frontWeights(app.db); |
| 532 | 532 | const now = Date.now(); |
| 533 | 533 | return items.map((row) => { |
| 534 | 534 | const why = whyOf(row, query.sort, now, weights); |
| 535 | 535 | const figures = figuresOf(row, now, query.sort); |
| 536 | const out = figures === "" ? row : { ...row, figures }; | |
| 536 | // An app row says its numbers flat as well, read from the same `numbers` and `shippedAt` the | |
| 537 | // page's line is made of. | |
| 538 | const out = row.kind !== "app" ? (figures === "" ? row : { ...row, figures }) : { ...row, figures, forks: row.numbers.forks, ships: row.numbers.ships, likes: row.numbers.likes, updatedAt: row.shippedAt }; | |
| 537 | 539 | return why === undefined ? out : { ...out, why: why.text }; |
| 538 | 540 | }); |
| 539 | 541 | } |
src/contract/openapi/routes/getApiV1Explore.ts +1 −1
| @@ -1,3 +1,3 @@ | ||
| 1 | 1 | import { limitTo, type Route } from "../shared.ts"; |
| 2 | 2 | |
| 3 | export const route: Route = { method: "get", path: "/api/v1/explore", summary: "Explore apps and repositories; `q` takes words and `kind:`/`owner:` filters; answers { query, items, next_cursor } (`limit` 1 to 100 rows a page, default 30; next_cursor null on the last; a bad `limit`, `sort` or `kind` is a 422 naming the field); cursor from next_cursor; a bad cursor is a 400; the answer carries an ETag (cache-control private, no-cache; vary authorization, cookie) that changes when anything listed changes, and a repeat sending it in If-None-Match gets 304 with no body", auth: false, role: "anonymous", alsoStatus: [304, 422], query: { q: "string", cursor: "string", limit: limitTo(100), sort: "string", kind: "string" } }; | |
| 3 | export const route: Route = { method: "get", path: "/api/v1/explore", summary: "Explore apps and repositories; `q` takes words and `kind:`/`owner:` filters; answers { query, items, next_cursor } (an app item carries forks, ships, likes and updatedAt, the time of its last ship, not of its last push, null when never shipped) (`limit` 1 to 100 rows a page, default 30; next_cursor null on the last; a bad `limit`, `sort` or `kind` is a 422 naming the field); cursor from next_cursor; a bad cursor is a 400; the answer carries an ETag (cache-control private, no-cache; vary authorization, cookie) that changes when anything listed changes, and a repeat sending it in If-None-Match gets 304 with no body", auth: false, role: "anonymous", alsoStatus: [304, 422], query: { q: "string", cursor: "string", limit: limitTo(100), sort: "string", kind: "string" } }; |
test/explore-api-numbers.test.ts added +87
| @@ -0,0 +1,87 @@ | ||
| 1 | /** | |
| 2 | * An API explore row carries forks, ships, likes and updatedAt: the numbers the page's line says | |
| 3 | * (#13613). | |
| 4 | */ | |
| 5 | import { expect, test } from "bun:test"; | |
| 6 | import { rowsWithWhy, type ExploreRow } from "../src/actions/explore.ts"; | |
| 7 | import { createApp } from "../src/app.ts"; | |
| 8 | import { mkdtempSync } from "node:fs"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | ||
| 12 | const row = (kind: ExploreRow["kind"], shippedAt: string | null): ExploreRow => ({ | |
| 13 | kind, name: "a/b", url: "/a/b", description: "", shippedAt, score: 0, | |
| 14 | numbers: { forks: 3, ships: 5, likes: 2, awakeHours: 0, apps: 1 }, | |
| 15 | }); | |
| 16 | ||
| 17 | test("rows carry the flat numbers, and they are the ones the page's line says", () => { | |
| 18 | const app = createApp({ dataDir: mkdtempSync(join(tmpdir(), "forge-numbers-")), port: 0 }); | |
| 19 | const shipped = new Date(Date.now() - 2 * 86_400_000).toISOString(); | |
| 20 | const q = { kind: "app", sort: "score", text: "", archived: false, updated: "all" } as const; | |
| 21 | const [a, p] = rowsWithWhy(app, q, [row("app", shipped), row("person", null)]); | |
| 22 | app.close(); | |
| 23 | expect([a?.forks, a?.ships, a?.likes, a?.updatedAt]).toEqual([3, 5, 2, shipped]); | |
| 24 | expect(a?.figures).toBe("3 forks, 5 ships, 2 likes, shipped 2 days ago"); | |
| 25 | expect(p?.forks).toBeUndefined(); | |
| 26 | }); | |
| 27 | ||
| 28 | test("GET /api/v1/explore sends them for a seeded app", async () => { | |
| 29 | const { serve } = await import("../src/http/server.ts"); | |
| 30 | const { createUser, getUserByName } = await import("../src/core/users.ts"); | |
| 31 | const { createRepo } = await import("../src/actions/repos.ts"); | |
| 32 | const { createApp: insertApp } = await import("../src/core/apps.ts"); | |
| 33 | const app = createApp({ dataDir: mkdtempSync(join(tmpdir(), "forge-numbers-web-")), port: 0 }); | |
| 34 | const servers = await serve(app); | |
| 35 | try { | |
| 36 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-long-words-passphrase", adminIfFirst: true }); | |
| 37 | if (!made.ok) throw new Error("user"); | |
| 38 | const repo = await createRepo(app, getUserByName(app.db, "alice")!, { name: "tool", visibility: "public" }); | |
| 39 | if (!repo.ok) throw new Error("repo"); | |
| 40 | insertApp(app.db, { repoId: repo.value.id, slug: "alice-tool", access: "public" }); | |
| 41 | const api = (await (await fetch(`${servers.url}/api/v1/explore?q=kind:app`)).json()) as { items: { forks: number; ships: number; likes: number; updatedAt: string | null; figures: string }[] }; | |
| 42 | const it = api.items[0]!; | |
| 43 | expect([it.forks, it.ships, it.likes, it.updatedAt]).toEqual([0, 0, 0, null]); | |
| 44 | expect(it.figures).toBe("0 forks, 0 ships, never shipped"); | |
| 45 | const page = await (await fetch(`${servers.url}/explore?q=kind:app`)).text(); | |
| 46 | expect(page).toContain("0 forks, 0 ships, never shipped"); | |
| 47 | } finally { | |
| 48 | servers.stop(); | |
| 49 | app.close(); | |
| 50 | } | |
| 51 | }); | |
| 52 | ||
| 53 | test("the MCP explore tool sends them for a seeded app", async () => { | |
| 54 | const { serve } = await import("../src/http/server.ts"); | |
| 55 | const { createUser, getUserByName } = await import("../src/core/users.ts"); | |
| 56 | const { createRepo } = await import("../src/actions/repos.ts"); | |
| 57 | const { createApp: insertApp } = await import("../src/core/apps.ts"); | |
| 58 | const { createToken } = await import("../src/core/tokens.ts"); | |
| 59 | const { promote } = await import("./helpers/members.ts"); | |
| 60 | const app = createApp({ dataDir: mkdtempSync(join(tmpdir(), "forge-numbers-mcp-")), port: 0 }); | |
| 61 | const servers = await serve(app); | |
| 62 | try { | |
| 63 | const founder = await createUser(app.db, { origin: "seed", trusted: true, username: "founder", email: "[email protected]", password: "a-long-words-passphrase", adminIfFirst: true }); | |
| 64 | if (!founder.ok) throw new Error("founder"); | |
| 65 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-long-words-passphrase" }); | |
| 66 | if (!made.ok) throw new Error("user"); | |
| 67 | promote(app.db, made.value); | |
| 68 | const alice = getUserByName(app.db, "alice")!; | |
| 69 | const repo = await createRepo(app, alice, { name: "tool", visibility: "public" }); | |
| 70 | if (!repo.ok) throw new Error("repo"); | |
| 71 | insertApp(app.db, { repoId: repo.value.id, slug: "alice-tool", access: "public" }); | |
| 72 | const token = createToken(app.db, alice.id, "t", "rw").token; | |
| 73 | const res = await fetch(`${servers.url}/mcp`, { | |
| 74 | method: "POST", | |
| 75 | headers: { authorization: `Bearer ${token}`, "content-type": "application/json", accept: "application/json, text/event-stream" }, | |
| 76 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "explore", arguments: { q: "kind:app" } } }), | |
| 77 | }); | |
| 78 | const r = (await res.json()) as any; | |
| 79 | const out = JSON.parse(r.result.content[0].text) as { items: { forks: number; ships: number; likes: number; updatedAt: string | null; figures: string }[] }; | |
| 80 | const it = out.items[0]!; | |
| 81 | expect([it.forks, it.ships, it.likes, it.updatedAt]).toEqual([0, 0, 0, null]); | |
| 82 | expect(it.figures).toBe("0 forks, 0 ships, never shipped"); | |
| 83 | } finally { | |
| 84 | servers.stop(); | |
| 85 | app.close(); | |
| 86 | } | |
| 87 | }); |
new above
offline