// The /login list's state across a change the daemon refuses and a read that // fails. A real daemon cannot fail one read on cue, so E2E never sees the // screen drop rows it still holds when only the listing is missing. package tui import ( "encoding/json" "net/http" "strings" "testing" "albedo/cli/internal/daemon" "albedo/cli/internal/testwire" tea "charm.land/bubbletea/v2" ) // pinnedKeyLogin is /login listing one key account the work profile pins. func pinnedKeyLogin(t *testing.T, conn *daemon.Connection) LoginModel { t.Helper() m := loginModel(120, 40) m.Conn = conn loginKeyAccounts(&m) m.buildChoosePicker() return m } func TestLoginRefusedRemovalKeepsTheAccountListed(t *testing.T) { endpoint := "https://api.openai.com/v1" conn := commandTestConnection(t, func(w http.ResponseWriter, r *http.Request) { switch { case r.Method == http.MethodDelete && r.URL.Path == "/auth/accounts/key-1": w.Header().Set("Content-Type", "application/problem+json") w.WriteHeader(http.StatusConflict) _, _ = w.Write([]byte(`{"code":"account_in_use","detail":"Provider account is referenced by a saved profile."}`)) case r.URL.Path == "/auth": _ = json.NewEncoder(w).Encode(map[string]any{"providers": []any{}, "accounts": []any{map[string]any{ "id": "key-1", "provider": "openai", "label": "team", "detail": "api.openai.com", "kind": "key", "source": "saved", "endpoint": endpoint, "limited_until": nil, "selected_by_profiles": []string{"work"}, "rotated_by_profiles": []string{}, }}}) case r.URL.Path == "/settings": _ = json.NewEncoder(w).Encode(testwire.Settings()) default: t.Errorf("unexpected request %s %s", r.Method, r.URL) w.WriteHeader(http.StatusNotFound) } }) m := pinnedKeyLogin(t, conn) // The listing was read before a profile pinned the key, so the screen // asks, and the daemon refuses. focusLoginRow(&m, "account:1") m, _ = pressCtrl(m, 'd') if m.Step != StepRemove { t.Fatalf("ctrl+d on an unpinned account did not ask: step %v", m.Step) } m.Status = "a line from before the change" m, cmd := m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) if m.Step != StepSaving || cmd == nil { t.Fatalf("enter did not remove: step %v", m.Step) } m, _ = m.Update(cmd()) if m.Step != StepChoose || !strings.Contains(m.Error, "still used by work") { t.Fatalf("the refusal is not shown: step %v error %q", m.Step, m.Error) } if len(m.Accounts) != 1 || m.Accounts[0].ID != "key-1" || !strings.Contains(m.View(), "team") { t.Fatalf("the refused account is not listed: %+v\n%s", m.Accounts, m.View()) } if m.Status != "" { t.Fatalf("the status from before the change stayed: %q", m.Status) } } // A Mistral sign-in saves its key with no endpoint, so the profile's own // applies. Now that a sign-in draft starts from Mistral's endpoint, that key // must still be offered, and picking it must keep the endpoint. func TestLoginSignInDraftKeepsEndpointlessKeys(t *testing.T) { m := loginModel(120, 40) none, other := "", "https://elsewhere.example/v1" m.SignIns = append(m.SignIns, daemon.SignIn{Provider: "mistral", Label: "sign in to mistral"}) m.Accounts = []daemon.Account{ {Provider: "mistral", ID: "signed-in", Label: "Mistral sign-in", Kind: "key", Source: "saved", Endpoint: &none}, {Provider: "mistral", ID: "elsewhere", Label: "other endpoint", Kind: "key", Source: "saved", Endpoint: &other}, } m.askProfileName("mistral") if m.Draft.BaseURL != "https://api.mistral.ai/v1" { t.Fatalf("the sign-in draft has no endpoint: %+v", m.Draft) } if got := m.credentialAccounts(); len(got) != 1 || got[0].ID != "signed-in" { t.Fatalf("the chooser offers %+v, want only the endpointless sign-in key", got) } m.SelectedAccount = &m.Accounts[0] m.TextInput.SetValue("mistral") m, _ = m.acceptProfileName() if m.Draft.BaseURL != "https://api.mistral.ai/v1" || m.Draft.AccountID == nil || *m.Draft.AccountID != "signed-in" { t.Fatalf("pinning the sign-in key lost the endpoint: %+v", m.Draft) } } func TestLoginFailedReadKeepsTheRows(t *testing.T) { m := pinnedKeyLogin(t, &daemon.Connection{}) before := len(m.Accounts) m, _ = m.Update(signInsLoadedMsg{Gen: m.Generation, Err: &daemon.APIError{StatusCode: 503, Message: "daemon unavailable"}}) if len(m.Accounts) != before || len(m.SignIns) == 0 || len(m.Profiles.Providers) == 0 { t.Fatalf("a failed read cleared what was shown: %d accounts, %d sign-ins, %d profiles", len(m.Accounts), len(m.SignIns), len(m.Profiles.Providers)) } if m.Error != "daemon unavailable" { t.Fatalf("the failed read is not reported: %q", m.Error) } } func TestLoginPinnedAccountRemovalSaysWhichProfilesUseIt(t *testing.T) { m := pinnedKeyLogin(t, &daemon.Connection{}) m.Accounts[1].SelectedByProfiles = []string{"work", "home"} m.buildChoosePicker() focusLoginRow(&m, "account:1") m, cmd := pressCtrl(m, 'd') if m.Step != StepChoose || cmd != nil || m.asking() { t.Fatalf("ctrl+d on a pinned account asked anyway: step %v", m.Step) } if !strings.Contains(m.Status, "work, home") || !strings.Contains(m.Status, "ctrl+e") { t.Fatalf("the refusal does not name the profiles or the way out: %q", m.Status) } // The last account of a rotation is refused the same way; one of two is // not, and the confirm says which rotation it leaves. m.Accounts[1].SelectedByProfiles = nil m.Accounts[1].RotatedByProfiles = []string{"work"} work := m.Profiles.Providers["work"] work.AccountID, work.AccountIDs = nil, []string{"key-1"} m.Profiles.Providers["work"] = work m.buildChoosePicker() focusLoginRow(&m, "account:1") if m, _ = pressCtrl(m, 'd'); m.Step != StepChoose || !strings.Contains(m.Status, "last account work rotates") { t.Fatalf("removing a rotation's last account was not refused: step %v status %q", m.Step, m.Status) } work.AccountIDs = []string{"key-1", "key-2"} m.Profiles.Providers["work"] = work if m, _ = pressCtrl(m, 'd'); m.Step != StepRemove { t.Fatalf("one account of two in a rotation was refused: step %v status %q", m.Step, m.Status) } }