This commit is contained in:
Aiqiao Yan
2025-04-17 20:13:47 +00:00
parent 43f6a3831f
commit f1591cfa68
4 changed files with 46 additions and 14 deletions
+34 -7
View File
@@ -28,6 +28,14 @@ jest.unstable_mockModule('@azure-rest/ai-inference', () => ({
isUnexpected: jest.fn(() => false) isUnexpected: jest.fn(() => false)
})) }))
const mockExistsSync = jest.fn().mockReturnValue(true)
const mockReadFileSync = jest.fn().mockReturnValue('Hello, AI!')
jest.unstable_mockModule('fs', () => ({
existsSync: mockExistsSync,
readFileSync: mockReadFileSync
}))
jest.unstable_mockModule('@actions/core', () => core) jest.unstable_mockModule('@actions/core', () => core)
// The module being tested should be imported dynamically. This ensures that the // The module being tested should be imported dynamically. This ensures that the
@@ -35,7 +43,7 @@ jest.unstable_mockModule('@actions/core', () => core)
const { run } = await import('../src/main.js') const { run } = await import('../src/main.js')
describe('main.ts', () => { describe('main.ts', () => {
beforeEach(() => { it('Sets the response output', async () => {
// Set the action's inputs as return values from core.getInput(). // Set the action's inputs as return values from core.getInput().
core.getInput.mockImplementation((name) => { core.getInput.mockImplementation((name) => {
if (name === 'prompt') return 'Hello, AI!' if (name === 'prompt') return 'Hello, AI!'
@@ -43,13 +51,7 @@ describe('main.ts', () => {
if (name === 'model_name') return 'gpt-4o' if (name === 'model_name') return 'gpt-4o'
return '' return ''
}) })
})
afterEach(() => {
jest.resetAllMocks()
})
it('Sets the response output', async () => {
await run() await run()
expect(core.setOutput).toHaveBeenNthCalledWith( expect(core.setOutput).toHaveBeenNthCalledWith(
@@ -78,4 +80,29 @@ describe('main.ts', () => {
// Verify that the action was marked as failed. // Verify that the action was marked as failed.
expect(core.setFailed).toHaveBeenNthCalledWith(1, 'prompt is not set') expect(core.setFailed).toHaveBeenNthCalledWith(1, 'prompt is not set')
}) })
it('uses prompt-file', async () => {
const promptFile = 'prompt.txt'
core.getInput.mockImplementation((name) => {
if (name === 'prompt-file') return promptFile
if (name === 'system-prompt') return 'You are a test assistant.'
if (name === 'model-name') return 'gpt-4o'
return ''
})
await run()
expect(mockExistsSync).toHaveBeenCalledWith(promptFile)
expect(mockReadFileSync).toHaveBeenCalledWith(promptFile, 'utf-8')
expect(core.setOutput).toHaveBeenNthCalledWith(
1,
'response',
'Hello, user!'
)
expect(core.setOutput).toHaveBeenNthCalledWith(
2,
'response-path',
expect.stringContaining('modelResponse.txt')
)
})
}) })
Generated Vendored
+6 -2
View File
@@ -33560,14 +33560,18 @@ const RESPONSE_FILE = 'modelResponse.txt';
async function run() { async function run() {
try { try {
const promptFile = coreExports.getInput('prompt-file'); const promptFile = coreExports.getInput('prompt-file');
let prompt = coreExports.getInput('prompt'); const promptString = coreExports.getInput('prompt');
let prompt;
if (promptFile !== undefined && promptFile !== '') { if (promptFile !== undefined && promptFile !== '') {
if (!fs.existsSync(promptFile)) { if (!fs.existsSync(promptFile)) {
throw new Error(`Prompt file not found: ${promptFile}`); throw new Error(`Prompt file not found: ${promptFile}`);
} }
prompt = fs.readFileSync(promptFile, 'utf-8'); prompt = fs.readFileSync(promptFile, 'utf-8');
} }
if (prompt === undefined || prompt === '') { else if (promptString !== undefined && promptString !== '') {
prompt = promptString;
}
else {
throw new Error('prompt is not set'); throw new Error('prompt is not set');
} }
const systemPrompt = coreExports.getInput('system-prompt'); const systemPrompt = coreExports.getInput('system-prompt');
Generated Vendored
+1 -1
View File
File diff suppressed because one or more lines are too long
+5 -4
View File
@@ -15,16 +15,17 @@ const RESPONSE_FILE = 'modelResponse.txt'
export async function run(): Promise<void> { export async function run(): Promise<void> {
try { try {
const promptFile: string = core.getInput('prompt-file') const promptFile: string = core.getInput('prompt-file')
let prompt: string = core.getInput('prompt') const promptString: string = core.getInput('prompt')
let prompt: string
if (promptFile !== undefined && promptFile !== '') { if (promptFile !== undefined && promptFile !== '') {
if (!fs.existsSync(promptFile)) { if (!fs.existsSync(promptFile)) {
throw new Error(`Prompt file not found: ${promptFile}`) throw new Error(`Prompt file not found: ${promptFile}`)
} }
prompt = fs.readFileSync(promptFile, 'utf-8') prompt = fs.readFileSync(promptFile, 'utf-8')
} } else if (promptString !== undefined && promptString !== '') {
prompt = promptString
if (prompt === undefined || prompt === '') { } else {
throw new Error('prompt is not set') throw new Error('prompt is not set')
} }