| 1 | package bot |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // TestCardVectors pins the Go card validator to the shared contract file the |
| 10 | // web (test/cards.test.mjs) and native (CardVectorTests) suites also consume — |
| 11 | // three implementations, one spec. encoding/json marshals maps with sorted |
| 12 | // keys, so marshalled comparison is canonical. |
| 13 | func TestCardVectors(t *testing.T) { |
| 14 | raw, err := os.ReadFile("../../test/event-vectors/cards.json") |
| 15 | if err != nil { |
| 16 | t.Fatalf("read vectors: %v", err) |
| 17 | } |
| 18 | var file struct { |
| 19 | Schema int `json:"schema"` |
| 20 | Vectors []struct { |
| 21 | Name string `json:"name"` |
| 22 | Card any `json:"card"` |
| 23 | Want any `json:"want"` |
| 24 | } `json:"vectors"` |
| 25 | } |
| 26 | if err := json.Unmarshal(raw, &file); err != nil { |
| 27 | t.Fatalf("parse vectors: %v", err) |
| 28 | } |
| 29 | if file.Schema != cardSchema { |
| 30 | t.Fatalf("vector schema %d != cardSchema %d", file.Schema, cardSchema) |
| 31 | } |
| 32 | for _, v := range file.Vectors { |
| 33 | got, err := normalizeCard(v.Card) |
| 34 | if v.Want == nil { |
| 35 | if err == nil { |
| 36 | t.Errorf("%s: want fail-closed, got %v", v.Name, got) |
| 37 | } |
| 38 | continue |
| 39 | } |
| 40 | if err != nil { |
| 41 | t.Errorf("%s: %v", v.Name, err) |
| 42 | continue |
| 43 | } |
| 44 | gotJSON, _ := json.Marshal(got) |
| 45 | wantJSON, _ := json.Marshal(v.Want) |
| 46 | if string(gotJSON) != string(wantJSON) { |
| 47 | t.Errorf("%s:\n got %s\n want %s", v.Name, gotJSON, wantJSON) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |