package platform import ( "encoding/json" "net/http" "net/http/httptest" "strings" "testing" ) func TestHandlerDashboardSummary(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/dashboard/summary", nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } if !strings.Contains(rec.Body.String(), "onlineVehicles") { t.Fatalf("response missing onlineVehicles: %s", rec.Body.String()) } } func TestHandlerVehicles(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/vehicles?limit=10", nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } if !strings.Contains(rec.Body.String(), "LB9A32A24R0LS1426") { t.Fatalf("response missing vehicle: %s", rec.Body.String()) } } func TestHandlerVehicleCoverage(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/vehicles/coverage?limit=10&coverage=multi&online=online&bindingStatus=bound", nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } for _, want := range []string{"sourceCount", "onlineSourceCount", "protocols", "LB9A32A24R0LS1426"} { if !strings.Contains(rec.Body.String(), want) { t.Fatalf("response missing %q: %s", want, rec.Body.String()) } } } func TestHandlerVehicleDetail(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/vehicles/detail?vin=LB9A32A24R0LS1426", nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } for _, want := range []string{"lookupKey", "lookupResolved", "identity", "realtimeSummary", "realtime", "history", "raw", "mileage", "quality", "sources", "sourceStatus"} { if !strings.Contains(rec.Body.String(), want) { t.Fatalf("response missing %q: %s", want, rec.Body.String()) } } } func TestHandlerVehicleDetailResolvesPlateToVIN(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/vehicles/detail?vin=粤AG18312", nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } if !strings.Contains(rec.Body.String(), `"vin":"LB9A32A24R0LS1426"`) { t.Fatalf("response should resolve plate to VIN: %s", rec.Body.String()) } } func TestHandlerVehicleDetailAcceptsKeyword(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/vehicles/detail?keyword=粤AG18312", nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } if !strings.Contains(rec.Body.String(), `"vin":"LB9A32A24R0LS1426"`) { t.Fatalf("response should resolve keyword to VIN: %s", rec.Body.String()) } } func TestHandlerVehicleDetailKeepsUnresolvedLookupOutOfVIN(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/vehicles/detail?keyword=64646848247", nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } for _, want := range []string{`"lookupKey":"64646848247"`, `"lookupResolved":false`, `"vin":""`} { if !strings.Contains(rec.Body.String(), want) { t.Fatalf("response missing %q: %s", want, rec.Body.String()) } } var body struct { Data VehicleDetail `json:"data"` } if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { t.Fatalf("response JSON should decode: %v body=%s", err, rec.Body.String()) } if body.Data.VIN == "64646848247" { t.Fatalf("unresolved lookup should not be returned as top-level VIN: %s", rec.Body.String()) } if len(body.Data.Sources) != 0 || len(body.Data.SourceStatus) != 0 || len(body.Data.Raw.Items) != 0 || len(body.Data.History.Items) != 0 || len(body.Data.Mileage.Items) != 0 { t.Fatalf("unresolved lookup should not return vehicle data sections: %+v", body.Data) } } func TestHandlerRealtimeLocations(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/realtime/locations?limit=10", nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } if !strings.Contains(rec.Body.String(), "LB9A32A24R0LS1426") { t.Fatalf("response missing vehicle: %s", rec.Body.String()) } } func TestHandlerVehicleRealtime(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/realtime/vehicles?limit=10", nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } for _, want := range []string{"protocols", "sourceCount", "primaryProtocol", "LB9A32A24R0LS1426"} { if !strings.Contains(rec.Body.String(), want) { t.Fatalf("response missing %q: %s", want, rec.Body.String()) } } } func TestHandlerVehicleRealtimeAcceptsKeyword(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/realtime/vehicles?keyword=川AHTWO1&limit=10", nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } var body struct { Data struct { Items []VehicleRealtimeRow `json:"items"` } `json:"data"` } if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { t.Fatalf("response JSON should decode: %v body=%s", err, rec.Body.String()) } if len(body.Data.Items) != 1 || body.Data.Items[0].VIN != "LNXNEGRR7SR318212" { t.Fatalf("realtime vehicles should accept vehicle keyword, got %+v body=%s", body.Data.Items, rec.Body.String()) } } func TestHandlerHistoryMileageQualityOps(t *testing.T) { cases := []struct { path string want string }{ {"/api/history/locations?limit=10", "totalMileageKm"}, {"/api/history/raw-frames?protocol=GB32960&vin=LB9A32A24R0LS1426&limit=1&includeFields=true", "plate"}, {"/api/mileage/summary?limit=10", "totalMileageKm"}, {"/api/mileage/daily?limit=10", "dailyMileageKm"}, {"/api/quality/summary?limit=10", "issueVehicleCount"}, {"/api/quality/issues?limit=10", "sourceEndpoint"}, {"/api/ops/health", "linkHealth"}, } handler := NewHandler(NewService(NewMockStore())) for _, tc := range cases { t.Run(tc.path, func(t *testing.T) { rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, tc.path, nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } if !strings.Contains(rec.Body.String(), tc.want) { t.Fatalf("response missing %q: %s", tc.want, rec.Body.String()) } }) } } func TestHandlerVehicleDataAPIsResolveVehicleKeyword(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) cases := []struct { name string path string wantVIN string }{ {name: "history locations vin alias", path: "/api/history/locations?vin=粤AG18312&limit=10", wantVIN: "LB9A32A24R0LS1426"}, {name: "raw frames vin alias", path: "/api/history/raw-frames?vin=粤AG18312&limit=1", wantVIN: "LB9A32A24R0LS1426"}, {name: "daily mileage vin alias", path: "/api/mileage/daily?vin=粤AG18312&limit=10", wantVIN: "LB9A32A24R0LS1426"}, {name: "history locations keyword", path: "/api/history/locations?keyword=川AHTWO1&limit=10", wantVIN: "LNXNEGRR7SR318212"}, {name: "raw frames keyword", path: "/api/history/raw-frames?keyword=川AHTWO1&limit=1", wantVIN: "LNXNEGRR7SR318212"}, {name: "daily mileage keyword", path: "/api/mileage/daily?keyword=川AHTWO1&limit=10", wantVIN: "LNXNEGRR7SR318212"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, tc.path, nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } var body struct { Data struct { Items []struct { VIN string `json:"vin"` } `json:"items"` } `json:"data"` } if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { t.Fatalf("response JSON should decode: %v body=%s", err, rec.Body.String()) } if len(body.Data.Items) == 0 { t.Fatalf("vehicle data API should return rows for resolved keyword: %s", rec.Body.String()) } if body.Data.Items[0].VIN != tc.wantVIN { t.Fatalf("vehicle data API should resolve keyword to VIN %s, got %s body=%s", tc.wantVIN, body.Data.Items[0].VIN, rec.Body.String()) } }) } }