mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 19:04:58 +00:00
fix(imessage): prevent rpc spawn in tests
This commit is contained in:
22
src/imessage/client.test.ts
Normal file
22
src/imessage/client.test.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
const spawnMock = vi.hoisted(() => vi.fn());
|
||||||
|
|
||||||
|
vi.mock("node:child_process", () => ({
|
||||||
|
spawn: (...args: unknown[]) => spawnMock(...args),
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe("createIMessageRpcClient", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
spawnMock.mockReset();
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -37,6 +37,14 @@ type PendingRequest = {
|
|||||||
timer?: NodeJS.Timeout;
|
timer?: NodeJS.Timeout;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function isTestEnv(): boolean {
|
||||||
|
if (process.env.NODE_ENV === "test") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const vitest = process.env.VITEST?.trim().toLowerCase();
|
||||||
|
return Boolean(vitest);
|
||||||
|
}
|
||||||
|
|
||||||
export class IMessageRpcClient {
|
export class IMessageRpcClient {
|
||||||
private readonly cliPath: string;
|
private readonly cliPath: string;
|
||||||
private readonly dbPath?: string;
|
private readonly dbPath?: string;
|
||||||
@@ -63,6 +71,9 @@ export class IMessageRpcClient {
|
|||||||
if (this.child) {
|
if (this.child) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (isTestEnv()) {
|
||||||
|
throw new Error("Refusing to start imsg rpc in test environment; mock iMessage RPC client");
|
||||||
|
}
|
||||||
const args = ["rpc"];
|
const args = ["rpc"];
|
||||||
if (this.dbPath) {
|
if (this.dbPath) {
|
||||||
args.push("--db", this.dbPath);
|
args.push("--db", this.dbPath);
|
||||||
|
|||||||
Reference in New Issue
Block a user