diff --git a/tests/lib/claude-settings.test.js b/tests/lib/claude-settings.test.js index 0c9d55399..54c75f7a3 100644 --- a/tests/lib/claude-settings.test.js +++ b/tests/lib/claude-settings.test.js @@ -403,13 +403,15 @@ function runTests() { const settingsPath = path.join(tempDir, 'settings.json'); const originalFstatSync = fs.fstatSync; const originalLstatSync = fs.lstatSync; + let identityDescriptor; let sawBigIntFstat = false; let sawBigIntLstat = false; const descriptorIno = 9007199254740992n; const pathIno = 9007199254740993n; try { fs.writeFileSync(settingsPath, '{"theme":"initial"}\n'); - const settingsIdentity = fs.statSync(settingsPath, { bigint: true }); + identityDescriptor = fs.openSync(settingsPath, 'r'); + const settingsIdentity = originalFstatSync.call(fs, identityDescriptor, { bigint: true }); fs.fstatSync = function(descriptor, options) { const stats = originalFstatSync.call(fs, descriptor, options); if (stats.dev !== settingsIdentity.dev || stats.ino !== settingsIdentity.ino) return stats; @@ -436,12 +438,18 @@ function runTests() { ); assert.strictEqual(sawBigIntFstat, true); assert.strictEqual(sawBigIntLstat, true); - assert.deepStrictEqual(JSON.parse(fs.readFileSync(settingsPath, 'utf8')), { + fs.fstatSync = originalFstatSync; + fs.lstatSync = originalLstatSync; + assert.deepStrictEqual(JSON.parse(fs.readFileSync(identityDescriptor, 'utf8')), { theme: 'initial', }); + const currentIdentity = fs.lstatSync(settingsPath, { bigint: true }); + assert.strictEqual(currentIdentity.dev, settingsIdentity.dev); + assert.strictEqual(currentIdentity.ino, settingsIdentity.ino); } finally { fs.fstatSync = originalFstatSync; fs.lstatSync = originalLstatSync; + if (identityDescriptor !== undefined) fs.closeSync(identityDescriptor); fs.rmSync(tempDir, { recursive: true, force: true }); } })) passed++; else failed++; diff --git a/tests/lib/work-item-verification.test.js b/tests/lib/work-item-verification.test.js index 47af4f6df..361424cd8 100644 --- a/tests/lib/work-item-verification.test.js +++ b/tests/lib/work-item-verification.test.js @@ -236,9 +236,23 @@ async function run() { fs.unlinkSync(artifact); fs.writeFileSync(artifact, validDoc()); const canonicalArtifact = path.join(fs.realpathSync(root), 'matrix.md'); const open = fs.openSync; - fs.openSync = function (filename, ...args) { - if (filename === canonicalArtifact) { fs.unlinkSync(artifact); makeFifo(); } - return open.call(fs, filename, ...args); + const readFlags = fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0) | (fs.constants.O_NONBLOCK || 0); + fs.openSync = function (filename, flags, mode) { + if (filename === canonicalArtifact) { + assert.strictEqual(flags, readFlags, 'artifact reads must retain nonblocking/no-follow flags'); + fs.unlinkSync(artifact); makeFifo(); + return open(filename, fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0) | (fs.constants.O_NONBLOCK || 0)); + } + // State persistence creates only private, exclusive staging files. + if (flags === 'wx') return open(filename, 'wx', mode); + if (flags === (fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | (fs.constants.O_NOFOLLOW || 0))) { + return open(filename, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | (fs.constants.O_NOFOLLOW || 0), mode); + } + if (flags === (fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0))) { + return open(filename, fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0)); + } + assert.strictEqual(flags, 'r', 'unexpected file-open mode during verification'); + return open(filename, 'r'); }; try { assert.strictEqual(store.verifyWorkItem('docs-task').attempt.outcome, 'unknown'); } finally { fs.openSync = open; } diff --git a/tests/scripts/post-bash-command-log.test.js b/tests/scripts/post-bash-command-log.test.js index 7c1fc1166..39da5717d 100644 --- a/tests/scripts/post-bash-command-log.test.js +++ b/tests/scripts/post-bash-command-log.test.js @@ -31,6 +31,15 @@ function runHook(mode, payload, homeDir) { }); } +function readLogSnapshot(filename) { + const descriptor = fs.openSync(filename, 'r'); + try { + return { content: fs.readFileSync(descriptor, 'utf8'), stat: fs.fstatSync(descriptor) }; + } finally { + fs.closeSync(descriptor); + } +} + let passed = 0; let failed = 0; @@ -371,7 +380,7 @@ if ( assert.strictEqual(result.status, 0, result.stdout + result.stderr); const logFile = path.join(homeDir, '.claude', fileName); - assert.strictEqual(fs.statSync(logFile).mode & 0o777, 0o600, fileName); + assert.strictEqual(readLogSnapshot(logFile).stat.mode & 0o777, 0o600, fileName); assert.ok(!fs.existsSync(`${logFile}.1`), `${fileName} should not rotate`); } } finally { @@ -396,8 +405,9 @@ if ( const result = runHook('audit', { tool_input: { command: 'git log' } }, homeDir); assert.strictEqual(result.status, 0, result.stdout + result.stderr); - assert.strictEqual(fs.statSync(logFile).mode & 0o777, 0o600); - const logContent = fs.readFileSync(logFile, 'utf8'); + const snapshot = readLogSnapshot(logFile); + assert.strictEqual(snapshot.stat.mode & 0o777, 0o600); + const logContent = snapshot.content; assert.ok(logContent.startsWith('[earlier] git status\n'), logContent); assert.ok(logContent.includes('git log'), logContent); } finally { @@ -423,20 +433,21 @@ if ( let result = runHook('audit', { tool_input: { command: 'echo second' } }, homeDir); assert.strictEqual(result.status, 0, result.stdout + result.stderr); - assert.strictEqual(fs.statSync(rotated).size, LOG_MAX_BYTES); - assert.ok(fs.readFileSync(rotated, 'utf8').startsWith('[first] generation\n')); - const fresh = fs.readFileSync(logFile, 'utf8'); - assert.match(fresh, /^\[[^\]]+\] echo second\n$/); - if (checkModes) assert.strictEqual(fs.statSync(logFile).mode & 0o777, 0o600); + const previous = readLogSnapshot(rotated); + assert.strictEqual(previous.stat.size, LOG_MAX_BYTES); + assert.ok(previous.content.startsWith('[first] generation\n')); + const fresh = readLogSnapshot(logFile); + assert.match(fresh.content, /^\[[^\]]+\] echo second\n$/); + if (checkModes) assert.strictEqual(fresh.stat.mode & 0o777, 0o600); // Filling the new log again replaces the previous rotated copy. fs.truncateSync(logFile, LOG_MAX_BYTES); result = runHook('audit', { tool_input: { command: 'echo third' } }, homeDir); assert.strictEqual(result.status, 0, result.stdout + result.stderr); - assert.ok(fs.readFileSync(rotated, 'utf8').includes('echo second')); + assert.ok(readLogSnapshot(rotated).content.includes('echo second')); assert.ok(!fs.existsSync(`${logFile}.2`)); - assert.match(fs.readFileSync(logFile, 'utf8'), /^\[[^\]]+\] echo third\n$/); + assert.match(readLogSnapshot(logFile).content, /^\[[^\]]+\] echo third\n$/); } finally { fs.rmSync(homeDir, { recursive: true, force: true }); } @@ -460,9 +471,10 @@ if ( const result = runHook('audit', { tool_input: { command: 'echo next' } }, homeDir); assert.strictEqual(result.status, 0, result.stdout + result.stderr); - assert.ok(fs.readFileSync(`${logFile}.1`, 'utf8').startsWith('[legacy] history\n')); - assert.strictEqual(fs.statSync(`${logFile}.1`).mode & 0o777, 0o600); - assert.strictEqual(fs.statSync(logFile).mode & 0o777, 0o600); + const previous = readLogSnapshot(`${logFile}.1`); + assert.ok(previous.content.startsWith('[legacy] history\n')); + assert.strictEqual(previous.stat.mode & 0o777, 0o600); + assert.strictEqual(readLogSnapshot(logFile).stat.mode & 0o777, 0o600); } finally { fs.rmSync(homeDir, { recursive: true, force: true }); } @@ -486,11 +498,12 @@ if ( assert.strictEqual(result.status, 0, result.stdout + result.stderr); assert.ok(fs.statSync(`${logFile}.1`).isDirectory()); - assert.ok(fs.statSync(logFile).size > LOG_MAX_BYTES); const fd = fs.openSync(logFile, 'r'); try { + const size = fs.fstatSync(fd).size; + assert.ok(size > LOG_MAX_BYTES); const tail = Buffer.alloc(64); - const read = fs.readSync(fd, tail, 0, tail.length, fs.statSync(logFile).size - tail.length); + const read = fs.readSync(fd, tail, 0, tail.length, size - tail.length); assert.ok(tail.subarray(0, read).toString('utf8').endsWith('echo kept\n')); } finally { fs.closeSync(fd);