Files
openclaw/src/gateway/server.channels.e2e.test.ts
plum-dawg c96ffa7186 feat: Add Line plugin (#1630)
* feat: add LINE plugin (#1630) (thanks @plum-dawg)

* feat: complete LINE plugin (#1630) (thanks @plum-dawg)

* chore: drop line plugin node_modules (#1630) (thanks @plum-dawg)

* test: mock /context report in commands test (#1630) (thanks @plum-dawg)

* test: limit macOS CI workers to avoid OOM (#1630) (thanks @plum-dawg)

* test: reduce macOS CI vitest workers (#1630) (thanks @plum-dawg)

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-01-25 12:22:36 +00:00

225 lines
6.1 KiB
TypeScript

import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
import type { ChannelPlugin } from "../channels/plugins/types.js";
import type { PluginRegistry } from "../plugins/registry.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import {
connectOk,
installGatewayTestHooks,
rpcReq,
startServerWithClient,
} from "./test-helpers.js";
const loadConfigHelpers = async () => await import("../config/config.js");
installGatewayTestHooks({ scope: "suite" });
const registryState = vi.hoisted(() => ({
registry: {
plugins: [],
tools: [],
channels: [],
providers: [],
gatewayHandlers: {},
httpHandlers: [],
httpRoutes: [],
cliRegistrars: [],
services: [],
diagnostics: [],
} as PluginRegistry,
}));
vi.mock("./server-plugins.js", async () => {
const { setActivePluginRegistry } = await import("../plugins/runtime.js");
return {
loadGatewayPlugins: (params: { baseMethods: string[] }) => {
setActivePluginRegistry(registryState.registry);
return {
pluginRegistry: registryState.registry,
gatewayMethods: params.baseMethods ?? [],
};
},
};
});
const createRegistry = (channels: PluginRegistry["channels"]): PluginRegistry => ({
plugins: [],
tools: [],
channels,
providers: [],
gatewayHandlers: {},
httpHandlers: [],
httpRoutes: [],
cliRegistrars: [],
services: [],
diagnostics: [],
});
const createStubChannelPlugin = (params: {
id: ChannelPlugin["id"];
label: string;
summary?: Record<string, unknown>;
logoutCleared?: boolean;
}): ChannelPlugin => ({
id: params.id,
meta: {
id: params.id,
label: params.label,
selectionLabel: params.label,
docsPath: `/channels/${params.id}`,
blurb: "test stub.",
},
capabilities: { chatTypes: ["direct"] },
config: {
listAccountIds: () => ["default"],
resolveAccount: () => ({}),
isConfigured: async () => false,
},
status: {
buildChannelSummary: async () => ({
configured: false,
...params.summary,
}),
},
gateway: {
logoutAccount: async () => ({
cleared: params.logoutCleared ?? false,
envToken: false,
}),
},
});
const telegramPlugin: ChannelPlugin = {
...createStubChannelPlugin({
id: "telegram",
label: "Telegram",
summary: { tokenSource: "none", lastProbeAt: null },
logoutCleared: true,
}),
gateway: {
logoutAccount: async ({ cfg }) => {
const { writeConfigFile } = await import("../config/config.js");
const nextTelegram = cfg.channels?.telegram ? { ...cfg.channels.telegram } : {};
delete nextTelegram.botToken;
await writeConfigFile({
...cfg,
channels: {
...cfg.channels,
telegram: nextTelegram,
},
});
return { cleared: true, envToken: false, loggedOut: true };
},
},
};
const defaultRegistry = createRegistry([
{
pluginId: "whatsapp",
source: "test",
plugin: createStubChannelPlugin({ id: "whatsapp", label: "WhatsApp" }),
},
{
pluginId: "telegram",
source: "test",
plugin: telegramPlugin,
},
{
pluginId: "signal",
source: "test",
plugin: createStubChannelPlugin({
id: "signal",
label: "Signal",
summary: { lastProbeAt: null },
}),
},
]);
let server: Awaited<ReturnType<typeof startServerWithClient>>["server"];
let ws: Awaited<ReturnType<typeof startServerWithClient>>["ws"];
beforeAll(async () => {
setRegistry(defaultRegistry);
const started = await startServerWithClient();
server = started.server;
ws = started.ws;
await connectOk(ws);
});
afterAll(async () => {
ws.close();
await server.close();
});
function setRegistry(registry: PluginRegistry) {
registryState.registry = registry;
setActivePluginRegistry(registry);
}
describe("gateway server channels", () => {
test("channels.status returns snapshot without probe", async () => {
vi.stubEnv("TELEGRAM_BOT_TOKEN", undefined);
setRegistry(defaultRegistry);
const res = await rpcReq<{
channels?: Record<
string,
| {
configured?: boolean;
tokenSource?: string;
probe?: unknown;
lastProbeAt?: unknown;
}
| { linked?: boolean }
>;
}>(ws, "channels.status", { probe: false, timeoutMs: 2000 });
expect(res.ok).toBe(true);
const telegram = res.payload?.channels?.telegram;
const signal = res.payload?.channels?.signal;
expect(res.payload?.channels?.whatsapp).toBeTruthy();
expect(telegram?.configured).toBe(false);
expect(telegram?.tokenSource).toBe("none");
expect(telegram?.probe).toBeUndefined();
expect(telegram?.lastProbeAt).toBeNull();
expect(signal?.configured).toBe(false);
expect(signal?.probe).toBeUndefined();
expect(signal?.lastProbeAt).toBeNull();
});
test("channels.logout reports no session when missing", async () => {
setRegistry(defaultRegistry);
const res = await rpcReq<{ cleared?: boolean; channel?: string }>(ws, "channels.logout", {
channel: "whatsapp",
});
expect(res.ok).toBe(true);
expect(res.payload?.channel).toBe("whatsapp");
expect(res.payload?.cleared).toBe(false);
});
test("channels.logout clears telegram bot token from config", async () => {
vi.stubEnv("TELEGRAM_BOT_TOKEN", undefined);
setRegistry(defaultRegistry);
const { readConfigFileSnapshot, writeConfigFile } = await loadConfigHelpers();
await writeConfigFile({
channels: {
telegram: {
botToken: "123:abc",
groups: { "*": { requireMention: false } },
},
},
});
const res = await rpcReq<{
cleared?: boolean;
envToken?: boolean;
channel?: string;
}>(ws, "channels.logout", { channel: "telegram" });
expect(res.ok).toBe(true);
expect(res.payload?.channel).toBe("telegram");
expect(res.payload?.cleared).toBe(true);
expect(res.payload?.envToken).toBe(false);
const snap = await readConfigFileSnapshot();
expect(snap.valid).toBe(true);
expect(snap.config?.channels?.telegram?.botToken).toBeUndefined();
expect(snap.config?.channels?.telegram?.groups?.["*"]?.requireMention).toBe(false);
});
});