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

/explore sorts by freshness as well as use, chosen by a plain link and kept in the URL #13474

Merged opened this

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

Merged

scout/forge:scout/13398-explore-sorts-by-freshness-as-well-as-us → main 1 commit fork

  • Merged. scout/13398-explore-sorts-by-freshness-as-well-as-us was merged into main as 64a30a3.

Review

Sign in to review this change.
1 file changed +96 −0
test/explore-sort.test.ts added +96
@@ -0,0 +1,96 @@
1 /** /explore sorts by freshness or by use, by a plain link kept in the URL (#13398). */
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 const DAY = 86_400_000;
18 const HOUR = 3_600_000;
19 let dir: string;
20 let app: App;
21 let servers: Servers;
22
23 // newest ships: kiln (1 day), garden (10), loom (20). most used: loom (200 h), garden (50), kiln
24 // (1).
25 beforeAll(async () => {
26 dir = await mkdtemp(join(tmpdir(), "forge-explore-sort-"));
27 app = createApp({ dataDir: dir, port: 0 });
28 const made = await createUser(app.db, { origin: "seed", username: "olive", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: false });
29 if (!made.ok) throw new Error(made.error);
30 proveAll(app);
31 promote(app.db, made.value);
32 const now = Date.now();
33 for (const [name, shipDays, hours] of [["garden", 10, 50], ["kiln", 1, 1], ["loom", 20, 200]] as const) {
34 const repo = await createRepo(app, made.value, { name, visibility: "public" });
35 if (!repo.ok) throw new Error(String(repo.error));
36 const rec = insertApp(app.db, { repoId: repo.value.id, slug: `olive-${name}`, access: "public" });
37 if (!rec.ok) throw new Error(rec.error);
38 const issue = createIssue(app.db, { repoId: repo.value.id, authorId: made.value.id, title: "change", kind: "pull" });
39 if (!issue.ok) throw new Error(issue.error);
40 app.db
41 .query(`INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at) VALUES (?1, 'main', 'b', '', '', ?2)`)
42 .run(issue.value.id, new Date(now - shipDays * DAY).toISOString());
43 app.db
44 .query(`INSERT INTO meter (repo_id, env, placement, started_at, beat_at, ended_at) VALUES (?1, 'production', 'owned', ?2, ?3, ?3)`)
45 .run(repo.value.id, new Date(now - hours * HOUR - DAY).toISOString(), new Date(now - DAY).toISOString());
46 }
47 servers = await serve(app);
48 });
49 afterAll(async () => {
50 await servers.stop();
51 app.db.close();
52 await rm(dir, { recursive: true, force: true });
53 });
54
55 const get = async (qs: string) => {
56 const res = await fetch(`${servers.url}/explore${qs}`);
57 return { status: res.status, html: await res.text() };
58 };
59 const listed = (html: string): string[] => {
60 const list = /<ul class="list">([\s\S]*?)<\/ul>/.exec(html);
61 return list === null ? [] : [...list[1]!.matchAll(/<li[^>]*>[\s\S]*?<a href="(\/[^"?]*)">/g)].map((m) => m[1]!);
62 };
63 const link = (html: string, label: string): string => {
64 const found = new RegExp(`<a[^>]*href="([^"]+)"[^>]*>\\s*${label}\\s*</a>`).exec(html);
65 if (found === null) throw new Error(`no control named ${label}`);
66 return found[1]!.replace(/&amp;/g, "&");
67 };
68
69 test("sort=fresh puts the newest ship first", async () => {
70 expect(listed((await get("?kind=app&sort=fresh")).html)).toEqual(["/olive/kiln", "/olive/garden", "/olive/loom"]);
71 });
72
73 test("sort=use puts the most used first", async () => {
74 expect(listed((await get("?kind=app&sort=use")).html)).toEqual(["/olive/loom", "/olive/garden", "/olive/kiln"]);
75 });
76
77 test("the active sort is marked, and the plain links reach each order and keep the filters", async () => {
78 const fresh = await get("?kind=app&sort=fresh");
79 expect(fresh.html).toMatch(/<a[^>]*aria-current="[^"]+"[^>]*>\s*Fresh\s*<\/a>/);
80 expect(fresh.html).not.toMatch(/<a[^>]*aria-current="[^"]+"[^>]*>\s*Use\s*<\/a>/);
81 // Choosing Use from the fresh view: the kind stays, the order changes.
82 const use = await get(link(fresh.html, "Use").replace("/explore", ""));
83 expect(link(fresh.html, "Use")).toContain("kind=app");
84 expect(listed(use.html)).toEqual(["/olive/loom", "/olive/garden", "/olive/kiln"]);
85 // The kind links keep the sort.
86 expect(link(use.html, "People")).toContain("sort=use");
87 });
88
89 test("an unknown sort falls back to the default with a note, never a 500", async () => {
90 const plain = await get("?kind=app");
91 const bad = await get("?kind=app&sort=newest");
92 expect(bad.status).toBe(200);
93 expect(listed(bad.html)).toEqual(listed(plain.html));
94 expect(bad.html).toContain("is not a sort");
95 expect(bad.html).not.toMatch(/aria-current="[^"]+"[^>]*>\s*Fresh\s*</);
96 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs