| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestSteerText(t *testing.T) { |
| 9 | tests := []struct { |
| 10 | name string |
| 11 | content string |
| 12 | want string |
| 13 | wantOK bool |
| 14 | }{ |
| 15 | { |
| 16 | name: "happy path: prefix + newline + text", |
| 17 | content: MidTurnSteerPrefix + "\nplease use smaller diffs", |
| 18 | want: "please use smaller diffs", |
| 19 | wantOK: true, |
| 20 | }, |
| 21 | { |
| 22 | name: "prefix only, no user text", |
| 23 | content: MidTurnSteerPrefix, |
| 24 | want: "", |
| 25 | wantOK: true, |
| 26 | }, |
| 27 | { |
| 28 | name: "prefix with trailing whitespace only", |
| 29 | content: MidTurnSteerPrefix + "\n ", |
| 30 | want: " ", |
| 31 | wantOK: true, |
| 32 | }, |
| 33 | { |
| 34 | name: "round-trip through midTurnSteerMessage", |
| 35 | content: midTurnSteerMessage("stop using such large diffs"), |
| 36 | want: "stop using such large diffs", |
| 37 | wantOK: true, |
| 38 | }, |
| 39 | { |
| 40 | name: "user text with leading/trailing spaces preserved (matches live event)", |
| 41 | content: MidTurnSteerPrefix + "\n keep going but use read_file first ", |
| 42 | want: " keep going but use read_file first ", |
| 43 | wantOK: true, |
| 44 | }, |
| 45 | { |
| 46 | name: "regular user message, not steer", |
| 47 | content: "please use smaller diffs", |
| 48 | want: "", |
| 49 | wantOK: false, |
| 50 | }, |
| 51 | { |
| 52 | name: "empty string", |
| 53 | content: "", |
| 54 | want: "", |
| 55 | wantOK: false, |
| 56 | }, |
| 57 | { |
| 58 | name: "whitespace only", |
| 59 | content: " ", |
| 60 | want: "", |
| 61 | wantOK: false, |
| 62 | }, |
| 63 | { |
| 64 | name: "prefix-like but truncated (no closing bracket)", |
| 65 | content: "[Mid-turn steer queued by the user. Do not treat this as a new task\nplease go on", |
| 66 | want: "", |
| 67 | wantOK: false, |
| 68 | }, |
| 69 | { |
| 70 | name: "prefix appears mid-message, not at start", |
| 71 | content: "hey model " + MidTurnSteerPrefix + "\nuse smaller diffs", |
| 72 | want: "", |
| 73 | wantOK: false, |
| 74 | }, |
| 75 | { |
| 76 | name: "multiline steer text preserved", |
| 77 | content: MidTurnSteerPrefix + "\nline one\nline two", |
| 78 | want: "line one\nline two", |
| 79 | wantOK: true, |
| 80 | }, |
| 81 | } |
| 82 | for _, tt := range tests { |
| 83 | t.Run(tt.name, func(t *testing.T) { |
| 84 | got, ok := SteerText(tt.content) |
| 85 | if ok != tt.wantOK { |
| 86 | t.Errorf("SteerText() ok = %v, want %v", ok, tt.wantOK) |
| 87 | } |
| 88 | if got != tt.want { |
| 89 | t.Errorf("SteerText() text = %q, want %q", got, tt.want) |
| 90 | } |
| 91 | // Sanity: when ok is true the result must never contain the prefix. |
| 92 | if ok && strings.Contains(got, MidTurnSteerPrefix) { |
| 93 | t.Errorf("SteerText() returned text still contains the prefix: %q", got) |
| 94 | } |
| 95 | }) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestMidTurnSteerMessageRoundTrip(t *testing.T) { |
| 100 | inputs := []string{ |
| 101 | "stop", |
| 102 | "use read_file instead of cat", |
| 103 | "", |
| 104 | " keep going ", |
| 105 | } |
| 106 | for _, in := range inputs { |
| 107 | msg := midTurnSteerMessage(in) |
| 108 | got, ok := SteerText(msg) |
| 109 | if !ok { |
| 110 | t.Errorf("SteerText(midTurnSteerMessage(%q)): not recognized as steer", in) |
| 111 | continue |
| 112 | } |
| 113 | if got != in { |
| 114 | t.Errorf("SteerText(midTurnSteerMessage(%q)) = %q, want %q", in, got, in) |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 |