1
1
import { mkdir , rm , rmdir , writeFile } from "fs/promises" ;
2
- import generateDandoriTasks , { DandoriTask } from "../index" ;
2
+ import generateDandoriTasks , {
3
+ ChatGPTFunctionCallModel ,
4
+ DandoriTaskOptionalProperty ,
5
+ DandoriTaskProperty ,
6
+ DandoriTaskRequiredProperty ,
7
+ } from "../index" ;
3
8
import {
4
9
describe ,
5
10
beforeEach ,
@@ -184,12 +189,50 @@ describe("generateDandoriTasks", () => {
184
189
} ,
185
190
} ,
186
191
} as const ;
187
- const requiredProperties : readonly ( keyof DandoriTask ) [ ] = [
192
+ const requiredProperties : readonly DandoriTaskRequiredProperty [ ] = [
188
193
"id" ,
189
194
"name" ,
190
195
"fromTaskIdList" ,
191
196
] ;
192
197
const logPrefix = "Generating tasks" ;
198
+ const createOpenAiChatGptArguments = ( {
199
+ source,
200
+ model = "gpt-3.5-turbo-0613" ,
201
+ filter = Object . keys ( functionCallTaskProperties ) as DandoriTaskProperty [ ] ,
202
+ } : {
203
+ source : string ;
204
+ model ?: ChatGPTFunctionCallModel ;
205
+ filter ?: DandoriTaskProperty [ ] ;
206
+ } ) => ( {
207
+ messages : [ { role : "user" , content : source } ] ,
208
+ model,
209
+ function_call : { name : functionCallName } ,
210
+ functions : [
211
+ {
212
+ name : functionCallName ,
213
+ description :
214
+ "Get the tasks flow which will be used like Gantt chart." ,
215
+ parameters : {
216
+ type : "object" ,
217
+ properties : {
218
+ tasks : {
219
+ type : "array" ,
220
+ items : {
221
+ type : "object" ,
222
+ required : requiredProperties ,
223
+ properties : Object . fromEntries (
224
+ filter . map ( ( prop ) => [
225
+ prop ,
226
+ functionCallTaskProperties [ prop ] ,
227
+ ] ) ,
228
+ ) ,
229
+ } ,
230
+ } ,
231
+ } ,
232
+ } ,
233
+ } ,
234
+ ] ,
235
+ } ) ;
193
236
194
237
beforeEach ( ( ) => {
195
238
process . env [ openApiKeyPropName ] = apiKey ;
@@ -199,7 +242,7 @@ describe("generateDandoriTasks", () => {
199
242
expect ( process . env [ openApiKeyPropName ] ) . toBe ( apiKey ) ;
200
243
} ) ;
201
244
202
- describe ( "with model argument" , ( ) => {
245
+ describe ( "with options which include model argument" , ( ) => {
203
246
let result : Awaited < ReturnType < typeof generateDandoriTasks > > ;
204
247
const source = "with model argument" ;
205
248
const model = "gpt-4-0613" ;
@@ -211,33 +254,46 @@ describe("generateDandoriTasks", () => {
211
254
} ) ;
212
255
213
256
it ( "called chat.completions.create with valid arguments" , ( ) => {
214
- expect ( openAI . chat . completions . create ) . toBeCalledWith ( {
215
- messages : [ { role : "user" , content : source } ] ,
216
- model,
217
- function_call : { name : functionCallName } ,
218
- functions : [
219
- {
220
- name : functionCallName ,
221
- description :
222
- "Get the tasks flow which will be used like Gantt chart." ,
223
- parameters : {
224
- type : "object" ,
225
- properties : {
226
- tasks : {
227
- type : "array" ,
228
- items : {
229
- type : "object" ,
230
- required : requiredProperties ,
231
- properties : functionCallTaskProperties ,
232
- } ,
233
- } ,
234
- } ,
235
- } ,
236
- } ,
237
- ] ,
257
+ expect ( openAI . chat . completions . create ) . toBeCalledWith (
258
+ createOpenAiChatGptArguments ( { source, model } ) ,
259
+ ) ;
260
+ } ) ;
261
+
262
+ it ( "called logger.debug with valid arguments" , ( ) => {
263
+ expect ( logger . debug ) . toBeCalledWith ( openAiResArguments . tasks ) ;
264
+ } ) ;
265
+
266
+ it ( "return tasks" , ( ) => {
267
+ expect ( result ) . toStrictEqual ( openAiResArguments . tasks ) ;
268
+ } ) ;
269
+
270
+ it ( "called log with valid statement" , ( ) => {
271
+ expect ( runPromisesSequentiallyMock . mock . calls [ 0 ] [ 1 ] ) . toContain (
272
+ logPrefix ,
273
+ ) ;
274
+ } ) ;
275
+ } ) ;
276
+
277
+ describe ( "with options which include filter argument" , ( ) => {
278
+ let result : Awaited < ReturnType < typeof generateDandoriTasks > > ;
279
+ const source = "with filter argument" ;
280
+ const filter : DandoriTaskOptionalProperty [ ] = [ "deadline" ] ;
281
+
282
+ beforeEach ( async ( ) => {
283
+ result = await generateDandoriTasks ( source , {
284
+ filter,
238
285
} ) ;
239
286
} ) ;
240
287
288
+ it ( "called chat.completions.create with valid arguments" , ( ) => {
289
+ expect ( openAI . chat . completions . create ) . toBeCalledWith (
290
+ createOpenAiChatGptArguments ( {
291
+ source,
292
+ filter : [ ...filter , ...requiredProperties ] ,
293
+ } ) ,
294
+ ) ;
295
+ } ) ;
296
+
241
297
it ( "called logger.debug with valid arguments" , ( ) => {
242
298
expect ( logger . debug ) . toBeCalledWith ( openAiResArguments . tasks ) ;
243
299
} ) ;
@@ -253,7 +309,7 @@ describe("generateDandoriTasks", () => {
253
309
} ) ;
254
310
} ) ;
255
311
256
- describe ( "without model argument " , ( ) => {
312
+ describe ( "without options " , ( ) => {
257
313
let result : Awaited < ReturnType < typeof generateDandoriTasks > > ;
258
314
const source = "without model argument" ;
259
315
@@ -262,31 +318,9 @@ describe("generateDandoriTasks", () => {
262
318
} ) ;
263
319
264
320
it ( "called chat.completions.create with valid arguments" , ( ) => {
265
- expect ( openAI . chat . completions . create ) . toBeCalledWith ( {
266
- messages : [ { role : "user" , content : source } ] ,
267
- model : "gpt-3.5-turbo-0613" ,
268
- function_call : { name : functionCallName } ,
269
- functions : [
270
- {
271
- name : functionCallName ,
272
- description :
273
- "Get the tasks flow which will be used like Gantt chart." ,
274
- parameters : {
275
- type : "object" ,
276
- properties : {
277
- tasks : {
278
- type : "array" ,
279
- items : {
280
- type : "object" ,
281
- required : requiredProperties ,
282
- properties : functionCallTaskProperties ,
283
- } ,
284
- } ,
285
- } ,
286
- } ,
287
- } ,
288
- ] ,
289
- } ) ;
321
+ expect ( openAI . chat . completions . create ) . toBeCalledWith (
322
+ createOpenAiChatGptArguments ( { source } ) ,
323
+ ) ;
290
324
} ) ;
291
325
292
326
it ( "called logger.debug with valid arguments" , ( ) => {
0 commit comments