ANKER_MQTT_FRAME_LOG logs every inbound cloud frame with its bytes, decoded or not — the ones this package drops are exactly the ones worth naming, so they are logged before the drop. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
37 lines
1.2 KiB
Go
37 lines
1.2 KiB
Go
package ankersolix
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// The frame nobody can read is the one worth logging, so its bytes have to be in
|
|
// the line — that is the whole of what a capture is.
|
|
func TestFrameLogLineCarriesTheBytes(t *testing.T) {
|
|
data := []byte{0xff, 0x09, 0x01, 0x02}
|
|
line := frameLogLine("EVSN1", "dt/app/A5191/EVSN1/x", data, "", nil, errors.New("bad marker"))
|
|
if !strings.Contains(line, "hex=ff090102") {
|
|
t.Errorf("unreadable frame: %q, want the bytes in it", line)
|
|
}
|
|
if !strings.Contains(line, "unreadable=") || !strings.Contains(line, "EVSN1") {
|
|
t.Errorf("unreadable frame: %q, want the reason and the serial", line)
|
|
}
|
|
|
|
line = frameLogLine("EVSN1", "t", data, "0857", map[string]any{"a": 1}, nil)
|
|
if !strings.Contains(line, "type=0857") || !strings.Contains(line, "fields=1") {
|
|
t.Errorf("readable frame: %q, want its type and field count", line)
|
|
}
|
|
}
|
|
|
|
// One long frame must not fill a log file.
|
|
func TestFrameLogLineCapsTheHex(t *testing.T) {
|
|
line := frameLogLine("EVSN1", "t", make([]byte, 4096), "0410", nil, nil)
|
|
if len(line) > 1400 {
|
|
t.Errorf("line is %d chars, want it capped", len(line))
|
|
}
|
|
if !strings.Contains(line, "...") {
|
|
t.Error("a truncated frame should say so")
|
|
}
|
|
}
|