-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvelope_test.go
More file actions
72 lines (65 loc) · 1.88 KB
/
Copy pathenvelope_test.go
File metadata and controls
72 lines (65 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package mcp
import (
"context"
"encoding/json"
"errors"
"testing"
)
func TestErrorEnvelopeShape(t *testing.T) {
env := NewErrorEnvelope(CodeInvalidInput, errors.New("kind is required"), "req-abc123")
body, err := json.Marshal(env)
if err != nil {
t.Fatalf("marshal: %v", err)
}
var got map[string]any
if err := json.Unmarshal(body, &got); err != nil {
t.Fatalf("unmarshal: %v", err)
}
for _, key := range []string{"code", "message", "request_id", "error"} {
if _, ok := got[key]; !ok {
t.Fatalf("envelope missing %q: %s", key, body)
}
}
if got["code"] != CodeInvalidInput {
t.Fatalf("code = %v, want %s", got["code"], CodeInvalidInput)
}
if got["message"] != "kind is required" {
t.Fatalf("message = %v, want kind is required", got["message"])
}
if got["error"] != "kind is required" {
t.Fatalf("error legacy mirror = %v, want kind is required", got["error"])
}
}
func TestErrorEnvelopeCodes(t *testing.T) {
for _, c := range []string{
CodeInternalError, CodeInvalidInput, CodeFileReadFailed, CodeSerializationFailed,
} {
if c == "" {
t.Fatalf("empty error code constant")
}
}
}
func TestErrorEnvelopeNilError(t *testing.T) {
env := NewErrorEnvelope(CodeInternalError, nil, "rid-1")
if env.Message != "(no message)" {
t.Fatalf("message = %q, want (no message)", env.Message)
}
if env.Error != "(no message)" {
t.Fatalf("legacy error = %q, want (no message)", env.Error)
}
}
func TestRequestIDRoundTrip(t *testing.T) {
ctx := WithRequestID(context.Background(), "req-xyz")
if got := RequestID(ctx); got != "req-xyz" {
t.Fatalf("RequestID = %q, want req-xyz", got)
}
if got := RequestID(context.Background()); got != "" {
t.Fatalf("empty ctx RequestID = %q, want \"\"", got)
}
}
func TestNewRequestIDUUIDShape(t *testing.T) {
id := NewRequestID()
if len(id) != 36 {
t.Fatalf("NewRequestID returned %q (len=%d), want UUID-shape len 36", id, len(id))
}
}