summaryrefslogtreecommitdiffhomepage
path: root/src/features/surface-host/logic/plan.test.ts
blob: 50d6f118d7c458cc5a44d389e6f59cbf1a19b962 (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import type { SurfaceField, SurfaceSpec } from "@dispatch/ui-contract";
import { describe, expect, it } from "vitest";
import { buildInvoke, planSurface } from "./plan";

const makeSpec = (...fields: SurfaceField[]): SurfaceSpec => ({
	id: "test-surface",
	region: "test",
	title: "Test Surface",
	fields,
});

describe("planSurface", () => {
	it("maps a toggle field to a ToggleFieldView", () => {
		const plan = planSurface(
			makeSpec({ kind: "toggle", label: "Dark mode", value: true, action: { actionId: "dm" } }),
		);
		expect(plan.fields).toEqual([
			{ kind: "toggle", label: "Dark mode", value: true, action: { actionId: "dm" } },
		]);
	});

	it("maps a progress field to a ProgressFieldView", () => {
		const plan = planSurface(makeSpec({ kind: "progress", label: "Loading", value: 0.42 }));
		expect(plan.fields).toEqual([{ kind: "progress", label: "Loading", value: 0.42 }]);
	});

	it("maps a selector field to a SelectorFieldView", () => {
		const plan = planSurface(
			makeSpec({
				kind: "selector",
				label: "Model",
				value: "gpt-4",
				options: [
					{ value: "gpt-4", label: "GPT-4" },
					{ value: "gpt-3.5", label: "GPT-3.5" },
				],
				action: { actionId: "set-model" },
			}),
		);
		expect(plan.fields).toEqual([
			{
				kind: "selector",
				label: "Model",
				value: "gpt-4",
				options: [
					{ value: "gpt-4", label: "GPT-4" },
					{ value: "gpt-3.5", label: "GPT-3.5" },
				],
				action: { actionId: "set-model" },
			},
		]);
	});

	it("maps a stat field to a StatFieldView", () => {
		const plan = planSurface(makeSpec({ kind: "stat", label: "Tokens", value: "1,234" }));
		expect(plan.fields).toEqual([{ kind: "stat", label: "Tokens", value: "1,234" }]);
	});

	it("maps a button field to a ButtonFieldView", () => {
		const plan = planSurface(
			makeSpec({ kind: "button", label: "Retry", action: { actionId: "retry" } }),
		);
		expect(plan.fields).toEqual([
			{ kind: "button", label: "Retry", action: { actionId: "retry" } },
		]);
	});

	it("preserves field order", () => {
		const plan = planSurface(
			makeSpec(
				{ kind: "stat", label: "A", value: "1" },
				{ kind: "toggle", label: "B", value: false, action: { actionId: "b" } },
				{ kind: "progress", label: "C", value: 0.5 },
				{ kind: "button", label: "D", action: { actionId: "d" } },
			),
		);
		expect(plan.fields.map((f) => f.label)).toEqual(["A", "B", "C", "D"]);
	});

	it("drops unknown field kinds gracefully", () => {
		const plan = planSurface(
			makeSpec({ kind: "stat", label: "Known", value: "ok" }, {
				kind: "future-kind" as "stat",
				label: "Unknown",
				value: "?",
			} as SurfaceField),
		);
		expect(plan.fields).toHaveLength(1);
		expect(plan.fields[0]?.label).toBe("Known");
	});

	it("drops custom fields (no renderer registered)", () => {
		const plan = planSurface(
			makeSpec(
				{ kind: "stat", label: "Before", value: "1" },
				{ kind: "custom", rendererId: "chart", payload: { data: [1, 2, 3] } },
				{ kind: "stat", label: "After", value: "2" },
			),
		);
		expect(plan.fields).toHaveLength(2);
		expect(plan.fields.map((f) => f.label)).toEqual(["Before", "After"]);
	});

	it("returns empty fields for an empty spec", () => {
		const plan = planSurface(makeSpec());
		expect(plan.fields).toEqual([]);
	});

	it("drops all fields when all are custom", () => {
		const plan = planSurface(
			makeSpec(
				{ kind: "custom", rendererId: "x", payload: null },
				{ kind: "custom", rendererId: "y", payload: 42 },
			),
		);
		expect(plan.fields).toEqual([]);
	});
});

describe("buildInvoke", () => {
	it("builds an invoke message for a toggle field", () => {
		const field = { kind: "toggle" as const, label: "T", value: false, action: { actionId: "t" } };
		const msg = buildInvoke("s1", field, true);
		expect(msg).toEqual({ type: "invoke", surfaceId: "s1", actionId: "t", payload: true });
	});

	it("builds an invoke message for a selector field", () => {
		const field = {
			kind: "selector" as const,
			label: "S",
			value: "a",
			options: [],
			action: { actionId: "sel" },
		};
		const msg = buildInvoke("s1", field, "b");
		expect(msg).toEqual({ type: "invoke", surfaceId: "s1", actionId: "sel", payload: "b" });
	});

	it("builds an invoke message without payload for a button field", () => {
		const field = { kind: "button" as const, label: "B", action: { actionId: "btn" } };
		const msg = buildInvoke("s1", field);
		expect(msg).toEqual({ type: "invoke", surfaceId: "s1", actionId: "btn" });
	});

	it("omits payload key when value is undefined", () => {
		const field = { kind: "button" as const, label: "B", action: { actionId: "btn" } };
		const msg = buildInvoke("s1", field, undefined);
		expect(msg).not.toHaveProperty("payload");
	});

	it("uses the field's actionId, not a surface-level id", () => {
		const field = {
			kind: "toggle" as const,
			label: "X",
			value: true,
			action: { actionId: "custom-action-123" },
		};
		const msg = buildInvoke("surf", field, false);
		expect(msg.actionId).toBe("custom-action-123");
	});
});