From 03208a2524eb1b372f7d9e58707d28a0bbd5db57 Mon Sep 17 00:00:00 2001 From: Muthiah Muralidharan Date: Wed, 18 Nov 2026 22:01:29 +0530 Subject: [PATCH] test(listen): move the listen telemetry scenario to the integration suite The scenario that checks spooled `listen_phase` events across failed attempts, drops and reconnects built a Commander program to install the telemetry hooks. Unit tests use no Commander, so it now lives in `src/tests/integration/listen-telemetry.test.ts` and runs the real `chargebee listen` through `runCli`, with the same fake WebSocket and stubbed flush. The unit file drops the imports and the CI-detection hook that only this scenario needed. Co-Authored-By: Claude Opus 5.5 --- .../integration/listen-telemetry.test.ts | 157 ++++++++++++++++++ src/tests/unit/lib/tunnel/appsync.test.ts | 120 +------------ 2 files changed, 158 insertions(+), 119 deletions(-) create mode 100644 src/tests/integration/listen-telemetry.test.ts diff --git a/src/tests/integration/listen-telemetry.test.ts b/src/tests/integration/listen-telemetry.test.ts new file mode 100644 index 0000000..ff2d87e --- /dev/null +++ b/src/tests/integration/listen-telemetry.test.ts @@ -0,0 +1,157 @@ +/** + * Telemetry lifecycle events spooled by `chargebee listen` across failed + * attempts, drops and reconnects. Runs the real program with an in-process + * fake WebSocket and a stubbed flush — never AppSync, never the network. + */ +import { afterEach, beforeEach, describe, expect, it } from "bun:test"; +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +import { __resetRuntimeState } from "../../lib/api/sdk.js"; +import { __setAppsyncTableForTest } from "../../lib/config/urls.js"; +import { spoolPath } from "../../lib/telemetry/constants.js"; +import { + __resetTelemetryRecorderForTest, + __setSpawnForTest, + installTelemetry, +} from "../../lib/telemetry/index.js"; +import { writeState } from "../../lib/telemetry/state.js"; +import type { SpoolRecord } from "../../lib/telemetry/types.js"; +import { + __setTunnelTimingForTest, + __setWebSocketForTest, +} from "../../lib/tunnel/appsync.js"; +import { __setFetchForTest } from "../../lib/tunnel/forwarder.js"; +import { FakeWebSocket, waitUntil } from "../../lib/test-support/fake-websocket.js"; +import { + clearCiEnv, + createEnvPatcher, + installFakeClient, + runCli, + setStderrIsTTY, + uninstallFakeClient, +} from "../../lib/test-support/_helpers.js"; + +const env = createEnvPatcher(); +let configDir: string; +let restoreCiEnv: () => void = () => undefined; +let restoreStderrTty: (() => void) | undefined; + +beforeEach(() => { + // The recorder is disabled on CI runners; this file reads the spool it writes. + restoreCiEnv = clearCiEnv(); + configDir = mkdtempSync(join(tmpdir(), "cb-listen-telemetry-")); + env.set("CHARGEBEE_CONFIG_DIR", configDir); + env.set("CHARGEBEE_SITE", "acme-test"); + env.set("CHARGEBEE_API_KEY", "test_xxx"); + env.set("CHARGEBEE_HOST", undefined); + env.set("CHARGEBEE_REGION", undefined); + env.set("DO_NOT_TRACK", undefined); + env.set("CHARGEBEE_CLI_TELEMETRY", undefined); + // A later run: the first-run notice has been shown, so events are recorded. + writeState({ notice_shown: true }); + restoreStderrTty = setStderrIsTTY(false); + __resetRuntimeState(); + __resetTelemetryRecorderForTest(); + __setSpawnForTest(() => ({ unref() {} })); + __setAppsyncTableForTest({ + ".chargebee.com": { us: { dnsPrefix: "testapiid", region: "us-east-2" } }, + }); + installFakeClient({ constructions: [] }); + FakeWebSocket.instances = []; + __setWebSocketForTest(FakeWebSocket); + __setTunnelTimingForTest({ backoffMs: 5, maxBackoffMs: 5, closeFlushMs: 0, idleTimeoutMs: 5000 }); + __setFetchForTest(async () => new Response("ok", { status: 200 })); +}); + +afterEach(() => { + __resetTelemetryRecorderForTest(); + __setSpawnForTest(null); + __setAppsyncTableForTest(null); + __setWebSocketForTest(null); + __setTunnelTimingForTest(null); + __setFetchForTest(null); + uninstallFakeClient(); + __resetRuntimeState(); + restoreStderrTty?.(); + restoreStderrTty = undefined; + env.restore(); + restoreCiEnv(); + rmSync(configDir, { recursive: true, force: true }); +}); + +/** The `index`-th socket the run opens, once it has sent its first frame. */ +async function socket(index: number): Promise { + await waitUntil(() => FakeWebSocket.instances.length > index); + const ws = FakeWebSocket.instances[index]!; + await waitUntil(() => ws.sent.length > 0); + return ws; +} + +/** Close the socket before the handshake completes: a failed attempt. */ +async function failAttempt(index: number): Promise { + (await socket(index)).serverClose(); +} + +/** Complete the handshake: a working session. */ +async function subscribe(index: number): Promise { + const ws = await socket(index); + ws.emit({ type: "connection_ack" }); + await waitUntil(() => ws.parsed().some((m) => m.type === "subscribe")); + ws.emit({ type: "subscribe_success" }); + await waitUntil(() => ws.parsed().some((m) => m.type === "publish")); + return ws; +} + +function spooledListenEvents(): SpoolRecord["event"]["metadata"][] { + return readFileSync(spoolPath(), "utf-8") + .trim() + .split("\n") + .map((line) => (JSON.parse(line) as SpoolRecord).event.metadata) + .filter((metadata) => metadata.listen_phase !== undefined); +} + +describe("chargebee listen telemetry", () => { + it("reports failed attempts once per outage, and a recovered drop as reconnected, not an error", async () => { + const pending = runCli(["listen", "--forward-to", "http://localhost:4000/hook"], { + configure: (program) => installTelemetry(program, "0.0.0-test"), + }); + + try { + // Outage 1: three attempts close before the handshake completes. + await failAttempt(0); + await failAttempt(1); + await failAttempt(2); + // Recovery: the first working session of the run. + (await subscribe(3)).serverClose(); + // Outage 2: one attempt after the drop fails, then the next one subscribes. + await failAttempt(4); + // A drop straight back into a working session records only `reconnected`. + (await subscribe(5)).serverClose(); + await subscribe(6); + } finally { + process.emit("SIGINT"); + } + const { exitCode } = await pending; + expect(exitCode).toBe(0); + + const events = spooledListenEvents(); + expect(events.map((m) => m.listen_phase)).toEqual([ + "error", + "established", + "error", + "reconnected", + "reconnected", + "closed", + ]); + expect(events.filter((m) => m.listen_phase === "error").map((m) => m.err_type)).toEqual([ + "listen_connect_error", + "listen_connect_error", + ]); + expect(events.filter((m) => m.listen_phase === "reconnected").map((m) => m.status)).toEqual([ + "ok", + "ok", + ]); + }); +}); diff --git a/src/tests/unit/lib/tunnel/appsync.test.ts b/src/tests/unit/lib/tunnel/appsync.test.ts index 3399eda..ba1394d 100644 --- a/src/tests/unit/lib/tunnel/appsync.test.ts +++ b/src/tests/unit/lib/tunnel/appsync.test.ts @@ -1,19 +1,6 @@ -import { afterEach, beforeEach, describe, expect, test } from "bun:test"; -import { Command } from "commander"; +import { afterEach, describe, expect, test } from "bun:test"; import { getEventListeners } from "node:events"; -import { mkdtempSync, readFileSync, rmSync } from "node:fs"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { spoolPath } from "../../../../lib/telemetry/constants.js"; -import { clearCiEnv } from "../../../../lib/test-support/_helpers.js"; -import { - __resetTelemetryRecorderForTest, - __setSpawnForTest, - installTelemetry, -} from "../../../../lib/telemetry/index.js"; -import { writeState } from "../../../../lib/telemetry/state.js"; -import type { SpoolRecord } from "../../../../lib/telemetry/types.js"; import { __setFetchForTest } from "../../../../lib/tunnel/forwarder.js"; import { __setTunnelTimingForTest, @@ -34,14 +21,7 @@ import { waitUntil, } from "../../../../lib/test-support/fake-websocket.js"; -// The telemetry recorder is disabled on CI runners; the reliability tests read -// the spool it writes, so CI detection is cleared for this file. -let restoreCiEnv: () => void = () => undefined; -beforeEach(() => { - restoreCiEnv = clearCiEnv(); -}); afterEach(() => { - restoreCiEnv(); stopProgress(); }); @@ -820,104 +800,6 @@ describe("runAppSync reliability (fake WebSocket)", () => { } }); - test("reports failed attempts once per outage, and a recovered drop as reconnected, not an error", async () => { - __setWebSocketForTest(FakeWebSocket); - __setTunnelTimingForTest({ - backoffMs: 5, - closeFlushMs: 0, - idleTimeoutMs: 5000, - maxBackoffMs: 5, - }); - const dir = mkdtempSync(join(tmpdir(), "cb-appsync-telemetry-")); - const prevDir = process.env.CHARGEBEE_CONFIG_DIR; - process.env.CHARGEBEE_CONFIG_DIR = dir; - writeState({ notice_shown: true }); - __resetTelemetryRecorderForTest(); - __setSpawnForTest(() => ({ unref() {} })); - const { restore } = captureLogs(); - - try { - FakeWebSocket.instances = []; - const { opts, ac } = baseOpts(); - const program = new Command(); - program.exitOverride(); - program.command("listen").action(async () => { - await runAppSync(opts); - }); - installTelemetry(program, "1.0.0-test"); - const run = program.parseAsync(["listen"], { from: "user" }); - - // Outage 1: three attempts close before the handshake completes. - for (let i = 0; i < 3; i++) { - await waitUntil(() => FakeWebSocket.instances.length >= i + 1); - const ws = FakeWebSocket.instances[i]!; - await waitUntil(() => ws.sent.length > 0); - ws.serverClose(); - } - // Recovery: a full handshake reaches Ready. - await waitUntil(() => FakeWebSocket.instances.length >= 4); - const healthy = FakeWebSocket.instances[3]!; - await waitUntil(() => healthy.sent.length > 0); - healthy.emit({ type: "connection_ack" }); - await waitUntil(() => healthy.parsed().some((m) => m.type === "subscribe")); - healthy.emit({ type: "subscribe_success" }); - // A working session drops: no error yet, only the next result counts. - healthy.serverClose(); - // Outage 2: one attempt after the drop fails before the handshake. - await waitUntil(() => FakeWebSocket.instances.length >= 5); - const failed = FakeWebSocket.instances[4]!; - await waitUntil(() => failed.sent.length > 0); - failed.serverClose(); - // The next attempt subscribes again. - await waitUntil(() => FakeWebSocket.instances.length >= 6); - const back = FakeWebSocket.instances[5]!; - await waitUntil(() => back.sent.length > 0); - back.emit({ type: "connection_ack" }); - await waitUntil(() => back.parsed().some((m) => m.type === "subscribe")); - back.emit({ type: "subscribe_success" }); - // A drop straight back into a working session records only `reconnected`. - back.serverClose(); - await waitUntil(() => FakeWebSocket.instances.length >= 7); - const again = FakeWebSocket.instances[6]!; - await waitUntil(() => again.sent.length > 0); - again.emit({ type: "connection_ack" }); - await waitUntil(() => again.parsed().some((m) => m.type === "subscribe")); - again.emit({ type: "subscribe_success" }); - await waitUntil(() => again.parsed().some((m) => m.type === "publish")); - - ac.abort(); - await run; - - const metadata = readFileSync(spoolPath(), "utf-8") - .trim() - .split("\n") - .map((line) => (JSON.parse(line) as SpoolRecord).event.metadata); - expect(metadata.map((m) => m.listen_phase)).toEqual([ - "error", - "established", - "error", - "reconnected", - "reconnected", - "closed", - ]); - expect(metadata.filter((m) => m.listen_phase === "error").map((m) => m.err_type)).toEqual([ - "listen_connect_error", - "listen_connect_error", - ]); - expect(metadata.filter((m) => m.listen_phase === "reconnected").map((m) => m.status)).toEqual([ - "ok", - "ok", - ]); - } finally { - restore(); - __resetTelemetryRecorderForTest(); - __setSpawnForTest(null); - if (prevDir === undefined) delete process.env.CHARGEBEE_CONFIG_DIR; - else process.env.CHARGEBEE_CONFIG_DIR = prevDir; - rmSync(dir, { recursive: true, force: true }); - } - }); - /** Resolve with `"pending"` if `p` hasn't settled in `ms`, so a hung run still reaches cleanup. */ function within(p: Promise, ms = 2000): Promise { return Promise.race([p, new Promise<"pending">((r) => setTimeout(() => r("pending"), ms))]);