package main import ( "encoding/json" "fmt" "sort" "strconv" "strings" "testing" ) // relayedFixture is a relayed call as the app writes it (§15.29), // generated by the server's own logQuality and logAuto and given readable // tags: a Mac app (mmmmmm) sharing its screen to a Safari guest (ssssaf) // whose network found no public address, so Safari's own leg went through // the relay in Dublin over TLS; Safari froze and sent an automatic report. const relayedFixture = `{"time":"2026-10-28T14:02:30.100Z","level":"INFO","msg":"calls: quality","call":"relayedcall2f7ab","peer":"mmmmmmac0x4n13qq","app":"mac","version":"0.4.13","os":"macos","osVersion":"26.1","final":false,"seconds":30,"remotes":1,"relay":true,"maxLossPct":0.7,"maxRtt":1288,"freezes":0,"reconnecting":1,"screenKeyframes":3,"screenRebuilds":1,"report":{"seconds":30,"devices":{"input":"bluetooth","output":"built-in"},"events":{"reconnecting":1,"reconnectingSeconds":4.2}}} {"time":"2026-10-28T14:02:30.101Z","level":"INFO","msg":"calls: quality peer","call":"relayedcall2f7ab","peer":"mmmmmmac0x4n13qq","remote":"ssssafari26x5qqq","pathLocal":"srflx","pathRemote":"relay","pathProtocol":"udp","relayRegion":"eu-west-1","rtt":300,"rttAvg":398,"rttMax":1288,"jitterAvg":12,"jitterMax":40,"candHost":3,"candSrflx":1,"candRelay":2,"candRemoteHost":2,"candRemoteRelay":1,"directOk":false,"localDirectOk":true,"udpOk":true,"outAudioKbps":32,"outAudioLossPct":1.3,"outCameraKbps":380,"outCameraWidth":1280,"outCameraHeight":720,"outCameraFps":12.5,"outCameraLimit":"bandwidth","outCameraLimitedBandwidthSeconds":29,"outCameraEncoder":"hardware","outCameraCodec":"vp8","outScreenKbps":900,"outScreenWidth":1920,"outScreenHeight":1246,"outScreenFps":4,"outScreenLimit":"bandwidth","outScreenLimitedBandwidthSeconds":30,"outScreenEncoder":"hardware","outScreenCodec":"h264","outScreenMode":"balanced","outScreenKeyframes":3,"outScreenRebuilds":1,"inAudioKbps":31,"inAudioLossPct":0.7,"inAudioConcealedPct":0.2,"inAudioJitterBufferMs":90,"inCameraKbps":410,"inCameraWidth":640,"inCameraHeight":360,"inCameraFps":24,"inCameraFreezes":0,"inCameraDecoder":"hardware","inCameraCodec":"h264"} {"time":"2026-10-28T14:02:41.200Z","level":"WARN","msg":"calls: auto report","ref":"9P3J52YS","call":"relayedcall2f7ab","peer":"ssssafari26x5qqq","app":"web","version":"47afcb9","browser":"safari","browserVersion":"26.5","os":"macos","osVersion":"10.15.7","live":true,"trigger":"freezes","parts":1} {"time":"2026-10-28T14:02:41.201Z","level":"WARN","msg":"calls: auto report diagnostics","ref":"9P3J52YS","call":"relayedcall2f7ab","peer":"ssssafari26x5qqq","part":1,"of":1,"text":"Calls web diagnostics\nfirst part"} {"time":"2026-10-28T14:02:41.300Z","level":"INFO","msg":"calls: quality","call":"relayedcall2f7ab","peer":"ssssafari26x5qqq","app":"web","version":"47afcb9","browser":"safari","browserVersion":"26.5","os":"macos","osVersion":"10.15.7","final":false,"seconds":30,"remotes":1,"relay":true,"maxLossPct":1.3,"maxRtt":1312,"freezes":7,"reconnecting":2,"screenStalls":1,"screenStallsNoPackets":1,"autoReport":"9P3J52YS","report":{"seconds":30,"devices":{"input":"bluetooth","output":"built-in"},"events":{"reconnecting":1,"reconnectingSeconds":4.2}}} {"time":"2026-10-28T14:02:41.301Z","level":"INFO","msg":"calls: quality peer","call":"relayedcall2f7ab","peer":"ssssafari26x5qqq","remote":"mmmmmmac0x4n13qq","pathLocal":"relay","pathRemote":"srflx","pathProtocol":"tls","relayRegion":"eu-west-1","rtt":310,"rttAvg":402.5,"rttMax":1312,"jitterAvg":18,"jitterMax":96,"candHost":2,"candRelay":1,"candRemoteHost":1,"candRemoteSrflx":1,"candRemoteRelay":1,"directOk":false,"localDirectOk":false,"udpOk":false,"outAudioKbps":31.5,"outAudioLossPct":1,"outAudioRtt":420,"outCameraKbps":412,"outCameraWidth":640,"outCameraHeight":360,"outCameraFps":24,"outCameraLimit":"bandwidth","outCameraLimitedBandwidthSeconds":27.5,"outCameraLimitedCpuSeconds":0,"outCameraEncoder":"hardware","outCameraCodec":"h264","inAudioKbps":30,"inAudioLossPct":1.3,"inAudioJitter":22,"inAudioConcealedPct":1,"inAudioJitterBufferMs":140,"inCameraKbps":380,"inCameraWidth":1280,"inCameraHeight":720,"inCameraFps":12.5,"inCameraLossPct":1,"inCameraFreezes":4,"inCameraFreezeSeconds":6.1,"inCameraDecoded":375,"inCameraDropped":3,"inCameraDecoder":"hardware","inCameraCodec":"vp8","inScreenKbps":900,"inScreenWidth":1920,"inScreenHeight":1246,"inScreenFps":4,"inScreenFreezes":3,"inScreenFreezeSeconds":9,"inScreenDropped":0,"inScreenDecoder":"software","inScreenCodec":"h264","inScreenStalls":1,"inScreenStallsNoPackets":1,"reconnecting":1,"reconnectingSeconds":6.4,"autoRestarts":2} ` // carlosPrint is a JSON line as `carlos logs` prints it: its prefix, the // message, then every top-level scalar field but level as key=value in // key order, and nothing nested. func carlosPrint(t *testing.T, jsonl string) string { t.Helper() var b strings.Builder for _, l := range strings.Split(strings.TrimSpace(jsonl), "\n") { var m map[string]any if err := json.Unmarshal([]byte(l), &m); err != nil { t.Fatal(err) } keys := make([]string, 0, len(m)) for k := range m { keys = append(keys, k) } sort.Strings(keys) ts := m["time"].(string) fmt.Fprintf(&b, "%s %s [app] calls.bds.oncarlos.com %s", ts[:10], ts[11:19], m["msg"]) for _, k := range keys { switch v := m[k].(type) { case string: if k != "msg" && k != "level" { fmt.Fprintf(&b, " %s=%s", k, v) } case float64: fmt.Fprintf(&b, " %s=%s", k, strconv.FormatFloat(v, 'f', -1, 64)) case bool: fmt.Fprintf(&b, " %s=%v", k, v) } } b.WriteString("\n") } return b.String() } // The relayed call reads the same from the app's JSON and from carlos's // print of it, which leaves out `report` and everything nested. func TestSummaryOfARelayedCall(t *testing.T) { for name, text := range map[string]string{"json": relayedFixture, "carlos": carlosPrint(t, relayedFixture)} { out := runOnText(t, text, "--call", "relayedcall2f7ab") t.Logf("%s:\n%s", name, out) for _, want := range []string{ "2 people, 0 problem report(s), 1 automatic report(s)", " Paths (each side's own account)", "mmmmmm → ssssaf path srflx→relay udp, relay 100% via eu-west-1, RTT avg 398 ms, RTT max 1288 ms, jitter max 40 ms, audio concealed up to 0.2%, jitter buffer up to 90 ms", "mmmmmm's candidates: gathered host 3, srflx 1, relay 2; offered host 2, srflx 0, relay 1; direct pair worked no, own network reached out yes, UDP yes", "ssssaf → mmmmmm path relay→srflx tls, relay 100% via eu-west-1, RTT avg 402 ms, RTT max 1312 ms, jitter max 96 ms, audio concealed up to 1.0%, jitter buffer up to 140 ms", "ssssaf's candidates: gathered host 2, srflx 0, relay 1; offered host 1, srflx 1, relay 1; direct pair worked no, own network reached out no, UDP no", " Why relayed", "mmmmmm – ssssaf ssssaf found no public address (no srflx candidate: UDP to the outside blocked); → ssssaf's network forced the relay", " Media (sender → receiver)", "mmmmmm → ssssaf camera sent 1280×720, 12.5 fps, 380 kbps, limited by bandwidth (29 s; cpu 0 s), encoder hardware, vp8 received 1280×720, 12.5 fps, 380 kbps, freezes 4 (6.1 s), dropped 3, decoder hardware, vp8", "mmmmmm → ssssaf screen sent 1920×1246, 4.0 fps, 900 kbps, limited by bandwidth (30 s; cpu 0 s), mode balanced, encoder hardware, 1 rebuilds/shares again, 0 gave up, h264 received 1920×1246, 4.0 fps, 900 kbps, freezes 3 (9.0 s), stalls 1, decoder software, h264", "ssssaf → mmmmmm camera sent 640×360, 24.0 fps, 412 kbps, limited by bandwidth (28 s; cpu 0 s), encoder hardware, h264 received 640×360, 24.0 fps, 410 kbps, freezes 0 (0.0 s), decoder hardware, h264", "ssssaf sent automatic report 9P3J52YS (freezes)", "Automatic report 9P3J52YS at 14:02:41 from ssssaf (web), trigger freezes", } { if !strings.Contains(out, want) { t.Errorf("%s: missing %q", name, want) } } } } func TestAnAutomaticReportByItsReference(t *testing.T) { for name, text := range map[string]string{"json": relayedFixture, "carlos": carlosPrint(t, relayedFixture)} { out := runOnText(t, text, "--problem", "9p3j52ys") for _, want := range []string{"Automatic report 9P3J52YS", "trigger: freezes (sent by the client itself; nobody typed a note)", "Calls web diagnostics\nfirst part"} { if !strings.Contains(out, want) { t.Errorf("%s: --problem missing %q:\n%s", name, want, out) } } } if got := strings.Join(carlosArgs(options{app: "calls", account: "oficina", since: "2h"}), " "); !strings.Contains(got, "auto report") { t.Errorf("the summary's carlos logs leaves out the automatic reports: %q", got) } } // A pair where neither side sent peer lines (clients from before §15.29) // prints no Paths section, and the old summary is unchanged. func TestNoPeerLinesNoPaths(t *testing.T) { out := runOnText(t, carlosFixture, "--call", "ayhk4nffb2gz7eur") if strings.Contains(out, "Paths") || strings.Contains(out, "Why relayed") { t.Errorf("paths from no peer lines:\n%s", out) } } func TestWhyRelayed(t *testing.T) { measured := func(host, srflx, relay float64, local int, direct bool) *side { return &side{measured: true, host: host, srflx: srflx, relay: relay, localRelay: local, directOK: direct, localDirectOK: srflx > 0, udpOK: true, relayWindows: 1} } for _, c := range []struct { name string a, b *side want string }{ {"b has no srflx", measured(2, 1, 1, 0, false), measured(2, 0, 1, 1, false), "→ b's network forced the relay"}, {"a relay-only", measured(0, 0, 1, 1, false), measured(2, 1, 1, 0, false), "a gathered only relay candidates"}, {"both without", measured(1, 0, 1, 1, false), measured(1, 0, 1, 1, false), "→ both networks forced it"}, {"both found, a's own leg", measured(2, 1, 1, 3, false), measured(2, 1, 1, 0, false), "→ a's own leg went through the relay"}, {"both found, both legs", measured(2, 1, 1, 3, false), measured(2, 1, 1, 3, false), "no direct pair worked: a NAT or firewall"}, {"b an old client", measured(2, 0, 1, 1, false), nil, "→ a's network forced the relay"}, {"neither reported", &side{relayWindows: 1}, nil, "→ unknown"}, } { if got := whyRelayed("a", "b", c.a, c.b); !strings.Contains(got, c.want) { t.Errorf("%s: %q, want %q in it", c.name, got, c.want) } } } // Spec §15.30: the levels from the peer lines, both sides' accounts, // whether read as JSON or as carlos prints them. func TestVideoLevelsFromThePeerLines(t *testing.T) { fixture := strings.Replace(relayedFixture, `"autoRestarts":2}`, `"autoRestarts":2,"askedMinimal":240,"askedAudio":30}`, 1) fixture = strings.Replace(fixture, `"inCameraCodec":"h264"}`, `"inCameraCodec":"h264","sentMinimal":238}`, 1) for name, text := range map[string]string{"json": fixture, "carlos": carlosPrint(t, fixture)} { out := runOnText(t, text, "--call", "relayedcall2f7ab") for _, want := range []string{ " Video levels", "ssssaf asked mmmmmm for Minimal for 4m0s, Audio only for 30s", "mmmmmm sent ssssaf Minimal for 3m58s because they asked", } { if !strings.Contains(out, want) { t.Errorf("%s: missing %q in\n%s", name, want, out) } } } if out := runOnText(t, relayedFixture, "--call", "relayedcall2f7ab"); strings.Contains(out, "Video levels") { t.Error("a call at normal throughout has a Video levels section") } } // Spec §15.30 (2026-10-29): the machine's own load, per person, with the // worst window, read as JSON and as carlos prints it. func TestTheMachineLine(t *testing.T) { fixture := strings.Replace(relayedFixture, `"remotes":1,"relay":true,"maxLossPct":0.7`, `"remotes":1,"relay":true,"maxLossPct":0.7,"procCpuPct":182.5,"procMemMB":412,"sysLoad1":6.2,"thermal":"serious","lowPower":false`, 1) for name, text := range map[string]string{"json": fixture, "carlos": carlosPrint(t, fixture)} { out := runOnText(t, text, "--call", "relayedcall2f7ab") want := "machine: Calls' CPU up to 182.5% (worst window 14:02:30), memory up to 412 MB, load up to 6.2, thermal worst serious (14:02:30), Low Power Mode off" if !strings.Contains(out, want) { t.Errorf("%s: missing %q in\n%s", name, want, out) } if strings.Count(out, "machine:") != 1 { t.Errorf("%s: a person who reported nothing of their machine has a machine line:\n%s", name, out) } } }