summaryrefslogtreecommitdiff
path: root/apps/web/src/actions/keybindings/__tests__/persistence.test.ts
blob: bcccc6a49f3d96866cf4e2a38140b863af300cfb (plain) (blame)
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
import {
	afterEach,
	beforeEach,
	describe,
	expect,
	mock,
	test,
} from "bun:test";
import {
	decodePersistedKeybindingsState,
	migratePersistedKeybindingsState,
	parseImportedKeybindings,
	serializeKeybindingsState,
} from "../persistence";

describe("keybinding persistence", () => {
	let warnSpy: ReturnType<typeof mock>;
	let originalWarn: typeof console.warn;

	beforeEach(() => {
		originalWarn = console.warn;
		warnSpy = mock(() => {});
		console.warn = warnSpy;
	});

	afterEach(() => {
		console.warn = originalWarn;
	});

	test("migrates legacy persisted keybindings before decoding them", () => {
		const migrated = migratePersistedKeybindingsState({
			state: {
				keybindings: {
					s: "split-selected",
					"ctrl+v": "paste-selected",
				},
				isCustomized: true,
			},
			fromVersion: 2,
		});

		const decoded = decodePersistedKeybindingsState({ state: migrated });
		expect(decoded).not.toBeNull();
		if (!decoded) throw new Error("Expected migrated keybindings to decode");

		expect(decoded.isCustomized).toBe(true);
		expect(decoded.keybindings.get("s")).toBe("split");
		expect(decoded.keybindings.get("ctrl+v")).toBe("paste-copied");
		expect(decoded.keybindings.get("escape")).toBe("cancel-interaction");
		expect(warnSpy).not.toHaveBeenCalled();
	});

	test("filters invalid persisted entries at the boundary and warns", () => {
		const decoded = decodePersistedKeybindingsState({
			state: {
				keybindings: {
					space: "toggle-play",
					"shift+bogus": "toggle-play",
					"ctrl+v": "not-an-action",
				},
				isCustomized: false,
			},
		});

		expect(decoded).not.toBeNull();
		if (!decoded) throw new Error("Expected persisted keybindings to decode");

		expect(Array.from(decoded.keybindings.entries())).toEqual([
			["space", "toggle-play"],
		]);
		expect(warnSpy).toHaveBeenCalledTimes(1);
	});

	test("returns null and warns when persisted shape is unrecognizable", () => {
		const decoded = decodePersistedKeybindingsState({ state: "garbage" });
		expect(decoded).toBeNull();
		expect(warnSpy).toHaveBeenCalledTimes(1);
	});

	test("round-trips actions that have no default shortcut", () => {
		// `stop-playback` is a valid `TActionWithOptionalArgs` but is not in the
		// defaults table; the validator must still accept it.
		const serialized = serializeKeybindingsState({
			keybindings: new Map([
				["space", "toggle-play"],
				["x", "stop-playback"],
				["b", "toggle-bookmark"],
			]),
			isCustomized: true,
		});

		const decoded = decodePersistedKeybindingsState({ state: serialized });
		expect(decoded).not.toBeNull();
		if (!decoded) throw new Error("Expected round-tripped keybindings");

		expect(decoded.keybindings.get("space")).toBe("toggle-play");
		expect(decoded.keybindings.get("x")).toBe("stop-playback");
		expect(decoded.keybindings.get("b")).toBe("toggle-bookmark");
		expect(warnSpy).not.toHaveBeenCalled();
	});
});

describe("parseImportedKeybindings", () => {
	test("accepts a valid configuration", () => {
		const result = parseImportedKeybindings({
			config: {
				space: "toggle-play",
				x: "stop-playback",
			},
		});

		expect(result.get("space")).toBe("toggle-play");
		expect(result.get("x")).toBe("stop-playback");
	});

	test("throws on non-object input", () => {
		expect(() => parseImportedKeybindings({ config: null })).toThrow(
			/JSON object/,
		);
		expect(() => parseImportedKeybindings({ config: [] })).toThrow(
			/JSON object/,
		);
	});

	test("throws on non-string action values", () => {
		expect(() =>
			parseImportedKeybindings({ config: { space: 42 } }),
		).toThrow(/expected string/);
	});

	test("throws on invalid shortcut keys", () => {
		expect(() =>
			parseImportedKeybindings({
				config: { "shift+bogus": "toggle-play" },
			}),
		).toThrow(/shift\+bogus/);
	});

	test("throws on invalid actions", () => {
		expect(() =>
			parseImportedKeybindings({ config: { space: "not-an-action" } }),
		).toThrow(/not-an-action/);
	});
});