feat(cli): JSON output envelope with stable error codes

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-22 00:00:25 +01:00
parent 47f877ad82
commit 05abcf3bac
2 changed files with 92 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
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)
}
}