app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

search-match.test.ts (9819B)


      1 import { afterAll, describe, expect, it } from 'vitest';
      2 import { readFileSync } from 'node:fs';
      3 import { finalizeEvent } from 'applesauce-core/helpers';
      4 import { normalizePublicQuery } from '../../src/lib/catalog/query-input.ts';
      5 import { searchResolvedFood } from '../../src/lib/catalog/search-match.ts';
      6 import {
      7   createPublicHeadCandidate,
      8   publicHeadEnvelope
      9 } from '../../src/lib/catalog/heads.ts';
     10 import {
     11   createHeadResolver,
     12   resolveHeads,
     13   headResolutionSnapshot
     14 } from '../../src/lib/catalog/resolve-head.ts';
     15 import {
     16   verifyEnvelope,
     17   verifiedEnvelopeSnapshot
     18 } from '../../src/lib/nostr/verified-envelope.ts';
     19 import {
     20   createPublicScheduler,
     21   createPublicRun,
     22   openPublicRequest,
     23   closePublicScheduler
     24 } from '../../src/lib/nostr/request-scope.ts';
     25 import { validateRelayPolicy } from '../../src/lib/config/relays.ts';
     26 import {
     27   createPublicRetention,
     28   retainPublicEnvelope,
     29   publicRetentionKnown
     30 } from '../../src/lib/catalog/retention.ts';
     31 const corpus = JSON.parse(
     32   readFileSync(
     33     new URL(
     34       '../../../contracts/interop/food_availability/corpus.v1.json',
     35       import.meta.url
     36     ),
     37     'utf8'
     38   )
     39 ) as { vectors: { id: string; signed_wires: Record<string, string> }[] };
     40 const base = JSON.parse(
     41   corpus.vectors.find((v) => v.id.endsWith('_032'))!.signed_wires.previous
     42 ) as { tags: string[][]; content: string; created_at: number };
     43 const cleanup: (() => void)[] = [];
     44 afterAll(() => {
     45   for (const stop of cleanup) stop();
     46 });
     47 function fixture() {
     48   const key = crypto.getRandomValues(new Uint8Array(32));
     49   cleanup.push(() => key.fill(0));
     50   const proof = (
     51     time = base.created_at,
     52     tags = base.tags,
     53     content = base.content,
     54     kind = 30402
     55   ) => {
     56     const p = verifyEnvelope(
     57       JSON.stringify(
     58         finalizeEvent({ kind, created_at: time, tags, content }, key)
     59       )
     60     );
     61     if (!p.ok) throw Error('search fixture');
     62     return p.value;
     63   };
     64   const head = (p: ReturnType<typeof proof>) => {
     65     const h = createPublicHeadCandidate(p);
     66     if (!h) throw Error('search head');
     67     return h;
     68   };
     69   const policy = validateRelayPolicy(
     70     JSON.stringify({
     71       schemaVersion: 1,
     72       public: [],
     73       inbox: [],
     74       postingEnabled: false,
     75       messagingEnabled: false,
     76       operatorDenylist: []
     77     })
     78   )!;
     79   const scheduler = createPublicScheduler({
     80       now: () => 0,
     81       schedule: () => () => {}
     82     }),
     83     run = createPublicRun(scheduler, policy);
     84   cleanup.push(() => closePublicScheduler(scheduler));
     85   const retention = createPublicRetention();
     86   let available = true,
     87     now = base.created_at + 10,
     88     assessments = 0;
     89   const model = createHeadResolver(
     90     run,
     91     (kind, filters, next) => openPublicRequest(run, kind, () => () => {}, next),
     92     {
     93       nowSeconds: () => {
     94         assessments++;
     95         return now;
     96       }
     97     },
     98     {
     99       retain: (heads) => {
    100         for (const h of heads)
    101           retainPublicEnvelope(retention, publicHeadEnvelope(h));
    102         return true;
    103       },
    104       read: (h) => publicRetentionKnown(retention, h),
    105       available: () => available
    106     }
    107   );
    108   return {
    109     proof,
    110     head,
    111     model,
    112     retention,
    113     add: (p: ReturnType<typeof proof>) => resolveHeads(model, [head(p)]),
    114     retain: (p: ReturnType<typeof proof>) => retainPublicEnvelope(retention, p),
    115     unavailable: () => {
    116       available = false;
    117     },
    118     clock: (value: number) => {
    119       now = value;
    120     },
    121     clockSamples: () => assessments
    122   };
    123 }
    124 describe('deterministic admitted public food matching', () => {
    125   it('requires all terms across title summary description and public location', () => {
    126     const f = fixture();
    127     f.add(f.proof());
    128     const result = searchResolvedFood(f.model, 'NANTES fresh week SAANICH');
    129     expect(result.ok).toBe(true);
    130     if (!result.ok) throw Error('query');
    131     expect(result.rows).toHaveLength(1);
    132     expect(searchResolvedFood(f.model, 'Nantes missing')).toMatchObject({
    133       ok: true,
    134       rows: []
    135     });
    136   });
    137   it('normalizes NFKC case and whitespace copies while preserving signed food fields', () => {
    138     const f = fixture(),
    139       tags = base.tags.map((t) =>
    140         t[0] === 'title' ? ['title', 'CARROTS'] : [...t]
    141       ),
    142       p = f.proof(base.created_at, tags);
    143     f.add(p);
    144     const before = verifiedEnvelopeSnapshot(p);
    145     const result = searchResolvedFood(f.model, '  carrots\n SAANICH  ');
    146     expect(result).toMatchObject({ ok: true, query: 'carrots saanich' });
    147     if (!result.ok) throw Error('query');
    148     expect(result.rows).toHaveLength(1);
    149     expect(result.rows[0].state.food!.title).toBe('CARROTS');
    150     expect(verifiedEnvelopeSnapshot(p)).toEqual(before);
    151   });
    152   it('empty queries browse only known active supply and preserve last-known uncertainty', () => {
    153     const f = fixture();
    154     f.add(f.proof());
    155     const result = searchResolvedFood(f.model, ' \n ');
    156     if (!result.ok) throw Error('query');
    157     expect(result.rows).toHaveLength(1);
    158     expect(result.rows[0].lastKnown).toBe(true);
    159     expect(result.rows[0].definitiveAbsence).toBe(false);
    160   });
    161   it('sold revisions exclude the prior active keyword match', () => {
    162     const f = fixture();
    163     f.add(f.proof());
    164     f.retain(
    165       f.proof(
    166         base.created_at + 1,
    167         base.tags.map((t) => (t[0] === 'status' ? ['status', 'sold'] : [...t]))
    168       )
    169     );
    170     expect(searchResolvedFood(f.model, 'carrots')).toMatchObject({
    171       ok: true,
    172       rows: []
    173     });
    174     expect(searchResolvedFood(f.model, '')).toMatchObject({
    175       ok: true,
    176       rows: []
    177     });
    178   });
    179   it('unsupported newest heads block older focused matches', () => {
    180     const f = fixture();
    181     f.add(f.proof());
    182     f.retain(
    183       f.proof(
    184         base.created_at + 1,
    185         base.tags.map((t) =>
    186           t[0] === 'status' ? ['status', 'unknown'] : [...t]
    187         )
    188       )
    189     );
    190     expect(searchResolvedFood(f.model, 'carrots')).toMatchObject({
    191       ok: true,
    192       rows: []
    193     });
    194   });
    195   it('authorized retained deletion evidence excludes matches without older fallback', () => {
    196     const f = fixture(),
    197       p = f.proof();
    198     f.add(p);
    199     f.retain(
    200       f.proof(
    201         base.created_at - 1,
    202         [['e', verifiedEnvelopeSnapshot(p)!.id]],
    203         '',
    204         5
    205       )
    206     );
    207     expect(searchResolvedFood(f.model, 'carrots')).toMatchObject({
    208       ok: true,
    209       rows: []
    210     });
    211   });
    212   it('future and clock-unavailable heads cannot match or expose older rows', () => {
    213     const f = fixture();
    214     f.add(f.proof());
    215     f.retain(f.proof(base.created_at + 1000));
    216     expect(searchResolvedFood(f.model, '')).toMatchObject({
    217       ok: true,
    218       rows: []
    219     });
    220     const unavailable = fixture();
    221     unavailable.clock(NaN);
    222     unavailable.add(unavailable.proof());
    223     expect(headResolutionSnapshot(unavailable.model)[0].state.display).toBe(
    224       'clock_unavailable'
    225     );
    226     expect(searchResolvedFood(unavailable.model, 'carrots')).toMatchObject({
    227       ok: true,
    228       rows: []
    229     });
    230   });
    231   it('terminal projection unavailability differs from a bounded empty match', () => {
    232     const f = fixture();
    233     f.add(f.proof());
    234     f.unavailable();
    235     expect(searchResolvedFood(f.model, 'carrots')).toEqual({
    236       ok: true,
    237       query: 'carrots',
    238       available: false,
    239       rows: [],
    240       definitiveAbsence: false
    241     });
    242   });
    243   it('plain punctuation is literal and does not activate regex stemming or a geocoder', () => {
    244     const f = fixture();
    245     f.add(f.proof());
    246     for (const q of ['.*', 'carrot.*', '[a-z]', 'Central.*Saanich', 'carro']) {
    247       const result = searchResolvedFood(f.model, q);
    248       expect(result.ok).toBe(true);
    249       if (!result.ok) throw Error('query');
    250       expect(result.rows.length).toBe(q === 'carro' ? 1 : 0);
    251     }
    252     expect(searchResolvedFood(f.model, 'carroted')).toMatchObject({
    253       ok: true,
    254       rows: []
    255     });
    256   });
    257   it('enforces actual512UTF8 bytes and12 normalized terms before matching', () => {
    258     const f = fixture();
    259     f.add(f.proof());
    260     expect(searchResolvedFood(f.model, '菜'.repeat(170) + 'aa').ok).toBe(true);
    261     expect(searchResolvedFood(f.model, '菜'.repeat(171))).toEqual({
    262       ok: false,
    263       error: 'query_too_long'
    264     });
    265     expect(
    266       searchResolvedFood(f.model, Array(12).fill('carrots').join(' ')).ok
    267     ).toBe(true);
    268     expect(
    269       searchResolvedFood(f.model, Array(13).fill('carrots').join(' '))
    270     ).toEqual({ ok: false, error: 'too_many_terms' });
    271     expect(searchResolvedFood(f.model, 'ﷺ'.repeat(100)).ok).toBe(false);
    272   });
    273   it('rejects malformed query inputs without materializing a projection', () => {
    274     const f = fixture();
    275     f.add(f.proof());
    276     const assessments = f.clockSamples();
    277     for (const input of [undefined, {}, '\ud800', '\u0000'])
    278       expect(searchResolvedFood(f.model, input)).toEqual({
    279         ok: false,
    280         error: 'invalid_query'
    281       });
    282     expect(normalizePublicQuery('')).toMatchObject({
    283       ok: true,
    284       text: '',
    285       terms: []
    286     });
    287     expect(f.clockSamples()).toBe(assessments);
    288   });
    289   it('returns exact decimal and unknown quantity values without conversion or mutation', () => {
    290     const f = fixture(),
    291       tags = base.tags
    292         .filter((t) => t[0] !== 'radroots:quantity')
    293         .map((t) =>
    294           t[0] === 'price' ? ['price', '9007199254740993.125', t[2]] : [...t]
    295         );
    296     f.add(f.proof(base.created_at, tags));
    297     const result = searchResolvedFood(f.model, 'carrots');
    298     if (!result.ok) throw Error('query');
    299     expect(result.rows).toHaveLength(1);
    300     expect(result.rows[0].state.food!.price.amount).toBe(
    301       '9007199254740993.125'
    302     );
    303     expect(result.rows[0].state.food!.quantity).toBeNull();
    304     expect(headResolutionSnapshot(f.model)[0].state.food!.price.amount).toBe(
    305       '9007199254740993.125'
    306     );
    307   });
    308 });