@@ -203,6 +203,79 @@ func TestAPIController_Apply(t *testing.T) {
203203 projectCommandRunner .VerifyWasCalled (Times (expectedCalls )).Apply (Any [command.ProjectContext ]())
204204}
205205
206+ // TestAPIController_Plan_PreWorkflowHooksReceiveCorrectCommand verifies that when
207+ // calling the Plan API endpoint, the pre-workflow hooks receive a CommentCommand
208+ // with Name set to command.Plan (not the zero value which would be command.Apply).
209+ func TestAPIController_Plan_PreWorkflowHooksReceiveCorrectCommand (t * testing.T ) {
210+ ac , _ , _ := setup (t )
211+
212+ // Get access to the pre-workflow hooks mock for verification
213+ preWorkflowHooksRunner := ac .PreWorkflowHooksCommandRunner .(* MockPreWorkflowHooksCommandRunner )
214+
215+ body , _ := json .Marshal (controllers.APIRequest {
216+ Repository : "Repo" ,
217+ Ref : "main" ,
218+ Type : "Gitlab" ,
219+ Projects : []string {"default" },
220+ })
221+
222+ req , _ := http .NewRequest ("POST" , "" , bytes .NewBuffer (body ))
223+ req .Header .Set (atlantisTokenHeader , atlantisToken )
224+ w := httptest .NewRecorder ()
225+ ac .Plan (w , req )
226+ ResponseContains (t , w , http .StatusOK , "" )
227+
228+ // Capture the CommentCommand passed to RunPreHooks and verify Name is Plan
229+ _ , capturedCmd := preWorkflowHooksRunner .VerifyWasCalled (Times (1 )).
230+ RunPreHooks (Any [* command.Context ](), Any [* events.CommentCommand ]()).
231+ GetCapturedArguments ()
232+
233+ Assert (t , capturedCmd .Name == command .Plan ,
234+ "expected CommentCommand.Name to be Plan (%d), got %s (%d)" ,
235+ command .Plan , capturedCmd .Name .String (), capturedCmd .Name )
236+ }
237+
238+ // TestAPIController_Apply_PreWorkflowHooksReceiveCorrectCommand verifies that when
239+ // calling the Apply API endpoint, the pre-workflow hooks receive a CommentCommand
240+ // with Name set to command.Apply for the apply phase (and command.Plan for the
241+ // plan phase that runs first).
242+ func TestAPIController_Apply_PreWorkflowHooksReceiveCorrectCommand (t * testing.T ) {
243+ ac , _ , _ := setup (t )
244+
245+ // Get access to the pre-workflow hooks mock for verification
246+ preWorkflowHooksRunner := ac .PreWorkflowHooksCommandRunner .(* MockPreWorkflowHooksCommandRunner )
247+
248+ body , _ := json .Marshal (controllers.APIRequest {
249+ Repository : "Repo" ,
250+ Ref : "main" ,
251+ Type : "Gitlab" ,
252+ Projects : []string {"default" },
253+ })
254+
255+ req , _ := http .NewRequest ("POST" , "" , bytes .NewBuffer (body ))
256+ req .Header .Set (atlantisTokenHeader , atlantisToken )
257+ w := httptest .NewRecorder ()
258+ ac .Apply (w , req )
259+ ResponseContains (t , w , http .StatusOK , "" )
260+
261+ // Apply calls apiPlan first (which runs pre-hooks with Plan), then apiApply (which runs pre-hooks with Apply)
262+ // So we expect 2 calls: first with Plan, second with Apply
263+ _ , capturedCmds := preWorkflowHooksRunner .VerifyWasCalled (Times (2 )).
264+ RunPreHooks (Any [* command.Context ](), Any [* events.CommentCommand ]()).
265+ GetAllCapturedArguments ()
266+
267+ Assert (t , len (capturedCmds ) == 2 ,
268+ "expected 2 pre-workflow hook calls, got %d" , len (capturedCmds ))
269+
270+ Assert (t , capturedCmds [0 ].Name == command .Plan ,
271+ "expected first CommentCommand.Name to be Plan (%d), got %s (%d)" ,
272+ command .Plan , capturedCmds [0 ].Name .String (), capturedCmds [0 ].Name )
273+
274+ Assert (t , capturedCmds [1 ].Name == command .Apply ,
275+ "expected second CommentCommand.Name to be Apply (%d), got %s (%d)" ,
276+ command .Apply , capturedCmds [1 ].Name .String (), capturedCmds [1 ].Name )
277+ }
278+
206279func TestAPIController_ListLocks (t * testing.T ) {
207280 ac , _ , _ := setup (t )
208281 time := time .Now ()
0 commit comments