package daemon import ( "encoding/json" "net/http" "testing" "albedo/cli/internal/config" ) func TestSelectProfileAccountScopesTheSavedProfile(t *testing.T) { personal := "https://api.githubcopilot.com" enterprise := "https://copilot-api.contoso.example" account := func(id string, endpoint *string) Account { return Account{Provider: "github-copilot", ID: id, Label: "work", Endpoint: endpoint} } profiles := func() config.Profiles { return config.Profiles{ ETag: `"providers-1"`, Providers: map[string]config.Settings{ "copilot": {Extension: "github-copilot", BaseURL: personal, Protocol: "chat_completions"}, }, } } // The pin goes through the real provider save, so the daemon-bound patch // carries the scope the profile ends up with. savedPatch := func(t *testing.T, account Account) map[string]any { t.Helper() var sent map[string]any conn := controlledConnection(t, func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") if r.Method != http.MethodPatch || r.URL.String() != "/settings?group=providers" || r.Header.Get("If-Match") != `"providers-1"` { t.Errorf("wrong provider save: %s %s %v", r.Method, r.URL, r.Header) } if err := json.NewDecoder(r.Body).Decode(&sent); err != nil { t.Errorf("provider save body: %v", err) } _ = json.NewEncoder(w).Encode(map[string]any{ "application": map[string]any{ "active_service_revision": nil, "composition_changed": false, "desired_revision": "", "validation": "", "warnings": []any{}, }, "group": "providers", "resource": map[string]any{ "etag": `"providers-2"`, "url": "/settings?group=providers", "value": map[string]any{ "default_profile": nil, "profiles": map[string]any{}, }, }, }) }) if err := SelectProfileAccount(t.Context(), conn, "copilot", account, profiles()); err != nil { t.Fatalf("SelectProfileAccount failed: %v", err) } return sent } changed := savedPatch(t, account("acc-ent", &enterprise)) if _, selected := changed["default_profile"]; selected { t.Fatal("re-pinning must not change the default profile") } pinned := changed["profiles"].(map[string]any)["copilot"].(map[string]any) if pinned["endpoint"] != enterprise || pinned["account_id"] != "acc-ent" || pinned["account_ids"] != nil { t.Fatalf("pinning an enterprise account saved: %v", pinned) } kept := savedPatch(t, account("acc-plain", nil)) keptProfile := kept["profiles"].(map[string]any)["copilot"].(map[string]any) if keptProfile["endpoint"] != personal || keptProfile["account_id"] != "acc-plain" { t.Fatalf("an account without an endpoint changed the saved base url: %v", keptProfile) } } func TestSignInListPopulatesKind(t *testing.T) { conn := controlledConnection(t, func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/auth" { t.Errorf("unexpected path: %s", r.URL.Path) } _ = json.NewEncoder(w).Encode(map[string]any{ "providers": []map[string]any{ { "id": "claude", "label": "Claude", "detail": "console billing", "kind": "model", "flows": []string{"browser"}, "fields": []any{}, }, { "id": "colab", "label": "Google Colab", "detail": "service account", "kind": "service", "flows": []string{"browser"}, "fields": []any{}, }, }, "accounts": []map[string]any{ { "id": "acc-model", "provider": "claude", "label": "user@example.com", "detail": "pro plan", "kind": "model", "selected_by_profiles": []string{"claude"}, }, { "id": "acc-service", "provider": "colab", "label": "service@example.com", "detail": "colab tokens", "kind": "service", "selected_by_profiles": []string{}, }, }, }) }) list, err := SignInList(t.Context(), conn) if err != nil { t.Fatalf("SignInList failed: %v", err) } if len(list.Logins) != 2 || list.Logins[0].Kind != "model" || list.Logins[1].Kind != "service" { t.Fatalf("unexpected logins kind: %+v", list.Logins) } if len(list.Accounts) != 2 || list.Accounts[0].Kind != "model" || list.Accounts[1].Kind != "service" { t.Fatalf("unexpected accounts kind: %+v", list.Accounts) } }