| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "sort" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // relayedFixture is a relayed call as the app writes it (§15.29), |
| 13 | // generated by the server's own logQuality and logAuto and given readable |
| 14 | // tags: a Mac app (mmmmmm) sharing its screen to a Safari guest (ssssaf) |
| 15 | // whose network found no public address, so Safari's own leg went through |
| 16 | // the relay in Dublin over TLS; Safari froze and sent an automatic report. |
| 17 | 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}}} |
| 18 | {"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"} |
| 19 | {"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} |
| 20 | {"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"} |
| 21 | {"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}}} |
| 22 | {"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} |
| 23 | ` |
| 24 | |
| 25 | // carlosPrint is a JSON line as `carlos logs` prints it: its prefix, the |
| 26 | // message, then every top-level scalar field but level as key=value in |
| 27 | // key order, and nothing nested. |
| 28 | func carlosPrint(t *testing.T, jsonl string) string { |
| 29 | t.Helper() |
| 30 | var b strings.Builder |
| 31 | for _, l := range strings.Split(strings.TrimSpace(jsonl), "\n") { |
| 32 | var m map[string]any |
| 33 | if err := json.Unmarshal([]byte(l), &m); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | keys := make([]string, 0, len(m)) |
| 37 | for k := range m { |
| 38 | keys = append(keys, k) |
| 39 | } |
| 40 | sort.Strings(keys) |
| 41 | ts := m["time"].(string) |
| 42 | fmt.Fprintf(&b, "%s %s [app] calls.bds.oncarlos.com %s", ts[:10], ts[11:19], m["msg"]) |
| 43 | for _, k := range keys { |
| 44 | switch v := m[k].(type) { |
| 45 | case string: |
| 46 | if k != "msg" && k != "level" { |
| 47 | fmt.Fprintf(&b, " %s=%s", k, v) |
| 48 | } |
| 49 | case float64: |
| 50 | fmt.Fprintf(&b, " %s=%s", k, strconv.FormatFloat(v, 'f', -1, 64)) |
| 51 | case bool: |
| 52 | fmt.Fprintf(&b, " %s=%v", k, v) |
| 53 | } |
| 54 | } |
| 55 | b.WriteString("\n") |
| 56 | } |
| 57 | return b.String() |
| 58 | } |
| 59 | |
| 60 | // The relayed call reads the same from the app's JSON and from carlos's |
| 61 | // print of it, which leaves out `report` and everything nested. |
| 62 | func TestSummaryOfARelayedCall(t *testing.T) { |
| 63 | for name, text := range map[string]string{"json": relayedFixture, "carlos": carlosPrint(t, relayedFixture)} { |
| 64 | out := runOnText(t, text, "--call", "relayedcall2f7ab") |
| 65 | t.Logf("%s:\n%s", name, out) |
| 66 | for _, want := range []string{ |
| 67 | "2 people, 0 problem report(s), 1 automatic report(s)", |
| 68 | " Paths (each side's own account)", |
| 69 | "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", |
| 70 | "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", |
| 71 | "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", |
| 72 | "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", |
| 73 | " Why relayed", |
| 74 | "mmmmmm – ssssaf ssssaf found no public address (no srflx candidate: UDP to the outside blocked); → ssssaf's network forced the relay", |
| 75 | " Media (sender → receiver)", |
| 76 | "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", |
| 77 | "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", |
| 78 | "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", |
| 79 | "ssssaf sent automatic report 9P3J52YS (freezes)", |
| 80 | "Automatic report 9P3J52YS at 14:02:41 from ssssaf (web), trigger freezes", |
| 81 | } { |
| 82 | if !strings.Contains(out, want) { |
| 83 | t.Errorf("%s: missing %q", name, want) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestAnAutomaticReportByItsReference(t *testing.T) { |
| 90 | for name, text := range map[string]string{"json": relayedFixture, "carlos": carlosPrint(t, relayedFixture)} { |
| 91 | out := runOnText(t, text, "--problem", "9p3j52ys") |
| 92 | for _, want := range []string{"Automatic report 9P3J52YS", "trigger: freezes (sent by the client itself; nobody typed a note)", "Calls web diagnostics\nfirst part"} { |
| 93 | if !strings.Contains(out, want) { |
| 94 | t.Errorf("%s: --problem missing %q:\n%s", name, want, out) |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | if got := strings.Join(carlosArgs(options{app: "calls", account: "oficina", since: "2h"}), " "); !strings.Contains(got, "auto report") { |
| 99 | t.Errorf("the summary's carlos logs leaves out the automatic reports: %q", got) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // A pair where neither side sent peer lines (clients from before §15.29) |
| 104 | // prints no Paths section, and the old summary is unchanged. |
| 105 | func TestNoPeerLinesNoPaths(t *testing.T) { |
| 106 | out := runOnText(t, carlosFixture, "--call", "ayhk4nffb2gz7eur") |
| 107 | if strings.Contains(out, "Paths") || strings.Contains(out, "Why relayed") { |
| 108 | t.Errorf("paths from no peer lines:\n%s", out) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestWhyRelayed(t *testing.T) { |
| 113 | measured := func(host, srflx, relay float64, local int, direct bool) *side { |
| 114 | return &side{measured: true, host: host, srflx: srflx, relay: relay, localRelay: local, directOK: direct, localDirectOK: srflx > 0, udpOK: true, relayWindows: 1} |
| 115 | } |
| 116 | for _, c := range []struct { |
| 117 | name string |
| 118 | a, b *side |
| 119 | want string |
| 120 | }{ |
| 121 | {"b has no srflx", measured(2, 1, 1, 0, false), measured(2, 0, 1, 1, false), "→ b's network forced the relay"}, |
| 122 | {"a relay-only", measured(0, 0, 1, 1, false), measured(2, 1, 1, 0, false), "a gathered only relay candidates"}, |
| 123 | {"both without", measured(1, 0, 1, 1, false), measured(1, 0, 1, 1, false), "→ both networks forced it"}, |
| 124 | {"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"}, |
| 125 | {"both found, both legs", measured(2, 1, 1, 3, false), measured(2, 1, 1, 3, false), "no direct pair worked: a NAT or firewall"}, |
| 126 | {"b an old client", measured(2, 0, 1, 1, false), nil, "→ a's network forced the relay"}, |
| 127 | {"neither reported", &side{relayWindows: 1}, nil, "→ unknown"}, |
| 128 | } { |
| 129 | if got := whyRelayed("a", "b", c.a, c.b); !strings.Contains(got, c.want) { |
| 130 | t.Errorf("%s: %q, want %q in it", c.name, got, c.want) |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Spec §15.30: the levels from the peer lines, both sides' accounts, |
| 136 | // whether read as JSON or as carlos prints them. |
| 137 | func TestVideoLevelsFromThePeerLines(t *testing.T) { |
| 138 | fixture := strings.Replace(relayedFixture, `"autoRestarts":2}`, `"autoRestarts":2,"askedMinimal":240,"askedAudio":30}`, 1) |
| 139 | fixture = strings.Replace(fixture, `"inCameraCodec":"h264"}`, `"inCameraCodec":"h264","sentMinimal":238}`, 1) |
| 140 | for name, text := range map[string]string{"json": fixture, "carlos": carlosPrint(t, fixture)} { |
| 141 | out := runOnText(t, text, "--call", "relayedcall2f7ab") |
| 142 | for _, want := range []string{ |
| 143 | " Video levels", |
| 144 | "ssssaf asked mmmmmm for Minimal for 4m0s, Audio only for 30s", |
| 145 | "mmmmmm sent ssssaf Minimal for 3m58s because they asked", |
| 146 | } { |
| 147 | if !strings.Contains(out, want) { |
| 148 | t.Errorf("%s: missing %q in\n%s", name, want, out) |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | if out := runOnText(t, relayedFixture, "--call", "relayedcall2f7ab"); strings.Contains(out, "Video levels") { |
| 153 | t.Error("a call at normal throughout has a Video levels section") |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Spec §15.30 (2026-10-29): the machine's own load, per person, with the |
| 158 | // worst window, read as JSON and as carlos prints it. |
| 159 | func TestTheMachineLine(t *testing.T) { |
| 160 | fixture := strings.Replace(relayedFixture, `"remotes":1,"relay":true,"maxLossPct":0.7`, |
| 161 | `"remotes":1,"relay":true,"maxLossPct":0.7,"procCpuPct":182.5,"procMemMB":412,"sysLoad1":6.2,"thermal":"serious","lowPower":false`, 1) |
| 162 | for name, text := range map[string]string{"json": fixture, "carlos": carlosPrint(t, fixture)} { |
| 163 | out := runOnText(t, text, "--call", "relayedcall2f7ab") |
| 164 | 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" |
| 165 | if !strings.Contains(out, want) { |
| 166 | t.Errorf("%s: missing %q in\n%s", name, want, out) |
| 167 | } |
| 168 | if strings.Count(out, "machine:") != 1 { |
| 169 | t.Errorf("%s: a person who reported nothing of their machine has a machine line:\n%s", name, out) |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |