aboutsummaryrefslogtreecommitdiffstats
path: root/tools/search-follows-test.js
blob: c0d86739d9407166584689b194f51e6558001ed7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// People search finds the people you follow first (Adam, 2026-11-17: Derek
// Ross moved to a new npub after his key leaked; searching "derek ross" came
// up with an empty impostor, because Primal doesn't index the new key yet
// and ranks by its own trust score). A follow whose name or NIP-05 matches
// outranks the search indexes; while follows' names are still loading, the
// answer isn't cached. Run: bun tools/search-follows-test.js
globalThis.localStorage = { getItem: () => null, setItem() {}, removeItem() {} };
const NEW = '6e479b7a5671f58a7a5d04ecedb00c1345b8918a8f8e270dc807ddb2818dba28';
const OLD = '3f770d65d3a764a9c5cb503ae123e62ec7598ad035d836e2a810f3877a745b24';
const FAKE = '1419ab45e0bdf22b51aeb0ddf2435b568e530b8ecf6f6f82172b181302f076ee';
// Primal's answer for "derekross": the impostor first
class FakeWS {
  constructor() { this.readyState = 0; setTimeout(() => { this.readyState = 1; this._open?.(); }, 5); }
  addEventListener(t, f) { if (t === 'open') this._open = f; }
  send(raw) {
    const [, sid, { cache: [, args] }] = JSON.parse(raw);
    const evs = args.query === 'derekross' ? [{ kind: 0, pubkey: FAKE, content: JSON.stringify({ name: 'Derek Ross' }) }] : [];
    setTimeout(() => { for (const e of evs) this.onmessage({ data: JSON.stringify(['EVENT', sid, e]) }); this.onmessage({ data: JSON.stringify(['EOSE', sid]) }); }, 5);
  }
  close() {}
}
globalThis.WebSocket = FakeWS;
globalThis.fetch = async () => ({ json: async () => ({ results: [] }) });
const { searchRecipients, setLocalPeople } = await import('../src/recipient-search.js');

let fails = 0;
const check = (name, ok, extra = '') => { console.log(`${ok ? ' ✓' : ' ✗'} ${name}${extra ? ' — ' + extra : ''}`); if (!ok) fails++; };

let partial = true;
setLocalPeople(() => {
  const out = [
    { pk: NEW, name: 'Derek Ross', nip05: '[email protected]' },
    { pk: OLD, name: 'nobody' },
    { pk: 'a'.repeat(64), name: 'Someone Else' },
  ];
  out.partial = partial;
  return out;
});
let rows = await searchRecipients('derek ross');
check('a follow named Derek Ross comes first', rows[0]?.pk === NEW, rows.map((r) => r.pk.slice(0, 8)).join(','));
check('the index result still listed after it', rows.some((r) => r.pk === FAKE));
check('the renamed old key is not a match', !rows.some((r) => r.pk === OLD));
partial = false;
setLocalPeople(() => [{ pk: NEW, name: 'Derek Ross (new)', nip05: '[email protected]' }]);
rows = await searchRecipients('derek ross');
check('a partial answer was not cached (asks again)', rows[0]?.name === 'Derek Ross (new)', rows[0]?.name);
rows = await searchRecipients('derekross.me');
check('NIP-05 matches too', rows[0]?.pk === NEW);
setLocalPeople(() => []);
rows = await searchRecipients('derek ross');
check('a complete answer is cached', rows[0]?.pk === NEW);
console.log(fails ? 'FAIL' : 'PASS');
process.exit(fails ? 1 : 0);