app

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

inbox-probe-publication.test.ts (2966B)


      1 import { afterEach, expect, it, vi } from 'vitest';
      2 import type { InboxProbeAcknowledgement } from '../../src/lib/persistence/inbox-probes.ts';
      3 import type { PrivateSession } from '../../src/lib/runtime/private-session.ts';
      4 import type { PrivateStorageRepository } from '../../src/lib/persistence/private-storage.ts';
      5 import type { RelayPolicy } from '../../src/lib/config/relays.ts';
      6 import type {
      7   InboxProbePublication,
      8   InboxProbePublicationAction
      9 } from '../../src/lib/nostr/private-publisher.ts';
     10 import type {
     11   PrivatePool,
     12   InboxProbeArchiveAttemptResult
     13 } from '../../src/lib/nostr/private-pool.ts';
     14 afterEach(() => vi.unstubAllGlobals());
     15 for (const browser of [false, true]) {
     16   it(`cold ${browser ? 'browser-shaped' : 'SSR'} publication/receipt imports and forged capabilities cause no effects`, async () => {
     17     vi.resetModules();
     18     let effects = 0;
     19     const trap = () => {
     20       effects++;
     21       throw Error('forged work');
     22     };
     23     if (browser) {
     24       vi.stubGlobal('window', {
     25         get nostr() {
     26           return trap();
     27         }
     28       });
     29       vi.stubGlobal('navigator', {
     30         get locks() {
     31           return trap();
     32         }
     33       });
     34       vi.stubGlobal('WebSocket', trap);
     35     }
     36     const publisher = await import('../../src/lib/nostr/private-publisher.ts'),
     37       pool = await import('../../src/lib/nostr/private-pool.ts'),
     38       persistence = await import('../../src/lib/persistence/inbox-probes.ts');
     39     const repository = {} as PrivateStorageRepository,
     40       session = {} as PrivateSession,
     41       policy = {} as RelayPolicy;
     42     for (const fake of [
     43       null,
     44       undefined,
     45       false,
     46       0,
     47       'token',
     48       Symbol(),
     49       {},
     50       { status: 'accepted', current: () => true }
     51     ]) {
     52       expect(
     53         publisher.captureInboxProbePublicationAction(
     54           repository,
     55           session,
     56           policy,
     57           fake as InboxProbeAcknowledgement,
     58           'reviewed_inbox_probe_network'
     59         )
     60       ).toBeUndefined();
     61       expect(
     62         publisher.captureInboxProbePublication(
     63           repository,
     64           session,
     65           policy,
     66           fake as InboxProbeAcknowledgement,
     67           fake as InboxProbePublicationAction,
     68           'wss://inbox.example.org',
     69           'reviewed_inbox_probe_publication'
     70         )
     71       ).toBeUndefined();
     72       expect(
     73         await pool.publishInboxProbeArchiveAttempt(
     74           {} as PrivatePool,
     75           fake as InboxProbePublication,
     76           new AbortController().signal
     77         )
     78       ).toEqual({ status: 'stopped' });
     79       expect(
     80         pool.inboxProbeArchiveAttemptEvidence(
     81           fake as InboxProbeArchiveAttemptResult
     82         )
     83       ).toBeUndefined();
     84       expect(
     85         await persistence.commitInboxProbeArchiveAttempt(
     86           repository,
     87           fake as InboxProbeArchiveAttemptResult
     88         )
     89       ).toEqual({ status: 'invalid', disposition: 'original_protected' });
     90     }
     91     expect(effects).toBe(0);
     92   });
     93 }