| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "reflect" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | func TestStepHostedDeviceViewExposesOnlyFailedNames(t *testing.T) { |
| 12 | stored := `{"repo":"oficina/equipo","branch":"fix","sha":"candidate","state":"failure","reported_at":123,"failed":["check shard 5 of 23","format"],"url":"https://private.invalid","token":"not-for-devices"}` |
| 13 | for _, tc := range []struct { |
| 14 | name, gate, stored string |
| 15 | present bool |
| 16 | }{ |
| 17 | {"hosted", wire.GateHosted, stored, true}, |
| 18 | {"local", ".amadan/ci.d", stored, false}, |
| 19 | {"missing", wire.GateHosted, "", false}, |
| 20 | {"malformed", wire.GateHosted, "{", false}, |
| 21 | } { |
| 22 | t.Run(tc.name, func(t *testing.T) { |
| 23 | view := stepView(stepRecord{ID: "s", Gate: tc.gate, HostedCI: tc.stored}) |
| 24 | raw, err := json.Marshal(view) |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | var body map[string]any |
| 29 | if err := json.Unmarshal(raw, &body); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | hosted, exists := body["hosted"] |
| 33 | if exists != tc.present { |
| 34 | t.Fatalf("hosted present = %v, want %v", exists, tc.present) |
| 35 | } |
| 36 | if tc.present { |
| 37 | want := map[string]any{"failed": []any{"check shard 5 of 23", "format"}} |
| 38 | if !reflect.DeepEqual(hosted, want) { |
| 39 | t.Fatalf("hosted = %#v; want only %#v", hosted, want) |
| 40 | } |
| 41 | } |
| 42 | }) |
| 43 | } |
| 44 | } |
| 45 | |