11import { mkdir , mkdtemp , rm , writeFile } from 'node:fs/promises' ;
22import { tmpdir } from 'node:os' ;
33import path from 'node:path' ;
4- import { describe , expect , it } from 'bun:test' ;
4+ import { afterEach , beforeEach , describe , expect , it } from 'bun:test' ;
55import {
66 createUniqueHash ,
77 getEarliestTimestamp ,
@@ -65,8 +65,17 @@ describe('deduplication functionality', () => {
6565 describe ( 'getEarliestTimestamp' , ( ) => {
6666 let tempDir : string ;
6767
68- it ( 'should extract earliest timestamp from JSONL file' , async ( ) => {
68+ beforeEach ( async ( ) => {
6969 tempDir = await mkdtemp ( path . join ( tmpdir ( ) , 'ccusage-test-' ) ) ;
70+ } ) ;
71+
72+ afterEach ( async ( ) => {
73+ if ( tempDir != null ) {
74+ await rm ( tempDir , { recursive : true , force : true } ) ;
75+ }
76+ } ) ;
77+
78+ it ( 'should extract earliest timestamp from JSONL file' , async ( ) => {
7079 const testFile = path . join ( tempDir , 'test.jsonl' ) ;
7180
7281 const content = [
@@ -79,12 +88,9 @@ describe('deduplication functionality', () => {
7988
8089 const timestamp = await getEarliestTimestamp ( testFile ) ;
8190 expect ( timestamp ) . toEqual ( new Date ( '2025-01-10T10:00:00Z' ) ) ;
82-
83- await rm ( tempDir , { recursive : true } ) ;
8491 } ) ;
8592
8693 it ( 'should handle files without timestamps' , async ( ) => {
87- tempDir = await mkdtemp ( path . join ( tmpdir ( ) , 'ccusage-test-' ) ) ;
8894 const testFile = path . join ( tempDir , 'test.jsonl' ) ;
8995
9096 const content = [
@@ -96,12 +102,9 @@ describe('deduplication functionality', () => {
96102
97103 const timestamp = await getEarliestTimestamp ( testFile ) ;
98104 expect ( timestamp ) . toBeNull ( ) ;
99-
100- await rm ( tempDir , { recursive : true } ) ;
101105 } ) ;
102106
103107 it ( 'should skip invalid JSON lines' , async ( ) => {
104- tempDir = await mkdtemp ( path . join ( tmpdir ( ) , 'ccusage-test-' ) ) ;
105108 const testFile = path . join ( tempDir , 'test.jsonl' ) ;
106109
107110 const content = [
@@ -114,17 +117,23 @@ describe('deduplication functionality', () => {
114117
115118 const timestamp = await getEarliestTimestamp ( testFile ) ;
116119 expect ( timestamp ) . toEqual ( new Date ( '2025-01-10T10:00:00Z' ) ) ;
117-
118- await rm ( tempDir , { recursive : true } ) ;
119120 } ) ;
120121 } ) ;
121122
122123 describe ( 'sortFilesByTimestamp' , ( ) => {
123124 let tempDir : string ;
124125
125- it ( 'should sort files by earliest timestamp' , async ( ) => {
126+ beforeEach ( async ( ) => {
126127 tempDir = await mkdtemp ( path . join ( tmpdir ( ) , 'ccusage-test-' ) ) ;
128+ } ) ;
127129
130+ afterEach ( async ( ) => {
131+ if ( tempDir != null ) {
132+ await rm ( tempDir , { recursive : true , force : true } ) ;
133+ }
134+ } ) ;
135+
136+ it ( 'should sort files by earliest timestamp' , async ( ) => {
128137 const file1 = path . join ( tempDir , 'file1.jsonl' ) ;
129138 const file2 = path . join ( tempDir , 'file2.jsonl' ) ;
130139 const file3 = path . join ( tempDir , 'file3.jsonl' ) ;
@@ -141,13 +150,9 @@ describe('deduplication functionality', () => {
141150 const sorted = await sortFilesByTimestamp ( [ file1 , file2 , file3 ] ) ;
142151
143152 expect ( sorted ) . toEqual ( [ file2 , file3 , file1 ] ) ; // Chronological order
144-
145- await rm ( tempDir , { recursive : true } ) ;
146153 } ) ;
147154
148155 it ( 'should place files without timestamps at the end' , async ( ) => {
149- tempDir = await mkdtemp ( path . join ( tmpdir ( ) , 'ccusage-test-' ) ) ;
150-
151156 const file1 = path . join ( tempDir , 'file1.jsonl' ) ;
152157 const file2 = path . join ( tempDir , 'file2.jsonl' ) ;
153158 const file3 = path . join ( tempDir , 'file3.jsonl' ) ;
@@ -159,8 +164,6 @@ describe('deduplication functionality', () => {
159164 const sorted = await sortFilesByTimestamp ( [ file1 , file2 , file3 ] ) ;
160165
161166 expect ( sorted ) . toEqual ( [ file3 , file1 , file2 ] ) ; // file2 without timestamp goes to end
162-
163- await rm ( tempDir , { recursive : true } ) ;
164167 } ) ;
165168 } ) ;
166169
0 commit comments