56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
package envelope
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestFrameEnvelopeVehicleKeyPrefersVIN(t *testing.T) {
|
|
e := FrameEnvelope{Protocol: ProtocolJT808, VIN: "LNBVIN00000000001", Phone: "013307795425"}
|
|
if got := e.VehicleKey(); got != "LNBVIN00000000001" {
|
|
t.Fatalf("VehicleKey() = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestFrameEnvelopeVehicleKeyFallsBackToPhone(t *testing.T) {
|
|
e := FrameEnvelope{Protocol: ProtocolJT808, Phone: "013307795425"}
|
|
if got := e.VehicleKey(); got != "JT808:013307795425" {
|
|
t.Fatalf("VehicleKey() = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestFrameEnvelopeEventIDStable(t *testing.T) {
|
|
e := FrameEnvelope{
|
|
Protocol: ProtocolJT808,
|
|
MessageID: "0x0200",
|
|
Phone: "013307795425",
|
|
Sequence: 1,
|
|
EventTimeMS: 1782745114000,
|
|
ReceivedAtMS: 1782745114999,
|
|
RawHex: "7e02000000ff7e",
|
|
}
|
|
a := e.StableEventID()
|
|
b := e.StableEventID()
|
|
if a == "" || a != b {
|
|
t.Fatalf("event id must be non-empty and stable: %q %q", a, b)
|
|
}
|
|
}
|
|
|
|
func TestFrameEnvelopeMarshalDefaults(t *testing.T) {
|
|
e := FrameEnvelope{Protocol: ProtocolGB32960, VIN: "LNBVIN00000000002", MessageID: "0x02"}
|
|
payload, err := e.MarshalJSONBytes()
|
|
if err != nil {
|
|
t.Fatalf("MarshalJSONBytes() error = %v", err)
|
|
}
|
|
var decoded FrameEnvelope
|
|
if err := json.Unmarshal(payload, &decoded); err != nil {
|
|
t.Fatalf("json.Unmarshal() error = %v", err)
|
|
}
|
|
if decoded.EventID == "" {
|
|
t.Fatal("event id should be filled")
|
|
}
|
|
if decoded.ParseStatus != ParseOK {
|
|
t.Fatalf("parse status = %q", decoded.ParseStatus)
|
|
}
|
|
}
|