1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { logField, matches, needles, parseSearch, traceField } from "./search.ts";
4
5test("parseSearch reads fields, comparisons, exclusions and quotes, and leaves unknown fields as text", () => {
6 assert.deepEqual(parseSearch(`status:5xx -path:/health duration:>1s "GET /x" user:42 -noise`, traceField), [
7 { field: "status", op: ":", value: "5", exclude: false },
8 { field: "path", op: ":", value: "/health", exclude: true },
9 { field: "duration", op: ">", value: "1s", exclude: false },
10 { field: null, op: ":", value: "GET /x", exclude: false },
11 { field: null, op: ":", value: "user:42", exclude: false },
12 { field: null, op: ":", value: "noise", exclude: true },
13 ]);
14 assert.deepEqual(parseSearch(`container:web http.route:/a`, logField).map((term) => term.field), ["container", null]);
15 assert.deepEqual(parseSearch(` "" - `, logField), [{ field: null, op: ":", value: "-", exclude: false }]);
16});
17
18test("matches takes fields by prefix, free text anywhere, and comparisons as numbers", () => {
19 const [status, text, above] = parseSearch("status:4xx GET status:>=500", traceField);
20 assert.ok(matches(status!, "404"));
21 assert.ok(!matches(status!, "204"));
22 assert.ok(matches(text!, "a get b"));
23 assert.ok(matches(above!, "503") && !matches(above!, "499"));
24 assert.ok(!matches(text!, undefined));
25 assert.deepEqual(needles(parseSearch("a -b container:c", logField)), ["a"]);
26});