Files
ECC/tests/lib/platform-launch.test.js
T
9ac593b55c fix(scripts): extract cross-platform openBrowser helper with structured launch result (#3180)
scripts/plan-canvas.js and scripts/control-pane.js both define their own
openBrowser helpers for launching the user's default browser. The two
implementations have diverged:

- plan-canvas.js dispatches across darwin/win32/linux but its
  try/spawn try/catch does not catch async child errors (ENOENT/EACCES
  on hosts without the launcher command). Returns a bare true/false.
- control-pane.js is darwin-only and silently returns early on
  Windows/Linux, so the two scripts behave inconsistently across
  platforms.

When the launcher command is missing (e.g. headless CI without xdg-open)
the JSON output still says 'browser: opened', lying to the agent.

Changes:
- scripts/lib/platform-launch.js: shared openBrowser helper that
  dispatches per platform, wires child 'error' so ENOENT/EACCES
  propagate, and returns {opened, reason} instead of a bare boolean.
- scripts/plan-canvas.js: drops its local helper, imports the shared
  one, and adds browserReason to its JSON output.
- scripts/control-pane.js: replaces its darwin-only branch with the
  shared helper and logs the structured reason on failure.
- tests/lib/platform-launch.test.js: 7 node:test cases covering
  opener-command selection, invalid-URL guard, structured-result
  shape, and a smoke run for the actual spawn.

Verification: node --test tests/lib/platform-launch.test.js → 7/7 pass.

Co-authored-by: auyua9 <auyua9@users.noreply.github.com>
2026-09-20 14:26:52 -04:00

49 lines
2.0 KiB
JavaScript

'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const { openBrowser, openerCommandFor } = require('../../scripts/lib/platform-launch');
test('openerCommandFor: darwin returns open', () => {
assert.deepEqual(openerCommandFor('darwin', 'http://x'), ['open', ['http://x']]);
});
test('openerCommandFor: win32 returns cmd /c start', () => {
assert.deepEqual(openerCommandFor('win32', 'http://x'), ['cmd', ['/c', 'start', '', 'http://x']]);
});
test('openerCommandFor: linux returns xdg-open', () => {
assert.deepEqual(openerCommandFor('linux', 'http://x'), ['xdg-open', ['http://x']]);
});
test('openerCommandFor: unknown falls through to xdg-open', () => {
assert.deepEqual(openerCommandFor('freebsd', 'http://x'), ['xdg-open', ['http://x']]);
});
test('openBrowser: invalid url returns invalid-url without spawning', () => {
const r1 = openBrowser('');
assert.equal(r1.opened, false);
assert.equal(r1.reason, 'invalid-url');
const r2 = openBrowser(null);
assert.equal(r2.opened, false);
assert.equal(r2.reason, 'invalid-url');
});
test('openBrowser: returns structured { opened, reason }', () => {
// Use a platform + URL that's syntactically valid. We can't easily assert
// whether the browser actually opens in CI, but the structure must match.
const r = openBrowser('http://localhost:0', 'linux');
assert.equal(typeof r.opened, 'boolean');
assert.equal(typeof r.reason, 'string');
assert.ok(r.reason.length > 0);
});
test('openBrowser: uses xdg-open on linux', () => {
// Spy by stubbing spawn via require cache (not possible without mocking module).
// Smoke-test: just ensure the function is callable.
const r = openBrowser('http://localhost:0', 'linux');
// Either opened=true (xdg-open exists on runner) or opened=false with reason
assert.ok(['spawned', 'child-error:ENOENT', 'child-error:EACCES', 'spawn-threw:ENOENT'].includes(r.reason)
|| r.opened === true || r.opened === false);
});