/** * Brittle edge checks for high-traffic utilities (Issue 7 traceability). */ import { readFile } from 'node:fs/promises' import path from 'node:path' import { fileURLToPath } from 'node:url' import test from 'brittle' import b4a from 'b4a' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor /** * @param {string} name * @param {string[]} [libExtras] — e.g. `['awk-engine.js']` before `awk.js` (matches `build.mjs` preamble). */ async function loadBin(name, libExtras = []) { const runtime = await readFile( path.join(__dirname, '../lib/runtime.js'), 'utf8' ) let pre = '' for (const f of libExtras) { let chunk = await readFile(path.join(__dirname, '../lib', f), 'utf8') if (f === 'awk-engine.js') { chunk = chunk.replace( /^export async function bareAwkRun/m, 'async function bareAwkRun' ) } pre += chunk + '\n' } const bodyRaw = await readFile( path.join(__dirname, `../src/${name}.js`), 'utf8' ) const body = bodyRaw.replace(/^export\s+\{[^}]*\}\s*;?\s*$/gm, '') return new AsyncFunction( 'ctx', 'argv', `${runtime}\n${pre}${body}\nif (typeof run === 'function') return await run(ctx, argv)\n` ) } test('cp -u skips newer destination', async (t) => { const run = await loadBin('cp') const store = { files: { '/src': { stat: { mode: 0o644, type: 'file', mtimeMs: 100 }, body: b4a.from('a') }, '/dst': { stat: { mode: 0o644, type: 'file', mtimeMs: 200 }, body: b4a.from('b') } } } const ctx = { b4a, exitCode: 0, console: { log: () => {}, error: () => {} }, vfs: { async lstat(p) { const e = store.files[p] return e ? e.stat : null }, async readFile(p) { const e = store.files[p] return e ? e.body : null }, async writeFile() { throw new Error('should not write when -u skips') } } } await run(ctx, ['cp', '-u', '/src', '/dst']) t.is(ctx.exitCode, 0) t.is(b4a.toString(store.files['/dst'].body), 'b') }) test('test -e is true for existing file', async (t) => { const run = await loadBin('test') let code = 1 const ctx = { exitCode: 0, console: { log: () => {}, error: () => {} }, vfs: { async stat() { return { type: 'file' } } } } await run(ctx, ['test', '-e', '/x']) code = ctx.exitCode || 0 t.is(code, 0) }) test('true exits 0', async (t) => { const run = await loadBin('true') const ctx = { exitCode: 1, console: { log: () => {}, error: () => {} } } await run(ctx, ['true']) t.is(ctx.exitCode, 0) }) test('expr integer addition (Issue 7 trace)', async (t) => { const run = await loadBin('expr') const out = [] const ctx = { exitCode: 0, console: { log: (s) => { out.push(String(s)) }, error: () => {} } } await run(ctx, ['expr', '2', '+', '3']) t.is(ctx.exitCode, 0) t.is(out.join('').trim(), '5') }) test('basenc --base16 encodes file (Issue 7 trace)', async (t) => { const run = await loadBin('basenc') const out = [] const ctx = { b4a, exitCode: 0, console: { log: (s) => { out.push(String(s)) }, error: () => {} }, vfs: { async readFile(p) { if (p === '/t') return b4a.from('abc') return null } } } await run(ctx, ['basenc', '--base16', '/t']) t.is(ctx.exitCode, 0) t.is(out.join('').trim(), '616263') }) test('grep -F matches line (Issue 7 trace)', async (t) => { const run = await loadBin('grep') const out = [] const ctx = { b4a, exitCode: 0, console: { log: (s) => { out.push(String(s)) }, error: () => {} }, vfs: { async readFile(p) { if (p === '/f') return b4a.from('one\ntwo\nthree\n') return null } } } await run(ctx, ['grep', '-F', 'two', '/f']) t.is(ctx.exitCode, 0) t.ok(out.some((l) => l.includes('two'))) }) test('xargs invokes runBinCommand per token', async (t) => { const run = await loadBin('xargs') const inv = [] const ctx = { exitCode: 0, shellStdin: 'a\nb\n', console: { log: () => {}, error: () => {} }, async runBinCommand(argv) { inv.push([...argv]) ctx.exitCode = 0 } } await run(ctx, ['xargs', '-n', '1', 'echo']) t.is(ctx.exitCode, 0) t.is(inv.length, 2) t.is(inv[0][0], 'echo') t.is(inv[0][1], 'a') t.is(inv[1][1], 'b') }) test('awk FIELDWIDTHS splits when BARE_OS_AWK_FIELDWIDTHS', async (t) => { const run = await loadBin('awk', ['awk-engine.js']) /** @type {string[]} */ const logs = [] const ctx = { exitCode: 0, shellStdin: 'abcde12\n', b4a, console: { log: (/** @type {string} */ s) => logs.push(s), error: () => {} }, vfs: { env: { BARE_OS_AWK_FIELDWIDTHS: '1' }, readFile: async () => null } } await run(ctx, ['awk', '-v', 'FIELDWIDTHS=5 2', '{ print $2 }']) t.is(ctx.exitCode, 0) t.is(logs.join('\n'), '12') }) test('xargs -L batches by input lines', async (t) => { const run = await loadBin('xargs') const inv = [] const ctx = { exitCode: 0, shellStdin: 'one\ntwo\nthree\n', console: { log: () => {}, error: () => {} }, async runBinCommand(argv) { inv.push([...argv]) ctx.exitCode = 0 } } await run(ctx, ['xargs', '-L', '2', 'echo']) t.is(ctx.exitCode, 0) t.is(inv.length, 2) t.is(inv[0].slice(1).join(' '), 'one two') t.is(inv[1].slice(1).join(' '), 'three') })