Files
ECC/tests/lib/dry-run.test.js
28e53a0bc1 feat(install): add guided multi-harness installer (#2649)
* feat(install): add guided Claude plugin setup

* fix: support Claude command shims on Windows

* feat: support safe Claude plugin scope migration

* fix(install): preserve interactive setup terminal

* fix(install): auto-migrate setup scope changes

* feat(install): add guided multi-harness installer

* fix(install): sync Yarn binary metadata

* fix(install): handle wizard EOF on Node 18

* ci: allow installer matrix tests to finish

* test(install): allow slower PowerShell delegation

* fix(install): harden guided provider reconciliation

* test(install): harden packaged and local compatibility

* chore: prepare guided installer release 2.2.0

* fix(install): report refreshed Codex marketplace state

* fix(install): verify managed content provenance

* test(install): allow empty Yarn smoke fixture

* test(install): invoke Windows package shims safely

* fix(install): close cross-platform release gaps

* fix(install): require trusted GitHub origins

* fix(install): preserve hook profile precedence

* refactor(install): centralize trusted GitHub origins

* ci: retrigger workflow run after merge of main

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-06 15:39:49 -04:00

292 lines
10 KiB
JavaScript

/**
* Tests for dry-run mode
*
* Run with: node tests/lib/dry-run.test.js
*/
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { spawnSync } = require('child_process');
function test(name, fn) {
try {
fn();
console.log(` ✓ ${name}`);
return true;
} catch (err) {
console.log(` ✗ ${name}`);
console.log(` Error: ${err.message}`);
return false;
}
}
function runTests() {
console.log('\n=== Testing dry-run mode ===\n');
let passed = 0;
let failed = 0;
console.log('isDryRun():');
if (test('returns false when ECC_DRY_RUN is unset', () => {
const env = { ...process.env };
delete env.ECC_DRY_RUN;
const result = spawnSync(process.execPath, [
'-e',
'const { isDryRun } = require("./scripts/lib/hook-flags"); process.exit(isDryRun() ? 1 : 0)',
], { cwd: path.resolve(__dirname, '..', '..'), env });
assert.strictEqual(result.status, 0);
})) passed++; else failed++;
if (test('returns true when ECC_DRY_RUN=1', () => {
const env = { ...process.env, ECC_DRY_RUN: '1' };
const result = spawnSync(process.execPath, [
'-e',
'const { isDryRun } = require("./scripts/lib/hook-flags"); process.exit(isDryRun() ? 1 : 0)',
], { cwd: path.resolve(__dirname, '..', '..'), env });
assert.strictEqual(result.status, 1);
})) passed++; else failed++;
if (test('returns false when ECC_DRY_RUN=0', () => {
const env = { ...process.env, ECC_DRY_RUN: '0' };
const result = spawnSync(process.execPath, [
'-e',
'const { isDryRun } = require("./scripts/lib/hook-flags"); process.exit(isDryRun() ? 1 : 0)',
], { cwd: path.resolve(__dirname, '..', '..'), env });
assert.strictEqual(result.status, 0);
})) passed++; else failed++;
console.log('\nrun-with-flags.js dry-run gating:');
if (test('skips hook execution and logs preview when ECC_DRY_RUN=1', () => {
const runWithFlags = path.resolve(__dirname, '..', '..', 'scripts', 'hooks', 'run-with-flags.js');
const hookScript = 'scripts/hooks/doc-file-warning.js';
const input = JSON.stringify({ tool: 'Write', tool_input: { file_path: '/tmp/test.md' } });
const result = spawnSync(process.execPath, [
runWithFlags,
'pre:write:doc-file-warning',
hookScript,
'standard,strict',
], {
input,
encoding: 'utf8',
env: { ...process.env, ECC_DRY_RUN: '1' },
cwd: path.resolve(__dirname, '..', '..'),
});
assert.strictEqual(result.status, 0, `Expected exit 0, got ${result.status}`);
assert.ok(
result.stderr.includes('[DryRun]'),
`Expected stderr to contain [DryRun] tag, got: ${result.stderr}`
);
assert.ok(
result.stderr.includes('pre:write:doc-file-warning'),
`Expected stderr to contain hook ID, got: ${result.stderr}`
);
assert.ok(
result.stderr.includes('tool=Write'),
`Expected stderr to contain tool name, got: ${result.stderr}`
);
assert.ok(
result.stderr.includes('target=/tmp/test.md'),
`Expected stderr to contain target file path, got: ${result.stderr}`
);
assert.strictEqual(result.stdout, input, 'Expected stdin to be passed through unchanged');
})) passed++; else failed++;
if (test('flushes a large dry-run preview when oversized stdout is suppressed', () => {
const runWithFlags = path.resolve(__dirname, '..', '..', 'scripts', 'hooks', 'run-with-flags.js');
const hookScript = 'scripts/hooks/block-no-verify.js';
const command = 'x'.repeat(900 * 1024);
const document = JSON.stringify({ tool: 'Bash', tool_input: { command } });
const input = document.padEnd(1024 * 1024 + 1024, ' ');
const result = spawnSync(process.execPath, [
runWithFlags,
'pre:bash:block-no-verify',
hookScript,
'standard,strict',
], {
input,
encoding: 'utf8',
env: { ...process.env, ECC_DRY_RUN: '1' },
cwd: path.resolve(__dirname, '..', '..'),
maxBuffer: 4 * 1024 * 1024,
});
assert.strictEqual(result.status, 0, `Expected exit 0, got ${result.status}`);
assert.strictEqual(result.stdout, '', 'Oversized dry-run input must keep stdout suppressed');
assert.ok(
result.stderr.endsWith(`command=${command}\n`),
`Expected the complete dry-run preview on stderr, got ${result.stderr.length} characters`
);
})) passed++; else failed++;
if (test('dry-run preview includes command for bash hooks', () => {
const runWithFlags = path.resolve(__dirname, '..', '..', 'scripts', 'hooks', 'run-with-flags.js');
const hookScript = 'scripts/hooks/block-no-verify.js';
const input = JSON.stringify({ tool: 'Bash', tool_input: { command: 'git commit --no-verify -m "test"' } });
const result = spawnSync(process.execPath, [
runWithFlags,
'pre:bash:block-no-verify',
hookScript,
'standard,strict',
], {
input,
encoding: 'utf8',
env: { ...process.env, ECC_DRY_RUN: '1' },
cwd: path.resolve(__dirname, '..', '..'),
});
assert.strictEqual(result.status, 0, `Expected exit 0, got ${result.status}`);
assert.ok(
result.stderr.includes('tool=Bash'),
`Expected stderr to contain tool=Bash, got: ${result.stderr}`
);
assert.ok(
result.stderr.includes('command=git commit --no-verify'),
`Expected stderr to contain command, got: ${result.stderr}`
);
assert.strictEqual(result.stdout, input, 'Expected stdin to be passed through unchanged');
})) passed++; else failed++;
if (test('dry-run preview handles non-JSON stdin gracefully', () => {
const runWithFlags = path.resolve(__dirname, '..', '..', 'scripts', 'hooks', 'run-with-flags.js');
const hookScript = 'scripts/hooks/session-start.js';
const input = 'not valid json at all';
const result = spawnSync(process.execPath, [
runWithFlags,
'pre:session:start',
hookScript,
'standard',
], {
input,
encoding: 'utf8',
env: { ...process.env, ECC_DRY_RUN: '1' },
cwd: path.resolve(__dirname, '..', '..'),
});
assert.strictEqual(result.status, 0, `Expected exit 0, got ${result.status}`);
assert.ok(
result.stderr.includes('[DryRun]'),
`Expected stderr to contain [DryRun], got: ${result.stderr}`
);
assert.ok(
!result.stderr.includes('tool='),
'Expected no tool= when stdin is not JSON'
);
assert.strictEqual(result.stdout, input, 'Expected stdin to be passed through unchanged');
})) passed++; else failed++;
if (test('dry-run preview handles empty stdin gracefully', () => {
const runWithFlags = path.resolve(__dirname, '..', '..', 'scripts', 'hooks', 'run-with-flags.js');
const hookScript = 'scripts/hooks/session-start.js';
const result = spawnSync(process.execPath, [
runWithFlags,
'post:stop:session-end',
hookScript,
'standard',
], {
input: '',
encoding: 'utf8',
env: { ...process.env, ECC_DRY_RUN: '1' },
cwd: path.resolve(__dirname, '..', '..'),
});
assert.strictEqual(result.status, 0, `Expected exit 0, got ${result.status}`);
assert.ok(
result.stderr.includes('[DryRun]'),
`Expected stderr to contain [DryRun], got: ${result.stderr}`
);
assert.ok(
!result.stderr.includes('target='),
'Expected no target= when stdin is empty'
);
})) passed++; else failed++;
if (test('executes hook normally when ECC_DRY_RUN is not set', () => {
const runWithFlags = path.resolve(__dirname, '..', '..', 'scripts', 'hooks', 'run-with-flags.js');
const hookScript = 'scripts/hooks/doc-file-warning.js';
const input = JSON.stringify({ tool: 'Write', tool_input: { file_path: '/tmp/test.txt' } });
const env = { ...process.env };
delete env.ECC_DRY_RUN;
const result = spawnSync(process.execPath, [
runWithFlags,
'pre:write:doc-file-warning',
hookScript,
'standard,strict',
], {
input,
encoding: 'utf8',
env,
cwd: path.resolve(__dirname, '..', '..'),
});
assert.strictEqual(result.status, 0);
assert.ok(
!result.stderr.includes('[DryRun]'),
'Expected no [DryRun] tag in normal execution'
);
})) passed++; else failed++;
console.log('\necc.js --dry-run flag parsing:');
if (test('--dry-run sets ECC_DRY_RUN env var for child commands', () => {
const eccJs = path.resolve(__dirname, '..', '..', 'scripts', 'ecc.js');
const result = spawnSync(process.execPath, [eccJs, '--dry-run', '--help'], {
encoding: 'utf8',
env: { ...process.env },
});
assert.strictEqual(result.status, 0);
assert.ok(result.stdout.includes('--dry-run'), 'Help text should mention --dry-run');
})) passed++; else failed++;
if (test('--dry-run is stripped from args so command routing works', () => {
const eccJs = path.resolve(__dirname, '..', '..', 'scripts', 'ecc.js');
const result = spawnSync(process.execPath, [eccJs, '--dry-run', 'doctor'], {
encoding: 'utf8',
env: { ...process.env },
});
assert.ok(
!result.stderr.includes('Unknown command: --dry-run'),
'Global --dry-run must not be treated as an unknown command'
);
})) passed++; else failed++;
if (test('--dry-run works with implicit install routing', () => {
const eccJs = path.resolve(__dirname, '..', '..', 'scripts', 'ecc.js');
const homeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-dry-run-home-'));
try {
const result = spawnSync(process.execPath, [eccJs, '--dry-run', '--json', 'typescript'], {
encoding: 'utf8',
env: {
...process.env,
CLAUDE_CONFIG_DIR: path.join(homeDir, '.claude'),
HOME: homeDir,
USERPROFILE: homeDir,
},
maxBuffer: 10 * 1024 * 1024,
});
assert.strictEqual(result.status, 0, `Expected exit 0, got ${result.status}: ${result.stderr}`);
const payload = JSON.parse(result.stdout);
assert.strictEqual(payload.dryRun, true, 'Expected dryRun=true in JSON output');
assert.deepStrictEqual(payload.plan.legacyLanguages, ['typescript']);
} finally {
fs.rmSync(homeDir, { force: true, recursive: true });
}
})) passed++; else failed++;
console.log(`\nResults: ${passed} passed, ${failed} failed`);
process.exit(failed > 0 ? 1 : 0);
}
runTests();