280 lines
8.8 KiB
TypeScript
280 lines
8.8 KiB
TypeScript
import { DEFAULT_ACCOUNT_ID } from "openclaw/plugin-sdk/routing";
|
|
import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { createQueuedWizardPrompter } from "../../../test/helpers/plugins/setup-wizard.js";
|
|
import { whatsappPlugin } from "./channel.js";
|
|
import type { OpenClawConfig } from "./runtime-api.js";
|
|
import { finalizeWhatsAppSetup } from "./setup-finalize.js";
|
|
|
|
const hoisted = vi.hoisted(() => ({
|
|
loginWeb: vi.fn(async () => {}),
|
|
pathExists: vi.fn(async () => false),
|
|
resolveWhatsAppAuthDir: vi.fn(() => ({
|
|
authDir: "/tmp/openclaw-whatsapp-test",
|
|
})),
|
|
}));
|
|
|
|
vi.mock("./login.js", () => ({
|
|
loginWeb: hoisted.loginWeb,
|
|
}));
|
|
|
|
vi.mock("openclaw/plugin-sdk/setup", async () => {
|
|
const actual = await vi.importActual<typeof import("openclaw/plugin-sdk/setup")>(
|
|
"openclaw/plugin-sdk/setup",
|
|
);
|
|
const normalizeE164 = (value?: string | null) => {
|
|
const raw = `${value ?? ""}`.trim();
|
|
if (!raw) {
|
|
return "";
|
|
}
|
|
const digits = raw.replace(/[^\d+]/g, "");
|
|
return digits.startsWith("+") ? digits : `+${digits}`;
|
|
};
|
|
return {
|
|
...actual,
|
|
DEFAULT_ACCOUNT_ID,
|
|
normalizeAccountId: (value?: string | null) => value?.trim() || DEFAULT_ACCOUNT_ID,
|
|
normalizeAllowFromEntries: (entries: string[], normalize: (value: string) => string) => [
|
|
...new Set(entries.map((entry) => (entry === "*" ? "*" : normalize(entry))).filter(Boolean)),
|
|
],
|
|
normalizeE164,
|
|
pathExists: hoisted.pathExists,
|
|
splitSetupEntries: (raw: string) =>
|
|
raw
|
|
.split(",")
|
|
.map((entry) => entry.trim())
|
|
.filter(Boolean),
|
|
setSetupChannelEnabled: (cfg: OpenClawConfig, channel: string, enabled: boolean) => ({
|
|
...cfg,
|
|
channels: {
|
|
...cfg.channels,
|
|
[channel]: {
|
|
...(cfg.channels?.[channel as keyof NonNullable<OpenClawConfig["channels"]>] as object),
|
|
enabled,
|
|
},
|
|
},
|
|
}),
|
|
};
|
|
});
|
|
|
|
vi.mock("./accounts.js", async () => {
|
|
const actual = await vi.importActual<typeof import("./accounts.js")>("./accounts.js");
|
|
return {
|
|
...actual,
|
|
resolveWhatsAppAuthDir: hoisted.resolveWhatsAppAuthDir,
|
|
};
|
|
});
|
|
|
|
function createRuntime(): RuntimeEnv {
|
|
return {
|
|
error: vi.fn(),
|
|
} as unknown as RuntimeEnv;
|
|
}
|
|
|
|
async function runConfigureWithHarness(params: {
|
|
harness: ReturnType<typeof createQueuedWizardPrompter>;
|
|
cfg?: OpenClawConfig;
|
|
runtime?: RuntimeEnv;
|
|
forceAllowFrom?: boolean;
|
|
}) {
|
|
const result = await finalizeWhatsAppSetup({
|
|
cfg: params.cfg ?? ({} as OpenClawConfig),
|
|
accountId: DEFAULT_ACCOUNT_ID,
|
|
forceAllowFrom: params.forceAllowFrom ?? false,
|
|
prompter: params.harness.prompter,
|
|
runtime: params.runtime ?? createRuntime(),
|
|
});
|
|
return {
|
|
accountId: DEFAULT_ACCOUNT_ID,
|
|
cfg: result.cfg,
|
|
};
|
|
}
|
|
|
|
function createSeparatePhoneHarness(params: { selectValues: string[]; textValues?: string[] }) {
|
|
return createQueuedWizardPrompter({
|
|
confirmValues: [false],
|
|
selectValues: params.selectValues,
|
|
textValues: params.textValues,
|
|
});
|
|
}
|
|
|
|
async function runSeparatePhoneFlow(params: { selectValues: string[]; textValues?: string[] }) {
|
|
hoisted.pathExists.mockResolvedValue(true);
|
|
const harness = createSeparatePhoneHarness({
|
|
selectValues: params.selectValues,
|
|
textValues: params.textValues,
|
|
});
|
|
const result = await runConfigureWithHarness({
|
|
harness,
|
|
});
|
|
return { harness, result };
|
|
}
|
|
|
|
describe("whatsapp setup wizard", () => {
|
|
beforeEach(() => {
|
|
hoisted.loginWeb.mockReset();
|
|
hoisted.pathExists.mockReset();
|
|
hoisted.pathExists.mockResolvedValue(false);
|
|
hoisted.resolveWhatsAppAuthDir.mockReset();
|
|
hoisted.resolveWhatsAppAuthDir.mockReturnValue({ authDir: "/tmp/openclaw-whatsapp-test" });
|
|
});
|
|
|
|
it("applies owner allowlist when forceAllowFrom is enabled", async () => {
|
|
const harness = createQueuedWizardPrompter({
|
|
confirmValues: [false],
|
|
textValues: ["+1 (555) 555-0123"],
|
|
});
|
|
|
|
const result = await runConfigureWithHarness({
|
|
harness,
|
|
forceAllowFrom: true,
|
|
});
|
|
|
|
expect(result.accountId).toBe(DEFAULT_ACCOUNT_ID);
|
|
expect(hoisted.loginWeb).not.toHaveBeenCalled();
|
|
expect(result.cfg.channels?.whatsapp?.selfChatMode).toBe(true);
|
|
expect(result.cfg.channels?.whatsapp?.dmPolicy).toBe("allowlist");
|
|
expect(result.cfg.channels?.whatsapp?.allowFrom).toEqual(["+15555550123"]);
|
|
expect(harness.text).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
message: "Your personal WhatsApp number (the phone you will message from)",
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("supports disabled DM policy for separate-phone setup", async () => {
|
|
const { harness, result } = await runSeparatePhoneFlow({
|
|
selectValues: ["separate", "disabled"],
|
|
});
|
|
|
|
expect(result.cfg.channels?.whatsapp?.selfChatMode).toBe(false);
|
|
expect(result.cfg.channels?.whatsapp?.dmPolicy).toBe("disabled");
|
|
expect(result.cfg.channels?.whatsapp?.allowFrom).toBeUndefined();
|
|
expect(harness.text).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("normalizes allowFrom entries when list mode is selected", async () => {
|
|
const { result } = await runSeparatePhoneFlow({
|
|
selectValues: ["separate", "allowlist", "list"],
|
|
textValues: ["+1 (555) 555-0123, +15555550123, *"],
|
|
});
|
|
|
|
expect(result.cfg.channels?.whatsapp?.selfChatMode).toBe(false);
|
|
expect(result.cfg.channels?.whatsapp?.dmPolicy).toBe("allowlist");
|
|
expect(result.cfg.channels?.whatsapp?.allowFrom).toEqual(["+15555550123", "*"]);
|
|
});
|
|
|
|
it("enables allowlist self-chat mode for personal-phone setup", async () => {
|
|
hoisted.pathExists.mockResolvedValue(true);
|
|
const harness = createQueuedWizardPrompter({
|
|
confirmValues: [false],
|
|
selectValues: ["personal"],
|
|
textValues: ["+1 (555) 111-2222"],
|
|
});
|
|
|
|
const result = await runConfigureWithHarness({
|
|
harness,
|
|
});
|
|
|
|
expect(result.cfg.channels?.whatsapp?.selfChatMode).toBe(true);
|
|
expect(result.cfg.channels?.whatsapp?.dmPolicy).toBe("allowlist");
|
|
expect(result.cfg.channels?.whatsapp?.allowFrom).toEqual(["+15551112222"]);
|
|
});
|
|
|
|
it("forces wildcard allowFrom for open policy without allowFrom follow-up prompts", async () => {
|
|
hoisted.pathExists.mockResolvedValue(true);
|
|
const harness = createSeparatePhoneHarness({
|
|
selectValues: ["separate", "open"],
|
|
});
|
|
|
|
const result = await runConfigureWithHarness({
|
|
harness,
|
|
cfg: {
|
|
channels: {
|
|
whatsapp: {
|
|
allowFrom: ["+15555550123"],
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(result.cfg.channels?.whatsapp?.selfChatMode).toBe(false);
|
|
expect(result.cfg.channels?.whatsapp?.dmPolicy).toBe("open");
|
|
expect(result.cfg.channels?.whatsapp?.allowFrom).toEqual(["*", "+15555550123"]);
|
|
expect(harness.select).toHaveBeenCalledTimes(2);
|
|
expect(harness.text).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("runs WhatsApp login when not linked and user confirms linking", async () => {
|
|
hoisted.pathExists.mockResolvedValue(false);
|
|
const harness = createQueuedWizardPrompter({
|
|
confirmValues: [true],
|
|
selectValues: ["separate", "disabled"],
|
|
});
|
|
const runtime = createRuntime();
|
|
|
|
await runConfigureWithHarness({
|
|
harness,
|
|
runtime,
|
|
});
|
|
|
|
expect(hoisted.loginWeb).toHaveBeenCalledWith(false, undefined, runtime, DEFAULT_ACCOUNT_ID);
|
|
});
|
|
|
|
it("skips relink note when already linked and relink is declined", async () => {
|
|
hoisted.pathExists.mockResolvedValue(true);
|
|
const harness = createSeparatePhoneHarness({
|
|
selectValues: ["separate", "disabled"],
|
|
});
|
|
|
|
await runConfigureWithHarness({
|
|
harness,
|
|
});
|
|
|
|
expect(hoisted.loginWeb).not.toHaveBeenCalled();
|
|
expect(harness.note).not.toHaveBeenCalledWith(
|
|
expect.stringContaining("openclaw channels login"),
|
|
"WhatsApp",
|
|
);
|
|
});
|
|
|
|
it("shows follow-up login command note when not linked and linking is skipped", async () => {
|
|
hoisted.pathExists.mockResolvedValue(false);
|
|
const harness = createSeparatePhoneHarness({
|
|
selectValues: ["separate", "disabled"],
|
|
});
|
|
|
|
await runConfigureWithHarness({
|
|
harness,
|
|
});
|
|
|
|
expect(harness.note).toHaveBeenCalledWith(
|
|
expect.stringContaining("openclaw channels login"),
|
|
"WhatsApp",
|
|
);
|
|
});
|
|
|
|
it("heartbeat readiness uses configured defaultAccount for active listener checks", async () => {
|
|
const result = await whatsappPlugin.heartbeat?.checkReady?.({
|
|
cfg: {
|
|
channels: {
|
|
whatsapp: {
|
|
defaultAccount: "work",
|
|
accounts: {
|
|
work: {
|
|
authDir: "/tmp/work",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
} as OpenClawConfig,
|
|
deps: {
|
|
webAuthExists: async () => true,
|
|
hasActiveWebListener: (accountId?: string) => accountId === "work",
|
|
},
|
|
});
|
|
|
|
expect(result).toEqual({ ok: true, reason: "ok" });
|
|
});
|
|
});
|