app

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

continuation.test.ts (6321B)


      1 import { afterEach, describe, expect, it } from 'vitest';
      2 import { finalizeEvent } from 'applesauce-core/helpers';
      3 import { verifyEnvelope } from '../../src/lib/nostr/verified-envelope.ts';
      4 import {
      5   createPublicHeadCandidate,
      6   publicHeadSnapshot
      7 } from '../../src/lib/catalog/heads.ts';
      8 import {
      9   createChronologicalContinuation,
     10   localSearchPage
     11 } from '../../src/lib/catalog/continuation.ts';
     12 const keys: Uint8Array[] = [];
     13 afterEach(() => {
     14   for (const key of keys.splice(0)) key.fill(0);
     15 });
     16 function head(time: number, name: string, kind = 30402) {
     17   const key = crypto.getRandomValues(new Uint8Array(32));
     18   keys.push(key);
     19   const proof = verifyEnvelope(
     20     JSON.stringify(
     21       finalizeEvent(
     22         { kind, created_at: time, tags: [['d', name]], content: '' },
     23         key
     24       )
     25     )
     26   );
     27   if (!proof.ok) throw Error('fixture verification');
     28   const candidate = createPublicHeadCandidate(proof.value);
     29   if (!candidate) throw Error('fixture head');
     30   return candidate;
     31 }
     32 describe('inclusive chronological continuation and local pages', () => {
     33   it('does not cross a saturated source bucket because another source returned an older item', () => {
     34     const owner = createChronologicalContinuation(),
     35       a = head(100, 'a'),
     36       b = head(99, 'b');
     37     const first = owner.begin();
     38     first.observe(a);
     39     first.observe(b);
     40     first.finish(true, 'bounded-eose', [
     41       { source: 'one', saturated: true, eventIds: [publicHeadSnapshot(a).id] },
     42       { source: 'two', saturated: false, eventIds: [publicHeadSnapshot(b).id] }
     43     ]);
     44     expect(owner.snapshot().until).toBe(100);
     45     const next = owner.begin();
     46     next.observe(a);
     47     next.observe(b);
     48     next.finish(true, 'bounded-eose', [
     49       { source: 'one', saturated: true, eventIds: [publicHeadSnapshot(a).id] },
     50       { source: 'two', saturated: false, eventIds: [publicHeadSnapshot(b).id] }
     51     ]);
     52     expect(owner.snapshot()).toMatchObject({
     53       until: 100,
     54       gap: true,
     55       coverage: 'partial'
     56     });
     57   });
     58 
     59   it('repeats the observed boundary and stops a repeated saturated equal-time bucket with an explicit gap', () => {
     60     const owner = createChronologicalContinuation(),
     61       item = head(100, 'a');
     62     const first = owner.begin();
     63     first.observe(item);
     64     first.observe(item);
     65     first.finish(true, 'bounded-eose');
     66     expect(owner.snapshot()).toMatchObject({
     67       until: 100,
     68       windows: 1,
     69       observed: 1,
     70       gap: false,
     71       definitiveAbsence: false
     72     });
     73     const next = owner.begin();
     74     expect(next.until).toBe(100);
     75     next.observe(item);
     76     next.finish(true, 'bounded-eose');
     77     expect(owner.snapshot()).toMatchObject({
     78       until: 100,
     79       gap: true,
     80       coverage: 'partial'
     81     });
     82     expect(() => owner.restart()).toThrow('food_search_chronological_gap');
     83   });
     84   it('accepts new boundary IDs as progress and advances only to an actually observed older timestamp', () => {
     85     const owner = createChronologicalContinuation(),
     86       a = head(100, 'a'),
     87       b = head(100, 'b'),
     88       c = head(99, 'c');
     89     const first = owner.begin();
     90     first.observe(a);
     91     first.finish(true, 'bounded-eose');
     92     const next = owner.begin();
     93     next.observe(a);
     94     next.observe(b);
     95     next.finish(true, 'bounded-eose');
     96     expect(owner.snapshot()).toMatchObject({
     97       until: 100,
     98       gap: false,
     99       windows: 2
    100     });
    101     expect(() => owner.begin()).toThrow(
    102       'food_search_chronological_window_limit'
    103     );
    104     const older = owner.restart(),
    105       window = older.begin();
    106     expect(window.until).toBe(100);
    107     window.observe(c);
    108     window.finish(false, 'bounded-eose');
    109     expect(older.snapshot()).toMatchObject({
    110       until: 99,
    111       windows: 1,
    112       gap: false
    113     });
    114   });
    115   it('ignores wrong-kind/out-of-bound/forged candidates for cursor purposes and preserves partial coverage', () => {
    116     const owner = createChronologicalContinuation(),
    117       first = owner.begin();
    118     first.observe(head(10, 'a'));
    119     first.finish(false, 'partial');
    120     const next = owner.begin();
    121     next.observe(head(11, 'future'));
    122     next.observe(head(0, 'profile', 0));
    123     expect(() => next.observe({} as ReturnType<typeof head>)).toThrow(
    124       'public_head_invalid'
    125     );
    126     next.finish(true, 'bounded-eose');
    127     expect(owner.snapshot()).toMatchObject({
    128       until: 10,
    129       gap: true,
    130       coverage: 'partial'
    131     });
    132   });
    133   it('does not invent an older boundary for an empty window, and refuses parallel windows', () => {
    134     const owner = createChronologicalContinuation(),
    135       first = owner.begin();
    136     expect(() => owner.begin()).toThrow('food_search_chronological_pending');
    137     first.finish(false, 'bounded-eose');
    138     expect(owner.snapshot().until).toBeUndefined();
    139     expect(() => owner.restart()).toThrow(
    140       'food_search_chronological_boundary_missing'
    141     );
    142     expect(() => first.observe(head(0, 'late'))).toThrow(
    143       'food_search_chronological_window_closed'
    144     );
    145   });
    146   it('restarts with known boundary evidence without mutating the predecessor or erasing partial coverage', () => {
    147     const owner = createChronologicalContinuation(),
    148       item = head(0, 'zero'),
    149       first = owner.begin();
    150     first.observe(item);
    151     first.finish(true, 'partial');
    152     const next = owner.restart(),
    153       window = next.begin();
    154     window.observe(item);
    155     window.finish(true, 'bounded-eose');
    156     expect(next.snapshot()).toMatchObject({
    157       until: 0,
    158       gap: true,
    159       coverage: 'partial'
    160     });
    161     expect(owner.snapshot()).toMatchObject({ windows: 1, gap: false });
    162     const copy = owner.snapshot();
    163     Object.assign(copy, { until: 100 });
    164     expect(owner.snapshot().until).toBe(0);
    165   });
    166   it('shows20 more local rows per page without changing order or creating network work', () => {
    167     const rows = Array.from({ length: 45 }, (_, n) => n);
    168     expect(localSearchPage(rows, 1)).toEqual({
    169       rows: rows.slice(0, 20),
    170       hasMore: true
    171     });
    172     expect(localSearchPage(rows, 2)).toEqual({
    173       rows: rows.slice(0, 40),
    174       hasMore: true
    175     });
    176     expect(localSearchPage(rows, 3)).toEqual({ rows, hasMore: false });
    177     for (const value of [0, -1, 1.5, NaN, 101])
    178       expect(() => localSearchPage(rows, value)).toThrow(
    179         'food_search_local_page_invalid'
    180       );
    181     expect(rows).toHaveLength(45);
    182   });
    183 });