@@ -66,7 +66,7 @@ function mockInputs(inputs: Record<string, string> = {}): void {
66
66
*/
67
67
function verifyStandardResponse ( ) : void {
68
68
expect ( core . setOutput ) . toHaveBeenNthCalledWith ( 1 , 'response' , 'Hello, user!' )
69
- expect ( core . setOutput ) . toHaveBeenNthCalledWith ( 2 , 'response-file' , expect . stringContaining ( 'modelResponse.txt ' ) )
69
+ expect ( core . setOutput ) . toHaveBeenNthCalledWith ( 2 , 'response-file' , expect . stringContaining ( 'modelResponse- ' ) )
70
70
}
71
71
72
72
vi . mock ( 'fs' , ( ) => ( {
@@ -75,6 +75,19 @@ vi.mock('fs', () => ({
75
75
writeFileSync : mockWriteFileSync ,
76
76
} ) )
77
77
78
+ // Mocks for tmp module to control temporary file creation and cleanup
79
+ const mockRemoveCallback = vi . fn ( )
80
+ const mockFileSync = vi . fn ( ) . mockReturnValue ( {
81
+ name : '/secure/temp/dir/modelResponse-abc123.txt' ,
82
+ removeCallback : mockRemoveCallback ,
83
+ } )
84
+ const mockSetGracefulCleanup = vi . fn ( )
85
+
86
+ vi . mock ( 'tmp' , ( ) => ( {
87
+ fileSync : mockFileSync ,
88
+ setGracefulCleanup : mockSetGracefulCleanup ,
89
+ } ) )
90
+
78
91
// Mock MCP and inference modules
79
92
// eslint-disable-next-line @typescript-eslint/no-explicit-any
80
93
const mockConnectToGitHubMCP = vi . fn ( ) as MockedFunction < any >
@@ -269,4 +282,43 @@ describe('main.ts', () => {
269
282
expect ( core . setFailed ) . toHaveBeenCalledWith ( `File for prompt-file was not found: ${ promptFile } ` )
270
283
expect ( mockProcessExit ) . toHaveBeenCalledWith ( 1 )
271
284
} )
285
+
286
+ it ( 'creates secure temporary files with proper cleanup' , async ( ) => {
287
+ mockInputs ( {
288
+ prompt : 'Test prompt' ,
289
+ 'system-prompt' : 'You are a test assistant.' ,
290
+ } )
291
+
292
+ await run ( )
293
+
294
+ expect ( mockSetGracefulCleanup ) . toHaveBeenCalledOnce ( )
295
+
296
+ expect ( mockFileSync ) . toHaveBeenCalledWith ( {
297
+ prefix : 'modelResponse-' ,
298
+ postfix : '.txt' ,
299
+ } )
300
+
301
+ expect ( core . setOutput ) . toHaveBeenNthCalledWith ( 2 , 'response-file' , '/secure/temp/dir/modelResponse-abc123.txt' )
302
+ expect ( mockWriteFileSync ) . toHaveBeenCalledWith ( '/secure/temp/dir/modelResponse-abc123.txt' , 'Hello, user!' , 'utf-8' )
303
+ expect ( mockRemoveCallback ) . toHaveBeenCalledOnce ( )
304
+
305
+ expect ( mockProcessExit ) . toHaveBeenCalledWith ( 0 )
306
+ } )
307
+
308
+ it ( 'handles cleanup errors gracefully' , async ( ) => {
309
+ mockRemoveCallback . mockImplementationOnce ( ( ) => {
310
+ throw new Error ( 'Cleanup failed' )
311
+ } )
312
+
313
+ mockInputs ( {
314
+ prompt : 'Test prompt' ,
315
+ 'system-prompt' : 'You are a test assistant.' ,
316
+ } )
317
+
318
+ await run ( )
319
+
320
+ expect ( mockRemoveCallback ) . toHaveBeenCalledOnce ( )
321
+ expect ( core . warning ) . toHaveBeenCalledWith ( 'Failed to cleanup temporary file: Error: Cleanup failed' )
322
+ expect ( mockProcessExit ) . toHaveBeenCalledWith ( 0 )
323
+ } )
272
324
} )
0 commit comments