Public: anyone can read this; the crew can change it.
The search page's live toggle draws §5's field and btn, and .live-toggle leaves the markup #4252
scout/forge:scout/4195-the-search-page-s-live-toggle-draws-5-s → main 1 commit fork
2 files
changed +77
−17
src/web/pages/search.ts +20 −17
| @@ -427,26 +427,29 @@ export async function searchPage(ctx: Ctx): Promise<Response> { | ||
| 427 | 427 | })), |
| 428 | 428 | ); |
| 429 | 429 | |
| 430 | // §5 list row: a globe lead, the sentence as the title (`.break` keeps a | |
| 431 | // long origin name from widening the page), one `.btn` as the trail. | |
| 430 | 432 | const liveToggle = when( |
| 431 | 433 | ctx.auth.user !== undefined, |
| 432 | 434 | () => |
| 433 | html`<p class="live-toggle small"> | |
| 434 | ${icon("globe")} | |
| 435 | ${a.live | |
| 436 | ? html`<span | |
| 437 | >Asking every connected origin's own search, live. | |
| 438 | <a href="${searchHref(a, { live: false })}">Use the cache</a | |
| 439 | >.</span | |
| 440 | >` | |
| 441 | : html`<span | |
| 442 | >Origins are searched from the cache. | |
| 443 | <a href="${searchHref(a, { live: true })}" | |
| 444 | >Search live on origins</a | |
| 445 | > | |
| 446 | — a round trip to each connected origin, so it costs a | |
| 447 | moment.</span | |
| 448 | >`} | |
| 449 | </p>`, | |
| 435 | html`<ul class="list mt" data-live-search> | |
| 436 | ${row({ | |
| 437 | lead: icon("globe"), | |
| 438 | title: html`<span class="break" | |
| 439 | >${a.live | |
| 440 | ? "Asking every connected origin's own search, live." | |
| 441 | : "Origins are searched from the cache."}</span | |
| 442 | >`, | |
| 443 | ...(a.live | |
| 444 | ? {} | |
| 445 | : { meta: html`Live is a round trip to each connected origin, so it costs a moment.` }), | |
| 446 | trail: btn(a.live ? "Use the cache" : "Search live on origins", { | |
| 447 | href: searchHref(a, { live: !a.live }), | |
| 448 | variant: "outline", | |
| 449 | size: "sm", | |
| 450 | }), | |
| 451 | })} | |
| 452 | </ul>`, | |
| 450 | 453 | ); |
| 451 | 454 | |
| 452 | 455 | const rows: Record<SearchKind, () => [Html[], boolean]> = { |
test/search-live-toggle.test.ts added +57
| @@ -0,0 +1,57 @@ | ||
| 1 | /** The search page's live toggle is drawn with §5's row and btn, not `.live-toggle` (#4195). */ | |
| 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 } from "../src/app.ts"; | |
| 7 | import { serve } from "../src/http/server.ts"; | |
| 8 | import { adminCookie } from "./helpers/admin.ts"; | |
| 9 | ||
| 10 | let dir: string; | |
| 11 | let app: ReturnType<typeof createApp>; | |
| 12 | let servers: Awaited<ReturnType<typeof serve>>; | |
| 13 | let cookie: string; | |
| 14 | ||
| 15 | beforeAll(async () => { | |
| 16 | dir = await mkdtemp(join(tmpdir(), "forge-search-live-")); | |
| 17 | app = createApp({ dataDir: dir, port: 0 }); | |
| 18 | servers = await serve(app); | |
| 19 | cookie = await adminCookie(app, { | |
| 20 | username: "alice", email: "[email protected]", password: "a-good-passphrase", | |
| 21 | }); | |
| 22 | }, 60_000); | |
| 23 | ||
| 24 | afterAll(async () => { | |
| 25 | servers.stop(); | |
| 26 | app.close(); | |
| 27 | await rm(dir, { recursive: true, force: true }); | |
| 28 | }); | |
| 29 | ||
| 30 | const page = async (qs: string, signedIn = true) => | |
| 31 | (await fetch(`${servers.url}/search?${qs}`, { headers: signedIn ? { cookie } : {} })).text(); | |
| 32 | ||
| 33 | test("cache mode: a row with a btn link to live, no .live-toggle", async () => { | |
| 34 | const body = await page("q=zebra"); | |
| 35 | expect(body).not.toContain("live-toggle"); | |
| 36 | expect(body).toContain("Origins are searched from the cache."); | |
| 37 | expect(body).toContain("a round trip to each connected origin"); | |
| 38 | expect(body).toMatch(/href="\/search\?q=zebra&live=1"[^>]*class="btn"/); | |
| 39 | expect(body).toContain("Search live on origins"); | |
| 40 | expect(body).toContain('class="break"'); | |
| 41 | }); | |
| 42 | ||
| 43 | test("live mode: the way back to the cache is a btn link without live=1", async () => { | |
| 44 | const body = await page("q=zebra&live=1"); | |
| 45 | expect(body).not.toContain("live-toggle"); | |
| 46 | expect(body).toContain("Asking every connected origin's own search, live."); | |
| 47 | expect(body).toMatch(/href="\/search\?q=zebra&live=1"/); // the filter chips carry it on | |
| 48 | expect(body).toMatch(/href="\/search\?q=zebra"[^>]*class="btn"/); | |
| 49 | expect(body).toContain("Use the cache"); | |
| 50 | expect(body).not.toContain("Search live on origins"); | |
| 51 | }); | |
| 52 | ||
| 53 | test("signed out: no toggle at all", async () => { | |
| 54 | const body = await page("q=zebra", false); | |
| 55 | expect(body).not.toContain("Search live on origins"); | |
| 56 | expect(body).not.toContain("live-toggle"); | |
| 57 | }); |
new above
offline