app

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

food-boundaries.test.ts (2749B)


      1 import assert from 'node:assert/strict';
      2 import { readFile } from 'node:fs/promises';
      3 import test from 'node:test';
      4 import { buildFoodTemplate } from '../../src/lib/contracts/food-availability-v1/write.ts';
      5 const fixture = JSON.parse(
      6   await readFile(
      7     new URL(
      8       '../../../contracts/interop/food_availability/numeric_boundaries.v1.json',
      9       import.meta.url
     10     ),
     11     'utf8'
     12   )
     13 ) as {
     14   vectors: {
     15     id: string;
     16     kind: 'timestamp' | 'identifier';
     17     value: string;
     18     expected: { typescript: Record<string, unknown> };
     19   }[];
     20 };
     21 const base = {
     22   identifier: 'boundary-id',
     23   title: 'Fresh food',
     24   description: 'Fresh food available locally.',
     25   location: 'Victoria',
     26   amount: '1',
     27   currency: 'CAD',
     28   unit: 'g',
     29   status: 'active' as const,
     30   published_at: 1,
     31   created_at: 1
     32 };
     33 assert.equal(fixture.vectors.length, 10);
     34 for (const row of fixture.vectors)
     35   void test(`actual TS Food boundary ${row.id}`, () => {
     36     if (row.kind === 'identifier') {
     37       const result = buildFoodTemplate({ ...base, identifier: row.value });
     38       assert.ok(result.ok);
     39       assert.deepEqual(
     40         { result: 'accepted', identifier: result.wire_parts.tags[0][1] },
     41         row.expected.typescript
     42       );
     43       return;
     44     }
     45     assert.match(row.value, /^(0|[1-9][0-9]*)$/u);
     46     const value = BigInt(row.value);
     47     const result = buildFoodTemplate({
     48       ...base,
     49       published_at: Number(value),
     50       created_at: Number(value)
     51     });
     52     if (value > BigInt(Number.MAX_SAFE_INTEGER)) {
     53       assert.equal(result.ok, false);
     54       if (!result.ok) assert.equal(result.error.code, 'timestamp_invalid');
     55       assert.deepEqual(row.expected.typescript, {
     56         result: 'unsupported',
     57         reason:
     58           'Canonical timestamp exceeds Number.MAX_SAFE_INTEGER; current JavaScript adapter must reject it without rounding.'
     59       });
     60     } else if (!result.ok) {
     61       assert.deepEqual(
     62         { result: 'rejected', code: result.error.code },
     63         row.expected.typescript
     64       );
     65     } else {
     66       assert.deepEqual(
     67         {
     68           result: 'accepted',
     69           timestamp: String(Number(value)),
     70           encoded_published_at: result.wire_parts.tags.find(
     71             (tag) => tag[0] === 'published_at'
     72           )?.[1]
     73         },
     74         row.expected.typescript
     75       );
     76     }
     77   });
     78 void test('canonical raw identifier spellings remain distinct in actual unsigned outputs', () => {
     79   const results = fixture.vectors
     80     .filter((row) => row.kind === 'identifier')
     81     .map((row) => buildFoodTemplate({ ...base, identifier: row.value }));
     82   assert.equal(results.length, 2);
     83   assert.ok(results[0].ok && results[1].ok);
     84   assert.notEqual(
     85     results[0].wire_parts.tags[0][1],
     86     results[1].wire_parts.tags[0][1]
     87   );
     88 });