1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import { describe, expect, it } from "vitest";
import {
normalizeNamedAgent,
normalizeNamedAgentRecord,
normalizeNamedAgents,
toNamedAgentInput,
} from "./normalize";
import type { NamedAgent } from "./types";
describe("normalizeNamedAgent", () => {
it("coerces a well-formed record", () => {
expect(normalizeNamedAgent({ id: "x", name: "Coding", model: "opencode-1/claude" })).toEqual({
id: "x",
name: "Coding",
model: "opencode-1/claude",
});
});
it("rejects a record missing any field", () => {
expect(normalizeNamedAgent({ id: "x", name: "Coding" })).toBeNull();
expect(normalizeNamedAgent({ name: "Coding", model: "m" })).toBeNull();
expect(normalizeNamedAgent(null)).toBeNull();
});
});
describe("normalizeNamedAgents", () => {
it("coerces a list body", () => {
const agents: readonly NamedAgent[] = normalizeNamedAgents({
namedAgents: [
{ id: "a", name: "Coding", model: "opencode-1/claude" },
{ id: "b", name: "Research", model: "umans/glm" },
],
});
expect(agents).toHaveLength(2);
expect(agents[0]?.name).toBe("Coding");
});
it("drops malformed entries and survives a missing list key", () => {
expect(
normalizeNamedAgents({
namedAgents: [
{ id: "a", name: "x" },
{ id: "b", name: "y", model: "m" },
],
}).map((a) => a.id),
).toEqual(["b"]);
expect(normalizeNamedAgents({})).toEqual([]);
expect(normalizeNamedAgents(null)).toEqual([]);
});
});
describe("normalizeNamedAgentRecord", () => {
it("reads the canonical saved record", () => {
expect(
normalizeNamedAgentRecord({
namedAgent: { id: "a", name: "Coding", model: "opencode-1/claude" },
})?.id,
).toBe("a");
});
it("returns null for a malformed record", () => {
expect(normalizeNamedAgentRecord({ namedAgent: {} })).toBeNull();
expect(normalizeNamedAgentRecord({})).toBeNull();
});
});
describe("toNamedAgentInput", () => {
it("trims a name and keeps the model", () => {
expect(toNamedAgentInput(" Coding ", "opencode-1/claude")).toEqual({
name: "Coding",
model: "opencode-1/claude",
});
});
it("defaults non-strings to empty", () => {
expect(toNamedAgentInput(null, 42)).toEqual({ name: "", model: "" });
});
});
|