feat(platform): expose missing vehicle source slots

This commit is contained in:
lingniu
2026-07-04 05:21:54 +08:00
parent ff74375c45
commit d51c83db2a
3 changed files with 75 additions and 5 deletions

View File

@@ -201,7 +201,7 @@ func TestHandlerVehicleServiceIncludesVehicleLevelStatus(t *testing.T) {
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 {
if body.Data.ServiceStatus.OnlineSourceCount != 1 || body.Data.ServiceStatus.SourceCount != 3 {
t.Fatalf("vehicle service should expose source counts, got %+v body=%s", body.Data.ServiceStatus, rec.Body.String())
}
}
@@ -227,7 +227,7 @@ func TestHandlerVehicleServiceIncludesUnifiedOverview(t *testing.T) {
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" {
if overview.SourceCount != 3 || overview.OnlineSourceCount != 1 || overview.CoverageStatus != "partial" {
t.Fatalf("overview should summarize source coverage, got %+v", overview)
}
if overview.LastSeen == "" || overview.PrimaryProtocol == "" {
@@ -256,7 +256,7 @@ func TestHandlerVehicleServiceIncludesSourceConsistency(t *testing.T) {
t.Fatalf("vehicle service should include sourceConsistency: %s", rec.Body.String())
}
consistency := body.Data.SourceConsistency
if consistency.SourceCount != 2 || consistency.OnlineSourceCount != 1 || consistency.LocatedSourceCount != 2 {
if consistency.SourceCount != 3 || consistency.OnlineSourceCount != 1 || consistency.LocatedSourceCount != 2 {
t.Fatalf("sourceConsistency should summarize source coverage, got %+v", consistency)
}
if consistency.MileageDeltaKm <= 0 || consistency.SourceTimeDeltaSeconds <= 0 {
@@ -270,6 +270,35 @@ func TestHandlerVehicleServiceIncludesSourceConsistency(t *testing.T) {
}
}
func TestHandlerVehicleServiceExposesMissingSourceSlots(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())
}
byProtocol := map[string]VehicleSourceStatus{}
for _, source := range body.Data.SourceStatus {
byProtocol[source.Protocol] = source
}
for _, protocol := range []string{"GB32960", "JT808", "YUTONG_MQTT"} {
if _, ok := byProtocol[protocol]; !ok {
t.Fatalf("vehicle service should expose expected source slot %s, got %+v", protocol, body.Data.SourceStatus)
}
}
mqtt := byProtocol["YUTONG_MQTT"]
if mqtt.Online || mqtt.HasRealtime || mqtt.HasHistory || mqtt.HasRaw || mqtt.HasMileage || mqtt.LastSeen != "" {
t.Fatalf("missing MQTT source should be an empty coverage slot, got %+v", mqtt)
}
}
func TestHandlerVehicleServiceOverviewEndpoint(t *testing.T) {
handler := NewHandler(NewService(NewMockStore()))
rec := httptest.NewRecorder()

View File

@@ -56,6 +56,8 @@ type Service struct {
runtime RuntimeInfo
}
var canonicalVehicleProtocols = []string{"GB32960", "JT808", "YUTONG_MQTT"}
func NewService(store Store) *Service {
return &Service{store: store}
}
@@ -278,6 +280,7 @@ func (s *Service) VehicleDetail(ctx context.Context, vin string, protocol string
}
sourceStatus := vehicleSourceStatus(vehicles.Items, realtime.Items, history.Items, raw.Items, mileage.Items)
sourceStatus = s.enrichVehicleSourceStatus(ctx, queryVIN, sourceStatus)
sourceStatus = completeVehicleSourceStatus(sourceStatus, protocol)
return VehicleDetail{
VIN: resolvedVIN,
LookupKey: keyword,
@@ -834,6 +837,42 @@ func vehicleSourceStatus(vehicles []VehicleRow, realtime []RealtimeLocationRow,
return out
}
func completeVehicleSourceStatus(statuses []VehicleSourceStatus, protocol string) []VehicleSourceStatus {
protocol = strings.TrimSpace(protocol)
if protocol != "" {
for _, status := range statuses {
if status.Protocol == protocol {
return []VehicleSourceStatus{status}
}
}
return []VehicleSourceStatus{{Protocol: protocol}}
}
byProtocol := map[string]VehicleSourceStatus{}
for _, status := range statuses {
if strings.TrimSpace(status.Protocol) == "" {
continue
}
byProtocol[status.Protocol] = status
}
out := make([]VehicleSourceStatus, 0, len(canonicalVehicleProtocols)+len(statuses))
seen := map[string]struct{}{}
for _, protocol := range canonicalVehicleProtocols {
if status, ok := byProtocol[protocol]; ok {
out = append(out, status)
} else {
out = append(out, VehicleSourceStatus{Protocol: protocol})
}
seen[protocol] = struct{}{}
}
for _, status := range statuses {
if _, ok := seen[status.Protocol]; ok || strings.TrimSpace(status.Protocol) == "" {
continue
}
out = append(out, status)
}
return out
}
func buildVehicleServiceStatus(resolved bool, statuses []VehicleSourceStatus) *VehicleServiceStatus {
if !resolved {
return &VehicleServiceStatus{

View File

@@ -1795,7 +1795,7 @@ test('filters vehicle list by service status', async () => {
});
});
test('switches vehicle detail to a source from the coverage cards', async () => {
test('switches vehicle detail to a source from the source matrix', async () => {
window.history.replaceState(null, '', '/#/detail?keyword=VIN001');
const fetchMock = vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => {
const path = String(input);
@@ -1839,7 +1839,7 @@ test('switches vehicle detail to a source from the coverage cards', async () =>
sourceStatus: [
{ protocol: 'GB32960', online: true, lastSeen: '2026-07-03 20:12:10', hasRealtime: true, hasHistory: true, hasRaw: true, hasMileage: true },
{ protocol: 'JT808', online: true, lastSeen: '2026-07-03 20:12:08', hasRealtime: true, hasHistory: true, hasRaw: true, hasMileage: true },
{ protocol: 'YUTONG_MQTT', online: false, lastSeen: '2026-07-03 20:10:00', hasRealtime: true, hasHistory: false, hasRaw: true, hasMileage: false }
{ protocol: 'YUTONG_MQTT', online: false, lastSeen: '', hasRealtime: false, hasHistory: false, hasRaw: false, hasMileage: false }
],
realtime: [],
history: { items: [], total: 0, limit: 20, offset: 0 },
@@ -1869,6 +1869,8 @@ test('switches vehicle detail to a source from the coverage cards', async () =>
expect(screen.getByText('主来源')).toBeInTheDocument();
expect(screen.getByText('在线证据')).toBeInTheDocument();
expect(screen.getByText('离线证据')).toBeInTheDocument();
expect(screen.getAllByText('YUTONG_MQTT').length).toBeGreaterThan(0);
expect(screen.getAllByText('无上报').length).toBeGreaterThan(0);
fireEvent.click(screen.getByRole('button', { name: '仅看 JT808' }));
await waitFor(() => {