Files
2026-06-22 00:00:25 +01:00

44 lines
1.1 KiB
Go

package cli
import (
"bytes"
"encoding/json"
"testing"
)
func TestSuccessEnvelope(t *testing.T) {
var buf bytes.Buffer
if err := Success(map[string]any{"count": 2}).Write(&buf); err != nil {
t.Fatalf("write: %v", err)
}
var got map[string]any
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if got["error"] != false {
t.Fatalf("error should be false: %v", got["error"])
}
if _, ok := got["data"]; !ok {
t.Fatal("data key missing")
}
if ed, ok := got["error_detail"].(map[string]any); !ok || len(ed) != 0 {
t.Fatalf("error_detail should be empty object: %v", got["error_detail"])
}
}
func TestFailureEnvelope(t *testing.T) {
var buf bytes.Buffer
_ = Failure(CodeNotFound, "uid 7 not found").Write(&buf)
var got struct {
Error bool `json:"error"`
ErrorDetail struct {
Code string `json:"code"`
Message string `json:"message"`
} `json:"error_detail"`
}
_ = json.Unmarshal(buf.Bytes(), &got)
if !got.Error || got.ErrorDetail.Code != "not_found" {
t.Fatalf("bad failure envelope: %+v", got)
}
}