web: parse search type prefixes into kind filters
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { parseSearchQuery, SEARCH_KINDS } from "../src/search.js";
|
||||
|
||||
test("both type aliases and kinds are case insensitive", () => {
|
||||
for (const { value } of SEARCH_KINDS.filter((kind) => kind.value)) {
|
||||
for (const prefix of ["t", "type", "T", "TyPe"]) {
|
||||
assert.deepEqual(parseSearchQuery(`${prefix}:${value.toUpperCase()} plan`), {
|
||||
query: " plan", kind: value, hasType: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("normal, quoted and unsupported text is preserved byte for byte", () => {
|
||||
for (const query of [
|
||||
' ordinary text\twith\nspacing ',
|
||||
'"t:pipeline" "hello world"', "'type:artifact'",
|
||||
'"escaped \\" t:run"', '"unclosed t:run',
|
||||
"t:prompt type:unknown t: t:PIPELINES", "not:t:run t:run,",
|
||||
"prefix\"t:run\"", "\\t:run",
|
||||
]) {
|
||||
assert.deepEqual(parseSearchQuery(query, "field"), { query, kind: "field", hasType: false });
|
||||
}
|
||||
assert.equal(parseSearchQuery('t:artifact "t:pipeline" report').query, ' "t:pipeline" report');
|
||||
});
|
||||
|
||||
test("last supported token wins over selector; unknown tokens do not override it", () => {
|
||||
assert.deepEqual(parseSearchQuery("t:run plan TYPE:artifact t:unknown", "pipeline"), {
|
||||
query: " plan t:unknown", kind: "artifact", hasType: true,
|
||||
});
|
||||
assert.equal(parseSearchQuery("plan", "step").kind, "step");
|
||||
});
|
||||
|
||||
test("type-only query is distinguishable from literal text and blank input", () => {
|
||||
assert.deepEqual(parseSearchQuery("t:pipeline"), { query: "", kind: "pipeline", hasType: true });
|
||||
assert.deepEqual(parseSearchQuery(""), { query: "", kind: "", hasType: false });
|
||||
});
|
||||
@@ -0,0 +1,86 @@
|
||||
// Isolated browser fixture: real view + repository, recorded wire frames; no
|
||||
// connection to any Pipelines service and no production data or mutations.
|
||||
import assert from "node:assert/strict";
|
||||
import { createServer } from "node:http";
|
||||
import { readFile, mkdir } from "node:fs/promises";
|
||||
import { resolve, extname } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { launchChrome, eventually } from "./browser.mjs";
|
||||
|
||||
const root = fileURLToPath(new URL("../", import.meta.url));
|
||||
const out = resolve(process.argv[2] || "captures/search-types");
|
||||
await mkdir(out, { recursive: true });
|
||||
const server = createServer(async (req, res) => {
|
||||
try {
|
||||
if (req.url === "/") {
|
||||
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
||||
const index = await readFile(resolve(root, "index.html"), "utf8");
|
||||
const styles = index.match(/<link rel="stylesheet"[^>]+>/g).join("\n");
|
||||
res.end(`<!doctype html><meta charset="utf-8">${styles}<body><p>Isolated search fixture — recorded requests, no live service</p><div id="host"></div>`);
|
||||
return;
|
||||
}
|
||||
const path = resolve(root, `.${req.url}`);
|
||||
if (!path.startsWith(root)) throw new Error("invalid path");
|
||||
res.setHeader("Content-Type", extname(path) === ".js" ? "text/javascript" : "text/css");
|
||||
res.end(await readFile(path));
|
||||
} catch {
|
||||
res.writeHead(404).end();
|
||||
}
|
||||
});
|
||||
await new Promise((done) => server.listen(0, "127.0.0.1", done));
|
||||
let browser;
|
||||
try {
|
||||
browser = await launchChrome();
|
||||
const page = await browser.openPage(`http://127.0.0.1:${server.address().port}`);
|
||||
await page.evaluate(`(async () => {
|
||||
const { createSearchView } = await import('/src/views/search.js');
|
||||
const { SearchSequencer } = await import('/src/search.js');
|
||||
const { PipelinesRepository } = await import('/src/repository.js');
|
||||
window.framesSent = [];
|
||||
const repo = new PipelinesRepository({ call: async (frame) => {
|
||||
framesSent.push(frame);
|
||||
return { search_results: { results: [] } };
|
||||
}});
|
||||
const state = { searchQuery: '', searchKind: '', searchSort: 'relevance',
|
||||
searchResults: [], searchSequencer: new SearchSequencer(), searchActiveIndex: 0 };
|
||||
const view = createSearchView({ state, repo, searchHost: document.querySelector('#host'), renderHeader() {} });
|
||||
view.openSearch();
|
||||
})()`);
|
||||
const last = () => page.evaluate("framesSent.at(-1)");
|
||||
const search = async (text) => {
|
||||
const before = await page.evaluate("framesSent.length");
|
||||
await page.fill("#searchInput", text);
|
||||
await eventually(async () => (await page.evaluate("framesSent.length")) > before);
|
||||
};
|
||||
await search("T:pipeline plan");
|
||||
assert.deepEqual((await last()).search, { query: " plan", kind: "pipeline", limit: 0 });
|
||||
assert.equal(await page.evaluate("document.querySelector('#searchKind').value"), "pipeline");
|
||||
await page.screenshot(`${out}/01-type-filter.png`);
|
||||
|
||||
await search("t:run plan TYPE:artifact");
|
||||
assert.equal((await last()).search.kind, "artifact");
|
||||
await page.screenshot(`${out}/02-last-type-wins.png`);
|
||||
await page.evaluate("document.querySelector('#searchKind').value = 'step'; document.querySelector('#searchKind').dispatchEvent(new Event('change'))");
|
||||
await eventually(async () => (await last()).search.kind === "step");
|
||||
assert.equal(await page.evaluate("document.querySelector('#searchInput').value"), " plan ");
|
||||
await page.screenshot(`${out}/03-selector-replaces-token.png`);
|
||||
|
||||
await search('"t:run" t:unknown');
|
||||
assert.equal((await last()).search.query, '"t:run" t:unknown');
|
||||
await page.screenshot(`${out}/04-literal-tokens.png`);
|
||||
|
||||
const before = await page.evaluate("framesSent.length");
|
||||
await page.fill("#searchInput", "t:pipeline");
|
||||
await eventually(() => page.evaluate("document.querySelector('#searchResults').textContent.includes('Add search text')"));
|
||||
await new Promise((done) => setTimeout(done, 400));
|
||||
assert.equal(await page.evaluate("framesSent.length"), before);
|
||||
await page.screenshot(`${out}/05-type-only-help.png`);
|
||||
await page.click("#searchClearFilter");
|
||||
assert.equal(await page.evaluate("document.querySelector('#searchInput').value"), "");
|
||||
assert.equal(await page.evaluate("document.querySelector('#searchKind').value"), "");
|
||||
await page.screenshot(`${out}/06-cleared-filter.png`);
|
||||
console.log(`PASS: type tokens, wire kind/query, precedence, selector replacement, literals, type-only help and clear. Captures: ${out}`);
|
||||
} finally {
|
||||
await browser?.close();
|
||||
await new Promise((done) => server.close(done));
|
||||
}
|
||||
Reference in New Issue
Block a user