fix(node): mirror official Claude hooks
This commit is contained in:
1 parent
7fe99f7e5f
commit
baaa71e32f
13 files changed
+1824
-149
No files matched your search
@@ -0,0 +1,323 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
lstat,
|
||||
mkdir,
|
||||
readFile,
|
||||
readdir,
|
||||
symlink,
|
||||
writeFile,
|
||||
} from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import test from 'node:test';
|
||||
import {
|
||||
createFixture,
|
||||
installationModuleUrl,
|
||||
installConfig,
|
||||
ioModuleUrl,
|
||||
runNode,
|
||||
writeJson,
|
||||
} from '../test-support/claudecode-test-helpers.mjs';
|
||||
|
||||
const managedFileNames = ['guard.js', 'config.json', 'private.key', 'hook.js'];
|
||||
|
||||
async function snapshotInstallation(fixture) {
|
||||
const entries = await Promise.all(managedFileNames.map(async (name) => [
|
||||
path.join(fixture.agentDir, name),
|
||||
await readFile(path.join(fixture.agentDir, name), 'utf-8'),
|
||||
]));
|
||||
entries.push([fixture.settingsPath, await readFile(fixture.settingsPath, 'utf-8')]);
|
||||
return new Map(entries);
|
||||
}
|
||||
|
||||
async function assertSnapshot(snapshot) {
|
||||
for (const [filePath, expected] of snapshot) {
|
||||
assert.equal(await readFile(filePath, 'utf-8'), expected, filePath);
|
||||
}
|
||||
}
|
||||
|
||||
async function assertNoTransactionFiles(fixture) {
|
||||
const names = [
|
||||
...await readdir(fixture.agentDir),
|
||||
...await readdir(path.dirname(fixture.settingsPath)),
|
||||
];
|
||||
assert.equal(names.some((name) => /\.(tmp|rollback)$/.test(name)), false, names.join(', '));
|
||||
}
|
||||
|
||||
async function preservedRollback(fixture, basename) {
|
||||
const entries = await readdir(fixture.agentDir);
|
||||
const matches = entries.filter(
|
||||
(name) => name.startsWith(`.${basename}.`) && name.endsWith('.rollback'),
|
||||
);
|
||||
assert.equal(matches.length, 1, entries.join(', '));
|
||||
return path.join(fixture.agentDir, matches[0]);
|
||||
}
|
||||
|
||||
function testEnvironment(fixture, overrides = {}) {
|
||||
return {
|
||||
HOME: fixture.homeDir,
|
||||
USERPROFILE: fixture.homeDir,
|
||||
CLAUDE_CONFIG_DIR: fixture.claudeConfigDir,
|
||||
ELYDORA_INSTALL_CONFIG: JSON.stringify(installConfig(fixture, overrides)),
|
||||
};
|
||||
}
|
||||
|
||||
test('Claude rolls back all five files when a transaction commit fails', async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
assert.equal((await fixture.install()).code, 0);
|
||||
const before = await snapshotInstallation(fixture);
|
||||
const source = `
|
||||
import { rename } from 'node:fs/promises';
|
||||
import { readClaudeDocument } from ${JSON.stringify(ioModuleUrl)};
|
||||
import {
|
||||
commitClaudeInstallation,
|
||||
prepareClaudeInstallation,
|
||||
} from ${JSON.stringify(installationModuleUrl)};
|
||||
const config = JSON.parse(process.env.ELYDORA_INSTALL_CONFIG);
|
||||
const document = await readClaudeDocument();
|
||||
const prepared = await prepareClaudeInstallation(config, { document, changed: false });
|
||||
let calls = 0;
|
||||
await commitClaudeInstallation(prepared, async (from, to) => {
|
||||
calls += 1;
|
||||
if (calls === 3) throw new Error('injected rename failure');
|
||||
await rename(from, to);
|
||||
});
|
||||
`;
|
||||
const result = await runNode(
|
||||
['--input-type=module', '--eval', source],
|
||||
testEnvironment(fixture, { orgId: 'org-updated', token: 'token-updated' }),
|
||||
fixture.projectDir,
|
||||
);
|
||||
assert.equal(result.code, 1);
|
||||
assert.match(result.stderr, /injected rename failure/i);
|
||||
await assertSnapshot(before);
|
||||
await assertNoTransactionFiles(fixture);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('Claude detects concurrent settings changes before committing runtime updates', async () => {
|
||||
const fixture = await createFixture({ settings: { model: 'sonnet' } });
|
||||
try {
|
||||
assert.equal((await fixture.install()).code, 0);
|
||||
const before = await snapshotInstallation(fixture);
|
||||
const concurrent = '{"model":"opus","hooks":{"Notification":[]}}\n';
|
||||
const source = `
|
||||
import { writeFile } from 'node:fs/promises';
|
||||
import { readClaudeDocument } from ${JSON.stringify(ioModuleUrl)};
|
||||
import {
|
||||
commitClaudeInstallation,
|
||||
prepareClaudeInstallation,
|
||||
} from ${JSON.stringify(installationModuleUrl)};
|
||||
const document = await readClaudeDocument();
|
||||
const prepared = await prepareClaudeInstallation(
|
||||
JSON.parse(process.env.ELYDORA_INSTALL_CONFIG),
|
||||
{ document, changed: false },
|
||||
);
|
||||
await writeFile(document.filePath, process.env.ELYDORA_CONCURRENT, 'utf-8');
|
||||
await commitClaudeInstallation(prepared);
|
||||
`;
|
||||
const result = await runNode(
|
||||
['--input-type=module', '--eval', source],
|
||||
{
|
||||
...testEnvironment(fixture, { orgId: 'updated' }),
|
||||
ELYDORA_CONCURRENT: concurrent,
|
||||
},
|
||||
fixture.projectDir,
|
||||
);
|
||||
assert.equal(result.code, 1);
|
||||
assert.match(result.stderr, /changed during Claude Code installation/i);
|
||||
for (const [filePath, expected] of before) {
|
||||
if (filePath !== fixture.settingsPath) {
|
||||
assert.equal(await readFile(filePath, 'utf-8'), expected);
|
||||
}
|
||||
}
|
||||
assert.equal(await readFile(fixture.settingsPath, 'utf-8'), concurrent);
|
||||
await assertNoTransactionFiles(fixture);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('Claude rejects a stale settings document before staging files', async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
assert.equal((await fixture.install()).code, 0);
|
||||
const before = await snapshotInstallation(fixture);
|
||||
const concurrent = '{"hooks":{"Notification":[]}}\n';
|
||||
const source = `
|
||||
import { writeFile } from 'node:fs/promises';
|
||||
import { readClaudeDocument } from ${JSON.stringify(ioModuleUrl)};
|
||||
import { prepareClaudeInstallation } from ${JSON.stringify(installationModuleUrl)};
|
||||
const document = await readClaudeDocument();
|
||||
await writeFile(document.filePath, process.env.ELYDORA_CONCURRENT, 'utf-8');
|
||||
await prepareClaudeInstallation(
|
||||
JSON.parse(process.env.ELYDORA_INSTALL_CONFIG),
|
||||
{ document, changed: false },
|
||||
);
|
||||
`;
|
||||
const result = await runNode(
|
||||
['--input-type=module', '--eval', source],
|
||||
{
|
||||
...testEnvironment(fixture),
|
||||
ELYDORA_CONCURRENT: concurrent,
|
||||
},
|
||||
fixture.projectDir,
|
||||
);
|
||||
assert.equal(result.code, 1);
|
||||
assert.match(result.stderr, /changed before installation/i);
|
||||
for (const [filePath, expected] of before) {
|
||||
if (filePath !== fixture.settingsPath) {
|
||||
assert.equal(await readFile(filePath, 'utf-8'), expected);
|
||||
}
|
||||
}
|
||||
assert.equal(await readFile(fixture.settingsPath, 'utf-8'), concurrent);
|
||||
await assertNoTransactionFiles(fixture);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('Claude preserves rollback data after a committed runtime changes', async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
assert.equal((await fixture.install()).code, 0);
|
||||
const original = await readFile(fixture.guardScriptPath, 'utf-8');
|
||||
const source = `
|
||||
import { rename, rm, writeFile } from 'node:fs/promises';
|
||||
import { readClaudeDocument } from ${JSON.stringify(ioModuleUrl)};
|
||||
import {
|
||||
commitClaudeInstallation,
|
||||
prepareClaudeInstallation,
|
||||
} from ${JSON.stringify(installationModuleUrl)};
|
||||
const document = await readClaudeDocument();
|
||||
const prepared = await prepareClaudeInstallation(
|
||||
JSON.parse(process.env.ELYDORA_INSTALL_CONFIG),
|
||||
{ document, changed: false },
|
||||
);
|
||||
let calls = 0;
|
||||
await commitClaudeInstallation(prepared, async (from, to) => {
|
||||
calls += 1;
|
||||
if (calls === 1) {
|
||||
await rename(from, to);
|
||||
await rm(to);
|
||||
await writeFile(to, 'external change\\n');
|
||||
return;
|
||||
}
|
||||
if (calls === 2) throw new Error('injected later commit failure');
|
||||
await rename(from, to);
|
||||
});
|
||||
`;
|
||||
const result = await runNode(
|
||||
['--input-type=module', '--eval', source],
|
||||
testEnvironment(fixture, { orgId: 'updated' }),
|
||||
fixture.projectDir,
|
||||
);
|
||||
assert.equal(result.code, 1);
|
||||
assert.match(result.stderr, /original content preserved at/i);
|
||||
assert.equal(await readFile(fixture.guardScriptPath, 'utf-8'), 'external change\n');
|
||||
assert.equal(await readFile(await preservedRollback(fixture, 'guard.js'), 'utf-8'), original);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('Claude rejects orphaned and mismatched runtime identities before writes', async (t) => {
|
||||
await t.test('orphaned runtime', async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
await mkdir(fixture.agentDir, { recursive: true });
|
||||
await writeFile(fixture.guardScriptPath, 'orphaned guard\n');
|
||||
const result = await fixture.install();
|
||||
assert.equal(result.code, 1);
|
||||
assert.match(result.stderr, /identity cannot be verified/i);
|
||||
assert.equal(await readFile(fixture.guardScriptPath, 'utf-8'), 'orphaned guard\n');
|
||||
await assertMissing(fixture.settingsPath);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
|
||||
await t.test('mismatched identity', async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
await writeJson(path.join(fixture.agentDir, 'config.json'), {
|
||||
agent_name: 'claudecode',
|
||||
agent_id: 'another-agent',
|
||||
});
|
||||
const configPath = path.join(fixture.agentDir, 'config.json');
|
||||
const before = await readFile(configPath, 'utf-8');
|
||||
const result = await fixture.install();
|
||||
assert.equal(result.code, 1);
|
||||
assert.match(result.stderr, /identity does not match/i);
|
||||
assert.equal(await readFile(configPath, 'utf-8'), before);
|
||||
await assertMissing(fixture.settingsPath);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
async function assertMissing(filePath) {
|
||||
await assert.rejects(lstat(filePath), { code: 'ENOENT' });
|
||||
}
|
||||
|
||||
test('Claude rejects linked configuration, settings, and runtime paths', async (t) => {
|
||||
for (const kind of ['configuration', 'settings', 'runtime']) {
|
||||
await t.test(kind, async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
const target = path.join(fixture.rootDir, `${kind}-target`);
|
||||
await mkdir(target, { recursive: true });
|
||||
await mkdir(fixture.homeDir, { recursive: true });
|
||||
let link;
|
||||
let linkType = 'junction';
|
||||
if (kind === 'configuration') {
|
||||
link = fixture.claudeConfigDir;
|
||||
} else if (kind === 'runtime') {
|
||||
link = path.join(fixture.homeDir, '.elydora');
|
||||
} else {
|
||||
await mkdir(fixture.claudeConfigDir, { recursive: true });
|
||||
const targetFile = path.join(target, 'settings.json');
|
||||
await writeFile(targetFile, '{}\n');
|
||||
link = fixture.settingsPath;
|
||||
linkType = 'file';
|
||||
await symlink(targetFile, link, linkType).catch((error) => { throw error; });
|
||||
}
|
||||
if (kind !== 'settings') {
|
||||
try {
|
||||
await symlink(target, link, linkType);
|
||||
} catch (error) {
|
||||
if (error?.code === 'EPERM') {
|
||||
t.skip(`symbolic links unavailable: ${error.message}`);
|
||||
return;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
const result = await fixture.install();
|
||||
assert.equal(result.code, 1);
|
||||
assert.match(result.stderr, /physical (directory|file)/i);
|
||||
} catch (error) {
|
||||
if (error?.code === 'EPERM') {
|
||||
t.skip(`symbolic links unavailable: ${error.message}`);
|
||||
return;
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test('Claude installation leaves no transaction artifacts', async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
assert.equal((await fixture.install()).code, 0);
|
||||
await assertNoTransactionFiles(fixture);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user