test: split imessage status coverage

This commit is contained in:
Peter Steinberger
2026-04-07 13:58:24 +01:00
parent 4c67991f43
commit 67da64f98d
4 changed files with 223 additions and 194 deletions

View File

@@ -34,6 +34,7 @@ import {
imessageSecurityAdapter,
imessageSetupWizard,
} from "./shared.js";
import { probeIMessageStatusAccount } from "./status-core.js";
import {
inferIMessageTargetChatType,
looksLikeIMessageExplicitTargetId,
@@ -196,12 +197,11 @@ export const imessagePlugin: ChannelPlugin<ResolvedIMessageAccount, IMessageProb
dbPath: snapshot.dbPath ?? null,
}),
probeAccount: async ({ account, timeoutMs }) =>
await (
await loadIMessageChannelRuntime()
).probeIMessageAccount({
await probeIMessageStatusAccount({
account,
timeoutMs,
cliPath: account.config.cliPath,
dbPath: account.config.dbPath,
probeIMessageAccount: async (params) =>
await (await loadIMessageChannelRuntime()).probeIMessageAccount(params),
}),
resolveAccountSnapshot: ({ account, runtime }) => ({
accountId: account.accountId,

View File

@@ -0,0 +1,20 @@
import type { ResolvedIMessageAccount } from "./accounts.js";
import type { IMessageProbe } from "./probe.js";
type ProbeIMessageAccount = (params?: {
timeoutMs?: number;
cliPath?: string;
dbPath?: string;
}) => Promise<IMessageProbe>;
export async function probeIMessageStatusAccount(params: {
account: ResolvedIMessageAccount;
timeoutMs?: number;
probeIMessageAccount: ProbeIMessageAccount;
}): Promise<IMessageProbe> {
return await params.probeIMessageAccount({
timeoutMs: params.timeoutMs,
cliPath: params.account.config.cliPath,
dbPath: params.account.config.dbPath,
});
}

View File

@@ -0,0 +1,197 @@
import * as processRuntime from "openclaw/plugin-sdk/process-runtime";
import * as setupRuntime from "openclaw/plugin-sdk/setup";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createPluginSetupWizardStatus } from "../../../test/helpers/plugins/setup-wizard.js";
import { resolveIMessageAccount } from "./accounts.js";
import * as channelRuntimeModule from "./channel.runtime.js";
import * as clientModule from "./client.js";
import { probeIMessage } from "./probe.js";
import { imessageSetupWizard } from "./setup-surface.js";
import { probeIMessageStatusAccount } from "./status-core.js";
const getIMessageSetupStatus = createPluginSetupWizardStatus({
id: "imessage",
meta: {
label: "iMessage",
},
setupWizard: imessageSetupWizard,
} as never);
const spawnMock = vi.hoisted(() => vi.fn());
vi.mock("node:child_process", async () => {
const actual = await vi.importActual<typeof import("node:child_process")>("node:child_process");
return {
...actual,
spawn: (...args: unknown[]) => spawnMock(...args),
};
});
describe("createIMessageRpcClient", () => {
beforeEach(() => {
spawnMock.mockClear();
vi.stubEnv("VITEST", "true");
});
it("refuses to spawn imsg rpc in test environments", async () => {
const { createIMessageRpcClient } = await import("./client.js");
await expect(createIMessageRpcClient()).rejects.toThrow(
/Refusing to start imsg rpc in test environment/i,
);
expect(spawnMock).not.toHaveBeenCalled();
});
});
describe("imessage setup status", () => {
it("does not inherit configured state from a sibling account", async () => {
const result = await getIMessageSetupStatus({
cfg: {
channels: {
imessage: {
accounts: {
default: {
cliPath: "/usr/local/bin/imsg",
},
work: {},
},
},
},
},
accountOverrides: {
imessage: "work",
},
});
expect(result.configured).toBe(false);
expect(result.statusLines).toContain("iMessage: needs setup");
});
it("uses configured defaultAccount for omitted setup status cliPath", async () => {
const status = await getIMessageSetupStatus({
cfg: {
channels: {
imessage: {
cliPath: "/tmp/root-imsg",
defaultAccount: "work",
accounts: {
work: {
cliPath: "/tmp/work-imsg",
},
},
},
},
} as never,
accountOverrides: {},
});
expect(status.statusLines).toContain("imsg: missing (/tmp/work-imsg)");
});
it("does not inherit configured state from a sibling when defaultAccount is named", async () => {
const status = await getIMessageSetupStatus({
cfg: {
channels: {
imessage: {
defaultAccount: "work",
accounts: {
default: {
cliPath: "/usr/local/bin/imsg",
},
work: {},
},
},
},
} as never,
accountOverrides: {},
});
expect(status.configured).toBe(false);
expect(status.statusLines).toContain("iMessage: needs setup");
});
it("setup status lines use the selected account cliPath", async () => {
const status = await getIMessageSetupStatus({
cfg: {
channels: {
imessage: {
cliPath: "/tmp/root-imsg",
accounts: {
work: {
cliPath: "/tmp/work-imsg",
},
},
},
},
} as never,
accountOverrides: { imessage: "work" },
});
expect(status.statusLines).toContain("imsg: missing (/tmp/work-imsg)");
});
});
describe("probeIMessage", () => {
beforeEach(() => {
vi.restoreAllMocks();
spawnMock.mockClear();
vi.spyOn(setupRuntime, "detectBinary").mockResolvedValue(true);
vi.spyOn(processRuntime, "runCommandWithTimeout").mockResolvedValue({
stdout: "",
stderr: 'unknown command "rpc" for "imsg"',
code: 1,
signal: null,
killed: false,
termination: "exit",
});
});
it("marks unknown rpc subcommand as fatal", async () => {
const createIMessageRpcClientMock = vi
.spyOn(clientModule, "createIMessageRpcClient")
.mockResolvedValue({
request: vi.fn(),
stop: vi.fn(),
} as unknown as Awaited<ReturnType<typeof clientModule.createIMessageRpcClient>>);
const result = await probeIMessage(1000, { cliPath: "imsg-test-rpc" });
expect(result.ok).toBe(false);
expect(result.fatal).toBe(true);
expect(result.error).toMatch(/rpc/i);
expect(createIMessageRpcClientMock).not.toHaveBeenCalled();
});
it("status probe uses account-scoped cliPath and dbPath", async () => {
const probeSpy = vi.spyOn(channelRuntimeModule, "probeIMessageAccount").mockResolvedValue({
ok: true,
cliPath: "imsg-work",
dbPath: "/tmp/work-db",
} as Awaited<ReturnType<typeof channelRuntimeModule.probeIMessageAccount>>);
const cfg = {
channels: {
imessage: {
cliPath: "imsg-root",
dbPath: "/tmp/root-db",
accounts: {
work: {
cliPath: "imsg-work",
dbPath: "/tmp/work-db",
},
},
},
},
} as const;
const account = resolveIMessageAccount({ cfg, accountId: "work" });
await probeIMessageStatusAccount({
account,
timeoutMs: 2500,
probeIMessageAccount: channelRuntimeModule.probeIMessageAccount,
});
expect(probeSpy).toHaveBeenCalledWith({
timeoutMs: 2500,
cliPath: "imsg-work",
dbPath: "/tmp/work-db",
});
});
});

View File

@@ -1,15 +1,8 @@
import * as processRuntime from "openclaw/plugin-sdk/process-runtime";
import * as setupRuntime from "openclaw/plugin-sdk/setup";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createPluginSetupWizardStatus } from "../../../test/helpers/plugins/setup-wizard.js";
import { imessagePlugin } from "./channel.js";
import * as channelRuntimeModule from "./channel.runtime.js";
import * as clientModule from "./client.js";
import { describe, expect, it } from "vitest";
import {
resolveIMessageGroupRequireMention,
resolveIMessageGroupToolPolicy,
} from "./group-policy.js";
import { probeIMessage } from "./probe.js";
import { imessageDmPolicy } from "./setup-core.js";
import { parseIMessageAllowFromEntries } from "./setup-surface.js";
import {
@@ -21,18 +14,6 @@ import {
parseIMessageTarget,
} from "./targets.js";
const getIMessageSetupStatus = createPluginSetupWizardStatus(imessagePlugin);
const spawnMock = vi.hoisted(() => vi.fn());
vi.mock("node:child_process", async () => {
const actual = await vi.importActual<typeof import("node:child_process")>("node:child_process");
return {
...actual,
spawn: (...args: unknown[]) => spawnMock(...args),
};
});
describe("imessage targets", () => {
it("parses chat_id targets", () => {
const target = parseIMessageTarget("chat_id:123");
@@ -118,21 +99,6 @@ describe("imessage targets", () => {
});
});
describe("createIMessageRpcClient", () => {
beforeEach(() => {
spawnMock.mockClear();
vi.stubEnv("VITEST", "true");
});
it("refuses to spawn imsg rpc in test environments", async () => {
const { createIMessageRpcClient } = await import("./client.js");
await expect(createIMessageRpcClient()).rejects.toThrow(
/Refusing to start imsg rpc in test environment/i,
);
expect(spawnMock).not.toHaveBeenCalled();
});
});
describe("imessage group policy", () => {
it("uses generic channel group policy helpers", () => {
const cfg = {
@@ -267,157 +233,3 @@ describe("parseIMessageAllowFromEntries", () => {
expect(next.channels?.imessage?.accounts?.work?.allowFrom).toEqual(["chat_id:123", "*"]);
});
});
describe("imessage setup status", () => {
it("does not inherit configured state from a sibling account", async () => {
const result = await getIMessageSetupStatus({
cfg: {
channels: {
imessage: {
accounts: {
default: {
cliPath: "/usr/local/bin/imsg",
},
work: {},
},
},
},
},
accountOverrides: {
imessage: "work",
},
});
expect(result.configured).toBe(false);
expect(result.statusLines).toContain("iMessage: needs setup");
});
it("uses configured defaultAccount for omitted setup status cliPath", async () => {
const status = await getIMessageSetupStatus({
cfg: {
channels: {
imessage: {
cliPath: "/tmp/root-imsg",
defaultAccount: "work",
accounts: {
work: {
cliPath: "/tmp/work-imsg",
},
},
},
},
} as never,
accountOverrides: {},
});
expect(status.statusLines).toContain("imsg: missing (/tmp/work-imsg)");
});
it("does not inherit configured state from a sibling when defaultAccount is named", async () => {
const status = await getIMessageSetupStatus({
cfg: {
channels: {
imessage: {
defaultAccount: "work",
accounts: {
default: {
cliPath: "/usr/local/bin/imsg",
},
work: {},
},
},
},
} as never,
accountOverrides: {},
});
expect(status.configured).toBe(false);
expect(status.statusLines).toContain("iMessage: needs setup");
});
});
describe("probeIMessage", () => {
beforeEach(() => {
vi.restoreAllMocks();
vi.spyOn(setupRuntime, "detectBinary").mockResolvedValue(true);
vi.spyOn(processRuntime, "runCommandWithTimeout").mockResolvedValue({
stdout: "",
stderr: 'unknown command "rpc" for "imsg"',
code: 1,
signal: null,
killed: false,
termination: "exit",
});
});
it("marks unknown rpc subcommand as fatal", async () => {
const createIMessageRpcClientMock = vi
.spyOn(clientModule, "createIMessageRpcClient")
.mockResolvedValue({
request: vi.fn(),
stop: vi.fn(),
} as unknown as Awaited<ReturnType<typeof clientModule.createIMessageRpcClient>>);
const result = await probeIMessage(1000, { cliPath: "imsg-test-rpc" });
expect(result.ok).toBe(false);
expect(result.fatal).toBe(true);
expect(result.error).toMatch(/rpc/i);
expect(createIMessageRpcClientMock).not.toHaveBeenCalled();
});
it("status probe uses account-scoped cliPath and dbPath", async () => {
const probeAccount = imessagePlugin.status?.probeAccount;
if (!probeAccount) {
throw new Error("imessage status.probeAccount unavailable");
}
const probeSpy = vi.spyOn(channelRuntimeModule, "probeIMessageAccount").mockResolvedValue({
ok: true,
cliPath: "imsg-work",
dbPath: "/tmp/work-db",
} as Awaited<ReturnType<typeof channelRuntimeModule.probeIMessageAccount>>);
const cfg = {
channels: {
imessage: {
cliPath: "imsg-root",
dbPath: "/tmp/root-db",
accounts: {
work: {
cliPath: "imsg-work",
dbPath: "/tmp/work-db",
},
},
},
},
} as const;
const account = imessagePlugin.config.resolveAccount(cfg, "work");
await probeAccount({ account, cfg, timeoutMs: 2500 } as never);
expect(probeSpy).toHaveBeenCalledWith({
timeoutMs: 2500,
cliPath: "imsg-work",
dbPath: "/tmp/work-db",
});
});
it("setup status lines use the selected account cliPath", async () => {
const status = await getIMessageSetupStatus({
cfg: {
channels: {
imessage: {
cliPath: "/tmp/root-imsg",
accounts: {
work: {
cliPath: "/tmp/work-imsg",
},
},
},
},
} as never,
accountOverrides: { imessage: "work" },
});
expect(status.statusLines).toContain("imsg: missing (/tmp/work-imsg)");
});
});