test: share legacy config schema assertions

This commit is contained in:
Peter Steinberger
2026-04-19 00:13:50 +01:00
parent 91bb931b0f
commit f8f9f13e0d
3 changed files with 55 additions and 77 deletions

View File

@@ -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([

View File

@@ -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([
{

View File

@@ -0,0 +1,44 @@
import { expect } from "vitest";
type SchemaParseResult<TData = unknown> =
| { 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);
}
}
}