diff --git a/internal/provider/provider_order.go b/internal/provider/provider_order.go index 4432b40b0..bf3ec9c56 100644 --- a/internal/provider/provider_order.go +++ b/internal/provider/provider_order.go @@ -3,6 +3,7 @@ package provider import ( "fmt" "slices" + "strings" ) // ordered is ps in the order the user put them in (#499): those order @@ -25,12 +26,32 @@ func ordered(ps []Provider, order []string) []Provider { if i, ok := at[p.ID]; ok { return i } + if i, ok := at[samePlace(p.ID)]; ok { + return i + } return len(order) } slices.SortStableFunc(ps, func(a, b Provider) int { return rank(a) - rank(b) }) return ps } +// samePlace is the id the same subscription has on another computer: a +// built-in's plugin is id-plugin where the built-in isn't moved onto it +// and id where it is (PluginID), and whether it is moved is each +// computer's own (migrations.json isn't synced). An order synced from +// the other computer names it by the other id, and it keeps its place by +// that one (#1940, Sun1090: WorkBuddy and WorkBuddy AI, put first on one +// computer, weren't on the other). +func samePlace(id string) string { + if base, ok := strings.CutSuffix(id, "-plugin"); ok && slices.Contains(accountIDs, base) { + return base + } + if slices.Contains(accountIDs, id) { + return id + "-plugin" + } + return "" +} + // SetOrder saves the order the providers are listed and tried in, by id. // Every id must be a provider's, once; the ones left out follow those // named, as they are now, so a provider added later goes after them all. diff --git a/internal/provider/provider_order_test.go b/internal/provider/provider_order_test.go index fafb383a4..02772ca44 100644 --- a/internal/provider/provider_order_test.go +++ b/internal/provider/provider_order_test.go @@ -100,3 +100,49 @@ func TestMirrorOrder(t *testing.T) { t.Fatalf("listed: %v", listed) } } + +// #1940 (Sun1090): WorkBuddy and WorkBuddy AI were put first on one +// computer, and the order synced to another kept them where they were. A +// subscription served by its plugin is workbuddy-plugin where the built-in +// isn't moved onto it and workbuddy where it is, and that is each +// computer's own, so the order synced names it by the other computer's id. +// It keeps its place by either. +func TestOrderKeepsAPluginsPlaceByEitherID(t *testing.T) { + ps := func(ids ...string) []Provider { + var out []Provider + for _, id := range ids { + out = append(out, Provider{ID: id}) + } + return out + } + ids := func(ps []Provider) []string { + var out []string + for _, p := range ps { + out = append(out, p.ID) + } + return out + } + // put in order where both were moved: workbuddy-ai, workbuddy + order := []string{WorkBuddyAIID, "workbuddy", "zhipu", "deepseek"} + // here neither is moved, so the plugin signed in under its own id + got := ids(ordered(ps("zhipu", "deepseek", "workbuddy-plugin", WorkBuddyAIID+"-plugin"), order)) + if want := []string{WorkBuddyAIID + "-plugin", "workbuddy-plugin", "zhipu", "deepseek"}; !slices.Equal(got, want) { + t.Fatalf("not moved here: %v, want %v", got, want) + } + // and the other way round + order = []string{"workbuddy-plugin", WorkBuddyAIID + "-plugin", "zhipu"} + got = ids(ordered(ps("zhipu", WorkBuddyAIID, "workbuddy"), order)) + if want := []string{"workbuddy", WorkBuddyAIID, "zhipu"}; !slices.Equal(got, want) { + t.Fatalf("moved here: %v, want %v", got, want) + } + // an id the order names itself wins over its counterpart's place + got = ids(ordered(ps("zhipu", "workbuddy", "workbuddy-plugin"), []string{"workbuddy-plugin", "zhipu", "workbuddy"})) + if want := []string{"workbuddy-plugin", "zhipu", "workbuddy"}; !slices.Equal(got, want) { + t.Fatalf("both here: %v, want %v", got, want) + } + // a custom provider whose id ends in -plugin is only itself + got = ids(ordered(ps("zhipu", "my-plugin", "my"), []string{"my", "zhipu"})) + if want := []string{"my", "zhipu", "my-plugin"}; !slices.Equal(got, want) { + t.Fatalf("custom: %v, want %v", got, want) + } +}