Add DisclawConfigSchema and AgentYamlSchema with Zod validation. loader.ts and identity.ts validate external data at parse time. Clear error messages with field name and expected type. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
84 lines
2.5 KiB
TypeScript
84 lines
2.5 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { ZodError } from "zod";
|
|
import { DisclawConfigSchema } from "../../src/config/schema";
|
|
import { AgentYamlSchema } from "../../src/agent/schema";
|
|
|
|
describe("DisclawConfigSchema", () => {
|
|
it("(a) parses valid disclaw.yaml data correctly", () => {
|
|
const input = {
|
|
workspaces_root: "/home/user/.disclaw/workspaces",
|
|
management_channel: "disclaw",
|
|
claude_command: "claude",
|
|
claude_timeout_seconds: 60,
|
|
max_concurrent_agents: 2,
|
|
env_blocklist: ["SECRET_KEY"],
|
|
};
|
|
|
|
const result = DisclawConfigSchema.parse(input);
|
|
|
|
expect(result.workspaces_root).toBe("/home/user/.disclaw/workspaces");
|
|
expect(result.management_channel).toBe("disclaw");
|
|
expect(result.claude_command).toBe("claude");
|
|
expect(result.claude_timeout_seconds).toBe(60);
|
|
expect(result.max_concurrent_agents).toBe(2);
|
|
expect(result.env_blocklist).toEqual(["SECRET_KEY"]);
|
|
});
|
|
|
|
it("(b) throws ZodError when workspaces_root is missing", () => {
|
|
const input = {
|
|
management_channel: "disclaw",
|
|
};
|
|
|
|
expect(() => DisclawConfigSchema.parse(input)).toThrow(ZodError);
|
|
});
|
|
|
|
it("(c) throws ZodError when claude_timeout_seconds is a string", () => {
|
|
const input = {
|
|
workspaces_root: "/some/path",
|
|
claude_timeout_seconds: "abc",
|
|
};
|
|
|
|
expect(() => DisclawConfigSchema.parse(input)).toThrow(ZodError);
|
|
});
|
|
|
|
it("(d) applies defaults when optional fields are not provided", () => {
|
|
const input = {
|
|
workspaces_root: "/some/path",
|
|
};
|
|
|
|
const result = DisclawConfigSchema.parse(input);
|
|
|
|
expect(result.management_channel).toBe("disclaw");
|
|
expect(result.claude_command).toBe("claude");
|
|
expect(result.claude_timeout_seconds).toBe(120);
|
|
expect(result.max_concurrent_agents).toBe(4);
|
|
expect(result.env_blocklist).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("AgentYamlSchema", () => {
|
|
it("(e) parses valid agent.yaml data correctly", () => {
|
|
const input = {
|
|
name: "my-agent",
|
|
display_name: "My Agent",
|
|
role: "Backend developer",
|
|
channel_id: "1234567890",
|
|
};
|
|
|
|
const result = AgentYamlSchema.parse(input);
|
|
|
|
expect(result.name).toBe("my-agent");
|
|
expect(result.display_name).toBe("My Agent");
|
|
expect(result.role).toBe("Backend developer");
|
|
expect(result.channel_id).toBe("1234567890");
|
|
});
|
|
|
|
it("(f) throws ZodError when channel_id is missing", () => {
|
|
const input = {
|
|
name: "my-agent",
|
|
role: "Backend developer",
|
|
};
|
|
|
|
expect(() => AgentYamlSchema.parse(input)).toThrow(ZodError);
|
|
});
|
|
});
|