app

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

generate-interop-manifest.mjs (2572B)


      1 // Test-only generation and validation. Ordinary web commands never regenerate.
      2 import assert from 'node:assert/strict';
      3 import { lstat, readFile, writeFile } from 'node:fs/promises';
      4 import { TextEncoder } from 'node:util';
      5 import {
      6   buildInteropManifest,
      7   interopInputPaths,
      8   validateInteropManifest
      9 } from '../tests/conformance/interop-manifest.ts';
     10 import { webTemplates } from '../tests/conformance/food-writer-cases.ts';
     11 assert.equal(process.version, 'v24.21.0');
     12 const args = process.argv.slice(2);
     13 assert.ok(args.length === 0 || (args.length === 1 && args[0] === '--write'));
     14 const root = new URL('../../', import.meta.url);
     15 const templatePath =
     16   'contracts/interop/food_availability/web_templates.v1.json';
     17 const manifestPath = 'contracts/interop/manifest.json';
     18 const generatedTemplates = new TextEncoder().encode(
     19   JSON.stringify(webTemplates, null, 2) + '\n'
     20 );
     21 const inputs = new Map();
     22 for (const path of interopInputPaths) {
     23   if (path === templatePath && args[0] === '--write') {
     24     inputs.set(path, generatedTemplates);
     25     continue;
     26   }
     27   const file = new URL(path, root);
     28   const info = await lstat(file);
     29   assert.ok(
     30     info.isFile() &&
     31       !info.isSymbolicLink() &&
     32       info.size > 0 &&
     33       info.size <= 1024 * 1024
     34   );
     35   const bytes = new Uint8Array(await readFile(file));
     36   assert.equal(bytes.byteLength, info.size);
     37   inputs.set(path, bytes);
     38 }
     39 const manifest = buildInteropManifest(inputs, webTemplates);
     40 if (args[0] === '--write') {
     41   for (const [path, bytes] of [
     42     [templatePath, generatedTemplates],
     43     [manifestPath, JSON.stringify(manifest, null, 2) + '\n']
     44   ]) {
     45     const file = new URL(path, root);
     46     try {
     47       const info = await lstat(file);
     48       assert.ok(info.isFile() && !info.isSymbolicLink() && info.nlink === 1);
     49     } catch (error) {
     50       if (error.code !== 'ENOENT') throw error;
     51     }
     52     await writeFile(file, bytes);
     53   }
     54 } else {
     55   const file = new URL(manifestPath, root);
     56   const info = await lstat(file);
     57   assert.ok(
     58     info.isFile() &&
     59       !info.isSymbolicLink() &&
     60       info.size > 0 &&
     61       info.size <= 1024 * 1024
     62   );
     63   validateInteropManifest(
     64     JSON.parse(await readFile(file, 'utf8')),
     65     inputs,
     66     webTemplates
     67   );
     68 }
     69 console.log(
     70   JSON.stringify({
     71     result: 'PASS',
     72     public_cases: 40,
     73     website_cases: 16,
     74     signed_reader_cases: 18,
     75     revision: manifest.oracle.revision,
     76     native_consumer: 'REQUIRED_NATIVE_TEST',
     77     boundary_cases: 10,
     78     qualification: manifest.qualification,
     79     action: args[0] === '--write' ? 'EXPLICIT_GENERATION' : 'CHECK_ONLY'
     80   })
     81 );