fixup tests and npm run all
This commit is contained in:
+33
-23
@@ -7,11 +7,29 @@
|
||||
*/
|
||||
import { jest } from '@jest/globals'
|
||||
import * as core from '../__fixtures__/core.js'
|
||||
import { wait } from '../__fixtures__/wait.js'
|
||||
|
||||
// Mocks should be declared before the module being tested is imported.
|
||||
const mockPost = jest.fn().mockImplementation(() => ({
|
||||
body: {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'Hello, user!'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}))
|
||||
|
||||
jest.unstable_mockModule('@azure-rest/ai-inference', () => ({
|
||||
default: jest.fn(() => ({
|
||||
path: jest.fn(() => ({
|
||||
post: mockPost
|
||||
}))
|
||||
})),
|
||||
isUnexpected: jest.fn(() => false)
|
||||
}))
|
||||
|
||||
jest.unstable_mockModule('@actions/core', () => core)
|
||||
jest.unstable_mockModule('../src/wait.js', () => ({ wait }))
|
||||
|
||||
// The module being tested should be imported dynamically. This ensures that the
|
||||
// mocks are used in place of any actual dependencies.
|
||||
@@ -20,43 +38,35 @@ const { run } = await import('../src/main.js')
|
||||
describe('main.ts', () => {
|
||||
beforeEach(() => {
|
||||
// Set the action's inputs as return values from core.getInput().
|
||||
core.getInput.mockImplementation(() => '500')
|
||||
|
||||
// Mock the wait function so that it does not actually wait.
|
||||
wait.mockImplementation(() => Promise.resolve('done!'))
|
||||
core.getInput.mockImplementation((name) => {
|
||||
if (name === 'prompt') return 'Hello, AI!'
|
||||
if (name === 'system_prompt') return 'You are a test assistant.'
|
||||
if (name === 'model_name') return 'gpt-4o'
|
||||
return ''
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks()
|
||||
})
|
||||
|
||||
it('Sets the time output', async () => {
|
||||
it('Sets the response output', async () => {
|
||||
await run()
|
||||
|
||||
// Verify the time output was set.
|
||||
expect(core.setOutput).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'time',
|
||||
// Simple regex to match a time string in the format HH:MM:SS.
|
||||
expect.stringMatching(/^\d{2}:\d{2}:\d{2}/)
|
||||
'response',
|
||||
'Hello, user!'
|
||||
)
|
||||
})
|
||||
|
||||
it('Sets a failed status', async () => {
|
||||
// Clear the getInput mock and return an invalid value.
|
||||
core.getInput.mockClear().mockReturnValueOnce('this is not a number')
|
||||
|
||||
// Clear the wait mock and return a rejected promise.
|
||||
wait
|
||||
.mockClear()
|
||||
.mockRejectedValueOnce(new Error('milliseconds is not a number'))
|
||||
// Clear the getInput mock and return an empty prompt
|
||||
core.getInput.mockClear().mockReturnValueOnce('')
|
||||
|
||||
await run()
|
||||
|
||||
// Verify that the action was marked as failed.
|
||||
expect(core.setFailed).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'milliseconds is not a number'
|
||||
)
|
||||
expect(core.setFailed).toHaveBeenNthCalledWith(1, 'prompt is not set')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
/**
|
||||
* Unit tests for src/wait.ts
|
||||
*/
|
||||
import { wait } from '../src/wait.js'
|
||||
|
||||
describe('wait.ts', () => {
|
||||
it('Throws an invalid number', async () => {
|
||||
const input = parseInt('foo', 10)
|
||||
|
||||
expect(isNaN(input)).toBe(true)
|
||||
|
||||
await expect(wait(input)).rejects.toThrow('milliseconds is not a number')
|
||||
})
|
||||
|
||||
it('Waits with a valid number', async () => {
|
||||
const start = new Date()
|
||||
await wait(500)
|
||||
const end = new Date()
|
||||
|
||||
const delta = Math.abs(end.getTime() - start.getTime())
|
||||
|
||||
expect(delta).toBeGreaterThan(450)
|
||||
})
|
||||
})
|
||||
+1
-1
@@ -1 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="106" height="20" role="img" aria-label="Coverage: 100%"><title>Coverage: 100%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="106" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="63" height="20" fill="#555"/><rect x="63" width="43" height="20" fill="#4c1"/><rect width="106" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="325" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="530">Coverage</text><text x="325" y="140" transform="scale(.1)" fill="#fff" textLength="530">Coverage</text><text aria-hidden="true" x="835" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="330">100%</text><text x="835" y="140" transform="scale(.1)" fill="#fff" textLength="330">100%</text></g></svg>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="116" height="20" role="img" aria-label="Coverage: 88.88%"><title>Coverage: 88.88%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="116" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="63" height="20" fill="#555"/><rect x="63" width="53" height="20" fill="#dfb317"/><rect width="116" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="325" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="530">Coverage</text><text x="325" y="140" transform="scale(.1)" fill="#fff" textLength="530">Coverage</text><text aria-hidden="true" x="885" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">88.88%</text><text x="885" y="140" transform="scale(.1)" fill="#fff" textLength="430">88.88%</text></g></svg>
|
||||
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
+6350
-38
File diff suppressed because it is too large
Load Diff
+1
-1
File diff suppressed because one or more lines are too long
@@ -10,6 +10,10 @@ import { AzureKeyCredential } from '@azure/core-auth'
|
||||
export async function run(): Promise<void> {
|
||||
try {
|
||||
const prompt: string = core.getInput('prompt')
|
||||
if (prompt === undefined || prompt === '') {
|
||||
throw new Error('prompt is not set')
|
||||
}
|
||||
|
||||
const systemPrompt: string = core.getInput('system_prompt')
|
||||
const modelName = core.getInput('model_name') || 'gpt-4o'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user