summaryrefslogtreecommitdiffhomepage
path: root/src/core/protocol/reducer.test.ts
blob: 57e12f2d483640c5ce720fb7da6599b0f686ab37 (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
import { describe, expect, it } from "vitest";
import { applyServerMessage, initialState, invoke, subscribe, unsubscribe } from "./reducer";

const makeSpec = (id: string, title = id) => ({
	id,
	region: "test",
	title,
	fields: [],
});

describe("initialState", () => {
	it("returns empty catalog, no subscriptions, no error", () => {
		const s = initialState();
		expect(s.catalog).toEqual([]);
		expect(s.subscriptions.size).toBe(0);
		expect(s.lastError).toBeNull();
	});
});

describe("applyServerMessage — catalog", () => {
	it("replaces the catalog", () => {
		const s = initialState();
		const catalog = [
			{ id: "a", region: "r", title: "A" },
			{ id: "b", region: "r", title: "B" },
		];
		const next = applyServerMessage(s, { type: "catalog", catalog });
		expect(next.catalog).toEqual(catalog);
	});
});

describe("applyServerMessage — surface", () => {
	it("sets the spec for a subscribed surface", () => {
		let s = initialState();
		const result = subscribe(s, "s1");
		s = result.state;
		const spec = makeSpec("s1", "Surface 1");
		const next = applyServerMessage(s, { type: "surface", spec });
		expect(next.subscriptions.get("s1")).toEqual(spec);
	});

	it("ignores a surface message for a non-subscribed surface", () => {
		const s = initialState();
		const spec = makeSpec("unknown");
		const next = applyServerMessage(s, { type: "surface", spec });
		expect(next.subscriptions.has("unknown")).toBe(false);
	});
});

describe("applyServerMessage — update", () => {
	it("replaces spec for a subscribed surface", () => {
		let s = initialState();
		s = subscribe(s, "s1").state;
		s = applyServerMessage(s, { type: "surface", spec: makeSpec("s1", "V1") });
		const next = applyServerMessage(s, {
			type: "update",
			update: { surfaceId: "s1", spec: makeSpec("s1", "V2") },
		});
		expect(next.subscriptions.get("s1")?.title).toBe("V2");
	});

	it("ignores an update for a non-subscribed surface", () => {
		const s = initialState();
		const next = applyServerMessage(s, {
			type: "update",
			update: { surfaceId: "nope", spec: makeSpec("nope") },
		});
		expect(next.subscriptions.has("nope")).toBe(false);
	});
});

describe("applyServerMessage — error", () => {
	it("records the error without throwing", () => {
		const s = initialState();
		const err = { type: "error" as const, surfaceId: "s1", message: "boom" };
		const next = applyServerMessage(s, err);
		expect(next.lastError).toEqual(err);
	});

	it("records error without surfaceId", () => {
		const s = initialState();
		const err = { type: "error" as const, message: "global boom" };
		const next = applyServerMessage(s, err);
		expect(next.lastError).toEqual(err);
	});
});

describe("subscribe", () => {
	it("emits exactly one subscribe message", () => {
		const s = initialState();
		const result = subscribe(s, "s1");
		expect(result.outgoing).toEqual([{ type: "subscribe", surfaceId: "s1" }]);
		expect(result.outgoing).toHaveLength(1);
	});

	it("adds the surface to subscriptions with null spec", () => {
		const s = initialState();
		const result = subscribe(s, "s1");
		expect(result.state.subscriptions.get("s1")).toBeNull();
	});

	it("is idempotent — second subscribe is a no-op", () => {
		let s = initialState();
		s = subscribe(s, "s1").state;
		const result = subscribe(s, "s1");
		expect(result.outgoing).toEqual([]);
		expect(result.state).toBe(s);
	});
});

describe("unsubscribe", () => {
	it("emits unsubscribe and drops the spec", () => {
		let s = initialState();
		s = subscribe(s, "s1").state;
		s = applyServerMessage(s, { type: "surface", spec: makeSpec("s1") });
		const result = unsubscribe(s, "s1");
		expect(result.outgoing).toEqual([{ type: "unsubscribe", surfaceId: "s1" }]);
		expect(result.state.subscriptions.has("s1")).toBe(false);
	});

	it("is a no-op if not subscribed", () => {
		const s = initialState();
		const result = unsubscribe(s, "nope");
		expect(result.outgoing).toEqual([]);
		expect(result.state).toBe(s);
	});
});

describe("invoke", () => {
	it("emits the correct InvokeMessage", () => {
		const s = initialState();
		const result = invoke(s, "s1", "toggle", true);
		expect(result.outgoing).toEqual([
			{ type: "invoke", surfaceId: "s1", actionId: "toggle", payload: true },
		]);
	});

	it("omits payload when not provided", () => {
		const s = initialState();
		const result = invoke(s, "s1", "click");
		expect(result.outgoing).toEqual([
			{ type: "invoke", surfaceId: "s1", actionId: "click", payload: undefined },
		]);
	});

	it("does not mutate state", () => {
		const s = initialState();
		const result = invoke(s, "s1", "a1");
		expect(result.state).toBe(s);
	});
});