Files
pipelines/web/test/search.test.mjs
T

520 lines
21 KiB
JavaScript

// Pure omni-search tests: the per-kind row text, the in-hand narrowing and
// ordering, the routing target/tab resolution, the debounce/stale-token sequencer
// and the failure copy. Everything here is DOM-free and needs no server, mirroring
// android/.../ui/OmniSearchResultsTest.kt and OmniSearchTest.kt.
import { test } from "node:test";
import assert from "node:assert/strict";
import {
DEFAULT_SEARCH_KIND,
DEFAULT_SEARCH_SORT,
SEARCH_KINDS,
SEARCH_SORTS,
SEARCH_UNSUPPORTED,
SearchSequencer,
filterSearchResults,
resolveRunTarget,
searchControlsSummary,
searchFailureText,
searchFieldTab,
searchKindLabel,
searchPanelBody,
searchPanelEmptyMessage,
searchPanelProgress,
searchQuerySendable,
searchResultKindLabel,
searchRow,
searchTarget,
snippetSpans,
sortSearchResults,
} from "../src/search.js";
const UTC = { timeZone: "UTC" };
// ---------------------------------------------------------------------------
// Fixtures — the same shapes the Android test uses
// ---------------------------------------------------------------------------
function pipelineHit(overrides = {}) {
return {
kind: "pipeline",
target: "pipeline:7",
pipeline: { id: 7, name: "release", version: 3, stages: 4, ...overrides },
};
}
function versionHit(overrides = {}) {
return {
kind: "version",
target: "pipeline:9:v2",
version: {
pipeline_id: 9,
version: 2,
content_hash: "abc",
created_at: "2026-01-01T00:00:00Z",
...overrides,
},
};
}
function runHit(overrides = {}) {
return {
kind: "run",
target: "run:11",
run: {
id: 11,
pipeline_name: "release",
status: "succeeded",
created_at: "2026-01-01T00:00:00Z",
...overrides,
},
};
}
function stepHit(overrides = {}) {
return {
kind: "step",
target: "run:11:4",
step: { run_id: 11, sequence: 4, stage: "builder", visit: 2, outcome: "success", ...overrides },
};
}
function fieldHit(overrides = {}, matchedField = "output") {
return {
kind: "field",
target: "run:11:4:output",
matched_field: matchedField,
field: {
run_id: 11,
sequence: 4,
field: "output",
snippet: "the release is ready",
...overrides,
},
};
}
function artifactHit(overrides = {}) {
return {
kind: "artifact",
target: "/artifacts/11/4/notes.md",
artifact: {
run_id: 11,
sequence: 4,
name: "notes.md",
content_type: "text/markdown",
size_bytes: 2048,
url: "/artifacts/11/4/notes.md",
...overrides,
},
};
}
// ---------------------------------------------------------------------------
// Kind/sort vocabulary
// ---------------------------------------------------------------------------
test("the kind options are exactly the server vocabulary, All kinds first", () => {
assert.deepEqual(
SEARCH_KINDS.map((option) => [option.value, option.label]),
[
["", "All kinds"],
["pipeline", "Pipelines"],
["run", "Runs"],
["step", "Steps"],
["field", "Fields"],
["artifact", "Artifacts"],
["version", "Versions"],
],
);
assert.equal(DEFAULT_SEARCH_KIND, "");
assert.deepEqual(
SEARCH_SORTS.map((option) => [option.value, option.label]),
[
["relevance", "Relevance"],
["newest", "Newest first"],
["name", "Name"],
],
);
assert.equal(DEFAULT_SEARCH_SORT, "relevance");
// A stale/unknown value falls back rather than inventing an option.
assert.equal(searchKindLabel("prompts"), "All kinds");
assert.equal(searchKindLabel("run"), "Runs");
});
// ---------------------------------------------------------------------------
// Row text
// ---------------------------------------------------------------------------
test("pipeline rows carry the name (or the id fallback) and the version/stage line", () => {
assert.deepEqual(searchRow(pipelineHit(), "release", UTC), {
title: "release",
secondary: "pipeline 7 · v3 · 4 stages",
snippet: null,
});
const blank = searchRow(pipelineHit({ name: " " }), "release", UTC);
assert.equal(blank.title, "pipeline 7");
// The stage count keeps the app's singular form.
assert.equal(searchRow(pipelineHit({ stages: 1 }), "", UTC).secondary, "pipeline 7 · v3 · 1 stage");
});
test("version rows name the pipeline/version and omit blank content parts", () => {
assert.deepEqual(searchRow(versionHit(), "release", UTC), {
title: "pipeline 9 · v2",
secondary: "content abc · 1 Jan 00:00",
snippet: null,
});
const blankHash = searchRow(versionHit({ content_hash: " " }), "release", UTC);
assert.equal(blankHash.secondary, "1 Jan 00:00");
});
test("run rows join the non-blank status, workspace and timestamp", () => {
assert.deepEqual(searchRow(runHit(), "release", UTC), {
title: "run #11 · release",
secondary: "succeeded · 1 Jan 00:00",
snippet: null,
});
const withWorkspace = searchRow(runHit({ workspace: "/tmp/ws" }), "", UTC);
assert.equal(withWorkspace.secondary, "succeeded · /tmp/ws · 1 Jan 00:00");
const unnamed = searchRow(runHit({ pipeline_name: "" }), "", UTC);
assert.equal(unnamed.title, "run #11");
});
test("step rows carry outcome, route and the first non-blank diagnostic line", () => {
const withTarget = searchRow(stepHit({ target: "#success" }), "", UTC);
assert.deepEqual(withTarget, {
title: "run #11 · builder visit 2",
secondary: "success · → #success",
snippet: null,
});
const withDiagnostic = searchRow(
stepHit({ diagnostic: "\n the first line\nsecond line" }),
"",
UTC,
);
assert.equal(withDiagnostic.secondary, "success · the first line");
// Every part blank: the sequence is the honest fallback.
const allBlank = searchRow(stepHit({ outcome: "", target: "", diagnostic: "" }), "", UTC);
assert.equal(allBlank.secondary, "step 4");
// A blank stage falls back to the word "step".
assert.equal(searchRow(stepHit({ stage: "" }), "", UTC).title, "run #11 · step visit 2");
});
test("field rows carry the server snippet with the matched query span", () => {
const row = searchRow(fieldHit(), "release", UTC);
assert.equal(row.title, "run #11 · step 4 · output");
assert.equal(row.secondary, "the release is ready");
assert.deepEqual(row.snippet, [
{ text: "the ", matched: false },
{ text: "release", matched: true },
{ text: " is ready", matched: false },
]);
// No occurrence: one unmatched run, never dropped text.
assert.deepEqual(searchRow(fieldHit(), "absent", UTC).snippet, [
{ text: "the release is ready", matched: false },
]);
// A blank field falls back to the word "field".
assert.equal(searchRow(fieldHit({ field: " " }), "release", UTC).title, "run #11 · step 4 · field");
});
test("artifact rows join name, run, step, content type and human size", () => {
assert.deepEqual(searchRow(artifactHit(), "notes", UTC), {
title: "notes.md",
secondary: "run #11 · step 4 · text/markdown · 2.0 KiB",
snippet: null,
});
assert.equal(searchRow(artifactHit({ name: "", content_type: "" }), "", UTC).title, "artifact");
});
test("an unknown row is read-only and never dropped", () => {
const named = searchRow({ kind: "future_kind", target: "widget:7" }, "x", UTC);
assert.deepEqual(named, {
title: "widget:7",
secondary: 'kind "future_kind" · read-only',
snippet: null,
});
const anonymous = searchRow({ kind: "", target: "" }, "x", UTC);
assert.deepEqual(anonymous, { title: "Unrecognised result", secondary: "read-only", snippet: null });
});
test("snippetSpans splits only the first occurrence, case-insensitively", () => {
assert.deepEqual(snippetSpans("the Release notes mention it", "release"), [
{ text: "the ", matched: false },
{ text: "Release", matched: true },
{ text: " notes mention it", matched: false },
]);
// The FIRST occurrence is the emphasised one; a later one is left in the tail.
assert.deepEqual(snippetSpans("aXbXc", "x"), [
{ text: "a", matched: false },
{ text: "X", matched: true },
{ text: "bXc", matched: false },
]);
// A match at the very start/end needs no empty leading/trailing run.
assert.deepEqual(snippetSpans("release", "release"), [{ text: "release", matched: true }]);
assert.deepEqual(snippetSpans("nothing here", "release"), [{ text: "nothing here", matched: false }]);
assert.deepEqual(snippetSpans("nothing here", " "), [{ text: "nothing here", matched: false }]);
assert.deepEqual(snippetSpans("", "release"), []);
});
test("searchResultKindLabel names the kind in words, artifact by its content type", () => {
assert.equal(searchResultKindLabel(pipelineHit()), "Pipeline");
assert.equal(searchResultKindLabel(versionHit()), "Pipeline version");
assert.equal(searchResultKindLabel(runHit()), "Run");
assert.equal(searchResultKindLabel(stepHit()), "Step");
assert.equal(searchResultKindLabel(fieldHit()), "Field");
assert.equal(searchResultKindLabel(artifactHit({ content_type: "text/markdown" })), "Document artifact");
assert.equal(
searchResultKindLabel(artifactHit({ name: "shot.png", content_type: "image/png" })),
"Image artifact",
);
assert.equal(searchResultKindLabel({ kind: "future_kind", target: "x" }), "Unrecognised result");
});
// ---------------------------------------------------------------------------
// Kind filtering + in-hand sorting
// ---------------------------------------------------------------------------
test("the kind filter narrows the list, and All kinds keeps it whole", () => {
const mixed = [pipelineHit(), runHit(), stepHit(), fieldHit(), artifactHit(), versionHit()];
assert.deepEqual(filterSearchResults(mixed, ""), mixed);
assert.deepEqual(filterSearchResults(mixed, "run"), [mixed[1]]);
assert.deepEqual(filterSearchResults(mixed, "version"), [mixed[5]]);
assert.deepEqual(filterSearchResults(mixed, "nothing"), []);
});
function hitWithRecency(kind, recency) {
return { kind, target: `${kind}:1`, recency };
}
test("Relevance keeps the server's ranking untouched", () => {
const ordered = [
hitWithRecency("run", "2026-01-01T00:00:00Z"),
hitWithRecency("pipeline", "2026-03-01T00:00:00Z"),
];
assert.deepEqual(sortSearchResults(ordered, "relevance"), ordered);
assert.deepEqual(sortSearchResults(ordered, "stale-sort-value"), ordered);
});
test("Newest first orders by parsed recency, descending", () => {
const older = hitWithRecency("run", "2026-01-01T00:00:00Z");
const newer = hitWithRecency("pipeline", "2026-03-01T00:00:00Z");
assert.deepEqual(sortSearchResults([older, newer], "newest"), [newer, older]);
});
test("Newest first keeps ties stable, and puts a missing/invalid recency last", () => {
const first = hitWithRecency("run", "2026-01-01T00:00:00Z");
const second = hitWithRecency("pipeline", "2026-01-01T00:00:00Z");
assert.deepEqual(sortSearchResults([first, second], "newest"), [first, second]);
const dated = hitWithRecency("run", "2026-01-01T00:00:00Z");
const undated = hitWithRecency("run", null);
const invalid = hitWithRecency("run", "not-a-date");
assert.deepEqual(sortSearchResults([undated, invalid, dated], "newest"), [dated, undated, invalid]);
});
test("Name orders by the displayed title, case-insensitively, with ties stable", () => {
const alpha1 = pipelineHit({ id: 1, name: "alpha" });
const beta = pipelineHit({ id: 2, name: "Beta" });
const alpha2 = pipelineHit({ id: 3, name: "ALPHA" });
assert.deepEqual(sortSearchResults([beta, alpha1, alpha2], "name"), [alpha1, alpha2, beta]);
});
// ---------------------------------------------------------------------------
// Summary + empty state
// ---------------------------------------------------------------------------
test("the controls summary is exactly {Kind} · {Sort}, with no (in hand) suffix", () => {
assert.equal(searchControlsSummary("", "relevance"), "All kinds · Relevance");
assert.equal(searchControlsSummary("run", "newest"), "Runs · Newest first");
assert.equal(searchControlsSummary("field", "name"), "Fields · Name");
assert.equal(searchControlsSummary("bogus", "bogus"), "All kinds · Relevance");
});
test("the empty state names the query and the active filter, or nothing", () => {
const empty = { query: "release", loading: false, error: "" };
assert.equal(searchPanelEmptyMessage(empty, [], ""), 'No matches for "release".');
assert.equal(searchPanelEmptyMessage(empty, [], "run"), 'No runs match "release".');
assert.equal(searchPanelEmptyMessage(empty, [], "version"), 'No versions match "release".');
// Rows shown, still loading, a blank query or an error: no message at all.
assert.equal(searchPanelEmptyMessage(empty, [runHit()], ""), null);
assert.equal(searchPanelEmptyMessage({ ...empty, loading: true }, [], ""), null);
assert.equal(searchPanelEmptyMessage({ query: " " }, [], ""), null);
assert.equal(searchPanelEmptyMessage({ ...empty, error: "boom" }, [], ""), null);
});
test("a blank or whitespace query is never sent to the server", () => {
assert.equal(searchQuerySendable(""), false);
assert.equal(searchQuerySendable(" "), false);
assert.equal(searchQuerySendable(undefined), false);
assert.equal(searchQuerySendable("plan"), true);
assert.equal(searchQuerySendable(" plan "), true);
});
test("the refresh bar shows only when results are retained while a newer query loads", () => {
assert.equal(searchPanelProgress({ loading: true, results: [runHit()] }), true);
assert.equal(searchPanelProgress({ loading: true, results: [] }), false);
assert.equal(searchPanelProgress({ loading: false, results: [runHit()] }), false);
assert.equal(searchPanelProgress({}), false);
});
test("the panel body reports an error first, then rows, Searching…, the empty line or nothing", () => {
const loading = { query: "plan", loading: true, results: [], error: "" };
// The first load, nothing yet: the searching row.
assert.deepEqual(searchPanelBody(loading, [], ""), { kind: "searching", message: "" });
// A retained result set is still shown while a refresh is in flight.
const refresh = { query: "plan", loading: true, results: [runHit()], error: "" };
assert.deepEqual(searchPanelBody(refresh, [runHit()], ""), { kind: "results", message: "" });
// An error takes precedence over results.
assert.deepEqual(searchPanelBody({ ...refresh, error: "boom" }, [runHit()], ""), {
kind: "error",
message: "boom",
});
// A landed, empty reply: the empty-state sentence, naming the kind when set.
const landed = { query: "plan", loading: false, results: [], error: "" };
assert.deepEqual(searchPanelBody(landed, [], ""), { kind: "empty", message: 'No matches for "plan".' });
assert.deepEqual(searchPanelBody(landed, [], "run"), {
kind: "empty",
message: 'No runs match "plan".',
});
// A blank query, or a filter that emptied a list while a refresh is in flight:
// nothing at all.
assert.deepEqual(searchPanelBody({ query: " ", loading: false }, [], ""), { kind: "idle", message: "" });
assert.deepEqual(searchPanelBody(refresh, [], "run"), { kind: "idle", message: "" });
});
// ---------------------------------------------------------------------------
// Routing target + tab resolution
// ---------------------------------------------------------------------------
test("pipeline and version hits open the pipeline detail (version 0 = latest)", () => {
assert.deepEqual(searchTarget(pipelineHit()), { kind: "pipeline", pipelineId: 7, version: 3 });
assert.deepEqual(searchTarget(pipelineHit({ version: 0 })), {
kind: "pipeline",
pipelineId: 7,
version: 0,
});
assert.deepEqual(searchTarget(versionHit()), { kind: "pipeline", pipelineId: 9, version: 2 });
});
test("run, step, field and artifact hits open runs", () => {
assert.deepEqual(searchTarget(runHit()), { kind: "run", runId: 11, sequence: 0, matchedField: "" });
assert.deepEqual(searchTarget(stepHit()), { kind: "run", runId: 11, sequence: 4, matchedField: "" });
assert.deepEqual(searchTarget(fieldHit({}, "output")), {
kind: "run",
runId: 11,
sequence: 4,
matchedField: "output",
});
assert.deepEqual(searchTarget(artifactHit()), { kind: "run", runId: 11, sequence: 4, matchedField: "" });
});
test("an unknown result has no target and is never navigated", () => {
assert.equal(searchTarget({ kind: "future_kind", target: "widget:7" }), null);
});
test("the matched field resolves the run-detail tab, everything else Summary", () => {
assert.equal(searchFieldTab("input"), "input");
assert.equal(searchFieldTab("output"), "output");
assert.equal(searchFieldTab("transcript"), "summary");
assert.equal(searchFieldTab("evidence"), "summary");
assert.equal(searchFieldTab("other"), "summary");
assert.equal(searchFieldTab(""), "summary");
assert.equal(searchFieldTab(undefined), "summary");
});
const STEPS = [
{ sequence: 1, stage: "planner" },
{ sequence: 2, stage: "builder" },
{ sequence: 3, stage: "builder" },
];
test("a text-match target resolves to the run-detail tab holding it", () => {
assert.deepEqual(resolveRunTarget({ kind: "run", runId: 11, matchedField: "input" }, 11, STEPS), {
tab: "input",
});
assert.deepEqual(resolveRunTarget({ kind: "run", runId: 11, matchedField: "output" }, 11, STEPS), {
tab: "output",
});
assert.deepEqual(resolveRunTarget({ kind: "run", runId: 11, matchedField: "transcript" }, 11, STEPS), {
tab: "summary",
});
});
test("a step/artifact target resolves the visit index within its stage, not the visit number", () => {
// "builder" recorded two visits; sequence 3 is its second, index 1.
assert.deepEqual(resolveRunTarget({ kind: "run", runId: 11, sequence: 3 }, 11, STEPS), {
stageKey: "builder",
visitIndex: 1,
});
assert.deepEqual(resolveRunTarget({ kind: "run", runId: 11, sequence: 2 }, 11, STEPS), {
stageKey: "builder",
visitIndex: 0,
});
assert.deepEqual(resolveRunTarget({ kind: "run", runId: 11, sequence: 1 }, 11, STEPS), {
stageKey: "planner",
visitIndex: 0,
});
});
test("a bare run target or a visit that cannot be located falls back to the run detail", () => {
assert.deepEqual(resolveRunTarget({ kind: "run", runId: 11, sequence: 0 }, 11, STEPS), {});
assert.deepEqual(resolveRunTarget({ kind: "run", runId: 11, sequence: 99 }, 11, STEPS), {});
assert.deepEqual(resolveRunTarget(null, 11, STEPS), {});
assert.deepEqual(resolveRunTarget({ kind: "run", runId: 11, sequence: 1 }, 11, []), {});
});
// ---------------------------------------------------------------------------
// Debounce + stale-reply sequencing
// ---------------------------------------------------------------------------
test("a burst of keystrokes collapses to one request for the last query", () => {
const sequencer = new SearchSequencer();
const first = sequencer.edit("r");
const second = sequencer.edit("re");
const third = sequencer.edit("rel");
assert.equal(sequencer.begin(first), null);
assert.equal(sequencer.begin(second), null);
assert.deepEqual(sequencer.begin(third), { token: third.token, query: "rel" });
});
test("only the newest recorded edit is accepted, so a stale reply can never land", () => {
const sequencer = new SearchSequencer();
const sentOld = sequencer.begin(sequencer.edit("old"));
// A keystroke while the old reply is in flight invalidates it immediately.
const next = sequencer.edit("new");
assert.equal(sequencer.accepts(sentOld.token), false);
const sentNew = sequencer.begin(next);
assert.equal(sequencer.accepts(sentNew.token), true);
// A token that was never recorded is not accepted either.
assert.equal(sequencer.accepts(sentNew.token + 100), false);
// Closing records an empty edit, which invalidates the in-flight reply too.
sequencer.edit("");
assert.equal(sequencer.accepts(sentNew.token), false);
});
test("Enter sends the current query at once, skipping the debounce", () => {
const sequencer = new SearchSequencer();
sequencer.edit("release");
const sent = sequencer.begin(sequencer.edit("release"));
assert.deepEqual(sent, { token: sent.token, query: "release" });
assert.equal(sequencer.accepts(sent.token), true);
});
// ---------------------------------------------------------------------------
// Failure copy
// ---------------------------------------------------------------------------
test("a missing payload or an unknown command reports the unsupported sentence", () => {
assert.equal(searchFailureText(null), SEARCH_UNSUPPORTED);
assert.equal(searchFailureText(undefined), SEARCH_UNSUPPORTED);
assert.equal(searchFailureText(new Error("")), SEARCH_UNSUPPORTED);
assert.equal(searchFailureText(new Error("unknown command: search")), SEARCH_UNSUPPORTED);
assert.equal(searchFailureText(new Error("unsupported command")), SEARCH_UNSUPPORTED);
});
test("any other failure carries the server's own text", () => {
assert.equal(searchFailureText(new Error("boom")), "boom");
assert.equal(searchFailureText(new Error("request timed out after 30000ms")), "request timed out after 30000ms");
assert.equal(searchFailureText("plain string"), "plain string");
});