app

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

unlocked-session.spec.ts (8822B)


      1 import { test, expect } 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 Fixture from './harness/unlocked-session.ts';
      6 declare global {
      7   interface Window {
      8     hcp093: typeof Fixture;
      9     hcp093Fixture: Awaited<ReturnType<typeof Fixture.makeFixture>>;
     10   }
     11 }
     12 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string;
     13 test.beforeAll(async () => {
     14   server = await createStaticHarness();
     15   const result = await build({
     16     configFile: false,
     17     logLevel: 'silent',
     18     build: {
     19       write: false,
     20       minify: false,
     21       lib: {
     22         entry: fileURLToPath(
     23           new URL('./harness/unlocked-session.ts', import.meta.url)
     24         ),
     25         name: 'hcp093',
     26         formats: ['iife']
     27       }
     28     }
     29   });
     30   const output = Array.isArray(result) ? result[0] : result;
     31   if (!('output' in output)) throw Error('bundle missing');
     32   const chunks = output.output.filter((x) => x.type === 'chunk');
     33   expect(chunks).toHaveLength(1);
     34   bundle = chunks[0].code;
     35 });
     36 test.afterAll(async () => {
     37   await server.close();
     38 });
     39 test.beforeEach(async ({ page }) => {
     40   await page.goto(server.url + '/search');
     41   await page.addScriptTag({ content: bundle });
     42   await page.evaluate(async () => {
     43     window.hcp093Fixture = await window.hcp093.makeFixture();
     44   });
     45 });
     46 test.afterEach(async ({ page }) => {
     47   if (!page.isClosed())
     48     await page.evaluate(() => window.hcp093Fixture?.close());
     49 });
     50 test('cache capture requires explicit reviewed action and performs no extension or store effect', async ({
     51   page
     52 }) => {
     53   const r = await page.evaluate(() => {
     54     const f = window.hcp093Fixture,
     55       before = f.counts();
     56     return {
     57       capture: f.captureCache('unreviewed'),
     58       messages: f.messages(),
     59       before,
     60       after: f.counts()
     61     };
     62   });
     63   expect(r.capture).toBe(false);
     64   expect(r.messages).toBeUndefined();
     65   expect(r.after).toEqual(r.before);
     66 });
     67 test('only actual admitted room enters current cache without another SDK operation', async ({
     68   page
     69 }) => {
     70   const r = await page.evaluate(async () => {
     71     const f = window.hcp093Fixture;
     72     await f.unlock();
     73     f.admit('inbound');
     74     f.captureCache();
     75     const before = f.counts();
     76     return {
     77       result: f.cache(),
     78       messages: f.messages(),
     79       snapshot: f.cacheSnapshot(),
     80       before,
     81       after: f.counts(),
     82       peer: f.peer,
     83       id: f.rumorId
     84     };
     85   });
     86   expect(r.result).toBe('added');
     87   expect(r.messages?.status).toBe('ready');
     88   expect(r.messages?.messages).toHaveLength(1);
     89   expect(r.messages?.messages[0]).toMatchObject({
     90     peer: r.peer,
     91     rumorId: r.id,
     92     role: 'inbound'
     93   });
     94   expect(r.snapshot).toMatchObject({ closed: false, count: 1 });
     95   expect(r.after).toEqual(r.before);
     96 });
     97 test('multiple genuine encrypted wraps of same immutable rumor create one logical projection', async ({
     98   page
     99 }) => {
    100   const r = await page.evaluate(async () => {
    101     const f = window.hcp093Fixture;
    102     await f.unlock();
    103     f.admit('inbound');
    104     f.captureCache();
    105     const first = f.cache();
    106     const next = await f.anotherWrap();
    107     return {
    108       first,
    109       next,
    110       second: f.cache(),
    111       messages: f.messages(),
    112       snapshot: f.cacheSnapshot(),
    113       id: f.rumorId
    114     };
    115   });
    116   expect(r.first).toBe('added');
    117   expect(r.second).toBe('duplicate');
    118   expect(r.next.rumorId).toBe(r.id);
    119   expect(r.messages?.messages).toHaveLength(1);
    120   expect(r.snapshot?.count).toBe(1);
    121 });
    122 test('mutating detached returned projection cannot alias a different sender or rumor', async ({
    123   page
    124 }) => {
    125   const r = await page.evaluate(async () => {
    126     const f = window.hcp093Fixture;
    127     await f.unlock();
    128     f.admit('inbound');
    129     f.captureCache();
    130     f.cache();
    131     const copy = f.messages();
    132     if (copy?.status === 'ready') {
    133       const row = copy.messages[0] as unknown as {
    134         peer: string;
    135         rumorId: string;
    136         content: string;
    137       };
    138       row.peer = f.other;
    139       row.rumorId = '0'.repeat(64);
    140       row.content = 'MUTATED_CALLER_COPY';
    141     }
    142     return { fresh: f.messages(), peer: f.peer, id: f.rumorId };
    143   });
    144   expect(r.fresh?.messages[0]).toMatchObject({
    145     peer: r.peer,
    146     rumorId: r.id,
    147     content: 'Actual encrypted room fixture'
    148   });
    149 });
    150 test('forged and copied room capabilities cannot populate cache', async ({
    151   page
    152 }) => {
    153   const r = await page.evaluate(async () => {
    154     const f = window.hcp093Fixture;
    155     await f.unlock();
    156     f.admit('inbound');
    157     f.captureCache();
    158     return {
    159       fake: f.forgedCache(),
    160       copy: f.copiedCache(),
    161       messages: f.messages(),
    162       snapshot: f.cacheSnapshot()
    163     };
    164   });
    165   expect(r.fake).toBe('rejected');
    166   expect(r.copy).toBe('rejected');
    167   expect(r.messages?.messages).toEqual([]);
    168   expect(r.snapshot?.count).toBe(0);
    169 });
    170 test('same owner with different actual identity generation cannot import old room proof', async ({
    171   page
    172 }) => {
    173   const r = await page.evaluate(async () => {
    174     const f = window.hcp093Fixture;
    175     await f.unlock();
    176     f.admit('inbound');
    177     f.captureCache();
    178     return { other: await f.otherGeneration(), original: f.cache() };
    179   });
    180   expect(r.other.accepted).toBe('rejected');
    181   expect(r.other.snapshot.count).toBe(0);
    182   expect(r.original).toBe('added');
    183 });
    184 test('logout clears reachable projections and rejects late original admission', async ({
    185   page
    186 }) => {
    187   const r = await page.evaluate(async () => {
    188     const f = window.hcp093Fixture;
    189     await f.unlock();
    190     f.admit('inbound');
    191     f.captureCache();
    192     f.cache();
    193     f.disconnect();
    194     return {
    195       messages: f.messages(),
    196       snapshot: f.cacheSnapshot(),
    197       raw: f.rawCacheSnapshot(),
    198       late: f.cache()
    199     };
    200   });
    201   expect(r.messages).toMatchObject({ status: 'closed', messages: [] });
    202   expect(r.snapshot).toMatchObject({ closed: true, count: 0, bytes: 0 });
    203   expect(r.late).toBe('closed');
    204   expect(r.raw).toMatchObject({ closed: true, count: 0, bytes: 0 });
    205 });
    206 test('explicit cache Stop discards preview and does not cancel or repeat provider calls', async ({
    207   page
    208 }) => {
    209   const r = await page.evaluate(async () => {
    210     const f = window.hcp093Fixture;
    211     await f.unlock();
    212     f.admit('inbound');
    213     f.captureCache();
    214     f.cache();
    215     const before = f.counts();
    216     f.closeCache();
    217     return {
    218       messages: f.messages(),
    219       snapshot: f.cacheSnapshot(),
    220       before,
    221       after: f.counts()
    222     };
    223   });
    224   expect(r.messages).toMatchObject({ status: 'closed', messages: [] });
    225   expect(r.snapshot).toMatchObject({ closed: true, count: 0, bytes: 0 });
    226   expect(r.after).toEqual(r.before);
    227 });
    228 test('original private-session close clears cache while public identity may remain connected', async ({
    229   page
    230 }) => {
    231   const r = await page.evaluate(async () => {
    232     const f = window.hcp093Fixture;
    233     await f.unlock();
    234     f.admit('inbound');
    235     f.captureCache();
    236     f.cache();
    237     f.closePrivate();
    238     return { messages: f.messages(), snapshot: f.cacheSnapshot() };
    239   });
    240   expect(r.messages).toMatchObject({ status: 'closed', messages: [] });
    241   expect(r.snapshot?.count).toBe(0);
    242 });
    243 test('actual fresh changed extension owner invalidates generation and cached preview', async ({
    244   page
    245 }) => {
    246   const r = await page.evaluate(async () => {
    247     const f = window.hcp093Fixture;
    248     await f.unlock();
    249     f.admit('inbound');
    250     f.captureCache();
    251     f.cache();
    252     await f.changedOwner();
    253     return { messages: f.messages(), snapshot: f.cacheSnapshot() };
    254   });
    255   expect(r.messages).toMatchObject({ status: 'closed', messages: [] });
    256   expect(r.snapshot?.count).toBe(0);
    257 });
    258 test('actual scoped cache quota refuses new verified projection without eviction or automatic reset', async ({
    259   page
    260 }) => {
    261   const r = await page.evaluate(async () => {
    262     const f = window.hcp093Fixture;
    263     await f.unlock();
    264     f.admit('inbound');
    265     f.captureCache();
    266     return {
    267       quota: f.quota(),
    268       result: f.cache(),
    269       raw: f.rawCacheSnapshot(),
    270       snapshot: f.cacheSnapshot(),
    271       messages: f.messages()
    272     };
    273   });
    274   expect(r.quota).toBe(2000);
    275   expect(r.result).toBe('limit');
    276   expect(r.raw).toMatchObject({ closed: false, count: 2000, bytes: 4000 });
    277   expect(r.snapshot?.count).toBe(0);
    278   expect(r.messages?.messages).toEqual([]);
    279 });
    280 test('poisoned existing serialized cache cannot substitute another peer under a verified rumor hash', async ({
    281   page
    282 }) => {
    283   const r = await page.evaluate(async () => {
    284     const f = window.hcp093Fixture;
    285     await f.unlock();
    286     f.admit('inbound');
    287     f.captureCache();
    288     f.cache();
    289     return { injected: f.corruptCachedWire(), messages: f.messages() };
    290   });
    291   expect(r.injected).toBe('accepted');
    292   expect(r.messages).toMatchObject({ status: 'conflict', messages: [] });
    293 });