Compare commits

..

2 Commits

Author SHA1 Message Date
Affaan Mustafa
2e43eac8d9 fix: export codex sync env to child scripts 2026-03-30 02:08:33 -04:00
Affaan Mustafa
f015f3eb75 test: isolate package-manager dependent hooks and formatter tests 2026-03-30 02:08:33 -04:00
7 changed files with 103 additions and 212 deletions

View File

@@ -310,7 +310,7 @@
"timeout": 10
}
],
"description": "Send desktop notification (macOS/WSL) with task summary when Claude responds"
"description": "Send macOS desktop notification with task summary when Claude responds"
}
],
"SessionEnd": [

View File

@@ -3,11 +3,9 @@
* Desktop Notification Hook (Stop)
*
* Sends a native desktop notification with the task summary when Claude
* finishes responding. Supports:
* - macOS: osascript (native)
* - WSL: PowerShell 7 or Windows PowerShell + BurntToast module
*
* On WSL, if BurntToast is not installed, logs a tip for installation.
* finishes responding. Currently supports macOS (osascript); other
* platforms exit silently. Windows (PowerShell) and Linux (notify-send)
* support is planned.
*
* Hook ID : stop:desktop-notify
* Profiles: standard, strict
@@ -21,64 +19,6 @@ const { isMacOS, log } = require('../lib/utils');
const TITLE = 'Claude Code';
const MAX_BODY_LENGTH = 100;
/**
* Memoized WSL detection at module load (avoids repeated /proc/version reads).
*/
let isWSL = false;
if (process.platform === 'linux') {
try {
isWSL = require('fs').readFileSync('/proc/version', 'utf8').toLowerCase().includes('microsoft');
} catch {
isWSL = false;
}
}
/**
* Find available PowerShell executable on WSL.
* Returns first accessible path, or null if none found.
*/
function findPowerShell() {
if (!isWSL) return null;
const candidates = [
'pwsh.exe', // WSL interop resolves from Windows PATH
'powershell.exe', // WSL interop for Windows PowerShell
'/mnt/c/Program Files/PowerShell/7/pwsh.exe', // PowerShell 7 (default install)
'/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe', // Windows PowerShell
];
for (const path of candidates) {
try {
const result = spawnSync(path, ['-Command', 'exit 0'],
{ stdio: ['ignore', 'pipe', 'ignore'], timeout: 1000 });
if (result.status === 0) {
return path;
}
} catch {
// continue
}
}
return null;
}
/**
* Send a Windows Toast notification via PowerShell BurntToast.
* Returns true on success, false on failure.
*/
function notifyWindows(pwshPath, title, body) {
const safeBody = body.replace(/'/g, "''");
const safeTitle = title.replace(/'/g, "''");
const command = `Import-Module BurntToast; New-BurntToastNotification -Text '${safeTitle}', '${safeBody}'`;
const result = spawnSync(pwshPath, ['-Command', command],
{ stdio: ['ignore', 'pipe', 'pipe'], timeout: 5000 });
if (result.error || result.status !== 0) {
const stderr = typeof result.stderr?.toString === 'function' ? result.stderr.toString().trim() : '';
log(`[DesktopNotify] BurntToast failed (exit ${result.status}): ${result.error ? result.error.message : stderr}`);
return false;
}
return true;
}
/**
* Extract a short summary from the last assistant message.
* Takes the first non-empty line and truncates to MAX_BODY_LENGTH chars.
@@ -113,28 +53,20 @@ function notifyMacOS(title, body) {
}
}
// TODO: future platform support
// function notifyWindows(title, body) { ... }
// function notifyLinux(title, body) { ... }
/**
* Fast-path entry point for run-with-flags.js (avoids extra process spawn).
*/
function run(raw) {
try {
if (!isMacOS) return raw;
const input = raw.trim() ? JSON.parse(raw) : {};
const summary = extractSummary(input.last_assistant_message);
if (isMacOS) {
notifyMacOS(TITLE, summary);
} else if (isWSL) {
const ps = findPowerShell();
if (ps && notifyWindows(ps, TITLE, summary)) {
// notification sent successfully
} else if (ps) {
// PowerShell found but BurntToast not available
log('[DesktopNotify] Tip: Install BurntToast module to enable notifications');
} else {
// No PowerShell found
log('[DesktopNotify] Tip: Install BurntToast module in PowerShell for notifications');
}
}
notifyMacOS(TITLE, summary);
} catch (err) {
log(`[DesktopNotify] Error: ${err.message}`);
}

View File

@@ -460,16 +460,28 @@ fi
log "Installing global git safety hooks"
if [[ "$MODE" == "dry-run" ]]; then
"$HOOKS_INSTALLER" --dry-run
HOME="$HOME" \
CODEX_HOME="$CODEX_HOME" \
AGENTS_HOME="${AGENTS_HOME:-$HOME/.agents}" \
ECC_GLOBAL_HOOKS_DIR="${ECC_GLOBAL_HOOKS_DIR:-$CODEX_HOME/git-hooks}" \
"$HOOKS_INSTALLER" --dry-run
else
"$HOOKS_INSTALLER"
HOME="$HOME" \
CODEX_HOME="$CODEX_HOME" \
AGENTS_HOME="${AGENTS_HOME:-$HOME/.agents}" \
ECC_GLOBAL_HOOKS_DIR="${ECC_GLOBAL_HOOKS_DIR:-$CODEX_HOME/git-hooks}" \
"$HOOKS_INSTALLER"
fi
log "Running global regression sanity check"
if [[ "$MODE" == "dry-run" ]]; then
printf '[dry-run] %s\n' "$SANITY_CHECKER"
else
"$SANITY_CHECKER"
HOME="$HOME" \
CODEX_HOME="$CODEX_HOME" \
AGENTS_HOME="${AGENTS_HOME:-$HOME/.agents}" \
ECC_GLOBAL_HOOKS_DIR="${ECC_GLOBAL_HOOKS_DIR:-$CODEX_HOME/git-hooks}" \
"$SANITY_CHECKER"
fi
log "Sync complete"

View File

@@ -1,118 +0,0 @@
/**
* Tests for scripts/hooks/desktop-notify.js
*/
const assert = require('assert');
const fs = require('fs');
const Module = require('module');
const path = require('path');
const modulePath = path.join(__dirname, '..', '..', 'scripts', 'hooks', 'desktop-notify.js');
const moduleSource = fs.readFileSync(modulePath, 'utf8');
function test(name, fn) {
try {
fn();
console.log(`${name}`);
return true;
} catch (error) {
console.log(`${name}`);
console.log(` Error: ${error.message}`);
return false;
}
}
function loadDesktopNotify({ procVersion = 'Linux version microsoft', spawnImpl, isMacOS = false }) {
const logs = [];
const mod = new Module(modulePath, module);
mod.filename = modulePath;
mod.paths = Module._nodeModulePaths(path.dirname(modulePath));
const originalRequire = mod.require.bind(mod);
mod.require = request => {
if (request === 'child_process') {
return { spawnSync: spawnImpl };
}
if (request === '../lib/utils') {
return {
isMacOS,
log: message => logs.push(message),
};
}
if (request === 'fs') {
return {
...fs,
readFileSync(target, encoding) {
if (target === '/proc/version') {
return procVersion;
}
return fs.readFileSync(target, encoding);
}
};
}
return originalRequire(request);
};
const platformDescriptor = Object.getOwnPropertyDescriptor(process, 'platform');
Object.defineProperty(process, 'platform', {
configurable: true,
value: 'linux',
});
try {
mod._compile(moduleSource, modulePath);
} finally {
Object.defineProperty(process, 'platform', platformDescriptor);
}
return { run: mod.exports.run, logs };
}
let passed = 0;
let failed = 0;
if (
test('successful WSL toast does not log BurntToast install guidance', () => {
const calls = [];
const { run, logs } = loadDesktopNotify({
spawnImpl(command, args) {
calls.push({ command, args });
if (calls.length === 1) {
return { status: 0, stderr: Buffer.from('') };
}
return { status: 0, stderr: Buffer.from('') };
}
});
const payload = JSON.stringify({ last_assistant_message: 'Build completed successfully' });
assert.strictEqual(run(payload), payload);
assert.strictEqual(calls.length, 2, 'Expected PowerShell probe and notification send');
assert.strictEqual(logs.length, 0, `Expected no warnings, got: ${logs.join('\n')}`);
})
)
passed++;
else failed++;
if (
test('failed WSL toast logs failure and install guidance once', () => {
const { run, logs } = loadDesktopNotify({
spawnImpl(command, args) {
if (args[1] === 'exit 0') {
return { status: 0, stderr: Buffer.from('') };
}
return { status: 1, stderr: Buffer.from('module missing') };
}
});
const payload = JSON.stringify({ last_assistant_message: 'Done' });
assert.strictEqual(run(payload), payload);
assert.ok(logs.some(message => message.includes('BurntToast failed')), 'Expected BurntToast failure log');
assert.ok(logs.some(message => message.includes('Install BurntToast module')), 'Expected install tip');
})
)
passed++;
else failed++;
console.log(`\nPassed: ${passed}`);
console.log(`Failed: ${failed}`);
process.exit(failed > 0 ? 1 : 0);

View File

@@ -1221,9 +1221,14 @@ async function runTests() {
fs.writeFileSync(path.join(rootDir, '.prettierrc'), '{}');
fs.writeFileSync(filePath, 'export const value = 1;\n');
createCommandShim(binDir, 'npx', logFile);
const isolatedHome = path.join(testDir, 'isolated-home');
fs.mkdirSync(path.join(isolatedHome, '.claude'), { recursive: true });
const stdinJson = JSON.stringify({ tool_input: { file_path: filePath } });
const result = await runScript(path.join(scriptsDir, 'post-edit-format.js'), stdinJson, withPrependedPath(binDir));
const result = await runScript(path.join(scriptsDir, 'post-edit-format.js'), stdinJson, withPrependedPath(binDir, {
HOME: isolatedHome,
USERPROFILE: isolatedHome
}));
assert.strictEqual(result.code, 0, 'Should exit 0 for config-only repo');
const logEntries = readCommandLog(logFile);

View File

@@ -37,6 +37,33 @@ function cleanupTestDir(testDir) {
fs.rmSync(testDir, { recursive: true, force: true });
}
function withIsolatedHome(fn) {
const isolatedHome = fs.mkdtempSync(path.join(os.tmpdir(), 'pm-home-'));
const originalHome = process.env.HOME;
const originalUserProfile = process.env.USERPROFILE;
process.env.HOME = isolatedHome;
process.env.USERPROFILE = isolatedHome;
try {
return fn(isolatedHome);
} finally {
if (originalHome !== undefined) {
process.env.HOME = originalHome;
} else {
delete process.env.HOME;
}
if (originalUserProfile !== undefined) {
process.env.USERPROFILE = originalUserProfile;
} else {
delete process.env.USERPROFILE;
}
fs.rmSync(isolatedHome, { recursive: true, force: true });
}
}
// Test suite
function runTests() {
console.log('\n=== Testing package-manager.js ===\n');
@@ -711,9 +738,11 @@ function runTests() {
const originalEnv = process.env.CLAUDE_PACKAGE_MANAGER;
try {
delete process.env.CLAUDE_PACKAGE_MANAGER;
const result = pm.getPackageManager({ projectDir: testDir });
assert.strictEqual(result.name, 'npm', 'Should default to npm');
assert.strictEqual(result.source, 'default');
withIsolatedHome(() => {
const result = pm.getPackageManager({ projectDir: testDir });
assert.strictEqual(result.name, 'npm', 'Should default to npm');
assert.strictEqual(result.source, 'default');
});
} finally {
if (originalEnv !== undefined) {
process.env.CLAUDE_PACKAGE_MANAGER = originalEnv;

View File

@@ -58,6 +58,33 @@ function cleanupTmpDirs() {
tmpDirs.length = 0;
}
function withIsolatedHome(fn) {
const isolatedHome = fs.mkdtempSync(path.join(os.tmpdir(), 'resolve-fmt-home-'));
const originalHome = process.env.HOME;
const originalUserProfile = process.env.USERPROFILE;
process.env.HOME = isolatedHome;
process.env.USERPROFILE = isolatedHome;
try {
return fn(isolatedHome);
} finally {
if (originalHome !== undefined) {
process.env.HOME = originalHome;
} else {
delete process.env.HOME;
}
if (originalUserProfile !== undefined) {
process.env.USERPROFILE = originalUserProfile;
} else {
delete process.env.USERPROFILE;
}
fs.rmSync(isolatedHome, { recursive: true, force: true });
}
}
function runTests() {
console.log('\n=== Testing resolve-formatter.js ===\n');
@@ -168,10 +195,12 @@ function runTests() {
run('resolveFormatterBin: falls back to npx for biome', () => {
const root = makeTmpDir();
const result = resolveFormatterBin(root, 'biome');
const expectedBin = process.platform === 'win32' ? 'npx.cmd' : 'npx';
assert.strictEqual(result.bin, expectedBin);
assert.deepStrictEqual(result.prefix, ['@biomejs/biome']);
withIsolatedHome(() => {
const result = resolveFormatterBin(root, 'biome');
const expectedBin = process.platform === 'win32' ? 'npx.cmd' : 'npx';
assert.strictEqual(result.bin, expectedBin);
assert.deepStrictEqual(result.prefix, ['@biomejs/biome']);
});
});
run('resolveFormatterBin: uses local prettier binary when available', () => {
@@ -188,10 +217,12 @@ function runTests() {
run('resolveFormatterBin: falls back to npx for prettier', () => {
const root = makeTmpDir();
const result = resolveFormatterBin(root, 'prettier');
const expectedBin = process.platform === 'win32' ? 'npx.cmd' : 'npx';
assert.strictEqual(result.bin, expectedBin);
assert.deepStrictEqual(result.prefix, ['prettier']);
withIsolatedHome(() => {
const result = resolveFormatterBin(root, 'prettier');
const expectedBin = process.platform === 'win32' ? 'npx.cmd' : 'npx';
assert.strictEqual(result.bin, expectedBin);
assert.deepStrictEqual(result.prefix, ['prettier']);
});
});
run('resolveFormatterBin: returns null for unknown formatter', () => {