b67b37dc59
Drop zip/JSON from the root API. dump remains an adapter for the Android app format; inventory and an AMS wand both call Parse(uid, image).
97 lines
2.3 KiB
Go
97 lines
2.3 KiB
Go
package bambunfc
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func loadRaw(t *testing.T, uidHex string) (uid, image []byte) {
|
|
t.Helper()
|
|
var err error
|
|
uid, err = hex.DecodeString(uidHex)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
image, err = os.ReadFile(filepath.Join("testdata", "dumps", uidHex+".bin"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return uid, image
|
|
}
|
|
|
|
func parseFile(t *testing.T, uidHex string) Tag {
|
|
t.Helper()
|
|
uid, image := loadRaw(t, uidHex)
|
|
tag, err := Parse(uid, image)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return tag
|
|
}
|
|
|
|
func TestParseRawFixtures(t *testing.T) {
|
|
t.Parallel()
|
|
uids := []string{
|
|
"e27be276", "52d60177", "92a80577", "b2d32177", "92b6fb31", "f28c58ed",
|
|
}
|
|
var translucent, basic int
|
|
for _, id := range uids {
|
|
tag := parseFile(t, id)
|
|
if tag.Brand != BrandBambuLab {
|
|
t.Errorf("chip %s brand %q", tag.ChipUID, tag.Brand)
|
|
}
|
|
if tag.ChipUID != id {
|
|
t.Errorf("chip UID %q; want %q", tag.ChipUID, id)
|
|
}
|
|
if tag.DiameterMM != 1.75 {
|
|
t.Errorf("chip %s diameter %v", tag.ChipUID, tag.DiameterMM)
|
|
}
|
|
switch tag.Type {
|
|
case "PETG Translucent":
|
|
translucent++
|
|
if tag.Color.Hex() != "#61B0FF80" {
|
|
t.Errorf("translucent color %s", tag.Color.Hex())
|
|
}
|
|
case "PETG Basic":
|
|
basic++
|
|
if tag.Color.Hex() != "#000000FF" {
|
|
t.Errorf("basic color %s", tag.Color.Hex())
|
|
}
|
|
default:
|
|
t.Errorf("unexpected type %q", tag.Type)
|
|
}
|
|
}
|
|
if translucent != 4 || basic != 2 {
|
|
t.Errorf("translucent=%d basic=%d", translucent, basic)
|
|
}
|
|
|
|
a := parseFile(t, "e27be276")
|
|
if a.TrayUID != "2fb8e0e972084e74bf737393b28e7b12" {
|
|
t.Errorf("TrayUID = %s", a.TrayUID)
|
|
}
|
|
}
|
|
|
|
func TestParseErrors(t *testing.T) {
|
|
t.Parallel()
|
|
uid, image := loadRaw(t, "e27be276")
|
|
if _, err := Parse(uid, image[:1023]); !errors.Is(err, ErrImageSize) {
|
|
t.Errorf("short image err = %v; want ErrImageSize", err)
|
|
}
|
|
blank := make([]byte, 1024)
|
|
if _, err := Parse(uid, blank); !errors.Is(err, ErrNotBambu) {
|
|
t.Errorf("blank image err = %v; want ErrNotBambu", err)
|
|
}
|
|
if _, err := Parse(nil, image); !errors.Is(err, ErrUID) {
|
|
t.Errorf("empty uid err = %v; want ErrUID", err)
|
|
}
|
|
|
|
noTray := append([]byte(nil), image...)
|
|
copy(noTray[9*16:10*16], make([]byte, 16))
|
|
if _, err := Parse(uid, noTray); !errors.Is(err, ErrIncomplete) {
|
|
t.Errorf("zero tray err = %v; want ErrIncomplete", err)
|
|
}
|
|
}
|