app

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

inbox-probe-readback.test.ts (4752B)


      1 import { afterEach, expect, it, vi } from 'vitest';
      2 import { finalizeEvent, generateSecretKey } from 'applesauce-core/helpers';
      3 import { nip44 } from 'applesauce-core/helpers/encryption';
      4 import { inboxProbeReadbackMatches } from '../../src/lib/persistence/private-records.ts';
      5 import type { InboxProbeAcknowledgement } from '../../src/lib/persistence/inbox-probes.ts';
      6 import type { PrivateStorageRepository } from '../../src/lib/persistence/private-storage.ts';
      7 import type { PrivateSession } from '../../src/lib/runtime/private-session.ts';
      8 import type { RelayPolicy } from '../../src/lib/config/relays.ts';
      9 import type {
     10   InboxProbeReadback,
     11   InboxProbePublicationAction
     12 } from '../../src/lib/nostr/private-publisher.ts';
     13 import type {
     14   PrivatePool,
     15   InboxProbeReadbackAttemptResult
     16 } from '../../src/lib/nostr/private-pool.ts';
     17 afterEach(() => vi.unstubAllGlobals());
     18 for (const browser of [false, true])
     19   it(`cold ${browser ? 'browser-shaped' : 'SSR'} readback families reject forged inputs before any effects`, async () => {
     20     vi.resetModules();
     21     let effects = 0;
     22     const trap = () => {
     23       effects++;
     24       throw Error('forged effects');
     25     };
     26     if (browser) {
     27       vi.stubGlobal('window', {
     28         get nostr() {
     29           return trap();
     30         }
     31       });
     32       vi.stubGlobal('navigator', {
     33         get locks() {
     34           return trap();
     35         }
     36       });
     37       vi.stubGlobal('WebSocket', trap);
     38     }
     39     const p = await import('../../src/lib/nostr/private-publisher.ts'),
     40       t = await import('../../src/lib/nostr/private-pool.ts'),
     41       s = await import('../../src/lib/persistence/inbox-probes.ts');
     42     for (const fake of [
     43       null,
     44       undefined,
     45       0,
     46       false,
     47       '',
     48       Symbol(),
     49       {},
     50       { current: () => true, status: 'readback' }
     51     ]) {
     52       expect(
     53         p.captureInboxProbeReadback(
     54           {} as PrivateStorageRepository,
     55           {} as PrivateSession,
     56           {} as RelayPolicy,
     57           fake as InboxProbeAcknowledgement,
     58           fake as InboxProbePublicationAction,
     59           'wss://inbox.example.org',
     60           'reviewed_inbox_probe_readback'
     61         )
     62       ).toBeUndefined();
     63       expect(
     64         p.inboxProbeReadbackSnapshot(fake as InboxProbeReadback)
     65       ).toBeUndefined();
     66       expect(
     67         await t.readInboxProbeAttempt(
     68           {} as PrivatePool,
     69           fake as InboxProbeReadback,
     70           new AbortController().signal
     71         )
     72       ).toEqual({ status: 'stopped' });
     73       expect(
     74         t.inboxProbeReadbackAttemptEvidence(
     75           fake as InboxProbeReadbackAttemptResult
     76         )
     77       ).toBeUndefined();
     78       expect(
     79         await s.commitInboxProbeReadbackAttempt(
     80           {} as PrivateStorageRepository,
     81           fake as InboxProbeReadbackAttemptResult
     82         )
     83       ).toEqual({ status: 'invalid', disposition: 'original_protected' });
     84     }
     85     expect(effects).toBe(0);
     86   });
     87 it('pure readback comparison preserves bounded alternate serialization and unsigned metadata with independently identical signed core', () => {
     88   const owner =
     89       '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
     90     key = generateSecretKey(),
     91     conversation = nip44.v2.utils.getConversationKey(key, owner);
     92   try {
     93     const e = finalizeEvent(
     94         {
     95           kind: 1059,
     96           created_at: 1700000000,
     97           tags: [['p', owner]],
     98           content: nip44.v2.encrypt(
     99             'Structural source fixture, no nonce proof',
    100             conversation
    101           )
    102         },
    103         key
    104       ),
    105       self = { eventId: e.id, wire: JSON.stringify(e) };
    106     const alternate = {
    107       sig: e.sig,
    108       content: e.content,
    109       tags: e.tags,
    110       created_at: e.created_at,
    111       kind: e.kind,
    112       pubkey: e.pubkey,
    113       id: e.id,
    114       observed: { source: 'fixture' }
    115     };
    116     for (const wire of [
    117       self.wire,
    118       self.wire + '\n',
    119       JSON.stringify(alternate, null, 2)
    120     ])
    121       expect(inboxProbeReadbackMatches(self, owner, wire)).toBe(true);
    122     for (const wire of [
    123       null,
    124       undefined,
    125       {},
    126       JSON.stringify({ ...e, kind: 14 }),
    127       JSON.stringify({ ...e, sig: '0'.repeat(128), verified: true }),
    128       JSON.stringify({ ...e, id: 'f'.repeat(64) }),
    129       JSON.stringify({ ...e, content: e.content + 'a' }),
    130       JSON.stringify({ ...e, tags: [['p', 'f'.repeat(64)]] }),
    131       self.wire.replace('"kind":1059', '"kind":1059,"kind":1059'),
    132       self.wire.replace('"kind":1059', '"kind":1.059e3'),
    133       self.wire + ' '.repeat(32768)
    134     ])
    135       expect(inboxProbeReadbackMatches(self, owner, wire)).toBe(false);
    136     expect(inboxProbeReadbackMatches(self, 'f'.repeat(64), self.wire)).toBe(
    137       false
    138     );
    139   } finally {
    140     conversation.fill(0);
    141     key.fill(0);
    142   }
    143 });