app

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

interop-consumer.test.ts (4226B)


      1 import assert from 'node:assert/strict';
      2 import { lstat, readFile } from 'node:fs/promises';
      3 import test from 'node:test';
      4 import { readFoodEvent } from '../../src/lib/contracts/food-availability-v1/read.ts';
      5 import { buildFoodTemplate } from '../../src/lib/contracts/food-availability-v1/write.ts';
      6 import {
      7   interopInputPaths,
      8   type buildInteropManifest
      9 } from './interop-manifest.ts';
     10 import { consumeInteropTypescript } from './interop-consumer.ts';
     11 import { webTemplates } from './food-writer-cases.ts';
     12 const root = new URL('../../../', import.meta.url);
     13 const inputs = new Map<string, Uint8Array>();
     14 for (const path of interopInputPaths) {
     15   const file = new URL(path, root);
     16   const info = await lstat(file);
     17   assert.ok(
     18     info.isFile() &&
     19       !info.isSymbolicLink() &&
     20       info.size > 0 &&
     21       info.size <= 1024 * 1024
     22   );
     23   const bytes = new Uint8Array(await readFile(file));
     24   assert.equal(bytes.byteLength, info.size);
     25   inputs.set(path, bytes);
     26 }
     27 const manifest: unknown = JSON.parse(
     28   await readFile(new URL('contracts/interop/manifest.json', root), 'utf8')
     29 );
     30 void test('shared manifest executes actual TS adapters and reports every unsupported counterpart', () => {
     31   const result = consumeInteropTypescript(manifest, inputs);
     32   assert.equal(result.executed_cases, 40);
     33   assert.equal(result.unsupported_cases, 26);
     34   assert.equal(new Set(result.cases.map((row) => row.id)).size, 66);
     35   console.log(JSON.stringify(result));
     36 });
     37 void test('changed actual reader projection fails against the pinned oracle expectation', () => {
     38   assert.throws(() =>
     39     consumeInteropTypescript(manifest, inputs, {
     40       read: (raw) => {
     41         const actual = readFoodEvent(raw);
     42         return actual.outcome === 'admitted'
     43           ? {
     44               ...actual,
     45               projection: {
     46                 ...actual.projection,
     47                 location: 'deliberately changed reader semantics'
     48               }
     49             }
     50           : actual;
     51       }
     52     })
     53   );
     54 });
     55 void test('changed actual writer output fails against retained Rust wire parts', () => {
     56   const writers = structuredClone(webTemplates);
     57   writers[0] = {
     58     ...writers[0],
     59     wire_parts: {
     60       ...writers[0].wire_parts,
     61       content:
     62         writers[0].wire_parts.content + 'deliberately changed writer semantics'
     63     }
     64   };
     65   assert.throws(() => consumeInteropTypescript(manifest, inputs, { writers }));
     66 });
     67 void test('changed actual boundary builder output fails rather than accepting matching metadata', () => {
     68   assert.throws(() =>
     69     consumeInteropTypescript(manifest, inputs, {
     70       build: (draft) => {
     71         const actual = buildFoodTemplate(draft);
     72         return actual.ok
     73           ? {
     74               ...actual,
     75               wire_parts: {
     76                 ...actual.wire_parts,
     77                 tags: actual.wire_parts.tags.map((tag) =>
     78                   tag[0] === 'published_at' ? [tag[0], '2'] : tag
     79                 )
     80               }
     81             }
     82           : actual;
     83       }
     84     })
     85   );
     86 });
     87 void test('unsafe JavaScript timestamps cannot be promoted from unsupported to accepted', () => {
     88   assert.throws(() =>
     89     consumeInteropTypescript(manifest, inputs, {
     90       build: (draft) => {
     91         const actual = buildFoodTemplate(draft);
     92         return Number.isSafeInteger(draft.published_at)
     93           ? actual
     94           : buildFoodTemplate({ ...draft, published_at: 1, created_at: 1 });
     95       }
     96     })
     97   );
     98 });
     99 void test('zero matching profile cases fails', () => {
    100   assert.throws(
    101     () =>
    102       consumeInteropTypescript(manifest, inputs, {
    103         profile: 'unmatched_profile'
    104       }),
    105     /zero matching/
    106   );
    107 });
    108 void test('changed manifest vector hash fails the actual TS consumer', () => {
    109   const changed = structuredClone(manifest) as ReturnType<
    110     typeof buildInteropManifest
    111   >;
    112   changed.cases[0].fixture.sha256 = '0'.repeat(64);
    113   assert.throws(() => consumeInteropTypescript(changed, inputs));
    114 });
    115 void test('changed raw vector bytes fail the actual TS consumer', () => {
    116   const changed = new Map(inputs);
    117   const path = interopInputPaths[0];
    118   const bytes = inputs.get(path)!;
    119   changed.set(path, new Uint8Array([...bytes, 32]));
    120   assert.throws(() => consumeInteropTypescript(manifest, changed));
    121 });