app

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

self-recovery.test.ts (3036B)


      1 import { describe, expect, it } from 'vitest';
      2 import type { IdentitySession } from '../../src/lib/runtime/identity-session.ts';
      3 import type { ReservedSendIdentity } from '../../src/lib/messaging/send-identity.ts';
      4 import type { VerifiedOutboundEnvelope } from '../../src/lib/nostr/verify-outbound-envelope.ts';
      5 import type { PrivateStorageRepository } from '../../src/lib/persistence/private-storage.ts';
      6 import {
      7   commitSelfRecovery,
      8   selfRecoveryAcknowledgementSnapshot,
      9   type SelfRecoveryAcknowledgement
     10 } from '../../src/lib/persistence/private-sends.ts';
     11 import {
     12   captureSelfRecoveryPreparation,
     13   prepareSelfRecovery,
     14   selfRecoveryPreparationSnapshot,
     15   preparedSelfRecovery,
     16   selfRecoveryPeerPreparation,
     17   stopSelfRecoveryPreparation,
     18   type SelfRecoveryPreparation
     19 } from '../../src/lib/messaging/prepare-send.ts';
     20 
     21 describe('self recovery is an explicit current-owner local acknowledgement', () => {
     22   it('SSR and forged namespaces/identities cannot start crypto or persistence', () => {
     23     expect(
     24       captureSelfRecoveryPreparation(
     25         {} as PrivateStorageRepository,
     26         {} as IdentitySession,
     27         {} as ReservedSendIdentity,
     28         'reviewed_self_recovery'
     29       )
     30     ).toBeUndefined();
     31   });
     32   it('forged preparation and receipt objects grant no acknowledgement or peer permission', async () => {
     33     const preparation = {} as SelfRecoveryPreparation,
     34       receipt = {} as SelfRecoveryAcknowledgement;
     35     expect(
     36       await prepareSelfRecovery(preparation, 'reviewed_self_recovery')
     37     ).toEqual({ status: 'invalid' });
     38     expect(selfRecoveryPreparationSnapshot(preparation)).toBeUndefined();
     39     expect(preparedSelfRecovery(preparation)).toBeUndefined();
     40     expect(
     41       await selfRecoveryPeerPreparation(preparation, receipt)
     42     ).toBeUndefined();
     43     expect(selfRecoveryAcknowledgementSnapshot(receipt)).toBeUndefined();
     44     expect(() => stopSelfRecoveryPreparation(preparation)).not.toThrow();
     45   });
     46   it('detached factory data cannot produce a durable receipt', async () => {
     47     expect(
     48       await commitSelfRecovery(
     49         {} as PrivateStorageRepository,
     50         {} as IdentitySession,
     51         {} as ReservedSendIdentity,
     52         {} as VerifiedOutboundEnvelope,
     53         'reviewed_self_commit'
     54       )
     55     ).toEqual({ status: 'invalid' });
     56   });
     57   it('unreviewed values are rejected without coercion or provider callbacks', async () => {
     58     let conversions = 0;
     59     const review = {
     60       toString() {
     61         conversions++;
     62         return 'reviewed_self_commit';
     63       }
     64     };
     65     expect(
     66       await commitSelfRecovery(
     67         {} as PrivateStorageRepository,
     68         {} as IdentitySession,
     69         {} as ReservedSendIdentity,
     70         {} as VerifiedOutboundEnvelope,
     71         review
     72       )
     73     ).toEqual({ status: 'invalid' });
     74     expect(
     75       captureSelfRecoveryPreparation(
     76         {} as PrivateStorageRepository,
     77         {} as IdentitySession,
     78         {} as ReservedSendIdentity,
     79         review
     80       )
     81     ).toBeUndefined();
     82     expect(conversions).toBe(0);
     83   });
     84 });