@@ -66,7 +66,7 @@ function mockInputs(inputs: Record<string, string> = {}): void {
6666 */
6767function verifyStandardResponse ( ) : void {
6868 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- ' ) )
7070}
7171
7272vi . mock ( 'fs' , ( ) => ( {
@@ -75,6 +75,19 @@ vi.mock('fs', () => ({
7575 writeFileSync : mockWriteFileSync ,
7676} ) )
7777
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+
7891// Mock MCP and inference modules
7992// eslint-disable-next-line @typescript-eslint/no-explicit-any
8093const mockConnectToGitHubMCP = vi . fn ( ) as MockedFunction < any >
@@ -269,4 +282,43 @@ describe('main.ts', () => {
269282 expect ( core . setFailed ) . toHaveBeenCalledWith ( `File for prompt-file was not found: ${ promptFile } ` )
270283 expect ( mockProcessExit ) . toHaveBeenCalledWith ( 1 )
271284 } )
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+ } )
272324} )
0 commit comments