fix(imessage): prevent rpc spawn in tests

This commit is contained in:
Peter Steinberger
2026-02-13 17:36:30 +01:00
parent aa7fbf0488
commit 45b9aad0f4
2 changed files with 33 additions and 0 deletions

View 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();
});
});

View File

@@ -37,6 +37,14 @@ type PendingRequest = {
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 {
private readonly cliPath: string;
private readonly dbPath?: string;
@@ -63,6 +71,9 @@ export class IMessageRpcClient {
if (this.child) {
return;
}
if (isTestEnv()) {
throw new Error("Refusing to start imsg rpc in test environment; mock iMessage RPC client");
}
const args = ["rpc"];
if (this.dbPath) {
args.push("--db", this.dbPath);