Repository navigation
Expand file tree
/
Copy pathwolverine.test.js
More file actions
128 lines (104 loc) · 3.75 KB
/
Copy pathwolverine.test.js
File metadata and controls
128 lines (104 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const fs = require("fs");
const { Configuration, OpenAIApi } = require("openai");
const { sendErrorToGpt4, runScript, applyChanges } = require("./wolverine");
jest.mock("fs");
jest.mock("openai");
describe("runScript", () => {
beforeEach(() => {
fs.spawnSync.mockReset();
});
it("calls spawnSync with the expected parameters", () => {
const expectedScriptName = "script.js";
const expectedArgs = ["arg1", "arg2"];
const expectedOptions = { encoding: "utf-8" };
runScript(expectedScriptName, expectedArgs);
expect(fs.spawnSync).toHaveBeenCalledWith(
process.execPath,
[expectedScriptName, ...expectedArgs],
expectedOptions
);
});
it("returns the output and return code", () => {
const expectedOutput = "output content";
const expectedReturnCode = 0;
fs.spawnSync.mockReturnValueOnce({
stdout: "",
stderr: "",
status: expectedReturnCode,
});
fs.spawnSync.mockReturnValueOnce({
stdout: expectedOutput,
stderr: "",
status: expectedReturnCode,
});
let result = runScript("script.js", ["arg1", "arg2"]);
expect(result.output).toBe(expectedOutput);
expect(result.returnCode).toBe(expectedReturnCode);
result = runScript("script.js", ["arg1", "arg2"]);
expect(result.output).toBe(expectedOutput);
expect(result.returnCode).toBe(expectedReturnCode);
});
});
describe('sendErrorToGpt4', () => {
beforeEach(() => {
fs.readFileSync.mockReset();
fs.readFileSync.mockReturnValue('dummy-key');
openai.createChatCompletion.mockReset();
});
it('calls createChatCompletion with the expected parameters', async () => {
const expectedPrompt = 'prompt text';
const expectedModel = 'gpt-3.5-turbo';
const expectedMessages = [{ role: 'user', content: expectedPrompt }];
const expectedTemperature = 1.0;
const expectedApiKey = 'dummy-key';
fs.readFileSync.mockReturnValueOnce(expectedPrompt);
openai.createChatCompletion.mockResolvedValueOnce({
data: {
choices: [{ message: { content: 'response' } }],
},
});
const configuration = new Configuration({ apiKey: expectedApiKey });
const openai = new OpenAIApi(configuration);
await sendErrorToGpt4('file/path', ['arg1', 'arg2'], 'error message');
expect(openai.createChatCompletion).toHaveBeenCalledWith({
model: expectedModel,
messages: expectedMessages,
temperature: expectedTemperature,
});
});
it('returns the response content', async () => {
const expectedResponse = 'response content';
openai.createChatCompletion.mockResolvedValueOnce({
data: {
choices: [{ message: { content: expectedResponse } }],
},
});
const result = await sendErrorToGpt4(
'file/path',
['arg1', 'arg2'],
'error message'
);
expect(result).toBe(expectedResponse);
});
});
describe("applyChanges", () => {
beforeEach(() => {
fs.readFileSync.mockReset();
fs.writeFileSync.mockReset();
});
it("updates the file with the expected changes", () => {
const expectedFilePath = "file/path";
const expectedOriginalFileContent = "original file content";
const expectedChangesJson =
'[{"operation":"Replace","line":1,"content":"new content"}]';
const expectedNewFileContent = "new content";
fs.readFileSync.mockReturnValueOnce(expectedOriginalFileContent);
fs.readFileSync.mockReturnValueOnce(expectedChangesJson);
applyChanges(expectedFilePath, expectedChangesJson);
expect(fs.readFileSync).toHaveBeenCalledWith(expectedFilePath, "utf-8");
expect(fs.writeFileSync).toHaveBeenCalledWith(
expectedFilePath,
expectedNewFileContent
);
});
});