From a799fd7ca79120f6a03a29bd56f6763ddeb285e4 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 7 Apr 2026 09:31:52 +0100 Subject: [PATCH] test: accept flattened config audit records --- src/config/io.audit.test.ts | 55 +++++++++++++++++++++++++++++++++++++ src/config/io.audit.ts | 19 +++++++++---- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/src/config/io.audit.test.ts b/src/config/io.audit.test.ts index 71798386d7f..24ae28224d0 100644 --- a/src/config/io.audit.test.ts +++ b/src/config/io.audit.test.ts @@ -201,4 +201,59 @@ describe("config io audit helpers", () => { nextHash: "next-hash", }); }); + + it("also accepts flattened audit record params from legacy call sites", async () => { + const home = await suiteRootTracker.make("append-flat"); + const record = finalizeConfigWriteAuditRecord({ + base: createConfigWriteAuditRecordBase({ + configPath: path.join(home, ".openclaw", "openclaw.json"), + env: {} as NodeJS.ProcessEnv, + existsBefore: true, + previousHash: "prev-hash", + nextHash: "next-hash", + previousBytes: 12, + nextBytes: 24, + previousMetadata: { + dev: "10", + ino: "11", + mode: 0o600, + nlink: 1, + uid: 501, + gid: 20, + }, + changedPathCount: 1, + hasMetaBefore: true, + hasMetaAfter: true, + gatewayModeBefore: "local", + gatewayModeAfter: "local", + suspicious: [], + now: "2026-04-07T08:00:00.000Z", + }), + result: "rename", + nextMetadata: { + dev: "12", + ino: "13", + mode: 0o600, + nlink: 1, + uid: 501, + gid: 20, + }, + }); + + await appendConfigAuditRecord({ + fs, + env: {} as NodeJS.ProcessEnv, + homedir: () => home, + ...record, + }); + + const auditPath = path.join(home, ".openclaw", "logs", "config-audit.jsonl"); + const lines = fs.readFileSync(auditPath, "utf-8").trim().split("\n"); + expect(lines).toHaveLength(1); + expect(JSON.parse(lines[0])).toMatchObject({ + event: "config.write", + result: "rename", + nextHash: "next-hash", + }); + }); }); diff --git a/src/config/io.audit.ts b/src/config/io.audit.ts index 36a01e315a4..d2875f9a70e 100644 --- a/src/config/io.audit.ts +++ b/src/config/io.audit.ts @@ -143,6 +143,19 @@ type ConfigAuditFs = { ): unknown; }; +type ConfigAuditAppendParams = + | { + fs: ConfigAuditFs; + env: NodeJS.ProcessEnv; + homedir: () => string; + record: ConfigAuditRecord; + } + | ({ + fs: ConfigAuditFs; + env: NodeJS.ProcessEnv; + homedir: () => string; + } & ConfigAuditRecord); + function normalizeAuditLabel(value: string | undefined): string | null { if (typeof value !== "string") { return null; @@ -219,17 +232,11 @@ export function createConfigWriteAuditRecordBase(params: { previousBytes: params.previousBytes, nextBytes: params.nextBytes, previousDev: params.previousMetadata.dev, - nextDev: null, previousIno: params.previousMetadata.ino, - nextIno: null, previousMode: params.previousMetadata.mode, - nextMode: null, previousNlink: params.previousMetadata.nlink, - nextNlink: null, previousUid: params.previousMetadata.uid, - nextUid: null, previousGid: params.previousMetadata.gid, - nextGid: null, changedPathCount: typeof params.changedPathCount === "number" ? params.changedPathCount : null, hasMetaBefore: params.hasMetaBefore, hasMetaAfter: params.hasMetaAfter,