fix(go): normalize gb32960 realtime vendor fragments

This commit is contained in:
lingniu
2026-07-03 11:20:17 +08:00
parent 8789a92428
commit b948a46e64
3 changed files with 224 additions and 0 deletions

View File

@@ -200,6 +200,77 @@ func TestRepositoryMergesNestedGB32960MotorSlicesBySerialNo(t *testing.T) {
}
}
func TestRepositoryCollapsesGB32960VendorStackFragmentsIntoSingleRealtimeSummary(t *testing.T) {
repo, closeFn := newTestRepository(t)
defer closeFn()
ctx := context.Background()
first := map[string]any{
"engine_work_state": 1,
"stack_water_outlet_temp_c": 65,
"hydrogen_inlet_pressure_kpa": 130,
"cell_count": 432,
"avg_cell_voltage_v": 0.78,
"frame_cell_start": 201,
"frame_cell_count": 200,
"frame_max_cell_voltage_v": 1.0,
"frame_min_cell_voltage_v": 0.78,
}
second := map[string]any{
"engine_work_state": 1,
"stack_water_outlet_temp_c": 63,
"hydrogen_inlet_pressure_kpa": 130,
"cell_count": 432,
"avg_cell_voltage_v": 0.80,
"frame_cell_start": 401,
"frame_cell_count": 32,
"frame_max_cell_voltage_v": 1.0,
"frame_min_cell_voltage_v": 1.0,
}
for i, summary := range []map[string]any{first, second} {
if err := repo.Update(ctx, envelope.FrameEnvelope{
Protocol: envelope.ProtocolGB32960,
VIN: "VIN001",
EventTimeMS: int64(1000 + i),
ReceivedAtMS: int64(1100 + i),
Parsed: map[string]any{
"data_units": []any{
map[string]any{
"type": "0x30",
"name": "gd_fc_stack",
"value": map[string]any{
"stack_count": 1,
"summaries": []any{summary},
},
},
},
},
}); err != nil {
t.Fatalf("Update() error = %v", err)
}
}
realtimeRaw, err := repo.GetRealtimeRaw(ctx, "VIN001", envelope.ProtocolGB32960)
if err != nil {
t.Fatalf("GetRealtimeRaw() error = %v", err)
}
unit := realtimeRaw["data_units"].([]any)[0].(map[string]any)
value := unit["value"].(map[string]any)
summaries := value["summaries"].([]any)
if len(summaries) != 1 {
t.Fatalf("stack summaries should be a current stack state, not appended fragments: %#v", summaries)
}
summary := summaries[0].(map[string]any)
for _, fragmentOnly := range []string{"frame_cell_start", "frame_cell_count", "frame_max_cell_voltage_v", "frame_min_cell_voltage_v"} {
if _, ok := summary[fragmentOnly]; ok {
t.Fatalf("stack summary should not expose fragment-only field %s: %#v", fragmentOnly, summary)
}
}
if summary["stack_water_outlet_temp_c"] != float64(63) {
t.Fatalf("summary should keep latest stack-level values: %#v", summary)
}
}
func TestRepositoryReplacesJT808LocationWhenUpdatingRealtimeRaw(t *testing.T) {
repo, closeFn := newTestRepository(t)
defer closeFn()