oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package wire_test
2
3import (
4 "bytes"
5 "encoding/json"
6 "reflect"
7 "strings"
8 "testing"
9 "time"
10
11 "amadan.net/oficina/equipo/internal/wire"
12 "amadan.net/oficina/equipo/internal/wire/wiretest"
13)
14
15// frozen14Headroom is a protocol 14 instance's headroom: no reading time.
16var frozen14Headroom = map[string]bool{"utilization": true, "resets_at": true, "windows": true}
17
18// headrooms are the headroom objects a frame's body carries.
19func headrooms(t *testing.T, raw []byte) []map[string]json.RawMessage {
20 t.Helper()
21 var v struct {
22 Connections []struct {
23 Headroom map[string]json.RawMessage `json:"headroom"`
24 } `json:"connections"`
25 Headroom map[string]json.RawMessage `json:"headroom"`
26 }
27 if err := json.Unmarshal(raw, &v); err != nil {
28 t.Fatal(err)
29 }
30 out := []map[string]json.RawMessage{}
31 if v.Headroom != nil {
32 out = append(out, v.Headroom)
33 }
34 for _, c := range v.Connections {
35 if c.Headroom != nil {
36 out = append(out, c.Headroom)
37 }
38 }
39 return out
40}
41
42// Protocol 15 (ProtocolReadings) adds a headroom's reading time. Below
43// it, every headroom a runner sends (hello, sync, a connection report)
44// decodes strictly against the frozen protocol 14 headroom, and the
45// caller's own values are untouched; at 15 nothing changes.
46func TestEncodeBelowProtocol15DropsReadingTimes(t *testing.T) {
47 if wire.ProtocolReadings != wire.Protocol15 || wire.ProtocolCurrent != wire.ProtocolDesign {
48 t.Fatalf("readings %d, current %d", wire.ProtocolReadings, wire.ProtocolCurrent)
49 }
50 at := time.Date(2026, 11, 12, 9, 58, 0, 0, time.UTC)
51 used := 0.42
52 read := &wire.Headroom{Utilization: &used, Windows: []wire.Window{{Name: "primary", UsedPercent: 42, Minutes: 300}}, CheckedAt: &at}
53 held := []wire.HeldConnection{{LocalID: "codex-1", Person: "p", Provider: "codex", Kind: wire.KindSubscription, State: wire.ConnHealthy, Headroom: read}}
54 hello, _ := wire.NewFrame("h", wire.FrameHello, wire.Hello{Repos: []string{}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: held, Protocol: wire.ProtocolCurrent})
55 sync, _ := wire.NewFrame("s", wire.FrameSync, wire.Sync{Connections: held})
56 report, _ := wire.NewFrame("c", wire.FrameConnection, wire.ConnectionReport{LocalID: "codex-1", Person: "p", Provider: "codex", State: wire.ConnHealthy, Headroom: read})
57 for _, f := range []wire.Frame{hello, sync, report} {
58 if same, err := wire.Encode(f, wire.Protocol15); err != nil || !bytes.Equal(same.Data, f.Data) {
59 t.Fatalf("%s changed at 15: %v", f.Type, err)
60 }
61 old, err := wire.Encode(f, wire.Protocol14)
62 if err != nil {
63 t.Fatalf("%s at 14: %v", f.Type, err)
64 }
65 hs := headrooms(t, old.Data)
66 if len(hs) != 1 {
67 t.Fatalf("%s: %d headrooms in %s", f.Type, len(hs), old.Data)
68 }
69 for k := range hs[0] {
70 if !frozen14Headroom[k] {
71 t.Fatalf("%s carries %q, which a protocol 14 peer refuses: %s", f.Type, k, old.Data)
72 }
73 }
74 if _, _, err := wire.ParseFrame(mustFrame(t, old)); err != nil {
75 t.Fatalf("%s at 14 does not parse: %v", f.Type, err)
76 }
77 }
78 if read.CheckedAt == nil || held[0].Headroom.CheckedAt == nil {
79 t.Fatal("Encode changed the caller's headroom")
80 }
81}
82
83func mustFrame(t *testing.T, f wire.Frame) []byte {
84 t.Helper()
85 raw, err := json.Marshal(f)
86 if err != nil {
87 t.Fatal(err)
88 }
89 return raw
90}
91
92// The runner's first hello is said before the instance's protocol is
93// known: WithoutReadingTimes drops the times in new values only.
94func TestWithoutReadingTimes(t *testing.T) {
95 at := time.Date(2026, 11, 12, 9, 58, 0, 0, time.UTC)
96 h := wire.Hello{Connections: []wire.HeldConnection{{LocalID: "a", Headroom: &wire.Headroom{CheckedAt: &at}}, {LocalID: "b"}}}
97 before := h.Connections
98 if !wire.WithoutReadingTimes(&h) {
99 t.Fatal("nothing dropped")
100 }
101 if h.Connections[0].Headroom.CheckedAt != nil || before[0].Headroom.CheckedAt == nil {
102 t.Fatal("the time stayed, or the caller's changed")
103 }
104 if wire.WithoutReadingTimes(&h) {
105 t.Fatal("dropped twice")
106 }
107}
108
109// TestAdviceTypesCarryNoSecrets: the secret-shape guard covers advice,
110// the usage totals and their fixtures. An advice's key is its kind and
111// subject, the one listed exception.
112func TestAdviceTypesCarryNoSecrets(t *testing.T) {
113 var names []string
114 seen := map[reflect.Type]bool{}
115 for _, v := range []any{wire.AdviceList{}, wire.Advice{}, wire.AdviceAction{}, wire.UsageTotals{}, wire.Card{}} {
116 fieldNames(reflect.TypeOf(v), seen, &names)
117 }
118 for _, n := range names {
119 _, field, _ := strings.Cut(n, ".")
120 for _, bad := range secretShaped {
121 if strings.Contains(strings.ToLower(field), bad) && n != "Advice.key" {
122 t.Errorf("%s looks like a secret (%q)", n, bad)
123 }
124 }
125 }
126 for _, name := range []string{"api/advice.json", "api/advice-action.json", "api/card-advice.json", "api/usage-month.json", "frames/hello-readings.json"} {
127 raw := strings.ReplaceAll(strings.ToLower(string(wiretest.Fixture(name))), `"key":`, "")
128 for _, bad := range []string{"token\"", "secret", "credential", "password", "sealed", "_key", "access_token", "refresh"} {
129 if strings.Contains(raw, bad) {
130 t.Errorf("%s contains %q", name, bad)
131 }
132 }
133 }
134}
135