package xiaomi import ( "errors" "testing" ) func TestSentinelErrors(t *testing.T) { tests := []struct { name string err error }{ {"ErrNotSupported", ErrNotSupported}, {"ErrDeviceNotFound", ErrDeviceNotFound}, {"ErrHomeNotFound", ErrHomeNotFound}, {"ErrRoomNotFound", ErrRoomNotFound}, {"ErrDeviceOffline", ErrDeviceOffline}, {"ErrNoSPEC", ErrNoSPEC}, {"ErrInvalidProp", ErrInvalidProp}, {"ErrNotInitialized", ErrNotInitialized}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.err == nil { t.Errorf("%s sentinel error is nil", tt.name) } }) } } func TestClientError(t *testing.T) { t.Run("with DID", func(t *testing.T) { ce := &ClientError{Op: "GetProp", DID: "abc123", Err: ErrDeviceOffline} expected := `xiaomi: GetProp device=abc123: xiaomi: device is offline` if ce.Error() != expected { t.Errorf("Error() = %q, want %q", ce.Error(), expected) } }) t.Run("without DID", func(t *testing.T) { ce := &ClientError{Op: "GetHomeList", Err: ErrHomeNotFound} expected := `xiaomi: GetHomeList: xiaomi: home not found` if ce.Error() != expected { t.Errorf("Error() = %q, want %q", ce.Error(), expected) } }) } func TestWrapErr(t *testing.T) { t.Run("wraps error", func(t *testing.T) { err := wrapErr("SetProp", "did1", ErrDeviceOffline) if err == nil { t.Fatal("expected non-nil error") } ce, ok := err.(*ClientError) if !ok { t.Fatalf("expected *ClientError, got %T", err) } if ce.Op != "SetProp" { t.Errorf("Op = %q, want SetProp", ce.Op) } if ce.DID != "did1" { t.Errorf("DID = %q, want did1", ce.DID) } }) t.Run("nil returns nil", func(t *testing.T) { err := wrapErr("op", "did", nil) if err != nil { t.Errorf("expected nil, got %v", err) } }) } func TestClientErrorUnwrap(t *testing.T) { inner := errors.New("inner error") ce := &ClientError{Op: "test", Err: inner} if !errors.Is(ce, inner) { t.Error("errors.Is(ce, inner) should be true") } }