Files
pbakaus_impeccable/tests/cli-args.test.mjs
T
Paul BakausandClaude 4e381305e1 Fix source-safety, detector, and lock defects in Live polling work
Addresses the review findings on #371, plus several the bots did not catch.
All fixes have regression coverage that fails on the prior code.

Source corruption:
- Vue accept dropped valueless root attrs (disabled, v-cloak) and, worse,
  rewrote @click="x" as a literal click="x" DOM attribute, because the attr
  parser was name-anchored and skipped the sigil. Tokenize the whole Vue attr
  grammar and normalize shorthands so accept round-trips directives.
- --variant was interpolated unescaped into a RegExp, so --variant '.*' matched
  the original block first and reported a successful accept while silently
  restoring the original. Validate against the digits pattern the browser and
  the /events schema already enforce.
- --id reached path.join unvalidated, so --id ../../../../etc/evil wrote and
  read receipts outside the project. Hoist the existing safeSessionId check
  into impeccable-paths and apply it at every id-to-path sink.

Accept/lock correctness:
- Plain HTML/JSX accept and discard did not catch SOURCE_LOCKED, so contention
  exited non-zero with empty stdout and the agent got no JSON to retry on.
- Lock staleness was mtime-only and never read the pid it records: a holder
  whose critical section outran 60s had its live lock swept, admitting a second
  writer to the same file, while a crashed holder blocked accepts for a full
  60s. Decide staleness by owner liveness, and release only our own lock.

Detector:
- isNeutralColor only parses computed color forms, so routing authored CSS
  through it reported inset 4px 0 0 #000 / black / #e5e7eb as chromatic
  side-tab stripes. Add an authored-color neutrality test covering hex and
  named neutrals; the fixture had no literal-color cases at all.
- Rule line numbers were off by one for every rule after the first, and
  commented-out CSS was scanned as live rules.

Server:
- An error reply carries no sourceEventType, and inferSourceEventType returned
  undefined, which acknowledgePendingEvent treats as a wildcard: a stale
  generate worker's failure consumed the user's queued Accept, which then
  reached no agent and left the browser in SAVING forever.
- The generate preflight spawned live-wrap.mjs synchronously inside the request
  handler, freezing the single-threaded server for the whole scaffold (~7.6s
  measured on this repo, 15s ceiling) and stalling Accept/Discard/SSE. Make it
  async, claiming the lease before the first await so no event double-delivers.
- Every browser checkpoint was echoed back as variant_progress, so a Tune
  slider drag remounted the preview under the user's cursor and latched the
  *_reviewable phases from the wrong trigger. Gate on the reason.

Cleanup:
- Collapse four divergent benchmark argv parsers into scripts/lib/cli-args.mjs.
  Three silently misread flags: --iterations 20 benchmarked 5, --agent llm ran
  the fake agent, --median-target=0.4 used the default threshold.
- Drop a snapshot cache this branch made write-only (it grew per session for
  the server's lifetime and was never read), a dead exported reconcile helper,
  and the unused deferReply branch.

Prepared with AI assistance under maintainer direction.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-17 13:48:44 -07:00

102 lines
3.5 KiB
JavaScript

/**
* Tests for scripts/lib/cli-args.mjs — the shared argv parser for the Live
* benchmark / judging scripts.
* Run with: node --test tests/cli-args.test.mjs
*/
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { boolFlag, parseArgs, positiveIntFlag, toCamel } from '../scripts/lib/cli-args.mjs';
describe('parseArgs', () => {
it('reads space-separated values', () => {
// The regression: without the argv[i+1] lookahead this yielded
// {fixture: true, iterations: true}, silently benchmarking the defaults.
assert.deepEqual(
parseArgs(['--fixture', 'vite8-react-modal', '--iterations', '20']),
{ fixture: 'vite8-react-modal', iterations: '20' },
);
});
it('reads --flag=value values', () => {
assert.deepEqual(parseArgs(['--fixture=vite8-react-plain']), { fixture: 'vite8-react-plain' });
});
it('treats a flag followed by another flag as boolean', () => {
assert.deepEqual(parseArgs(['--headed', '--quiet']), { headed: true, quiet: true });
});
it('treats a trailing flag as boolean', () => {
assert.deepEqual(parseArgs(['--append']), { append: true });
});
it('camel-cases kebab keys so both spellings land on one key', () => {
assert.deepEqual(parseArgs(['--simulated-tail-ms=250']), { simulatedTailMs: '250' });
assert.deepEqual(parseArgs(['--simulatedTailMs=250']), { simulatedTailMs: '250' });
assert.deepEqual(parseArgs(['--median-target', '0.4']), { medianTarget: '0.4' });
});
it('keeps a value that contains an equals sign intact', () => {
assert.deepEqual(parseArgs(['--model=claude-sonnet-4-6=x']), { model: 'claude-sonnet-4-6=x' });
});
it('ignores positional args and a bare --', () => {
assert.deepEqual(parseArgs(['positional', '--', '--real', 'v']), { real: 'v' });
});
it('lets a later occurrence win', () => {
assert.deepEqual(parseArgs(['--agent', 'fake', '--agent', 'llm']), { agent: 'llm' });
});
});
describe('toCamel', () => {
it('upcases after hyphens only', () => {
assert.equal(toCamel('simulated-tail-ms'), 'simulatedTailMs');
assert.equal(toCamel('p95-target'), 'p95Target');
assert.equal(toCamel('already'), 'already');
});
});
describe('boolFlag', () => {
it('accepts the bare-flag sentinel and the explicit spellings alike', () => {
// --headed and --headed=true must not diverge.
assert.equal(boolFlag(true), true);
assert.equal(boolFlag('true'), true);
assert.equal(boolFlag('1'), true);
assert.equal(boolFlag('yes'), true);
assert.equal(boolFlag(''), true);
});
it('recognizes negative spellings', () => {
assert.equal(boolFlag('false'), false);
assert.equal(boolFlag('0'), false);
assert.equal(boolFlag('no'), false);
});
it('falls back when absent or unrecognized', () => {
assert.equal(boolFlag(undefined), false);
assert.equal(boolFlag(undefined, true), true);
assert.equal(boolFlag('maybe', true), true);
});
});
describe('positiveIntFlag', () => {
it('parses positive integers', () => {
assert.equal(positiveIntFlag('20', 5), 20);
});
it('falls back when absent or given as a bare flag', () => {
assert.equal(positiveIntFlag(undefined, 5), 5);
assert.equal(positiveIntFlag(true, 5), 5);
});
it('throws rather than silently using the default', () => {
// Quietly benchmarking 5 iterations when 20 were asked for is the failure
// this replaces.
for (const bad of ['abc', '0', '-3', '2.5', '20x']) {
assert.throws(() => positiveIntFlag(bad, 5), /positive integer/, `accepted ${bad}`);
}
});
});