Skip to content

Commit b5c9bc0

Browse files
committed
adding chat mocked tests with ts-mockito
1 parent 87967b4 commit b5c9bc0

2 files changed

Lines changed: 48 additions & 27 deletions

File tree

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"README.md"
1010
],
1111
"dependencies": {
12-
"log4js": "^6.9.1"
12+
"log4js": "^6.9.1",
13+
"ts-mockito": "^2.6.1"
1314
},
1415
"scripts": {
1516
"build": "tsc",

‎src/tests/ChatTest.test.ts‎

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,55 @@
11
import {CxWrapper} from '../main/wrapper/CxWrapper';
22
import {CxCommandOutput} from "../main/wrapper/CxCommandOutput";
3-
import {BaseTest} from "./BaseTest";
43
import CxChat from "../main/chat/CxChat";
4+
import {anything, instance, mock, when} from 'ts-mockito';
5+
import {BaseTest} from "./BaseTest";
6+
7+
function createOutput(exitCode:number,payload:CxChat):CxCommandOutput {
8+
const output = new CxCommandOutput();
9+
output.exitCode=exitCode;
10+
output.status="";
11+
output.payload=[payload];
12+
return output;
13+
}
14+
15+
describe("Gpt Chat Cases", () => {
16+
// tests preparation
17+
const cxScanConfig = new BaseTest();
18+
const mockedWrapper: CxWrapper = mock(CxWrapper);
19+
const originalWrapper: CxWrapper = new CxWrapper(cxScanConfig);
20+
const outputSuccessful = createOutput(0,new CxChat("CONVERSATION",["RESPONSE"] ));
521

6-
describe("Chat", () => {
7-
it('Try call', async () => {
8-
const config = new BaseTest();
9-
config.pathToExecutable = "path\\to\\cli\\dev\\build"
10-
const wrapper = new CxWrapper(config);
11-
let cxCommandOutput: CxCommandOutput = await wrapper.chat(
12-
"apikey",
13-
"path\\to\\result\\file",
14-
3,
15-
"MEDIUM",
16-
"Security Opt Not Set",
17-
"Explain the result",
18-
null,
19-
"gpt-3.5-turbo"
22+
when(mockedWrapper.chat("APIKEY","FILE",anything(),anything(),anything(),anything(),anything(), anything())).thenResolve(
23+
outputSuccessful
24+
);
25+
const wrapper: CxWrapper = instance(mockedWrapper);
26+
27+
it('Gpt Chat Successful case', async () => {
28+
const cxCommandOutput = await wrapper.chat(
29+
"APIKEY",
30+
"FILE",
31+
0,
32+
"SEVERITY",
33+
"VULNERABILITY",
34+
"INPUT",
35+
"CONVERSATION",
36+
"MODEL"
2037
);
2138
expect(cxCommandOutput.exitCode).toBe(0);
22-
const output: CxChat = cxCommandOutput.payload.pop();
23-
cxCommandOutput = await wrapper.chat(
24-
"apikey",
25-
"path\\to\\result\\file",
26-
3,
27-
"MEDIUM",
28-
"Security Opt Not Set",
29-
"Can you provide a snippet with the fix?",
30-
output.conversationId,
31-
"gpt-3.5-turbo"
39+
});
40+
41+
it('Gpt Chat Failed case', async () => {
42+
const cxCommandOutput = await originalWrapper.chat(
43+
"APIKEY",
44+
"",
45+
0,
46+
"SEVERITY",
47+
"VULNERABILITY",
48+
"INPUT",
49+
"CONVERSATION",
50+
"MODEL"
3251
);
33-
expect(cxCommandOutput.exitCode).toBe(0);
52+
expect(cxCommandOutput.exitCode).toBe(1);
53+
expect(cxCommandOutput.status).toBe("open : no such file or directory\n");
3454
});
3555
});

0 commit comments

Comments
 (0)