Public: anyone can read this; the crew can change it.
Every search door takes the same longest query, and no page echoes more of it than fits on a line #13862
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13846-every-search-door-takes-the-same-longest → main 2 commits fork
10 files
changed +146
−25
src/actions/explore.ts +3 −3
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | * - Forges are the children this forge spawned, for a site admin. |
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | import { cappedQuery, MAX_QUERY_CHARS } from "../core/query_limit.ts"; | |
| 19 | 20 | import type { Database } from "bun:sqlite"; |
| 20 | 21 | import type { App } from "../app.ts"; |
| 21 | 22 | import { appsForRepos } from "../core/apps.ts"; |
| @@ -138,8 +139,7 @@ export function exploreLineFrom(get: (name: string) => string | null | undefined | ||
| 138 | 139 | if (value !== "") given.set(key, value); |
| 139 | 140 | } |
| 140 | 141 | // A parameter wins: the same key typed in `q` is dropped, not left to the reader's order. |
| 141 | const typed = (get("q") ?? "") | |
| 142 | .trim() | |
| 142 | const typed = cappedQuery(get("q")) | |
| 143 | 143 | .split(/\s+/) |
| 144 | 144 | .filter((w) => w !== "" && !(w.includes(":") && given.has(w.slice(0, w.indexOf(":")).toLowerCase()))); |
| 145 | 145 | return [...typed, ...[...given].map(([k, v]) => `${k}:${v}`)].join(" "); |
| @@ -244,7 +244,7 @@ export function readExploreLine(line: string): ExploreRead { | ||
| 244 | 244 | query: { |
| 245 | 245 | kind, |
| 246 | 246 | sort, |
| 247 | text: words.join(" ").slice(0, 100), | |
| 247 | text: words.join(" ").slice(0, MAX_QUERY_CHARS), | |
| 248 | 248 | archived, |
| 249 | 249 | updated, |
| 250 | 250 | ...(owner === undefined ? {} : { owner }), |
src/core/query_limit.ts added +32
| @@ -0,0 +1,32 @@ | ||
| 1 | /** | |
| 2 | * The longest search query, for every door that takes one: /explore, /search and /docs, as pages | |
| 3 | * and as `GET /api/v1/explore` and `GET /api/v1/search`. Longer is a paste, not a search. | |
| 4 | */ | |
| 5 | ||
| 6 | /** The longest query, in characters. */ | |
| 7 | export const MAX_QUERY_CHARS = 200; | |
| 8 | ||
| 9 | /** The most of a query an empty-state title or chip repeats back. */ | |
| 10 | export const ECHO_CHARS = 60; | |
| 11 | ||
| 12 | /** Whether a raw `q` is longer than the limit (after trimming). */ | |
| 13 | export function queryTooLong(raw: string | null | undefined): boolean { | |
| 14 | return [...(raw ?? "").trim()].length > MAX_QUERY_CHARS; | |
| 15 | } | |
| 16 | ||
| 17 | /** The API's 422 message for a `q` that is too long. */ | |
| 18 | export const QUERY_TOO_LONG = `q is at most ${MAX_QUERY_CHARS} characters`; | |
| 19 | ||
| 20 | /** The one line a page says when it searched only the first characters of a longer `q`. */ | |
| 21 | export const QUERY_TRIMMED_NOTE = `That search was longer than ${MAX_QUERY_CHARS} characters, so this is the first ${MAX_QUERY_CHARS}.`; | |
| 22 | ||
| 23 | /** The first `MAX_QUERY_CHARS` characters of a trimmed `q`. */ | |
| 24 | export function cappedQuery(raw: string | null | undefined): string { | |
| 25 | return [...(raw ?? "").trim()].slice(0, MAX_QUERY_CHARS).join(""); | |
| 26 | } | |
| 27 | ||
| 28 | /** `text` for a title or chip: at most `max` characters, then an ellipsis. */ | |
| 29 | export function echoed(text: string, max: number = ECHO_CHARS): string { | |
| 30 | const chars = [...text]; | |
| 31 | return chars.length <= max ? text : `${chars.slice(0, max).join("").trimEnd()}…`; | |
| 32 | } |
src/core/search.ts +16 −6
| @@ -30,13 +30,14 @@ import { runGitCapture } from "../git/env.ts"; | ||
| 30 | 30 | import { isSafeRef } from "../git/guard.ts"; |
| 31 | 31 | import { repoDir } from "../git/repos.ts"; |
| 32 | 32 | import { GREP_LIMITS, grepSlots, type GrepSlots } from "../search/grep_slots.ts"; |
| 33 | import { MAX_QUERY_CHARS } from "./query_limit.ts"; | |
| 33 | 34 | import { appsForRepos } from "./apps.ts"; |
| 34 | 35 | import { likeEscape } from "./issues.ts"; |
| 35 | 36 | import { enterableApps, listReposVisibleTo, type Repo } from "./repos.ts"; |
| 36 | 37 | import type { User } from "./users.ts"; |
| 37 | 38 | |
| 38 | /** The longest query we will run. Longer is a paste, not a search. */ | |
| 39 | export const MAX_QUERY = 200; | |
| 39 | /** The longest query we will run: the one limit every search door shares. */ | |
| 40 | export const MAX_QUERY = MAX_QUERY_CHARS; | |
| 40 | 41 | /** Repos considered for one query (and, for code, grepped). */ |
| 41 | 42 | export const MAX_SEARCH_REPOS = 50; |
| 42 | 43 | /** Total code hits returned, across every repo. */ |
| @@ -47,7 +48,10 @@ export const MAX_HITS_PER_REPO = 20; | ||
| 47 | 48 | export const GREP_TIMEOUT_MS = 3_000; |
| 48 | 49 | /** A first code search starts no new grep after this long; `?code=all` runs the rest. */ |
| 49 | 50 | export const CODE_BUDGET_MS = 30; |
| 50 | /** A grep in a budgeted search is given up after this long and reported as `more`: a cold clone must not hold the page (#7198). */ | |
| 51 | /** | |
| 52 | * A grep in a budgeted search is given up after this long and reported as `more`: a cold clone must | |
| 53 | * not hold the page (#7198). | |
| 54 | */ | |
| 51 | 55 | export const BUDGETED_GREP_MS = 400; |
| 52 | 56 | |
| 53 | 57 | /** |
| @@ -76,7 +80,8 @@ export function searchKind(raw: string | null): SearchKind { | ||
| 76 | 80 | |
| 77 | 81 | /** |
| 78 | 82 | * What a caller who asked wrongly is told, in one sentence, or undefined when |
| 79 | * the request was fine (an unknown kind or limit is `searchRefusal`'s 422, not a note). Plain text: the caller escapes it where it draws it. | |
| 83 | * the request was fine (an unknown kind or limit is `searchRefusal`'s 422, not a note). Plain text: | |
| 84 | * the caller escapes it where it draws it. | |
| 80 | 85 | */ |
| 81 | 86 | export function searchNote(rawQ: string | null, kind: SearchKind = "all"): string | undefined { |
| 82 | 87 | if (cleanQuery(rawQ ?? "") === "") |
| @@ -248,7 +253,9 @@ function rankByName( | ||
| 248 | 253 | .map((x) => x.r); |
| 249 | 254 | } |
| 250 | 255 | |
| 251 | /** The one repository a scoped search is about, when its owner, name or description holds the words. */ | |
| 256 | /** | |
| 257 | * The one repository a scoped search is about, when its owner, name or description holds the words. | |
| 258 | */ | |
| 252 | 259 | export function matchOneRepo(repo: Repo, query: string, app: boolean): RepoHit[] { |
| 253 | 260 | const needle = query.trim().toLowerCase(); |
| 254 | 261 | const hay = `${repo.ownerName}/${repo.name} ${repo.description}`.toLowerCase(); |
| @@ -611,7 +618,10 @@ export interface GrepWho { | ||
| 611 | 618 | readonly now?: () => number; |
| 612 | 619 | /** Resolves after `ms`; `clear` stops it. A `setTimeout` by default. */ |
| 613 | 620 | readonly timer?: (ms: number) => BudgetTimer; |
| 614 | /** The first batch's ceiling (`BUDGETED_GREP_MS`), kept apart from `timer` so tests fire each on its own. */ | |
| 621 | /** | |
| 622 | * The first batch's ceiling (`BUDGETED_GREP_MS`), kept apart from `timer` so tests fire each on | |
| 623 | * its own. | |
| 624 | */ | |
| 615 | 625 | readonly firstTimer?: (ms: number) => BudgetTimer; |
| 616 | 626 | } |
| 617 | 627 |
src/http/api.ts +4
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | * back to continue); errors are `{ error, message }`. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { queryTooLong, QUERY_TOO_LONG } from "../core/query_limit.ts"; | |
| 11 | 12 | import { parseAttestation } from "../core/attestation.ts"; |
| 12 | 13 | import { listAppReleases } from "../core/app_releases.ts"; |
| 13 | 14 | import { openapiHandler } from "../contract/served.ts"; |
| @@ -1591,6 +1592,7 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1591 | 1592 | const q = ctx.url.searchParams; |
| 1592 | 1593 | const refusal = searchRefusal(q.get("kind"), q.get("limit")); |
| 1593 | 1594 | if (refusal) return apiError(refusal.code, 422, refusal.message); |
| 1595 | if (queryTooLong(q.get("q"))) return json({ error: "invalid", field: "q", message: QUERY_TOO_LONG }, 422); | |
| 1594 | 1596 | const kind = searchKind(q.get("kind")); |
| 1595 | 1597 | const query = q.get("q") ?? ""; |
| 1596 | 1598 | const greps = |
| @@ -3617,6 +3619,8 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 3617 | 3619 | if (limitRaw !== null && !(/^\d{1,3}$/.test(limitRaw) && Number(limitRaw) >= 1 && Number(limitRaw) <= 100)) |
| 3618 | 3620 | return json({ error: "invalid", field: "limit", message: "limit is an integer from 1 to 100" }, 422); |
| 3619 | 3621 | const limit = limitRaw === null ? 30 : Number(limitRaw); |
| 3622 | if (queryTooLong(ctx.url.searchParams.get("q"))) | |
| 3623 | return json({ error: "invalid", field: "q", message: QUERY_TOO_LONG }, 422); | |
| 3620 | 3624 | const line = exploreLineFrom((name) => ctx.url.searchParams.get(name)); |
| 3621 | 3625 | const { query, unknown } = readExploreLine(line); |
| 3622 | 3626 | for (const field of ["sort", "kind"] as const) { |
src/web/pages/api.ts +2 −2
| @@ -12,6 +12,7 @@ import { narrow, reference, type Area } from "../../contract/reference.ts"; | ||
| 12 | 12 | import { openapiText, roleOf } from "../../contract/served.ts"; |
| 13 | 13 | import type { Role } from "../../contract/openapi/shared.ts"; |
| 14 | 14 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; |
| 15 | import { cappedQuery } from "../../core/query_limit.ts"; | |
| 15 | 16 | import { canEnterApp } from "../../core/repos.ts"; |
| 16 | 17 | import type { Ctx } from "../../http/context.ts"; |
| 17 | 18 | import { noRoute, prefersJson } from "../../http/respond.ts"; |
| @@ -21,8 +22,7 @@ import { loadRepo, notFoundPage, repoBase, repoHeader } from "../chrome.ts"; | ||
| 21 | 22 | import { html } from "../html.ts"; |
| 22 | 23 | import { page } from "../layout.ts"; |
| 23 | 24 | |
| 24 | const MAX_QUERY = 100; | |
| 25 | const asked = (ctx: Ctx): string => (ctx.url.searchParams.get("q") ?? "").trim().slice(0, MAX_QUERY); | |
| 25 | const asked = (ctx: Ctx): string => cappedQuery(ctx.url.searchParams.get("q")); | |
| 26 | 26 | const count = (areas: readonly Area[]): number => areas.reduce((n, a) => n + a.operations.length, 0); |
| 27 | 27 | |
| 28 | 28 | /** |
src/web/pages/docs_link.ts +8 −5
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | * are kept in memory for the life of the process. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { cappedQuery, echoed, MAX_QUERY_CHARS, queryTooLong, QUERY_TRIMMED_NOTE } from "../../core/query_limit.ts"; | |
| 10 | 11 | import type { Database } from "bun:sqlite"; |
| 11 | 12 | import { upstreamBlob } from "../../core/upstream.ts"; |
| 12 | 13 | import type { Ctx } from "../../http/context.ts"; |
| @@ -61,20 +62,22 @@ function indexRow(e: { name: string; title: string }): Html { | ||
| 61 | 62 | export function docsFolder(ctx: Ctx): Response { |
| 62 | 63 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 63 | 64 | const began = performance.now(); |
| 64 | const q = (ctx.url.searchParams.get("q") ?? "").trim().slice(0, 80); | |
| 65 | const rawQ = ctx.url.searchParams.get("q"); | |
| 66 | const q = cappedQuery(rawQ); | |
| 67 | const trimmed = queryTooLong(rawQ) ? html`<p class="muted small" role="status" data-query-trimmed>${QUERY_TRIMMED_NOTE}</p>` : html``; | |
| 65 | 68 | const form = html`<form class="docs-search" role="search" action="/docs" method="get"> |
| 66 | <input class="input" name="q" type="search" maxlength="80" value="${q}" placeholder="Search the docs" aria-label="Search the docs" autocomplete="off" /> | |
| 69 | <input class="input" name="q" type="search" maxlength="${MAX_QUERY_CHARS}" value="${q}" placeholder="Search the docs" aria-label="Search the docs" autocomplete="off" /> | |
| 67 | 70 | </form>`; |
| 68 | 71 | let body: Html; |
| 69 | 72 | if (q !== "") { |
| 70 | 73 | const hits = searchDocs(q); |
| 71 | body = html`<h1>Documentation</h1>${form} | |
| 74 | body = html`<h1>Documentation</h1>${form}${trimmed} | |
| 72 | 75 | ${hits.length === 0 |
| 73 | ? html`<p>Nothing matches “${q}”. Try one word.</p>` | |
| 76 | ? html`<p>Nothing matches “${echoed(q)}”. Try one word.</p>` | |
| 74 | 77 | : html`<ul class="list">${hits.map((h) => row({ href: `/docs/${h.name}`, title: h.title, meta: html`${h.snippet}` }))}</ul>`}`; |
| 75 | 78 | } else { |
| 76 | 79 | const { guide: spine, rest } = docSpine(); |
| 77 | body = html`<h1>Documentation</h1>${form} | |
| 80 | body = html`<h1>Documentation</h1>${form}${trimmed} | |
| 78 | 81 | <h2>Start here: the guide</h2> |
| 79 | 82 | <ol class="list docs-index">${spine.map(indexRow)}</ol> |
| 80 | 83 | <h2>Reference</h2> |
src/web/pages/search.ts +10 −5
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | * that was never meant to be confirmed. |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | import { cappedQuery, echoed, queryTooLong, QUERY_TRIMMED_NOTE } from "../../core/query_limit.ts"; | |
| 13 | 14 | import { returnTo, withReturn } from "../return_to.ts"; |
| 14 | 15 | import { DETACH_AGAIN, DETACH_STAYS, DETACH_STOPS } from "../../actions/detach_words.ts"; |
| 15 | 16 | import { DISCONNECT_DETAIL } from "../../actions/origin_words.ts"; |
| @@ -387,7 +388,7 @@ export async function searchPage(ctx: Ctx): Promise<Response> { | ||
| 387 | 388 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 388 | 389 | const params = ctx.url.searchParams; |
| 389 | 390 | const a: Asked = { |
| 390 | query: cleanQuery(params.get("q") ?? ""), | |
| 391 | query: cleanQuery(cappedQuery(params.get("q"))), | |
| 391 | 392 | kind: searchKind(params.get("kind")), |
| 392 | 393 | live: params.get("live") === "1", |
| 393 | 394 | codeAll: params.get("code") === "all", |
| @@ -419,13 +420,17 @@ export async function searchPage(ctx: Ctx): Promise<Response> { | ||
| 419 | 420 | </button> |
| 420 | 421 | </form>`; |
| 421 | 422 | |
| 423 | const trimmed = queryTooLong(params.get("q")) | |
| 424 | ? html`<p class="muted small" role="status" data-query-trimmed>${QUERY_TRIMMED_NOTE}</p>` | |
| 425 | : html``; | |
| 426 | ||
| 422 | 427 | if (a.query === "") { |
| 423 | 428 | const rendering = performance.now(); |
| 424 | 429 | const blank = page({ |
| 425 | 430 | ctx, |
| 426 | 431 | title: "Search", |
| 427 | 432 | styleCss: SEARCH_CSS, |
| 428 | body: html`<div data-search data-palette-auto>${pageHead("Search")} ${form} | |
| 433 | body: html`<div data-search data-palette-auto>${pageHead("Search")} ${form}${trimmed} | |
| 429 | 434 | </div>`, |
| 430 | 435 | }); |
| 431 | 436 | notePhase(ctx.request, "render", performance.now() - rendering); |
| @@ -509,7 +514,7 @@ export async function searchPage(ctx: Ctx): Promise<Response> { | ||
| 509 | 514 | const nothing = |
| 510 | 515 | a.kind === "all" && |
| 511 | 516 | found.repos.length + found.issues.length + found.code.length === 0; |
| 512 | const body = html`<div data-search>${pageHead(html`Results for “${a.query}”`)} ${form} | |
| 517 | const body = html`<div data-search>${pageHead(html`Results for “${echoed(a.query)}”`)} ${form}${trimmed} | |
| 513 | 518 | <div class="bar mb">${chips}</div> |
| 514 | 519 | ${liveToggle} |
| 515 | 520 | ${when( |
| @@ -535,7 +540,7 @@ export async function searchPage(ctx: Ctx): Promise<Response> { | ||
| 535 | 540 | )} |
| 536 | 541 | ${nothing |
| 537 | 542 | ? html`<p class="muted next-step" data-next-step> |
| 538 | Nothing matches “${a.query}”. Search reads names, descriptions, issues, | |
| 543 | Nothing matches “${echoed(a.query)}”. Search reads names, descriptions, issues, | |
| 539 | 544 | pulls and the code you can see. <a href="/explore">Explore</a> |
| 540 | 545 | ${when( |
| 541 | 546 | a.query !== "", |
| @@ -552,7 +557,7 @@ export async function searchPage(ctx: Ctx): Promise<Response> { | ||
| 552 | 557 | .map((w) => |
| 553 | 558 | section(a, w, ...rows[w[0]](), w[0] === "code" ? moreCode : null), |
| 554 | 559 | )}</div>`; |
| 555 | const response = page({ ctx, title: `Search · ${a.query}`, body, styleCss: SEARCH_CSS }); | |
| 560 | const response = page({ ctx, title: `Search · ${echoed(a.query)}`, body, styleCss: SEARCH_CSS }); | |
| 556 | 561 | notePhase(ctx.request, "render", performance.now() - rendering); |
| 557 | 562 | return response; |
| 558 | 563 | } |
src/web/studio/explore.ts +4 −2
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | * `actions/explore.ts`; this page only draws it. |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | import { echoed, queryTooLong, QUERY_TRIMMED_NOTE } from "../../core/query_limit.ts"; | |
| 9 | 10 | import { |
| 10 | 11 | explorePageAt, |
| 11 | 12 | explorePageFrom, |
| @@ -115,7 +116,7 @@ function emptyState(ctx: Ctx, query: ExploreQuery): Html { | ||
| 115 | 116 | if (line !== "") |
| 116 | 117 | return empty({ |
| 117 | 118 | icon: "search", |
| 118 | title: `Nothing matches ${line}`, | |
| 119 | title: `Nothing matches ${echoed(line)}`, | |
| 119 | 120 | note: "The chips above take one filter off; Clear filters above takes them all.", |
| 120 | 121 | }); |
| 121 | 122 | return empty({ |
| @@ -137,7 +138,7 @@ function chipRow(query: ExploreQuery): Html { | ||
| 137 | 138 | const chips = [...removable(query), ...terms(query).filter((t) => t.term.startsWith("sort:"))]; |
| 138 | 139 | if (chips.length === 0) return html``; |
| 139 | 140 | return html`<div class="bar mb small" data-filter-chips role="group" aria-label="Active filters"> |
| 140 | ${chips.map((t) => btn(html`${shorten(t.term)} <span aria-hidden="true">×</span>`, { href: href(t.without), size: "sm", title: `Remove ${t.term}`, label: `Remove filter ${t.term}` }))} | |
| 141 | ${chips.map((t) => btn(html`${shorten(t.term)} <span aria-hidden="true">×</span>`, { href: href(t.without), size: "sm", title: `Remove ${echoed(t.term)}`, label: `Remove filter ${echoed(t.term)}` }))} | |
| 141 | 142 | </div>`; |
| 142 | 143 | } |
| 143 | 144 | |
| @@ -252,6 +253,7 @@ export async function explorePage(ctx: Ctx): Promise<Response> { | ||
| 252 | 253 | }, |
| 253 | 254 | ])} |
| 254 | 255 | </div> |
| 256 | ${queryTooLong(ctx.url.searchParams.get("q")) ? html`<p class="muted mb" role="status" data-query-trimmed>${QUERY_TRIMMED_NOTE}</p>` : html``} | |
| 255 | 257 | ${chipRow(query)} |
| 256 | 258 | ${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``} |
| 257 | 259 | ${note === "" ? html`` : html`<p class="muted mb" role="status">${note}${ |
test/query-limit.test.ts added +65
| @@ -0,0 +1,65 @@ | ||
| 1 | /** One longest query for every search door (#13846). */ | |
| 2 | import { afterAll, beforeAll, describe, 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 { echoed, ECHO_CHARS, MAX_QUERY_CHARS } from "../src/core/query_limit.ts"; | |
| 8 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 9 | ||
| 10 | let dataDir: string; | |
| 11 | let app: App; | |
| 12 | let servers: Servers; | |
| 13 | const long = "a".repeat(MAX_QUERY_CHARS + 1); | |
| 14 | const at = (path: string) => fetch(`${servers.url}${path}`); | |
| 15 | ||
| 16 | beforeAll(async () => { | |
| 17 | dataDir = await mkdtemp(join(tmpdir(), "forge-query-limit-")); | |
| 18 | app = createApp({ dataDir, port: 0 }); | |
| 19 | servers = await serve(app); | |
| 20 | }); | |
| 21 | afterAll(async () => { | |
| 22 | servers.stop(); | |
| 23 | await rm(dataDir, { recursive: true, force: true }); | |
| 24 | }); | |
| 25 | ||
| 26 | describe("the API", () => { | |
| 27 | for (const path of ["/api/v1/explore", "/api/v1/search"]) { | |
| 28 | test(`${path} answers 422 naming q and the limit`, async () => { | |
| 29 | const res = await at(`${path}?q=${long}`); | |
| 30 | expect(res.status).toBe(422); | |
| 31 | const body = (await res.json()) as { field: string; message: string }; | |
| 32 | expect(body.field).toBe("q"); | |
| 33 | expect(body.message).toContain(String(MAX_QUERY_CHARS)); | |
| 34 | }); | |
| 35 | test(`${path} takes a query at the limit`, async () => { | |
| 36 | expect((await at(`${path}?q=${"a".repeat(MAX_QUERY_CHARS)}`)).status).toBe(200); | |
| 37 | }); | |
| 38 | } | |
| 39 | }); | |
| 40 | ||
| 41 | describe("the pages", () => { | |
| 42 | for (const path of ["/explore", "/search", "/docs"]) { | |
| 43 | test(`${path} searches the first characters and says so`, async () => { | |
| 44 | const res = await at(`${path}?q=${long}`); | |
| 45 | expect(res.status).toBe(200); | |
| 46 | const text = await res.text(); | |
| 47 | expect(text).toContain("data-query-trimmed"); | |
| 48 | // The box holds the capped query, never the whole paste. | |
| 49 | const value = /name="q"[^>]*value="([^"]*)"|value="([^"]*)"[^>]*name="q"/.exec(text); | |
| 50 | expect((value?.[1] ?? value?.[2] ?? "").length).toBe(MAX_QUERY_CHARS); | |
| 51 | expect(text.length).toBeLessThan(60_000); | |
| 52 | }); | |
| 53 | } | |
| 54 | test("the explorer's empty title repeats at most 60 characters", async () => { | |
| 55 | const text = await (await at(`/explore?q=${"b".repeat(150)}`)).text(); | |
| 56 | expect(text).toContain(`Nothing matches ${"b".repeat(ECHO_CHARS)}…`); | |
| 57 | const title = /empty-title">([^<]*)</.exec(text)?.[1] ?? ""; | |
| 58 | expect(title).toBe(`Nothing matches ${"b".repeat(ECHO_CHARS)}…`); | |
| 59 | }); | |
| 60 | }); | |
| 61 | ||
| 62 | test("echoed shortens with an ellipsis and leaves short text alone", () => { | |
| 63 | expect(echoed("short")).toBe("short"); | |
| 64 | expect(echoed("x".repeat(100))).toBe(`${"x".repeat(ECHO_CHARS)}…`); | |
| 65 | }); |
test/search.test.ts +2 −2
| @@ -510,8 +510,8 @@ describe("search: one query, four sources", () => { | ||
| 510 | 510 | "--open-files-in-pager=sh", |
| 511 | 511 | "-O sh", |
| 512 | 512 | " ", |
| 513 | // Longer than this and the transport answers 431 before we do. | |
| 514 | "a".repeat(4_000), | |
| 513 | // The longest query there is (MAX_QUERY_CHARS); longer is a 422. | |
| 514 | "a".repeat(200), | |
| 515 | 515 | "(flux|.*)+$", |
| 516 | 516 | "[", |
| 517 | 517 | ]; |
new above
offline