app

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

inbox-readback.test.ts (7363B)


      1 import { it, expect } from 'vitest';
      2 import { makeFixture } from '../e2e/harness/approved-signing.ts';
      3 import { fixturePolicy, discovery } from '../e2e/harness/inbox-setup.ts';
      4 import {
      5   createPublicScheduler,
      6   createPublicRun,
      7   openInboxRequest,
      8   openPublicRequest,
      9   closePublicScheduler
     10 } from '../../src/lib/nostr/request-scope.ts';
     11 it('forwards the actual canonical admitted inbox source with the verified proof', () => {
     12   const f = makeFixture(10050),
     13     scheduler = createPublicScheduler({
     14       now: () => 0,
     15       schedule: () => () => {}
     16     });
     17   const seen: (string | undefined)[] = [];
     18   try {
     19     const run = createPublicRun(scheduler, fixturePolicy());
     20     openInboxRequest(
     21       run,
     22       f.owner,
     23       (sink) => {
     24         sink({
     25           type: 'EVENT',
     26           from: discovery + '/',
     27           id: 'owned',
     28           event: f.sign(f.template)
     29         });
     30         sink({ type: 'EOSE', from: discovery + '/', id: 'owned' });
     31         return () => {};
     32       },
     33       (_proof, source?: string) => {
     34         seen.push(source);
     35       }
     36     );
     37     expect(seen).toEqual([discovery]);
     38   } finally {
     39     closePublicScheduler(scheduler);
     40     f.close();
     41   }
     42 });
     43 it('unknown inbox sources emit no proof and ordinary public callbacks retain their original arity', () => {
     44   const f = makeFixture(10050),
     45     publicFixture = makeFixture(30402),
     46     scheduler = createPublicScheduler({
     47       now: () => 0,
     48       schedule: () => () => {}
     49     });
     50   const inbox: unknown[] = [],
     51     publicArguments: number[] = [];
     52   try {
     53     openInboxRequest(
     54       createPublicRun(scheduler, fixturePolicy()),
     55       f.owner,
     56       (sink) => {
     57         sink({
     58           type: 'EVENT',
     59           from: 'wss://foreign.example.org',
     60           id: 'owned',
     61           event: f.sign(f.template)
     62         });
     63         return () => {};
     64       },
     65       (...args) => {
     66         inbox.push(args);
     67       }
     68     );
     69     openPublicRequest(
     70       createPublicRun(scheduler, fixturePolicy()),
     71       'head',
     72       (sink) => {
     73         sink({
     74           type: 'EVENT',
     75           from: discovery,
     76           id: 'owned',
     77           event: publicFixture.sign(publicFixture.template)
     78         });
     79         return () => {};
     80       },
     81       (...args) => {
     82         publicArguments.push(args.length);
     83       }
     84     );
     85     expect(inbox).toEqual([]);
     86     expect(publicArguments).toEqual([1]);
     87   } finally {
     88     closePublicScheduler(scheduler);
     89     f.close();
     90     publicFixture.close();
     91   }
     92 });
     93 
     94 import {
     95   decodePublicRecord,
     96   publicRecordSnapshot,
     97   publicRecordWire
     98 } from '../../src/lib/persistence/records.ts';
     99 import {
    100   preparePublicReceiptTransition,
    101   publicTransitionSnapshot
    102 } from '../../src/lib/persistence/artifact-records.ts';
    103 function receiptFixture(kind: 10050 | 30402 = 10050) {
    104   const f = makeFixture(kind);
    105   const row = publicRecordSnapshot(f.record, f.owner, f.id);
    106   if (!row || row.family === 'public_draft')
    107     throw new Error('missing operation');
    108   const event = f.sign(f.template),
    109     wire = JSON.stringify(event);
    110   const fact = {
    111     actionId: crypto.randomUUID(),
    112     origin: row.capture.targets[0],
    113     role: kind === 10050 ? 'preference' : 'publication',
    114     attempt: 1,
    115     eventId: event.id,
    116     status: 'accepted',
    117     observedAtMilliseconds: 102,
    118     readbackWire: wire
    119   };
    120   const record = {
    121     ...row,
    122     revision: 2,
    123     artifact: { eventId: event.id, wire },
    124     receipts: []
    125   };
    126   return { ...f, record, fact, wire };
    127 }
    128 it('preference readback names its actual discovery origin without changing the acknowledged write target', () => {
    129   const f = receiptFixture();
    130   try {
    131     const row = {
    132       ...f.record,
    133       receipts: [{ ...f.fact, readbackOrigin: 'wss://discovery.example.org' }]
    134     };
    135     const raw = JSON.stringify(row),
    136       decoded = decodePublicRecord(raw, f.owner, f.id);
    137     expect(decoded.ok).toBe(true);
    138     if (!decoded.ok) throw new Error(decoded.reason);
    139     expect(publicRecordWire(decoded.value, f.owner, f.id)).toBe(raw);
    140   } finally {
    141     f.close();
    142   }
    143 });
    144 it('legacy ACK and legacy readback receipts roundtrip unchanged without an invented discovery source', () => {
    145   const f = receiptFixture();
    146   try {
    147     for (const readbackWire of [null, f.wire]) {
    148       const raw = JSON.stringify({
    149         ...f.record,
    150         receipts: [{ ...f.fact, readbackWire }]
    151       });
    152       const decoded = decodePublicRecord(raw, f.owner, f.id);
    153       expect(decoded.ok).toBe(true);
    154       if (!decoded.ok) throw new Error(decoded.reason);
    155       expect(publicRecordWire(decoded.value, f.owner, f.id)).toBe(raw);
    156     }
    157   } finally {
    158     f.close();
    159   }
    160 });
    161 it('readback source rejects null wire, noncanonical origins, arbitrary fields, invalid signatures and ordinary publication rows', () => {
    162   const f = receiptFixture(),
    163     publicFixture = receiptFixture(30402);
    164   try {
    165     for (const fact of [
    166       {
    167         ...f.fact,
    168         readbackOrigin: 'wss://discovery.example.org',
    169         readbackWire: null
    170       },
    171       { ...f.fact, readbackOrigin: 'wss://discovery.example.org/' },
    172       { ...f.fact, readbackOrigin: 'ws://127.0.0.1' },
    173       { ...f.fact, readbackOrigin: 'wss://discovery.example.org', extra: true },
    174       {
    175         ...f.fact,
    176         readbackOrigin: 'wss://discovery.example.org',
    177         readbackWire: JSON.stringify({
    178           ...JSON.parse(f.wire),
    179           content: 'forged'
    180         })
    181       }
    182     ])
    183       expect(
    184         decodePublicRecord(
    185           JSON.stringify({ ...f.record, receipts: [fact] }),
    186           f.owner,
    187           f.id
    188         ).ok
    189       ).toBe(false);
    190     expect(
    191       decodePublicRecord(
    192         JSON.stringify({
    193           ...publicFixture.record,
    194           receipts: [
    195             {
    196               ...publicFixture.fact,
    197               readbackOrigin: 'wss://discovery.example.org'
    198             }
    199           ]
    200         }),
    201         publicFixture.owner,
    202         publicFixture.id
    203       ).ok
    204     ).toBe(false);
    205   } finally {
    206     f.close();
    207     publicFixture.close();
    208   }
    209 });
    210 it('distinct discovery sources remain separate typed receipt facts within the same readback action', () => {
    211   const f = receiptFixture();
    212   try {
    213     const decoded = decodePublicRecord(JSON.stringify(f.record), f.owner, f.id);
    214     if (!decoded.ok) throw new Error(decoded.reason);
    215     const first = preparePublicReceiptTransition(
    216       decoded.value,
    217       f.owner,
    218       f.id,
    219       JSON.stringify({
    220         ...f.fact,
    221         readbackOrigin: 'wss://discovery.example.org'
    222       })
    223     );
    224     expect(first.ok).toBe(true);
    225     if (!first.ok) throw new Error(first.reason);
    226     const transition = publicTransitionSnapshot(first.value, f.owner, f.id);
    227     const next = decodePublicRecord(transition?.nextWire, f.owner, f.id);
    228     if (!next.ok) throw new Error(next.reason);
    229     const second = preparePublicReceiptTransition(
    230       next.value,
    231       f.owner,
    232       f.id,
    233       JSON.stringify({ ...f.fact, readbackOrigin: 'wss://second.example.org' })
    234     );
    235     expect(second.ok).toBe(true);
    236     if (!second.ok) throw new Error(second.reason);
    237     const settled = publicTransitionSnapshot(second.value, f.owner, f.id);
    238     const both = decodePublicRecord(settled?.nextWire, f.owner, f.id);
    239     if (!both.ok) throw new Error(both.reason);
    240     const row = publicRecordSnapshot(both.value, f.owner, f.id);
    241     expect(row && row.family !== 'public_draft' && row.receipts).toHaveLength(
    242       2
    243     );
    244   } finally {
    245     f.close();
    246   }
    247 });