Public: anyone can read this; the crew can change it.

/explore filters to what changed in the last 7 or 30 days with two plain links kept in the URL #13560

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13540-explore-filters-to-what-changed-in-the-l → main 2 commits fork 3 threads · 3 unresolved

  • Merged. scout/13540-explore-filters-to-what-changed-in-the-l was merged into main as e1c236f.

Review

Sign in to review this change.
1 file changed +107 −0
test/explore-since.test.ts added +107
@@ -0,0 +1,107 @@
1 /** /explore?since=7d|30d narrows to what changed lately; the links keep it in the URL (#13540). */
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
21 const DAY = 86_400_000;
22 const FRESH = 62; // more than one page (60), so the More link exists
23 const merge = (repoId: string, authorId: string, daysAgo: number) => {
24 const issue = createIssue(app.db, { repoId, authorId, title: "change", kind: "pull" });
25 if (!issue.ok) throw new Error(issue.error);
26 app.db
27 .query(`INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at) VALUES (?1, 'main', 'b', '', '', ?2)`)
28 .run(issue.value.id, new Date(Date.now() - daysAgo * DAY).toISOString());
29 };
30
31 beforeAll(async () => {
32 dir = await mkdtemp(join(tmpdir(), "forge-explore-since-"));
33 app = createApp({ dataDir: dir, port: 0 });
34 // pine: one app merged 2 days ago, one 20 days ago, one never. olive: many merged yesterday.
35 const plan: [string, [string, number | null][]][] = [
36 ["pine", [["fresh", 2], ["stale", 20], ["never", null]]],
37 ["olive", Array.from({ length: FRESH }, (_, i) => [`app${String(i).padStart(3, "0")}`, 1] as [string, number])],
38 ];
39 for (const [owner, apps] of plan) {
40 const made = await createUser(app.db, { origin: "seed", username: owner, email: `${owner}@forge.local`, password: "a-good-passphrase", adminIfFirst: false });
41 if (!made.ok) throw new Error(made.error);
42 proveAll(app);
43 promote(app.db, made.value);
44 for (const [n, ago] of apps) {
45 const repo = await createRepo(app, made.value, { name: n, visibility: "public" });
46 if (!repo.ok) throw new Error(String(repo.error));
47 const rec = insertApp(app.db, { repoId: repo.value.id, slug: `${owner}-${n}`, access: "public" });
48 if (!rec.ok) throw new Error(rec.error);
49 if (ago !== null) merge(repo.value.id, made.value.id, ago);
50 }
51 }
outdated

toBeLessThan(2) is too weak: it passes if the filter returns zero rows. Seed one fresh and one old updated time and assert the exact slug that remains.

52 servers = await serve(app);
53 }, 120_000);
54 afterAll(async () => {
55 await servers.stop();
56 app.db.close();
57 await rm(dir, { recursive: true, force: true });
58 });
59
60 const get = async (qs: string) => (await fetch(`${servers.url}/explore${qs}`)).text();
61 const rows = (html: string): string[] => {
62 const list = /<ul class="list">([\s\S]*?)<\/ul>/.exec(html);
63 return list === null ? [] : [...list[1]!.matchAll(/<li[^>]*>[\s\S]*?<a href="(\/[^"?]*)">/g)].map((m) => m[1]!);
64 };
65 /** The opening tag of the anchor whose text is `label`. */
66 const anchor = (html: string, label: string): string => {
67 const m = new RegExp(`<a\\s[^>]*>\\s*${label}\\s*</a`).exec(html);
outdated

Vacuous: depends on attribute order, so it passes whatever the markup. Parse the This week anchor and assert it lacks aria-current.

68 if (m === null) throw new Error(`no link ${label}`);
outdated

No test that the More-results (paging) link keeps since, which box 3 asks for.

69 return m[0];
70 };
71 const hrefOf = (tag: string): string => /href="([^"]*)"/.exec(tag)![1]!.replaceAll("&amp;", "&");
72
73 test("since=7d and since=30d keep exactly the apps changed inside the window", async () => {
74 expect(rows(await get("?kind=app&owner=pine&sort=name")).sort()).toEqual(["/pine/fresh", "/pine/never", "/pine/stale"]);
75 expect(rows(await get("?kind=app&owner=pine&since=7d"))).toEqual(["/pine/fresh"]);
76 expect(rows(await get("?kind=app&owner=pine&since=30d&sort=name"))).toEqual(["/pine/fresh", "/pine/stale"]);
77 });
78
79 test("an unknown since is ignored with a note, and the list is whole", async () => {
80 const res = await fetch(`${servers.url}/explore?kind=app&owner=pine&since=9y`);
81 expect(res.status).toBe(200);
82 const html = await res.text();
83 expect(rows(html).length).toBe(3);
84 expect(html).toContain("1d, 7d, 30d");
85 });
86
87 test("the active window is marked, Any time clears it, sort links keep it", async () => {
88 const on = await get("?kind=app&owner=pine&since=7d");
89 expect(anchor(on, "This week")).toContain('aria-current="page"');
90 expect(anchor(on, "Any time")).not.toContain("aria-current");
91 expect(hrefOf(anchor(on, "Any time"))).toBe("/explore?kind=app&owner=pine");
92 expect(hrefOf(anchor(on, "Forks"))).toBe("/explore?kind=app&sort=forks&since=7d&owner=pine");
93 const off = await get("?kind=app&owner=pine");
94 expect(anchor(off, "This week")).not.toContain("aria-current");
95 expect(anchor(off, "Any time")).toContain('aria-current="page"');
96 });
97
98 test("the More results link keeps since and leads to the rest of the window", async () => {
99 const first = await get("?since=7d&kind=app&owner=olive");
100 expect(rows(first).length).toBe(60);
101 const more = /<a[^>]*href="[^"]*cursor=[^"]*"[^>]*>/.exec(first);
102 const link = hrefOf(more![0]);
103 expect(link).toContain("since=7d");
104 const second = await (await fetch(`${servers.url}${link}`)).text();
105 expect(rows(second).length).toBe(FRESH - 60);
106 expect(new Set([...rows(first), ...rows(second)]).size).toBe(FRESH);
107 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs