Skip to content

Commit c052964

Browse files
test: add UTs
1 parent 8578b30 commit c052964

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

src/components/agent-preview-react.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function Typing(): React.ReactNode {
4949
);
5050
}
5151

52-
const saveTranscriptsToFile = (
52+
export const saveTranscriptsToFile = (
5353
outputDir: string,
5454
messages: Array<{ timestamp: Date; role: string; content: string }>,
5555
responses: AgentPreviewSendResponse[]
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright 2025, Salesforce, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as fs from 'node:fs';
18+
import * as os from 'node:os';
19+
import * as path from 'node:path';
20+
import { describe, it, beforeEach, afterEach } from 'mocha';
21+
import { expect } from 'chai';
22+
import type { AgentPreviewSendResponse } from '@salesforce/agents';
23+
import { saveTranscriptsToFile } from '../../src/components/agent-preview-react.js';
24+
25+
describe('AgentPreviewReact saveTranscriptsToFile', () => {
26+
let testDir: string;
27+
28+
beforeEach(() => {
29+
testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-preview-test-'));
30+
});
31+
32+
afterEach(() => {
33+
if (fs.existsSync(testDir)) {
34+
fs.rmSync(testDir, { recursive: true, force: true });
35+
}
36+
});
37+
38+
it('should create output directory if it does not exist', () => {
39+
const outputDir = path.join(testDir, 'nested', 'directory');
40+
const messages: Array<{ timestamp: Date; role: string; content: string }> = [];
41+
const responses: AgentPreviewSendResponse[] = [];
42+
43+
saveTranscriptsToFile(outputDir, messages, responses);
44+
45+
expect(fs.existsSync(outputDir)).to.be.true;
46+
});
47+
48+
it('should write transcript.json with messages', () => {
49+
const outputDir = path.join(testDir, 'output');
50+
const messages: Array<{ timestamp: Date; role: string; content: string }> = [
51+
{ timestamp: new Date('2025-01-01T00:00:00Z'), role: 'user', content: 'Hello' },
52+
{ timestamp: new Date('2025-01-01T00:00:01Z'), role: 'agent', content: 'Hi there' },
53+
];
54+
const responses: AgentPreviewSendResponse[] = [];
55+
56+
saveTranscriptsToFile(outputDir, messages, responses);
57+
58+
const transcriptPath = path.join(outputDir, 'transcript.json');
59+
expect(fs.existsSync(transcriptPath)).to.be.true;
60+
61+
const content = JSON.parse(fs.readFileSync(transcriptPath, 'utf8')) as Array<{
62+
role: string;
63+
content: string;
64+
}>;
65+
expect(content).to.have.lengthOf(2);
66+
expect(content[0]?.role).to.equal('user');
67+
expect(content[0]?.content).to.equal('Hello');
68+
expect(content[1]?.role).to.equal('agent');
69+
expect(content[1]?.content).to.equal('Hi there');
70+
});
71+
72+
it('should write responses.json with responses', () => {
73+
const outputDir = path.join(testDir, 'output');
74+
const messages: Array<{ timestamp: Date; role: string; content: string }> = [];
75+
const responses: AgentPreviewSendResponse[] = [
76+
{
77+
messages: [{ message: 'Response 1' }],
78+
},
79+
{
80+
messages: [{ message: 'Response 2' }],
81+
},
82+
] as unknown as AgentPreviewSendResponse[];
83+
84+
saveTranscriptsToFile(outputDir, messages, responses);
85+
86+
const responsesPath = path.join(outputDir, 'responses.json');
87+
expect(fs.existsSync(responsesPath)).to.be.true;
88+
89+
const content = JSON.parse(fs.readFileSync(responsesPath, 'utf8')) as Array<{
90+
messages: Array<{ message: string }>;
91+
}>;
92+
expect(content).to.have.lengthOf(2);
93+
expect(content[0]?.messages[0]?.message).to.equal('Response 1');
94+
expect(content[1]?.messages[0]?.message).to.equal('Response 2');
95+
});
96+
97+
it('should write both transcript.json and responses.json', () => {
98+
const outputDir = path.join(testDir, 'output');
99+
const messages: Array<{ timestamp: Date; role: string; content: string }> = [
100+
{ timestamp: new Date(), role: 'user', content: 'Test' },
101+
];
102+
const responses: AgentPreviewSendResponse[] = [
103+
{
104+
messages: [{ message: 'Test response' }],
105+
},
106+
] as unknown as AgentPreviewSendResponse[];
107+
108+
saveTranscriptsToFile(outputDir, messages, responses);
109+
110+
expect(fs.existsSync(path.join(outputDir, 'transcript.json'))).to.be.true;
111+
expect(fs.existsSync(path.join(outputDir, 'responses.json'))).to.be.true;
112+
});
113+
114+
it('should not create files if outputDir is empty string', () => {
115+
const outputDir = '';
116+
const messages: Array<{ timestamp: Date; role: string; content: string }> = [
117+
{ timestamp: new Date(), role: 'user', content: 'Test' },
118+
];
119+
const responses: AgentPreviewSendResponse[] = [];
120+
121+
// Should not throw
122+
expect(() => saveTranscriptsToFile(outputDir, messages, responses)).to.not.throw();
123+
});
124+
125+
it('should format JSON with proper indentation', () => {
126+
const outputDir = path.join(testDir, 'output');
127+
const messages: Array<{ timestamp: Date; role: string; content: string }> = [
128+
{ timestamp: new Date('2025-01-01T00:00:00Z'), role: 'user', content: 'Test' },
129+
];
130+
const responses: AgentPreviewSendResponse[] = [];
131+
132+
saveTranscriptsToFile(outputDir, messages, responses);
133+
134+
const transcriptPath = path.join(outputDir, 'transcript.json');
135+
const content = fs.readFileSync(transcriptPath, 'utf8');
136+
137+
// Should have newlines (pretty-printed JSON)
138+
expect(content).to.include('\n');
139+
// Should parse as valid JSON
140+
expect(() => JSON.parse(content) as unknown).to.not.throw();
141+
});
142+
});

0 commit comments

Comments
 (0)