11import { fetchTraceRecords , getTrace } from '../get-trace' ;
22import type { FetchTraceRecordsOptions } from '../types' ;
3- import { afterEach , describe , expect , it , vi } from 'vitest' ;
3+ import { existsSync , mkdtempSync , readFileSync , rmSync } from 'node:fs' ;
4+ import { tmpdir } from 'node:os' ;
5+ import { join } from 'node:path' ;
6+ import { afterAll , afterEach , beforeAll , describe , expect , it , vi } from 'vitest' ;
47
58const { mockSend } = vi . hoisted ( ( ) => ( {
69 mockSend : vi . fn ( ) ,
@@ -22,13 +25,6 @@ vi.mock('../../../aws', () => ({
2225 getCredentialProvider : vi . fn ( ) . mockReturnValue ( { } ) ,
2326} ) ) ;
2427
25- vi . mock ( 'node:fs' , ( ) => ( {
26- default : {
27- mkdirSync : vi . fn ( ) ,
28- writeFileSync : vi . fn ( ) ,
29- } ,
30- } ) ) ;
31-
3228const baseOptions : FetchTraceRecordsOptions = {
3329 region : 'us-west-2' ,
3430 runtimeId : 'runtime-123' ,
@@ -183,11 +179,19 @@ describe('fetchTraceRecords', () => {
183179} ) ;
184180
185181describe ( 'getTrace' , ( ) => {
182+ let tmpDir : string ;
183+
184+ beforeAll ( ( ) => {
185+ tmpDir = mkdtempSync ( join ( tmpdir ( ) , 'get-trace-test-' ) ) ;
186+ } ) ;
187+
188+ afterAll ( ( ) => {
189+ rmSync ( tmpDir , { recursive : true , force : true } ) ;
190+ } ) ;
191+
186192 afterEach ( ( ) => vi . clearAllMocks ( ) ) ;
187193
188194 it ( 'calls fetchTraceRecords and writes result to disk' , async ( ) => {
189- const fs = await import ( 'node:fs' ) ;
190-
191195 mockSend . mockResolvedValueOnce ( { queryId : 'q-1' } ) . mockResolvedValueOnce ( {
192196 status : 'Complete' ,
193197 results : [
@@ -198,36 +202,38 @@ describe('getTrace', () => {
198202 ] ,
199203 } ) ;
200204
205+ const outputPath = join ( tmpDir , 'test-trace.json' ) ;
201206 const result = await getTrace ( {
202207 region : 'us-west-2' ,
203208 runtimeId : 'runtime-123' ,
204209 agentName : 'my-agent' ,
205210 traceId : 'abc123def456' ,
206- outputPath : '/tmp/test-trace.json' ,
211+ outputPath,
207212 startTime : 1000000 ,
208213 endTime : 2000000 ,
209214 } ) ;
210215
211216 expect ( result . success ) . toBe ( true ) ;
212217 expect ( result . filePath ) . toContain ( 'test-trace.json' ) ;
213- expect ( fs . default . mkdirSync ) . toHaveBeenCalled ( ) ;
214- expect ( fs . default . writeFileSync ) . toHaveBeenCalledWith ( '/tmp/test-trace.json' , expect . stringContaining ( '"traceId"' ) ) ;
218+ const content = JSON . parse ( readFileSync ( outputPath , 'utf-8' ) ) ;
219+ expect ( content ) . toHaveLength ( 1 ) ;
220+ expect ( content [ 0 ] [ '@message' ] ) . toEqual ( { traceId : 'abc123' } ) ;
215221 } ) ;
216222
217223 it ( 'returns error from fetchTraceRecords without writing file' , async ( ) => {
218- const fs = await import ( 'node:fs' ) ;
219-
224+ const outputPath = join ( tmpDir , 'should-not-exist.json' ) ;
220225 const result = await getTrace ( {
221226 region : 'us-west-2' ,
222227 runtimeId : 'runtime-123' ,
223228 agentName : 'my-agent' ,
224229 traceId : 'invalid!@#$' ,
230+ outputPath,
225231 startTime : 1000000 ,
226232 endTime : 2000000 ,
227233 } ) ;
228234
229235 expect ( result . success ) . toBe ( false ) ;
230236 expect ( result . error ) . toContain ( 'Invalid trace ID format' ) ;
231- expect ( fs . default . writeFileSync ) . not . toHaveBeenCalled ( ) ;
237+ expect ( existsSync ( outputPath ) ) . toBe ( false ) ;
232238 } ) ;
233239} ) ;
0 commit comments