forked from StartAutomating/PowerShellAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-GPT4Completion.tests.ps1
322 lines (232 loc) · 10.1 KB
/
Get-GPT4Completion.tests.ps1
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
Import-Module "$PSScriptRoot\..\PowerShellAI.psd1" -Force
Describe "Get-GPT4Completion" -Tag GPT4Completion {
BeforeAll {
$script:savedKey = $env:OpenAIKey
$env:OpenAIKey = 'sk-1234567890'
Set-chatSessionPath -Path 'TestDrive:\PowerShell\ChatGPT'
Mock Invoke-RestMethodWithProgress -ModuleName PowerShellAI -ParameterFilter {
$Params.Method -eq 'Post' -and $Params.Uri -eq (Get-OpenAIChatCompletionUri)
} -MockWith {
[PSCustomObject]@{
choices = @(
[PSCustomObject]@{
message = [PSCustomObject]@{
content = 'Mocked Get-GPT4Completion call'
}
}
)
}
}
}
BeforeEach {
Stop-Chat
Clear-ChatMessages
Get-ChatSessionPath | Get-ChildItem -ErrorAction SilentlyContinue | Remove-Item -Force
}
AfterAll {
$env:OpenAIKey = $savedKey
Stop-Chat
}
It 'Test if chat is in progress initially' {
$actual = Test-ChatInProgress
$actual | Should -BeFalse
}
It "Test Get-GPT4Completion function exists" {
$actual = Get-Command Get-GPT4Completion -ErrorAction SilentlyContinue
$actual | Should -Not -BeNullOrEmpty
}
It 'Test Test-ChatInProgress function exists' {
$actual = Get-Command Test-ChatInProgress -ErrorAction SilentlyContinue
$actual | Should -Not -BeNullOrEmpty
}
It 'Test Stop-Chat function exists' {
$actual = Get-Command Stop-Chat -ErrorAction SilentlyContinue
$actual | Should -Not -BeNullOrEmpty
}
It "Test chat alias exists" {
$actual = Get-Alias chat -ErrorAction SilentlyContinue
$actual | Should -Not -BeNullOrEmpty
$actual.Definition | Should -Be Get-GPT4Completion
}
It 'Test Add-ChatMessage function exists' {
$actual = Get-Command Add-ChatMessage -ErrorAction SilentlyContinue
$actual | Should -Not -BeNullOrEmpty
}
It 'Test New-ChatMessageTemplate function exists' {
$actual = Get-Command New-ChatMessageTemplate -ErrorAction SilentlyContinue
$actual | Should -Not -BeNullOrEmpty
}
It 'Test if chat is in progress after message' {
$null = Get-GPT4Completion 'test'
$actual = Test-ChatInProgress
$actual | Should -BeTrue
}
It 'Test if chat is in progress after New-Chat' {
$null = New-Chat
$actual = Test-ChatInProgress
$actual | Should -BeTrue
}
It 'Test if chat is in progress after New-ChatMessage and then New-Chat' {
$null = New-ChatMessage -Role user -Content 'test'
$actual = Test-ChatInProgress
$actual | Should -BeTrue
New-Chat
$actual = Test-ChatInProgress
$actual | Should -BeTrue
}
It 'Test New-ChatMessageTemplate function exists' {
$actual = Get-Command New-ChatMessageTemplate -ErrorAction SilentlyContinue
$actual | Should -Not -BeNullOrEmpty
}
It 'Test New-ChatMessageTemplate has these parameters' {
$actual = Get-Command New-ChatMessageTemplate
$keys = $actual.Parameters.keys
$keys.Contains("Role") | Should -BeTrue
$keys.Contains("Content") | Should -BeTrue
$keys.Contains("Name") | Should -BeTrue
}
It 'Test New-ChatMessageTemplate Role paramater has this set' {
$actual = Get-Command New-ChatMessageTemplate
$validateSet = $actual.Parameters.Role.Attributes | Where-Object { $_ -is [System.Management.Automation.ValidateSetAttribute] }
$validateSet | Should -Not -BeNullOrEmpty
$validateSet.ValidValues.Count | Should -Be 4
$validateSet.ValidValues[0] | Should -BeExactly 'user'
$validateSet.ValidValues[1] | Should -BeExactly 'system'
$validateSet.ValidValues[2] | Should -BeExactly 'assistant'
$validateSet.ValidValues[3] | Should -BeExactly 'function'
}
It 'Test New-ChatMessageTemplate throws when role=function and name=null' {
{New-ChatMessageTemplate -Role function} | Should -Throw "Name is required if role is function"
}
It 'Test New-ChatMessageTemplate role is function and name provided' {
$actual = New-ChatMessageTemplate -Role function -Content 'It is hot!' -Name 'Get-Weather'
$actual | Should -Not -BeNullOrEmpty
$actual.role | Should -Be 'function'
$actual.name | Should -BeExactly 'Get-Weather'
$actual.content | Should -BeExactly 'It is hot!'
}
It 'Test if Add-ChatMessage has these parameters' {
$actual = Get-Command Add-ChatMessage
$keys = $actual.Parameters.keys
$keys.Contains("Message") | Should -BeTrue
}
It "Tests Get-GPT4Completion has these parameters" {
$actual = Get-Command Get-GPT4Completion -ErrorAction SilentlyContinue
$keys = $actual.Parameters.keys
$keys.Contains("Content") | Should -BeTrue
}
It 'Test Add-ChatMessage adds message' {
Add-ChatMessage -Message ([PSCustomObject]@{
role = 'system'
content = 'system test'
})
Add-ChatMessage -Message ([PSCustomObject]@{
role = 'user'
content = 'user test'
})
Add-ChatMessage -Message ([PSCustomObject]@{
role = 'assistant'
content = 'assistant test'
})
Add-ChatMessage -Message ([PSCustomObject]@{
role = 'function'
content = 'function test'
})
$actual = Get-ChatMessages
$actual.Count | Should -Be 4
$actual[0].role | Should -Be 'system'
$actual[0].content | Should -Be 'system test'
$actual[1].role | Should -Be 'user'
$actual[1].content | Should -Be 'user test'
$actual[2].role | Should -Be 'assistant'
$actual[2].content | Should -Be 'assistant test'
$actual[3].role | Should -Be 'function'
$actual[3].content | Should -Be 'function test'
}
It 'Test New-ChatMessageTemplate creates and populates template' {
$actual = New-ChatMessageTemplate -Role user -Content 'test'
$actual.role | Should -Be 'user'
$actual.content | Should -Be 'test'
}
It 'Test New-ChatMessageTemplate creates empty template' {
$actual = New-ChatMessageTemplate
$actual.role | Should -BeNullOrEmpty
$actual.content | Should -BeNullOrEmpty
}
It 'Test if Stop-Chat stops chat and resets messages' {
$null = New-Chat 'test'
$actual = Test-ChatInProgress
$actual | Should -BeTrue
Stop-Chat
$actual = Test-ChatInProgress
$actual | Should -BeFalse
(Get-ChatMessages).Count | Should -Be 0
}
It 'Test message is added via New-Chat' {
$actual = New-Chat 'test system message'
$actual | Should -BeExactly 'Mocked Get-GPT4Completion call'
$messages = Get-ChatMessages
$messages.Count | Should -Be 2
$messages[0].role | Should -BeExactly 'system'
$messages[0].content | Should -BeExactly 'test system message'
$messages[1].role | Should -BeExactly 'assistant'
$messages[1].content | Should -BeExactly 'Mocked Get-GPT4Completion call'
}
It 'Test message is added via chat' {
$actual = Get-GPT4Completion 'test user message'
$actual | Should -BeExactly 'Mocked Get-GPT4Completion call'
$messages = Get-ChatMessages
$messages.Count | Should -Be 2
$messages[0].role | Should -BeExactly 'user'
$messages[0].content | Should -BeExactly 'test user message'
$messages[1].role | Should -BeExactly 'assistant'
$messages[1].content | Should -BeExactly 'Mocked Get-GPT4Completion call'
}
It 'Test message is added via New-Chat and Test-ChatInProgress' {
Test-ChatInProgress | Should -BeFalse
$actual = New-Chat 'test system message'
$actual | Should -BeExactly 'Mocked Get-GPT4Completion call'
Test-ChatInProgress | Should -BeTrue
Stop-Chat
Test-ChatinProgress | Should -BeFalse
}
It 'Test message is added via chat and Test-ChatInProgress' {
Test-ChatInProgress | Should -BeFalse
$actual = Get-GPT4Completion 'test user message'
$actual | Should -BeExactly 'Mocked Get-GPT4Completion call'
Test-ChatInProgress | Should -BeTrue
Stop-Chat
Test-ChatinProgress | Should -BeFalse
}
It 'Test system message is added via New-Chat and Export works' {
$actual = New-Chat 'test system message'
$actual | Should -BeExactly 'Mocked Get-GPT4Completion call'
$sessions = Get-ChatSession
$sessions.Count | Should -Be 1
$content = $sessions | Get-ChatSessionContent
$content.Count | Should -Be 2
$content[0].role | Should -BeExactly 'system'
$content[0].content | Should -BeExactly 'test system message'
$content[1].role | Should -BeExactly 'assistant'
$content[1].content | Should -BeExactly 'Mocked Get-GPT4Completion call'
}
It 'Test user message is added via chat and Export works' {
$actual = Get-GPT4Completion 'test user message'
$actual | Should -BeExactly 'Mocked Get-GPT4Completion call'
$sessions = Get-ChatSession
$sessions.Count | Should -Be 1
$content = $sessions | Get-ChatSessionContent
$content.Count | Should -Be 2
$content[0].role | Should -BeExactly 'user'
$content[0].content | Should -BeExactly 'test user message'
$content[1].role | Should -BeExactly 'assistant'
$content[1].content | Should -BeExactly 'Mocked Get-GPT4Completion call'
}
It 'Test temperature is set calling chat' {
$actual = Get-GPT4Completion 'test user message' -temperature 1
(Get-ChatSessionOptions)['temperature'] | Should -Be 1
$actual | Should -BeExactly 'Mocked Get-GPT4Completion call'
Reset-ChatSessionOptions
(Get-ChatSessionOptions)['temperature'] | Should -Be 0.0
}
}