|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { |
| 5 | + isChannelUuid, |
| 6 | + isHexPubkey, |
| 7 | + normalizeFromHandle, |
| 8 | + normalizeInChannel, |
| 9 | + parseSearchOperators, |
| 10 | +} from "./parseSearchOperators.ts"; |
| 11 | + |
| 12 | +test("leaves plain text unchanged", () => { |
| 13 | + assert.deepEqual(parseSearchOperators(" deploy "), { |
| 14 | + text: "deploy", |
| 15 | + from: null, |
| 16 | + in: null, |
| 17 | + since: null, |
| 18 | + until: null, |
| 19 | + }); |
| 20 | +}); |
| 21 | + |
| 22 | +test("extracts from / in / after / before and keeps remaining FTS text", () => { |
| 23 | + const parsed = parseSearchOperators( |
| 24 | + "deploy from:deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef in:#general after:2024-01-15 before:2024-02-01 status", |
| 25 | + ); |
| 26 | + assert.equal(parsed.text, "deploy status"); |
| 27 | + assert.equal( |
| 28 | + parsed.from, |
| 29 | + "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef", |
| 30 | + ); |
| 31 | + assert.equal(parsed.in, "#general"); |
| 32 | + assert.equal( |
| 33 | + parsed.since, |
| 34 | + Math.floor(new Date(2024, 0, 15).getTime() / 1000), |
| 35 | + ); |
| 36 | + assert.equal( |
| 37 | + parsed.until, |
| 38 | + Math.floor(new Date(2024, 1, 1).getTime() / 1000) - 1, |
| 39 | + ); |
| 40 | +}); |
| 41 | + |
| 42 | +test("before: excludes the named day's exact local midnight", () => { |
| 43 | + const localMidnight = Math.floor(new Date(2024, 1, 1).getTime() / 1000); |
| 44 | + const parsed = parseSearchOperators("deploy before:2024-02-01"); |
| 45 | + |
| 46 | + assert.equal(parsed.until, localMidnight - 1); |
| 47 | + // The relay keeps events where `created_at <= until`, so a message stamped |
| 48 | + // exactly at midnight must fail that predicate. |
| 49 | + assert.equal(localMidnight <= parsed.until, false); |
| 50 | +}); |
| 51 | + |
| 52 | +test("keeps invalid date operators in the FTS text", () => { |
| 53 | + const parsed = parseSearchOperators("notes after:yesterday before:soon"); |
| 54 | + assert.equal(parsed.text, "notes after:yesterday before:soon"); |
| 55 | + assert.equal(parsed.since, null); |
| 56 | + assert.equal(parsed.until, null); |
| 57 | +}); |
| 58 | + |
| 59 | +test("later operators of the same kind win", () => { |
| 60 | + const parsed = parseSearchOperators( |
| 61 | + "from:@alice from:@bob in:one in:two after:2024-01-01 after:2024-03-01", |
| 62 | + ); |
| 63 | + assert.equal(parsed.from, "@bob"); |
| 64 | + assert.equal(parsed.in, "two"); |
| 65 | + assert.equal(parsed.since, Math.floor(new Date(2024, 2, 1).getTime() / 1000)); |
| 66 | + assert.equal(parsed.text, ""); |
| 67 | +}); |
| 68 | + |
| 69 | +test("rejects impossible calendar dates", () => { |
| 70 | + const parsed = parseSearchOperators("x after:2024-02-30"); |
| 71 | + assert.equal(parsed.since, null); |
| 72 | + assert.equal(parsed.text, "x after:2024-02-30"); |
| 73 | +}); |
| 74 | + |
| 75 | +test("does not treat hyphen or slash adjacent tokens as operators", () => { |
| 76 | + assert.deepEqual(parseSearchOperators("built-in:react hooks"), { |
| 77 | + text: "built-in:react hooks", |
| 78 | + from: null, |
| 79 | + in: null, |
| 80 | + since: null, |
| 81 | + until: null, |
| 82 | + }); |
| 83 | + assert.deepEqual(parseSearchOperators("sign-in:flow broken"), { |
| 84 | + text: "sign-in:flow broken", |
| 85 | + from: null, |
| 86 | + in: null, |
| 87 | + since: null, |
| 88 | + until: null, |
| 89 | + }); |
| 90 | + assert.deepEqual(parseSearchOperators("https://x.com/in:foo"), { |
| 91 | + text: "https://x.com/in:foo", |
| 92 | + from: null, |
| 93 | + in: null, |
| 94 | + since: null, |
| 95 | + until: null, |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +test("strips trailing punctuation from operator values", () => { |
| 100 | + const parsed = parseSearchOperators("deploy in:general, from:@alice."); |
| 101 | + assert.equal(parsed.in, "#general".replace("#", "") || "general"); |
| 102 | + assert.equal(parsed.in, "general"); |
| 103 | + assert.equal(parsed.from, "@alice"); |
| 104 | + assert.equal(parsed.text, "deploy"); |
| 105 | +}); |
| 106 | + |
| 107 | +test("helpers recognize hex pubkeys and channel uuids", () => { |
| 108 | + assert.equal( |
| 109 | + isHexPubkey( |
| 110 | + "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef", |
| 111 | + ), |
| 112 | + true, |
| 113 | + ); |
| 114 | + assert.equal(isHexPubkey("npub1abc"), false); |
| 115 | + assert.equal(isChannelUuid("11111111-1111-1111-1111-111111111111"), true); |
| 116 | + assert.equal(normalizeFromHandle("@alice"), "alice"); |
| 117 | + assert.equal(normalizeInChannel("#general"), "general"); |
| 118 | +}); |
0 commit comments