openclaw/extensions/whatsapp/src/reconnect.test.ts
scoootscooob 16505718e8
refactor: move WhatsApp channel implementation to extensions/ (#45725)
* refactor: move WhatsApp channel from src/web/ to extensions/whatsapp/

Move all WhatsApp implementation code (77 source/test files + 9 channel
plugin files) from src/web/ and src/channels/plugins/*/whatsapp* to
extensions/whatsapp/src/.

- Leave thin re-export shims at all original locations so cross-cutting
  imports continue to resolve
- Update plugin-sdk/whatsapp.ts to only re-export generic framework
  utilities; channel-specific functions imported locally by the extension
- Update vi.mock paths in 15 cross-cutting test files
- Rename outbound.ts -> send.ts to match extension naming conventions
  and avoid false positive in cfg-threading guard test
- Widen tsconfig.plugin-sdk.dts.json rootDir to support shim->extension
  cross-directory references

Part of the core-channels-to-extensions migration (PR 6/10).

* style: format WhatsApp extension files

* fix: correct stale import paths in WhatsApp extension tests

Fix vi.importActual, test mock, and hardcoded source paths that weren't
updated during the file move:
- media.test.ts: vi.importActual path
- onboarding.test.ts: vi.importActual path
- test-helpers.ts: test/mocks/baileys.js path
- monitor-inbox.test-harness.ts: incomplete media/store mock
- login.test.ts: hardcoded source file path
- message-action-runner.media.test.ts: vi.mock/importActual path
2026-03-14 02:44:55 -07:00

52 lines
1.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../../../src/config/config.js";
import {
computeBackoff,
DEFAULT_HEARTBEAT_SECONDS,
DEFAULT_RECONNECT_POLICY,
resolveHeartbeatSeconds,
resolveReconnectPolicy,
sleepWithAbort,
} from "./reconnect.js";
describe("web reconnect helpers", () => {
const cfg: OpenClawConfig = {};
it("resolves sane reconnect defaults with clamps", () => {
const policy = resolveReconnectPolicy(cfg, {
initialMs: 100,
maxMs: 5,
factor: 20,
jitter: 2,
maxAttempts: -1,
});
expect(policy.initialMs).toBe(250); // clamped to minimum
expect(policy.maxMs).toBeGreaterThanOrEqual(policy.initialMs);
expect(policy.factor).toBeLessThanOrEqual(10);
expect(policy.jitter).toBeLessThanOrEqual(1);
expect(policy.maxAttempts).toBeGreaterThanOrEqual(0);
});
it("computes increasing backoff with jitter", () => {
const policy = { ...DEFAULT_RECONNECT_POLICY, jitter: 0 };
const first = computeBackoff(policy, 1);
const second = computeBackoff(policy, 2);
expect(first).toBe(policy.initialMs);
expect(second).toBeGreaterThan(first);
expect(second).toBeLessThanOrEqual(policy.maxMs);
});
it("returns heartbeat default when unset", () => {
expect(resolveHeartbeatSeconds(cfg)).toBe(DEFAULT_HEARTBEAT_SECONDS);
expect(resolveHeartbeatSeconds(cfg, 5)).toBe(5);
});
it("sleepWithAbort rejects on abort", async () => {
const controller = new AbortController();
const promise = sleepWithAbort(50, controller.signal);
controller.abort();
await expect(promise).rejects.toThrow("aborted");
});
});