app

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

history-pagination.spec.ts (17768B)


      1 import { test, expect } from '@playwright/test';
      2 import { build } from 'vite';
      3 import { fileURLToPath } from 'node:url';
      4 import { once } from 'node:events';
      5 import { WebSocketServer } from 'ws';
      6 import { createStaticHarness } from '../integration/harness/static.ts';
      7 import type * as Fixture from './harness/history-pagination.ts';
      8 declare global {
      9   interface Window {
     10     hcp097: typeof Fixture;
     11     hcp097Fixture: Awaited<ReturnType<typeof Fixture.makeFixture>>;
     12     hcp097Pending: Promise<boolean>;
     13   }
     14 }
     15 const origins = ['wss://archive.example.org', 'wss://peer.example.org'];
     16 const servers: WebSocketServer[] = [],
     17   endpoints: string[] = [];
     18 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string;
     19 let requests: { origin: string; frame: unknown[] }[] = [],
     20   closes: unknown[][] = [];
     21 let frames: Record<string, unknown>[][] = [[], []],
     22   fault: number | undefined,
     23   hold: number | undefined,
     24   ignoreLimit = false,
     25   ignoreWindow = false;
     26 test.beforeAll(async () => {
     27   server = await createStaticHarness();
     28   for (let i = 0; i < 2; i++) {
     29     const ws = new WebSocketServer({
     30       host: '127.0.0.1',
     31       port: 0,
     32       maxPayload: 65536
     33     });
     34     servers.push(ws);
     35     await once(ws, 'listening');
     36     const address = ws.address();
     37     if (!address || typeof address === 'string') throw Error('no loopback');
     38     endpoints.push('ws://127.0.0.1:' + address.port);
     39     ws.on('connection', (socket) =>
     40       socket.on('message', (bytes) => {
     41         const raw = Buffer.isBuffer(bytes)
     42           ? bytes
     43           : Array.isArray(bytes)
     44             ? Buffer.concat(bytes)
     45             : Buffer.from(bytes);
     46         const frame = JSON.parse(raw.toString()) as unknown[];
     47         if (frame[0] === 'CLOSE') {
     48           closes.push(frame);
     49           return;
     50         }
     51         if (frame[0] !== 'REQ') return;
     52         requests.push({ origin: origins[i], frame });
     53         const f = frame[2] as { limit: number; since?: number; until?: number };
     54         if (f.limit === 0) {
     55           socket.send(JSON.stringify(['EOSE', frame[1]]));
     56           return;
     57         }
     58         if (fault === i) {
     59           socket.send(
     60             JSON.stringify(['CLOSED', frame[1], 'controlled unavailable'])
     61           );
     62           return;
     63         }
     64         const rows = frames[i]
     65           .filter(
     66             (e) =>
     67               ignoreWindow ||
     68               ((f.since === undefined || Number(e.created_at) >= f.since) &&
     69                 (f.until === undefined || Number(e.created_at) <= f.until))
     70           )
     71           .sort(
     72             (a, b) =>
     73               Number(b.created_at) - Number(a.created_at) ||
     74               String(a.id).localeCompare(String(b.id))
     75           );
     76         for (const event of ignoreLimit ? rows : rows.slice(0, f.limit))
     77           socket.send(JSON.stringify(['EVENT', frame[1], event]));
     78         if (hold !== i) socket.send(JSON.stringify(['EOSE', frame[1]]));
     79       })
     80     );
     81   }
     82   const output = await build({
     83     configFile: false,
     84     logLevel: 'silent',
     85     build: {
     86       write: false,
     87       minify: false,
     88       lib: {
     89         entry: fileURLToPath(
     90           new URL('./harness/history-pagination.ts', import.meta.url)
     91         ),
     92         name: 'hcp097',
     93         formats: ['iife']
     94       }
     95     }
     96   });
     97   const result = Array.isArray(output) ? output[0] : output;
     98   if (!('output' in result)) throw Error('no bundle');
     99   const chunks = result.output.filter((c) => c.type === 'chunk');
    100   expect(chunks).toHaveLength(1);
    101   bundle = chunks[0].code;
    102 });
    103 test.afterAll(async () => {
    104   for (const ws of servers) {
    105     for (const socket of ws.clients) socket.terminate();
    106     await new Promise<void>((resolve) => ws.close(() => resolve()));
    107   }
    108   await server.close();
    109 });
    110 test.beforeEach(async ({ page }) => {
    111   requests = [];
    112   frames = [[], []];
    113   closes = [];
    114   fault = undefined;
    115   hold = undefined;
    116   ignoreLimit = false;
    117   ignoreWindow = false;
    118   await page.addInitScript(
    119     ({ origins, endpoints }) => {
    120       const Native = window.WebSocket;
    121       window.WebSocket = class extends Native {
    122         constructor(url: string | URL, protocols?: string | string[]) {
    123           const i = origins.indexOf(new URL(String(url)).origin);
    124           if (i < 0) throw Error('unapproved fixture destination');
    125           super(endpoints[i], protocols);
    126         }
    127       };
    128     },
    129     { origins, endpoints }
    130   );
    131   await page.goto(server.url + '/search');
    132   await page.addScriptTag({ content: bundle });
    133   await page.evaluate(async () => {
    134     window.hcp097Fixture = await window.hcp097.makeFixture();
    135     window.hcp097Fixture.capture();
    136     window.hcp097Fixture.start();
    137   });
    138   await expect
    139     .poll(() => page.evaluate(() => window.hcp097Fixture.snapshot()?.backfill))
    140     .toBe('complete');
    141   await expect.poll(() => requests.length).toBe(6);
    142   requests = [];
    143 });
    144 test.afterEach(async ({ page }) => {
    145   if (!page.isClosed())
    146     await page.evaluate(() => window.hcp097Fixture?.close());
    147 });
    148 const event = (wire: string) => JSON.parse(wire) as Record<string, unknown>;
    149 test('actual retained outer metadata sets inclusive until independently of original inner ordering', async ({
    150   page
    151 }) => {
    152   const wire = await page.evaluate(() =>
    153     window.hcp097Fixture.oldSignedOuter(100)
    154   );
    155   await page.evaluate((w) => window.hcp097Fixture.seed(w), wire);
    156   frames[0] = [event(wire)];
    157   expect(await page.evaluate(() => window.hcp097Fixture.captureOlder())).toBe(
    158     true
    159   );
    160   expect(await page.evaluate(() => window.hcp097Fixture.loadOlder())).toBe(
    161     true
    162   );
    163   expect(requests[0].frame[2]).toEqual({
    164     kinds: [1059],
    165     '#p': [await page.evaluate(() => window.hcp097Fixture.owner)],
    166     limit: 200,
    167     until: 100
    168   });
    169   expect(
    170     await page.evaluate(
    171       () => window.hcp097Fixture.olderSnapshot()?.historyComplete
    172     )
    173   ).toBe(false);
    174 });
    175 test('equal-time full bucket stays inclusive and repeated page saturates without automatic third request', async ({
    176   page
    177 }) => {
    178   const seed = await page.evaluate(() =>
    179     window.hcp097Fixture.oldSignedOuter(100)
    180   );
    181   await page.evaluate((w) => window.hcp097Fixture.seed(w), seed);
    182   frames[0] = (
    183     await page.evaluate(() =>
    184       Array.from({ length: 200 }, () =>
    185         window.hcp097Fixture.oldSignedOuter(100)
    186       )
    187     )
    188   ).map(event);
    189   await page.evaluate(async () => {
    190     window.hcp097Fixture.captureOlder();
    191     await window.hcp097Fixture.loadOlder();
    192   });
    193   expect(requests).toHaveLength(2);
    194   expect(
    195     await page.evaluate(() => window.hcp097Fixture.olderSnapshot()?.sources[0])
    196   ).toMatchObject({ until: 100, state: 'capped' });
    197   await page.evaluate(() => window.hcp097Fixture.loadOlder());
    198   expect(requests).toHaveLength(4);
    199   expect(
    200     await page.evaluate(() => window.hcp097Fixture.olderSnapshot()?.sources[0])
    201   ).toMatchObject({ until: 100, state: 'saturated' });
    202   expect(
    203     requests
    204       .filter((r) => r.origin === origins[0])
    205       .map((r) => (r.frame[2] as { until: number }).until)
    206   ).toEqual([100, 100]);
    207 });
    208 test('actual older retained ID progresses source boundary without changing original outbox', async ({
    209   page
    210 }) => {
    211   const original = await page.evaluate(
    212     async () => (await window.hcp097Fixture.row())?.wire
    213   );
    214   const seed = await page.evaluate(() =>
    215     window.hcp097Fixture.oldSignedOuter(100)
    216   );
    217   await page.evaluate((w) => window.hcp097Fixture.seed(w), seed);
    218   const older = await page.evaluate(() =>
    219     window.hcp097Fixture.oldSignedOuter(99)
    220   );
    221   frames[0] = [event(seed), event(older)];
    222   await page.evaluate(async () => {
    223     window.hcp097Fixture.captureOlder();
    224     await window.hcp097Fixture.loadOlder();
    225   });
    226   expect(
    227     await page.evaluate(() => window.hcp097Fixture.olderSnapshot()?.sources[0])
    228   ).toMatchObject({ until: 99, state: 'older_available' });
    229   expect(
    230     await page.evaluate(async () => (await window.hcp097Fixture.row())?.wire)
    231   ).toBe(original);
    232   expect(
    233     await page.evaluate(
    234       (id) => window.hcp097Fixture.stored(id),
    235       String(frames[0][1].id)
    236     )
    237   ).toBeDefined();
    238   expect(
    239     await page.evaluate(
    240       (id) => window.hcp097Fixture.publicContains(id),
    241       String(frames[0][1].id)
    242     )
    243   ).toBe(false);
    244 });
    245 test('one actual relay CLOSED preserves successful peer and old confirmed metadata', async ({
    246   page
    247 }) => {
    248   const seed = await page.evaluate(() =>
    249     window.hcp097Fixture.oldSignedOuter(100)
    250   );
    251   await page.evaluate(async (w) => {
    252     await window.hcp097Fixture.seed(w);
    253     await window.hcp097Fixture.seed(w, 'wss://peer.example.org');
    254   }, seed);
    255   const older = await page.evaluate(() =>
    256     window.hcp097Fixture.oldSignedOuter(99)
    257   );
    258   fault = 0;
    259   frames[1] = [event(older)];
    260   await page.evaluate(async () => {
    261     window.hcp097Fixture.captureOlder();
    262     await window.hcp097Fixture.loadOlder();
    263   });
    264   expect(requests).toHaveLength(2);
    265   expect(
    266     await page.evaluate(() => window.hcp097Fixture.olderSnapshot()?.sources)
    267   ).toMatchObject([
    268     { state: 'partial', until: 100 },
    269     { state: 'older_available', until: 99 }
    270   ]);
    271   expect(
    272     await page.evaluate(
    273       (id) => window.hcp097Fixture.stored(id),
    274       String(event(seed).id)
    275     )
    276   ).toBeDefined();
    277   expect(
    278     await page.evaluate(
    279       (id) => window.hcp097Fixture.stored(id),
    280       String(event(older).id)
    281     )
    282   ).toBeDefined();
    283 });
    284 test('copied history token wrong review and closed original unlock cannot open older requests', async ({
    285   page
    286 }) => {
    287   expect(
    288     await page.evaluate(() => window.hcp097Fixture.captureOlder('unreviewed'))
    289   ).toBe(false);
    290   await page.evaluate(() => window.hcp097Fixture.captureOlder());
    291   expect(
    292     await page.evaluate(() => window.hcp097Fixture.copiedOlderStart())
    293   ).toBe(false);
    294   await page.evaluate(() => window.hcp097Fixture.closeUnlock());
    295   expect(await page.evaluate(() => window.hcp097Fixture.loadOlder())).toBe(
    296     false
    297   );
    298   expect(requests).toEqual([]);
    299 });
    300 test('original stop during first source wait closes it and fences remaining source requests', async ({
    301   page
    302 }) => {
    303   hold = 0;
    304   await page.evaluate(() => {
    305     window.hcp097Fixture.captureOlder();
    306     window.hcp097Pending = window.hcp097Fixture.loadOlder();
    307   });
    308   await expect.poll(() => requests.length).toBe(1);
    309   await page.evaluate(() => window.hcp097Fixture.stopOlder());
    310   await expect
    311     .poll(() => closes.some((frame) => frame[1] === requests[0].frame[1]))
    312     .toBe(true);
    313   await page.evaluate(() => window.hcp097Pending);
    314   expect(requests).toHaveLength(1);
    315   expect(
    316     await page.evaluate(() => window.hcp097Fixture.olderSnapshot()?.state)
    317   ).toBe('stopped');
    318 });
    319 test('fresh extension owner change after history capture prevents either source query', async ({
    320   page
    321 }) => {
    322   await page.evaluate(() => {
    323     window.hcp097Fixture.captureOlder();
    324     window.hcp097Fixture.changeKey();
    325   });
    326   await page.evaluate(() => window.hcp097Fixture.loadOlder());
    327   expect(requests).toEqual([]);
    328   expect(
    329     await page.evaluate(
    330       () => window.hcp097Fixture.olderSnapshot()?.historyComplete
    331     )
    332   ).toBe(false);
    333 });
    334 test('501 duplicate deliveries exhaust shared action cap before dedup with no automatic retry or eviction', async ({
    335   page
    336 }) => {
    337   const wire = await page.evaluate(() => window.hcp097Fixture.outer);
    338   frames[0] = Array.from({ length: 501 }, () => event(wire));
    339   ignoreLimit = true;
    340   await page.evaluate(async () => {
    341     window.hcp097Fixture.captureOlder();
    342     await window.hcp097Fixture.loadOlder();
    343   });
    344   expect(requests).toHaveLength(1);
    345   expect(
    346     await page.evaluate(() => window.hcp097Fixture.olderSnapshot())
    347   ).toMatchObject({
    348     state: 'needs_action',
    349     reason: 'budget',
    350     historyComplete: false
    351   });
    352 });
    353 
    354 test('actual page reload recovers owner-scoped oldest metadata from existing IndexedDB without resaving outbox', async ({
    355   page
    356 }) => {
    357   const wire = await page.evaluate(() =>
    358     window.hcp097Fixture.oldSignedOuter(100)
    359   );
    360   await page.evaluate((w) => window.hcp097Fixture.seed(w), wire);
    361   const original = await page.evaluate(
    362     async () => (await window.hcp097Fixture.row())?.wire
    363   );
    364   await page.evaluate(() => window.hcp097Fixture.close());
    365   await page.reload();
    366   await page.addScriptTag({ content: bundle });
    367   await page.evaluate(async () => {
    368     window.hcp097Fixture = await window.hcp097.makeFixture(undefined, 0, false);
    369     window.hcp097Fixture.capture();
    370     window.hcp097Fixture.start();
    371   });
    372   await expect
    373     .poll(() => page.evaluate(() => window.hcp097Fixture.snapshot()?.backfill))
    374     .toBe('complete');
    375   requests = [];
    376   frames[0] = [event(wire)];
    377   await page.evaluate(async () => {
    378     window.hcp097Fixture.captureOlder();
    379     await window.hcp097Fixture.loadOlder();
    380   });
    381   expect((requests[0].frame[2] as { until: number }).until).toBe(100);
    382   expect(
    383     await page.evaluate(
    384       (id) => window.hcp097Fixture.stored(id),
    385       String(event(wire).id)
    386     )
    387   ).toBeDefined();
    388   expect(
    389     await page.evaluate(async () => (await window.hcp097Fixture.row())?.wire)
    390   ).toBe(original);
    391   expect(
    392     await page.evaluate(
    393       () => window.hcp097Fixture.olderSnapshot()?.historyComplete
    394     )
    395   ).toBe(false);
    396 });
    397 
    398 test('budget pause requires a new explicit review and starts a fresh bounded action without automatic retry', async ({
    399   page
    400 }) => {
    401   const wire = await page.evaluate(() => window.hcp097Fixture.outer);
    402   frames[0] = Array.from({ length: 501 }, () => event(wire));
    403   ignoreLimit = true;
    404   await page.evaluate(async () => {
    405     window.hcp097Fixture.captureOlder();
    406     await window.hcp097Fixture.loadOlder();
    407   });
    408   expect(requests).toHaveLength(1);
    409   expect(
    410     await page.evaluate(() => window.hcp097Fixture.olderSnapshot()?.reason)
    411   ).toBe('budget');
    412   expect(
    413     await page.evaluate(() => window.hcp097Fixture.loadOlder('unreviewed'))
    414   ).toBe(false);
    415   expect(requests).toHaveLength(1);
    416   frames = [[], []];
    417   ignoreLimit = false;
    418   expect(await page.evaluate(() => window.hcp097Fixture.loadOlder())).toBe(
    419     true
    420   );
    421   expect(requests).toHaveLength(3);
    422   expect(
    423     await page.evaluate(() => window.hcp097Fixture.olderSnapshot())
    424   ).toMatchObject({
    425     state: 'older_available',
    426     reason: null,
    427     historyComplete: false
    428   });
    429 });
    430 
    431 test('CR005500 retained-capacity duplicate deliveries keep bounded budget facts', async ({
    432   page
    433 }) => {
    434   const capacity = await page.evaluate(() =>
    435     window.hcp097Fixture.seedCapacity()
    436   );
    437   expect(capacity.count).toBe(2000);
    438   expect(capacity.bytes).toBeLessThanOrEqual(50331648);
    439   const wire = await page.evaluate(() => window.hcp097Fixture.outer);
    440   frames[0] = Array.from({ length: 501 }, () => event(wire));
    441   ignoreLimit = true;
    442   ignoreWindow = true;
    443   const started = performance.now();
    444   await page.evaluate(async () => {
    445     window.hcp097Fixture.captureOlder();
    446     await window.hcp097Fixture.loadOlder();
    447   });
    448   const snapshot = await page.evaluate(() =>
    449     window.hcp097Fixture.olderSnapshot()
    450   );
    451   expect(requests).toHaveLength(1);
    452   expect(snapshot).toMatchObject({
    453     state: 'needs_action',
    454     reason: 'budget',
    455     historyComplete: false
    456   });
    457   console.log(
    458     JSON.stringify({
    459       fixture: 'CR005_TEST_ONLY_500_DELIVERIES_AT_2000_CAPACITY',
    460       capacity,
    461       wireDeliveriesSent: 501,
    462       logicalActionCap: 500,
    463       milliseconds: performance.now() - started,
    464       snapshot,
    465       qualification:
    466         'Actual source controller/IDB; hostile controlled relay ignores window/limit to charge500 before dedup; no access/operator/fullQ'
    467     })
    468   );
    469 });
    470 test('CR005 stop during capacity-scale queued history fences remaining writes and owns teardown', async ({
    471   page
    472 }) => {
    473   await page.evaluate(() =>
    474     window.hcp097Fixture.seedCapacity('wss://peer.example.org')
    475   );
    476   const wires = await page.evaluate(() =>
    477     window.hcp097Fixture.capacityOuterWires()
    478   );
    479   expect(wires).toHaveLength(500);
    480   frames[0] = wires.map((wire) => event(wire));
    481   await page.evaluate(() => window.hcp097Fixture.observeCapacityWrites());
    482   ignoreLimit = true;
    483   hold = 0;
    484   await page.evaluate(() => {
    485     window.hcp097Fixture.captureOlder();
    486     window.hcp097Pending = window.hcp097Fixture.loadOlder();
    487   });
    488   await expect.poll(() => requests.length).toBeGreaterThan(0);
    489   await expect
    490     .poll(() => page.evaluate(() => window.hcp097Fixture.capacityWriteCount()))
    491     .toBeGreaterThan(0);
    492   const result = await page.evaluate(async () => {
    493     const started = performance.now(),
    494       writesBeforeStop = window.hcp097Fixture.capacityWriteCount();
    495     window.hcp097Fixture.stopOlder();
    496     const immediate = window.hcp097Fixture.olderSnapshot();
    497     const completed = await window.hcp097Pending;
    498     return {
    499       immediate,
    500       completed,
    501       writesBeforeStop,
    502       writesAfterDrain: window.hcp097Fixture.capacityWriteCount(),
    503       milliseconds: performance.now() - started
    504     };
    505   });
    506   expect(result.immediate).toMatchObject({
    507     state: 'stopped',
    508     historyComplete: false
    509   });
    510   expect(result.completed).toBe(false);
    511   expect(result.writesBeforeStop).toBeGreaterThan(0);
    512   expect(result.writesBeforeStop).toBeLessThan(500);
    513   expect(result.writesAfterDrain).toBe(result.writesBeforeStop);
    514   await expect.poll(() => closes.length).toBeGreaterThan(0);
    515   console.log(
    516     JSON.stringify({
    517       fixture: 'CR005_TEST_ONLY_CAPACITY_STOP',
    518       deliveriesSent: 500,
    519       ...result,
    520       qualification:
    521         'Stop/owned controller teardown only, no rolled-back in-flight write or operator/fullQ claim'
    522     })
    523   );
    524 });
    525 
    526 test('CR005 retained old Stop cannot retire the genuine successor history registry owner', async ({
    527   page
    528 }) => {
    529   const result = await page.evaluate(() => {
    530     window.hcp097Fixture.captureOlder();
    531     return window.hcp097Fixture.oldStopAfterSuccessor();
    532   });
    533   expect(result).toMatchObject({
    534     successor: true,
    535     third: false,
    536     current: { state: 'ready', historyComplete: false }
    537   });
    538 });