app

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

interop-consumer.ts (6764B)


      1 import assert from 'node:assert/strict';
      2 import { readFoodEvent } from '../../src/lib/contracts/food-availability-v1/read.ts';
      3 import {
      4   buildFoodTemplate,
      5   type FoodDraft
      6 } from '../../src/lib/contracts/food-availability-v1/write.ts';
      7 import { webTemplates } from './food-writer-cases.ts';
      8 import {
      9   validateInteropManifest,
     10   type InteropInputs,
     11   buildInteropManifest
     12 } from './interop-manifest.ts';
     13 
     14 type Manifest = ReturnType<typeof buildInteropManifest>;
     15 type Recipe = {
     16   id: string;
     17   expected: Record<string, unknown>;
     18   signed_wires: Record<string, string>;
     19 };
     20 type Boundary = {
     21   id: string;
     22   kind: 'timestamp' | 'identifier';
     23   value: string;
     24   expected: { typescript: Record<string, unknown> };
     25 };
     26 const base: FoodDraft = {
     27   identifier: 'boundary-id',
     28   title: 'Fresh food',
     29   description: 'Fresh food available locally.',
     30   location: 'Victoria',
     31   amount: '1',
     32   currency: 'CAD',
     33   unit: 'g',
     34   status: 'active',
     35   published_at: 1,
     36   created_at: 1
     37 };
     38 
     39 function reader(
     40   raw: string,
     41   expected: Record<string, unknown>,
     42   read: typeof readFoodEvent
     43 ) {
     44   const actual = read(raw);
     45   if ('error' in expected) {
     46     assert.equal(actual.outcome, 'rejected');
     47     assert.ok(actual.outcome === 'rejected');
     48     assert.deepEqual(actual.error, expected.error);
     49     return { outcome: actual.outcome, error: actual.error };
     50   }
     51   assert.ok(actual.outcome !== 'rejected');
     52   assert.equal(actual.event_id, expected.event_id);
     53   if ('partition' in expected) {
     54     assert.ok(actual.outcome === 'excluded');
     55     assert.deepEqual(actual.partition, expected.partition);
     56     return {
     57       outcome: actual.outcome,
     58       event_id: actual.event_id,
     59       partition: actual.partition
     60     };
     61   }
     62   assert.ok(actual.outcome === 'admitted');
     63   if ('projection' in expected) {
     64     assert.deepEqual(actual.projection, expected.projection);
     65     return {
     66       outcome: actual.outcome,
     67       event_id: actual.event_id,
     68       projection: actual.projection
     69     };
     70   }
     71   assert.ok('image_count' in expected);
     72   const observed = {
     73     event_id: actual.event_id,
     74     image_count: actual.projection.images.length,
     75     first_raw_tag: actual.projection.images[0]?.raw_tag,
     76     last_raw_tag: actual.projection.images.at(-1)?.raw_tag,
     77     diagnostics: actual.projection.diagnostics
     78   };
     79   for (const [key, value] of Object.entries(observed))
     80     assert.deepEqual(value, expected[key], key);
     81   return { outcome: actual.outcome, ...observed };
     82 }
     83 
     84 export function consumeInteropTypescript(
     85   manifest: unknown,
     86   inputs: InteropInputs,
     87   options: {
     88     profile?: string;
     89     read?: typeof readFoodEvent;
     90     build?: typeof buildFoodTemplate;
     91     writers?: typeof webTemplates;
     92   } = {}
     93 ) {
     94   const writers = options.writers ?? webTemplates;
     95   validateInteropManifest(manifest, inputs, writers);
     96   const checked = manifest as Manifest;
     97   const decode = (path: string): unknown =>
     98     JSON.parse(
     99       new TextDecoder('utf-8', { fatal: true }).decode(inputs.get(path))
    100     );
    101   const corpus = decode(
    102     'contracts/interop/food_availability/corpus.v1.json'
    103   ) as { vectors: Recipe[] };
    104   const boundaries = decode(
    105     'contracts/interop/food_availability/numeric_boundaries.v1.json'
    106   ) as { vectors: Boundary[] };
    107   const cases: {
    108     id: string;
    109     result: 'PASS' | 'UNSUPPORTED';
    110     actual?: unknown;
    111     reason?: string;
    112   }[] = [];
    113   for (const row of checked.cases) {
    114     if (
    115       row.protocol.profile !== (options.profile ?? 'food_availability_profile')
    116     )
    117       continue;
    118     const participation = row.participation.typescript;
    119     if (participation === 'UNSUPPORTED_CURRENT_ADAPTER') {
    120       assert.ok(row.participation.unsupported_reason);
    121       cases.push({
    122         id: row.id,
    123         result: 'UNSUPPORTED',
    124         reason: row.participation.unsupported_reason
    125       });
    126     } else if (participation === 'food_reader') {
    127       const recipe = corpus.vectors[row.fixture.case_index];
    128       assert.equal(recipe.id, row.id);
    129       cases.push({
    130         id: row.id,
    131         result: 'PASS',
    132         actual: reader(
    133           recipe.signed_wires.event,
    134           recipe.expected,
    135           options.read ?? readFoodEvent
    136         )
    137       });
    138     } else if (participation === 'food_writer') {
    139       const actual = writers[row.fixture.case_index];
    140       assert.equal(actual.id, row.id);
    141       assert.deepEqual(
    142         { result: 'accepted', wire_parts: actual.wire_parts },
    143         row.expected
    144       );
    145       cases.push({
    146         id: row.id,
    147         result: 'PASS',
    148         actual: { wire_parts: actual.wire_parts, summary: actual.summary }
    149       });
    150     } else {
    151       const recipe = boundaries.vectors[row.fixture.case_index];
    152       assert.equal(recipe.id, row.id);
    153       const build = options.build ?? buildFoodTemplate;
    154       const value = recipe.kind === 'timestamp' ? BigInt(recipe.value) : null;
    155       const result = build(
    156         recipe.kind === 'identifier'
    157           ? { ...base, identifier: recipe.value }
    158           : { ...base, published_at: Number(value), created_at: Number(value) }
    159       );
    160       if (participation === 'UNSUPPORTED_JAVASCRIPT_U64') {
    161         assert.ok(value !== null && value > BigInt(Number.MAX_SAFE_INTEGER));
    162         assert.equal(result.ok, false);
    163         assert.ok(!result.ok);
    164         assert.equal(result.error.code, 'timestamp_invalid');
    165         assert.ok(row.participation.unsupported_reason);
    166         cases.push({
    167           id: row.id,
    168           result: 'UNSUPPORTED',
    169           reason: row.participation.unsupported_reason,
    170           actual: { rejected_code: result.error.code }
    171         });
    172       } else {
    173         assert.equal(participation, 'food_boundary');
    174         const actual = !result.ok
    175           ? { result: 'rejected', code: result.error.code }
    176           : recipe.kind === 'identifier'
    177             ? { result: 'accepted', identifier: result.wire_parts.tags[0][1] }
    178             : {
    179                 result: 'accepted',
    180                 timestamp: String(Number(value)),
    181                 encoded_published_at: result.wire_parts.tags.find(
    182                   (tag) => tag[0] === 'published_at'
    183                 )?.[1]
    184               };
    185         assert.deepEqual(actual, recipe.expected.typescript);
    186         cases.push({ id: row.id, result: 'PASS', actual });
    187       }
    188     }
    189   }
    190   assert.ok(cases.length > 0, 'zero matching TypeScript manifest cases');
    191   assert.equal(cases.length, 66);
    192   const executed = cases.filter((row) => row.result === 'PASS').length;
    193   assert.equal(executed, 40);
    194   assert.equal(cases.length - executed, 26);
    195   return {
    196     consumer: 'actual_typescript_food_adapters',
    197     matched_cases: cases.length,
    198     executed_cases: executed,
    199     unsupported_cases: cases.length - executed,
    200     revision: checked.oracle.revision,
    201     qualification: checked.qualification,
    202     signing: checked.signing,
    203     cases
    204   };
    205 }