← Files AICtrlARCHIVED FILE

skills/ux-testing/scripts/ux.cjs

2.46 KB · Nov 29, 2026 · 00:25 UTC

↓ Download file

See the change to this file →

// Tester client: node ux.cjs <session> <action> [args...]
// Actions: open · snapshot · click <name> · type [--field "<label>"] <text> [--enter] · select "<label>" <option>
//          · press <key> · wait <ms> · screenshot
// Operator only (the harness requires the verify token; run with UX_VERIFY_TOKEN set, never in a tester brief):
// "open <scenario> [--viewport WxH]" (start a tester mid-flow or at another size; testers run plain "open"),
// re-opening a live session, "errors" (page errors in this session), "close" (end a finished session).
// Exits non-zero when the harness is unreachable or answers with an ERROR, so shell && chains stop.
// Never calls process.exit(): it sets the exit code and returns, so piped stderr is always flushed.
const fs = require('fs');
const http = require('http');
const { tokenFile } = require('./common.cjs');

function fail(message) {
  console.error(message);
  process.exitCode = 1;
}

function main() {
  const port = Number(process.env.UX_PORT || 3917);
  const [session, action, ...args] = process.argv.slice(2);
  if (!session || !action) return fail('Usage: node ux.cjs <session> <action> [args...]');

  let token;
  try {
    token = fs.readFileSync(tokenFile(port), 'utf8').trim();
  } catch {
    return fail(`ERROR: harness not running on port ${port} (no token file).`);
  }

  let viewport;
  const vi = action === 'open' ? args.indexOf('--viewport') : -1;
  if (vi !== -1) {
    const m = /^(\d+)x(\d+)$/.exec(args[vi + 1] || '');
    if (!m) return fail('ERROR: --viewport needs WIDTHxHEIGHT, e.g. 390x844 (phone) or 1366x768 (laptop).');
    viewport = { width: Number(m[1]), height: Number(m[2]) };
    args.splice(vi, 2);
  }
  if (action === 'open' && args.length > 1) {
    console.error(`warning: "open" takes one scenario; ignoring: ${args.slice(1).join(' ')}`);
  }
  const body = JSON.stringify(
    action === 'open' ? { session, action, scenario: args[0], viewport } : { session, action, args },
  );
  const req = http.request(
    {
      host: '127.0.0.1', port, method: 'POST', path: '/',
      headers: { 'x-ux-token': token, ...(process.env.UX_VERIFY_TOKEN ? { 'x-ux-verify-token': process.env.UX_VERIFY_TOKEN } : {}) },
    },
    (res) => {
      let d = '';
      res.on('data', (c) => (d += c));
      res.on('end', () => {
        if (d.startsWith('ERROR')) fail(d);
        else console.log(d);
      });
    },
  );
  req.on('error', (e) => fail('ERROR: harness not reachable: ' + e.message));
  req.end(body);
}

main();

SHA-256: 6616867206ba539ae7f3218925b33b9077ca9878eaae2951d0c2c4d82c93fd92