package wire_test import ( "bytes" "encoding/json" "reflect" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/wire" "amadan.net/oficina/equipo/internal/wire/wiretest" ) // frozen14Headroom is a protocol 14 instance's headroom: no reading time. var frozen14Headroom = map[string]bool{"utilization": true, "resets_at": true, "windows": true} // headrooms are the headroom objects a frame's body carries. func headrooms(t *testing.T, raw []byte) []map[string]json.RawMessage { t.Helper() var v struct { Connections []struct { Headroom map[string]json.RawMessage `json:"headroom"` } `json:"connections"` Headroom map[string]json.RawMessage `json:"headroom"` } if err := json.Unmarshal(raw, &v); err != nil { t.Fatal(err) } out := []map[string]json.RawMessage{} if v.Headroom != nil { out = append(out, v.Headroom) } for _, c := range v.Connections { if c.Headroom != nil { out = append(out, c.Headroom) } } return out } // Protocol 15 (ProtocolReadings) adds a headroom's reading time. Below // it, every headroom a runner sends (hello, sync, a connection report) // decodes strictly against the frozen protocol 14 headroom, and the // caller's own values are untouched; at 15 nothing changes. func TestEncodeBelowProtocol15DropsReadingTimes(t *testing.T) { if wire.ProtocolReadings != wire.Protocol15 || wire.ProtocolCurrent != wire.ProtocolDesign { t.Fatalf("readings %d, current %d", wire.ProtocolReadings, wire.ProtocolCurrent) } at := time.Date(2026, 11, 12, 9, 58, 0, 0, time.UTC) used := 0.42 read := &wire.Headroom{Utilization: &used, Windows: []wire.Window{{Name: "primary", UsedPercent: 42, Minutes: 300}}, CheckedAt: &at} held := []wire.HeldConnection{{LocalID: "codex-1", Person: "p", Provider: "codex", Kind: wire.KindSubscription, State: wire.ConnHealthy, Headroom: read}} hello, _ := wire.NewFrame("h", wire.FrameHello, wire.Hello{Repos: []string{}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: held, Protocol: wire.ProtocolCurrent}) sync, _ := wire.NewFrame("s", wire.FrameSync, wire.Sync{Connections: held}) report, _ := wire.NewFrame("c", wire.FrameConnection, wire.ConnectionReport{LocalID: "codex-1", Person: "p", Provider: "codex", State: wire.ConnHealthy, Headroom: read}) for _, f := range []wire.Frame{hello, sync, report} { if same, err := wire.Encode(f, wire.Protocol15); err != nil || !bytes.Equal(same.Data, f.Data) { t.Fatalf("%s changed at 15: %v", f.Type, err) } old, err := wire.Encode(f, wire.Protocol14) if err != nil { t.Fatalf("%s at 14: %v", f.Type, err) } hs := headrooms(t, old.Data) if len(hs) != 1 { t.Fatalf("%s: %d headrooms in %s", f.Type, len(hs), old.Data) } for k := range hs[0] { if !frozen14Headroom[k] { t.Fatalf("%s carries %q, which a protocol 14 peer refuses: %s", f.Type, k, old.Data) } } if _, _, err := wire.ParseFrame(mustFrame(t, old)); err != nil { t.Fatalf("%s at 14 does not parse: %v", f.Type, err) } } if read.CheckedAt == nil || held[0].Headroom.CheckedAt == nil { t.Fatal("Encode changed the caller's headroom") } } func mustFrame(t *testing.T, f wire.Frame) []byte { t.Helper() raw, err := json.Marshal(f) if err != nil { t.Fatal(err) } return raw } // The runner's first hello is said before the instance's protocol is // known: WithoutReadingTimes drops the times in new values only. func TestWithoutReadingTimes(t *testing.T) { at := time.Date(2026, 11, 12, 9, 58, 0, 0, time.UTC) h := wire.Hello{Connections: []wire.HeldConnection{{LocalID: "a", Headroom: &wire.Headroom{CheckedAt: &at}}, {LocalID: "b"}}} before := h.Connections if !wire.WithoutReadingTimes(&h) { t.Fatal("nothing dropped") } if h.Connections[0].Headroom.CheckedAt != nil || before[0].Headroom.CheckedAt == nil { t.Fatal("the time stayed, or the caller's changed") } if wire.WithoutReadingTimes(&h) { t.Fatal("dropped twice") } } // TestAdviceTypesCarryNoSecrets: the secret-shape guard covers advice, // the usage totals and their fixtures. An advice's key is its kind and // subject, the one listed exception. func TestAdviceTypesCarryNoSecrets(t *testing.T) { var names []string seen := map[reflect.Type]bool{} for _, v := range []any{wire.AdviceList{}, wire.Advice{}, wire.AdviceAction{}, wire.UsageTotals{}, wire.Card{}} { fieldNames(reflect.TypeOf(v), seen, &names) } for _, n := range names { _, field, _ := strings.Cut(n, ".") for _, bad := range secretShaped { if strings.Contains(strings.ToLower(field), bad) && n != "Advice.key" { t.Errorf("%s looks like a secret (%q)", n, bad) } } } for _, name := range []string{"api/advice.json", "api/advice-action.json", "api/card-advice.json", "api/usage-month.json", "frames/hello-readings.json"} { raw := strings.ReplaceAll(strings.ToLower(string(wiretest.Fixture(name))), `"key":`, "") for _, bad := range []string{"token\"", "secret", "credential", "password", "sealed", "_key", "access_token", "refresh"} { if strings.Contains(raw, bad) { t.Errorf("%s contains %q", name, bad) } } } }