import test from "node:test"; import assert from "node:assert/strict"; import { markRanges, nearRegex, parseAsk } from "./query.mjs"; test("a phrase is the engine's plain query; quotes are dropped", () => { assert.deepEqual(parseAsk(" how many employees "), { query: "how many employees", regex: false, kind: "phrase" }); assert.deepEqual(parseAsk('"how many employees"'), { query: "how many employees", regex: false, kind: "phrase" }); assert.equal(parseAsk("").error, "Type what to find"); }); test("/…/ is a regex, and a bad one says what is wrong", () => { assert.deepEqual(parseAsk("/fir(ed|ing) them/"), { query: "fir(ed|ing) them", regex: true, kind: "regex" }); assert.match(parseAsk("/fir(ed/").error, /^Not a regex/); }); test("near:N compiles to a regex over one caption line, either order", () => { const p = parseAsk("fired near:3 employees"); assert.equal(p.kind, "near"); assert.equal(p.regex, true); assert.deepEqual(p.near, { a: "fired", b: "employees", n: 3 }); const re = new RegExp(p.query, "i"); assert.ok(re.test("you fired all the employees")); assert.ok(re.test("employees? you fired them")); assert.ok(!re.test("fired one two three four employees")); assert.equal(nearRegex("a.b", "c", 1), "a\\.b\\W+(?:\\w+\\W+){0,0}c|c\\W+(?:\\w+\\W+){0,0}a\\.b"); }); test("markRanges finds the query, case-insensitive, and the fired aliases, merged", () => { assert.deepEqual(markRanges("How many employees? how MANY", { query: "how many", regex: false }), [ [0, 8], [20, 28], ]); assert.deepEqual(markRanges("k cups and cake cups", { query: "k cups", regex: false, aliases: [{ pattern: "(k|cake)[ -]?cups", regex: true }] }), [ [0, 6], [11, 20], ]); assert.deepEqual(markRanges("anything", { query: "(", regex: true }), []); });