app

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

message-metadata.spec.ts (12214B)


      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/message-metadata.ts';
      6 declare global {
      7   interface Window {
      8     hcp094: typeof Fixture;
      9     hcp094Fixture: 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/message-metadata.ts', import.meta.url)
     24         ),
     25         name: 'hcp094',
     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.hcp094Fixture = await window.hcp094.makeFixture();
     44   });
     45 });
     46 test.afterEach(async ({ page }) => {
     47   if (!page.isClosed())
     48     await page.evaluate(() => window.hcp094Fixture?.close());
     49 });
     50 async function ready(page: import('@playwright/test').Page, cache = true) {
     51   return await page.evaluate(async (cache) => {
     52     const f = window.hcp094Fixture;
     53     await f.unlock();
     54     f.admit('inbound');
     55     f.captureCache();
     56     if (cache) f.cache();
     57     return await f.captureMetadata();
     58   }, cache);
     59 }
     60 test('metadata requires reviewed owner-generation scope without signing or persistence on capture', async ({
     61   page
     62 }) => {
     63   const r = await page.evaluate(async () => {
     64     const f = window.hcp094Fixture;
     65     f.captureCache();
     66     const before = f.counts();
     67     const rejected = await f.captureMetadata('unreviewed');
     68     return { rejected, before, after: f.counts() };
     69   });
     70   expect(r.rejected).toBe(false);
     71   expect(r.after).toEqual(r.before);
     72 });
     73 test('unlock and fetch never mark a verified old rumor read', async ({
     74   page
     75 }) => {
     76   expect(await ready(page)).toBe(true);
     77   const r = await page.evaluate(async () => {
     78     const f = window.hcp094Fixture;
     79     return { unread: await f.unread(), raw: await f.rawMetadata() };
     80   });
     81   expect(r.unread).toMatchObject({ status: 'ready', count: 1 });
     82   expect(r.raw.pairs).toEqual([]);
     83   const rows = r.raw.received as { wire: string }[];
     84   expect(
     85     rows.map((x) => (JSON.parse(x.wire) as { read: unknown }).read)
     86   ).toEqual([null]);
     87 });
     88 test('two actual encrypted wraps of one rumor retain exactly one local unread', async ({
     89   page
     90 }) => {
     91   await ready(page);
     92   const r = await page.evaluate(async () => {
     93     const f = window.hcp094Fixture;
     94     await f.anotherWrap();
     95     f.cache();
     96     return { unread: await f.unread(), raw: await f.rawMetadata() };
     97   });
     98   expect(r.unread).toMatchObject({ status: 'ready', count: 1 });
     99   expect(r.raw.received).toHaveLength(2);
    100 });
    101 test('only explicit displayed action persists minimum read and opaque pair fields without more SDK calls', async ({
    102   page
    103 }) => {
    104   await ready(page);
    105   const r = await page.evaluate(async () => {
    106     const f = window.hcp094Fixture;
    107     const before = f.counts();
    108     return {
    109       save: await f.display(),
    110       unread: await f.unread(),
    111       raw: await f.rawMetadata(),
    112       before,
    113       after: f.counts(),
    114       id: f.rumorId,
    115       owner: f.owner,
    116       peer: f.peer
    117     };
    118   });
    119   expect(r.save.status).toBe('saved');
    120   expect(r.unread).toMatchObject({ status: 'ready', count: 0 });
    121   expect(r.after).toEqual(r.before);
    122   const received = JSON.parse(
    123     (r.raw.received as { wire: string }[])[0].wire
    124   ) as { read: { rumorHash: string; atMilliseconds: number } };
    125   expect(received.read.rumorHash).toBe(r.id);
    126   expect(Object.keys(received.read).sort()).toEqual([
    127     'atMilliseconds',
    128     'rumorHash'
    129   ]);
    130   expect(received.read.atMilliseconds).toBeGreaterThan(1700000000000);
    131   const pair = JSON.parse((r.raw.pairs as { wire: string }[])[0].wire) as {
    132     id: string;
    133     owner: string;
    134     peer: string;
    135     family: string;
    136     schema: number;
    137   };
    138   expect(Object.keys(pair).sort()).toEqual([
    139     'family',
    140     'id',
    141     'owner',
    142     'peer',
    143     'schema'
    144   ]);
    145   expect(pair).toMatchObject({
    146     owner: r.owner,
    147     peer: r.peer,
    148     family: 'conversation_handle',
    149     schema: 1
    150   });
    151   expect(pair.id).toMatch(/^[0-9a-f-]{36}$/);
    152   expect(JSON.stringify(r.raw)).not.toContain('Actual encrypted room fixture');
    153 });
    154 test('read fact suppresses later different wrap of same actual rumor', async ({
    155   page
    156 }) => {
    157   await ready(page);
    158   const r = await page.evaluate(async () => {
    159     const f = window.hcp094Fixture;
    160     await f.display();
    161     await f.anotherWrap();
    162     f.cache();
    163     return {
    164       unread: await f.unread(),
    165       save: await f.display(),
    166       raw: await f.rawMetadata()
    167     };
    168   });
    169   expect(r.unread).toMatchObject({ status: 'ready', count: 0 });
    170   expect(r.save.status).toBe('saved');
    171   expect(r.raw.pairs).toHaveLength(1);
    172 });
    173 test('repeating explicit display reuses exact mapping and immutable first read time', async ({
    174   page
    175 }) => {
    176   await ready(page);
    177   const r = await page.evaluate(async () => {
    178     const f = window.hcp094Fixture;
    179     const first = await f.display(),
    180       before = await f.rawMetadata(),
    181       second = await f.display(),
    182       after = await f.rawMetadata();
    183     return { first, second, before, after };
    184   });
    185   expect(r.first.status).toBe('saved');
    186   expect(r.second.status).toBe('existing');
    187   expect(r.after).toEqual(r.before);
    188 });
    189 test('unreviewed display refuses read and mapping writes', async ({ page }) => {
    190   await ready(page);
    191   const r = await page.evaluate(async () => {
    192     const f = window.hcp094Fixture;
    193     return {
    194       save: await f.display('fetched_message'),
    195       unread: await f.unread(),
    196       raw: await f.rawMetadata()
    197     };
    198   });
    199   expect(r.save.status).toBe('invalid');
    200   expect(r.unread).toMatchObject({ count: 1 });
    201   expect(r.raw.pairs).toEqual([]);
    202 });
    203 test('forged and copied room custody cannot persist a local read', async ({
    204   page
    205 }) => {
    206   await ready(page);
    207   const r = await page.evaluate(async () => {
    208     const f = window.hcp094Fixture;
    209     return {
    210       fake: await f.forgedDisplay(),
    211       copy: await f.copiedDisplay(),
    212       raw: await f.rawMetadata()
    213     };
    214   });
    215   expect(r.fake.status).toBe('invalid');
    216   expect(r.copy.status).toBe('invalid');
    217   expect(r.raw.pairs).toEqual([]);
    218 });
    219 test('authenticated room outside the accepted unlocked projection cannot mark read', async ({
    220   page
    221 }) => {
    222   await ready(page, false);
    223   const r = await page.evaluate(() => window.hcp094Fixture.display());
    224   expect(r.status).toBe('invalid');
    225 });
    226 test('other actual owner has no prior read or mapping state', async ({
    227   page
    228 }) => {
    229   await ready(page);
    230   const r = await page.evaluate(async () => {
    231     const f = window.hcp094Fixture;
    232     await f.display();
    233     return await f.otherOwnerMetadata();
    234   });
    235   expect(r).toMatchObject({ status: 'ready', readRumors: [], pairs: [] });
    236 });
    237 test('same owner different actual generation cannot capture original cache metadata authority', async ({
    238   page
    239 }) => {
    240   await ready(page);
    241   expect(
    242     await page.evaluate(() => window.hcp094Fixture.wrongGenerationMetadata())
    243   ).toBe(false);
    244 });
    245 test('actual IndexedDB put abort grants no read or mapping credit and preserves unread', async ({
    246   page
    247 }) => {
    248   await ready(page);
    249   const r = await page.evaluate(async () => {
    250     const f = window.hcp094Fixture;
    251     const restore = f.abortNextWrite();
    252     let save;
    253     try {
    254       save = await f.display();
    255     } finally {
    256       restore();
    257     }
    258     return { save, unread: await f.unread(), raw: await f.rawMetadata() };
    259   });
    260   expect(r.save.status).toBe('aborted');
    261   expect(r.unread).toMatchObject({ count: 1 });
    262   expect(r.raw.pairs).toEqual([]);
    263 });
    264 test('logout while owner lock is unavailable admits no late metadata write', async ({
    265   page
    266 }) => {
    267   await ready(page);
    268   const r = await page.evaluate(async () => {
    269     const f = window.hcp094Fixture;
    270     return { save: await f.queuedLogout(), raw: await f.rawMetadata() };
    271   });
    272   expect(['busy', 'stopped']).toContain(r.save.status);
    273   expect(r.raw.pairs).toEqual([]);
    274 });
    275 test('unknown pair field fails closed without clearing corrupt or ciphertext evidence', async ({
    276   page
    277 }) => {
    278   await ready(page);
    279   const r = await page.evaluate(async () => {
    280     const f = window.hcp094Fixture;
    281     await f.corruptPair();
    282     const before = await f.rawMetadata();
    283     return {
    284       save: await f.display(),
    285       snapshot: await f.metadataSnapshot(),
    286       before,
    287       after: await f.rawMetadata()
    288     };
    289   });
    290   expect(r.save.status).toBe('corrupt_record');
    291   expect(r.snapshot?.status).toBe('corrupt_record');
    292   expect(r.after).toEqual(r.before);
    293 });
    294 test('disclosure accurately names local peer and use-time exposure without encrypted-device or remote-read claims', async ({
    295   page
    296 }) => {
    297   const text = await page.evaluate(() => window.hcp094Fixture.disclosure());
    298   expect(text).toContain('peers');
    299   expect(text).toContain('use times');
    300   expect(text).toContain('local');
    301   expect(text).toContain('not full-device encryption');
    302   expect(text).toContain('No remote Seen/Read');
    303 });
    304 
    305 test('actual reload preserves local read across a newly encrypted wrap of the same verified rumor', async ({
    306   page
    307 }) => {
    308   await ready(page);
    309   expect(
    310     (await page.evaluate(() => window.hcp094Fixture.display())).status
    311   ).toBe('saved');
    312   await page.evaluate(() => window.hcp094Fixture.close());
    313   await page.reload();
    314   await page.addScriptTag({ content: bundle });
    315   await page.evaluate(async () => {
    316     window.hcp094Fixture = await window.hcp094.makeFixture('inbound', false);
    317   });
    318   await ready(page);
    319   expect(
    320     await page.evaluate(() => window.hcp094Fixture.unread())
    321   ).toMatchObject({ status: 'ready', count: 0 });
    322 });
    323 test('a genuinely decrypted delayed old rumor remains new locally after another rumor was read', async ({
    324   page
    325 }) => {
    326   await ready(page);
    327   const r = await page.evaluate(async () => {
    328     const f = window.hcp094Fixture;
    329     await f.display();
    330     const next = await f.anotherWrap(true),
    331       admit = f.cache();
    332     return { next, admit, unread: await f.unread(), original: f.rumorId };
    333   });
    334   expect(r.admit).toBe('added');
    335   expect(r.next.rumorId).not.toBe(r.original);
    336   expect(r.unread).toMatchObject({
    337     status: 'ready',
    338     count: 1,
    339     rumorIds: [r.next.rumorId]
    340   });
    341 });
    342 test('actual postcommit readback failure reports uncertainty rather than saved credit', async ({
    343   page
    344 }) => {
    345   await ready(page);
    346   const r = await page.evaluate(async () => {
    347     const f = window.hcp094Fixture,
    348       restore = f.failNextReadback();
    349     let save;
    350     try {
    351       save = await f.display();
    352     } finally {
    353       restore();
    354     }
    355     return { save, raw: await f.rawMetadata() };
    356   });
    357   expect(r.save).toMatchObject({
    358     status: 'unknown_completion',
    359     phase: 'readback',
    360     reason: 'aborted'
    361   });
    362   expect(
    363     (
    364       JSON.parse((r.raw.received as { wire: string }[])[0].wire) as {
    365         read: unknown;
    366       }
    367     ).read
    368   ).not.toBeNull();
    369   expect(r.raw.pairs).toHaveLength(1);
    370 });
    371 test('actual rival metadata transaction defeats stale whole-wire CAS without overwriting its evidence', async ({
    372   page
    373 }) => {
    374   await ready(page);
    375   const r = await page.evaluate(async () => {
    376     const f = window.hcp094Fixture,
    377       restore = await f.raceNextWrite();
    378     let save;
    379     try {
    380       save = await f.display();
    381     } finally {
    382       restore();
    383     }
    384     return { save, raw: await f.rawMetadata() };
    385   });
    386   expect(r.save.status).toBe('conflict');
    387   const row = JSON.parse((r.raw.received as { wire: string }[])[0].wire) as {
    388     read: unknown;
    389     sources: string[];
    390   };
    391   expect(row.read).toBeNull();
    392   expect(row.sources).toContain('wss://race.example.org');
    393   expect(r.raw.pairs).toEqual([]);
    394 });