app

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

outbound-envelope.spec.ts (6437B)


      1 import { test, expect, type Page } from '@playwright/test';
      2 import { build } from 'vite';
      3 import { fileURLToPath } from 'node:url';
      4 import { createStaticHarness } from '../integration/harness/static.ts';
      5 import type * as Layers from './harness/outbound-envelope.ts';
      6 declare global {
      7   interface Window {
      8     hcp077: typeof Layers;
      9     hcp077Fixture: Awaited<ReturnType<typeof Layers.makeFixture>>;
     10     hcp077Operation: Parameters<typeof Layers.stopPrivateSeal>[0];
     11     hcp077Job: ReturnType<typeof Layers.buildPrivateSeal>;
     12   }
     13 }
     14 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string;
     15 test.beforeAll(async () => {
     16   server = await createStaticHarness();
     17   const result = await build({
     18     configFile: false,
     19     logLevel: 'silent',
     20     build: {
     21       write: false,
     22       minify: false,
     23       lib: {
     24         entry: fileURLToPath(
     25           new URL('./harness/outbound-envelope.ts', import.meta.url)
     26         ),
     27         name: 'hcp077',
     28         formats: ['iife']
     29       }
     30     }
     31   });
     32   const output = Array.isArray(result) ? result[0] : result;
     33   if (!('output' in output)) throw Error('missing bundle');
     34   const chunks = output.output.filter((x) => x.type === 'chunk');
     35   expect(chunks).toHaveLength(1);
     36   bundle = chunks[0].code;
     37 });
     38 test.afterAll(async () => server.close());
     39 async function load(page: Page) {
     40   await page.goto(server.url + '/search');
     41   await page.addScriptTag({ content: bundle });
     42   await page.evaluate(async () => {
     43     window.hcp077Fixture = await window.hcp077.makeFixture();
     44   });
     45 }
     46 for (const role of ['peer', 'self'] as const)
     47   test(`genuine ${role} nested proof binds original reservation and expires on disconnect`, async ({
     48     page
     49   }) => {
     50     await load(page);
     51     const result = await page.evaluate(async (role) => {
     52       const f = window.hcp077Fixture,
     53         s = window.hcp077;
     54       try {
     55         const op = f.operation(role);
     56         if (!op) throw Error('missing operation');
     57         const sealed = await s.buildPrivateSeal(op);
     58         if (sealed.status !== 'sealed') throw Error('missing seal');
     59         const wrap = s.buildPrivateGiftwrap(
     60           sealed.seal,
     61           'reviewed_private_wrap'
     62         );
     63         if (!wrap) throw Error('missing wrap');
     64         const proof = s.verifyOutboundEnvelope(
     65           f.reserved,
     66           wrap,
     67           'reviewed_outbound_layers'
     68         );
     69         const snapshot = proof && s.verifiedOutboundSnapshot(proof);
     70         const cast = s.verifyOutboundEnvelope(
     71           f.reserved,
     72           {} as typeof wrap,
     73           'reviewed_outbound_layers'
     74         );
     75         const wrongReview = s.verifyOutboundEnvelope(f.reserved, wrap, 'wrong');
     76         const second = await f.reserveAgain();
     77         const wrongReservation = s.verifyOutboundEnvelope(
     78           second,
     79           wrap,
     80           'reviewed_outbound_layers'
     81         );
     82         f.disconnect();
     83         return {
     84           admitted: !!snapshot,
     85           role: snapshot?.role,
     86           cast: !!cast,
     87           wrongReview: !!wrongReview,
     88           differentGenuineReservation: second !== f.reserved,
     89           wrongReservation: !!wrongReservation,
     90           expired: proof && !s.verifiedOutboundSnapshot(proof)
     91         };
     92       } finally {
     93         f.close();
     94       }
     95     }, role);
     96     expect(result).toEqual({
     97       admitted: true,
     98       role,
     99       cast: false,
    100       wrongReview: false,
    101       differentGenuineReservation: true,
    102       wrongReservation: false,
    103       expired: true
    104     });
    105   });
    106 for (const mode of ['wrong_plaintext', 'declined_decrypt'] as const)
    107   test(`actual ${mode} never spends SIGN or grants a sender seal`, async ({
    108     page
    109   }) => {
    110     await load(page);
    111     const result = await page.evaluate(async (mode) => {
    112       const f = window.hcp077Fixture;
    113       try {
    114         const op = f.operation();
    115         if (!op) throw Error('missing operation');
    116         f.mode(mode);
    117         const before = f.counts(),
    118           identity = f.identityState();
    119         const result = await window.hcp077.buildPrivateSeal(op),
    120           after = f.counts();
    121         return {
    122           status: result.status,
    123           signs: after.signs - before.signs,
    124           decrypts: after.decrypts - before.decrypts,
    125           identityPreserved: f.identityState() === identity
    126         };
    127       } finally {
    128         f.close();
    129       }
    130     }, mode);
    131     expect(result.status).toBe(
    132       mode === 'wrong_plaintext' ? 'mismatch' : 'refused'
    133     );
    134     expect(result.signs).toBe(0);
    135     expect(result.decrypts).toBe(1);
    136     expect(result.identityPreserved).toBe(true);
    137   });
    138 for (const action of ['stop', 'expire'] as const)
    139   test(`${action} during actual decrypt keeps slot and owner lock until settlement without late SIGN`, async ({
    140     page
    141   }) => {
    142     await load(page);
    143     await page.evaluate(() => {
    144       const f = window.hcp077Fixture,
    145         op = f.operation();
    146       if (!op) throw Error('missing operation');
    147       f.mode('hold_decrypt');
    148       window.hcp077Operation = op;
    149       window.hcp077Job = window.hcp077.buildPrivateSeal(op);
    150     });
    151     await expect
    152       .poll(() => page.evaluate(() => window.hcp077Fixture.slot()))
    153       .toMatchObject({ state: 'active', pending: 'decrypt' });
    154     const pending = await page.evaluate(async (action) => {
    155       const f = window.hcp077Fixture,
    156         s = window.hcp077,
    157         identity = f.identityState();
    158       if (action === 'expire') s.expirePrivateSealWait(window.hcp077Operation);
    159       else s.stopPrivateSeal(window.hcp077Operation);
    160       const other = f.operation();
    161       if (!other) throw Error('missing contender');
    162       return {
    163         slot: f.slot(),
    164         busy: (await s.buildPrivateSeal(other)).status,
    165         identityPreserved: f.identityState() === identity
    166       };
    167     }, action);
    168     expect(pending.slot).toMatchObject({ state: 'active', pending: 'decrypt' });
    169     expect(pending.busy).toBe('busy');
    170     expect(pending.identityPreserved).toBe(true);
    171     const settled = await page.evaluate(async () => {
    172       const f = window.hcp077Fixture,
    173         before = f.counts();
    174       try {
    175         f.settle();
    176         const result = await window.hcp077Job;
    177         return {
    178           status: result.status,
    179           slot: f.slot(),
    180           unchanged: JSON.stringify(before) === JSON.stringify(f.counts())
    181         };
    182       } finally {
    183         f.close();
    184       }
    185     });
    186     expect(settled).toEqual({
    187       status: 'stopped',
    188       slot: { state: 'idle' },
    189       unchanged: true
    190     });
    191   });