|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + REDACTED_SENTINEL, |
| 4 | + redactConfigSnapshot, |
| 5 | + restoreRedactedValues as restoreRedactedValues_orig, |
| 6 | +} from "./redact-snapshot.js"; |
| 7 | +import { __test__ } from "./schema.hints.js"; |
| 8 | +import type { ConfigUiHints } from "./schema.js"; |
| 9 | +import type { ConfigFileSnapshot } from "./types.openclaw.js"; |
| 10 | +import { OpenClawSchema } from "./zod-schema.js"; |
| 11 | + |
| 12 | +const { mapSensitivePaths } = __test__; |
| 13 | +const mainSchemaHints = mapSensitivePaths(OpenClawSchema, "", {}); |
| 14 | + |
| 15 | +type TestSnapshot<TConfig extends Record<string, unknown>> = ConfigFileSnapshot & { |
| 16 | + parsed: TConfig; |
| 17 | + resolved: TConfig; |
| 18 | + config: TConfig; |
| 19 | +}; |
| 20 | + |
| 21 | +function makeSnapshot<TConfig extends Record<string, unknown>>( |
| 22 | + config: TConfig, |
| 23 | + raw?: string, |
| 24 | +): TestSnapshot<TConfig> { |
| 25 | + return { |
| 26 | + path: "/home/user/.openclaw/config.json5", |
| 27 | + exists: true, |
| 28 | + raw: raw ?? JSON.stringify(config), |
| 29 | + parsed: config, |
| 30 | + resolved: config as ConfigFileSnapshot["resolved"], |
| 31 | + valid: true, |
| 32 | + config: config as ConfigFileSnapshot["config"], |
| 33 | + hash: "abc123", |
| 34 | + issues: [], |
| 35 | + warnings: [], |
| 36 | + legacyIssues: [], |
| 37 | + } as unknown as TestSnapshot<TConfig>; |
| 38 | +} |
| 39 | + |
| 40 | +function restoreRedactedValues<TOriginal>( |
| 41 | + incoming: unknown, |
| 42 | + original: TOriginal, |
| 43 | + hints?: ConfigUiHints, |
| 44 | +): TOriginal { |
| 45 | + const result = restoreRedactedValues_orig(incoming, original, hints); |
| 46 | + expect(result.ok).toBe(true); |
| 47 | + return result.result as TOriginal; |
| 48 | +} |
| 49 | + |
| 50 | +describe("realredactConfigSnapshot_real", () => { |
| 51 | + it("main schema redact works (samples)", () => { |
| 52 | + const schema = OpenClawSchema.toJSONSchema({ |
| 53 | + target: "draft-07", |
| 54 | + unrepresentable: "any", |
| 55 | + }); |
| 56 | + schema.title = "OpenClawConfig"; |
| 57 | + const hints = mainSchemaHints; |
| 58 | + |
| 59 | + const snapshot = makeSnapshot({ |
| 60 | + agents: { |
| 61 | + defaults: { |
| 62 | + memorySearch: { |
| 63 | + remote: { |
| 64 | + apiKey: "1234", |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + list: [ |
| 69 | + { |
| 70 | + memorySearch: { |
| 71 | + remote: { |
| 72 | + apiKey: "6789", |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | + const result = redactConfigSnapshot(snapshot, hints); |
| 81 | + const config = result.config as typeof snapshot.config; |
| 82 | + expect(config.agents.defaults.memorySearch.remote.apiKey).toBe(REDACTED_SENTINEL); |
| 83 | + expect(config.agents.list[0].memorySearch.remote.apiKey).toBe(REDACTED_SENTINEL); |
| 84 | + const restored = restoreRedactedValues(result.config, snapshot.config, hints); |
| 85 | + expect(restored.agents.defaults.memorySearch.remote.apiKey).toBe("1234"); |
| 86 | + expect(restored.agents.list[0].memorySearch.remote.apiKey).toBe("6789"); |
| 87 | + }); |
| 88 | +}); |
0 commit comments