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

View File

@@ -28,6 +28,14 @@ jest.unstable_mockModule('@azure-rest/ai-inference', () => ({
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)
// 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')
describe('main.ts', () => {
beforeEach(() => {
it('Sets the response output', async () => {
// Set the action's inputs as return values from core.getInput().
core.getInput.mockImplementation((name) => {
if (name === 'prompt') return 'Hello, AI!'
@@ -43,13 +51,7 @@ describe('main.ts', () => {
if (name === 'model_name') return 'gpt-4o'
return ''
})
})
afterEach(() => {
jest.resetAllMocks()
})
it('Sets the response output', async () => {
await run()
expect(core.setOutput).toHaveBeenNthCalledWith(
@@ -78,4 +80,29 @@ describe('main.ts', () => {
// Verify that the action was marked as failed.
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')
)
})
})

8
dist/index.js generated vendored
View File

@@ -33560,14 +33560,18 @@ const RESPONSE_FILE = 'modelResponse.txt';
async function run() {
try {
const promptFile = coreExports.getInput('prompt-file');
let prompt = coreExports.getInput('prompt');
const promptString = coreExports.getInput('prompt');
let prompt;
if (promptFile !== undefined && promptFile !== '') {
if (!fs.existsSync(promptFile)) {
throw new Error(`Prompt file not found: ${promptFile}`);
}
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');
}
const systemPrompt = coreExports.getInput('system-prompt');

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View File

@@ -15,16 +15,17 @@ const RESPONSE_FILE = 'modelResponse.txt'
export async function run(): Promise<void> {
try {
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 (!fs.existsSync(promptFile)) {
throw new Error(`Prompt file not found: ${promptFile}`)
}
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')
}