|
| 1 | +package tui |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + tea "charm.land/bubbletea/v2" |
| 9 | + |
| 10 | + "github.com/Gitlawb/zero/internal/config" |
| 11 | + mcppkg "github.com/Gitlawb/zero/internal/mcp" |
| 12 | +) |
| 13 | + |
| 14 | +// AN ALREADY-OPEN MANAGER HAS TO BE TOLD, NOT ASKED. |
| 15 | +// |
| 16 | +// Optional MCP registration runs on its own goroutine so a slow server cannot |
| 17 | +// delay the first response, which means its result lands with no user input |
| 18 | +// behind it. Bubble Tea renders only in response to a message, so a getter that |
| 19 | +// reports late failures is not enough on its own: an overlay opened while a |
| 20 | +// server was still connecting keeps rendering the configuration-derived enabled |
| 21 | +// state until unrelated input or a resize happens to redraw it. |
| 22 | +func TestAnOpenManagerRebuildsWhenBackgroundStartupCompletes(t *testing.T) { |
| 23 | + cfg := config.MCPConfig{Servers: map[string]config.MCPServerConfig{ |
| 24 | + "optional": {URL: "https://optional.invalid/mcp"}, |
| 25 | + }} |
| 26 | + var late []mcppkg.SkippedServer |
| 27 | + done := make(chan struct{}) |
| 28 | + |
| 29 | + m := &model{ |
| 30 | + width: 120, |
| 31 | + mcpConfig: cfg, |
| 32 | + mcpStartupConfig: cfg, |
| 33 | + mcpManager: &mcpManagerState{}, |
| 34 | + mcpLateSkipped: func() []mcppkg.SkippedServer { return late }, |
| 35 | + mcpStartupCompleted: done, |
| 36 | + } |
| 37 | + |
| 38 | + // Opened while the background registration is still in flight. |
| 39 | + before := m.mcpManagerOverlay(m.width) |
| 40 | + if !strings.Contains(before, "optional") { |
| 41 | + t.Fatalf("SETUP INVALID: the manager does not list the server:\n%s", before) |
| 42 | + } |
| 43 | + if strings.Contains(strings.ToLower(before), "failed") { |
| 44 | + t.Fatalf("SETUP INVALID: it already reads as failed before the result arrived:\n%s", before) |
| 45 | + } |
| 46 | + |
| 47 | + // The background registration finishes and reports the failure. No key, no |
| 48 | + // resize, no command: only the completion. |
| 49 | + late = []mcppkg.SkippedServer{{Name: "optional", Err: errors.New("optional server refused the connection")}} |
| 50 | + close(done) |
| 51 | + |
| 52 | + updated, _ := m.Update(mcpStartupCompletedMsg{}) |
| 53 | + next, ok := updated.(model) |
| 54 | + if !ok { |
| 55 | + t.Fatalf("Update returned %T, want a model", updated) |
| 56 | + } |
| 57 | + |
| 58 | + // Asserted on the CACHE, not by rendering. Rendering calls mcpViewState, |
| 59 | + // which invalidates on demand, so an overlay drawn after the message would |
| 60 | + // look right even if the handler did nothing: the test would pass for the |
| 61 | + // wrong reason and stop pinning the handler at all. The cache being fresh |
| 62 | + // BEFORE anything renders is what says the completion was consumed. |
| 63 | + var cached string |
| 64 | + for _, server := range next.mcpViewStateCache.Servers { |
| 65 | + if server.Name == "optional" { |
| 66 | + cached = server.State + "|" + server.Error |
| 67 | + } |
| 68 | + } |
| 69 | + if !strings.Contains(cached, "failed") { |
| 70 | + t.Errorf("the completion did not rebuild the view state: %q", cached) |
| 71 | + } |
| 72 | + if !strings.Contains(cached, "refused the connection") { |
| 73 | + t.Errorf("the rebuilt state carries no reason: %q", cached) |
| 74 | + } |
| 75 | + |
| 76 | + after := next.mcpManagerOverlay(next.width) |
| 77 | + if !strings.Contains(strings.ToLower(after), "failed") { |
| 78 | + t.Errorf("the open manager still shows the configured state after the background failure:\n%s", after) |
| 79 | + } |
| 80 | + if !strings.Contains(after, "refused the connection") { |
| 81 | + t.Errorf("the reason never reached the open manager:\n%s", after) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// And Init actually schedules the wait, or the message above would never be |
| 86 | +// produced in a real session. |
| 87 | +func TestInitWaitsForBackgroundStartupCompletion(t *testing.T) { |
| 88 | + done := make(chan struct{}) |
| 89 | + m := model{mcpStartupCompleted: done} |
| 90 | + close(done) |
| 91 | + |
| 92 | + cmd := m.Init() |
| 93 | + if cmd == nil { |
| 94 | + t.Fatal("Init scheduled nothing") |
| 95 | + } |
| 96 | + if !producesMCPStartupCompletion(cmd) { |
| 97 | + t.Error("Init never scheduled the wait for background MCP startup, so a completed registration reaches no open surface") |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// producesMCPStartupCompletion runs a batched command tree far enough to see |
| 102 | +// whether the completion wait is part of it. |
| 103 | +func producesMCPStartupCompletion(cmd tea.Cmd) bool { |
| 104 | + if cmd == nil { |
| 105 | + return false |
| 106 | + } |
| 107 | + switch msg := cmd().(type) { |
| 108 | + case mcpStartupCompletedMsg: |
| 109 | + return true |
| 110 | + case tea.BatchMsg: |
| 111 | + for _, sub := range msg { |
| 112 | + if producesMCPStartupCompletion(sub) { |
| 113 | + return true |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + return false |
| 118 | +} |
0 commit comments