-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathparity_corpus_loader_test.go
More file actions
169 lines (151 loc) · 5.65 KB
/
Copy pathparity_corpus_loader_test.go
File metadata and controls
169 lines (151 loc) · 5.65 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package peac
import (
"encoding/json"
"os"
"path/filepath"
"runtime"
"testing"
)
// parityFamily is the shape of a parity-corpus family file at
// specs/conformance/parity-corpus/<family>/vectors.json.
//
// The Go-side loader does typed JSON unmarshal + explicit required-field
// assertions. Full JSON Schema validation is performed on the TypeScript
// side; the Go SDK has no external dependencies, and adding a Go-side
// schema validator is deferred per the v0.13.1 plan amendment.
type parityFamily struct {
Family string `json:"family"`
Description string `json:"description"`
Version string `json:"version"`
Generator string `json:"generator"`
Vectors []parityVector `json:"vectors"`
}
type parityVector struct {
ID string `json:"id"`
Description string `json:"description"`
Input parityVectorInput `json:"input"`
Expected parityVectorOutput `json:"expected"`
}
type parityVectorInput struct {
Payload map[string]interface{} `json:"payload"`
Header map[string]interface{} `json:"header,omitempty"`
}
type parityVectorOutput struct {
Accepted bool `json:"accepted"`
Errors []parityVectorIssue `json:"errors,omitempty"`
Warnings []parityVectorIssue `json:"warnings,omitempty"`
}
type parityVectorIssue struct {
Code string `json:"code"`
Path string `json:"path,omitempty"`
}
var parityFloorCounts = map[string]int{
"default-flows": 12,
"jose-hardening": 8,
"runtime-governance": 7,
"commerce-bridges": 4,
"provisioning-lifecycle": 29,
}
func parityCorpusRoot(t *testing.T) string {
t.Helper()
_, thisFile, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("runtime.Caller failed")
}
return filepath.Join(filepath.Dir(thisFile), "..", "..", "specs", "conformance", "parity-corpus")
}
// loadParityFamily reads, parses, and applies required-field assertions to
// a single parity-corpus family. Schema validation is on the TS side; the
// Go side enforces the floor count, required envelope fields, vector id
// uniqueness, and that every vector has a non-empty payload.
func loadParityFamily(t *testing.T, family string) parityFamily {
t.Helper()
root := parityCorpusRoot(t)
vectorsPath := filepath.Join(root, family, "vectors.json")
data, err := os.ReadFile(vectorsPath)
if err != nil {
t.Fatalf("parity-corpus(%s): read vectors.json: %v", family, err)
}
var loaded parityFamily
if err := json.Unmarshal(data, &loaded); err != nil {
t.Fatalf("parity-corpus(%s): unmarshal vectors.json: %v", family, err)
}
if loaded.Family != family {
t.Fatalf("parity-corpus(%s): family field = %q, want %q", family, loaded.Family, family)
}
if loaded.Description == "" {
t.Fatalf("parity-corpus(%s): description must be non-empty", family)
}
if loaded.Version == "" {
t.Fatalf("parity-corpus(%s): version must be non-empty", family)
}
floor, ok := parityFloorCounts[family]
if !ok {
t.Fatalf("parity-corpus(%s): unknown family (no floor count)", family)
}
if len(loaded.Vectors) < floor {
t.Fatalf("parity-corpus(%s): vector count %d below floor %d", family, len(loaded.Vectors), floor)
}
seen := make(map[string]struct{}, len(loaded.Vectors))
for i, v := range loaded.Vectors {
if v.ID == "" {
t.Fatalf("parity-corpus(%s): vector index %d has empty id", family, i)
}
if v.Description == "" {
t.Fatalf("parity-corpus(%s): vector %s has empty description", family, v.ID)
}
if _, dup := seen[v.ID]; dup {
t.Fatalf("parity-corpus(%s): duplicate vector id %s", family, v.ID)
}
seen[v.ID] = struct{}{}
if len(v.Input.Payload) == 0 {
t.Fatalf("parity-corpus(%s): vector %s has empty payload", family, v.ID)
}
// jose-hardening requires header; other families allow header to be omitted.
if family == "jose-hardening" && len(v.Input.Header) == 0 {
t.Fatalf("parity-corpus(%s): vector %s requires non-empty header", family, v.ID)
}
}
return loaded
}
// TestParityCorpusLoader smoke-tests the Go-side corpus loader. Asserts that
// every parity-corpus family loads successfully, meets its floor count, and
// has unique vector ids. No validator code is exercised here; this commit
// ships the loader only.
func TestParityCorpusLoader(t *testing.T) {
for _, family := range []string{
"default-flows",
"jose-hardening",
"runtime-governance",
"commerce-bridges",
"provisioning-lifecycle",
} {
t.Run(family, func(tt *testing.T) {
loaded := loadParityFamily(tt, family)
if loaded.Family != family {
tt.Fatalf("family mismatch: got %q want %q", loaded.Family, family)
}
tt.Logf("loaded %d vectors for family %s (floor %d)", len(loaded.Vectors), family, parityFloorCounts[family])
})
}
}
// TestParityCorpusLoaderMissingFamilyRejected verifies that the loader
// fails (via t.Fatal) when asked for an unregistered family. We can't
// directly assert on t.Fatal in-test, so we exercise the path through
// a sub-helper that returns an error instead.
func TestParityCorpusLoaderUnknownFamilyHandled(t *testing.T) {
root := parityCorpusRoot(t)
missing := filepath.Join(root, "nonexistent-family", "vectors.json")
if _, err := os.Stat(missing); !os.IsNotExist(err) {
t.Fatalf("setup: expected nonexistent-family/vectors.json not to exist, got err=%v", err)
}
// Confirm the parity floor map has exactly five entries (the five families).
if got, want := len(parityFloorCounts), 5; got != want {
t.Fatalf("parityFloorCounts has %d entries, want %d", got, want)
}
for _, want := range []string{"default-flows", "jose-hardening", "runtime-governance", "commerce-bridges", "provisioning-lifecycle"} {
if _, ok := parityFloorCounts[want]; !ok {
t.Fatalf("parityFloorCounts missing family %q", want)
}
}
}