Skip to content

Commit e924236

Browse files
committed
refactor: use explicit resource management in deduplication tests
Replace beforeEach/afterEach hooks with 'await using' syntax for fs-fixture cleanup as requested in PR review. This follows the pattern used in other test files and leverages TypeScript's explicit resource management feature. Changes: - Remove beforeEach/afterEach hooks - Use 'await using fixture = await createFixture()' syntax - Automatic cleanup when leaving scope - Each test is now more self-contained
1 parent b5e8b35 commit e924236

File tree

1 file changed

+23
-54
lines changed

1 file changed

+23
-54
lines changed

src/data-loader-deduplication.test.ts

Lines changed: 23 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
1+
import { describe, expect, it } from 'bun:test';
22
import { createFixture } from 'fs-fixture';
33
import {
44
createUniqueHash,
@@ -61,24 +61,16 @@ describe('deduplication functionality', () => {
6161
});
6262

6363
describe('getEarliestTimestamp', () => {
64-
let fixture: Awaited<ReturnType<typeof createFixture>>;
65-
66-
beforeEach(async () => {
67-
fixture = await createFixture({});
68-
});
69-
70-
afterEach(async () => {
71-
await fixture.rm();
72-
});
73-
7464
it('should extract earliest timestamp from JSONL file', async () => {
7565
const content = [
7666
JSON.stringify({ timestamp: '2025-01-15T12:00:00Z', message: { usage: {} } }),
7767
JSON.stringify({ timestamp: '2025-01-10T10:00:00Z', message: { usage: {} } }),
7868
JSON.stringify({ timestamp: '2025-01-12T11:00:00Z', message: { usage: {} } }),
7969
].join('\n');
8070

81-
await fixture.writeFile('test.jsonl', content);
71+
await using fixture = await createFixture({
72+
'test.jsonl': content,
73+
});
8274

8375
const timestamp = await getEarliestTimestamp(fixture.getPath('test.jsonl'));
8476
expect(timestamp).toEqual(new Date('2025-01-10T10:00:00Z'));
@@ -90,7 +82,9 @@ describe('deduplication functionality', () => {
9082
JSON.stringify({ data: 'no timestamp' }),
9183
].join('\n');
9284

93-
await fixture.writeFile('test.jsonl', content);
85+
await using fixture = await createFixture({
86+
'test.jsonl': content,
87+
});
9488

9589
const timestamp = await getEarliestTimestamp(fixture.getPath('test.jsonl'));
9690
expect(timestamp).toBeNull();
@@ -103,33 +97,22 @@ describe('deduplication functionality', () => {
10397
'{ broken: json',
10498
].join('\n');
10599

106-
await fixture.writeFile('test.jsonl', content);
100+
await using fixture = await createFixture({
101+
'test.jsonl': content,
102+
});
107103

108104
const timestamp = await getEarliestTimestamp(fixture.getPath('test.jsonl'));
109105
expect(timestamp).toEqual(new Date('2025-01-10T10:00:00Z'));
110106
});
111107
});
112108

113109
describe('sortFilesByTimestamp', () => {
114-
let fixture: Awaited<ReturnType<typeof createFixture>>;
115-
116-
beforeEach(async () => {
117-
fixture = await createFixture({});
118-
});
119-
120-
afterEach(async () => {
121-
await fixture.rm();
122-
});
123-
124110
it('should sort files by earliest timestamp', async () => {
125-
// File 1: earliest timestamp 2025-01-15
126-
await fixture.writeFile('file1.jsonl', JSON.stringify({ timestamp: '2025-01-15T10:00:00Z' }));
127-
128-
// File 2: earliest timestamp 2025-01-10
129-
await fixture.writeFile('file2.jsonl', JSON.stringify({ timestamp: '2025-01-10T10:00:00Z' }));
130-
131-
// File 3: earliest timestamp 2025-01-12
132-
await fixture.writeFile('file3.jsonl', JSON.stringify({ timestamp: '2025-01-12T10:00:00Z' }));
111+
await using fixture = await createFixture({
112+
'file1.jsonl': JSON.stringify({ timestamp: '2025-01-15T10:00:00Z' }),
113+
'file2.jsonl': JSON.stringify({ timestamp: '2025-01-10T10:00:00Z' }),
114+
'file3.jsonl': JSON.stringify({ timestamp: '2025-01-12T10:00:00Z' }),
115+
});
133116

134117
const file1 = fixture.getPath('file1.jsonl');
135118
const file2 = fixture.getPath('file2.jsonl');
@@ -141,9 +124,11 @@ describe('deduplication functionality', () => {
141124
});
142125

143126
it('should place files without timestamps at the end', async () => {
144-
await fixture.writeFile('file1.jsonl', JSON.stringify({ timestamp: '2025-01-15T10:00:00Z' }));
145-
await fixture.writeFile('file2.jsonl', JSON.stringify({ no_timestamp: true }));
146-
await fixture.writeFile('file3.jsonl', JSON.stringify({ timestamp: '2025-01-10T10:00:00Z' }));
127+
await using fixture = await createFixture({
128+
'file1.jsonl': JSON.stringify({ timestamp: '2025-01-15T10:00:00Z' }),
129+
'file2.jsonl': JSON.stringify({ no_timestamp: true }),
130+
'file3.jsonl': JSON.stringify({ timestamp: '2025-01-10T10:00:00Z' }),
131+
});
147132

148133
const file1 = fixture.getPath('file1.jsonl');
149134
const file2 = fixture.getPath('file2.jsonl');
@@ -156,16 +141,8 @@ describe('deduplication functionality', () => {
156141
});
157142

158143
describe('loadDailyUsageData with deduplication', () => {
159-
let fixture: Awaited<ReturnType<typeof createFixture>>;
160-
161-
afterEach(async () => {
162-
if (fixture != null) {
163-
await fixture.rm();
164-
}
165-
});
166-
167144
it('should deduplicate entries with same message and request IDs', async () => {
168-
fixture = await createFixture({
145+
await using fixture = await createFixture({
169146
projects: {
170147
project1: {
171148
session1: {
@@ -213,7 +190,7 @@ describe('deduplication functionality', () => {
213190
});
214191

215192
it('should process files in chronological order', async () => {
216-
fixture = await createFixture({
193+
await using fixture = await createFixture({
217194
projects: {
218195
'newer.jsonl': JSON.stringify({
219196
timestamp: '2025-01-15T10:00:00Z',
@@ -256,16 +233,8 @@ describe('deduplication functionality', () => {
256233
});
257234

258235
describe('loadSessionData with deduplication', () => {
259-
let fixture: Awaited<ReturnType<typeof createFixture>>;
260-
261-
afterEach(async () => {
262-
if (fixture != null) {
263-
await fixture.rm();
264-
}
265-
});
266-
267236
it('should deduplicate entries across sessions', async () => {
268-
fixture = await createFixture({
237+
await using fixture = await createFixture({
269238
projects: {
270239
project1: {
271240
session1: {

0 commit comments

Comments
 (0)