From b4e8eedbef5a02d948e2a54cc1dbcdaf8d7979c7 Mon Sep 17 00:00:00 2001 From: matt Date: Fri, 13 Feb 2026 23:24:44 +0900 Subject: [PATCH] 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. --- test/main/utils/jsonl.test.ts | 69 ++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/test/main/utils/jsonl.test.ts b/test/main/utils/jsonl.test.ts index 55b35f5e..e748b821 100644 --- a/test/main/utils/jsonl.test.ts +++ b/test/main/utils/jsonl.test.ts @@ -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 + } + } }); }); });