Files
adolf/extensions/feishu/src/monitor.transport-status.test.ts
alvis bedb527145
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Vendor OpenClaw source as Adolf fork baseline
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11),
free to diverge. Tree copied sans upstream .git; upstream remote added for
future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19.
Preserves docs/ARCHITECTURE.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
2026-07-05 09:36:54 +00:00

233 lines
6.9 KiB
TypeScript

// Tests for status publishing in monitorWebSocket and monitorWebhook. These
// tests exercise the status sink wiring used by the gateway health monitor.
// See PROPOSAL.md for the incident background.
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
type StatusPatch = {
connected?: boolean;
lastConnectedAt?: number | null;
lastEventAt?: number | null;
lastTransportActivityAt?: number | null;
lastError?: string | null;
};
type StatusSink = (patch: StatusPatch) => void;
function createRecordingSink(): { sink: StatusSink; calls: StatusPatch[] } {
const calls: StatusPatch[] = [];
return {
sink: (patch) => {
calls.push(patch);
},
calls,
};
}
async function loadTransportModule() {
return await import("./monitor.transport.js");
}
describe("monitorWebSocket status publishing", () => {
let originalNow: () => number;
let nowValue: number;
beforeEach(() => {
nowValue = 1_700_000_000_000;
originalNow = Date.now;
Date.now = () => nowValue;
});
afterEach(() => {
Date.now = originalNow;
vi.restoreAllMocks();
});
it("publishes connected state when the WS SDK reports ready or reconnected", async () => {
const recorder = createRecordingSink();
const fakeWsClient = {
start: vi.fn(async () => undefined),
close: vi.fn(),
};
const { monitorWebSocket } = await loadTransportModule();
const account = {
accountId: "acct-1",
appId: "app",
appSecret: "secret",
domain: "https://open.feishu.cn",
encryptKey: undefined,
verificationToken: undefined,
config: { connectionMode: "websocket" as const },
} as never;
const abortController = new AbortController();
// Start the monitor in background; it will call createFeishuWSClient.
const wsClientModule = await import("./client.js");
let callbacks:
| {
onReady?: () => void;
onReconnected?: () => void;
onReconnecting?: () => void;
}
| undefined;
vi.spyOn(wsClientModule, "createFeishuWSClient").mockImplementation(
async (_account, nextCallbacks) => {
callbacks = nextCallbacks as typeof callbacks;
return fakeWsClient as never;
},
);
const monitorPromise = monitorWebSocket({
account,
accountId: "acct-1",
abortSignal: abortController.signal,
eventDispatcher: { register: () => undefined } as never,
statusSink: recorder.sink,
});
// Let the WS handshake complete.
await new Promise<void>((resolve) => {
setImmediate(() => resolve());
});
expect(recorder.calls).toEqual([]);
callbacks?.onReady?.();
const first = recorder.calls[0];
expect(first?.connected).toBe(true);
expect(first?.lastConnectedAt).toBe(nowValue);
expect(first?.lastEventAt).toBe(nowValue);
expect(first?.lastTransportActivityAt).toBeUndefined();
expect(first?.lastError).toBeNull();
nowValue += 1_000;
callbacks?.onReconnected?.();
const second = recorder.calls[1];
expect(second?.connected).toBe(true);
expect(second?.lastConnectedAt).toBe(nowValue);
expect(second?.lastEventAt).toBe(nowValue);
expect(second?.lastTransportActivityAt).toBeUndefined();
expect(second?.lastError).toBeNull();
nowValue += 1_000;
callbacks?.onReconnecting?.();
const third = recorder.calls[2];
expect(third?.connected).toBe(false);
expect(third?.lastEventAt).toBe(nowValue);
expect(third?.lastTransportActivityAt).toBeUndefined();
// Trigger abort to terminate the monitor cleanly.
abortController.abort();
await monitorPromise;
});
it("publishes disconnected when WS handshake throws", async () => {
const recorder = createRecordingSink();
const { monitorWebSocket } = await loadTransportModule();
const account = {
accountId: "acct-2",
appId: "app",
appSecret: "secret",
domain: "https://open.feishu.cn",
encryptKey: undefined,
verificationToken: undefined,
config: { connectionMode: "websocket" as const },
} as never;
const abortController = new AbortController();
const wsClientModule = await import("./client.js");
vi.spyOn(wsClientModule, "createFeishuWSClient").mockRejectedValue(new Error("boom"));
// Pre-abort so the monitor exits after the first failed attempt.
setImmediate(() => abortController.abort());
const monitorPromise = monitorWebSocket({
account,
accountId: "acct-2",
abortSignal: abortController.signal,
eventDispatcher: { register: () => undefined } as never,
statusSink: recorder.sink,
});
await monitorPromise;
const disconnected = recorder.calls.find((c) => c.connected === false);
expect(disconnected).toBeDefined();
expect(disconnected?.lastEventAt).toBe(nowValue);
expect(disconnected?.lastTransportActivityAt).toBeUndefined();
});
});
describe("monitorWebhook status publishing", () => {
let originalNow: () => number;
let nowValue: number;
beforeEach(() => {
nowValue = 1_700_000_001_000;
originalNow = Date.now;
Date.now = () => nowValue;
});
afterEach(() => {
Date.now = originalNow;
vi.restoreAllMocks();
});
it("publishes connected on listen success", async () => {
const recorder = createRecordingSink();
const { monitorWebhook } = await loadTransportModule();
const account = {
accountId: "webhook-acct",
appId: "app",
appSecret: "secret",
domain: "https://open.feishu.cn",
encryptKey: "ek",
verificationToken: "vt",
config: {
connectionMode: "webhook" as const,
webhookPort: 0,
webhookPath: "/feishu/events",
webhookHost: "127.0.0.1",
},
} as never;
const abortController = new AbortController();
const monitorPromise = monitorWebhook({
account,
accountId: "webhook-acct",
abortSignal: abortController.signal,
eventDispatcher: { register: () => undefined, invoke: vi.fn() } as never,
statusSink: recorder.sink,
});
// Give the server time to listen.
await new Promise<void>((resolve) => {
setTimeout(() => resolve(), 50);
});
const connected = recorder.calls.find((c) => c.connected === true);
expect(connected).toBeDefined();
expect(connected?.lastConnectedAt).toBe(nowValue);
expect(connected?.lastEventAt).toBe(nowValue);
expect(connected?.lastTransportActivityAt).toBeUndefined();
abortController.abort();
await monitorPromise;
});
});
describe("FeishuStatusSink type contract", () => {
it("accepts a partial patch with only lastEventAt", async () => {
// Verifies the type signature allows the patterns we use. A compile-time
// check via tsserver; the runtime assertion is the call must not throw.
const recorder = createRecordingSink();
const sink: StatusSink = recorder.sink;
sink({ lastEventAt: 12345 });
expect(recorder.calls).toEqual([{ lastEventAt: 12345 }]);
});
});