60255
Zn
Zoea-Nova / files
Zoea-Nova / internal / tui / input_reset_test.go
14.1 KB
1package tui 2 3import ( 4 "testing" 5 "time" 6 7 tea "github.com/charmbracelet/bubbletea" 8 "github.com/xonecas/zoea-nova/internal/config" 9 "github.com/xonecas/zoea-nova/internal/core" 10 "github.com/xonecas/zoea-nova/internal/provider" 11 "github.com/xonecas/zoea-nova/internal/store" 12) 13 14// TestInputReset_DirectMessage verifies that the input box resets after sending a direct message. 15func TestInputReset_DirectMessage(t *testing.T) { 16 t.Run("non_empty_message_resets_input", func(t *testing.T) { 17 m, cleanup := setupTestModel(t) 18 defer cleanup() 19 20 // Create a mysis to message 21 mysis, _ := m.commander.CreateMysis("test-mysis", "ollama-qwen") 22 m.commander.StartMysis(mysis.ID()) 23 m.refreshMysisList() 24 m.selectedIdx = 0 25 26 // Press 'm' to open message input 27 newModel, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'m'}}) 28 m = newModel.(Model) 29 30 // Verify input is active 31 if !m.input.IsActive() { 32 t.Fatal("input should be active after 'm' key") 33 } 34 if m.input.Mode() != InputModeMessage { 35 t.Fatalf("input mode = %d, want InputModeMessage (%d)", m.input.Mode(), InputModeMessage) 36 } 37 38 // Type a message 39 m.input.textInput.SetValue("hello world") 40 41 // Verify input has value 42 if m.input.Value() != "hello world" { 43 t.Fatalf("input value = %q, want %q", m.input.Value(), "hello world") 44 } 45 46 // Press Enter to send 47 newModel, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) 48 m = newModel.(Model) 49 50 // Verify input is reset immediately (before async completion) 51 if m.input.Value() != "" { 52 t.Errorf("input value = %q, want empty string (input should reset immediately after Enter)", m.input.Value()) 53 } 54 if m.input.Mode() != InputModeNone { 55 t.Errorf("input mode = %d, want InputModeNone (%d)", m.input.Mode(), InputModeNone) 56 } 57 if m.input.IsActive() { 58 t.Error("input should not be active after Enter (showing 'Sending...' indicator)") 59 } 60 61 // With SendMessageAsync, m.sending is NOT set (message sent immediately) 62 // Network indicator shows activity, but input is ready for next message 63 if m.sending { 64 t.Error("sending flag should be false (SendMessageAsync returns immediately)") 65 } 66 67 // Verify async command was returned 68 if cmd == nil { 69 t.Fatal("cmd should not be nil (async send should be scheduled)") 70 } 71 72 // Execute the async command to get the result 73 result := cmd() 74 if result == nil { 75 t.Fatal("async command should return a result") 76 } 77 78 // Process the result 79 newModel, _ = m.Update(result) 80 m = newModel.(Model) 81 82 // Verify input is still reset after async completion 83 if m.input.Value() != "" { 84 t.Errorf("input value = %q, want empty string (input should remain reset after async completion)", m.input.Value()) 85 } 86 87 // Verify sending state is cleared 88 if m.sending { 89 t.Error("sending flag should be false after async completion") 90 } 91 }) 92 93 t.Run("empty_message_sends_and_shows_indicator", func(t *testing.T) { 94 m, cleanup := setupTestModel(t) 95 defer cleanup() 96 97 // Create a mysis to message 98 mysis, _ := m.commander.CreateMysis("test-mysis", "ollama-qwen") 99 m.commander.StartMysis(mysis.ID()) 100 m.refreshMysisList() 101 m.selectedIdx = 0 102 103 // Press 'm' to open message input 104 newModel, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'m'}}) 105 m = newModel.(Model) 106 107 // Verify input is active 108 if !m.input.IsActive() { 109 t.Fatal("input should be active after 'm' key") 110 } 111 112 // Press Enter without typing (empty message) 113 // NOTE: Direct messages DO NOT validate for empty - this differs from broadcasts 114 newModel, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) 115 m = newModel.(Model) 116 117 // Verify input is reset and showing "Sending..." indicator 118 if m.input.Value() != "" { 119 t.Errorf("input value = %q, want empty string", m.input.Value()) 120 } 121 if m.input.Mode() != InputModeNone { 122 t.Errorf("input mode = %d, want InputModeNone (%d)", m.input.Mode(), InputModeNone) 123 } 124 if m.input.IsActive() { 125 t.Error("input should not be active (showing 'Sending...' indicator)") 126 } 127 128 // Direct messages are sent even when empty (app.go:650-658, no empty check) 129 if cmd == nil { 130 t.Error("cmd should not be nil (direct messages are sent even when empty)") 131 } 132 // With SendMessageAsync, m.sending is NOT set 133 if m.sending { 134 t.Error("sending flag should be false (SendMessageAsync returns immediately)") 135 } 136 }) 137} 138 139// TestInputReset_Broadcast verifies that the input box resets after broadcasting. 140func TestInputReset_Broadcast(t *testing.T) { 141 t.Run("non_empty_broadcast_resets_input", func(t *testing.T) { 142 m, cleanup := setupTestModel(t) 143 defer cleanup() 144 145 // Create myses to broadcast to 146 m.commander.CreateMysis("mysis-1", "ollama-qwen") 147 m.commander.CreateMysis("mysis-2", "ollama-qwen") 148 m.refreshMysisList() 149 150 // Press 'b' to open broadcast input 151 newModel, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'b'}}) 152 m = newModel.(Model) 153 154 // Verify input is active 155 if !m.input.IsActive() { 156 t.Fatal("input should be active after 'b' key") 157 } 158 if m.input.Mode() != InputModeBroadcast { 159 t.Fatalf("input mode = %d, want InputModeBroadcast (%d)", m.input.Mode(), InputModeBroadcast) 160 } 161 162 // Type a message 163 m.input.textInput.SetValue("broadcast message") 164 165 // Press Enter to send 166 newModel, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) 167 m = newModel.(Model) 168 169 // Verify input is reset immediately 170 if m.input.Value() != "" { 171 t.Errorf("input value = %q, want empty string (input should reset immediately after Enter)", m.input.Value()) 172 } 173 if m.input.Mode() != InputModeNone { 174 t.Errorf("input mode = %d, want InputModeNone (%d)", m.input.Mode(), InputModeNone) 175 } 176 if m.input.IsActive() { 177 t.Error("input should not be active after Enter (showing 'Sending...' indicator)") 178 } 179 180 // Verify sending state is set 181 if !m.sending { 182 t.Error("sending flag should be true after Enter") 183 } 184 if m.sendingMode != InputModeBroadcast { 185 t.Errorf("sendingMode = %d, want InputModeBroadcast (%d)", m.sendingMode, InputModeBroadcast) 186 } 187 188 // Verify async command was returned 189 if cmd == nil { 190 t.Fatal("cmd should not be nil (async broadcast should be scheduled)") 191 } 192 }) 193 194 t.Run("empty_broadcast_resets_without_sending", func(t *testing.T) { 195 m, cleanup := setupTestModel(t) 196 defer cleanup() 197 198 // Press 'b' to open broadcast input 199 newModel, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'b'}}) 200 m = newModel.(Model) 201 202 // Press Enter without typing (empty broadcast) 203 newModel, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) 204 m = newModel.(Model) 205 206 // Verify input is reset 207 if m.input.Value() != "" { 208 t.Errorf("input value = %q, want empty string", m.input.Value()) 209 } 210 if m.input.Mode() != InputModeNone { 211 t.Errorf("input mode = %d, want InputModeNone (%d)", m.input.Mode(), InputModeNone) 212 } 213 if m.input.IsActive() { 214 t.Error("input should not be active after empty broadcast") 215 } 216 217 // Empty broadcasts should NOT trigger sending (app.go:629-632) 218 if cmd != nil { 219 t.Error("cmd should be nil (empty broadcasts should not be sent)") 220 } 221 if m.sending { 222 t.Error("sending flag should be false (empty broadcasts should not trigger sending)") 223 } 224 }) 225} 226 227// TestInputReset_Integration verifies input reset behavior in integration scenarios. 228func TestInputReset_Integration(t *testing.T) { 229 t.Run("send_multiple_messages_sequentially", func(t *testing.T) { 230 m, cleanup := setupTestModel(t) 231 defer cleanup() 232 233 // Create a mysis 234 mysis, _ := m.commander.CreateMysis("test-mysis", "ollama-qwen") 235 m.commander.StartMysis(mysis.ID()) 236 m.refreshMysisList() 237 m.selectedIdx = 0 238 239 // Send first message 240 newModel, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'m'}}) 241 m = newModel.(Model) 242 m.input.textInput.SetValue("message 1") 243 newModel, cmd1 := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) 244 m = newModel.(Model) 245 246 // Verify input is reset after first message 247 if m.input.Value() != "" { 248 t.Errorf("input value = %q after first message, want empty", m.input.Value()) 249 } 250 if m.input.Mode() != InputModeNone { 251 t.Errorf("input mode = %d after first message, want InputModeNone (%d)", m.input.Mode(), InputModeNone) 252 } 253 if m.input.IsActive() { 254 t.Error("input should not be active after first message (showing 'Sending...' indicator)") 255 } 256 257 // Complete first async send 258 if cmd1 != nil { 259 result1 := cmd1() 260 newModel, _ = m.Update(result1) 261 m = newModel.(Model) 262 } 263 264 // Wait a bit for async completion 265 time.Sleep(10 * time.Millisecond) 266 267 // Send second message 268 newModel, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'m'}}) 269 m = newModel.(Model) 270 m.input.textInput.SetValue("message 2") 271 newModel, cmd2 := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) 272 m = newModel.(Model) 273 274 // Verify input is reset after second message 275 if m.input.Value() != "" { 276 t.Errorf("input value = %q after second message, want empty", m.input.Value()) 277 } 278 if m.input.Mode() != InputModeNone { 279 t.Errorf("input mode = %d after second message, want InputModeNone (%d)", m.input.Mode(), InputModeNone) 280 } 281 if m.input.IsActive() { 282 t.Error("input should not be active after second message (showing 'Sending...' indicator)") 283 } 284 285 // Complete second async send 286 if cmd2 != nil { 287 result2 := cmd2() 288 newModel, _ = m.Update(result2) 289 m = newModel.(Model) 290 } 291 292 // Verify final state 293 if m.input.Value() != "" { 294 t.Errorf("final input value = %q, want empty", m.input.Value()) 295 } 296 if m.sending { 297 t.Error("sending flag should be false after all messages sent") 298 } 299 }) 300} 301 302// setupTestModelWithSlowProvider creates a test model with a slow mock provider. 303// The slow provider simulates a long LLM response time (5 seconds). 304func setupTestModelWithSlowProvider(t *testing.T) (Model, func()) { 305 t.Helper() 306 307 s, err := store.OpenMemory() 308 if err != nil { 309 t.Fatalf("OpenMemory() error: %v", err) 310 } 311 312 bus := core.NewEventBus(100) 313 eventCh := bus.Subscribe() 314 315 reg := provider.NewRegistry() 316 317 // Create a mock factory that returns a provider with a 5-second delay 318 slowFactory := &slowMockFactory{ 319 name: "ollama-qwen", 320 response: "mock response", 321 delay: 5 * time.Second, 322 } 323 reg.RegisterFactory("ollama-qwen", slowFactory) 324 reg.RegisterFactory("zen-nano", provider.NewMockFactory("zen-nano", "mock response")) 325 326 cfg := &config.Config{ 327 Swarm: config.SwarmConfig{ 328 MaxMyses: 16, 329 DefaultProvider: "ollama-qwen", 330 }, 331 Providers: map[string]config.ProviderConfig{ 332 "ollama-qwen": {Endpoint: "http://mock", Model: "qwen3:8b", Temperature: 0.7}, 333 "zen-nano": {Endpoint: "http://mock", Model: "gpt-5-nano", Temperature: 0.7}, 334 }, 335 } 336 337 commander := core.NewCommander(s, reg, bus, cfg, "") 338 339 model := New(commander, s, eventCh, false, cfg) 340 model.width = 80 341 model.height = 24 342 343 // Set fixed test time for deterministic timestamps 344 fixedTime := testTime() 345 model.testTime = &fixedTime 346 347 cleanup := func() { 348 commander.StopAll() 349 bus.Close() 350 s.Close() 351 } 352 353 return model, cleanup 354} 355 356// slowMockFactory creates mock providers with a configurable delay. 357type slowMockFactory struct { 358 name string 359 response string 360 delay time.Duration 361} 362 363func (f *slowMockFactory) Name() string { return f.name } 364 365func (f *slowMockFactory) Create(model string, temperature float64) provider.Provider { 366 p := provider.NewMock(f.name, f.response) 367 p.SetDelay(f.delay) 368 return p 369} 370 371// TestInputReset_SendingClearsImmediately verifies that m.sending is cleared 372// immediately when Enter is pressed, not after the LLM finishes responding. 373// This test demonstrates the bug where m.sending remains true until the async 374// operation completes, preventing users from sending multiple messages quickly. 375func TestInputReset_SendingClearsImmediately(t *testing.T) { 376 m, cleanup := setupTestModelWithSlowProvider(t) 377 defer cleanup() 378 379 // Create a mysis with the slow mock provider (5 second delay) 380 mysis, _ := m.commander.CreateMysis("slow-mysis", "ollama-qwen") 381 m.commander.StartMysis(mysis.ID()) 382 m.refreshMysisList() 383 m.selectedIdx = 0 384 385 // Press 'm' to open message input 386 newModel, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'m'}}) 387 m = newModel.(Model) 388 389 // Type a message 390 m.input.textInput.SetValue("test message") 391 392 // Press Enter to send 393 startTime := time.Now() 394 newModel, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) 395 m = newModel.(Model) 396 elapsedAfterEnter := time.Since(startTime) 397 398 // BUG: m.sending should be FALSE immediately after Enter, 399 // but currently it's TRUE until the async operation completes 400 // (which takes 5 seconds due to the slow provider) 401 if m.sending { 402 t.Errorf("BUG DETECTED: m.sending = true after Enter (elapsed: %v), want false immediately", elapsedAfterEnter) 403 t.Logf("This demonstrates the bug: m.sending should be cleared immediately after Enter, not after LLM completes") 404 t.Logf("Expected behavior: User can press 'm' again immediately after sending") 405 t.Logf("Actual behavior: User must wait %v for LLM to finish before m.sending clears", 5*time.Second) 406 } 407 408 // Verify that the elapsed time is small (< 100ms), proving that 409 // we're checking immediately after Enter, not after the 5-second delay 410 if elapsedAfterEnter > 100*time.Millisecond { 411 t.Fatalf("Test error: elapsed time %v is too large, expected < 100ms", elapsedAfterEnter) 412 } 413 414 // Verify async command was returned (this is correct behavior) 415 if cmd == nil { 416 t.Fatal("cmd should not be nil (async send should be scheduled)") 417 } 418 419 // Now execute the async command (with SendMessageAsync, this returns IMMEDIATELY) 420 asyncStartTime := time.Now() 421 result := cmd() 422 asyncElapsed := time.Since(asyncStartTime) 423 424 // With SendMessageAsync, the command returns IMMEDIATELY (not after LLM completes) 425 // This is the fix - the async operation should be fast (< 100ms) 426 if asyncElapsed > 100*time.Millisecond { 427 t.Errorf("async operation took %v, expected < 100ms (SendMessageAsync returns immediately)", asyncElapsed) 428 } 429 430 // Process the async result 431 newModel, _ = m.Update(result) 432 m = newModel.(Model) 433 434 // m.sending should still be false (was never set with async approach) 435 if m.sending { 436 t.Error("m.sending should be false (never set with SendMessageAsync)") 437 } 438 439 // The LLM processing happens in the background (via goroutine in SendMessageAsync) 440 // The TUI doesn't wait for it, so the user can immediately send another message 441 t.Log("SUCCESS: Input cleared immediately, LLM processing happens in background") 442}