Skip to content

Commit f1591cf

Browse files
committed
add test
1 parent 43f6a38 commit f1591cf

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

__tests__/main.test.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,30 @@ jest.unstable_mockModule('@azure-rest/ai-inference', () => ({
2828
isUnexpected: jest.fn(() => false)
2929
}))
3030

31+
const mockExistsSync = jest.fn().mockReturnValue(true)
32+
const mockReadFileSync = jest.fn().mockReturnValue('Hello, AI!')
33+
34+
jest.unstable_mockModule('fs', () => ({
35+
existsSync: mockExistsSync,
36+
readFileSync: mockReadFileSync
37+
}))
38+
3139
jest.unstable_mockModule('@actions/core', () => core)
3240

3341
// The module being tested should be imported dynamically. This ensures that the
3442
// mocks are used in place of any actual dependencies.
3543
const { run } = await import('../src/main.js')
3644

3745
describe('main.ts', () => {
38-
beforeEach(() => {
46+
it('Sets the response output', async () => {
3947
// Set the action's inputs as return values from core.getInput().
4048
core.getInput.mockImplementation((name) => {
4149
if (name === 'prompt') return 'Hello, AI!'
4250
if (name === 'system_prompt') return 'You are a test assistant.'
4351
if (name === 'model_name') return 'gpt-4o'
4452
return ''
4553
})
46-
})
47-
48-
afterEach(() => {
49-
jest.resetAllMocks()
50-
})
5154

52-
it('Sets the response output', async () => {
5355
await run()
5456

5557
expect(core.setOutput).toHaveBeenNthCalledWith(
@@ -78,4 +80,29 @@ describe('main.ts', () => {
7880
// Verify that the action was marked as failed.
7981
expect(core.setFailed).toHaveBeenNthCalledWith(1, 'prompt is not set')
8082
})
83+
84+
it('uses prompt-file', async () => {
85+
const promptFile = 'prompt.txt'
86+
core.getInput.mockImplementation((name) => {
87+
if (name === 'prompt-file') return promptFile
88+
if (name === 'system-prompt') return 'You are a test assistant.'
89+
if (name === 'model-name') return 'gpt-4o'
90+
return ''
91+
})
92+
93+
await run()
94+
95+
expect(mockExistsSync).toHaveBeenCalledWith(promptFile)
96+
expect(mockReadFileSync).toHaveBeenCalledWith(promptFile, 'utf-8')
97+
expect(core.setOutput).toHaveBeenNthCalledWith(
98+
1,
99+
'response',
100+
'Hello, user!'
101+
)
102+
expect(core.setOutput).toHaveBeenNthCalledWith(
103+
2,
104+
'response-path',
105+
expect.stringContaining('modelResponse.txt')
106+
)
107+
})
81108
})

dist/index.js

Lines changed: 6 additions & 2 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.

src/main.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@ const RESPONSE_FILE = 'modelResponse.txt'
1515
export async function run(): Promise<void> {
1616
try {
1717
const promptFile: string = core.getInput('prompt-file')
18-
let prompt: string = core.getInput('prompt')
18+
const promptString: string = core.getInput('prompt')
1919

20+
let prompt: string
2021
if (promptFile !== undefined && promptFile !== '') {
2122
if (!fs.existsSync(promptFile)) {
2223
throw new Error(`Prompt file not found: ${promptFile}`)
2324
}
2425
prompt = fs.readFileSync(promptFile, 'utf-8')
25-
}
26-
27-
if (prompt === undefined || prompt === '') {
26+
} else if (promptString !== undefined && promptString !== '') {
27+
prompt = promptString
28+
} else {
2829
throw new Error('prompt is not set')
2930
}
3031

0 commit comments

Comments
 (0)