@@ -277,3 +277,141 @@ func TestRunRewriteScenario_InvalidConfiguration(t *testing.T) {
277277 t .Fatalf ("error = %v, want detect callback validation error" , err )
278278 }
279279}
280+
281+ func TestRunRewriteScenario_CancelledContext (t * testing.T ) {
282+ ctx , cancel := context .WithCancel (context .Background ())
283+ cancel ()
284+
285+ result , err := RunRewriteScenario (ctx , RewriteScenarioOptions {
286+ MaxAttempts : 2 ,
287+ Detect : func (_ context.Context , _ int ) (bool , error ) {
288+ return true , nil
289+ },
290+ Intervene : func (_ context.Context , _ int ) error {
291+ return nil
292+ },
293+ Verify : func (_ context.Context , _ int ) (bool , error ) {
294+ return false , nil
295+ },
296+ })
297+ if err == nil {
298+ t .Fatal ("RunRewriteScenario() error = nil, want cancellation error" )
299+ }
300+ if ! errors .Is (err , context .Canceled ) {
301+ t .Fatalf ("RunRewriteScenario() error = %v, want context.Canceled" , err )
302+ }
303+ if result .Attempts != 0 {
304+ t .Fatalf ("result.Attempts = %d, want 0" , result .Attempts )
305+ }
306+ }
307+
308+ func TestRunRewriteScenario_NilContextFallback (t * testing.T ) {
309+ detectCalls := 0
310+ result , err := RunRewriteScenario (nil , RewriteScenarioOptions {
311+ MaxAttempts : 1 ,
312+ Detect : func (_ context.Context , attempt int ) (bool , error ) {
313+ detectCalls ++
314+ if attempt != 1 {
315+ t .Fatalf ("attempt = %d, want 1" , attempt )
316+ }
317+ return false , nil
318+ },
319+ Intervene : func (_ context.Context , _ int ) error {
320+ t .Fatal ("Intervene should not run when detect reports clean" )
321+ return nil
322+ },
323+ Verify : func (_ context.Context , _ int ) (bool , error ) {
324+ t .Fatal ("Verify should not run when detect reports clean" )
325+ return true , nil
326+ },
327+ })
328+ if err != nil {
329+ t .Fatalf ("RunRewriteScenario() error = %v" , err )
330+ }
331+ if ! result .Clean {
332+ t .Fatalf ("result.Clean = false, want true" )
333+ }
334+ if detectCalls != 1 {
335+ t .Fatalf ("detectCalls = %d, want 1" , detectCalls )
336+ }
337+ }
338+
339+ func TestRunRewriteScenario_CancelledAfterDetectSkipsIntervene (t * testing.T ) {
340+ ctx , cancel := context .WithCancel (context .Background ())
341+
342+ interveneCalls := 0
343+ verifyCalls := 0
344+
345+ result , err := RunRewriteScenario (ctx , RewriteScenarioOptions {
346+ MaxAttempts : 2 ,
347+ Detect : func (_ context.Context , _ int ) (bool , error ) {
348+ cancel ()
349+ return true , nil
350+ },
351+ Intervene : func (_ context.Context , _ int ) error {
352+ interveneCalls ++
353+ return nil
354+ },
355+ Verify : func (_ context.Context , _ int ) (bool , error ) {
356+ verifyCalls ++
357+ return true , nil
358+ },
359+ })
360+ if err == nil {
361+ t .Fatal ("RunRewriteScenario() error = nil, want cancellation error" )
362+ }
363+ if ! errors .Is (err , context .Canceled ) {
364+ t .Fatalf ("RunRewriteScenario() error = %v, want context.Canceled" , err )
365+ }
366+ if ! strings .Contains (err .Error (), "cancelled before intervene" ) {
367+ t .Fatalf ("error = %v, want message about intervene phase" , err )
368+ }
369+ if result .Attempts != 1 {
370+ t .Fatalf ("result.Attempts = %d, want 1" , result .Attempts )
371+ }
372+ if len (result .Passes ) != 1 || ! result .Passes [0 ].NeedsIntervention {
373+ t .Fatalf ("passes = %+v, want one pass with detect outcome recorded" , result .Passes )
374+ }
375+ if interveneCalls != 0 || verifyCalls != 0 {
376+ t .Fatalf ("calls = intervene:%d verify:%d, want 0/0" , interveneCalls , verifyCalls )
377+ }
378+ }
379+
380+ func TestRunRewriteScenario_CancelledAfterInterveneSkipsVerify (t * testing.T ) {
381+ ctx , cancel := context .WithCancel (context .Background ())
382+
383+ verifyCalls := 0
384+
385+ result , err := RunRewriteScenario (ctx , RewriteScenarioOptions {
386+ MaxAttempts : 2 ,
387+ Detect : func (_ context.Context , _ int ) (bool , error ) {
388+ return true , nil
389+ },
390+ Intervene : func (_ context.Context , _ int ) error {
391+ cancel ()
392+ return nil
393+ },
394+ Verify : func (_ context.Context , _ int ) (bool , error ) {
395+ verifyCalls ++
396+ return true , nil
397+ },
398+ })
399+ if err == nil {
400+ t .Fatal ("RunRewriteScenario() error = nil, want cancellation error" )
401+ }
402+ if ! errors .Is (err , context .Canceled ) {
403+ t .Fatalf ("RunRewriteScenario() error = %v, want context.Canceled" , err )
404+ }
405+ if ! strings .Contains (err .Error (), "cancelled before verify" ) {
406+ t .Fatalf ("error = %v, want message about verify phase" , err )
407+ }
408+ if result .Attempts != 1 {
409+ t .Fatalf ("result.Attempts = %d, want 1" , result .Attempts )
410+ }
411+ if len (result .Passes ) != 1 || ! result .Passes [0 ].Intervened {
412+ t .Fatalf ("passes = %+v, want one pass with intervene=true" , result .Passes )
413+ }
414+ if verifyCalls != 0 {
415+ t .Fatalf ("verifyCalls = %d, want 0" , verifyCalls )
416+ }
417+ }
0 commit comments