test: use platform spy helper in cli tests

This commit is contained in:
Vincent Koc
2026-05-17 17:01:18 +08:00
parent 673596013e
commit 54d063167e
3 changed files with 14 additions and 30 deletions

View File

@@ -1,6 +1,7 @@
import { Command } from "commander";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { PluginPackageChannel } from "../plugins/manifest.js";
import { mockProcessPlatform } from "../test-utils/vitest-spies.js";
import { registerChannelsCli } from "./channels-cli.js";
const listBundledPackageChannelMetadataMock = vi.hoisted(() =>
@@ -19,13 +20,10 @@ function getChannelAddOptionFlags(program: Command): string[] {
describe("registerChannelsCli", () => {
const originalArgv = [...process.argv];
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");
afterEach(() => {
process.argv = [...originalArgv];
if (originalPlatform) {
Object.defineProperty(process, "platform", originalPlatform);
}
vi.restoreAllMocks();
vi.clearAllMocks();
});
@@ -78,7 +76,7 @@ describe("registerChannelsCli", () => {
cliAddOptions: [{ flags: "--homeserver <url>", description: "Matrix homeserver URL" }],
},
]);
Object.defineProperty(process, "platform", { value: "win32", configurable: true });
mockProcessPlatform("win32");
process.argv = [
"C:\\Program Files\\nodejs\\node.exe",
"C:\\repo\\openclaw.js",

View File

@@ -1,4 +1,5 @@
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { withMockedPlatform } from "../test-utils/vitest-spies.js";
const { spawnMock } = vi.hoisted(() => ({
spawnMock: vi.fn(),
@@ -28,18 +29,6 @@ function expectTaskkillCall(index: number, args: string[]) {
]);
}
async function withPlatform<T>(platform: NodeJS.Platform, run: () => Promise<T> | T): Promise<T> {
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");
Object.defineProperty(process, "platform", { value: platform, configurable: true });
try {
return await run();
} finally {
if (originalPlatform) {
Object.defineProperty(process, "platform", originalPlatform);
}
}
}
describe("killProcessTree", () => {
let killSpy: ReturnType<typeof vi.spyOn>;
@@ -67,7 +56,7 @@ describe("killProcessTree", () => {
return true;
}) as typeof process.kill);
await withPlatform("win32", async () => {
await withMockedPlatform("win32", async () => {
killProcessTree(4242, { graceMs: 25 });
expect(spawnMock).toHaveBeenCalledTimes(1);
@@ -86,7 +75,7 @@ describe("killProcessTree", () => {
return true;
}) as typeof process.kill);
await withPlatform("win32", async () => {
await withMockedPlatform("win32", async () => {
killProcessTree(5252, { graceMs: 10 });
await vi.advanceTimersByTimeAsync(10);
@@ -108,7 +97,7 @@ describe("killProcessTree", () => {
return true;
}) as typeof process.kill);
await withPlatform("linux", async () => {
await withMockedPlatform("linux", async () => {
killProcessTree(3333, { graceMs: 10 });
await vi.advanceTimersByTimeAsync(10);
@@ -127,7 +116,7 @@ describe("killProcessTree", () => {
return true;
}) as typeof process.kill);
await withPlatform("linux", async () => {
await withMockedPlatform("linux", async () => {
killProcessTree(4444, { graceMs: 5 });
await vi.advanceTimersByTimeAsync(5);
@@ -145,7 +134,7 @@ describe("killProcessTree", () => {
return true;
}) as typeof process.kill);
await withPlatform("linux", async () => {
await withMockedPlatform("linux", async () => {
killProcessTree(5555, { graceMs: 10, detached: false });
await vi.advanceTimersByTimeAsync(10);
@@ -169,7 +158,7 @@ describe("killProcessTree", () => {
return true;
}) as typeof process.kill);
await withPlatform("linux", async () => {
await withMockedPlatform("linux", async () => {
killProcessTree(6666, { graceMs: 10 });
await vi.advanceTimersByTimeAsync(10);

View File

@@ -1,16 +1,13 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { mockProcessPlatform } from "../test-utils/vitest-spies.js";
import { visibleWidth } from "./ansi.js";
import { resolveNoteColumns, wrapNoteMessage } from "./note.js";
import { renderTable } from "./table.js";
describe("renderTable", () => {
const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
afterEach(() => {
vi.unstubAllEnvs();
if (originalPlatformDescriptor) {
Object.defineProperty(process, "platform", originalPlatformDescriptor);
}
vi.restoreAllMocks();
});
it("prefers shrinking flex columns to avoid wrapping non-flex labels", () => {
@@ -215,7 +212,7 @@ describe("renderTable", () => {
});
it("falls back to ASCII borders on legacy Windows consoles", () => {
Object.defineProperty(process, "platform", { value: "win32", configurable: true });
mockProcessPlatform("win32");
vi.stubEnv("WT_SESSION", "");
vi.stubEnv("TERM_PROGRAM", "");
vi.stubEnv("TERM", "vt100");
@@ -233,7 +230,7 @@ describe("renderTable", () => {
});
it("keeps unicode borders on modern Windows terminals", () => {
Object.defineProperty(process, "platform", { value: "win32", configurable: true });
mockProcessPlatform("win32");
vi.stubEnv("WT_SESSION", "1");
vi.stubEnv("TERM", "");
vi.stubEnv("TERM_PROGRAM", "");