package api import ( "encoding/json" "net/http" "strings" "testing" ) // Toyota, Lexus and Subaru are one connector shown as three integrations, so // each brand must be a vehicle provider of its own, labelled with its app. func TestEveryBrandIsAVehicleProvider(t *testing.T) { want := map[string]string{"toyota": "MyToyota", "lexus": "Lexus Link+", "subaru": "MySubaru"} for id, label := range want { src, ok := vehicleSourceByID(id) if !ok { t.Errorf("%q is not a vehicle provider", id) continue } if src.label() != label { t.Errorf("%q label = %q, want %q", id, src.label(), label) } } if n := len(vehicleSources()); n != len(want) { t.Errorf("vehicle providers = %d, want %d", n, len(want)) } } // Before the split every brand lived under pluginSettings.toyota with a brand // field. A Lexus or Subaru entry there belongs to that brand now, and must leave // the Toyota card empty rather than show the other brand's login. func TestLegacyBrandEntryMovesToItsBrand(t *testing.T) { lexus, _ := brandByName("lexus") subaru, _ := brandByName("subaru") raw := json.RawMessage(`{"toyota":{"config":{"username":"me@x","password":"pw","brand":"L"},"enabled":true},"anker-solix":{"enabled":true}}`) if got := storedFor(raw, lexus); got.Config.Username != "me@x" || got.Config.Password != "pw" || !got.Enabled { t.Errorf("lexus entry = %+v, want the legacy login, enabled", got) } if got := storedFor(raw, toyotaBrand); got.Config.Username != "" || got.Enabled { t.Errorf("toyota entry = %+v, want empty — the legacy login was a Lexus one", got) } if got := storedFor(raw, subaru); got.Config.Username != "" { t.Errorf("subaru entry = %+v, want empty", got) } // Saving any brand writes the move back and keeps unrelated plugins. out := mergeToyota(raw, toyotaBrand, func(ts *toyotaStored) { ts.Config.Username = "t@x" }) var doc map[string]map[string]any _ = json.Unmarshal(out, &doc) if doc["lexus"] == nil || doc["anker-solix"] == nil { t.Fatalf("merged doc lost an entry: %s", out) } if cfg, _ := doc["toyota"]["config"].(map[string]any); cfg["username"] != "t@x" || cfg["brand"] != nil { t.Errorf("toyota entry after save = %v", doc["toyota"]) } // A plain Toyota entry, with or without the old field, stays where it is. for _, plain := range []string{ `{"toyota":{"config":{"username":"t@x","brand":"T"}}}`, `{"toyota":{"config":{"username":"t@x"}}}`, } { if got := storedFor(json.RawMessage(plain), toyotaBrand); got.Config.Username != "t@x" { t.Errorf("%s: toyota entry = %+v", plain, got) } } // A brand that already has its own entry keeps it; the legacy one is dropped. both := json.RawMessage(`{"toyota":{"config":{"username":"old","brand":"S"}},"subaru":{"config":{"username":"new"}}}`) if got := storedFor(both, subaru); got.Config.Username != "new" { t.Errorf("subaru entry = %+v, want its own", got) } } // A car linked before the split says provider "toyota" whatever its make; it // must read through its own brand's account. func TestLegacyCarLinkFollowsItsMake(t *testing.T) { cases := []struct{ provider, make, want string }{ {"toyota", "Lexus", "lexus"}, {"toyota", "SUBARU", "subaru"}, {"toyota", "Toyota", "toyota"}, {"toyota", "", "toyota"}, {"lexus", "Lexus", "lexus"}, } for _, c := range cases { src, ok := carVehicleSource(c.provider, c.make) if !ok || src.id() != c.want { t.Errorf("carVehicleSource(%q, %q) = %v, want %q", c.provider, c.make, src, c.want) } } } // Each brand answers on its own route with its own identity, and the provider // listing offers all three — the server end of "three plugins in the apps". func TestEveryBrandHasItsOwnIntegrationRoutes(t *testing.T) { srv := newProviderTestServer(t, &fakeProviderPB{}) for _, c := range []struct{ brand, make, app string }{ {"toyota", "Toyota", "MyToyota"}, {"lexus", "Lexus", "Lexus Link+"}, {"subaru", "Subaru", "MySubaru"}, } { status, body := call(t, srv, http.MethodGet, "/api/integrations/"+c.brand, "") if status != http.StatusOK { t.Errorf("GET %s: status %d", c.brand, status) continue } if body["brand"] != c.brand || body["make"] != c.make || body["app"] != c.app { t.Errorf("GET %s: identity = %v/%v/%v", c.brand, body["brand"], body["make"], body["app"]) } scopes, _ := body["scopes"].(map[string]any) user, _ := scopes["user"].(map[string]any) fields, _ := user["fields"].(map[string]any) if _, hasBrand := fields["brand"]; hasBrand || fields["username"] == nil { t.Errorf("GET %s: fields = %v, want username/password and no brand picker", c.brand, fields) } // Plugin not enabled for lexus/subaru in this store: the probe says so, // naming the brand rather than Toyota. status, body = call(t, srv, http.MethodPost, "/api/integrations/"+c.brand+"/health", "") h, _ := body["health"].(map[string]any) if status != http.StatusOK || h["status"] != "down" || !strings.Contains(h["detail"].(string), c.make) { t.Errorf("POST %s/health: %d %v", c.brand, status, body) } } _, body := call(t, srv, http.MethodGet, "/api/vehicle-providers", "") if list, _ := body["providers"].([]any); len(list) != 3 { t.Errorf("providers = %v, want three", body["providers"]) } }