Skip to content

Commit e2c92fc

Browse files
authored
test(cli): scope module resets to mocked policy cases (#134364)
1 parent 37a0942 commit e2c92fc

2 files changed

Lines changed: 50 additions & 47 deletions

File tree

‎src/cli/command-path-policy.test.ts‎

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Command path policy tests cover allowed CLI command path shapes and lazy imports.
22
import { importFreshModule } from "openclaw/plugin-sdk/test-fixtures";
3-
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import { describe, expect, it, vi } from "vitest";
44
import type { CliCommandCatalogEntry, CliCommandPathPolicy } from "./command-catalog.js";
55
import {
66
resolveCliCommandPathPolicy,
@@ -59,11 +59,6 @@ function expectConfigGuardResolver(
5959
}
6060

6161
describe("command-path-policy", () => {
62-
afterEach(() => {
63-
vi.doUnmock("./command-catalog.js");
64-
vi.resetModules();
65-
});
66-
6762
it("resolves status policy with shared startup semantics", () => {
6863
expectResolvedPolicy(["status"], {
6964
configGuard: "skip",
@@ -610,22 +605,28 @@ describe("command-path-policy", () => {
610605
},
611606
];
612607

613-
vi.doMock("./command-catalog.js", async (importOriginal) => {
614-
const actual = await importOriginal<typeof import("./command-catalog.js")>();
615-
return { ...actual, cliCommandCatalog: catalog };
616-
});
617-
const { resolveCliNetworkProxyPolicy: resolveCliNetworkProxyPolicyLocal } =
618-
await importFreshModule<typeof import("./command-path-policy.js")>(
619-
import.meta.url,
620-
"./command-path-policy.js?catalog-overrides",
621-
);
608+
vi.resetModules();
609+
try {
610+
vi.doMock("./command-catalog.js", async (importOriginal) => {
611+
const actual = await importOriginal<typeof import("./command-catalog.js")>();
612+
return { ...actual, cliCommandCatalog: catalog };
613+
});
614+
const { resolveCliNetworkProxyPolicy: resolveCliNetworkProxyPolicyLocal } =
615+
await importFreshModule<typeof import("./command-path-policy.js")>(
616+
import.meta.url,
617+
"./command-path-policy.js?catalog-overrides",
618+
);
622619

623-
expect(resolveCliNetworkProxyPolicyLocal(["node", "openclaw", "nodes", "camera", "snap"])).toBe(
624-
"default",
625-
);
626-
expect(resolveCliNetworkProxyPolicyLocal(["node", "openclaw", "nodes", "camera", "list"])).toBe(
627-
"bypass",
628-
);
620+
expect(
621+
resolveCliNetworkProxyPolicyLocal(["node", "openclaw", "nodes", "camera", "snap"]),
622+
).toBe("default");
623+
expect(
624+
resolveCliNetworkProxyPolicyLocal(["node", "openclaw", "nodes", "camera", "list"]),
625+
).toBe("bypass");
626+
} finally {
627+
vi.doUnmock("./command-catalog.js");
628+
vi.resetModules();
629+
}
629630
});
630631

631632
it("stops catalog policy resolution before positional arguments", () => {

‎src/cli/command-startup-policy.test.ts‎

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Command startup policy tests cover which CLI commands require startup side effects.
22
import { importFreshModule } from "openclaw/plugin-sdk/test-fixtures";
3-
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import { describe, expect, it, vi } from "vitest";
44
import { cliCommandCatalog } from "./command-catalog.js";
55
import { resolveCliExecutionStartupContext } from "./command-execution-startup.js";
66
import { resolveCliStartupPolicy } from "./command-startup-policy.js";
@@ -18,11 +18,6 @@ function resolvePolicy(params: {
1818
}
1919

2020
describe("command-startup-policy", () => {
21-
afterEach(() => {
22-
vi.doUnmock("./command-path-policy.js");
23-
vi.resetModules();
24-
});
25-
2621
it("resolves config guard policy for Commander and invocation-aware commands", () => {
2722
for (const commandPath of [
2823
["backup", "create"],
@@ -165,27 +160,34 @@ describe("command-startup-policy", () => {
165160
});
166161

167162
it("skips when-suppressed guards only for suppressed output", async () => {
168-
vi.doMock("./command-path-policy.js", () => ({
169-
resolveCliCommandPathPolicy: () => ({
170-
configGuard: "when-suppressed",
171-
loadPlugins: "never",
172-
pluginRegistry: { scope: "all" },
173-
ownsProtocolStdout: false,
174-
hideBanner: false,
175-
ensureCliPath: true,
176-
networkProxy: "default",
177-
}),
178-
}));
179-
const { resolveCliStartupPolicy: resolveWithSuppressedGuard } = await importFreshModule<
180-
typeof import("./command-startup-policy.js")
181-
>(import.meta.url, "./command-startup-policy.js?when-suppressed");
163+
vi.resetModules();
164+
try {
165+
vi.doMock("./command-path-policy.js", () => ({
166+
resolveCliCommandPathPolicy: () => ({
167+
configGuard: "when-suppressed",
168+
loadPlugins: "never",
169+
pluginRegistry: { scope: "all" },
170+
ownsProtocolStdout: false,
171+
hideBanner: false,
172+
ensureCliPath: true,
173+
networkProxy: "default",
174+
}),
175+
}));
176+
const { resolveCliStartupPolicy: resolveWithSuppressedGuard } = await importFreshModule<
177+
typeof import("./command-startup-policy.js")
178+
>(import.meta.url, "./command-startup-policy.js?when-suppressed");
182179

183-
expect(
184-
resolveWithSuppressedGuard({ commandPath: ["test"], jsonOutputMode: false }).skipConfigGuard,
185-
).toBe(false);
186-
expect(
187-
resolveWithSuppressedGuard({ commandPath: ["test"], jsonOutputMode: true }).skipConfigGuard,
188-
).toBe(true);
180+
expect(
181+
resolveWithSuppressedGuard({ commandPath: ["test"], jsonOutputMode: false })
182+
.skipConfigGuard,
183+
).toBe(false);
184+
expect(
185+
resolveWithSuppressedGuard({ commandPath: ["test"], jsonOutputMode: true }).skipConfigGuard,
186+
).toBe(true);
187+
} finally {
188+
vi.doUnmock("./command-path-policy.js");
189+
vi.resetModules();
190+
}
189191
});
190192

191193
it("matches plugin preload policy", () => {

0 commit comments

Comments
 (0)