app

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

private-send-reservations.test.ts (3482B)


      1 import { expect, it } from 'vitest';
      2 import { inspectPrivateSendReservation } from '../../src/lib/persistence/private-send-reservations.ts';
      3 const owner =
      4     '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
      5   peer = 'c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5',
      6   id = '12345678-1234-4234-8234-123456789abc';
      7 const row = {
      8   schema: 1,
      9   family: 'private_send_reservation',
     10   owner,
     11   id,
     12   revision: 0,
     13   peer,
     14   rumorHash: 'f'.repeat(64),
     15   createdAt: 1700000100
     16 };
     17 const read = (value: unknown) =>
     18   inspectPrivateSendReservation(JSON.stringify(value), owner, id);
     19 it('reservation contains stable command and rumor metadata without plaintext fields', () => {
     20   expect(read(row)).toEqual(row);
     21   expect(Object.keys(read(row)!).sort()).toEqual([
     22     'createdAt',
     23     'family',
     24     'id',
     25     'owner',
     26     'peer',
     27     'revision',
     28     'rumorHash',
     29     'schema'
     30   ]);
     31 });
     32 it('body, subject, private context, tags, wire and unknown fields are not admitted', () => {
     33   for (const key of [
     34     'body',
     35     'content',
     36     'subject',
     37     'product',
     38     'contact',
     39     'tags',
     40     'wire',
     41     'unknown'
     42   ])
     43     expect(read({ ...row, [key]: 'Private sentinel' })).toBeUndefined();
     44 });
     45 it('owner and UUID command namespace are exact and cannot be coerced', () => {
     46   expect(
     47     inspectPrivateSendReservation(JSON.stringify(row), peer, id)
     48   ).toBeUndefined();
     49   expect(
     50     inspectPrivateSendReservation(
     51       JSON.stringify(row),
     52       owner,
     53       '12345678-1234-4234-8234-123456789abd'
     54     )
     55   ).toBeUndefined();
     56   for (const value of [
     57     null,
     58     1,
     59     owner.toUpperCase(),
     60     '0'.repeat(64),
     61     { toString: () => owner }
     62   ])
     63     expect(
     64       inspectPrivateSendReservation(JSON.stringify(row), value, id)
     65     ).toBeUndefined();
     66   for (const value of [
     67     null,
     68     id.toUpperCase(),
     69     'a'.repeat(64),
     70     '12345678-1234-1234-8234-123456789abc'
     71   ])
     72     expect(read({ ...row, id: value })).toBeUndefined();
     73 });
     74 it('self, malformed/noncanonical peers and unsupported revisions/families are rejected', () => {
     75   for (const value of [owner, peer.toUpperCase(), '0'.repeat(64), null])
     76     expect(read({ ...row, peer: value })).toBeUndefined();
     77   for (const value of [1, -1, '0', null])
     78     expect(read({ ...row, revision: value })).toBeUndefined();
     79   expect(read({ ...row, schema: 2 })).toBeUndefined();
     80   expect(read({ ...row, family: 'private_send' })).toBeUndefined();
     81 });
     82 it('rumor ID and timestamp are bounded canonical data, never floats or coercions', () => {
     83   for (const value of ['F'.repeat(64), 'f'.repeat(63), null, 0])
     84     expect(read({ ...row, rumorHash: value })).toBeUndefined();
     85   for (const value of [-1, 1.5, Number.MAX_SAFE_INTEGER + 1, '100', null])
     86     expect(read({ ...row, createdAt: value })).toBeUndefined();
     87   expect(
     88     inspectPrivateSendReservation(
     89       JSON.stringify(row).replace('1700000100', '-0'),
     90       owner,
     91       id
     92     )
     93   ).toBeUndefined();
     94   expect(read({ ...row, createdAt: 0 })!.createdAt).toBe(0);
     95 });
     96 it('raw objects cannot invoke conversion and oversized/malformed input stays unsupported', () => {
     97   let converted = false;
     98   expect(
     99     inspectPrivateSendReservation(
    100       {
    101         toJSON() {
    102           converted = true;
    103           return row;
    104         }
    105       },
    106       owner,
    107       id
    108     )
    109   ).toBeUndefined();
    110   expect(converted).toBe(false);
    111   for (const raw of ['', '{', 'null', '[]', ' '.repeat(4097)])
    112     expect(inspectPrivateSendReservation(raw, owner, id)).toBeUndefined();
    113 });