Public: anyone can read this; the crew can change it.
/explore pages its list with a plain Next and Previous link kept in the URL #13467
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13442-explore-pages-its-list-with-a-plain-next → main 8 commits fork 5 threads · 5 unresolved
9 files
changed +210
−115
docs/design/areas/explore.md +1 −1
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | # Explore |
| 2 | 2 | |
| 3 | `/explore` (`src/web/studio/explore.ts`). Paging is one `btn` (outline, "More results") in `.explore-more`, which makes it full width and 44px tall so it is a thumb-sized target on a 360px phone. It is an ordinary link, so it works without a script. | |
| 3 | `/explore` (`src/web/studio/explore.ts`). Paging is a `<nav>` of `.explore-pager`: Previous and Next (outline `btn` links, 44px tall) around "Page N of M", with the page in the URL (`?page=2`) beside the filters. They are ordinary links, so it works without a script; the nav is absent when one page holds everything. | |
| 4 | 4 | |
| 5 | 5 | The count line above the list (`[data-hits]`, a muted span at the end of the sort bar) is server-rendered with a 24px line height, so the list never moves; "Clear filters" appears in it only while a filter (not the sort) is active. |
| 6 | 6 |
docs/features/explore.md +1 −1
| @@ -65,7 +65,7 @@ An unknown filter word is refused the same way everywhere: `400` from the API (` | ||
| 65 | 65 | |
| 66 | 66 | ## Paging (#11376) |
| 67 | 67 | |
| 68 | A list holds 60 rows a page. When there are more, `/explore` ends with a full-width **More results** button, a plain link to the same view (kind, sort, since, owner, archived and the words) plus `cursor=…`; the last page has none, and it works without JavaScript. A cursor belongs to one sort: one that is malformed, over 400 characters or made under another sort shows the first page with a one-line note, never an error. `GET /api/v1/explore` returns `next_cursor` and takes `?cursor=`, and the MCP `explore` tool takes and returns the same cursor. | |
| 68 | A list holds 60 rows a page. When there are more, `/explore` ends with **Previous** and **Next** links around "Page N of M", plain links that keep kind, sort, since, owner, archived and the words and add `page=N` (page 1 has no `page`). `?page=2` is server-rendered and works without JavaScript. The page links say `rel="prev"` and `rel="next"`. An old `?cursor=` link shows the first page with one line saying the link is out of date. A `page` that is junk, zero or negative shows the first page, and one past the end shows the last, never an error. `GET /api/v1/explore` returns `next_cursor` and takes `?cursor=`, and the MCP `explore` tool takes and returns the same cursor. | |
| 69 | 69 | |
| 70 | 70 | The terminal's explore lines page the same way: when the answer carries `next_cursor` the list ends with one line, `more results — n`, and `n` fetches the next page and appends it; the last page shows no line. The cursor is kept with the line it came from, so changing the line starts again at the first page. A cursor the forge refuses shows the first page with one plain line (`that page is no longer there — showing the first page`), never an error screen. A fetch that fails (not a refusal) keeps the pages already shown and says `could not fetch more results — n to retry`. The line takes the list's last row, so it shows on any terminal tall enough for a list. |
| 71 | 71 |
src/actions/explore.ts +38
| @@ -143,6 +143,17 @@ export function exploreLineFrom(get: (name: string) => string | null | undefined | ||
| 143 | 143 | return [...typed, ...[...given].map(([k, v]) => `${k}:${v}`)].join(" "); |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | /** | |
| 147 | * The page number a request asks for (`?page=2`): a positive whole number, else 1. Junk, zero, | |
| 148 | * negative, fractional or huge all mean the first page; one past the end is clamped by | |
| 149 | * `explorePageAt`. The one place `page` is read. | |
| 150 | */ | |
| 151 | export function explorePageFrom(get: (name: string) => string | null | undefined): number { | |
| 152 | const raw = (get("page") ?? "").trim(); | |
| 153 | if (!/^[0-9]{1,6}$/.test(raw)) return 1; | |
| 154 | return Math.max(1, Number(raw)); | |
| 155 | } | |
| 156 | ||
| 146 | 157 | /** A query as separate URL parameters, the form the page links to; "" for the default view. */ |
| 147 | 158 | export function exploreParams(query: ExploreQuery): string { |
| 148 | 159 | const p = new URLSearchParams(); |
| @@ -576,6 +587,29 @@ export async function explorePage( | ||
| 576 | 587 | }; |
| 577 | 588 | } |
| 578 | 589 | |
| 590 | export interface ExplorePageAt { | |
| 591 | readonly items: readonly ExploreRow[]; | |
| 592 | /** The page shown, 1-based, after clamping into 1..pages. */ | |
| 593 | readonly page: number; | |
| 594 | readonly pages: number; | |
| 595 | readonly total: number; | |
| 596 | } | |
| 597 | ||
| 598 | /** Page `n` of the ranking (an out-of-range `n` answers the last page, never an error). */ | |
| 599 | export async function explorePageAt( | |
| 600 | app: App, | |
| 601 | viewer: User | undefined, | |
| 602 | query: ExploreQuery, | |
| 603 | n: number, | |
| 604 | limit: number = ROWS, | |
| 605 | ): Promise<ExplorePageAt> { | |
| 606 | const { rows, stamp } = await everything(app, viewer, query); | |
| 607 | const pages = Math.max(1, Math.ceil(rows.length / limit)); | |
| 608 | const page = Math.min(Math.max(1, Math.trunc(n) || 1), pages); | |
| 609 | const items = rows.slice((page - 1) * limit, page * limit); | |
| 610 | return { items: await described(app, items, stamp), page, pages, total: rows.length }; | |
| 611 | } | |
| 612 | ||
| 579 | 613 | interface Place { |
| 580 | 614 | readonly key: number | string; |
| 581 | 615 | readonly kind: ExploreKind; |
| @@ -654,12 +688,16 @@ async function forges(app: App, viewer: User | undefined, query: ExploreQuery): | ||
| 654 | 688 | })); |
| 655 | 689 | } |
| 656 | 690 | |
| 691 | /** How many times the full ranking was computed (not read from the kept one); tests read it. */ | |
| 692 | export const rankWork = { runs: 0 }; | |
| 693 | ||
| 657 | 694 | function rank( |
| 658 | 695 | app: App, |
| 659 | 696 | viewer: User | undefined, |
| 660 | 697 | query: ExploreQuery, |
| 661 | 698 | now: number, |
| 662 | 699 | ): ExploreRow[] { |
| 700 | rankWork.runs++; | |
| 663 | 701 | const db = app.db; |
| 664 | 702 | const repos = listReposVisibleTo(db, viewer, { |
| 665 | 703 | limit: READ, |
src/web/studio/explore.ts +24 −17
| @@ -7,7 +7,8 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | import { |
| 10 | explorePage as readPage, | |
| 10 | explorePageAt, | |
| 11 | explorePageFrom, | |
| 11 | 12 | exploreLine, |
| 12 | 13 | exploreCount, |
| 13 | 14 | exploreFiltered, |
| @@ -32,9 +33,11 @@ import { compact } from "../compact.ts"; | ||
| 32 | 33 | import { page } from "../layout.ts"; |
| 33 | 34 | import { btn, card, empty, nameMark, row as rowOf, seg, tabs, time } from "../ui.ts"; |
| 34 | 35 | |
| 35 | const href = (query: ExploreQuery): string => { | |
| 36 | const params = exploreParams(query); | |
| 37 | return params === "" ? "/explore" : `/explore?${params}`; | |
| 36 | const href = (query: ExploreQuery, page = 1): string => { | |
| 37 | const params = new URLSearchParams(exploreParams(query)); | |
| 38 | if (page > 1) params.set("page", String(page)); | |
| 39 | const text = params.toString(); | |
| 40 | return text === "" ? "/explore" : `/explore?${text}`; | |
| 38 | 41 | }; |
| 39 | 42 | |
| 40 | 43 | function rowHtml(row: ExploreRow, signedIn: boolean, query: ExploreQuery, now: number, weights: FrontWeights): Html { |
| @@ -163,6 +166,16 @@ function countLine(query: ExploreQuery, total: number, seesForges: boolean): Htm | ||
| 163 | 166 | }</span>`; |
| 164 | 167 | } |
| 165 | 168 | |
| 169 | /** Previous and Next as plain links that keep the filters; nothing when one page holds all. */ | |
| 170 | function pager(query: ExploreQuery, page: number, pages: number): Html { | |
| 171 | if (pages <= 1) return html``; | |
| 172 | return html`<nav class="explore-pager" aria-label="Pages" data-pager> | |
| 173 | ${page > 1 ? btn("Previous", { href: href(query, page - 1), variant: "outline", extra: "explore-prev", rel: "prev" }) : html`<span></span>`} | |
| 174 | <span class="muted small">Page ${page} of ${pages}</span> | |
| 175 | ${page < pages ? btn("Next", { href: href(query, page + 1), variant: "outline", extra: "explore-next", rel: "next" }) : html`<span></span>`} | |
| 176 | </nav>`; | |
| 177 | } | |
outdated
Add rel="prev"/rel="next" to the links (btn extra attrs) so crawlers and the description match the markup. |
||
| 178 | ||
| 166 | 179 | // GET /explore |
| 167 | 180 | export async function explorePage(ctx: Ctx): Promise<Response> { |
| 168 | 181 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| @@ -171,14 +184,10 @@ export async function explorePage(ctx: Ctx): Promise<Response> { | ||
| 171 | 184 | const note = exploreNote(unknown); |
| 172 | 185 | const suggestion = exploreSuggestion(line); |
| 173 | 186 | const ranking = performance.now(); |
| 174 | // A cursor that is not one for this query (junk, over-long, another sort) shows the first page. | |
| 175 | const asked = ctx.url.searchParams.get("cursor"); | |
| 176 | const after = asked === null ? null : await readPage(ctx.app, ctx.auth.user, query, asked); | |
| 177 | const returned = asked !== null && after === null; | |
| 178 | const page0 = after ?? (await readPage(ctx.app, ctx.auth.user, query, null)); | |
| 179 | const here = href(query); | |
| 180 | const rows = page0?.items ?? []; | |
| 181 | const more = page0?.nextCursor ?? null; | |
| 187 | const shown = await explorePageAt(ctx.app, ctx.auth.user, query, explorePageFrom((name) => ctx.url.searchParams.get(name))); | |
Non-blocking: the note says "first page" even when ?page=N is also given and page N is shown. Reword or only show it when page is absent. |
||
| 188 | const rows = shown.items; | |
| 189 | // Old `?cursor=` links were published; they cannot name a page now, so say so. | |
| 190 | const oldLink = ctx.url.searchParams.has("cursor"); | |
| 182 | 191 | const weights = frontWeights(ctx.app.db); |
| 183 | 192 | notePhase(ctx.request, "read", performance.now() - ranking); |
| 184 | 193 | const rendering = performance.now(); |
| @@ -242,7 +251,7 @@ export async function explorePage(ctx: Ctx): Promise<Response> { | ||
| 242 | 251 | ])} |
| 243 | 252 | </div> |
| 244 | 253 | ${chipRow(query)} |
| 245 | ${returned ? html`<p class="muted mb" role="status">That page link is not one for this list, so this is the first page.</p>` : html``} | |
| 254 | ${oldLink ? html`<p class="muted mb" role="status" data-old-cursor>That page link is out of date, so this is the first page.</p>` : html``} | |
| 246 | 255 | ${note === "" ? html`` : html`<p class="muted mb" role="status">${note}${ |
| 247 | 256 | suggestion === undefined |
| 248 | 257 | ? html`` |
| @@ -273,15 +282,13 @@ export async function explorePage(ctx: Ctx): Promise<Response> { | ||
| 273 | 282 | > |
| 274 | 283 | </div> |
| 275 | 284 | </details> |
| 276 | <p class="mb small">${countLine(query, page0?.total ?? 0, ctx.auth.user?.isAdmin === true)}</p> | |
| 285 | <p class="mb small">${countLine(query, shown.total, ctx.auth.user?.isAdmin === true)}</p> | |
| 277 | 286 | ${rows.length === 0 |
| 278 | 287 | ? emptyState(ctx, query) |
| 279 | 288 | : card(html`<ul class="list">${rows.map((r) => rowHtml(r, ctx.auth.user !== undefined, query, now, weights))}</ul>`, { |
| 280 | 289 | pad: false, |
| 281 | 290 | extra: "bare", |
| 282 | })}${more === null | |
| 283 | ? html`` | |
| 284 | : html`<div class="explore-more">${btn("More results", { href: `${here}${here.includes("?") ? "&" : "?"}cursor=${encodeURIComponent(more)}`, variant: "outline", extra: "explore-more-link" })}</div>`}</div>`; | |
| 291 | })}${pager(query, shown.page, shown.pages)}</div>`; | |
| 285 | 292 | const drawn = page({ ctx, title: "Explore", description: "Apps, forges and people on this forge, ranked by use, forks, ships and freshness.", body: compact(body), feed: { href: "/explore.atom", title: "Explore" } }); |
| 286 | 293 | // An unknown filter is ignored with a plain note on the page (200); the API and the MCP stay |
| 287 | 294 | // strict. |
src/web/style/content.ts +2 −2
| @@ -300,8 +300,8 @@ form.inline { display: inline; margin-left: auto; } | ||
| 300 | 300 | what is below. */ |
| 301 | 301 | .sort-toggle { display: flex; align-items: center; min-height: 44px; padding: 0 4px; cursor: pointer; } |
| 302 | 302 | .sort-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: var(--radius-sm); } |
| 303 | .explore-more { margin-top: 12px; } | |
| 304 | .explore-more .btn { width: 100%; min-height: 44px; } | |
| 303 | .explore-pager { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 12px; } | |
| 304 | .explore-pager .btn { min-height: 44px; } | |
| 305 | 305 | .door-says { min-height: 1lh; margin: 6px 0 0; } |
| 306 | 306 | .door-agent { display: flex; flex-direction: column; gap: 8px; width: 100%; max-width: 520px; font-size: 13px; color: var(--muted-fg); } |
| 307 | 307 | .door-agent a { margin-left: 8px; color: var(--fg); text-decoration: underline; text-underline-offset: 3px; text-decoration-color: var(--border-strong); } |
test/explore-count.test.ts +2 −2
| @@ -72,10 +72,10 @@ test("a filter that leaves one says '1 app', singular", async () => { | ||
| 72 | 72 | expect(count(await get("/explore?q=kind:app ledger"))).toMatch(/^1 app\b(?!s)/); |
| 73 | 73 | }); |
| 74 | 74 | |
| 75 | test("the count is the whole filtered list on a page after a cursor", async () => { | |
| 75 | test("the count is the whole filtered list on a page after the next page", async () => { | |
| 76 | 76 | const first = await get("/explore?kind=app&q=bulk"); |
| 77 | 77 | expect(count(first)).toMatch(/^62 apps/); |
| 78 | const next = first.match(/href="([^"]*cursor=[^"]*)"/)?.[1]?.replaceAll("&", "&"); | |
| 78 | const next = first.match(/href="([^"]*page=2[^"]*)"/)?.[1]?.replaceAll("&", "&"); | |
| 79 | 79 | expect(next).toBeDefined(); |
| 80 | 80 | const second = await get(next!); |
| 81 | 81 | expect(count(second)).toMatch(/^62 apps/); |
test/explore-cursor-web.test.ts deleted −90
| @@ -1,90 +0,0 @@ | ||
| 1 | /** /explore follows its cursor without a script: a "More" link carries the filters to the next page. */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createRepo } from "../src/actions/repos.ts"; | |
| 8 | import { createApp as insertApp } from "../src/core/apps.ts"; | |
| 9 | import { createUser } from "../src/core/users.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | import { quietLogs } from "./helpers/globals.ts"; | |
| 12 | import { proveAll } from "./helpers/verified.ts"; | |
| 13 | import { promote } from "./helpers/members.ts"; | |
| 14 | ||
| 15 | quietLogs(); | |
| 16 | let dir: string; | |
| 17 | let app: App; | |
| 18 | let servers: Servers; | |
| 19 | const TOTAL = 65; | |
| 20 | ||
| 21 | beforeAll(async () => { | |
| 22 | dir = await mkdtemp(join(tmpdir(), "forge-explore-cursor-")); | |
| 23 | app = createApp({ dataDir: dir, port: 0 }); | |
| 24 | const made = await createUser(app.db, { origin: "seed", username: "olive", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: false }); | |
| 25 | if (!made.ok) throw new Error(made.error); | |
| 26 | proveAll(app); | |
| 27 | promote(app.db, made.value); | |
| 28 | for (let i = 0; i < TOTAL; i++) { | |
| 29 | const n = `app${String(i).padStart(3, "0")}`; | |
| 30 | const repo = await createRepo(app, made.value, { name: n, visibility: "public" }); | |
| 31 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 32 | const rec = insertApp(app.db, { repoId: repo.value.id, slug: `olive-${n}`, access: "public" }); | |
| 33 | if (!rec.ok) throw new Error(rec.error); | |
| 34 | } | |
| 35 | servers = await serve(app); | |
| 36 | }, 120_000); | |
| 37 | afterAll(async () => { | |
| 38 | await servers.stop(); | |
| 39 | app.db.close(); | |
| 40 | await rm(dir, { recursive: true, force: true }); | |
| 41 | }); | |
| 42 | ||
| 43 | const get = async (path: string) => { | |
| 44 | const res = await fetch(`${servers.url}${path}`); | |
| 45 | return { status: res.status, html: await res.text() }; | |
| 46 | }; | |
| 47 | const rows = (html: string): string[] => [...html.matchAll(/<a href="(\/olive\/app\d+)">/g)].map((m) => m[1]!); | |
| 48 | const more = (html: string): string[] => [...html.matchAll(/<a[^>]*href="([^"]*)"[^>]*explore-more-link/g)].map((m) => m[1]!.replaceAll("&", "&")); | |
| 49 | ||
| 50 | test("walking the More link reads every row once and the last page has none", async () => { | |
| 51 | const seen: string[] = []; | |
| 52 | let path: string | undefined = "/explore?kind=app&owner=olive&sort=name"; | |
| 53 | let pages = 0; | |
| 54 | while (path !== undefined) { | |
| 55 | const { status, html } = await get(path); | |
| 56 | expect(status).toBe(200); | |
| 57 | seen.push(...rows(html)); | |
| 58 | const links = more(html); | |
| 59 | expect(links.length).toBeLessThanOrEqual(1); | |
| 60 | if (links[0] !== undefined) { | |
| 61 | expect(links[0]).toContain("kind=app"); | |
| 62 | expect(links[0]).toContain("owner=olive"); | |
| 63 | expect(links[0]).toContain("sort=name"); | |
| 64 | } | |
| 65 | path = links[0]; | |
| 66 | expect(++pages).toBeLessThan(10); | |
| 67 | } | |
| 68 | expect(pages).toBe(2); | |
| 69 | expect(seen.length).toBe(TOTAL); | |
| 70 | expect(new Set(seen).size).toBe(TOTAL); | |
| 71 | }); | |
| 72 | ||
| 73 | test("a junk, over-long or other-sort cursor shows the first page", async () => { | |
| 74 | const first = await get("/explore?kind=app&owner=olive&sort=name"); | |
| 75 | const link = more(first.html)[0]!; | |
| 76 | const cursor = new URL(link, "http://x").searchParams.get("cursor")!; | |
| 77 | for (const bad of ["!!notbase64!!", "A".repeat(5000), "e30", encodeURIComponent(cursor) + "x"]) { | |
| 78 | const res = await get(`/explore?kind=app&owner=olive&sort=name&cursor=${bad}`); | |
| 79 | expect(res.status).toBe(200); | |
| 80 | expect(rows(res.html)).toEqual(rows(first.html)); | |
| 81 | expect(res.html).toContain("is not one for this list"); | |
| 82 | } | |
| 83 | // A name-sort cursor under another sort is refused the same way. | |
| 84 | const other = await get(`/explore?kind=app&owner=olive&sort=forks&cursor=${encodeURIComponent(cursor)}`); | |
| 85 | expect(other.status).toBe(200); | |
| 86 | expect(rows(other.html).length).toBe(60); | |
| 87 | expect(other.html).toContain("is not one for this list"); | |
| 88 | expect(first.html).not.toContain("is not one for this list"); | |
| 89 | expect(first.html).toContain("More results"); | |
| 90 | }); |
test/explore-pages-web.test.ts added +140
| @@ -0,0 +1,140 @@ | ||
| 1 | /** /explore pages with plain Previous and Next links: they carry the filters to the next page. */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createRepo } from "../src/actions/repos.ts"; | |
| 8 | import { createApp as insertApp } from "../src/core/apps.ts"; | |
| 9 | import { createUser } from "../src/core/users.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | import { quietLogs } from "./helpers/globals.ts"; | |
| 12 | import { proveAll } from "./helpers/verified.ts"; | |
| 13 | import { rankWork } from "../src/actions/explore.ts"; | |
| 14 | import { id } from "../src/kernel/id.ts"; | |
| 15 | import { promote } from "./helpers/members.ts"; | |
| 16 | ||
| 17 | quietLogs(); | |
| 18 | let dir: string; | |
| 19 | let app: App; | |
| 20 | let servers: Servers; | |
| 21 | const TOTAL = 65; | |
| 22 | ||
| 23 | beforeAll(async () => { | |
| 24 | dir = await mkdtemp(join(tmpdir(), "forge-explore-cursor-")); | |
| 25 | app = createApp({ dataDir: dir, port: 0 }); | |
| 26 | const made = await createUser(app.db, { origin: "seed", username: "olive", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: false }); | |
| 27 | if (!made.ok) throw new Error(made.error); | |
| 28 | proveAll(app); | |
| 29 | promote(app.db, made.value); | |
| 30 | for (let i = 0; i < TOTAL; i++) { | |
| 31 | const n = `app${String(i).padStart(3, "0")}`; | |
| 32 | const repo = await createRepo(app, made.value, { name: n, visibility: "public" }); | |
| 33 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 34 | const rec = insertApp(app.db, { repoId: repo.value.id, slug: `olive-${n}`, access: "public" }); | |
| 35 | if (!rec.ok) throw new Error(rec.error); | |
| 36 | } | |
| 37 | servers = await serve(app); | |
| 38 | }, 120_000); | |
| 39 | afterAll(async () => { | |
| 40 | await servers.stop(); | |
| 41 | app.db.close(); | |
| 42 | await rm(dir, { recursive: true, force: true }); | |
| 43 | }); | |
| 44 | ||
| 45 | const get = async (path: string) => { | |
| 46 | const res = await fetch(`${servers.url}${path}`); | |
| 47 | return { status: res.status, html: await res.text() }; | |
| 48 | }; | |
| 49 | const rows = (html: string): string[] => [...html.matchAll(/<a href="(\/(?:olive\/app|busy\/bulk)\d+)">/g)].map((m) => m[1]!); | |
| 50 | const link = (html: string, cls: string): string | undefined => | |
| 51 | [...html.matchAll(new RegExp(`<a[^>]*href="([^"]*)"[^>]*${cls}`, "g"))].map((m) => m[1]!.replaceAll("&", "&"))[0]; | |
| 52 | ||
| 53 | test("walking Next reads every row once; filters survive Next and Previous", async () => { | |
| 54 | const seen: string[] = []; | |
| 55 | let path: string | undefined = "/explore?kind=app&owner=olive&sort=name"; | |
| 56 | let pages = 0; | |
| 57 | while (path !== undefined) { | |
| 58 | const { status, html } = await get(path); | |
| 59 | expect(status).toBe(200); | |
| 60 | seen.push(...rows(html)); | |
| 61 | const next = link(html, "explore-next"); | |
| 62 | if (next !== undefined) { | |
| 63 | expect(next).toContain("kind=app"); | |
| 64 | expect(next).toContain("owner=olive"); | |
| 65 | expect(next).toContain("sort=name"); | |
| 66 | expect(next).toContain("page=2"); | |
| 67 | } | |
| 68 | path = next; | |
| 69 | expect(++pages).toBeLessThan(10); | |
| 70 | } | |
| 71 | expect(pages).toBe(2); | |
| 72 | expect(seen.length).toBe(TOTAL); | |
| 73 | expect(new Set(seen).size).toBe(TOTAL); | |
| 74 | const second = await get("/explore?kind=app&owner=olive&sort=name&page=2"); | |
| 75 | const prev = link(second.html, "explore-prev")!; | |
| 76 | expect(prev).toContain("sort=name"); | |
| 77 | expect(prev).not.toContain("page="); | |
| 78 | expect(link(second.html, "explore-next")).toBeUndefined(); | |
| 79 | expect(rows(second.html).length).toBe(5); | |
| 80 | }); | |
| 81 | ||
| 82 | test("a junk page shows the first, an out-of-range page the last, never an error", async () => { | |
| 83 | const first = await get("/explore?kind=app&owner=olive&sort=name"); | |
| 84 | expect(first.html).toContain("Page 1 of 2"); | |
| 85 | for (const bad of ["abc", "0", "-3", "1.5", "%00", "9".repeat(5000), ""]) { | |
| 86 | const res = await get(`/explore?kind=app&owner=olive&sort=name&page=${bad}`); | |
| 87 | expect(res.status).toBe(200); | |
| 88 | expect(rows(res.html)).toEqual(rows(first.html)); | |
outdated
Nothing covers 200 apps / the speed of page 2 now. Add a work-count test (ranking computed once across page 1 and 2) instead of a clock bound, with 200 apps. |
||
| 89 | } | |
| 90 | const last = await get("/explore?kind=app&owner=olive&sort=name&page=999"); | |
| 91 | expect(last.status).toBe(200); | |
| 92 | expect(last.html).toContain("Page 2 of 2"); | |
| 93 | expect(rows(last.html).length).toBe(5); | |
| 94 | }); | |
| 95 | ||
| 96 | test("an old ?cursor= link says it is out of date and shows the first page", async () => { | |
| 97 | const first = await get("/explore?kind=app&owner=olive&sort=name"); | |
| 98 | expect(first.html).not.toContain("data-old-cursor"); | |
| 99 | const old = await get("/explore?kind=app&owner=olive&sort=name&cursor=abc"); | |
| 100 | expect(old.status).toBe(200); | |
| 101 | expect(old.html).toContain("data-old-cursor"); | |
| 102 | expect(old.html).toContain("out of date"); | |
| 103 | expect(rows(old.html)).toEqual(rows(first.html)); | |
| 104 | }); | |
| 105 | ||
| 106 | test("the pager links say rel=next and rel=prev", async () => { | |
| 107 | const one = await get("/explore?kind=app&owner=olive&sort=name"); | |
| 108 | expect(one.html).toMatch(/<a[^>]*rel="next"/); | |
| 109 | expect(one.html).not.toMatch(/rel="prev"/); | |
| 110 | const two = await get("/explore?kind=app&owner=olive&sort=name&page=2"); | |
| 111 | expect(two.html).toMatch(/<a[^>]*rel="prev"/); | |
| 112 | }); | |
| 113 | ||
| 114 | test("200 apps: the ranking is computed once across pages 1, 2 and 4", async () => { | |
| 115 | const owner = await createUser(app.db, { origin: "seed", username: "busy", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: false }); | |
| 116 | if (!owner.ok) throw new Error(owner.error); | |
| 117 | proveAll(app); | |
| 118 | ||
| 119 | // Rows only (no git directory): the explore list reads the database, and 200 inits are slow. | |
| 120 | for (let i = 0; i < 200; i++) { | |
| 121 | const n = `bulk${String(i).padStart(3, "0")}`; | |
| 122 | const repoId = id(); | |
| 123 | app.db | |
| 124 | .query("INSERT INTO repo (id, owner_id, name, visibility, description, default_branch) VALUES (?1, ?2, ?3, 'public', '', 'main')") | |
| 125 | .run(repoId, owner.value.id, n); | |
| 126 | const rec = insertApp(app.db, { repoId, slug: `busy-${n}`, access: "public" }); | |
| 127 | if (!rec.ok) throw new Error(rec.error); | |
| 128 | } | |
| 129 | const q = "/explore?kind=app&owner=busy&sort=name"; | |
| 130 | await get(q); // warms the kept ranking | |
| 131 | const before = rankWork.runs; | |
| 132 | const p1 = await get(q); | |
| 133 | const p2 = await get(`${q}&page=2`); | |
| 134 | const p4 = await get(`${q}&page=4`); | |
| 135 | expect(p1.html).toContain("Page 1 of 4"); | |
| 136 | expect(p4.html).toContain("Page 4 of 4"); | |
| 137 | expect(new Set([...rows(p1.html), ...rows(p2.html)]).size).toBe(120); | |
| 138 | expect(rows(p4.html).length).toBe(20); | |
| 139 | expect(rankWork.runs).toBe(before); | |
| 140 | }, 120_000); |
test/explore-since.test.ts +2 −2
| @@ -95,10 +95,10 @@ test("the active window is marked, Any time clears it, sort links keep it", asyn | ||
| 95 | 95 | expect(anchor(off, "Any time")).toContain('aria-current="page"'); |
| 96 | 96 | }); |
| 97 | 97 | |
| 98 | test("the More results link keeps since and leads to the rest of the window", async () => { | |
| 98 | test("the Next link keeps since and leads to the rest of the window", async () => { | |
| 99 | 99 | const first = await get("?since=7d&kind=app&owner=olive"); |
| 100 | 100 | expect(rows(first).length).toBe(60); |
| 101 | const more = /<a[^>]*href="[^"]*cursor=[^"]*"[^>]*>/.exec(first); | |
| 101 | const more = /<a[^>]*href="[^"]*page=2[^"]*"[^>]*>/.exec(first); | |
| 102 | 102 | const link = hrefOf(more![0]); |
| 103 | 103 | expect(link).toContain("since=7d"); |
| 104 | 104 | const second = await (await fetch(`${servers.url}${link}`)).text(); |
new above
offline
Non-blocking: rankWork is test instrumentation exported from production code and incremented on the hot path. Consider an injected hook instead.