Public: anyone can read this; the crew can change it.
/explore can be filtered to apps changed this week with one chip, and the chip is a link that survives a reload #15883
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15848-explore-can-be-filtered-to-apps-changed → main 0 commits fork 2 threads · 2 unresolved
5 files
changed +137
−17
docs/features/explore.md +2 −2
| @@ -55,7 +55,7 @@ What a person filters on `/explore` an agent filters with the same words. Each f | ||
| 55 | 55 | |---|---|---|---| |
| 56 | 56 | | Best / Forks / Shipped / Liked / Use / Fresh / Newest | `sort=score` `forks` `ships` `likes` `use` `fresh` `new` (`name` by hand) | `?sort=forks` | `{ sort: "forks" }` | |
| 57 | 57 | | All / Apps / People / Forges (admin) | `kind=app` `person` `forge` | `?kind=app` | `{ kind: "app" }` | |
| 58 | | Any time / Today / This week / This month | `since=1d` `7d` `30d` (`updated=` is the same; `week=1` and `fresh=7` are `7d`, any other `fresh` is ignored, and `updated`/`since` win beside it) | `?since=7d` | `{ since: "7d" }` | | |
| 58 | | Any time / Today / This week / This month | `since=1d` `7d` `30d`, or any whole `Nd` from 1 to 365 such as `since=45d` (`updated=` is the same; `week=1` is `7d`; `fresh=N` is any whole number of days 1 to 365, anything else in `fresh` is ignored, and `updated`/`since` win beside it; the terminal line takes `updated:45d`) | `?since=7d` | `{ since: "7d" }` | | |
| 59 | 59 | | Show archived | `archived=yes` | `?archived=yes` | `{ archived: "yes" }` | |
| 60 | 60 | | (owner link on a row) | `owner=name` | `?owner=name` | `{ owner: "name" }` | |
| 61 | 61 | | (type it) at least N forks by people | `forks=3` (0–1000) | `?forks=3` | `{ forks: "3" }` | |
| @@ -69,7 +69,7 @@ What a person filters on `/explore` an agent filters with the same words. Each f | ||
| 69 | 69 | |
| 70 | 70 | The one line still works everywhere: `?q=kind:app sort:forks garden` and `{ q: "kind:app sort:forks garden" }` read the same as the separate parameters, and old `/explore?q=…` links keep working. When a parameter and the same key in `q` are both given, the parameter wins (`?q=sort:name&sort=forks` sorts by forks). A parameter's value is one word; one the explorer does not take is an unknown word like any typed in `q`, and is refused the same way (below). The terminal's search line takes only the line. It takes `forks:N`, `ships:N` and `live:yes` there too (`screens/dashboard.ts` reads the line with `readExploreLine`): `forks:1 live:no` leaves the rows the page leaves, and `forks:abc` or `live:maybe` is named under `Read as:` as `forks:abc is not a count`, `live:maybe is not an answer`. |
| 71 | 71 | |
| 72 | A bad `min_forks`, `since`, `updated` or `kind` parameter (`?since=9y`, `?kind=nope`) is refused naming the parameter (#14046, #14392): the API answers `422`: `{ "error": "invalid", "field": "since", "message": "since is not a window — use 1d, 7d, 30d" }`. The page answers `303` to the same address without it, then draws the same sentence once over the unfiltered list; a bad `kind` is the exception (#15653): the page answers `400` and draws `kind is not a kind — use app, person, forge` over the unfiltered list, with no redirect (still `422` from the API). A bad `kind:` typed in `q` takes the `303` like any unknown word (`test/explore-kind.test.ts`). The sentence names the parameter and never repeats the value. `since` is judged only when `updated` is not given, `min_forks` only when `forks` is not. | |
| 72 | A bad `min_forks`, `since`, `updated` or `kind` parameter (`?since=9y`, `?kind=nope`) is refused naming the parameter (#14046, #14392): the API answers `422`: `{ "error": "invalid", "field": "since", "message": "since is not a window — use 1d, 7d, 30d, or any whole Nd from 1d to 365d" }`. The page answers `303` to the same address without it, then draws the same sentence once over the unfiltered list; a bad `kind` is the exception (#15653): the page answers `400` and draws `kind is not a kind — use app, person, forge` over the unfiltered list, with no redirect (still `422` from the API). A bad `kind:` typed in `q` takes the `303` like any unknown word (`test/explore-kind.test.ts`). The sentence names the parameter and never repeats the value. `since` is judged only when `updated` is not given, `min_forks` only when `forks` is not. | |
| 73 | 73 | |
| 74 | 74 | The three return the same ordered rows (`test/explore-parity.test.ts`; the URL form in `test/explore-url.test.ts`). |
| 75 | 75 |
src/actions/explore.ts +37 −10
| @@ -86,13 +86,37 @@ function countOf(value: string): number | undefined { | ||
| 86 | 86 | return n <= COUNT_MAX ? n : undefined; |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | export type ExploreWindow = "all" | "1d" | "7d" | "30d"; | |
| 89 | export type ExploreWindow = "all" | `${number}d`; | |
| 90 | 90 | const WINDOWS: Record<string, number> = { |
| 91 | 91 | "1d": 86_400_000, |
| 92 | 92 | "7d": 7 * 86_400_000, |
| 93 | 93 | "30d": 30 * 86_400_000, |
| 94 | 94 | }; |
| 95 | 95 | |
| 96 | /** | |
| 97 | * The days in a window word: a named one, or any whole `Nd` from 1 to 365 (`?fresh=45`); else | |
| 98 | * undefined. | |
| 99 | */ | |
| 100 | const WINDOW_DAYS_MAX = 365; | |
| 101 | /** | |
| 102 | * A whole number of days from 1 to WINDOW_DAYS_MAX, or undefined: the one bound `Nd` and `fresh=N` | |
| 103 | * share. | |
| 104 | */ | |
| 105 | function daysOf(text: string): number | undefined { | |
| 106 | if (!/^\d{1,3}$/.test(text)) return undefined; | |
| 107 | const n = Number(text); | |
| 108 | return n >= 1 && n <= WINDOW_DAYS_MAX ? n : undefined; | |
| 109 | } | |
| 110 | /** What a refused window is told it may be. */ | |
| 111 | const WINDOW_HINT = `${Object.keys(WINDOWS).join(", ")}, or any whole Nd from 1d to ${WINDOW_DAYS_MAX}d`; | |
| 112 | ||
| 113 | function windowMs(word: string): number | undefined { | |
| 114 | const w = word.toLowerCase(); | |
| 115 | if (Object.hasOwn(WINDOWS, w)) return WINDOWS[w]; | |
| 116 | const days = daysOf(w.endsWith("d") ? w.slice(0, -1) : ""); | |
| 117 | return days === undefined ? undefined : days * 86_400_000; | |
| 118 | } | |
| 119 | ||
| 96 | 120 | const KINDS: Record<string, ExploreKind> = { |
| 97 | 121 | app: "app", |
| 98 | 122 | apps: "app", |
| @@ -147,8 +171,8 @@ export function badExploreParam(get: (name: string) => string | null | undefined | ||
| 147 | 171 | if (minForks !== "" && given("forks") === "" && countOf(minForks) === undefined) |
| 148 | 172 | return { field: "min_forks", message: `min_forks is not a count — use a whole number from 0 to ${COUNT_MAX}` }; |
| 149 | 173 | const window = given("updated") !== "" ? "updated" : "since"; |
| 150 | if (given(window) !== "" && !Object.hasOwn(WINDOWS, given(window).toLowerCase())) | |
| 151 | return { field: window, message: `${window} is not a window — use ${Object.keys(WINDOWS).join(", ")}` }; | |
| 174 | if (given(window) !== "" && windowMs(given(window)) === undefined) | |
| 175 | return { field: window, message: `${window} is not a window — use ${WINDOW_HINT}` }; | |
| 152 | 176 | if (given("kind") !== "" && !Object.hasOwn(KINDS, given("kind").toLowerCase())) |
| 153 | 177 | return { field: "kind", message: `kind is not a kind — use ${KIND_WORDS.join(", ")}` }; |
| 154 | 178 | return null; |
| @@ -172,10 +196,13 @@ export function exploreLineFrom(get: (name: string) => string | null | undefined | ||
| 172 | 196 | if (key === "live") value = LIVE_WORDS[value.toLowerCase()] ?? ""; |
| 173 | 197 | if (value !== "") given.set(key, value); |
| 174 | 198 | } |
| 175 | // `week=1` and `fresh=7` (#15088) are the same 7-day window; any other value is ignored, not an | |
| 176 | // error. | |
| 177 | if (!given.has("updated") && ((get("week") ?? "").trim() === "1" || (get("fresh") ?? "").trim() === "7")) { | |
| 178 | given.set("updated", "7d"); | |
| 199 | // `week=1` is the 7-day window, and `fresh=N` (#15088, #15848) any whole N days from 1 to 365; a | |
| 200 | // non-number, zero or one over 365 is ignored, not an error. | |
| 201 | if (!given.has("updated")) { | |
| 202 | const fresh = (get("fresh") ?? "").trim(); | |
| 203 | const days = daysOf(fresh); | |
| 204 | if ((get("week") ?? "").trim() === "1") given.set("updated", "7d"); | |
| 205 | else if (days !== undefined) given.set("updated", `${days}d`); | |
| 179 | 206 | } |
| 180 | 207 | // A parameter wins: the same key typed in `q` is dropped, not left to the reader's order. |
| 181 | 208 | const typed = cappedQuery(get("q")) |
| @@ -262,7 +289,7 @@ export function readExploreLine(line: string): ExploreRead { | ||
| 262 | 289 | if (["yes", "no"].includes(value.toLowerCase())) archived = value.toLowerCase() === "yes"; |
| 263 | 290 | else unknown.push(word); |
| 264 | 291 | } else if (k === "updated" && value !== "") { |
| 265 | if (Object.hasOwn(WINDOWS, value.toLowerCase())) updated = value.toLowerCase() as ExploreWindow; | |
| 292 | if (windowMs(value) !== undefined) updated = value.toLowerCase() as ExploreWindow; | |
| 266 | 293 | else unknown.push(word); |
| 267 | 294 | } else if (k === "owner" && value !== "") { |
| 268 | 295 | if (isSafeName(value)) owner = value; |
| @@ -315,7 +342,7 @@ export function exploreNote(unknown: readonly string[]): string { | ||
| 315 | 342 | const key = word.slice(0, word.indexOf(":")).toLowerCase(); |
| 316 | 343 | const shown = word.length > 40 ? `${word.slice(0, 39)}…` : word; |
| 317 | 344 | if (key === "owner") return `${shown} is not a name`; |
| 318 | if (key === "updated") return `${shown} is not a window — use ${Object.keys(WINDOWS).join(", ")}`; | |
| 345 | if (key === "updated") return `${shown} is not a window — use ${WINDOW_HINT}`; | |
| 319 | 346 | if (key === "archived" || key === "live" || key === "mine") return `${shown} is not an answer — use yes, no`; |
| 320 | 347 | if (key === "template") return `${shown} is not a template — use a template id or owner/name`; |
| 321 | 348 | if (key === "forks" || key === "ships") return `${shown} is not a count — use a whole number from 0 to ${COUNT_MAX}`; |
| @@ -1127,7 +1154,7 @@ function movedWithin(row: ExploreRow, query: ExploreQuery, now: number): boolean | ||
| 1127 | 1154 | if (query.updated === "all") return true; |
| 1128 | 1155 | if (row.shippedAt === null) return false; |
| 1129 | 1156 | const at = Date.parse(row.shippedAt); |
| 1130 | return now - at <= WINDOWS[query.updated]!; | |
| 1157 | return now - at <= windowMs(query.updated)!; | |
| 1131 | 1158 | } |
| 1132 | 1159 | |
| 1133 | 1160 | /** Rows sorted as the query says, in place. */ |
test/explore-fresh.test.ts added +93
| @@ -0,0 +1,93 @@ | ||
| 1 | /** /explore?fresh=N and /explore.json?fresh=N keep apps changed in the last N days (#15848). */ | |
| 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 { createIssue } from "../src/core/issues.ts"; | |
| 10 | import { createUser } from "../src/core/users.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | import { proveAll } from "./helpers/verified.ts"; | |
| 14 | import { promote } from "./helpers/members.ts"; | |
| 15 | ||
| 16 | quietLogs(); | |
| 17 | let dir: string; | |
| 18 | let app: App; | |
| 19 | let servers: Servers; | |
| 20 | const DAY = 86_400_000; | |
| 21 | ||
| 22 | beforeAll(async () => { | |
| 23 | dir = await mkdtemp(join(tmpdir(), "forge-explore-fresh-")); | |
| 24 | app = createApp({ dataDir: dir, port: 0 }); | |
| 25 | const made = await createUser(app.db, { origin: "seed", username: "olive", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: false }); | |
| 26 | if (!made.ok) throw new Error(made.error); | |
| 27 | proveAll(app); | |
| 28 | promote(app.db, made.value); | |
| 29 | for (const [n, ago] of [["recent", 2], ["old", 20]] as const) { | |
| 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 | const issue = createIssue(app.db, { repoId: repo.value.id, authorId: made.value.id, title: "change", kind: "pull" }); | |
| 35 | if (!issue.ok) throw new Error(issue.error); | |
| 36 | app.db.query(`INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at) VALUES (?1, 'main', 'b', '', '', ?2)`).run(issue.value.id, new Date(Date.now() - ago * DAY).toISOString()); | |
| 37 | } | |
| 38 | servers = await serve(app); | |
| 39 | }, 120_000); | |
| 40 | afterAll(async () => { | |
| 41 | await servers.stop(); | |
| 42 | app.db.close(); | |
| 43 | await rm(dir, { recursive: true, force: true }); | |
| 44 | }); | |
| 45 | ||
| 46 | const page = async (qs: string): Promise<string[]> => { | |
| 47 | const res = await fetch(`${servers.url}/explore?kind=app${qs}`); | |
| 48 | expect(res.status).toBe(200); | |
| 49 | const html = await res.text(); | |
| 50 | const list = /<ul class="list">([\s\S]*?)<\/ul>/.exec(html); | |
| 51 | return list === null ? [] : [...list[1]!.matchAll(/<a href="\/olive\/([^"?]*)">/g)].map((m) => m[1]!).filter((v, i, a) => a.indexOf(v) === i).sort(); | |
| 52 | }; | |
| 53 | const api = async (qs: string): Promise<string[]> => { | |
| 54 | const res = await fetch(`${servers.url}/explore.json?kind=app${qs}`); | |
| 55 | expect(res.status).toBe(200); | |
| 56 | const body = (await res.json()) as { items: { name?: string; repo?: { name: string } }[] }; | |
| 57 | return body.items.map((i) => i.repo?.name ?? i.name ?? "").sort(); | |
| 58 | }; | |
| 59 | ||
| 60 | test("fresh=7 keeps the recent app on the page and in the JSON", async () => { | |
| 61 | expect(await page("")).toEqual(["old", "recent"]); | |
| 62 | expect(await page("&fresh=7")).toEqual(["recent"]); | |
| 63 | expect(await api("")).toEqual(["old", "recent"]); | |
| 64 | expect(await api("&fresh=7")).toEqual(["recent"]); | |
| 65 | }); | |
| 66 | ||
| 67 | test("fresh takes any whole number of days to 365", async () => { | |
| 68 | expect(await page("&fresh=1")).toEqual([]); | |
| 69 | expect(await page("&fresh=30")).toEqual(["old", "recent"]); | |
| 70 | expect(await api("&fresh=15")).toEqual(["recent"]); | |
| 71 | expect(await api("&fresh=365")).toEqual(["old", "recent"]); | |
| 72 | }); | |
| 73 | ||
| 74 | test("a non-number or a number over 365 is ignored, not a 500", async () => { | |
| 75 | for (const v of ["abc", "366", "0", "-3", "1.5", "99999999"]) { | |
| 76 | expect(await page(`&fresh=${v}`)).toEqual(["old", "recent"]); | |
| 77 | expect(await api(`&fresh=${v}`)).toEqual(["old", "recent"]); | |
| 78 | } | |
| 79 | }); | |
| 80 | ||
| 81 | test("updated and since take any Nd to 365; one above is a 422 that says so", async () => { | |
| 82 | expect(await api("&updated=45d")).toEqual(["old", "recent"]); | |
| 83 | expect(await api("&since=15d")).toEqual(["recent"]); | |
| 84 | for (const key of ["updated", "since"]) { | |
| 85 | for (const v of ["366d", "0d", "1000d"]) { | |
| 86 | const res = await fetch(`${servers.url}/explore.json?kind=app&${key}=${v}`); | |
| 87 | expect(res.status).toBe(422); | |
| 88 | const body = (await res.json()) as { field: string; message: string }; | |
| 89 | expect(body.field).toBe(key); | |
| 90 | expect(body.message).toBe(`${key} is not a window — use 1d, 7d, 30d, or any whole Nd from 1d to 365d`); | |
| 91 | } | |
| 92 | } | |
| 93 | }); |
test/explore-quick-filters.test.ts +1 −1
| @@ -87,7 +87,7 @@ test("fresh=7 is the same week; off and any other value narrow nothing (#15088)" | ||
| 87 | 87 | expect(link(on, "This week").tag).toContain('aria-current="page"'); |
| 88 | 88 | // The chip toggles it off: following its link leaves the week out. |
| 89 | 89 | expect(names((await get(link(on, "Any time").href.replace("/explore", ""))).html)).toEqual(ALL); |
| 90 | for (const v of ["", "0", "8", "week", "-7", "7d"]) expect(names((await get(`?kind=app&fresh=${v}`)).html)).toEqual(ALL); | |
| 90 | for (const v of ["", "0", "366", "week", "-7", "7d"]) expect(names((await get(`?kind=app&fresh=${v}`)).html)).toEqual(ALL); | |
| 91 | 91 | }); |
| 92 | 92 | |
| 93 | 93 | test("both together intersect", async () => { |
test/explore-unknown-words.test.ts +4 −4
| @@ -41,7 +41,7 @@ describe("readExploreLine", () => { | ||
| 41 | 41 | expect(read.unknown).toEqual(["updated:2y"]); |
| 42 | 42 | expect(read.query.updated).toBe("all"); |
| 43 | 43 | expect(exploreLine(read.query)).toBe("garden"); |
| 44 | expect(exploreNote(read.unknown)).toBe("updated:2y is not a window — use 1d, 7d, 30d"); | |
| 44 | expect(exploreNote(read.unknown)).toBe("updated:2y is not a window — use 1d, 7d, 30d, or any whole Nd from 1d to 365d"); | |
| 45 | 45 | }); |
| 46 | 46 | |
| 47 | 47 | test("an unknown archived: is returned, left out, and worded", () => { |
| @@ -76,7 +76,7 @@ describe("readExploreLine", () => { | ||
| 76 | 76 | const res = await fetch(`${s.url}/api/v1/explore?q=${q}`); |
| 77 | 77 | expect(res.status).toBe(422); |
| 78 | 78 | const body = (await res.json()) as { message: string }; |
| 79 | expect(body.message).toBe("updated:2y is not a window — use 1d, 7d, 30d; archived:maybe is not an answer — use yes, no"); | |
| 79 | expect(body.message).toBe("updated:2y is not a window — use 1d, 7d, 30d, or any whole Nd from 1d to 365d; archived:maybe is not an answer — use yes, no"); | |
| 80 | 80 | } finally { |
| 81 | 81 | await s.stop(); |
| 82 | 82 | a.db.close(); |
| @@ -121,7 +121,7 @@ describe("the page and the API", () => { | ||
| 121 | 121 | |
| 122 | 122 | test("the page says an updated:, archived: or owner: word once, escaped", async () => { |
| 123 | 123 | const page = async (q: string) => await seen(`${servers.url}/explore?q=${encodeURIComponent(q)}`); |
| 124 | expect(await page("updated:2y")).toContain("updated:2y is not a window — use 1d, 7d, 30d"); | |
| 124 | expect(await page("updated:2y")).toContain("updated:2y is not a window — use 1d, 7d, 30d, or any whole Nd from 1d to 365d"); | |
| 125 | 125 | expect(await page("archived:maybe")).toContain("archived:maybe is not an answer — use yes, no"); |
| 126 | 126 | const out = await page("owner:<b>"); |
| 127 | 127 | expect(out).toContain("owner:<b> is not a name"); |
| @@ -173,7 +173,7 @@ describe("the page and the API", () => { | ||
| 173 | 173 | expect(fine.isError).toBe(false); |
| 174 | 174 | expect(JSON.parse(fine.text)).toHaveProperty("items"); |
| 175 | 175 | expect(await call("archived:maybe")).toEqual({ isError: true, text: expect.stringContaining("unknown_filter: archived:maybe is not an answer — use yes, no") }); |
| 176 | expect(await call("updated:2y")).toEqual({ isError: true, text: expect.stringContaining("unknown_filter: updated:2y is not a window — use 1d, 7d, 30d") }); | |
| 176 | expect(await call("updated:2y")).toEqual({ isError: true, text: expect.stringContaining("unknown_filter: updated:2y is not a window — use 1d, 7d, 30d, or any whole Nd from 1d to 365d") }); | |
| 177 | 177 | expect(await call("owner:a/b")).toEqual({ isError: true, text: expect.stringContaining("unknown_filter: owner:a/b is not a name") }); |
| 178 | 178 | }); |
| 179 | 179 | }); |
new above
offline