app

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

food-bidirectional.test.ts (2736B)


      1 import assert from 'node:assert/strict';
      2 import { readFile } from 'node:fs/promises';
      3 import test from 'node:test';
      4 import { webTemplates } from './food-writer-cases.ts';
      5 import { checkFoodReplay } from './food-bidirectional.ts';
      6 
      7 const root = new URL(
      8   '../../../contracts/interop/food_availability/',
      9   import.meta.url
     10 );
     11 const receipt = JSON.parse(
     12   await readFile(new URL('./food-writer-rust.v1.json', import.meta.url), 'utf8')
     13 ) as unknown;
     14 const hashes = await Promise.all(
     15   [
     16     'corpus.v1.json',
     17     'source_profile.v1.json',
     18     'provenance.v1.json',
     19     'oracle/src/main.rs'
     20   ].map(async (name) => [name, await readFile(new URL(name, root))] as const)
     21 );
     22 void test('actual website templates replay exact checked pinned Rust consumption without signing or deployment', () => {
     23   checkFoodReplay(receipt, webTemplates, hashes);
     24 });
     25 for (const mutation of ['tag', 'amount'] as const)
     26   void test(`bidirectional fixture comparison fails after deliberate ${mutation} mutation`, () => {
     27     const altered = webTemplates.map((row) => ({
     28       ...row,
     29       wire_parts: {
     30         ...row.wire_parts,
     31         tags: row.wire_parts.tags.map((tag) => [...tag])
     32       }
     33     }));
     34     const price = altered[0].wire_parts.tags.find((tag) => tag[0] === 'price')!;
     35     if (mutation === 'tag') price[0] = 'different-price';
     36     else price[1] = '1';
     37     assert.throws(() => checkFoodReplay(receipt, altered, hashes));
     38   });
     39 void test('replay refuses wrong public revision, producer identity and fixture-only claims', () => {
     40   const original = receipt as {
     41     result: Record<string, unknown>;
     42     inputs: Record<string, string>;
     43   };
     44   for (const change of [
     45     { result: { ...original.result, source: 'fixture_echo' } },
     46     { inputs: { ...original.inputs, 'oracle/src/main.rs': '0'.repeat(64) } },
     47     { revision: '0'.repeat(40) },
     48     { qualification: 'DEPLOYED_TERA' },
     49     { signing: 'SIGNED' },
     50     { web_input_sha256: '0'.repeat(64) }
     51   ])
     52     assert.throws(() =>
     53       checkFoodReplay(
     54         { ...(receipt as object), ...change },
     55         webTemplates,
     56         hashes
     57       )
     58     );
     59 });
     60 void test('replay refuses missing duplicate or altered Rust output cases', () => {
     61   const original = receipt as {
     62     result: { templates: { id: string; wire_parts: { content: string } }[] };
     63   };
     64   for (const mutation of ['missing', 'duplicate', 'content'] as const) {
     65     const changed = structuredClone(original);
     66     if (mutation === 'missing') changed.result.templates.pop();
     67     else if (mutation === 'duplicate')
     68       changed.result.templates[1].id = changed.result.templates[0].id;
     69     else changed.result.templates[0].wire_parts.content += 'changed';
     70     assert.throws(() => checkFoodReplay(changed, webTemplates, hashes));
     71   }
     72 });