|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + tea "charm.land/bubbletea/v2" |
| 9 | + |
| 10 | + "github.com/GrayCodeAI/reflect/internal/storage" |
| 11 | + "github.com/GrayCodeAI/reflect/internal/tool" |
| 12 | +) |
| 13 | + |
| 14 | +// isolateSteeringTestState isolates config/state dirs (including the |
| 15 | +// persisted prompt queue) for steering tests. |
| 16 | +func isolateSteeringTestState(t *testing.T) { |
| 17 | + t.Helper() |
| 18 | + isolateChatCommandSweepEnv(t) |
| 19 | + if err := os.MkdirAll(storage.StateDir(), 0o755); err != nil { |
| 20 | + t.Fatalf("mkdir state dir: %v", err) |
| 21 | + } |
| 22 | + tool.ResetPromptQueueForTest() |
| 23 | + t.Cleanup(tool.ResetPromptQueueForTest) |
| 24 | +} |
| 25 | + |
| 26 | +func TestSubmitWhileWaitingSteersActiveRun(t *testing.T) { |
| 27 | + m := newTestChatModel() |
| 28 | + m.waiting = true |
| 29 | + m.input.SetValue("focus on the failing test") |
| 30 | + |
| 31 | + result, _ := m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) |
| 32 | + m = requireChatModel(t, result) |
| 33 | + |
| 34 | + if len(m.messageQueue) != 0 { |
| 35 | + t.Fatalf("deferred queue should stay empty while steering is live, got %v", m.messageQueue) |
| 36 | + } |
| 37 | + sq := m.session.Persistence().Steering() |
| 38 | + if sq == nil || !sq.HasPending() { |
| 39 | + t.Fatal("steering text should be pending on the session's steering queue") |
| 40 | + } |
| 41 | + drained := sq.Drain() |
| 42 | + if len(drained) != 1 || drained[0].Content != "focus on the failing test" { |
| 43 | + t.Fatalf("steering queue = %+v, want one guidance message", drained) |
| 44 | + } |
| 45 | + if got := m.input.Value(); got != "" { |
| 46 | + t.Fatalf("input should be cleared after steering submit, got %q", got) |
| 47 | + } |
| 48 | + if got := lastSystemMessage(m.messages); !strings.Contains(got, "Steering active run: focus on the failing test") { |
| 49 | + t.Fatalf("expected steering status line, got %q", got) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestSubmitWhileWaitingFallsBackWithoutSteeringQueue(t *testing.T) { |
| 54 | + m := newTestChatModel() |
| 55 | + m.session.Persistence().SetSteering(nil) |
| 56 | + m.waiting = true |
| 57 | + m.input.SetValue("deferred note") |
| 58 | + |
| 59 | + result, _ := m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) |
| 60 | + m = requireChatModel(t, result) |
| 61 | + |
| 62 | + if len(m.messageQueue) != 1 || m.messageQueue[0] != "deferred note" { |
| 63 | + t.Fatalf("fallback deferred queue = %v, want [deferred note]", m.messageQueue) |
| 64 | + } |
| 65 | + if got := lastSystemMessage(m.messages); !strings.Contains(got, "Queued: deferred note") { |
| 66 | + t.Fatalf("expected queued status line, got %q", got) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// TestStreamDoneReclaimsUnconsumedSteering covers the conversational-run |
| 71 | +// edge: a turn with no tool batches never drains the steering queue, so the |
| 72 | +// done handler must hand the guidance back to the deferred queue instead of |
| 73 | +// stranding it for an unrelated future run. |
| 74 | +func TestStreamDoneReclaimsUnconsumedSteering(t *testing.T) { |
| 75 | + isolateSteeringTestState(t) |
| 76 | + |
| 77 | + m := newTestChatModel() |
| 78 | + m.waiting = true |
| 79 | + if !m.steerActiveRun("never drained") { |
| 80 | + t.Fatal("steering enqueue failed") |
| 81 | + } |
| 82 | + |
| 83 | + result, _ := m.Update(streamDoneMsg{}) |
| 84 | + m = requireChatModel(t, result) |
| 85 | + |
| 86 | + sq := m.session.Persistence().Steering() |
| 87 | + if sq.HasPending() { |
| 88 | + t.Fatal("unconsumed steering should be reclaimed at stream end") |
| 89 | + } |
| 90 | + // The stream-done auto-submit path turns the reclaimed guidance into |
| 91 | + // the next user turn (same behavior the deferred queue always had). |
| 92 | + if !m.waiting { |
| 93 | + t.Fatal("reclaimed steering should auto-start the next turn") |
| 94 | + } |
| 95 | + if got := lastMessageByRole(m.messages, "user"); got != "never drained" { |
| 96 | + t.Fatalf("last user message = %q, want %q", got, "never drained") |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestStreamErrorReclaimsUnconsumedSteering(t *testing.T) { |
| 101 | + m := newTestChatModel() |
| 102 | + m.waiting = true |
| 103 | + if !m.steerActiveRun("aborted run guidance") { |
| 104 | + t.Fatal("steering enqueue failed") |
| 105 | + } |
| 106 | + |
| 107 | + result, _ := m.Update(streamErrMsg{err: errNoInteractivePromptInput}) |
| 108 | + m = requireChatModel(t, result) |
| 109 | + |
| 110 | + if sq := m.session.Persistence().Steering(); sq.HasPending() { |
| 111 | + t.Fatal("unconsumed steering should be reclaimed on stream error") |
| 112 | + } |
| 113 | + if len(m.messageQueue) != 1 || m.messageQueue[0] != "aborted run guidance" { |
| 114 | + t.Fatalf("reclaimed queue = %v, want [aborted run guidance]", m.messageQueue) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// TestPromptQueueAddSteersLiveRun: /prompt-queue add during a live run feeds |
| 119 | +// the steering queue directly instead of persisting dead text. |
| 120 | +func TestPromptQueueAddSteersLiveRun(t *testing.T) { |
| 121 | + isolateSteeringTestState(t) |
| 122 | + |
| 123 | + m := newTestChatModel() |
| 124 | + m.waiting = true |
| 125 | + |
| 126 | + m.handleCommand("/prompt-queue add focus on the auth module") |
| 127 | + |
| 128 | + sq := m.session.Persistence().Steering() |
| 129 | + if sq == nil || !sq.HasPending() { |
| 130 | + t.Fatal("/prompt-queue add during a live run should steer the run") |
| 131 | + } |
| 132 | + drained := sq.Drain() |
| 133 | + if len(drained) != 1 || drained[0].Content != "focus on the auth module" { |
| 134 | + t.Fatalf("steering queue = %+v, want the queued prompt", drained) |
| 135 | + } |
| 136 | + // Steered live: nothing is written to the persisted queue. |
| 137 | + if items := tool.GetPromptQueue(); len(items) != 0 { |
| 138 | + t.Fatalf("persisted queue = %+v, want empty", items) |
| 139 | + } |
| 140 | + if got := lastSystemMessage(m.messages); !strings.Contains(got, "Steered into the active run") { |
| 141 | + t.Fatalf("expected steering status line, got %q", got) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// TestPromptQueueAddPersistsWhenIdle: with no live run, /prompt-queue add |
| 146 | +// persists for the next run's steering splice. |
| 147 | +func TestPromptQueueAddPersistsWhenIdle(t *testing.T) { |
| 148 | + isolateSteeringTestState(t) |
| 149 | + |
| 150 | + m := newTestChatModel() |
| 151 | + |
| 152 | + m.handleCommand("/prompt-queue add queued while idle") |
| 153 | + |
| 154 | + items := tool.GetPromptQueue() |
| 155 | + if len(items) != 1 || items[0].Prompt != "queued while idle" { |
| 156 | + t.Fatalf("persisted queue = %+v, want one queued prompt", items) |
| 157 | + } |
| 158 | + if sq := m.session.Persistence().Steering(); sq.HasPending() { |
| 159 | + t.Fatal("idle add must not enqueue steering") |
| 160 | + } |
| 161 | + if got := lastSystemMessage(m.messages); !strings.Contains(got, "steers the next run") { |
| 162 | + t.Fatalf("expected next-run status line, got %q", got) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +// TestPersistedPromptQueueFeedsNextRun: the run-start drain is the persisted |
| 167 | +// queue's consumer — FIFO order is preserved and the file is emptied. |
| 168 | +func TestPersistedPromptQueueFeedsNextRun(t *testing.T) { |
| 169 | + isolateSteeringTestState(t) |
| 170 | + |
| 171 | + if err := tool.EnqueuePrompt("first queued task", ""); err != nil { |
| 172 | + t.Fatal(err) |
| 173 | + } |
| 174 | + if err := tool.EnqueuePrompt("second queued task", ""); err != nil { |
| 175 | + t.Fatal(err) |
| 176 | + } |
| 177 | + |
| 178 | + m := newTestChatModel() |
| 179 | + m.drainPersistedPromptQueue() |
| 180 | + |
| 181 | + drained := m.session.Persistence().Steering().Drain() |
| 182 | + if len(drained) != 2 || drained[0].Content != "first queued task" || drained[1].Content != "second queued task" { |
| 183 | + t.Fatalf("steering feed = %+v, want FIFO [first queued task, second queued task]", drained) |
| 184 | + } |
| 185 | + if items := tool.GetPromptQueue(); len(items) != 0 { |
| 186 | + t.Fatalf("persisted queue should be drained, got %+v", items) |
| 187 | + } |
| 188 | +} |
0 commit comments