|
| 1 | +// Covers config journal snapshot fingerprints, slot ownership, and restoration. |
| 2 | +import fs from "node:fs"; |
| 3 | +import { promises as fsPromises } from "node:fs"; |
| 4 | +import path from "node:path"; |
| 5 | +import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest"; |
| 6 | +import { resetPluginStateStoreForTests } from "../plugin-state/plugin-state-store.js"; |
| 7 | +import { createSuiteTempRootTracker } from "../test-helpers/temp-dir.js"; |
| 8 | +import { |
| 9 | + fingerprintConfigSnapshotAuthoredConfig, |
| 10 | + readConfigSnapshotAuditRecord, |
| 11 | + readLatestConfigSnapshotAuditRecord, |
| 12 | + restoreConfigSnapshotAuditRecord, |
| 13 | + upsertConfigSnapshotAuditRecord, |
| 14 | +} from "./config-journal-snapshot.js"; |
| 15 | + |
| 16 | +describe("config journal snapshots", () => { |
| 17 | + const suiteRootTracker = createSuiteTempRootTracker({ |
| 18 | + prefix: "openclaw-config-journal-snapshot-", |
| 19 | + }); |
| 20 | + |
| 21 | + beforeAll(async () => { |
| 22 | + await suiteRootTracker.setup(); |
| 23 | + }); |
| 24 | + |
| 25 | + it("fingerprints every snapshot leaf with a per-install key", async () => { |
| 26 | + const home = await suiteRootTracker.make("fingerprint-home"); |
| 27 | + const stateDir = path.join(home, ".openclaw"); |
| 28 | + const fingerprinted = fingerprintConfigSnapshotAuthoredConfig( |
| 29 | + { |
| 30 | + gateway: { auth: { token: "test-token" }, port: 18789 }, |
| 31 | + }, |
| 32 | + { |
| 33 | + env: { OPENCLAW_STATE_DIR: stateDir } as NodeJS.ProcessEnv, |
| 34 | + homedir: () => home, |
| 35 | + }, |
| 36 | + ); |
| 37 | + |
| 38 | + expect(fingerprinted).toMatchObject({ |
| 39 | + gateway: { |
| 40 | + auth: { token: expect.stringMatching(/^fp:[0-9a-f]{12}$/) }, |
| 41 | + port: expect.stringMatching(/^fp:[0-9a-f]{12}$/), |
| 42 | + }, |
| 43 | + }); |
| 44 | + expect(JSON.stringify(fingerprinted)).not.toContain("test-token"); |
| 45 | + expect(fs.statSync(path.join(stateDir, "config-journal-fingerprint.key")).mode & 0o777).toBe( |
| 46 | + 0o600, |
| 47 | + ); |
| 48 | + |
| 49 | + // Type-only edits must change the fingerprint: "1" vs 1 vs true vs "true". |
| 50 | + const context = { |
| 51 | + env: { OPENCLAW_STATE_DIR: stateDir } as NodeJS.ProcessEnv, |
| 52 | + homedir: () => home, |
| 53 | + }; |
| 54 | + const fingerprintOf = (value: unknown) => |
| 55 | + (fingerprintConfigSnapshotAuthoredConfig({ leaf: value }, context) as { leaf: string }).leaf; |
| 56 | + const variants = [ |
| 57 | + fingerprintOf("1"), |
| 58 | + fingerprintOf(1), |
| 59 | + fingerprintOf(true), |
| 60 | + fingerprintOf("true"), |
| 61 | + ]; |
| 62 | + expect(new Set(variants).size).toBe(variants.length); |
| 63 | + expect(fingerprintOf("1")).toBe(fingerprintOf("1")); |
| 64 | + }); |
| 65 | + |
| 66 | + it("hands the snapshot slot to another config path via the unfiltered CAS token", async () => { |
| 67 | + const home = await suiteRootTracker.make("snapshot-path-transfer"); |
| 68 | + const env = { OPENCLAW_STATE_DIR: path.join(home, ".openclaw") } as NodeJS.ProcessEnv; |
| 69 | + const context = { env, homedir: () => home }; |
| 70 | + const pathA = path.join(home, ".openclaw", "config-a.json"); |
| 71 | + const pathB = path.join(home, ".openclaw", "config-b.json"); |
| 72 | + upsertConfigSnapshotAuditRecord({ |
| 73 | + ...context, |
| 74 | + configPath: pathA, |
| 75 | + rawHash: "path-a-hash", |
| 76 | + authoredConfig: { gateway: { port: 1 } }, |
| 77 | + }); |
| 78 | + // Path-filtered read for B sees nothing, but the unfiltered slot is the |
| 79 | + // CAS token that lets B take the slot over from A. |
| 80 | + expect(readConfigSnapshotAuditRecord({ ...context, configPath: pathB })).toBeNull(); |
| 81 | + const foreign = readLatestConfigSnapshotAuditRecord(context); |
| 82 | + expect(foreign?.configPath).toBe(path.resolve(pathA)); |
| 83 | + const taken = upsertConfigSnapshotAuditRecord({ |
| 84 | + ...context, |
| 85 | + configPath: pathB, |
| 86 | + rawHash: "path-b-hash", |
| 87 | + authoredConfig: { gateway: { port: 2 } }, |
| 88 | + expectedSnapshot: foreign, |
| 89 | + }); |
| 90 | + expect(taken?.configPath).toBe(path.resolve(pathB)); |
| 91 | + expect(readConfigSnapshotAuditRecord({ ...context, configPath: pathB })?.rawHash).toBe( |
| 92 | + "path-b-hash", |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it("does not restore a snapshot slot after another writer replaces it", async () => { |
| 97 | + const home = await suiteRootTracker.make("snapshot-compare-and-set"); |
| 98 | + const configPath = path.join(home, ".openclaw", "openclaw.json"); |
| 99 | + const env = { OPENCLAW_STATE_DIR: path.join(home, ".openclaw") } as NodeJS.ProcessEnv; |
| 100 | + const context = { env, homedir: () => home }; |
| 101 | + const prior = upsertConfigSnapshotAuditRecord({ |
| 102 | + ...context, |
| 103 | + configPath, |
| 104 | + rawHash: "prior", |
| 105 | + authoredConfig: { gateway: { port: 18789 } }, |
| 106 | + }); |
| 107 | + const written = upsertConfigSnapshotAuditRecord({ |
| 108 | + ...context, |
| 109 | + configPath, |
| 110 | + rawHash: "written", |
| 111 | + authoredConfig: { gateway: { port: 18790 } }, |
| 112 | + }); |
| 113 | + upsertConfigSnapshotAuditRecord({ |
| 114 | + ...context, |
| 115 | + configPath, |
| 116 | + rawHash: "newer-process", |
| 117 | + authoredConfig: { gateway: { port: 18791 } }, |
| 118 | + }); |
| 119 | + |
| 120 | + restoreConfigSnapshotAuditRecord({ |
| 121 | + ...context, |
| 122 | + snapshot: prior, |
| 123 | + expectedSnapshot: written, |
| 124 | + }); |
| 125 | + |
| 126 | + expect(readConfigSnapshotAuditRecord({ ...context, configPath })).toMatchObject({ |
| 127 | + rawHash: "newer-process", |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + it("falls back to a redaction marker when the fingerprint key cannot be stored", async () => { |
| 132 | + const home = await suiteRootTracker.make("fingerprint-fallback"); |
| 133 | + const statePath = path.join(home, "state-file"); |
| 134 | + await fsPromises.writeFile(statePath, "not a directory", "utf-8"); |
| 135 | + |
| 136 | + expect( |
| 137 | + fingerprintConfigSnapshotAuthoredConfig( |
| 138 | + { gateway: { auth: { token: "test-token" } } }, |
| 139 | + { env: { OPENCLAW_STATE_DIR: statePath } as NodeJS.ProcessEnv, homedir: () => home }, |
| 140 | + ), |
| 141 | + ).toEqual({ gateway: { auth: { token: "***" } } }); |
| 142 | + }); |
| 143 | + |
| 144 | + afterAll(async () => { |
| 145 | + await suiteRootTracker.cleanup(); |
| 146 | + }); |
| 147 | + |
| 148 | + afterEach(() => { |
| 149 | + resetPluginStateStoreForTests(); |
| 150 | + }); |
| 151 | +}); |
0 commit comments