homelabby/internal/labels/cable_test.go
Mikkel Georgsen 7800917500 feat(04-02): cable label renderer and IsCableDevice detection
- Add CableLabelData struct and RenderCable() producing 384x180 bitmap
- Cable template: HW ID, name, USB version/speed, wattage/test date
- IsCableDevice() detects cable records by name or AINotes heuristic
- T-04-07 mitigation applied: HWID format validated in RenderCable too
- All 5 cable tests pass (10 total in package)
2026-04-10 06:45:48 +00:00

119 lines
2.8 KiB
Go

package labels
import (
"testing"
"git.georgsen.dk/hwlab/internal/netbox"
)
// TestRenderCable_ImageDimensions verifies cable label is 384x180.
func TestRenderCable_ImageDimensions(t *testing.T) {
d := CableLabelData{
HWID: "HW-00042",
Name: "USB-C 2m Cable",
USBVersion: "USB 3.2 Gen 2",
MaxSpeedGbps: 10,
MaxWatts: 100,
TestDate: "2026-04-13",
}
img, err := RenderCable(d)
if err != nil {
t.Fatalf("RenderCable returned error: %v", err)
}
bounds := img.Bounds()
if bounds.Dx() != LabelWidth {
t.Errorf("expected width %d, got %d", LabelWidth, bounds.Dx())
}
if bounds.Dy() != CableLabelHeight {
t.Errorf("expected height %d, got %d", CableLabelHeight, bounds.Dy())
}
}
// TestRenderCable_FullData verifies a fully-populated CableLabelData renders without error.
func TestRenderCable_FullData(t *testing.T) {
d := CableLabelData{
HWID: "HW-00042",
Name: "USB-C 2m Cable",
USBVersion: "USB 3.2 Gen 2",
MaxSpeedGbps: 10,
MaxWatts: 100,
TestDate: "2026-04-13",
}
img, err := RenderCable(d)
if err != nil {
t.Fatalf("RenderCable returned error: %v", err)
}
if img == nil {
t.Fatal("expected non-nil image")
}
}
// TestIsCableDevice_True verifies that a device with "cable" in Name returns true.
func TestIsCableDevice_True(t *testing.T) {
tests := []struct {
name string
device netbox.Device
}{
{
name: "cable in device name",
device: netbox.Device{
Name: "USB-C Cable 2m",
},
},
{
name: "cable in AINotes",
device: netbox.Device{
Name: "HW-00050",
CustomFields: netbox.CustomFields{
AINotes: "This is a USB cable for data transfer",
},
},
},
{
name: "CABLE uppercase",
device: netbox.Device{
Name: "CABLE USB 3.0",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !IsCableDevice(tt.device) {
t.Errorf("expected IsCableDevice to return true for device: %+v", tt.device)
}
})
}
}
// TestIsCableDevice_False verifies that a device with no cable indicators returns false.
func TestIsCableDevice_False(t *testing.T) {
d := netbox.Device{
Name: "Intel NUC i5",
CustomFields: netbox.CustomFields{
AINotes: "Mini PC with 16GB RAM",
},
}
if IsCableDevice(d) {
t.Errorf("expected IsCableDevice to return false for non-cable device: %+v", d)
}
}
// TestRenderCable_EmptyTestDate verifies "Not tested" is used when TestDate is empty.
func TestRenderCable_EmptyTestDate(t *testing.T) {
d := CableLabelData{
HWID: "HW-00042",
Name: "USB-C Cable",
USBVersion: "USB 2.0",
MaxSpeedGbps: 0.48,
MaxWatts: 5,
TestDate: "", // intentionally empty
}
// Must not panic
img, err := RenderCable(d)
if err != nil {
t.Fatalf("RenderCable returned error on empty TestDate: %v", err)
}
if img == nil {
t.Fatal("expected non-nil image, got nil")
}
}