|
1 | 1 | package tui |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "os" |
5 | 6 | "testing" |
6 | 7 | ) |
@@ -68,6 +69,41 @@ func TestSelectManager(t *testing.T) { |
68 | 69 | }) |
69 | 70 | } |
70 | 71 |
|
| 72 | +func TestSimpleManagerRunBrowserFlow(t *testing.T) { |
| 73 | + t.Run("Non-error updates do not trigger fallback", func(t *testing.T) { |
| 74 | + m := NewSimpleManager() |
| 75 | + perform := func(ctx context.Context, updates chan<- FlowUpdate) (*TokenStorage, bool, error) { |
| 76 | + updates <- FlowUpdate{Type: StepStart, Step: 1} |
| 77 | + updates <- FlowUpdate{Type: BrowserOpened} |
| 78 | + updates <- FlowUpdate{Type: StepStart, Step: 2} |
| 79 | + updates <- FlowUpdate{Type: CallbackReceived} |
| 80 | + return &TokenStorage{AccessToken: "test-token"}, true, nil |
| 81 | + } |
| 82 | + _, ok, err := m.RunBrowserFlow(context.Background(), perform) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("unexpected error: %v", err) |
| 85 | + } |
| 86 | + if !ok { |
| 87 | + t.Error("non-error updates must not trigger fallback: expected ok=true") |
| 88 | + } |
| 89 | + }) |
| 90 | + |
| 91 | + t.Run("perform returning ok=false is propagated as fallback signal", func(t *testing.T) { |
| 92 | + m := NewSimpleManager() |
| 93 | + perform := func(ctx context.Context, updates chan<- FlowUpdate) (*TokenStorage, bool, error) { |
| 94 | + updates <- FlowUpdate{Type: StepError, Message: "browser failed"} |
| 95 | + return nil, false, nil |
| 96 | + } |
| 97 | + _, ok, err := m.RunBrowserFlow(context.Background(), perform) |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("unexpected error: %v", err) |
| 100 | + } |
| 101 | + if ok { |
| 102 | + t.Error("expected ok=false when perform signals fallback needed") |
| 103 | + } |
| 104 | + }) |
| 105 | +} |
| 106 | + |
71 | 107 | func TestFlowUpdateHelpers(t *testing.T) { |
72 | 108 | update := FlowUpdate{ |
73 | 109 | Type: StepStart, |
@@ -105,6 +141,108 @@ func TestFlowUpdateHelpers(t *testing.T) { |
105 | 141 | } |
106 | 142 | } |
107 | 143 |
|
| 144 | +func TestFlowUpdate_FallbackField(t *testing.T) { |
| 145 | + t.Run("Fallback defaults to false", func(t *testing.T) { |
| 146 | + update := FlowUpdate{Type: StepError, Message: "some hard error"} |
| 147 | + if update.Fallback { |
| 148 | + t.Error("Fallback should default to false for hard errors") |
| 149 | + } |
| 150 | + }) |
| 151 | + |
| 152 | + t.Run("Fallback can be set to true for soft errors", func(t *testing.T) { |
| 153 | + update := FlowUpdate{ |
| 154 | + Type: StepError, |
| 155 | + Fallback: true, |
| 156 | + Message: "Browser authorization timed out", |
| 157 | + } |
| 158 | + if !update.Fallback { |
| 159 | + t.Error("Fallback should be true for soft (recoverable) errors") |
| 160 | + } |
| 161 | + }) |
| 162 | + |
| 163 | + t.Run("Fallback is ignored on non-error updates", func(t *testing.T) { |
| 164 | + update := FlowUpdate{Type: StepStart, Step: 1, Fallback: false} |
| 165 | + if update.Fallback { |
| 166 | + t.Error("Fallback should not be set on non-error updates") |
| 167 | + } |
| 168 | + }) |
| 169 | +} |
| 170 | + |
| 171 | +// TestBrowserModel_HandleFlowUpdate_StepError verifies that StepError updates |
| 172 | +// are routed to the correct internal message type based on the Fallback field, |
| 173 | +// so the Bubble Tea program always receives a quit signal (no hanging). |
| 174 | +func TestBrowserModel_HandleFlowUpdate_StepError(t *testing.T) { |
| 175 | + t.Run("hard error returns errorMsg", func(t *testing.T) { |
| 176 | + updates := make(chan FlowUpdate, 1) |
| 177 | + model := NewBrowserModel(updates, nil) |
| 178 | + |
| 179 | + cmd := model.handleFlowUpdate(FlowUpdate{ |
| 180 | + Type: StepError, |
| 181 | + Message: "Authentication failed: state_mismatch: state parameter mismatch", |
| 182 | + }) |
| 183 | + |
| 184 | + if cmd == nil { |
| 185 | + t.Fatal("expected non-nil tea.Cmd for hard error") |
| 186 | + } |
| 187 | + msg := cmd() |
| 188 | + if _, ok := msg.(errorMsg); !ok { |
| 189 | + t.Errorf("expected errorMsg for hard error, got %T", msg) |
| 190 | + } |
| 191 | + }) |
| 192 | + |
| 193 | + t.Run("soft error with Fallback=true returns successMsg{ok:false}", func(t *testing.T) { |
| 194 | + updates := make(chan FlowUpdate, 1) |
| 195 | + model := NewBrowserModel(updates, nil) |
| 196 | + |
| 197 | + cmd := model.handleFlowUpdate(FlowUpdate{ |
| 198 | + Type: StepError, |
| 199 | + Fallback: true, |
| 200 | + Message: "Browser authorization timed out", |
| 201 | + }) |
| 202 | + |
| 203 | + if cmd == nil { |
| 204 | + t.Fatal("expected non-nil tea.Cmd for soft error") |
| 205 | + } |
| 206 | + msg := cmd() |
| 207 | + sm, ok := msg.(successMsg) |
| 208 | + if !ok { |
| 209 | + t.Errorf("expected successMsg for soft error, got %T", msg) |
| 210 | + } |
| 211 | + if sm.ok { |
| 212 | + t.Error("successMsg.ok should be false for fallback (device flow signal)") |
| 213 | + } |
| 214 | + if sm.storage != nil { |
| 215 | + t.Error("successMsg.storage should be nil for fallback") |
| 216 | + } |
| 217 | + }) |
| 218 | + |
| 219 | + t.Run( |
| 220 | + "browser open failure with Fallback=true returns successMsg{ok:false}", |
| 221 | + func(t *testing.T) { |
| 222 | + updates := make(chan FlowUpdate, 1) |
| 223 | + model := NewBrowserModel(updates, nil) |
| 224 | + |
| 225 | + cmd := model.handleFlowUpdate(FlowUpdate{ |
| 226 | + Type: StepError, |
| 227 | + Fallback: true, |
| 228 | + Message: "Could not open browser: exec: no command", |
| 229 | + }) |
| 230 | + |
| 231 | + if cmd == nil { |
| 232 | + t.Fatal("expected non-nil tea.Cmd") |
| 233 | + } |
| 234 | + msg := cmd() |
| 235 | + sm, ok := msg.(successMsg) |
| 236 | + if !ok { |
| 237 | + t.Errorf("expected successMsg, got %T", msg) |
| 238 | + } |
| 239 | + if sm.ok { |
| 240 | + t.Error("successMsg.ok should be false for fallback") |
| 241 | + } |
| 242 | + }, |
| 243 | + ) |
| 244 | +} |
| 245 | + |
108 | 246 | func TestFlowUpdateTypeString(t *testing.T) { |
109 | 247 | tests := []struct { |
110 | 248 | updateType FlowUpdateType |
|
0 commit comments