@@ -19,8 +19,13 @@ import * as os from 'node:os';
1919import * as path from 'node:path' ;
2020import { describe , it , beforeEach , afterEach } from 'mocha' ;
2121import { expect } from 'chai' ;
22+ import sinon , { SinonStubbedInstance } from 'sinon' ;
2223import type { AgentPreviewSendResponse } from '@salesforce/agents' ;
23- import { saveTranscriptsToFile } from '../../src/components/agent-preview-react.js' ;
24+ import { PlannerResponse } from '@salesforce/agents/lib/types.js' ;
25+ import type { Logger } from '@salesforce/core' ;
26+ import type { AgentPreviewBase } from '@salesforce/agents' ;
27+ import { saveTranscriptsToFile , getTraces } from '../../src/components/agent-preview-react.js' ;
28+ import { trace1 , trace2 } from '../testData.js' ;
2429
2530describe ( 'AgentPreviewReact saveTranscriptsToFile' , ( ) => {
2631 let testDir : string ;
@@ -139,4 +144,76 @@ describe('AgentPreviewReact saveTranscriptsToFile', () => {
139144 // Should parse as valid JSON
140145 expect ( ( ) => JSON . parse ( content ) as unknown ) . to . not . throw ( ) ;
141146 } ) ;
147+
148+ it ( 'should write traces.json when traces are provided' , ( ) => {
149+ const outputDir = path . join ( testDir , 'output' ) ;
150+ const messages : Array < { timestamp : Date ; role : string ; content : string } > = [ ] ;
151+ const responses : AgentPreviewSendResponse [ ] = [ ] ;
152+ const traces : PlannerResponse [ ] = [ trace1 , trace2 ] ;
153+
154+ saveTranscriptsToFile ( outputDir , messages , responses , traces ) ;
155+
156+ const tracesPath = path . join ( outputDir , 'traces.json' ) ;
157+ expect ( fs . existsSync ( tracesPath ) ) . to . be . true ;
158+
159+ const content = JSON . parse ( fs . readFileSync ( tracesPath , 'utf8' ) ) as PlannerResponse [ ] ;
160+ expect ( content ) . to . have . lengthOf ( 2 ) ;
161+ } ) ;
162+ } ) ;
163+
164+ describe ( 'AgentPreviewReact getTraces' , ( ) => {
165+ let mockAgent : SinonStubbedInstance < AgentPreviewBase > ;
166+ let mockLogger : SinonStubbedInstance < Logger > ;
167+ const sessionId = 'session-123' ;
168+ const messageIds = [ 'msg-1' , 'msg-2' ] ;
169+
170+ beforeEach ( ( ) => {
171+ mockAgent = {
172+ traces : sinon . stub ( ) ,
173+ } as SinonStubbedInstance < AgentPreviewBase > ;
174+
175+ mockLogger = {
176+ info : sinon . stub ( ) ,
177+ } as SinonStubbedInstance < Logger > ;
178+ } ) ;
179+
180+ afterEach ( ( ) => {
181+ sinon . restore ( ) ;
182+ } ) ;
183+
184+ it ( 'should return traces when agent.traces succeeds' , async ( ) => {
185+ const expectedTraces : PlannerResponse [ ] = [ trace1 ] ;
186+
187+ mockAgent . traces . resolves ( expectedTraces ) ;
188+
189+ const result = await getTraces ( mockAgent , sessionId , messageIds , mockLogger ) ;
190+
191+ expect ( result ) . to . deep . equal ( expectedTraces ) ;
192+ expect ( mockAgent . traces . calledWith ( sessionId , messageIds ) ) . to . be . true ;
193+ expect ( mockLogger . info . called ) . to . be . false ;
194+ } ) ;
195+
196+ it ( 'should return empty array when agent.traces throws an error' , async ( ) => {
197+ const error = new Error ( 'Failed to get traces' ) ;
198+ mockAgent . traces . rejects ( error ) ;
199+
200+ const result = await getTraces ( mockAgent , sessionId , messageIds , mockLogger ) ;
201+
202+ expect ( result ) . to . deep . equal ( [ ] ) ;
203+ expect ( mockAgent . traces . calledWith ( sessionId , messageIds ) ) . to . be . true ;
204+ expect (
205+ mockLogger . info . calledWith ( 'Error obtaining traces: Error - Failed to get traces' , { sessionId, messageIds } )
206+ ) . to . be . true ;
207+ } ) ;
208+
209+ it ( 'should handle empty messageIds array' , async ( ) => {
210+ const expectedTraces : PlannerResponse [ ] = [ ] ;
211+ mockAgent . traces . resolves ( expectedTraces ) ;
212+
213+ const result = await getTraces ( mockAgent , sessionId , [ ] , mockLogger ) ;
214+
215+ expect ( result ) . to . deep . equal ( expectedTraces ) ;
216+ expect ( mockAgent . traces . notCalled ) . to . be . true ;
217+ expect ( mockLogger . info . called ) . to . be . false ;
218+ } ) ;
142219} ) ;
0 commit comments