| 1 | import { describe, it, expect } from "vitest" ; |
| 2 | import { newEvent, newKey, pub, now, rand, sockets } from "./helpers.ts" ; |
| 3 | |
| 4 | const connect = sockets(); |
| 5 | |
| 6 | describe("NIP-50 search" , () => { |
| 7 | it("matches words in content, case-insensitively, ignores extensions, is injection-safe, and matches live" , async () => { |
| 8 | const sk = newKey(); |
| 9 | const c = await connect(); |
| 10 | const w = rand(); |
| 11 | const texts = [`Purple ${w}elephants dance` , `${w}orange juice` , `the ${w}elephant in the room` ]; |
| 12 | for (const [i, t] of texts.entries()) await c.publish(newEvent(sk, 1, t, [], now() - 10 + i)); |
| 13 | await c.publish(newEvent(sk, 4, `${w}elephant encrypted` , [["p" , pub(sk)]], now() - 5)); |
| 14 | |
| 15 | let r = await c.req({ search: `${w}elephant` , authors: [pub(sk)] }); |
| 16 | expect(r.events.map((e) => e.content)).toEqual([texts[2]]); |
| 17 | r = await c.req({ search: `PURPLE include:spam dance ${w}elephants` , authors: [pub(sk)] }); |
| 18 | expect(r.events.map((e) => e.content)).toEqual([texts[0]]); |
| 19 | r = await c.req({ search: `${w}juice OR NOT "` , authors: [pub(sk)] }); |
| 20 | expect(r.hints).toContain("finish" ); |
| 21 | |
| 22 | c.send("REQ" , "live" , { search: `${w}banana` , authors: [pub(sk)] }); |
| 23 | await c.drain(); |
| 24 | await c.publish(newEvent(sk, 1, `a ${w}Banana split` )); |
| 25 | await c.expect("EVENT" ); |
| 26 | }); |
| 27 | }); |