diff options
| author | Agando <roduccomm@gmail.com> | 2026-10-29 12:53:41 +0100 |
|---|---|---|
| committer | Agando <roduccomm@gmail.com> | 2026-10-29 12:53:41 +0100 |
| commit | 6daf236b585dc47ad8ffe293cdefd5de8231c5d8 (patch) | |
| tree | df2f7c72def23f4664090689c3a9f54f28d5cb69 /modules/test | |
| parent | 1a27ba0e2ec736aa9b30c69a3aa2ee2ab86d0709 (diff) | |
feat(newos4): register `test` / `[` so chains decide locally instead of asking a model (v4.6.135)
`[ -f "/qt.txt" ] && echo exists` — the probe that closed the v4.6.132 batch — was not a shell test
at all: both commands were unregistered, so the line fell through to the AI fallback and a model was
asked whether a file exists. `_evalTest()` has implemented `-f -d -e -z -n ! = !=` for the if/while
parser since the script runner was written; only the command was missing.
* `test` and `[` registered in ShellModule. Success resolves silently; false rejects with a
`silent` error so `&&`/`||` skip it — POSIX prints nothing on exit 1.
* `[` requires the closing bracket (`[ : missing ']'`), and that message is NOT silent.
* `test` already names the on-demand regression-suite runner ("test auto" / "test list" in
help.html). A argv that does not look like a test expression (`-f`/`-d`/`-e`/`-z`/`-n`/`!`,
or `a = b` / `a != b`) is delegated to the handler registered before us, so `test auto`
still runs the suite and the two meanings cannot collide.
* terminal.dispatchCommand() now honours `err.silent` — without it a top-level `test -f /nope`
printed `Error in ShellModule.test:` (measured on v4.6.134, fixed in v4.6.135).
* help.html documents the bracket form next to the existing `test` row.
Three tags were minted minutes apart — v4.6.133 (commands), v4.6.134 (suite delegation),
v4.6.135 (silent top-level) — each a real cache-bust for a fix that superseded the previous build;
the net change is this commit.
Verification
node modules/test/test-shell-test-cmd.js -> 24/24 pass (RED against HEAD: "test command
registered" fails, "m.commands.test is not a function")
node modules/test/test-pipe-guard.js -> all seven flags true
node modules/test/test-parse-args.js -> 28/28 · daily job -> node tests 22/22 pass
newos4-maintain.py -> atlas 355 commands (was 354), modules-index 349, help 364/364
live v4.6.135, read from the terminal DOM:
[ -f /qt.txt ] && echo F135 -> F135
test -f /nope || echo M135 -> M135
[ -d /nope ] && echo BAD || echo N134-> N134 (false branch skipped, nothing printed)
test -> no output (suite not loaded; POSIX false)
test -f /nope -> no output
js "a b".length | wc -c -> 1 (v4.6.132 pipe fix still intact)
The AI fallback is no longer reachable for a bracketed file test.
Diffstat (limited to 'modules/test')
| -rw-r--r-- | modules/test/test-shell-test-cmd.js | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/modules/test/test-shell-test-cmd.js b/modules/test/test-shell-test-cmd.js new file mode 100644 index 0000000..6abdd11 --- /dev/null +++ b/modules/test/test-shell-test-cmd.js @@ -0,0 +1,98 @@ +// node modules/test/test-shell-test-cmd.js +// +// `test` / `[` were not registered, so `[ -f /x ] && echo yes` fell through to the AI fallback and +// asked a model whether a file exists (measured 2026-10-29 on v4.6.132). _evalTest() already had the +// operators; the command was missing. Contract pinned here: +// success -> resolves silently +// false -> rejects with a `silent` error (so `&&`/`||` skip without printing "Error: ") +// `[` -> requires the closing bracket, never delegates +// `test` -> a non-POSIX argv still reaches the regression-suite runner registered before it +const fs = require('fs'), path = require('path'); +// shell.js ends with `if (typeof window.webx11 !== 'undefined' && window.webx11) { … }` — and +// `typeof window.webx11` throws when `window` itself is undeclared, so give it an empty object. +global.window = {}; +const src = fs.readFileSync(path.join(__dirname, '../ext/shell.js'), 'utf8'); +const ShellModule = new Function(src + '\n;return typeof ShellModule !== "undefined" ? ShellModule : null;')(); + +let pass = 0, fail = 0; +const ok = (name, cond, detail) => { cond ? pass++ : (fail++, console.log('FAIL ' + name + (detail ? ' — ' + detail : ''))); }; +const eq = (name, got, want) => ok(name, JSON.stringify(got) === JSON.stringify(want), 'got ' + JSON.stringify(got) + ' want ' + JSON.stringify(want)); + +function mk(filesArg) { + const printed = []; + const files = filesArg || { '/a.txt': 'x', '/dir': null }; // null => directory + const term = { + print: (m, lvl) => printed.push({ m: String(m), lvl: lvl || 'plain' }), + markDirty: () => {}, + fs: { + currentPath: [], + readFile: p => (typeof files[p] === 'string' ? files[p] : null), + readDir: p => (p in files && files[p] === null ? [] : null) + }, + // A real (simple) parser: the assertions prove _tokenize delegates by counting the calls. + security: { calls: 0, _parseArgs: function (s) { this.calls++; return s.trim() ? s.trim().split(/\s+/) : []; } } + }; + const m = new ShellModule(); + m.terminal = term; + m.registerCommands(); + return { m, term, printed }; +} + +const throws = async (fn) => { try { await fn(); return null; } catch (e) { return e; } }; + +(async () => { + const { m, term, printed } = mk(); + + ok('test command registered', typeof m.commands['test'] === 'function'); + ok('[ command registered', typeof m.commands['['] === 'function'); + + ok('test -f existing file -> resolves', await throws(() => m.commands['test'](['-f', '/a.txt'])) === null); + + const missing = await throws(() => m.commands['test'](['-f', '/nope'])); + ok('test -f missing file -> rejects', !!missing); + ok('rejection is silent', !!missing && missing.silent === true, 'silent=' + (missing && missing.silent)); + ok('rejection carries no message', !!missing && !missing.message, 'msg=' + JSON.stringify(missing && missing.message)); + + ok('[ -f /a.txt ] -> resolves', await throws(() => m.commands['['](['-f', '/a.txt', ']'])) === null); + + const noBracket = await throws(() => m.commands['['](['-f', '/a.txt'])); + ok('[ without ] -> rejects', !!noBracket && /missing '\]'/.test(noBracket.message), noBracket && noBracket.message); + ok('[ without ] is NOT silent (a syntax error must be visible)', !!noBracket && !noBracket.silent); + + ok('test -d dir -> resolves', await throws(() => m.commands['test'](['-d', '/dir'])) === null); + ok('test -d file -> rejects', !!(await throws(() => m.commands['test'](['-d', '/a.txt'])))); + + const noExpr = await throws(() => m.commands['test']([])); + ok('test with no fallback and no args -> silent reject (POSIX false)', !!noExpr && noExpr.silent === true); + + // shared printer: silent errors print nothing, real ones print "Error: …" + printed.length = 0; + m._printErr(term, Object.assign(new Error(''), { silent: true })); + ok('_printErr swallows silent errors', printed.length === 0, JSON.stringify(printed)); + m._printErr(term, new Error('boom')); + eq('_printErr prints real errors', printed, [{ m: 'Error: boom', lvl: 'error' }]); + + // _tokenize must delegate to the security parser, never to split(/\s+/) of its own + const before = term.security.calls; + eq('_tokenize delegates to security._parseArgs', m._tokenize('anything at all'), ['anything', 'at', 'all']); + ok('_tokenize actually called the parser', term.security.calls > before, 'calls=' + term.security.calls); + eq('_tokenize empty -> []', m._tokenize(' '), []); + eq('_tokenize undefined -> []', m._tokenize(undefined), []); + + // `test` also names the on-demand regression-suite runner ("test auto" / "test list"). + const { m: d } = mk(); + const seen = []; + const prev = d.commands['test']; + d.commands['test'] = (args) => d._cmdTest(args, 'test', (a) => { seen.push(a); return 'suite'; }); + ok('previous handler is what delegation falls back to', typeof prev === 'function'); + + ok('test auto delegates to the suite runner', (await d.commands['test'](['auto'])) === 'suite' && JSON.stringify(seen) === '[["auto"]]', JSON.stringify(seen)); + ok('bare test delegates to the suite runner', (await d.commands['test']([])) === 'suite' && seen.length === 2, JSON.stringify(seen)); + ok('test -f still runs POSIX (no delegation)', (await throws(() => d.commands['test'](['-f', '/a.txt']))) === null && seen.length === 2, 'seen=' + JSON.stringify(seen)); + ok('string equality form runs POSIX', (await throws(() => d.commands['test'](['/a.txt', '=', '/a.txt']))) === null && seen.length === 2, 'seen=' + JSON.stringify(seen)); + const t2 = await throws(() => d.commands['['](['-f', '/nope', ']'])); + ok('[ never delegates', !!t2 && t2.silent === true, JSON.stringify(seen)); + + console.log(pass + '/' + (pass + fail) + ' pass'); + process.exit(fail ? 1 : 0); +})(); |