aboutsummaryrefslogtreecommitdiff
path: root/modules/test/test-pipe-guard.js
blob: e689e603ec74cedba52a8b75a8dbf1a5be9fc2c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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);