Skip to content

Commit 4685e0d

Browse files
committed
Force exit once inference finishes in case we are holding any connections open
1 parent 0cbed4a commit 4685e0d

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

__tests__/main-prompt-integration.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,20 @@ vi.mock('../src/mcp.js', () => ({
3434

3535
vi.mock('@actions/core', () => core)
3636

37+
// Mock process.exit to prevent it from actually exiting during tests
38+
const mockProcessExit = vi.spyOn(process, 'exit').mockImplementation(() => {
39+
// Prevent actual exit, but don't throw - just return
40+
return undefined as never
41+
})
42+
3743
// The module being tested should be imported dynamically. This ensures that the
3844
// mocks are used in place of any actual dependencies.
3945
const {run} = await import('../src/main.js')
4046

4147
describe('main.ts - prompt.yml integration', () => {
4248
beforeEach(() => {
4349
vi.clearAllMocks()
50+
mockProcessExit.mockClear()
4451

4552
// Mock environment variables
4653
process.env['GITHUB_TOKEN'] = 'test-token'
@@ -103,8 +110,12 @@ model: openai/gpt-4o
103110
}
104111
})
105112

113+
// Expect the run function to complete successfully
106114
await run()
107115

116+
// Verify process.exit was called with code 0 (success)
117+
expect(mockProcessExit).toHaveBeenCalledWith(0)
118+
108119
// Verify simpleInference was called with the correct message structure
109120
expect(mockSimpleInference).toHaveBeenCalledWith(
110121
expect.objectContaining({
@@ -171,6 +182,9 @@ model: openai/gpt-4o
171182
messages: [{role: 'user', content: 'Here is the data: FILE_CONTENTS'}],
172183
}),
173184
)
185+
186+
// Verify process.exit was called with code 0 (success)
187+
expect(mockProcessExit).toHaveBeenCalledWith(0)
174188
})
175189

176190
it('should fall back to legacy format when not using prompt YAML', async () => {
@@ -215,5 +229,8 @@ model: openai/gpt-4o
215229
token: 'test-token',
216230
}),
217231
)
232+
233+
// Verify process.exit was called with code 0 (success)
234+
expect(mockProcessExit).toHaveBeenCalledWith(0)
218235
})
219236
})

__tests__/main.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ vi.mock('@actions/core', () => core)
9696

9797
// Mock process.exit to prevent it from actually exiting during tests
9898
const mockProcessExit = vi.spyOn(process, 'exit').mockImplementation(() => {
99-
throw new Error('process.exit called')
99+
// Prevent actual exit, but don't throw - just return
100+
return undefined as never
100101
})
101102

102103
// The module being tested should be imported dynamically. This ensures that the
@@ -127,6 +128,7 @@ describe('main.ts', () => {
127128

128129
expect(core.setOutput).toHaveBeenCalled()
129130
verifyStandardResponse()
131+
expect(mockProcessExit).toHaveBeenCalledWith(0)
130132
})
131133

132134
it('Sets a failed status when no prompt is set', async () => {
@@ -135,8 +137,7 @@ describe('main.ts', () => {
135137
'prompt-file': '',
136138
})
137139

138-
// Expect the run function to throw due to process.exit being mocked
139-
await expect(run()).rejects.toThrow('process.exit called')
140+
await run()
140141

141142
expect(core.setFailed).toHaveBeenCalledWith('Neither prompt-file nor prompt was set')
142143
expect(mockProcessExit).toHaveBeenCalledWith(1)
@@ -165,6 +166,7 @@ describe('main.ts', () => {
165166
expect(mockConnectToGitHubMCP).not.toHaveBeenCalled()
166167
expect(mockMcpInference).not.toHaveBeenCalled()
167168
verifyStandardResponse()
169+
expect(mockProcessExit).toHaveBeenCalledWith(0)
168170
})
169171

170172
it('uses MCP inference when enabled and connection succeeds', async () => {
@@ -197,6 +199,7 @@ describe('main.ts', () => {
197199
)
198200
expect(mockSimpleInference).not.toHaveBeenCalled()
199201
verifyStandardResponse()
202+
expect(mockProcessExit).toHaveBeenCalledWith(0)
200203
})
201204

202205
it('falls back to simple inference when MCP connection fails', async () => {
@@ -215,6 +218,7 @@ describe('main.ts', () => {
215218
expect(mockMcpInference).not.toHaveBeenCalled()
216219
expect(core.warning).toHaveBeenCalledWith('MCP connection failed, falling back to simple inference')
217220
verifyStandardResponse()
221+
expect(mockProcessExit).toHaveBeenCalledWith(0)
218222
})
219223

220224
it('properly integrates with loadContentFromFileOrInput', async () => {
@@ -248,6 +252,7 @@ describe('main.ts', () => {
248252
responseFormat: undefined,
249253
})
250254
verifyStandardResponse()
255+
expect(mockProcessExit).toHaveBeenCalledWith(0)
251256
})
252257

253258
it('handles non-existent prompt-file with an error', async () => {
@@ -259,8 +264,7 @@ describe('main.ts', () => {
259264
'prompt-file': promptFile,
260265
})
261266

262-
// Expect the run function to throw due to process.exit being mocked
263-
await expect(run()).rejects.toThrow('process.exit called')
267+
await run()
264268

265269
expect(core.setFailed).toHaveBeenCalledWith(`File for prompt-file was not found: ${promptFile}`)
266270
expect(mockProcessExit).toHaveBeenCalledWith(1)

dist/index.js

Lines changed: 2 additions & 0 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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ export async function run(): Promise<void> {
105105
} else {
106106
core.setFailed(`An unexpected error occurred: ${JSON.stringify(error, null, 2)}`)
107107
}
108-
109108
// Force exit to prevent hanging on open connections
110109
process.exit(1)
111110
}
111+
112+
// Force exit to prevent hanging on open connections
113+
process.exit(0)
112114
}
113115

114116
function tempDir(): string {

0 commit comments

Comments
 (0)