fix(gates): the shim reads a bare --gates and closed as on, and never swallows the next flag
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
8f9b0abfad
commit
e2fcf4060c
2 changed files with 68 additions and 5 deletions
|
|
@ -16,7 +16,9 @@
|
|||
// finish — pipeline reached its end
|
||||
//
|
||||
// CLI shim:
|
||||
// node lib/util/autonomy-gate.mjs --state X --event Y [--gates true|false]
|
||||
// node lib/util/autonomy-gate.mjs --state X --event Y [--gates [true|false|closed|open]]
|
||||
// (bare --gates = on; true|closed = on, false|open = off; any other value
|
||||
// is a usage error, exit 1; case-sensitive)
|
||||
// → JSON: { ok: true, next_state: "..." } (success)
|
||||
// → JSON: { ok: false, error: "..." } (invalid transition; exit 1)
|
||||
//
|
||||
|
|
@ -109,8 +111,22 @@ function parseArgs(argv) {
|
|||
if (a === '--state') out.state = argv[++i];
|
||||
else if (a === '--event') out.event = argv[++i];
|
||||
else if (a === '--gates') {
|
||||
const v = argv[++i];
|
||||
out.gates = v === 'true';
|
||||
// Bare `--gates` (last, or followed by another flag) means on and
|
||||
// consumes nothing. A value is case-sensitive: true|closed = on,
|
||||
// false|open = off; anything else is a usage error.
|
||||
const v = argv[i + 1];
|
||||
if (v === undefined || v.startsWith('--')) {
|
||||
out.gates = true;
|
||||
} else if (v === 'true' || v === 'closed') {
|
||||
out.gates = true;
|
||||
i++;
|
||||
} else if (v === 'false' || v === 'open') {
|
||||
out.gates = false;
|
||||
i++;
|
||||
} else {
|
||||
out.error = 'usage: --gates takes true, false, closed or open';
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
|
|
@ -118,10 +134,11 @@ function parseArgs(argv) {
|
|||
|
||||
if (isMain(import.meta.url)) {
|
||||
const args = parseArgs(process.argv.slice(2));
|
||||
if (!args.state || !args.event) {
|
||||
if (args.error || !args.state || !args.event) {
|
||||
process.stdout.write(JSON.stringify({
|
||||
ok: false,
|
||||
error: 'usage: autonomy-gate.mjs --state <state> --event <event> [--gates true|false]',
|
||||
error: args.error
|
||||
|| 'usage: autonomy-gate.mjs --state <state> --event <event> [--gates [true|false|closed|open]]',
|
||||
}) + '\n');
|
||||
process.exit(1);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -145,3 +145,49 @@ test('CLI shim missing required args returns usage error (exit 1)', () => {
|
|||
assert.equal(parsed.ok, false);
|
||||
assert.match(parsed.error, /usage:/);
|
||||
});
|
||||
|
||||
// ---- --gates value rule (fix round 4.5a, #1) ---------------------------------
|
||||
// The shim is tested only through its CLI; the exit code is asserted before
|
||||
// any JSON.parse, so a crash or a usage error never reads as a wrong value.
|
||||
|
||||
function shimState(args) {
|
||||
const r = runShim(args);
|
||||
assert.equal(r.code, 0, `expected exit 0 for ${JSON.stringify(args)}, got ${r.code}: ${r.out}`);
|
||||
const parsed = JSON.parse(r.out.trim());
|
||||
assert.equal(parsed.ok, true);
|
||||
return parsed.next_state;
|
||||
}
|
||||
|
||||
test('[#1] bare --gates as the last argument → gates_on', () => {
|
||||
assert.equal(shimState(['--state', 'idle', '--event', 'start', '--gates']), 'gates_on');
|
||||
});
|
||||
|
||||
test('[#1] bare --gates before other flags swallows none of them → exit 0, gates_on', () => {
|
||||
assert.equal(shimState(['--gates', '--state', 'idle', '--event', 'start']), 'gates_on');
|
||||
});
|
||||
|
||||
test('[#1] --gates closed → gates_on', () => {
|
||||
assert.equal(shimState(['--state', 'idle', '--event', 'start', '--gates', 'closed']), 'gates_on');
|
||||
});
|
||||
|
||||
for (const bad of ['TRUE', 'yes', 'adaptive']) {
|
||||
test(`[#1] --gates ${bad} → usage error (exit 1)`, () => {
|
||||
const r = runShim(['--state', 'idle', '--event', 'start', '--gates', bad]);
|
||||
assert.equal(r.code, 1, `expected exit 1 for --gates ${bad}, got ${r.code}: ${r.out}`);
|
||||
const parsed = JSON.parse(r.out.trim());
|
||||
assert.equal(parsed.ok, false);
|
||||
assert.match(parsed.error, /usage/);
|
||||
});
|
||||
}
|
||||
|
||||
test('--gates true → gates_on', () => {
|
||||
assert.equal(shimState(['--state', 'idle', '--event', 'start', '--gates', 'true']), 'gates_on');
|
||||
});
|
||||
|
||||
test('--gates false → auto_running', () => {
|
||||
assert.equal(shimState(['--state', 'idle', '--event', 'start', '--gates', 'false']), 'auto_running');
|
||||
});
|
||||
|
||||
test('--gates open → auto_running', () => {
|
||||
assert.equal(shimState(['--state', 'idle', '--event', 'start', '--gates', 'open']), 'auto_running');
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue