1
1
import { describe , it , expect , beforeEach , jest } from '@jest/globals'
2
- import * as core from '@actions/core'
3
- import * as fs from 'fs'
4
- import * as path from 'path'
5
- import { fileURLToPath } from 'url'
6
- import { run } from '../src/main'
7
-
8
- const __filename = fileURLToPath ( import . meta. url )
9
- const __dirname = path . dirname ( __filename )
10
-
11
- // Mock the action toolkit functions
12
- jest . mock ( '@actions/core' )
13
-
14
- // Mock fs to handle temporary file creation
15
- jest . mock ( 'fs' )
2
+ import * as core from '../__fixtures__/core.js'
3
+
4
+ // Create fs mocks
5
+ const mockExistsSync = jest . fn ( )
6
+ const mockReadFileSync = jest . fn ( )
7
+ const mockWriteFileSync = jest . fn ( )
8
+
9
+ // Create inference mocks
10
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
+ const mockSimpleInference = jest . fn ( ) as jest . MockedFunction < any >
12
+ const mockMcpInference = jest . fn ( )
13
+
14
+ // Create MCP mocks
15
+ const mockConnectToGitHubMCP = jest . fn ( )
16
+
17
+ // Mock fs module
18
+ jest . unstable_mockModule ( 'fs' , ( ) => ( {
19
+ existsSync : mockExistsSync ,
20
+ readFileSync : mockReadFileSync ,
21
+ writeFileSync : mockWriteFileSync
22
+ } ) )
16
23
17
24
// Mock the inference functions
18
- jest . mock ( '../src/inference' , ( ) => ( {
19
- simpleInference : jest . fn ( ) ,
20
- mcpInference : jest . fn ( )
25
+ jest . unstable_mockModule ( '../src/inference.js ' , ( ) => ( {
26
+ simpleInference : mockSimpleInference ,
27
+ mcpInference : mockMcpInference
21
28
} ) )
22
29
23
30
// Mock the MCP connection
24
- jest . mock ( '../src/mcp' , ( ) => ( {
25
- connectToGitHubMCP : jest . fn ( )
31
+ jest . unstable_mockModule ( '../src/mcp.js ' , ( ) => ( {
32
+ connectToGitHubMCP : mockConnectToGitHubMCP
26
33
} ) )
27
34
28
- import { simpleInference } from '../src/inference'
35
+ jest . unstable_mockModule ( '@actions/core' , ( ) => core )
36
+
37
+ // The module being tested should be imported dynamically. This ensures that the
38
+ // mocks are used in place of any actual dependencies.
39
+ const { run } = await import ( '../src/main.js' )
29
40
30
41
describe ( 'main.ts - prompt.yml integration' , ( ) => {
31
42
beforeEach ( ( ) => {
@@ -35,8 +46,7 @@ describe('main.ts - prompt.yml integration', () => {
35
46
process . env [ 'GITHUB_TOKEN' ] = 'test-token'
36
47
37
48
// Mock core.getInput to return appropriate values
38
- const mockGetInput = core . getInput as jest . Mock
39
- mockGetInput . mockImplementation ( ( name : string ) => {
49
+ core . getInput . mockImplementation ( ( name : string ) => {
40
50
switch ( name ) {
41
51
case 'model' :
42
52
return 'openai/gpt-4o'
@@ -55,12 +65,7 @@ describe('main.ts - prompt.yml integration', () => {
55
65
const mockGetBooleanInput = core . getBooleanInput as jest . Mock
56
66
mockGetBooleanInput . mockReturnValue ( false )
57
67
58
- // Mock fs.existsSync
59
- const mockExistsSync = fs . existsSync as jest . Mock
60
- mockExistsSync . mockReturnValue ( true )
61
-
62
68
// Mock fs.readFileSync for prompt file
63
- const mockReadFileSync = fs . readFileSync as jest . Mock
64
69
mockReadFileSync . mockReturnValue ( `
65
70
messages:
66
71
- role: system
@@ -71,17 +76,15 @@ model: openai/gpt-4o
71
76
` )
72
77
73
78
// Mock fs.writeFileSync
74
- const mockWriteFileSync = fs . writeFileSync as jest . Mock
75
79
mockWriteFileSync . mockImplementation ( ( ) => { } )
76
80
77
81
// Mock simpleInference
78
- const mockSimpleInference = simpleInference as jest . Mock
79
82
mockSimpleInference . mockResolvedValue ( 'Mocked AI response' )
80
83
} )
81
84
82
85
it ( 'should handle prompt YAML files with template variables' , async ( ) => {
83
- const mockGetInput = core . getInput as jest . Mock
84
- mockGetInput . mockImplementation ( ( name : string ) => {
86
+ mockExistsSync . mockReturnValue ( true )
87
+ core . getInput . mockImplementation ( ( name : string ) => {
85
88
switch ( name ) {
86
89
case 'prompt-file' :
87
90
return 'test.prompt.yml'
@@ -103,7 +106,6 @@ model: openai/gpt-4o
103
106
await run ( )
104
107
105
108
// Verify simpleInference was called with the correct message structure
106
- const mockSimpleInference = simpleInference as jest . Mock
107
109
expect ( mockSimpleInference ) . toHaveBeenCalledWith (
108
110
expect . objectContaining ( {
109
111
messages : [
@@ -135,8 +137,8 @@ model: openai/gpt-4o
135
137
} )
136
138
137
139
it ( 'should fall back to legacy format when not using prompt YAML' , async ( ) => {
138
- const mockGetInput = core . getInput as jest . Mock
139
- mockGetInput . mockImplementation ( ( name : string ) => {
140
+ mockExistsSync . mockReturnValue ( false )
141
+ core . getInput . mockImplementation ( ( name : string ) => {
140
142
switch ( name ) {
141
143
case 'prompt' :
142
144
return 'Hello, world!'
@@ -157,12 +159,19 @@ model: openai/gpt-4o
157
159
158
160
await run ( )
159
161
160
- // Verify simpleInference was called with legacy format
161
- const mockSimpleInference = simpleInference as jest . Mock
162
+ // Verify simpleInference was called with converted message format
162
163
expect ( mockSimpleInference ) . toHaveBeenCalledWith (
163
164
expect . objectContaining ( {
164
- systemPrompt : 'You are helpful' ,
165
- prompt : 'Hello, world!' ,
165
+ messages : [
166
+ {
167
+ role : 'system' ,
168
+ content : 'You are helpful'
169
+ } ,
170
+ {
171
+ role : 'user' ,
172
+ content : 'Hello, world!'
173
+ }
174
+ ] ,
166
175
modelName : 'openai/gpt-4o' ,
167
176
maxTokens : 200 ,
168
177
endpoint : 'https://models.github.ai/inference' ,
0 commit comments