-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_final_usage_zero_test.go
More file actions
87 lines (78 loc) · 2.82 KB
/
service_final_usage_zero_test.go
File metadata and controls
87 lines (78 loc) · 2.82 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package fizeau_test
import (
"encoding/json"
"strings"
"testing"
fizeau "github.com/easel/fizeau"
)
// TestServiceFinalUsage_DistinguishesZeroFromUnknown asserts that the public
// ServiceFinalUsage payload distinguishes "harness emitted explicit zero"
// from "harness did not emit this dimension", in both Go-struct shape and
// JSON round-trip. CONTRACT-003 forbids harness emitters from collapsing
// these two states.
func TestServiceFinalUsage_DistinguishesZeroFromUnknown(t *testing.T) {
zero := 0
knownZero := fizeau.ServiceFinalUsage{
InputTokens: &zero,
OutputTokens: &zero,
TotalTokens: &zero,
Source: "native_stream",
}
unknown := fizeau.ServiceFinalUsage{
Source: "native_stream",
}
// In-memory representations are distinguishable.
if knownZero.InputTokens == nil {
t.Fatal("knownZero.InputTokens should be non-nil pointer to 0")
}
if *knownZero.InputTokens != 0 {
t.Fatalf("knownZero.InputTokens should be 0, got %d", *knownZero.InputTokens)
}
if unknown.InputTokens != nil {
t.Fatal("unknown.InputTokens should be nil pointer (dimension not emitted)")
}
// JSON round-trip preserves the distinction:
// nil pointer -> field omitted (omitempty)
// non-nil pointer to 0 -> field present with value 0
knownRaw, err := json.Marshal(knownZero)
if err != nil {
t.Fatalf("marshal knownZero: %v", err)
}
unknownRaw, err := json.Marshal(unknown)
if err != nil {
t.Fatalf("marshal unknown: %v", err)
}
if !strings.Contains(string(knownRaw), `"input_tokens":0`) {
t.Fatalf("explicit zero must serialize as input_tokens:0, got %s", knownRaw)
}
if strings.Contains(string(unknownRaw), `"input_tokens"`) {
t.Fatalf("unknown dimension must be omitted from JSON, got %s", unknownRaw)
}
// Unmarshalling preserves provenance: present-with-zero round-trips to
// non-nil *int(0); absent round-trips to nil.
var knownBack fizeau.ServiceFinalUsage
if err := json.Unmarshal(knownRaw, &knownBack); err != nil {
t.Fatalf("unmarshal knownRaw: %v", err)
}
if knownBack.InputTokens == nil {
t.Fatal("round-tripped knownZero.InputTokens should be non-nil pointer to 0")
}
if *knownBack.InputTokens != 0 {
t.Fatalf("round-tripped value should be 0, got %d", *knownBack.InputTokens)
}
var unknownBack fizeau.ServiceFinalUsage
if err := json.Unmarshal(unknownRaw, &unknownBack); err != nil {
t.Fatalf("unmarshal unknownRaw: %v", err)
}
if unknownBack.InputTokens != nil {
t.Fatalf("round-tripped unknown.InputTokens should be nil, got %#v", unknownBack.InputTokens)
}
// A consumer that distinguishes via "is the pointer nil" must give
// distinct answers for the two payloads.
if isKnown(knownZero) == isKnown(unknown) {
t.Fatal("ServiceFinalUsage failed to distinguish explicit zero from unknown")
}
}
func isKnown(u fizeau.ServiceFinalUsage) bool {
return u.InputTokens != nil
}