test(policy): centralize doctor fixtures (#118392)

This commit is contained in:
Peter Steinberger
2026-08-02 20:04:24 -07:00
committed by GitHub
parent 11795bdc7c
commit 5d853c6fce
6 changed files with 387 additions and 666 deletions
@@ -20,32 +20,18 @@ import {
ctx,
repairCtx,
runPolicyChecks,
runPolicyChecksFixture,
runDeniedChannelRepair,
runPolicyRepairCheck,
describe0BeforeEach0,
describe0AfterEach1,
setupPolicyDoctorTest,
teardownPolicyDoctorTest,
writePolicyFixture,
} from "./register.test-harness.js";
async function writePolicyFixture(...json: Parameters<typeof JSON.stringify>): Promise<string> {
const [policy] = json;
const configPath = join(workspaceDir, "openclaw.jsonc");
await fs.writeFile(configPath, "{}", "utf-8");
await fs.writeFile(
join(workspaceDir, "policy.jsonc"),
typeof policy === "string" ? policy : JSON.stringify(...json),
"utf-8",
);
return configPath;
}
async function runPolicyChecksFixture(policy: unknown) {
return runPolicyChecks(ctx(await writePolicyFixture(policy), cfgWithPolicy()));
}
describe("registerPolicyDoctorChecks", () => {
beforeEach(describe0BeforeEach0);
beforeEach(setupPolicyDoctorTest);
afterEach(describe0AfterEach1);
afterEach(teardownPolicyDoctorTest);
it("allows scoped overrides that are stricter than top-level policy", async () => {
const result = await runPolicyChecksFixture({
@@ -11,22 +11,11 @@ import {
ctx,
registerChecks,
runPolicyDoctorLint,
describe0BeforeEach0,
describe0AfterEach1,
setupPolicyDoctorTest,
teardownPolicyDoctorTest,
writePolicyFixture,
} from "./register.test-harness.js";
async function writePolicyFixture(...json: Parameters<typeof JSON.stringify>): Promise<string> {
const [policy] = json;
const configPath = join(workspaceDir, "openclaw.jsonc");
await fs.writeFile(configPath, "{}", "utf-8");
await fs.writeFile(
join(workspaceDir, "policy.jsonc"),
typeof policy === "string" ? policy : JSON.stringify(...json),
"utf-8",
);
return configPath;
}
function writeExecApprovalsPolicyFixture(execApprovals: object): Promise<string> {
return writePolicyFixture({ execApprovals });
}
@@ -41,9 +30,9 @@ function runRegisteredPolicyDoctor(configPath: string, cfg: OpenClawConfig) {
}
describe("registerPolicyDoctorChecks", () => {
beforeEach(describe0BeforeEach0);
beforeEach(setupPolicyDoctorTest);
afterEach(describe0AfterEach1);
afterEach(teardownPolicyDoctorTest);
it("does not report Responses URL fetching when it is disabled", async () => {
const cfg = {
@@ -1,18 +1,16 @@
// Imported by register.test.ts to keep its mocked suite in one Vitest module graph.
import { promises as fs } from "node:fs";
import { join } from "node:path";
import { runDoctorLintChecks, type OpenClawConfig } from "openclaw/plugin-sdk/health";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { collectPolicyEvidence } from "../policy-state.js";
import { registerPolicyDoctorChecks } from "./register.js";
import {
workspaceDir,
cfgWithPolicy,
ctx,
runPolicyChecks,
runPolicyDoctorLint,
describe0BeforeEach0,
describe0AfterEach1,
setupPolicyDoctorTest,
teardownPolicyDoctorTest,
writePolicyFixture,
} from "./register.test-harness.js";
const scanPolicyIngress = (cfg: object) =>
@@ -32,9 +30,7 @@ const INGRESS_POLICY = {
};
async function runPolicyScenario(cfg: OpenClawConfig, policy: object, mode: PolicyScenarioMode) {
const configPath = join(workspaceDir, "openclaw.jsonc");
await fs.writeFile(configPath, "{}", "utf-8");
await fs.writeFile(join(workspaceDir, "policy.jsonc"), JSON.stringify(policy), "utf-8");
const configPath = await writePolicyFixture(policy);
const checkContext = ctx(configPath, cfg);
if (mode === "doctor") {
return runPolicyDoctorLint(checkContext);
@@ -99,9 +95,9 @@ function policyAgentScope(agentIds: string[], allowedAccess?: string[], allowHos
}
describe("registerPolicyDoctorChecks", () => {
beforeEach(describe0BeforeEach0);
beforeEach(setupPolicyDoctorTest);
afterEach(describe0AfterEach1);
afterEach(teardownPolicyDoctorTest);
it("ignores nested groupPolicy when channel ingress is disabled", async () => {
const { result } = await runIngressPolicyScenario({
@@ -13,8 +13,9 @@ import {
runPolicyChecks,
runPolicyDoctorLint,
runPolicyRepairCheck,
describe0BeforeEach0,
describe0AfterEach1,
setupPolicyDoctorTest,
teardownPolicyDoctorTest,
writePolicyFixture,
} from "./register.test-harness.js";
const scanPolicyMcpServers = (cfg: object) =>
@@ -22,18 +23,6 @@ const scanPolicyMcpServers = (cfg: object) =>
const scanPolicyIngress = (cfg: object) =>
collectPolicyEvidence(cfg as Record<string, unknown>).ingress ?? [];
async function writePolicyFixture(...json: Parameters<typeof JSON.stringify>): Promise<string> {
const [policy] = json;
const configPath = join(workspaceDir, "openclaw.jsonc");
await fs.writeFile(configPath, "{}", "utf-8");
await fs.writeFile(
join(workspaceDir, "policy.jsonc"),
typeof policy === "string" ? policy : JSON.stringify(...json),
"utf-8",
);
return configPath;
}
function writeModelPolicyFixture(providers: object): Promise<string> {
return writePolicyFixture({ models: { providers } });
}
@@ -51,9 +40,9 @@ function writeIngressPolicyFixture(ingress: object): Promise<string> {
}
describe("registerPolicyDoctorChecks", () => {
beforeEach(describe0BeforeEach0);
beforeEach(setupPolicyDoctorTest);
afterEach(describe0AfterEach1);
afterEach(teardownPolicyDoctorTest);
it("repairs required agent workspace deny tool findings", async () => {
const cfg = {
File diff suppressed because it is too large Load Diff
@@ -32,6 +32,20 @@ export function cfgWithPolicy(settings: Record<string, unknown> = {}): OpenClawC
};
}
export async function writePolicyFixture(
...json: Parameters<typeof JSON.stringify>
): Promise<string> {
const [policy] = json;
const configPath = join(workspaceDir, "openclaw.jsonc");
await fs.writeFile(configPath, "{}", "utf-8");
await fs.writeFile(
join(workspaceDir, "policy.jsonc"),
typeof policy === "string" ? policy : JSON.stringify(...json),
"utf-8",
);
return configPath;
}
export function ctx(configPath: string, cfg: OpenClawConfig = {}): HealthCheckContext {
return {
mode: "lint",
@@ -74,6 +88,13 @@ export async function runPolicyChecks(checkCtx: HealthCheckContext): Promise<{
return { findings };
}
export async function runPolicyChecksFixture(
policy: unknown,
cfg: OpenClawConfig = cfgWithPolicy(),
) {
return runPolicyChecks(ctx(await writePolicyFixture(policy), cfg));
}
export async function runPolicyDoctorLint(checkCtx: HealthCheckContext) {
return runDoctorLintChecks(checkCtx, { checks: registerChecks() });
}
@@ -103,7 +124,7 @@ export async function runPolicyRepairCheck(checkId: string, repairCheckCtx: Heal
return { ...result, findings, config, remainingFindings };
}
export const describe0BeforeEach0 = async () => {
export const setupPolicyDoctorTest = async () => {
clearHealthChecksForTest();
originalOpenClawHome = process.env.OPENCLAW_HOME;
originalOpenClawStateDir = process.env.OPENCLAW_STATE_DIR;
@@ -125,7 +146,7 @@ export const describe0BeforeEach0 = async () => {
}
};
export const describe0AfterEach1 = async () => {
export const teardownPolicyDoctorTest = async () => {
if (originalOpenClawHome === undefined) {
delete process.env.OPENCLAW_HOME;
} else {