11import { describe , it , expect , beforeEach , vi } from "vitest" ;
22import fs from "fs" ;
33import path from "path" ;
4- // Mock the global objects that GitHub Actions provides
54const mockCore = {
6- // Core logging functions
75 debug : vi . fn ( ) ,
86 info : vi . fn ( ) ,
97 notice : vi . fn ( ) ,
108 warning : vi . fn ( ) ,
119 error : vi . fn ( ) ,
12- // Core workflow functions
1310 setFailed : vi . fn ( ) ,
1411 setOutput : vi . fn ( ) ,
1512 exportVariable : vi . fn ( ) ,
1613 setSecret : vi . fn ( ) ,
17- // Input/state functions
1814 getInput : vi . fn ( ) ,
1915 getBooleanInput : vi . fn ( ) ,
2016 getMultilineInput : vi . fn ( ) ,
2117 getState : vi . fn ( ) ,
2218 saveState : vi . fn ( ) ,
23- // Group functions
2419 startGroup : vi . fn ( ) ,
2520 endGroup : vi . fn ( ) ,
2621 group : vi . fn ( ) ,
27- // Other utility functions
2822 addPath : vi . fn ( ) ,
2923 setCommandEcho : vi . fn ( ) ,
3024 isDebug : vi . fn ( ) . mockReturnValue ( ! 1 ) ,
3125 getIDToken : vi . fn ( ) ,
3226 toPlatformPath : vi . fn ( ) ,
3327 toPosixPath : vi . fn ( ) ,
3428 toWin32Path : vi . fn ( ) ,
35- // Summary object with chainable methods
3629 summary : { addRaw : vi . fn ( ) . mockReturnThis ( ) , write : vi . fn ( ) . mockResolvedValue ( ) } ,
3730 } ,
3831 mockExec = { exec : vi . fn ( ) } ,
3932 mockGithub = { graphql : vi . fn ( ) } ,
4033 mockContext = { repo : { owner : "testowner" , repo : "testrepo" } } ;
41- // Set up global variables
4234( ( global . core = mockCore ) ,
4335 ( global . exec = mockExec ) ,
4436 ( global . github = mockGithub ) ,
4537 ( global . context = mockContext ) ,
4638 describe ( "assign_issue.cjs" , ( ) => {
4739 let assignIssueScript ;
4840 ( beforeEach ( ( ) => {
49- // Reset all mocks
50- ( vi . clearAllMocks ( ) ,
51- // Reset environment variables
52- delete process . env . GH_TOKEN ,
53- delete process . env . ASSIGNEE ,
54- delete process . env . ISSUE_NUMBER ) ;
55- // Read the script content
41+ ( vi . clearAllMocks ( ) , delete process . env . GH_TOKEN , delete process . env . ASSIGNEE , delete process . env . ISSUE_NUMBER ) ;
5642 const scriptPath = path . join ( process . cwd ( ) , "assign_issue.cjs" ) ;
5743 assignIssueScript = fs . readFileSync ( scriptPath , "utf8" ) ;
5844 } ) ,
@@ -61,7 +47,6 @@ const mockCore = {
6147 ( ( process . env . ASSIGNEE = "test-user" ) ,
6248 ( process . env . ISSUE_NUMBER = "123" ) ,
6349 delete process . env . GH_TOKEN ,
64- // Execute the script
6550 await eval ( `(async () => { ${ assignIssueScript } })()` ) ,
6651 expect ( mockCore . setFailed ) . toHaveBeenCalledWith ( expect . stringContaining ( "GH_TOKEN environment variable is required but not set" ) ) ,
6752 expect ( mockCore . setFailed ) . toHaveBeenCalledWith ( expect . stringContaining ( "https://githubnext.github.io/gh-aw/reference/safe-outputs/#assigning-issues-to-copilot" ) ) ,
@@ -71,7 +56,6 @@ const mockCore = {
7156 ( ( process . env . GH_TOKEN = " " ) ,
7257 ( process . env . ASSIGNEE = "test-user" ) ,
7358 ( process . env . ISSUE_NUMBER = "123" ) ,
74- // Execute the script
7559 await eval ( `(async () => { ${ assignIssueScript } })()` ) ,
7660 expect ( mockCore . setFailed ) . toHaveBeenCalledWith ( expect . stringContaining ( "GH_TOKEN environment variable is required but not set" ) ) ,
7761 expect ( mockExec . exec ) . not . toHaveBeenCalled ( ) ) ;
@@ -80,7 +64,6 @@ const mockCore = {
8064 ( ( process . env . GH_TOKEN = "ghp_test123" ) ,
8165 ( process . env . ISSUE_NUMBER = "123" ) ,
8266 delete process . env . ASSIGNEE ,
83- // Execute the script
8467 await eval ( `(async () => { ${ assignIssueScript } })()` ) ,
8568 expect ( mockCore . setFailed ) . toHaveBeenCalledWith ( "ASSIGNEE environment variable is required but not set" ) ,
8669 expect ( mockExec . exec ) . not . toHaveBeenCalled ( ) ) ;
@@ -89,7 +72,6 @@ const mockCore = {
8972 ( ( process . env . GH_TOKEN = "ghp_test123" ) ,
9073 ( process . env . ASSIGNEE = " " ) ,
9174 ( process . env . ISSUE_NUMBER = "123" ) ,
92- // Execute the script
9375 await eval ( `(async () => { ${ assignIssueScript } })()` ) ,
9476 expect ( mockCore . setFailed ) . toHaveBeenCalledWith ( "ASSIGNEE environment variable is required but not set" ) ,
9577 expect ( mockExec . exec ) . not . toHaveBeenCalled ( ) ) ;
@@ -98,7 +80,6 @@ const mockCore = {
9880 ( ( process . env . GH_TOKEN = "ghp_test123" ) ,
9981 ( process . env . ASSIGNEE = "test-user" ) ,
10082 delete process . env . ISSUE_NUMBER ,
101- // Execute the script
10283 await eval ( `(async () => { ${ assignIssueScript } })()` ) ,
10384 expect ( mockCore . setFailed ) . toHaveBeenCalledWith ( "ISSUE_NUMBER environment variable is required but not set" ) ,
10485 expect ( mockExec . exec ) . not . toHaveBeenCalled ( ) ) ;
@@ -107,7 +88,6 @@ const mockCore = {
10788 ( ( process . env . GH_TOKEN = "ghp_test123" ) ,
10889 ( process . env . ASSIGNEE = "test-user" ) ,
10990 ( process . env . ISSUE_NUMBER = " " ) ,
110- // Execute the script
11191 await eval ( `(async () => { ${ assignIssueScript } })()` ) ,
11292 expect ( mockCore . setFailed ) . toHaveBeenCalledWith ( "ISSUE_NUMBER environment variable is required but not set" ) ,
11393 expect ( mockExec . exec ) . not . toHaveBeenCalled ( ) ) ;
@@ -119,7 +99,6 @@ const mockCore = {
11999 ( process . env . ASSIGNEE = "test-user" ) ,
120100 ( process . env . ISSUE_NUMBER = "456" ) ,
121101 mockExec . exec . mockResolvedValue ( 0 ) ,
122- // Execute the script
123102 await eval ( `(async () => { ${ assignIssueScript } })()` ) ,
124103 expect ( mockCore . info ) . toHaveBeenCalledWith ( "Assigning issue #456 to test-user" ) ,
125104 expect ( mockExec . exec ) . toHaveBeenCalledWith ( "gh" , [ "issue" , "edit" , "456" , "--add-assignee" , "test-user" ] , expect . objectContaining ( { env : expect . objectContaining ( { GH_TOKEN : "ghp_test123" } ) } ) ) ,
@@ -133,7 +112,6 @@ const mockCore = {
133112 ( process . env . ASSIGNEE = " test-user " ) ,
134113 ( process . env . ISSUE_NUMBER = " 123 " ) ,
135114 mockExec . exec . mockResolvedValue ( 0 ) ,
136- // Execute the script
137115 await eval ( `(async () => { ${ assignIssueScript } })()` ) ,
138116 expect ( mockCore . info ) . toHaveBeenCalledWith ( "Assigning issue #123 to test-user" ) ,
139117 expect ( mockExec . exec ) . toHaveBeenCalledWith ( "gh" , [ "issue" , "edit" , "123" , "--add-assignee" , "test-user" ] , expect . any ( Object ) ) ,
@@ -144,7 +122,6 @@ const mockCore = {
144122 ( process . env . ASSIGNEE = "test-user" ) ,
145123 ( process . env . ISSUE_NUMBER = "123" ) ,
146124 mockExec . exec . mockResolvedValue ( 0 ) ,
147- // Execute the script
148125 await eval ( `(async () => { ${ assignIssueScript } })()` ) ,
149126 expect ( mockCore . summary . addRaw ) . toHaveBeenCalledWith ( expect . stringContaining ( "## Issue Assignment" ) ) ,
150127 expect ( mockCore . summary . addRaw ) . toHaveBeenCalledWith ( expect . stringContaining ( "Successfully assigned issue #123 to `test-user`" ) ) ,
@@ -156,7 +133,6 @@ const mockCore = {
156133 ( ( process . env . GH_TOKEN = "ghp_test123" ) , ( process . env . ASSIGNEE = "test-user" ) , ( process . env . ISSUE_NUMBER = "999" ) ) ;
157134 const testError = new Error ( "User not found" ) ;
158135 ( mockExec . exec . mockRejectedValue ( testError ) ,
159- // Execute the script
160136 await eval ( `(async () => { ${ assignIssueScript } })()` ) ,
161137 expect ( mockCore . error ) . toHaveBeenCalledWith ( "Failed to assign issue: User not found" ) ,
162138 expect ( mockCore . setFailed ) . toHaveBeenCalledWith ( "Failed to assign issue #999 to test-user: User not found" ) ) ;
@@ -165,20 +141,14 @@ const mockCore = {
165141 ( ( process . env . GH_TOKEN = "ghp_test123" ) , ( process . env . ASSIGNEE = "test-user" ) , ( process . env . ISSUE_NUMBER = "999" ) ) ;
166142 const stringError = "Command failed" ;
167143 ( mockExec . exec . mockRejectedValue ( stringError ) ,
168- // Execute the script
169144 await eval ( `(async () => { ${ assignIssueScript } })()` ) ,
170145 expect ( mockCore . error ) . toHaveBeenCalledWith ( "Failed to assign issue: Command failed" ) ,
171146 expect ( mockCore . setFailed ) . toHaveBeenCalledWith ( "Failed to assign issue #999 to test-user: Command failed" ) ) ;
172147 } ) ,
173148 it ( "should handle top-level errors with catch handler" , async ( ) => {
174149 ( ( process . env . GH_TOKEN = "ghp_test123" ) , ( process . env . ASSIGNEE = "test-user" ) , ( process . env . ISSUE_NUMBER = "123" ) ) ;
175- // Mock exec to throw an error that isn't caught in main
176150 const uncaughtError = new Error ( "Uncaught error" ) ;
177- ( mockExec . exec . mockRejectedValue ( uncaughtError ) ,
178- // Execute the script
179- await eval ( `(async () => { ${ assignIssueScript } })()` ) ,
180- // The error should be caught in the main catch handler
181- expect ( mockCore . setFailed ) . toHaveBeenCalled ( ) ) ;
151+ ( mockExec . exec . mockRejectedValue ( uncaughtError ) , await eval ( `(async () => { ${ assignIssueScript } })()` ) , expect ( mockCore . setFailed ) . toHaveBeenCalled ( ) ) ;
182152 } ) ) ;
183153 } ) ,
184154 describe ( "Edge cases for regular users" , ( ) => {
@@ -187,7 +157,6 @@ const mockCore = {
187157 ( process . env . ASSIGNEE = "test-user" ) ,
188158 ( process . env . ISSUE_NUMBER = "123" ) ,
189159 mockExec . exec . mockResolvedValue ( 0 ) ,
190- // Execute the script
191160 await eval ( `(async () => { ${ assignIssueScript } })()` ) ,
192161 expect ( mockExec . exec ) . toHaveBeenCalledWith ( "gh" , [ "issue" , "edit" , "123" , "--add-assignee" , "test-user" ] , expect . any ( Object ) ) ) ;
193162 } ) ,
@@ -197,7 +166,6 @@ const mockCore = {
197166 ( process . env . ISSUE_NUMBER = "123" ) ,
198167 ( process . env . OTHER_VAR = "other_value" ) ,
199168 mockExec . exec . mockResolvedValue ( 0 ) ,
200- // Execute the script
201169 await eval ( `(async () => { ${ assignIssueScript } })()` ) ,
202170 expect ( mockExec . exec ) . toHaveBeenCalledWith ( "gh" , [ "issue" , "edit" , "123" , "--add-assignee" , "test-user" ] , { env : expect . objectContaining ( { GH_TOKEN : "ghp_test123" , OTHER_VAR : "other_value" } ) } ) ) ;
203171 } ) ,
@@ -206,17 +174,12 @@ const mockCore = {
206174 ( process . env . ASSIGNEE = "user-with-dash" ) ,
207175 ( process . env . ISSUE_NUMBER = "123" ) ,
208176 mockExec . exec . mockResolvedValue ( 0 ) ,
209- // Execute the script
210177 await eval ( `(async () => { ${ assignIssueScript } })()` ) ,
211178 expect ( mockExec . exec ) . toHaveBeenCalledWith ( "gh" , [ "issue" , "edit" , "123" , "--add-assignee" , "user-with-dash" ] , expect . any ( Object ) ) ,
212179 expect ( mockCore . setFailed ) . not . toHaveBeenCalled ( ) ) ;
213180 } ) ,
214181 it ( "should include documentation link in error message" , async ( ) => {
215- ( delete process . env . GH_TOKEN ,
216- ( process . env . ASSIGNEE = "test-user" ) ,
217- ( process . env . ISSUE_NUMBER = "123" ) ,
218- // Execute the script
219- await eval ( `(async () => { ${ assignIssueScript } })()` ) ) ;
182+ ( delete process . env . GH_TOKEN , ( process . env . ASSIGNEE = "test-user" ) , ( process . env . ISSUE_NUMBER = "123" ) , await eval ( `(async () => { ${ assignIssueScript } })()` ) ) ;
220183 const failedCall = mockCore . setFailed . mock . calls [ 0 ] [ 0 ] ;
221184 expect ( failedCall ) . toContain ( "https://githubnext.github.io/gh-aw/reference/safe-outputs/#assigning-issues-to-copilot" ) ;
222185 } ) ) ;
0 commit comments