// modules/test/test-pipe-guard.js — run: node modules/test/test-pipe-guard.js // Cheap tripwire for the three things that silently broke pipe handling before: // 1. _parseArgs('') returning a non-empty argv (parts[0] === undefined -> TypeError, 989 hits) // 2. the empty-stage guard being placed AFTER the parts[0] read // 3. v4.6.131: the raw stage text being discarded, in BOTH pipe implementations — the core one // and the ShellModule override that actually replaces it at boot (measured 2026-10-29: // terminal.executePipeChain.toString() contained `cmdText.split`, not `lastStage`). const fs = require('fs'), path = require('path'); const src = fs.readFileSync(path.join(__dirname, '../core/terminal.js'), 'utf8'); const shellSrc = fs.readFileSync(path.join(__dirname, '../ext/shell.js'), 'utf8'); // 1. _parseArgs('') must return [] — that precondition is what made parts[0] undefined. // The source is a CLASS METHOD, so wrap it in an object literal (a bare `new Function('m(){…}')` // is a SyntaxError). It touches no `this` (verified), so a detached call is safe. const m = src.match(/_parseArgs\((\w+)\)\s*\{[\s\S]*?\n\s{12}\}/); if (!m) { console.error('FAIL: _parseArgs not found — did the method signature or indent change?'); process.exit(1); } const parseArgs = new Function('return ({' + m[0] + '})._parseArgs;')(); const empty = parseArgs(''); const blank = parseArgs(' '); // 2. guards must exist AND precede the read they protect (ordering, not just presence) const ci = src.indexOf('_classify(raw)'); const cGuard = src.indexOf('if (!parts.length)', ci); const cRead = src.indexOf('parts[0].toLowerCase()', ci); const classifyGuarded = ci >= 0 && cGuard > ci && cGuard < cRead; // 3. core executePipeChain: skip empty stages without shifting the index (v4.6.131 replaced the // `.filter(argv => argv.length > 0)` with a lastStage scan so stages[i] stays aligned with // pipeParts[i]), and forward each stage's raw text to the dispatcher. const pi = src.indexOf('async executePipeChain('); const body = pi >= 0 ? src.slice(pi, src.indexOf('async capturePipeOutput(', pi)) : ''; const pEmpty = body.indexOf('if (lastStage < 0)'); const pSkip = body.indexOf('if (argv.length === 0) continue'); const pRead = body.indexOf('argv[0].toLowerCase()'); const pRaw = body.indexOf('dispatchCommand(cmd, args, pipeParts[i])'); const pipeGuarded = pi >= 0 && pEmpty > 0 && pSkip > pEmpty && pRead > pSkip && pRaw > pRead && body.indexOf('this.capturePipeOutput(cmd, args, pipeParts[i])') > 0; // 4. the override that actually runs: ShellModule.init() replaces terminal.executePipeChain, so a // fix in the core alone is dead code. Measured live: the wrapper is the shell's. const si = shellSrc.indexOf('this.terminal.executePipeChain = async function(pipeParts)'); const shellBody = si >= 0 ? shellSrc.slice(si, shellSrc.indexOf('_splitSequence(', si)) : ''; const shellForwardsRaw = si >= 0 && shellBody.indexOf('this.dispatchCommand(cmd, args, cmdText)') > 0 && shellBody.indexOf('this.capturePipeOutput(cmd, args, cmdText)') > 0; // 5. v4.6.132: that override must tokenise with the quote-aware parser, not split(/\s+/), and must // skip empty stages instead of dispatching cmd ''. const shellTokensQuoted = si >= 0 && shellBody.indexOf('this.security._parseArgs(String(cmdText') > 0 && shellBody.indexOf('if (argv.length === 0) continue') > 0 && shellBody.indexOf('cmdText.split(') < 0; const report = { empty, blank, classifyGuarded, pipeGuarded, shellForwardsRaw, shellTokensQuoted }; console.log(JSON.stringify(report)); const ok = Array.isArray(empty) && empty.length === 0 && Array.isArray(blank) && blank.length === 0 && classifyGuarded && pipeGuarded && shellForwardsRaw && shellTokensQuoted; process.exit(ok ? 0 : 1);