app

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

local-records.test.ts (1738B)


      1 import test from 'node:test';
      2 import assert from 'node:assert/strict';
      3 import {
      4   decodePublicRecord,
      5   publicRecordSnapshot
      6 } from '../../src/lib/persistence/records.ts';
      7 await test('actual bounded record codec revalidates stored public strings without opening storage or adopting a foreign author', () => {
      8   const owner =
      9     '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798';
     10   const peer =
     11     'c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5';
     12   const id = '3d030bf5-901d-45e1-8251-41cbdf805e96';
     13   const row = {
     14     schema: 1,
     15     family: 'public_draft',
     16     owner,
     17     id,
     18     revision: 2,
     19     savedAtMilliseconds: 1000,
     20     form: {
     21       title: '',
     22       description: 'incomplete public description',
     23       location: '',
     24       amount: '.',
     25       currency: '',
     26       unit: '',
     27       quantity: '',
     28       contactType: '',
     29       contactValue: ''
     30     }
     31   };
     32   const original = Object.getOwnPropertyDescriptor(globalThis, 'indexedDB');
     33   Object.defineProperty(globalThis, 'indexedDB', {
     34     configurable: true,
     35     get() {
     36       throw new Error('No IDB capability may be acquired by a codec');
     37     }
     38   });
     39   try {
     40     const ready = decodePublicRecord(JSON.stringify(row), owner, id);
     41     if (!ready.ok) throw new Error(ready.reason);
     42     assert.equal(ready.ok, true);
     43     assert.deepEqual(publicRecordSnapshot(ready.value, owner, id), row);
     44     assert.equal(publicRecordSnapshot(ready.value, peer, id), undefined);
     45     assert.deepEqual(decodePublicRecord(JSON.stringify(row), peer, id), {
     46       ok: false,
     47       reason: 'owner_mismatch'
     48     });
     49   } finally {
     50     if (original) Object.defineProperty(globalThis, 'indexedDB', original);
     51     else Reflect.deleteProperty(globalThis, 'indexedDB');
     52   }
     53 });