| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | tea "charm.land/bubbletea/v2" |
| 9 | |
| 10 | "reasonix/internal/config" |
| 11 | "reasonix/internal/control" |
| 12 | "reasonix/internal/provider" |
| 13 | ) |
| 14 | |
| 15 | type connectionSetupTestRunner struct{ calls int } |
| 16 | |
| 17 | func (r *connectionSetupTestRunner) Run(context.Context, string) error { |
| 18 | r.calls++ |
| 19 | return nil |
| 20 | } |
| 21 | |
| 22 | func TestConnectionSetupMasksCredential(t *testing.T) { |
| 23 | m := newTestChatTUI() |
| 24 | m.width = 80 |
| 25 | m.setup = &connectionSetup{providerName: "deepseek", key: "never-render-this-key"} |
| 26 | view := m.renderConnectionSetup() |
| 27 | if strings.Contains(view, m.setup.key) { |
| 28 | t.Fatal("connection setup rendered the credential") |
| 29 | } |
| 30 | if !strings.Contains(view, "Ctrl+T test") { |
| 31 | t.Fatalf("connection test action missing:\n%s", view) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestAuthenticationSlashAliases(t *testing.T) { |
| 36 | if got := canonicalBuiltinSlashCommand("/?"); got != "/help" { |
| 37 | t.Fatalf("/? = %q, want /help", got) |
| 38 | } |
| 39 | if got := canonicalBuiltinSlashCommand("/auth"); got != "/setup" { |
| 40 | t.Fatalf("/auth = %q, want /setup", got) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestConnectionSetupInvalidatesEditedProbe(t *testing.T) { |
| 45 | m := newTestChatTUI() |
| 46 | cancelled := false |
| 47 | setup := &connectionSetup{providerName: "relay", key: "old", testing: true, testVersion: 3, testCancel: func() { cancelled = true }} |
| 48 | m.setup = setup |
| 49 | updated, _ := m.handleConnectionSetupKey(tea.KeyPressMsg{Code: 'x', Text: "x"}) |
| 50 | m = updated.(chatTUI) |
| 51 | if !cancelled || m.setup.testing || m.setup.key != "oldx" { |
| 52 | t.Fatalf("edited setup = %+v, cancelled=%v", m.setup, cancelled) |
| 53 | } |
| 54 | before := len(m.transcript) |
| 55 | m.handleConnectionCredentialTested(connectionCredentialTestedMsg{providerName: "relay", setup: setup, version: 3}) |
| 56 | if len(m.transcript) != before { |
| 57 | t.Fatal("stale connection test updated the current UI") |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestConnectionSetupConflictKeepsEditorOpen(t *testing.T) { |
| 62 | m := newTestChatTUI() |
| 63 | m.setup = &connectionSetup{providerName: "relay", key: "draft", saving: true} |
| 64 | cmd := m.handleConnectionCredentialSaved(connectionCredentialSavedMsg{providerName: "relay", err: context.Canceled}) |
| 65 | if cmd != nil || m.setup == nil || m.setup.saving || m.setup.key != "draft" { |
| 66 | t.Fatalf("failed save lost editor state: setup=%+v cmd=%v", m.setup, cmd) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestConnectionSetupSaveSchedulesRuntimeRebuild(t *testing.T) { |
| 71 | runner := &connectionSetupTestRunner{} |
| 72 | ctrl := control.New(control.Options{Runner: runner}) |
| 73 | defer ctrl.Close() |
| 74 | m := newTestChatTUI() |
| 75 | m.ctrl = ctrl |
| 76 | m.modelRef = "relay/chat" |
| 77 | m.setup = &connectionSetup{providerName: "relay", key: "saved", saving: true} |
| 78 | m.buildController = func(controllerBuildSpec, []provider.Message, string, control.SessionAPI) (*control.Controller, error) { |
| 79 | return nil, nil |
| 80 | } |
| 81 | cmd := m.handleConnectionCredentialSaved(connectionCredentialSavedMsg{providerName: "relay", result: config.ConnectionCredentialResult{Persisted: true}}) |
| 82 | if cmd == nil || !m.modelSwitchPending || m.setup != nil { |
| 83 | t.Fatalf("saved credential did not schedule rebuild: pending=%v setup=%+v", m.modelSwitchPending, m.setup) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func TestMissingAuthenticationPreservesCLIInputWithoutProviderCall(t *testing.T) { |
| 88 | runner := &connectionSetupTestRunner{} |
| 89 | ctrl := control.New(control.Options{Runner: runner, Authentication: control.AuthenticationState{Status: control.AuthenticationMissingCredential, ProviderName: "relay"}}) |
| 90 | defer ctrl.Close() |
| 91 | m := newTestChatTUI() |
| 92 | m.ctrl = ctrl |
| 93 | m.input.SetValue("keep this draft") |
| 94 | before := len(m.transcript) |
| 95 | cmd := m.startControllerTurnWithQueue("keep this draft", "keep this draft", "", func(control.SessionAPI) { ctrl.Submit("must not run") }) |
| 96 | if cmd != nil { |
| 97 | t.Fatal("missing authentication scheduled a model turn") |
| 98 | } |
| 99 | if runner.calls != 0 || m.input.Value() != "keep this draft" { |
| 100 | t.Fatalf("blocked input changed state: calls=%d input=%q", runner.calls, m.input.Value()) |
| 101 | } |
| 102 | if len(m.transcript) != before+1 { |
| 103 | t.Fatalf("blocked input should add only a local notice: before=%d after=%d", before, len(m.transcript)) |
| 104 | } |
| 105 | } |
| 106 |