app

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

resume-preparation.spec.ts (15073B)


      1 import { expect, test, type Page } from '@playwright/test';
      2 import { build } from 'vite';
      3 import { fileURLToPath } from 'node:url';
      4 import { createHash } from 'node:crypto';
      5 import { createStaticHarness } from '../integration/harness/static.ts';
      6 import type * as Resume from './harness/resume-preparation.ts';
      7 declare global {
      8   interface Window {
      9     hcp083: typeof Resume;
     10     hcp083Fixture: Awaited<ReturnType<typeof Resume.makeFixture>>;
     11     hcp083Token: ReturnType<Window['hcp083Fixture']['capture']>;
     12     hcp083Pending: Promise<
     13       Awaited<ReturnType<typeof Resume.resumeEncryptedPreparation>>
     14     >;
     15   }
     16 }
     17 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string;
     18 test.beforeAll(async () => {
     19   server = await createStaticHarness();
     20   const built = await build({
     21     configFile: false,
     22     logLevel: 'silent',
     23     build: {
     24       write: false,
     25       minify: false,
     26       lib: {
     27         entry: fileURLToPath(
     28           new URL('./harness/resume-preparation.ts', import.meta.url)
     29         ),
     30         name: 'hcp083',
     31         formats: ['iife']
     32       }
     33     }
     34   });
     35   const output = Array.isArray(built) ? built[0] : built;
     36   if (!('output' in output)) throw Error('missing resume bundle');
     37   const chunks = output.output.filter((entry) => entry.type === 'chunk');
     38   expect(chunks).toHaveLength(1);
     39   bundle = chunks[0].code;
     40   console.log(
     41     JSON.stringify({
     42       fixture: 'HCP083_ACTUAL_RELOAD_SDK_IDB',
     43       bundleSha256: createHash('sha256').update(bundle).digest('hex'),
     44       moduleIds: Object.keys(chunks[0].modules),
     45       qualification:
     46         'Source-only disposable provider, actual SDK/IDB/WebLocks; no installed extension/relay/client Q'
     47     })
     48   );
     49 });
     50 test.afterAll(async () => server.close());
     51 async function load(page: Page, initialise = true) {
     52   await page.goto(server.url + '/search');
     53   await page.addScriptTag({ content: bundle });
     54   await page.evaluate(async (initialise) => {
     55     window.hcp083Fixture = await window.hcp083.makeFixture(initialise);
     56   }, initialise);
     57 }
     58 test('actual reload loses presentation capabilities and explicitly recovers the same rumor and missing peer without rebuilding self', async ({
     59   page
     60 }) => {
     61   let sockets = 0;
     62   page.on('websocket', () => sockets++);
     63   await load(page);
     64   const before = await page.evaluate(async () => {
     65     const row = await window.hcp083Fixture.row();
     66     if (!row) throw Error('missing self');
     67     return row.record;
     68   });
     69   await page.reload();
     70   await page.addScriptTag({ content: bundle });
     71   await page.evaluate(async () => {
     72     window.hcp083Fixture = await window.hcp083.makeFixture(false);
     73   });
     74   const result = await page.evaluate(async () => {
     75     const f = window.hcp083Fixture,
     76       s = window.hcp083;
     77     try {
     78       const token = f.capture(),
     79         initial = f.counts(),
     80         raw = await f.raw();
     81       const recovered = await s.resumeEncryptedPreparation(
     82           token,
     83           undefined,
     84           'reviewed_private_resume'
     85         ),
     86         rumor = s.resumeRecoveredRumor(token),
     87         memory = rumor && s.rumorPlanSnapshot(rumor),
     88         afterRecovery = f.counts();
     89       const pair = await s.resumeEncryptedPreparation(
     90           token,
     91           f.context,
     92           'reviewed_private_resume'
     93         ),
     94         row = await f.row(),
     95         peer =
     96           row?.record.peerArtifact &&
     97           f.decryptPeer(row.record.peerArtifact.wire);
     98       return {
     99         recovered: recovered.status,
    100         pair: pair.status,
    101         memory,
    102         record: row?.record,
    103         peer,
    104         noSelfCrypto:
    105           afterRecovery.encrypts === initial.encrypts &&
    106           afterRecovery.signs === initial.signs,
    107         privateOnly:
    108           !raw.includes(f.marker) && !(await f.raw()).includes(f.marker)
    109       };
    110     } finally {
    111       f.close();
    112     }
    113   });
    114   expect(result.recovered).toBe('recovered');
    115   expect(result.pair).toBe('prepared');
    116   expect(result.noSelfCrypto).toBe(true);
    117   expect(result.privateOnly).toBe(true);
    118   expect(result.record?.self).toEqual(before.self);
    119   expect(result.record?.rumorHash).toBe(before.rumorHash);
    120   expect(result.record?.createdAt).toBe(before.createdAt);
    121   expect(result.memory?.id).toBe(before.rumorHash);
    122   expect(result.memory?.wire).toContain(
    123     'HCP083_PRIVATE_RECOVERY_MEMORY_ONLY_SENTINEL'
    124   );
    125   expect(result.peer?.id).toBe(before.rumorHash);
    126   expect(result.peer?.created_at).toBe(before.createdAt);
    127   const originalRumor = JSON.parse(result.memory!.wire) as { content: string };
    128   expect(result.peer?.content).toBe(originalRumor.content);
    129   expect(result.peer?.content).toContain('Enquiry about "Carrots"');
    130   expect(result.peer?.content).toContain(
    131     'HCP083_PRIVATE_RECOVERY_MEMORY_ONLY_SENTINEL'
    132   );
    133   expect(sockets).toBe(0);
    134 });
    135 test('capture and unreviewed Resume spend no prompts or private effects', async ({
    136   page
    137 }) => {
    138   await load(page);
    139   const result = await page.evaluate(async () => {
    140     const f = window.hcp083Fixture,
    141       s = window.hcp083;
    142     try {
    143       const before = f.counts(),
    144         raw = await f.raw(),
    145         token = f.capture(),
    146         result = await s.resumeEncryptedPreparation(token, f.context, 'wrong');
    147       return {
    148         status: result.status,
    149         inert: JSON.stringify(before) === JSON.stringify(f.counts()),
    150         unchanged: raw === (await f.raw()),
    151         noRumor: !s.resumeRecoveredRumor(token)
    152       };
    153     } finally {
    154       f.close();
    155     }
    156   });
    157   expect(result).toEqual({
    158     status: 'invalid',
    159     inert: true,
    160     unchanged: true,
    161     noRumor: true
    162   });
    163 });
    164 test('erased evidence explicitly discloses loss without a new reservation or SDK prompt', async ({
    165   page
    166 }) => {
    167   await load(page);
    168   const result = await page.evaluate(async () => {
    169     const f = window.hcp083Fixture,
    170       s = window.hcp083;
    171     try {
    172       await f.write('erase');
    173       const before = f.counts(),
    174         token = f.capture(),
    175         r = await s.resumeEncryptedPreparation(
    176           token,
    177           f.context,
    178           'reviewed_private_resume'
    179         );
    180       return {
    181         status: r.status,
    182         copy: s.resumePreparationSnapshot(token)?.copy,
    183         inert: JSON.stringify(before) === JSON.stringify(f.counts()),
    184         raw: await f.raw(),
    185         noRumor: !s.resumeRecoveredRumor(token)
    186       };
    187     } finally {
    188       f.close();
    189     }
    190   });
    191   expect(result.status).toBe('lost_evidence');
    192   expect(result.copy).toContain('lost');
    193   expect(result.inert).toBe(true);
    194   expect(result.raw).toBe('[]');
    195   expect(result.noRumor).toBe(true);
    196 });
    197 test('corrupt stored recovery stays intact and blocks decryption and silent reset', async ({
    198   page
    199 }) => {
    200   await load(page);
    201   const result = await page.evaluate(async () => {
    202     const f = window.hcp083Fixture,
    203       s = window.hcp083;
    204     try {
    205       await f.write('corrupt');
    206       const raw = await f.raw(),
    207         before = f.counts(),
    208         r = await s.resumeEncryptedPreparation(
    209           f.capture(),
    210           f.context,
    211           'reviewed_private_resume'
    212         );
    213       return {
    214         status: r.status,
    215         unchanged: raw === (await f.raw()),
    216         inert: JSON.stringify(before) === JSON.stringify(f.counts())
    217       };
    218     } finally {
    219       f.close();
    220     }
    221   });
    222   expect(result).toEqual({
    223     status: 'corrupt_record',
    224     unchanged: true,
    225     inert: true
    226   });
    227 });
    228 test('fresh changed extension key blocks before the first recovery decrypt', async ({
    229   page
    230 }) => {
    231   await load(page);
    232   const result = await page.evaluate(async () => {
    233     const f = window.hcp083Fixture,
    234       s = window.hcp083;
    235     try {
    236       const raw = await f.raw(),
    237         before = f.counts(),
    238         token = f.capture();
    239       f.mode('changed_key');
    240       const r = await s.resumeEncryptedPreparation(
    241         token,
    242         f.context,
    243         'reviewed_private_resume'
    244       );
    245       return {
    246         status: r.status,
    247         unchanged: raw === (await f.raw()),
    248         noDecrypt: f.counts().decrypts === before.decrypts,
    249         noRumor: !s.resumeRecoveredRumor(token)
    250       };
    251     } finally {
    252       f.close();
    253     }
    254   });
    255   expect(result).toEqual({
    256     status: 'stopped',
    257     unchanged: true,
    258     noDecrypt: true,
    259     noRumor: true
    260   });
    261 });
    262 test('late decryption after Disconnect cannot restore memory or continue peer work', async ({
    263   page
    264 }) => {
    265   await load(page);
    266   await page.evaluate(() => {
    267     const f = window.hcp083Fixture;
    268     f.mode('hold_decrypt');
    269     window.hcp083Token = f.capture();
    270     window.hcp083Pending = window.hcp083.resumeEncryptedPreparation(
    271       window.hcp083Token,
    272       f.context,
    273       'reviewed_private_resume'
    274     );
    275   });
    276   await expect
    277     .poll(() => page.evaluate(() => window.hcp083Fixture.pending()))
    278     .toBe(true);
    279   const result = await page.evaluate(async () => {
    280     const f = window.hcp083Fixture,
    281       s = window.hcp083;
    282     try {
    283       const raw = await f.raw(),
    284         before = f.counts();
    285       f.disconnect();
    286       f.mode('normal');
    287       f.settle();
    288       const r = await window.hcp083Pending;
    289       return {
    290         status: r.status,
    291         unchanged: raw === (await f.raw()),
    292         noPeer:
    293           f.counts().encrypts === before.encrypts &&
    294           f.counts().signs === before.signs,
    295         noRumor: !s.resumeRecoveredRumor(window.hcp083Token)
    296       };
    297     } finally {
    298       f.close();
    299     }
    300   });
    301   expect(result).toEqual({
    302     status: 'stopped',
    303     unchanged: true,
    304     noPeer: true,
    305     noRumor: true
    306   });
    307 });
    308 test('stopped pending decryption occupies the shared owner until actual settlement', async ({
    309   page
    310 }) => {
    311   await load(page);
    312   await page.evaluate(() => {
    313     const f = window.hcp083Fixture;
    314     f.mode('hold_decrypt');
    315     window.hcp083Token = f.capture();
    316     window.hcp083Pending = window.hcp083.resumeEncryptedPreparation(
    317       window.hcp083Token,
    318       undefined,
    319       'reviewed_private_resume'
    320     );
    321   });
    322   await expect
    323     .poll(() => page.evaluate(() => window.hcp083Fixture.pending()))
    324     .toBe(true);
    325   const result = await page.evaluate(async () => {
    326     const f = window.hcp083Fixture,
    327       s = window.hcp083;
    328     try {
    329       s.stopResumePreparation(window.hcp083Token);
    330       const before = f.counts(),
    331         other = f.capture(),
    332         r = await s.resumeEncryptedPreparation(
    333           other,
    334           undefined,
    335           'reviewed_private_resume'
    336         );
    337       const noQueue = JSON.stringify(before) === JSON.stringify(f.counts());
    338       f.mode('normal');
    339       f.settle();
    340       return {
    341         second: r.status,
    342         first: (await window.hcp083Pending).status,
    343         noQueue,
    344         noRumor: !s.resumeRecoveredRumor(window.hcp083Token)
    345       };
    346     } finally {
    347       f.close();
    348     }
    349   });
    350   expect(result).toEqual({
    351     second: 'busy',
    352     first: 'stopped',
    353     noQueue: true,
    354     noRumor: true
    355   });
    356 });
    357 test('decryption refusal preserves self ciphertext and never falls back to encryption or signing', async ({
    358   page
    359 }) => {
    360   await load(page);
    361   const result = await page.evaluate(async () => {
    362     const f = window.hcp083Fixture,
    363       s = window.hcp083;
    364     try {
    365       const raw = await f.raw(),
    366         before = f.counts(),
    367         token = f.capture();
    368       f.mode('declined_decrypt');
    369       const r = await s.resumeEncryptedPreparation(
    370         token,
    371         f.context,
    372         'reviewed_private_resume'
    373       );
    374       return {
    375         status: r.status,
    376         unchanged: raw === (await f.raw()),
    377         noFallback:
    378           f.counts().encrypts === before.encrypts &&
    379           f.counts().signs === before.signs,
    380         noRumor: !s.resumeRecoveredRumor(token)
    381       };
    382     } finally {
    383       f.close();
    384     }
    385   });
    386   expect(result).toEqual({
    387     status: 'refused',
    388     unchanged: true,
    389     noFallback: true,
    390     noRumor: true
    391   });
    392 });
    393 test('substituted decrypted plaintext fails authenticated rumor recovery', async ({
    394   page
    395 }) => {
    396   await load(page);
    397   const result = await page.evaluate(async () => {
    398     const f = window.hcp083Fixture,
    399       s = window.hcp083;
    400     try {
    401       const raw = await f.raw(),
    402         token = f.capture();
    403       f.mode('wrong_plaintext');
    404       const r = await s.resumeEncryptedPreparation(
    405         token,
    406         f.context,
    407         'reviewed_private_resume'
    408       );
    409       return {
    410         status: r.status,
    411         unchanged: raw === (await f.raw()),
    412         noRumor: !s.resumeRecoveredRumor(token)
    413       };
    414     } finally {
    415       f.close();
    416     }
    417   });
    418   expect(result).toEqual({
    419     status: 'mismatch',
    420     unchanged: true,
    421     noRumor: true
    422   });
    423 });
    424 test('completed pair recovery and repeated Resume retain both original artifacts without peer regeneration', async ({
    425   page
    426 }) => {
    427   await load(page);
    428   const result = await page.evaluate(async () => {
    429     const f = window.hcp083Fixture,
    430       s = window.hcp083;
    431     try {
    432       const first = f.capture();
    433       await s.resumeEncryptedPreparation(
    434         first,
    435         f.context,
    436         'reviewed_private_resume'
    437       );
    438       const raw = await f.raw(),
    439         before = f.counts(),
    440         repeat = await s.resumeEncryptedPreparation(
    441           first,
    442           f.context,
    443           'reviewed_private_resume'
    444         ),
    445         inert = JSON.stringify(before) === JSON.stringify(f.counts());
    446       s.stopResumePreparation(first);
    447       const fresh = f.capture(),
    448         nextBefore = f.counts(),
    449         r = await s.resumeEncryptedPreparation(
    450           fresh,
    451           f.context,
    452           'reviewed_private_resume'
    453         );
    454       return {
    455         repeat: repeat.status,
    456         inert,
    457         fresh: r.status,
    458         noPeer:
    459           f.counts().encrypts === nextBefore.encrypts &&
    460           f.counts().signs === nextBefore.signs,
    461         unchanged: raw === (await f.raw())
    462       };
    463     } finally {
    464       f.close();
    465     }
    466   });
    467   expect(result).toEqual({
    468     repeat: 'prepared',
    469     inert: true,
    470     fresh: 'already_prepared',
    471     noPeer: true,
    472     unchanged: true
    473   });
    474 });
    475 test('unacknowledged full-wire mutation while decrypting is refused and retained', async ({
    476   page
    477 }) => {
    478   await load(page);
    479   await page.evaluate(() => {
    480     const f = window.hcp083Fixture;
    481     f.mode('hold_decrypt');
    482     window.hcp083Token = f.capture();
    483     window.hcp083Pending = window.hcp083.resumeEncryptedPreparation(
    484       window.hcp083Token,
    485       f.context,
    486       'reviewed_private_resume'
    487     );
    488   });
    489   await expect
    490     .poll(() => page.evaluate(() => window.hcp083Fixture.pending()))
    491     .toBe(true);
    492   const result = await page.evaluate(async () => {
    493     const f = window.hcp083Fixture,
    494       s = window.hcp083;
    495     try {
    496       await f.write('revision');
    497       const raw = await f.raw(),
    498         before = f.counts();
    499       f.mode('normal');
    500       f.settle();
    501       const r = await window.hcp083Pending;
    502       return {
    503         status: r.status,
    504         unchanged: raw === (await f.raw()),
    505         noPeer:
    506           f.counts().encrypts === before.encrypts &&
    507           f.counts().signs === before.signs,
    508         noRumor: !s.resumeRecoveredRumor(window.hcp083Token)
    509       };
    510     } finally {
    511       f.close();
    512     }
    513   });
    514   expect(result).toEqual({
    515     status: 'conflict',
    516     unchanged: true,
    517     noPeer: true,
    518     noRumor: true
    519   });
    520 });