| 1 | package builtin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "reasonix/internal/tool" |
| 10 | ) |
| 11 | |
| 12 | func TestApplyTerminalResultTypedOutcomes(t *testing.T) { |
| 13 | t.Run("success", func(t *testing.T) { |
| 14 | ex := &tool.ShellExecution{Kind: "shell"} |
| 15 | applyTerminalResult(ex, nil) |
| 16 | if ex.State != tool.ShellStateCompleted || ex.ExitCode == nil || *ex.ExitCode != 0 { |
| 17 | t.Fatalf("%+v", ex) |
| 18 | } |
| 19 | }) |
| 20 | t.Run("exit code", func(t *testing.T) { |
| 21 | ex := &tool.ShellExecution{Kind: "shell"} |
| 22 | applyTerminalResult(ex, TerminalExitError{Code: 7}) |
| 23 | if ex.State != tool.ShellStateFailed || ex.FailurePhase != tool.ShellPhaseExecution { |
| 24 | t.Fatalf("%+v", ex) |
| 25 | } |
| 26 | if ex.ExitCode == nil || *ex.ExitCode != 7 { |
| 27 | t.Fatalf("exitCode=%v", ex.ExitCode) |
| 28 | } |
| 29 | if ex.MutationRisk != tool.ShellMutationMayBePartial { |
| 30 | t.Fatalf("mutationRisk=%q", ex.MutationRisk) |
| 31 | } |
| 32 | }) |
| 33 | t.Run("timeout", func(t *testing.T) { |
| 34 | ex := &tool.ShellExecution{Kind: "shell"} |
| 35 | applyTerminalResult(ex, TerminalTimeoutError{Timeout: 2 * time.Second}) |
| 36 | if ex.State != tool.ShellStateTimedOut || ex.FailurePhase != tool.ShellPhaseTimeout { |
| 37 | t.Fatalf("%+v", ex) |
| 38 | } |
| 39 | if ex.MutationRisk != tool.ShellMutationMayBePartial { |
| 40 | t.Fatalf("mutationRisk=%q", ex.MutationRisk) |
| 41 | } |
| 42 | }) |
| 43 | t.Run("cancel", func(t *testing.T) { |
| 44 | ex := &tool.ShellExecution{Kind: "shell"} |
| 45 | applyTerminalResult(ex, context.Canceled) |
| 46 | if ex.State != tool.ShellStateCancelled || ex.FailurePhase != tool.ShellPhaseCancellation { |
| 47 | t.Fatalf("%+v", ex) |
| 48 | } |
| 49 | }) |
| 50 | t.Run("legacy plain error", func(t *testing.T) { |
| 51 | ex := &tool.ShellExecution{Kind: "shell"} |
| 52 | applyTerminalResult(ex, errors.New("exit status 3")) |
| 53 | if ex.State != tool.ShellStateFailed || ex.ExitCode != nil { |
| 54 | // Plain errors cannot recover the code; still mark partial risk. |
| 55 | t.Fatalf("legacy outcome = %+v", ex) |
| 56 | } |
| 57 | if ex.MutationRisk != tool.ShellMutationMayBePartial { |
| 58 | t.Fatalf("mutationRisk=%q", ex.MutationRisk) |
| 59 | } |
| 60 | }) |
| 61 | } |
| 62 |