app

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

commands.test.mjs (6833B)


      1 import assert from 'node:assert/strict';
      2 import { performance } from 'node:perf_hooks';
      3 import { spawnSync } from 'node:child_process';
      4 import {
      5   mkdtemp,
      6   mkdir,
      7   readFile,
      8   writeFile,
      9   rm,
     10   symlink
     11 } from 'node:fs/promises';
     12 import os from 'node:os';
     13 import path from 'node:path';
     14 import { test } from 'node:test';
     15 
     16 /** @param {import('node:test').TestContext} t */
     17 async function fixture(t) {
     18   const root = await mkdtemp(path.join(os.tmpdir(), 'hc web commands '));
     19   t.after(() => rm(root, { recursive: true, force: true }));
     20   await mkdir(path.join(root, 'web/tools'), { recursive: true });
     21   await mkdir(path.join(root, 'bin'));
     22   for (const [source, target] of [
     23     ['../../../Makefile', 'Makefile'],
     24     ['../../package.json', 'web/package.json'],
     25     ['../../.node-version', 'web/.node-version'],
     26     ['../../tools/doctor.mjs', 'web/tools/doctor.mjs']
     27   ])
     28     await writeFile(
     29       path.join(root, target),
     30       await readFile(new URL(source, import.meta.url))
     31     );
     32   await symlink(process.execPath, path.join(root, 'bin/node'));
     33   const log = path.join(root, 'calls.frames');
     34   await symlink('/bin/sh', path.join(root, 'bin/corepack'));
     35   await writeFile(
     36     path.join(root, 'web/pnpm'),
     37     `#!/bin/sh
     38 printf '%s\\0' "$PWD" "\${COREPACK_ENABLE_NETWORK-}" "$(( $# + 1 ))" "$0" "$@" >> "$HC_COMMAND_LOG"
     39 version=false
     40 failed=false
     41 for argument do
     42   if [ "$argument" = '--version' ]; then version=true; fi
     43   if [ -n "\${HC_FAIL_COMMAND-}" ] && [ "$argument" = "$HC_FAIL_COMMAND" ]; then failed=true; fi
     44 done
     45 if [ "$version" = true ]; then
     46   printf '%s\\n' "\${HC_PNPM_VERSION-12.9.1}"
     47 elif [ "$failed" = true ]; then
     48   exit 37
     49 fi
     50 `,
     51     { mode: 0o644 }
     52   );
     53   const env = {
     54     ...process.env,
     55     PATH: path.join(root, 'bin'),
     56     HC_COMMAND_LOG: log
     57   };
     58   /** @param {string} target @param {Record<string, string>} [extra] */
     59   const run = (target, extra = {}) => {
     60     const started = performance.now();
     61     const result = spawnSync(
     62       '/usr/bin/make',
     63       ['--no-print-directory', target],
     64       {
     65         cwd: root,
     66         env: { ...env, ...extra },
     67         encoding: 'utf8',
     68         timeout: 86400000
     69       }
     70     );
     71     return {
     72       ...result,
     73       diagnostic: JSON.stringify({
     74         target,
     75         status: result.status,
     76         signal: result.signal,
     77         error:
     78           result.error && 'code' in result.error
     79             ? result.error.code
     80             : undefined,
     81         elapsed_ms: performance.now() - started,
     82         stdout: result.stdout,
     83         stderr: result.stderr
     84       })
     85     };
     86   };
     87   const calls = async () => {
     88     const fields = (await readFile(log, 'utf8')).split('\0');
     89     assert.equal(fields.pop(), '');
     90     const records = [];
     91     for (let index = 0; index < fields.length;) {
     92       const cwd = fields[index++];
     93       const network = fields[index++];
     94       const count = Number(fields[index++]);
     95       assert.ok(
     96         Number.isSafeInteger(count) &&
     97           count >= 0 &&
     98           index + count <= fields.length
     99       );
    100       records.push({
    101         cwd,
    102         args: fields.slice(index, index + count),
    103         ...(network ? { network } : {})
    104       });
    105       index += count;
    106     }
    107     return records;
    108   };
    109   return { root, run, calls };
    110 }
    111 
    112 await test(
    113   'standalone web targets select web cwd and run every required lane without native tools',
    114   { timeout: 86400000 },
    115   async (t) => {
    116     const { root, run, calls } = await fixture(t);
    117     for (const target of [
    118       'web-doctor',
    119       'web-install',
    120       'web-check',
    121       'web-build',
    122       'web-dev'
    123     ]) {
    124       const result = run(target);
    125       assert.equal(result.status, 0, result.diagnostic);
    126     }
    127     const actual = await calls();
    128     assert.ok(actual.every((call) => call.cwd === path.join(root, 'web')));
    129     assert.ok(
    130       actual
    131         .filter((call) => call.args.includes('--version'))
    132         .every((call) => call.network === '0')
    133     );
    134     assert.deepEqual(
    135       actual
    136         .filter((call) => !call.args.includes('--version'))
    137         .map((call) => call.args),
    138       [
    139         ['pnpm', 'install', '--frozen-lockfile'],
    140         ['pnpm', 'run', 'check'],
    141         ['pnpm', 'run', 'lint'],
    142         ['pnpm', 'run', 'test:unit'],
    143         ['pnpm', 'run', 'test:conformance'],
    144         ['pnpm', 'run', 'build'],
    145         ['pnpm', 'run', 'dev']
    146       ]
    147     );
    148   }
    149 );
    150 
    151 await test(
    152   'doctor rejects a mismatched selected manager before installation',
    153   { timeout: 86400000 },
    154   async (t) => {
    155     const { run, calls } = await fixture(t);
    156     const result = run('web-install', { HC_PNPM_VERSION: '10.25.0' });
    157     assert.notEqual(result.status, 0, result.diagnostic);
    158     assert.match(
    159       result.stderr,
    160       /Expected pnpm 12\.9\.1; selected 10\.25\.0/,
    161       result.diagnostic
    162     );
    163     assert.equal((await calls()).length, 1);
    164   }
    165 );
    166 
    167 await test(
    168   'web check propagates failing commands and stops subsequent lanes',
    169   { timeout: 86400000 },
    170   async (t) => {
    171     const { run, calls } = await fixture(t);
    172     const result = run('web-check', { HC_FAIL_COMMAND: 'lint' });
    173     assert.notEqual(result.status, 0, result.diagnostic);
    174     assert.match(result.stderr, /Error 37/, result.diagnostic);
    175     assert.deepEqual(
    176       (await calls()).map((call) => call.args),
    177       [
    178         ['pnpm', '--version'],
    179         ['pnpm', 'run', 'check'],
    180         ['pnpm', 'run', 'lint']
    181       ]
    182     );
    183   }
    184 );
    185 
    186 await test(
    187   'missing web tooling fails without trying native tools',
    188   { timeout: 86400000 },
    189   async (t) => {
    190     const { root, run } = await fixture(t);
    191     await rm(path.join(root, 'bin/corepack'));
    192     const result = run('web-build');
    193     assert.notEqual(result.status, 0, result.diagnostic);
    194     assert.match(result.stderr, /ENOENT/, result.diagnostic);
    195   }
    196 );
    197 
    198 await test(
    199   'doctor rejects mismatched toolchain authorities',
    200   { timeout: 86400000 },
    201   async (t) => {
    202     const { root, run } = await fixture(t);
    203     await writeFile(path.join(root, 'web/.node-version'), '24.20.0\n');
    204     const result = run('web-doctor');
    205     assert.notEqual(result.status, 0, result.diagnostic);
    206     assert.match(
    207       result.stderr,
    208       /Web toolchain pins disagree or are not exact/,
    209       result.diagnostic
    210     );
    211   }
    212 );
    213 
    214 await test(
    215   'doctor rejects wrong selected Node before invoking Corepack',
    216   { timeout: 86400000 },
    217   async (t) => {
    218     const { root, run } = await fixture(t);
    219     const file = path.join(root, 'web/package.json');
    220     const inputs = JSON.parse(await readFile(file, 'utf8'));
    221     inputs.engines.node = '24.20.0';
    222     await writeFile(file, JSON.stringify(inputs));
    223     await writeFile(path.join(root, 'web/.node-version'), '24.20.0\n');
    224     const result = run('web-doctor');
    225     assert.notEqual(result.status, 0, result.diagnostic);
    226     assert.match(
    227       result.stderr,
    228       /Expected Node 24\.20\.0; selected 24\.21\.0/,
    229       result.diagnostic
    230     );
    231   }
    232 );