@@ -30,28 +30,39 @@ jest.unstable_mockModule('@azure-rest/ai-inference', () => ({
30
30
31
31
// Default to throwing errors to catch unexpected calls
32
32
const mockExistsSync = jest . fn ( ) . mockImplementation ( ( ) => {
33
- throw new Error ( 'Unexpected call to existsSync - test should override this implementation' )
33
+ throw new Error (
34
+ 'Unexpected call to existsSync - test should override this implementation'
35
+ )
34
36
} )
35
37
const mockReadFileSync = jest . fn ( ) . mockImplementation ( ( ) => {
36
- throw new Error ( 'Unexpected call to readFileSync - test should override this implementation' )
38
+ throw new Error (
39
+ 'Unexpected call to readFileSync - test should override this implementation'
40
+ )
37
41
} )
38
42
39
43
/**
40
44
* Helper function to mock file system operations for one or more files
41
45
* @param fileContents - Object mapping file paths to their contents
42
46
* @param nonExistentFiles - Array of file paths that should be treated as non-existent
43
47
*/
44
- function mockFileContent ( fileContents : Record < string , string > = { } , nonExistentFiles : string [ ] = [ ] ) : void {
48
+ function mockFileContent (
49
+ fileContents : Record < string , string > = { } ,
50
+ nonExistentFiles : string [ ] = [ ]
51
+ ) : void {
45
52
// Mock existsSync to return true for files that exist, false for those that don't
46
- mockExistsSync . mockImplementation ( function ( this : any , path : any ) : boolean {
53
+ mockExistsSync . mockImplementation ( function ( this : any , path : any ) : boolean {
47
54
if ( nonExistentFiles . includes ( path ) ) {
48
55
return false
49
56
}
50
57
return path in fileContents || true
51
58
} )
52
-
59
+
53
60
// Mock readFileSync to return the content for known files
54
- mockReadFileSync . mockImplementation ( function ( this : any , path : any , encoding : any ) : string {
61
+ mockReadFileSync . mockImplementation ( function (
62
+ this : any ,
63
+ path : any ,
64
+ encoding : any
65
+ ) : string {
55
66
if ( encoding === 'utf-8' && path in fileContents ) {
56
67
return fileContents [ path ]
57
68
}
@@ -66,12 +77,12 @@ function mockFileContent(fileContents: Record<string, string> = {}, nonExistentF
66
77
function mockInputs ( inputs : Record < string , string > = { } ) : void {
67
78
// Default values that are applied unless overridden
68
79
const defaultInputs : Record < string , string > = {
69
- ' token' : 'fake-token'
80
+ token : 'fake-token'
70
81
}
71
-
82
+
72
83
// Combine defaults with user-provided inputs
73
- const allInputs : Record < string , string > = { ...defaultInputs , ...inputs }
74
-
84
+ const allInputs : Record < string , string > = { ...defaultInputs , ...inputs }
85
+
75
86
core . getInput . mockImplementation ( ( name : string ) => {
76
87
return allInputs [ name ] || ''
77
88
} )
@@ -81,11 +92,7 @@ function mockInputs(inputs: Record<string, string> = {}): void {
81
92
* Helper function to verify common response assertions
82
93
*/
83
94
function verifyStandardResponse ( ) : void {
84
- expect ( core . setOutput ) . toHaveBeenNthCalledWith (
85
- 1 ,
86
- 'response' ,
87
- 'Hello, user!'
88
- )
95
+ expect ( core . setOutput ) . toHaveBeenNthCalledWith ( 1 , 'response' , 'Hello, user!' )
89
96
expect ( core . setOutput ) . toHaveBeenNthCalledWith (
90
97
2 ,
91
98
'response-file' ,
@@ -107,13 +114,13 @@ const { run } = await import('../src/main.js')
107
114
describe ( 'main.ts' , ( ) => {
108
115
// Reset all mocks before each test
109
116
beforeEach ( ( ) => {
110
- jest . clearAllMocks ( ) ;
111
- } ) ;
112
-
117
+ jest . clearAllMocks ( )
118
+ } )
119
+
113
120
it ( 'Sets the response output' , async ( ) => {
114
121
// Set the action's inputs as return values from core.getInput().
115
122
mockInputs ( {
116
- ' prompt' : 'Hello, AI!' ,
123
+ prompt : 'Hello, AI!' ,
117
124
'system-prompt' : 'You are a test assistant.'
118
125
} )
119
126
@@ -125,25 +132,28 @@ describe('main.ts', () => {
125
132
it ( 'Sets a failed status when no prompt is set' , async ( ) => {
126
133
// Clear the getInput mock and simulate no prompt or prompt-file input
127
134
mockInputs ( {
128
- ' prompt' : '' ,
135
+ prompt : '' ,
129
136
'prompt-file' : ''
130
137
} )
131
138
132
139
await run ( )
133
140
134
141
// Verify that the action was marked as failed.
135
- expect ( core . setFailed ) . toHaveBeenNthCalledWith ( 1 , 'Neither prompt-file nor prompt was set' )
142
+ expect ( core . setFailed ) . toHaveBeenNthCalledWith (
143
+ 1 ,
144
+ 'Neither prompt-file nor prompt was set'
145
+ )
136
146
} )
137
147
138
148
it ( 'uses prompt-file' , async ( ) => {
139
149
const promptFile = 'prompt.txt'
140
150
const promptContent = 'This is a prompt from a file'
141
-
151
+
142
152
// Set up mock to return specific content for the prompt file
143
153
mockFileContent ( {
144
154
[ promptFile ] : promptContent
145
155
} )
146
-
156
+
147
157
// Set up input mocks
148
158
mockInputs ( {
149
159
'prompt-file' : promptFile ,
@@ -159,10 +169,10 @@ describe('main.ts', () => {
159
169
160
170
it ( 'handles non-existent prompt-file with an error' , async ( ) => {
161
171
const promptFile = 'non-existent-prompt.txt'
162
-
172
+
163
173
// Mock the file not existing
164
174
mockFileContent ( { } , [ promptFile ] )
165
-
175
+
166
176
// Set up input mocks
167
177
mockInputs ( {
168
178
'prompt-file' : promptFile
@@ -188,7 +198,7 @@ describe('main.ts', () => {
188
198
189
199
// Set up input mocks
190
200
mockInputs ( {
191
- ' prompt' : promptString ,
201
+ prompt : promptString ,
192
202
'prompt-file' : promptFile ,
193
203
'system-prompt' : 'You are a test assistant.'
194
204
} )
@@ -197,7 +207,7 @@ describe('main.ts', () => {
197
207
198
208
expect ( mockExistsSync ) . toHaveBeenCalledWith ( promptFile )
199
209
expect ( mockReadFileSync ) . toHaveBeenCalledWith ( promptFile , 'utf-8' )
200
-
210
+
201
211
// Check that the post call was made with the prompt from the file, not the input parameter
202
212
expect ( mockPost ) . toHaveBeenCalledWith ( {
203
213
body : {
@@ -206,19 +216,20 @@ describe('main.ts', () => {
206
216
role : 'system' ,
207
217
content : expect . any ( String )
208
218
} ,
209
- { role : 'user' , content : promptFileContent } // Should use the file content, not the string input
219
+ { role : 'user' , content : promptFileContent } // Should use the file content, not the string input
210
220
] ,
211
221
max_tokens : expect . any ( Number ) ,
212
222
model : expect . any ( String )
213
223
}
214
224
} )
215
-
225
+
216
226
verifyStandardResponse ( )
217
227
} )
218
228
219
229
it ( 'uses system-prompt-file' , async ( ) => {
220
230
const systemPromptFile = 'system-prompt.txt'
221
- const systemPromptContent = 'You are a specialized system assistant for testing'
231
+ const systemPromptContent =
232
+ 'You are a specialized system assistant for testing'
222
233
223
234
// Set up mock to return specific content for the system prompt file
224
235
mockFileContent ( {
@@ -227,7 +238,7 @@ describe('main.ts', () => {
227
238
228
239
// Set up input mocks
229
240
mockInputs ( {
230
- ' prompt' : 'Hello, AI!' ,
241
+ prompt : 'Hello, AI!' ,
231
242
'system-prompt-file' : systemPromptFile
232
243
} )
233
244
@@ -237,16 +248,16 @@ describe('main.ts', () => {
237
248
expect ( mockReadFileSync ) . toHaveBeenCalledWith ( systemPromptFile , 'utf-8' )
238
249
verifyStandardResponse ( )
239
250
} )
240
-
251
+
241
252
it ( 'handles non-existent system-prompt-file with an error' , async ( ) => {
242
253
const systemPromptFile = 'non-existent-system-prompt.txt'
243
-
254
+
244
255
// Mock the file not existing
245
256
mockFileContent ( { } , [ systemPromptFile ] )
246
-
257
+
247
258
// Set up input mocks
248
259
mockInputs ( {
249
- ' prompt' : 'Hello, AI!' ,
260
+ prompt : 'Hello, AI!' ,
250
261
'system-prompt-file' : systemPromptFile
251
262
} )
252
263
@@ -257,11 +268,13 @@ describe('main.ts', () => {
257
268
`File for system-prompt-file was not found: ${ systemPromptFile } `
258
269
)
259
270
} )
260
-
271
+
261
272
it ( 'prefers system-prompt-file over system-prompt when both are provided' , async ( ) => {
262
273
const systemPromptFile = 'system-prompt.txt'
263
- const systemPromptFileContent = 'You are a specialized system assistant from file'
264
- const systemPromptString = 'You are a basic system assistant from input parameter'
274
+ const systemPromptFileContent =
275
+ 'You are a specialized system assistant from file'
276
+ const systemPromptString =
277
+ 'You are a basic system assistant from input parameter'
265
278
266
279
// Set up mock to return specific content for the system prompt file
267
280
mockFileContent ( {
@@ -270,7 +283,7 @@ describe('main.ts', () => {
270
283
271
284
// Set up input mocks
272
285
mockInputs ( {
273
- ' prompt' : 'Hello, AI!' ,
286
+ prompt : 'Hello, AI!' ,
274
287
'system-prompt-file' : systemPromptFile ,
275
288
'system-prompt' : systemPromptString
276
289
} )
@@ -279,7 +292,7 @@ describe('main.ts', () => {
279
292
280
293
expect ( mockExistsSync ) . toHaveBeenCalledWith ( systemPromptFile )
281
294
expect ( mockReadFileSync ) . toHaveBeenCalledWith ( systemPromptFile , 'utf-8' )
282
-
295
+
283
296
// Check that the post call was made with the system prompt from the file, not the input parameter
284
297
expect ( mockPost ) . toHaveBeenCalledWith ( {
285
298
body : {
@@ -294,22 +307,23 @@ describe('main.ts', () => {
294
307
model : expect . any ( String )
295
308
}
296
309
} )
297
-
310
+
298
311
verifyStandardResponse ( )
299
312
} )
300
-
313
+
301
314
it ( 'uses both prompt-file and system-prompt-file together' , async ( ) => {
302
315
const promptFile = 'prompt.txt'
303
316
const promptContent = 'This is a prompt from a file'
304
317
const systemPromptFile = 'system-prompt.txt'
305
- const systemPromptContent = 'You are a specialized system assistant from file'
318
+ const systemPromptContent =
319
+ 'You are a specialized system assistant from file'
306
320
307
321
// Set up mock to return specific content for both files
308
322
mockFileContent ( {
309
323
[ promptFile ] : promptContent ,
310
324
[ systemPromptFile ] : systemPromptContent
311
325
} )
312
-
326
+
313
327
// Set up input mocks
314
328
mockInputs ( {
315
329
'prompt-file' : promptFile ,
@@ -322,30 +336,30 @@ describe('main.ts', () => {
322
336
expect ( mockExistsSync ) . toHaveBeenCalledWith ( systemPromptFile )
323
337
expect ( mockReadFileSync ) . toHaveBeenCalledWith ( promptFile , 'utf-8' )
324
338
expect ( mockReadFileSync ) . toHaveBeenCalledWith ( systemPromptFile , 'utf-8' )
325
-
339
+
326
340
// Check that the post call was made with both the prompt and system prompt from files
327
341
expect ( mockPost ) . toHaveBeenCalledWith ( {
328
342
body : {
329
343
messages : [
330
344
{
331
345
role : 'system' ,
332
- content : systemPromptContent
346
+ content : systemPromptContent
333
347
} ,
334
348
{ role : 'user' , content : promptContent }
335
349
] ,
336
350
max_tokens : expect . any ( Number ) ,
337
351
model : expect . any ( String )
338
352
}
339
353
} )
340
-
354
+
341
355
verifyStandardResponse ( )
342
356
} )
343
-
357
+
344
358
it ( 'passes custom max-tokens parameter to the model' , async ( ) => {
345
359
const customMaxTokens = 500
346
-
360
+
347
361
mockInputs ( {
348
- ' prompt' : 'Hello, AI!' ,
362
+ prompt : 'Hello, AI!' ,
349
363
'system-prompt' : 'You are a test assistant.' ,
350
364
'max-tokens' : customMaxTokens . toString ( )
351
365
} )
@@ -360,7 +374,7 @@ describe('main.ts', () => {
360
374
model : expect . any ( String )
361
375
}
362
376
} )
363
-
377
+
364
378
verifyStandardResponse ( )
365
379
} )
366
380
} )
0 commit comments