refactor(jsonl.test): improve cleanup logic in analyzeSessionFileMetadata test

- Wrapped the cleanup of the temporary directory in a try-finally block to ensure it executes even if an error occurs during the test.
- Added retry logic to the directory removal process to handle potential ENOTEMPTY errors on Windows, enhancing test reliability.

This commit enhances the robustness of the test by ensuring proper cleanup and error handling.
This commit is contained in:
matt 2026-02-13 23:24:44 +09:00
parent 21ca76f37a
commit b4e8eedbef

View file

@ -139,37 +139,48 @@ describe('jsonl', () => {
describe('analyzeSessionFileMetadata', () => {
it('should extract first message, count, ongoing state, and git branch in one pass', async () => {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'jsonl-meta-'));
const filePath = path.join(tempDir, 'session.jsonl');
const lines = [
JSON.stringify({
type: 'user',
uuid: 'u1',
timestamp: '2026-01-01T00:00:00.000Z',
gitBranch: 'feature/test',
message: { role: 'user', content: 'hello world' },
isMeta: false,
}),
JSON.stringify({
type: 'assistant',
uuid: 'a1',
timestamp: '2026-01-01T00:00:01.000Z',
message: {
role: 'assistant',
content: [{ type: 'thinking', thinking: 'thinking...' }],
},
}),
];
fs.writeFileSync(filePath, `${lines.join('\n')}\n`, 'utf8');
try {
const filePath = path.join(tempDir, 'session.jsonl');
const lines = [
JSON.stringify({
type: 'user',
uuid: 'u1',
timestamp: '2026-01-01T00:00:00.000Z',
gitBranch: 'feature/test',
message: { role: 'user', content: 'hello world' },
isMeta: false,
}),
JSON.stringify({
type: 'assistant',
uuid: 'a1',
timestamp: '2026-01-01T00:00:01.000Z',
message: {
role: 'assistant',
content: [{ type: 'thinking', thinking: 'thinking...' }],
},
}),
];
fs.writeFileSync(filePath, `${lines.join('\n')}\n`, 'utf8');
const result = await analyzeSessionFileMetadata(filePath);
const result = await analyzeSessionFileMetadata(filePath);
expect(result.firstUserMessage?.text).toBe('hello world');
expect(result.firstUserMessage?.timestamp).toBe('2026-01-01T00:00:00.000Z');
expect(result.messageCount).toBe(1);
expect(result.isOngoing).toBe(true);
expect(result.gitBranch).toBe('feature/test');
fs.rmSync(tempDir, { recursive: true, force: true });
expect(result.firstUserMessage?.text).toBe('hello world');
expect(result.firstUserMessage?.timestamp).toBe('2026-01-01T00:00:00.000Z');
expect(result.messageCount).toBe(1);
expect(result.isOngoing).toBe(true);
expect(result.gitBranch).toBe('feature/test');
} finally {
try {
fs.rmSync(tempDir, {
recursive: true,
force: true,
maxRetries: 5,
retryDelay: 200,
});
} catch {
// Best-effort cleanup; ignore ENOTEMPTY on Windows when dir is in use
}
}
});
});
});