|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { HealthSummary } from "../gateway/health/types.js"; |
| 3 | +import { formatHealthChannelLines } from "./health-format.js"; |
| 4 | + |
| 5 | +const createHealthSummary = ( |
| 6 | + params: Pick<HealthSummary, "channels" | "channelOrder" | "channelLabels">, |
| 7 | +): HealthSummary => ({ |
| 8 | + ok: true, |
| 9 | + ts: 0, |
| 10 | + durationMs: 0, |
| 11 | + heartbeatSeconds: 60, |
| 12 | + defaultAgentId: "main", |
| 13 | + agents: [], |
| 14 | + sessions: { path: "/tmp/sessions.json", count: 0, recent: [] }, |
| 15 | + ...params, |
| 16 | +}); |
| 17 | + |
| 18 | +describe("formatHealthChannelLines", () => { |
| 19 | + it("formats per-account probe timings", () => { |
| 20 | + const summary = createHealthSummary({ |
| 21 | + channels: { |
| 22 | + telegram: { |
| 23 | + accountId: "main", |
| 24 | + configured: true, |
| 25 | + probe: { ok: true, elapsedMs: 196, bot: { username: "pinguini_ugi_bot" } }, |
| 26 | + accounts: { |
| 27 | + main: { |
| 28 | + accountId: "main", |
| 29 | + configured: true, |
| 30 | + probe: { ok: true, elapsedMs: 196, bot: { username: "pinguini_ugi_bot" } }, |
| 31 | + }, |
| 32 | + flurry: { |
| 33 | + accountId: "flurry", |
| 34 | + configured: true, |
| 35 | + probe: { ok: true, elapsedMs: 190, bot: { username: "flurry_ugi_bot" } }, |
| 36 | + }, |
| 37 | + poe: { |
| 38 | + accountId: "poe", |
| 39 | + configured: true, |
| 40 | + probe: { ok: true, elapsedMs: 188, bot: { username: "poe_ugi_bot" } }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + channelOrder: ["telegram"], |
| 46 | + channelLabels: { telegram: "Telegram" }, |
| 47 | + }); |
| 48 | + |
| 49 | + expect(formatHealthChannelLines(summary, { accountMode: "all" })).toStrictEqual([ |
| 50 | + "Telegram: ok (@pinguini_ugi_bot:main:196ms, @flurry_ugi_bot:flurry:190ms, @poe_ugi_bot:poe:188ms)", |
| 51 | + ]); |
| 52 | + }); |
| 53 | + |
| 54 | + it("formats statusState without inferring from linked", () => { |
| 55 | + const summary = createHealthSummary({ |
| 56 | + channels: { |
| 57 | + whatsapp: { |
| 58 | + accountId: "default", |
| 59 | + statusState: "unstable", |
| 60 | + configured: true, |
| 61 | + }, |
| 62 | + }, |
| 63 | + channelOrder: ["whatsapp"], |
| 64 | + channelLabels: { whatsapp: "WhatsApp" }, |
| 65 | + }); |
| 66 | + |
| 67 | + expect(formatHealthChannelLines(summary)).toStrictEqual(["WhatsApp: auth stabilizing"]); |
| 68 | + }); |
| 69 | + |
| 70 | + it.each([ |
| 71 | + [ |
| 72 | + "fresh probe failure over passive healthy state", |
| 73 | + { healthState: "healthy", probe: { ok: false, error: "sync rejected" } }, |
| 74 | + "failed (unknown) - sync rejected", |
| 75 | + ], |
| 76 | + [ |
| 77 | + "fresh probe success over passive healthy state", |
| 78 | + { healthState: "healthy", probe: { ok: true, elapsedMs: 12 } }, |
| 79 | + "ok (12ms)", |
| 80 | + ], |
| 81 | + [ |
| 82 | + "blocked lifecycle over a failed probe", |
| 83 | + { linked: true, healthState: "blocked", probe: { ok: false, error: "sync rejected" } }, |
| 84 | + "blocked", |
| 85 | + ], |
| 86 | + [ |
| 87 | + "unknown degraded lifecycle over a successful probe", |
| 88 | + { healthState: "degraded", probe: { ok: true, elapsedMs: 12 } }, |
| 89 | + "degraded", |
| 90 | + ], |
| 91 | + [ |
| 92 | + "unstable authentication over passive healthy state and a failed probe", |
| 93 | + { |
| 94 | + healthState: "healthy", |
| 95 | + statusState: "unstable", |
| 96 | + probe: { ok: false, error: "sync rejected" }, |
| 97 | + }, |
| 98 | + "auth stabilizing", |
| 99 | + ], |
| 100 | + [ |
| 101 | + "disabled status over stale passive healthy state", |
| 102 | + { healthState: "healthy", statusState: "disabled" }, |
| 103 | + "disabled", |
| 104 | + ], |
| 105 | + [ |
| 106 | + "unconfigured account over stale lifecycle and authentication state", |
| 107 | + { configured: false, healthState: "blocked", statusState: "unstable" }, |
| 108 | + "not configured", |
| 109 | + ], |
| 110 | + [ |
| 111 | + "fresh probe failure over configured status", |
| 112 | + { statusState: "configured", probe: { ok: false, error: "credentials rejected" } }, |
| 113 | + "failed (unknown) - credentials rejected", |
| 114 | + ], |
| 115 | + [ |
| 116 | + "fresh probe failure over affirmative linked state", |
| 117 | + { linked: true, probe: { ok: false, error: "session rejected" } }, |
| 118 | + "failed (unknown) - session rejected", |
| 119 | + ], |
| 120 | + [ |
| 121 | + "negative linked state over a failed probe", |
| 122 | + { linked: false, probe: { ok: false, error: "session rejected" } }, |
| 123 | + "not linked", |
| 124 | + ], |
| 125 | + ["passive healthy state without a probe", { healthState: "healthy" }, "healthy"], |
| 126 | + ])("formats %s", (_name, account, expected) => { |
| 127 | + const summary = createHealthSummary({ |
| 128 | + channels: { |
| 129 | + test: { |
| 130 | + accountId: "default", |
| 131 | + configured: true, |
| 132 | + ...account, |
| 133 | + }, |
| 134 | + }, |
| 135 | + channelOrder: ["test"], |
| 136 | + channelLabels: { test: "Test" }, |
| 137 | + }); |
| 138 | + |
| 139 | + expect(formatHealthChannelLines(summary)).toStrictEqual([`Test: ${expected}`]); |
| 140 | + }); |
| 141 | + |
| 142 | + it("surfaces a failed sibling probe over the selected account's passive healthy state", () => { |
| 143 | + const summary = createHealthSummary({ |
| 144 | + channels: { |
| 145 | + matrix: { |
| 146 | + accountId: "main", |
| 147 | + configured: true, |
| 148 | + healthState: "healthy", |
| 149 | + probe: { ok: true, elapsedMs: 12 }, |
| 150 | + accounts: { |
| 151 | + main: { |
| 152 | + accountId: "main", |
| 153 | + configured: true, |
| 154 | + healthState: "healthy", |
| 155 | + probe: { ok: true, elapsedMs: 12 }, |
| 156 | + }, |
| 157 | + alerts: { |
| 158 | + accountId: "alerts", |
| 159 | + configured: true, |
| 160 | + healthState: "healthy", |
| 161 | + probe: { ok: false, error: "sync rejected" }, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + channelOrder: ["matrix"], |
| 167 | + channelLabels: { matrix: "Matrix" }, |
| 168 | + }); |
| 169 | + |
| 170 | + expect(formatHealthChannelLines(summary, { accountMode: "all" })).toStrictEqual([ |
| 171 | + "Matrix: failed (unknown) - sync rejected", |
| 172 | + ]); |
| 173 | + }); |
| 174 | + |
| 175 | + it("formats iMessage probe failures as failed health lines", () => { |
| 176 | + const summary = createHealthSummary({ |
| 177 | + channels: { |
| 178 | + imessage: { |
| 179 | + accountId: "default", |
| 180 | + configured: true, |
| 181 | + probe: { |
| 182 | + ok: false, |
| 183 | + error: |
| 184 | + "imsg cannot access ~/Library/Messages/chat.db. Grant Full Disk Access to the Gateway/launcher process and restart Gateway.", |
| 185 | + }, |
| 186 | + }, |
| 187 | + }, |
| 188 | + channelOrder: ["imessage"], |
| 189 | + channelLabels: { imessage: "iMessage" }, |
| 190 | + }); |
| 191 | + |
| 192 | + expect(formatHealthChannelLines(summary)).toContain( |
| 193 | + "iMessage: failed (unknown) - imsg cannot access ~/Library/Messages/chat.db. Grant Full Disk Access to the Gateway/launcher process and restart Gateway.", |
| 194 | + ); |
| 195 | + }); |
| 196 | +}); |
0 commit comments