MainLoopStop must be self-describing in both registers: String() for logs and telemetry, Reason() for the manifest reason and scan warning that are the only stop diagnostics surviving a --format json run. Neither may collide across stops, or a reader cannot tell which exit fired from the artifact al
(t *testing.T)
| 10 | // stop diagnostics surviving a --format json run. Neither may collide across |
| 11 | // stops, or a reader cannot tell which exit fired from the artifact alone. |
| 12 | func TestMainLoopStopStringAndReason(t *testing.T) { |
| 13 | names := make(map[string]MainLoopStop) |
| 14 | reasons := make(map[string]MainLoopStop) |
| 15 | for _, tc := range []struct { |
| 16 | stop MainLoopStop |
| 17 | wantName string |
| 18 | }{ |
| 19 | {StopNone, "none"}, |
| 20 | {StopMaxRounds, "max_rounds"}, |
| 21 | {StopEmptyRounds, "empty_rounds"}, |
| 22 | {StopCompression, "compression"}, |
| 23 | {StopTokenBudget, "token_budget"}, |
| 24 | } { |
| 25 | t.Run(tc.wantName, func(t *testing.T) { |
| 26 | name := tc.stop.String() |
| 27 | if name != tc.wantName { |
| 28 | t.Errorf("String() = %q, want %q", name, tc.wantName) |
| 29 | } |
| 30 | reason := tc.stop.Reason() |
| 31 | if reason == "" { |
| 32 | t.Fatal("Reason() is empty; every stop needs a diagnostic sentence") |
| 33 | } |
| 34 | if prev, dup := names[name]; dup { |
| 35 | t.Errorf("String() %q is shared by %v and %v", name, prev, tc.stop) |
| 36 | } |
| 37 | if prev, dup := reasons[reason]; dup { |
| 38 | t.Errorf("Reason() %q is shared by %v and %v; stops must stay distinguishable", reason, prev, tc.stop) |
| 39 | } |
| 40 | names[name] = tc.stop |
| 41 | reasons[reason] = tc.stop |
| 42 | }) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // A value outside the enum must name itself in both registers rather than borrow |
| 47 | // the StopNone catch-all. This also guards the enum's growth: adding a constant |