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()) } if !strings.Contains(rec.Body.String(), "serviceStatuses") || !strings.Contains(rec.Body.String(), "degraded") { t.Fatalf("response missing vehicle service status distribution: %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()) } var body struct { Data Page[VehicleRow] `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 || body.Data.Items[0].ServiceStatus == nil { t.Fatalf("vehicle row should include canonical vehicle service status: %s", rec.Body.String()) } if body.Data.Items[0].ServiceStatus.Status != "degraded" { t.Fatalf("vehicle row should expose degraded status, got %+v", body.Data.Items[0].ServiceStatus) } } func TestHandlerVehiclesFiltersServiceStatus(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/vehicles?limit=10&serviceStatus=degraded", 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("degraded vehicles should include partially online vehicle: %s", rec.Body.String()) } if strings.Contains(rec.Body.String(), "LMRKH9AC2R1004087") { t.Fatalf("degraded vehicles should exclude healthy 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()) } } var body struct { Data Page[VehicleCoverageRow] `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 || body.Data.Items[0].ServiceStatus == nil { t.Fatalf("coverage row should include canonical vehicle service status: %s", rec.Body.String()) } if body.Data.Items[0].ServiceStatus.Status != "degraded" { t.Fatalf("coverage row should expose degraded status, got %+v", body.Data.Items[0].ServiceStatus) } } func TestHandlerVehicleCoverageFiltersServiceStatus(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/vehicles/coverage?limit=10&serviceStatus=degraded", 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("degraded coverage should include partially online multi-source vehicle: %s", rec.Body.String()) } if strings.Contains(rec.Body.String(), "LMRKH9AC2R1004087") { t.Fatalf("degraded coverage should exclude healthy single-source vehicle: %s", 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 TestHandlerVehicleServiceCanonicalEndpoint(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/vehicle-service?keyword=粤AG18312&protocol=JT808", 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 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.Resolution == nil || body.Data.Resolution.VIN != "LB9A32A24R0LS1426" { t.Fatalf("vehicle service should resolve canonical vehicle identity, got %+v body=%s", body.Data.Resolution, rec.Body.String()) } for _, row := range body.Data.Realtime { if row.Protocol != "JT808" { t.Fatalf("vehicle service should keep source filter, got realtime row %+v body=%s", row, rec.Body.String()) } } } func TestHandlerVehicleServiceIncludesVehicleLevelStatus(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/vehicle-service?keyword=粤AG18312", 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 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.ServiceStatus == nil { t.Fatalf("vehicle service should include serviceStatus: %s", rec.Body.String()) } if body.Data.ServiceStatus.Status != "degraded" || body.Data.ServiceStatus.Title != "部分来源离线" { t.Fatalf("vehicle service should summarize partial source health, got %+v body=%s", body.Data.ServiceStatus, rec.Body.String()) } if body.Data.ServiceStatus.OnlineSourceCount != 1 || body.Data.ServiceStatus.SourceCount != 2 { t.Fatalf("vehicle service should expose source counts, got %+v body=%s", body.Data.ServiceStatus, rec.Body.String()) } } func TestHandlerVehicleServiceIncludesUnifiedOverview(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/vehicle-service?keyword=粤AG18312", 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 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.ServiceOverview == nil { t.Fatalf("vehicle service should include serviceOverview: %s", rec.Body.String()) } overview := body.Data.ServiceOverview if overview.VIN != "LB9A32A24R0LS1426" || overview.Plate != "粤AG18312" { t.Fatalf("overview should expose canonical vehicle identity, got %+v", overview) } if overview.SourceCount != 2 || overview.OnlineSourceCount != 1 || overview.CoverageStatus != "partial" { t.Fatalf("overview should summarize source coverage, got %+v", overview) } if overview.LastSeen == "" || overview.PrimaryProtocol == "" { t.Fatalf("overview should expose latest service time and primary source, got %+v", overview) } if overview.RealtimeCount == 0 || overview.HistoryCount == 0 || overview.RawCount == 0 || overview.MileageCount == 0 { t.Fatalf("overview should expose available data counts, got %+v", overview) } } 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()) } 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.Resolution == nil || !body.Data.Resolution.Resolved || body.Data.Resolution.VIN != "LB9A32A24R0LS1426" { t.Fatalf("vehicle detail should include canonical identity resolution, got %+v body=%s", body.Data.Resolution, rec.Body.String()) } if len(body.Data.Resolution.Protocols) < 2 { t.Fatalf("vehicle detail resolution should include source protocols, got %+v", body.Data.Resolution.Protocols) } } 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 TestHandlerVehicleResolveReturnsCanonicalIdentity(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/vehicles/resolve?keyword=粤AG18312", 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 VehicleIdentityResolution `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.Resolved || body.Data.VIN != "LB9A32A24R0LS1426" || body.Data.Plate != "粤AG18312" { t.Fatalf("resolve should return canonical identity, got %+v body=%s", body.Data, rec.Body.String()) } if len(body.Data.Protocols) < 2 { t.Fatalf("resolve should include available source protocols, got %+v", body.Data.Protocols) } if body.Data.ServiceStatus == nil || body.Data.ServiceStatus.Status != "degraded" { t.Fatalf("resolve should include canonical service status, got %+v body=%s", body.Data.ServiceStatus, rec.Body.String()) } } func TestHandlerVehicleResolveKeepsUnresolvedKeyword(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/vehicles/resolve?keyword=64646848247", 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 VehicleIdentityResolution `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.Resolved || body.Data.VIN != "" || body.Data.LookupKey != "64646848247" { t.Fatalf("unresolved keyword should not fabricate identity, got %+v body=%s", body.Data, rec.Body.String()) } } 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()) } } 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) == 0 || body.Data.Items[0].ServiceStatus == nil { t.Fatalf("realtime vehicle row should include canonical serviceStatus: %s", rec.Body.String()) } } func TestHandlerVehicleRealtimeFiltersServiceStatus(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/realtime/vehicles?serviceStatus=degraded&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("degraded realtime should include partially online vehicle: %s", rec.Body.String()) } if strings.Contains(rec.Body.String(), "LNXNEGRR7SR318212") { t.Fatalf("degraded realtime should exclude healthy single-source vehicle: %s", 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 TestHandlerOpsHealthIncludesVehicleServiceRuntime(t *testing.T) { handler := NewHandler(NewServiceWithRuntime(NewMockStore(), RuntimeInfo{RequestTimeoutMs: 1500})) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/ops/health", 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 OpsHealth `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.Runtime.RequestTimeoutMs != 1500 { t.Fatalf("ops health should include request timeout runtime, got %+v body=%s", body.Data.Runtime, 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()) } }) } } func TestHandlerRawFramesPostAcceptsKeyword(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodPost, "/api/history/raw-frames/query", strings.NewReader(`{"keyword":"川AHTWO1","limit":1}`)) 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 []RawFrameRow `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("raw POST should resolve keyword to VIN, got %+v body=%s", body.Data.Items, rec.Body.String()) } } func TestHandlerRawFramesKeepsUnresolvedKeywordEmpty(t *testing.T) { handler := NewHandler(NewService(NewMockStore())) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/history/raw-frames?keyword=64646848247&limit=1", 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 Page[RawFrameRow] `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.Total != 0 || len(body.Data.Items) != 0 { t.Fatalf("unresolved keyword should not fabricate raw rows: %+v body=%s", body.Data, rec.Body.String()) } }