Files
Codeman/test/test-env-isolation.test.ts
Codeman maintainer 92af855ce4 fix(base-path): keep the crash beacon under the mount, strip CODEMAN_BASE_URL in tests, add the wiring test
The merge-time items from the #381 review. navigator.sendBeacon is not fetch,
so the base-aware wrapper never saw the two crash-diag beacons and a sub-path
install posted them to the origin root every two seconds. The test suite now
strips CODEMAN_BASE_URL like CODEMAN_GESTURE, since the constructor reads it
as a fallback and an operator who exports it would see the root-install
byte-identity assertions fail. test/base-path-server.test.ts boots a real
WebServer under /codeman and checks the ingress strip, the base injection,
the rebased redirects, the 404 envelope and a prefixed WebSocket upgrade.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-06 23:11:01 +02:00

75 lines
4.1 KiB
TypeScript

/**
* @fileoverview Pins the environment isolation `test/setup.ts` provides.
*
* The suite's hermeticity rests on a temp `HOME` plus a short list of env vars that are
* deleted before any application module loads. That list is easy to under-maintain: it grew
* once for auth (`CODEMAN_PASSWORD`/`CODEMAN_USERNAME`) and once for `CODEMAN_GESTURE`, both
* times only after a leak had already produced a confusing failure, and it was still missing
* the three INSTANCE-selection vars.
*
* Those three matter more than the ones already on the list, because `src/config/instance.ts`
* derives BOTH the data dir and the tmux socket from them, and `CODEMAN_DATA_DIR` is an
* absolute path that bypasses `HOME` entirely — so a developer who exports it has the suite
* reading and writing their real `state.json` rather than a throwaway tree.
*
* ⚠️ The runtime half of this file cannot fail on a machine where the vars were never set, so
* it is not enough on its own: a `delete` line removed from `setup.ts` would still pass here
* on almost every developer's box and on CI. The STATIC half is what actually guards the
* list — it reads `setup.ts` and asserts each name is deleted there, which fails wherever the
* suite runs. Both halves are deliberate; do not drop the static one as redundant.
*
* Port: none (pure, over process.env and one source file).
*/
import { describe, expect, it } from 'vitest';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
/** Every env var `setup.ts` must strip, with why it would otherwise leak. */
const STRIPPED_ENV_VARS: Array<[name: string, why: string]> = [
['CODEMAN_PASSWORD', 'auth from a running instance would make protected routes behave differently'],
['CODEMAN_USERNAME', 'same, and it changes which owner scoping resolves to'],
['CODEMAN_GESTURE', 'flips renderIndexHtml output and breaks byte-identity assertions'],
['CODEMAN_BASE_URL', 'mounts the server under a sub-path and breaks the root-install byte-identity assertions'],
['CODEMAN_INSTANCE', 'moves the data dir to ~/.codeman-<name> and the tmux socket to codeman-<name>'],
['CODEMAN_DATA_DIR', 'ABSOLUTE override: bypasses the temp HOME and points the suite at a real data dir'],
['CODEMAN_TMUX_SOCKET', 'renames the socket resolveTmuxSocketName() returns'],
];
const SETUP_SOURCE = readFileSync(fileURLToPath(new URL('./setup.ts', import.meta.url)), 'utf-8');
/**
* Just the top-of-file STRIP section, cut at the first hook.
*
* The teardown below it restores HOME/USERPROFILE/VITEST/PLAYWRIGHT_BROWSERS_PATH with the
* same `delete` syntax, and those are the opposite of a strip — counting them would make the
* anti-drift check demand a reason for a var the suite deliberately puts back.
*/
const SETUP_STRIP_SECTION = SETUP_SOURCE.split(/^afterEach\(/m)[0];
describe('test environment isolation', () => {
it.each(STRIPPED_ENV_VARS)('%s is unset while the suite runs', (name) => {
expect(process.env[name], `${name} leaked into the test environment`).toBeUndefined();
});
it.each(STRIPPED_ENV_VARS)('setup.ts deletes %s (%s)', (name) => {
// The half that fails everywhere, not just on a machine that happens to export the var.
expect(SETUP_SOURCE, `setup.ts no longer deletes ${name}`).toContain(`delete process.env.${name};`);
});
it('runs against a throwaway HOME, not the real one', () => {
// The property every other test's isolation is built on: `~/.codeman` and `~/codeman-cases`
// both resolve under here, so a test that writes state cannot reach the developer's own.
const home = process.env.HOME ?? process.env.USERPROFILE;
expect(home).toBeTruthy();
expect(home).toContain('codeman-vitest-');
});
it('lists every name the setup file strips (anti-drift)', () => {
// Catches the other direction: a var added to setup.ts but never given a reason here, so
// the next person cannot tell whether it is load-bearing or left over.
const deleted = [...SETUP_STRIP_SECTION.matchAll(/delete process\.env\.([A-Z0-9_]+);/g)].map((m) => m[1]).sort();
expect(deleted).toEqual(STRIPPED_ENV_VARS.map(([name]) => name).sort());
});
});