From f8f9f13e0d5cef08d6cfbb2524aa3dc9deb17b3a Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 19 Apr 2026 00:13:50 +0100 Subject: [PATCH] test: share legacy config schema assertions --- ...etection.accepts-imessage-dmpolicy.test.ts | 49 +++---------------- ...etection.rejects-routing-allowfrom.test.ts | 39 ++------------- .../legacy-config-detection.test-support.ts | 44 +++++++++++++++++ 3 files changed, 55 insertions(+), 77 deletions(-) create mode 100644 src/config/legacy-config-detection.test-support.ts diff --git a/src/config/config.legacy-config-detection.accepts-imessage-dmpolicy.test.ts b/src/config/config.legacy-config-detection.accepts-imessage-dmpolicy.test.ts index 9639a5b096b..00605d13edf 100644 --- a/src/config/config.legacy-config-detection.accepts-imessage-dmpolicy.test.ts +++ b/src/config/config.legacy-config-detection.accepts-imessage-dmpolicy.test.ts @@ -1,4 +1,9 @@ import { describe, expect, it } from "vitest"; +import { + expectSchemaConfigValue, + expectSchemaValid, + expectSchemaValidationIssue, +} from "./legacy-config-detection.test-support.js"; import { AudioSchema, BindingsSchema } from "./zod-schema.agents.js"; import { OpenClawSchema } from "./zod-schema.js"; import { @@ -8,46 +13,6 @@ import { SlackConfigSchema, } from "./zod-schema.providers-core.js"; -function expectSchemaConfigValue(params: { - schema: { safeParse: (value: unknown) => { success: true; data: unknown } | { success: false } }; - config: unknown; - readValue: (config: unknown) => unknown; - expectedValue: unknown; -}) { - const res = params.schema.safeParse(params.config); - expect(res.success).toBe(true); - if (!res.success) { - throw new Error("expected schema config to be valid"); - } - expect(params.readValue(res.data)).toBe(params.expectedValue); -} - -function expectSchemaValid( - schema: { - safeParse: (value: unknown) => { success: true } | { success: false }; - }, - config: unknown, -) { - const res = schema.safeParse(config); - expect(res.success).toBe(true); -} - -function expectInvalidSchemaIssuePath(params: { - schema: { - safeParse: ( - value: unknown, - ) => { success: true } | { success: false; error: { issues: Array<{ path: PropertyKey[] }> } }; - }; - config: unknown; - expectedPath: string; -}) { - const res = params.schema.safeParse(params.config); - expect(res.success).toBe(false); - if (!res.success) { - expect(res.error.issues[0]?.path.join(".")).toBe(params.expectedPath); - } -} - function expectOpenClawSchemaInvalidPreservesField(params: { config: unknown; readValue: (parsed: unknown) => unknown; @@ -121,7 +86,7 @@ describe("legacy config detection", () => { expectSchemaConfigValue({ schema, config, readValue, expectedValue }); }); it("rejects unsafe executable config values", () => { - expectInvalidSchemaIssuePath({ + expectSchemaValidationIssue({ schema: IMessageConfigSchema, config: { cliPath: "imsg; rm -rf /" }, expectedPath: "cliPath", @@ -163,7 +128,7 @@ describe("legacy config detection", () => { "allowFrom", ], ])("rejects: %s", (_name, schema, config, expectedPath) => { - expectInvalidSchemaIssuePath({ schema, config, expectedPath }); + expectSchemaValidationIssue({ schema, config, expectedPath }); }); it.each([ diff --git a/src/config/config.legacy-config-detection.rejects-routing-allowfrom.test.ts b/src/config/config.legacy-config-detection.rejects-routing-allowfrom.test.ts index f8c0a3f16b4..e31a504ff77 100644 --- a/src/config/config.legacy-config-detection.rejects-routing-allowfrom.test.ts +++ b/src/config/config.legacy-config-detection.rejects-routing-allowfrom.test.ts @@ -1,4 +1,8 @@ import { describe, expect, it } from "vitest"; +import { + expectSchemaConfigValue, + expectSchemaValidationIssue, +} from "./legacy-config-detection.test-support.js"; import { validateConfigObject } from "./validation.js"; import { DiscordConfigSchema, @@ -10,41 +14,6 @@ import { } from "./zod-schema.providers-core.js"; import { WhatsAppConfigSchema } from "./zod-schema.providers-whatsapp.js"; -function expectSchemaConfigValue(params: { - schema: { safeParse: (value: unknown) => { success: true; data: unknown } | { success: false } }; - config: unknown; - readValue: (config: unknown) => unknown; - expectedValue: unknown; -}) { - const res = params.schema.safeParse(params.config); - expect(res.success).toBe(true); - if (!res.success) { - throw new Error("expected schema config to be valid"); - } - expect(params.readValue(res.data)).toBe(params.expectedValue); -} - -function expectSchemaValidationIssue(params: { - schema: { - safeParse: ( - value: unknown, - ) => - | { success: true; data: unknown } - | { success: false; error: { issues: Array<{ path: PropertyKey[]; message: string }> } }; - }; - config: unknown; - expectedPath: string; - expectedMessage: string; -}) { - const res = params.schema.safeParse(params.config); - expect(res.success).toBe(false); - if (!res.success) { - const issue = res.error.issues[0]; - expect(issue?.path.join(".")).toBe(params.expectedPath); - expect(issue?.message).toContain(params.expectedMessage); - } -} - describe("legacy config detection", () => { it.each([ { diff --git a/src/config/legacy-config-detection.test-support.ts b/src/config/legacy-config-detection.test-support.ts new file mode 100644 index 00000000000..7a8107f47be --- /dev/null +++ b/src/config/legacy-config-detection.test-support.ts @@ -0,0 +1,44 @@ +import { expect } from "vitest"; + +type SchemaParseResult = + | { success: true; data: TData } + | { success: false; error: { issues: Array<{ path: PropertyKey[]; message?: string }> } }; + +export function expectSchemaConfigValue(params: { + schema: { safeParse: (value: unknown) => SchemaParseResult }; + config: unknown; + readValue: (config: unknown) => unknown; + expectedValue: unknown; +}) { + const res = params.schema.safeParse(params.config); + expect(res.success).toBe(true); + if (!res.success) { + throw new Error("expected schema config to be valid"); + } + expect(params.readValue(res.data)).toBe(params.expectedValue); +} + +export function expectSchemaValid( + schema: { safeParse: (value: unknown) => SchemaParseResult }, + config: unknown, +) { + const res = schema.safeParse(config); + expect(res.success).toBe(true); +} + +export function expectSchemaValidationIssue(params: { + schema: { safeParse: (value: unknown) => SchemaParseResult }; + config: unknown; + expectedPath: string; + expectedMessage?: string; +}) { + const res = params.schema.safeParse(params.config); + expect(res.success).toBe(false); + if (!res.success) { + const issue = res.error.issues[0]; + expect(issue?.path.join(".")).toBe(params.expectedPath); + if (params.expectedMessage !== undefined) { + expect(issue?.message).toContain(params.expectedMessage); + } + } +}