Files
pipelines/web/test/ui-search-types.mjs
T

87 lines
4.4 KiB
JavaScript

// 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));
}