|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { |
| 6 | + configAuditScrubToHealthFinding, |
| 7 | + configAuditScrubToRepairEffect, |
| 8 | + detectConfigAuditScrubIssue, |
| 9 | +} from "./doctor-config-audit-scrub.js"; |
| 10 | + |
| 11 | +let tempRoot: string | null = null; |
| 12 | + |
| 13 | +async function makeHome(): Promise<string> { |
| 14 | + tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-doctor-config-audit-")); |
| 15 | + return tempRoot; |
| 16 | +} |
| 17 | + |
| 18 | +afterEach(async () => { |
| 19 | + if (tempRoot !== null) { |
| 20 | + await fs.rm(tempRoot, { recursive: true, force: true }); |
| 21 | + tempRoot = null; |
| 22 | + } |
| 23 | +}); |
| 24 | + |
| 25 | +describe("detectConfigAuditScrubIssue", () => { |
| 26 | + it("detects config-audit scrub work without rewriting the log", async () => { |
| 27 | + const home = await makeHome(); |
| 28 | + const auditPath = path.join(home, ".openclaw", "logs", "config-audit.jsonl"); |
| 29 | + await fs.mkdir(path.dirname(auditPath), { recursive: true, mode: 0o700 }); |
| 30 | + const record = { |
| 31 | + ts: "2026-05-02T00:03:48.471Z", |
| 32 | + argv: ["node", "openclaw.mjs", "config", "set", "x", "xoxb-bad-token-1234567890abcdef"], |
| 33 | + execArgv: [], |
| 34 | + }; |
| 35 | + await fs.writeFile(auditPath, `${JSON.stringify(record)}\n`, { encoding: "utf8", mode: 0o600 }); |
| 36 | + |
| 37 | + const result = await detectConfigAuditScrubIssue({ |
| 38 | + env: {} as NodeJS.ProcessEnv, |
| 39 | + homedir: () => home, |
| 40 | + }); |
| 41 | + |
| 42 | + expect(result).toEqual({ |
| 43 | + scanned: 1, |
| 44 | + rewritten: 1, |
| 45 | + skipped: 0, |
| 46 | + aborted: false, |
| 47 | + auditPath, |
| 48 | + }); |
| 49 | + expect(await fs.readFile(auditPath, "utf8")).toBe(`${JSON.stringify(record)}\n`); |
| 50 | + }); |
| 51 | + |
| 52 | + it("maps scrub work to structured findings and dry-run effects", async () => { |
| 53 | + const home = await makeHome(); |
| 54 | + const auditPath = path.join(home, ".openclaw", "logs", "config-audit.jsonl"); |
| 55 | + const result = { scanned: 2, rewritten: 1, skipped: 0, aborted: false, auditPath }; |
| 56 | + |
| 57 | + expect(configAuditScrubToHealthFinding(result)).toEqual( |
| 58 | + expect.objectContaining({ |
| 59 | + checkId: "core/doctor/config-audit-scrub", |
| 60 | + severity: "warning", |
| 61 | + path: auditPath, |
| 62 | + }), |
| 63 | + ); |
| 64 | + expect(configAuditScrubToRepairEffect(result)).toEqual({ |
| 65 | + kind: "file", |
| 66 | + action: "would-scrub-config-audit-log", |
| 67 | + target: auditPath, |
| 68 | + dryRunSafe: false, |
| 69 | + }); |
| 70 | + }); |
| 71 | +}); |
0 commit comments