Skip to content

Commit eb37c9a

Browse files
committed
Formatting
1 parent 3e924fe commit eb37c9a

File tree

5 files changed

+98
-88
lines changed

5 files changed

+98
-88
lines changed

__tests__/main.test.ts

Lines changed: 65 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,39 @@ jest.unstable_mockModule('@azure-rest/ai-inference', () => ({
3030

3131
// Default to throwing errors to catch unexpected calls
3232
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+
)
3436
})
3537
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+
)
3741
})
3842

3943
/**
4044
* Helper function to mock file system operations for one or more files
4145
* @param fileContents - Object mapping file paths to their contents
4246
* @param nonExistentFiles - Array of file paths that should be treated as non-existent
4347
*/
44-
function mockFileContent(fileContents: Record<string, string> = {}, nonExistentFiles: string[] = []): void {
48+
function mockFileContent(
49+
fileContents: Record<string, string> = {},
50+
nonExistentFiles: string[] = []
51+
): void {
4552
// 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 {
4754
if (nonExistentFiles.includes(path)) {
4855
return false
4956
}
5057
return path in fileContents || true
5158
})
52-
59+
5360
// 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 {
5566
if (encoding === 'utf-8' && path in fileContents) {
5667
return fileContents[path]
5768
}
@@ -66,12 +77,12 @@ function mockFileContent(fileContents: Record<string, string> = {}, nonExistentF
6677
function mockInputs(inputs: Record<string, string> = {}): void {
6778
// Default values that are applied unless overridden
6879
const defaultInputs: Record<string, string> = {
69-
'token': 'fake-token'
80+
token: 'fake-token'
7081
}
71-
82+
7283
// Combine defaults with user-provided inputs
73-
const allInputs: Record<string, string> = {...defaultInputs, ...inputs}
74-
84+
const allInputs: Record<string, string> = { ...defaultInputs, ...inputs }
85+
7586
core.getInput.mockImplementation((name: string) => {
7687
return allInputs[name] || ''
7788
})
@@ -81,11 +92,7 @@ function mockInputs(inputs: Record<string, string> = {}): void {
8192
* Helper function to verify common response assertions
8293
*/
8394
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!')
8996
expect(core.setOutput).toHaveBeenNthCalledWith(
9097
2,
9198
'response-file',
@@ -107,13 +114,13 @@ const { run } = await import('../src/main.js')
107114
describe('main.ts', () => {
108115
// Reset all mocks before each test
109116
beforeEach(() => {
110-
jest.clearAllMocks();
111-
});
112-
117+
jest.clearAllMocks()
118+
})
119+
113120
it('Sets the response output', async () => {
114121
// Set the action's inputs as return values from core.getInput().
115122
mockInputs({
116-
'prompt': 'Hello, AI!',
123+
prompt: 'Hello, AI!',
117124
'system-prompt': 'You are a test assistant.'
118125
})
119126

@@ -125,25 +132,28 @@ describe('main.ts', () => {
125132
it('Sets a failed status when no prompt is set', async () => {
126133
// Clear the getInput mock and simulate no prompt or prompt-file input
127134
mockInputs({
128-
'prompt': '',
135+
prompt: '',
129136
'prompt-file': ''
130137
})
131138

132139
await run()
133140

134141
// 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+
)
136146
})
137147

138148
it('uses prompt-file', async () => {
139149
const promptFile = 'prompt.txt'
140150
const promptContent = 'This is a prompt from a file'
141-
151+
142152
// Set up mock to return specific content for the prompt file
143153
mockFileContent({
144154
[promptFile]: promptContent
145155
})
146-
156+
147157
// Set up input mocks
148158
mockInputs({
149159
'prompt-file': promptFile,
@@ -159,10 +169,10 @@ describe('main.ts', () => {
159169

160170
it('handles non-existent prompt-file with an error', async () => {
161171
const promptFile = 'non-existent-prompt.txt'
162-
172+
163173
// Mock the file not existing
164174
mockFileContent({}, [promptFile])
165-
175+
166176
// Set up input mocks
167177
mockInputs({
168178
'prompt-file': promptFile
@@ -188,7 +198,7 @@ describe('main.ts', () => {
188198

189199
// Set up input mocks
190200
mockInputs({
191-
'prompt': promptString,
201+
prompt: promptString,
192202
'prompt-file': promptFile,
193203
'system-prompt': 'You are a test assistant.'
194204
})
@@ -197,7 +207,7 @@ describe('main.ts', () => {
197207

198208
expect(mockExistsSync).toHaveBeenCalledWith(promptFile)
199209
expect(mockReadFileSync).toHaveBeenCalledWith(promptFile, 'utf-8')
200-
210+
201211
// Check that the post call was made with the prompt from the file, not the input parameter
202212
expect(mockPost).toHaveBeenCalledWith({
203213
body: {
@@ -206,19 +216,20 @@ describe('main.ts', () => {
206216
role: 'system',
207217
content: expect.any(String)
208218
},
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
210220
],
211221
max_tokens: expect.any(Number),
212222
model: expect.any(String)
213223
}
214224
})
215-
225+
216226
verifyStandardResponse()
217227
})
218228

219229
it('uses system-prompt-file', async () => {
220230
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'
222233

223234
// Set up mock to return specific content for the system prompt file
224235
mockFileContent({
@@ -227,7 +238,7 @@ describe('main.ts', () => {
227238

228239
// Set up input mocks
229240
mockInputs({
230-
'prompt': 'Hello, AI!',
241+
prompt: 'Hello, AI!',
231242
'system-prompt-file': systemPromptFile
232243
})
233244

@@ -237,16 +248,16 @@ describe('main.ts', () => {
237248
expect(mockReadFileSync).toHaveBeenCalledWith(systemPromptFile, 'utf-8')
238249
verifyStandardResponse()
239250
})
240-
251+
241252
it('handles non-existent system-prompt-file with an error', async () => {
242253
const systemPromptFile = 'non-existent-system-prompt.txt'
243-
254+
244255
// Mock the file not existing
245256
mockFileContent({}, [systemPromptFile])
246-
257+
247258
// Set up input mocks
248259
mockInputs({
249-
'prompt': 'Hello, AI!',
260+
prompt: 'Hello, AI!',
250261
'system-prompt-file': systemPromptFile
251262
})
252263

@@ -257,11 +268,13 @@ describe('main.ts', () => {
257268
`File for system-prompt-file was not found: ${systemPromptFile}`
258269
)
259270
})
260-
271+
261272
it('prefers system-prompt-file over system-prompt when both are provided', async () => {
262273
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'
265278

266279
// Set up mock to return specific content for the system prompt file
267280
mockFileContent({
@@ -270,7 +283,7 @@ describe('main.ts', () => {
270283

271284
// Set up input mocks
272285
mockInputs({
273-
'prompt': 'Hello, AI!',
286+
prompt: 'Hello, AI!',
274287
'system-prompt-file': systemPromptFile,
275288
'system-prompt': systemPromptString
276289
})
@@ -279,7 +292,7 @@ describe('main.ts', () => {
279292

280293
expect(mockExistsSync).toHaveBeenCalledWith(systemPromptFile)
281294
expect(mockReadFileSync).toHaveBeenCalledWith(systemPromptFile, 'utf-8')
282-
295+
283296
// Check that the post call was made with the system prompt from the file, not the input parameter
284297
expect(mockPost).toHaveBeenCalledWith({
285298
body: {
@@ -294,22 +307,23 @@ describe('main.ts', () => {
294307
model: expect.any(String)
295308
}
296309
})
297-
310+
298311
verifyStandardResponse()
299312
})
300-
313+
301314
it('uses both prompt-file and system-prompt-file together', async () => {
302315
const promptFile = 'prompt.txt'
303316
const promptContent = 'This is a prompt from a file'
304317
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'
306320

307321
// Set up mock to return specific content for both files
308322
mockFileContent({
309323
[promptFile]: promptContent,
310324
[systemPromptFile]: systemPromptContent
311325
})
312-
326+
313327
// Set up input mocks
314328
mockInputs({
315329
'prompt-file': promptFile,
@@ -322,30 +336,30 @@ describe('main.ts', () => {
322336
expect(mockExistsSync).toHaveBeenCalledWith(systemPromptFile)
323337
expect(mockReadFileSync).toHaveBeenCalledWith(promptFile, 'utf-8')
324338
expect(mockReadFileSync).toHaveBeenCalledWith(systemPromptFile, 'utf-8')
325-
339+
326340
// Check that the post call was made with both the prompt and system prompt from files
327341
expect(mockPost).toHaveBeenCalledWith({
328342
body: {
329343
messages: [
330344
{
331345
role: 'system',
332-
content: systemPromptContent
346+
content: systemPromptContent
333347
},
334348
{ role: 'user', content: promptContent }
335349
],
336350
max_tokens: expect.any(Number),
337351
model: expect.any(String)
338352
}
339353
})
340-
354+
341355
verifyStandardResponse()
342356
})
343-
357+
344358
it('passes custom max-tokens parameter to the model', async () => {
345359
const customMaxTokens = 500
346-
360+
347361
mockInputs({
348-
'prompt': 'Hello, AI!',
362+
prompt: 'Hello, AI!',
349363
'system-prompt': 'You are a test assistant.',
350364
'max-tokens': customMaxTokens.toString()
351365
})
@@ -360,7 +374,7 @@ describe('main.ts', () => {
360374
model: expect.any(String)
361375
}
362376
})
363-
377+
364378
verifyStandardResponse()
365379
})
366380
})

dist/index.js

Lines changed: 30 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)