-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeybindings_engine_test.js
More file actions
133 lines (112 loc) · 4.17 KB
/
Copy pathkeybindings_engine_test.js
File metadata and controls
133 lines (112 loc) · 4.17 KB
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
// SPDX-License-Identifier: MPL-2.0
/**
* KeybindingsEngine Tests — lookup, conflict detection, rebind, reset
*/
import { assertEquals, assertExists } from "jsr:@std/assert";
import {
chord,
bind,
defaults,
lookup,
detectConflicts,
rebind,
resetBinding,
resetAll,
defaultState,
} from "../src/core/KeybindingsEngine.res.js";
// ReScript compiles option<T> as: Some(x) → x, None → undefined
Deno.test("defaults has 19 keybindings", () => {
assertExists(defaults);
assertEquals(defaults.length, 19);
});
Deno.test("defaultState has bindings array", () => {
assertExists(defaultState);
assertEquals(defaultState.bindings.length, defaults.length);
assertEquals(defaultState.recording, false);
assertEquals(defaultState.conflicts.length, 0);
});
Deno.test("lookup finds Ctrl+Z = ActionUndo", () => {
const action = lookup(defaults, true, false, false, false, "z");
assertEquals(action, "ActionUndo");
});
Deno.test("lookup finds Ctrl+Shift+Z = ActionRedo", () => {
const action = lookup(defaults, true, true, false, false, "Z");
assertEquals(action, "ActionRedo");
});
Deno.test("lookup finds Ctrl+S = ActionSave", () => {
const action = lookup(defaults, true, false, false, false, "s");
assertEquals(action, "ActionSave");
});
Deno.test("lookup finds Escape = ActionCloseOverlay", () => {
const action = lookup(defaults, false, false, false, false, "Escape");
assertEquals(action, "ActionCloseOverlay");
});
Deno.test("lookup finds F11 = ActionFullscreen", () => {
const action = lookup(defaults, false, false, false, false, "F11");
assertEquals(action, "ActionFullscreen");
});
Deno.test("lookup returns undefined for unbound key", () => {
const action = lookup(defaults, false, false, false, false, "q");
assertEquals(action, undefined);
});
Deno.test("lookup returns undefined for wrong modifiers", () => {
const action = lookup(defaults, false, false, false, false, "z");
assertEquals(action, undefined);
});
Deno.test("detectConflicts returns empty for defaults", () => {
const conflicts = detectConflicts(defaults);
assertEquals(conflicts.length, 0);
});
Deno.test("detectConflicts finds duplicate chords", () => {
const dupes = [
bind(["Ctrl"], "z", "ActionUndo"),
bind(["Ctrl"], "z", "ActionSave"),
];
const conflicts = detectConflicts(dupes);
assertEquals(conflicts.length, 1);
assertEquals(conflicts[0][0], "ActionUndo");
assertEquals(conflicts[0][1], "ActionSave");
});
Deno.test("rebind replaces existing binding", () => {
const newChord = chord(["Ctrl", "Shift"], "U");
const result = rebind(defaults, "ActionUndo", newChord);
assertEquals(result.length, defaults.length);
const undoBinding = result.find((b) => b.action === "ActionUndo");
assertEquals(undoBinding.chord.key, "U");
assertEquals(undoBinding.custom, true);
});
Deno.test("rebind adds new binding for unknown action", () => {
const newChord = chord(["Ctrl"], "x");
const result = rebind(defaults, "ActionCustom", newChord);
assertEquals(result.length, defaults.length + 1);
const custom = result.find((b) => b.action === "ActionCustom");
assertExists(custom);
assertEquals(custom.chord.key, "x");
assertEquals(custom.custom, true);
});
Deno.test("resetBinding restores default chord", () => {
const newChord = chord(["Alt"], "u");
const modified = rebind(defaults, "ActionUndo", newChord);
const undoBefore = modified.find((b) => b.action === "ActionUndo");
assertEquals(undoBefore.chord.key, "u");
const reset = resetBinding(modified, "ActionUndo");
const undoAfter = reset.find((b) => b.action === "ActionUndo");
assertEquals(undoAfter.chord.key, "z");
assertEquals(undoAfter.custom, false);
});
Deno.test("resetAll returns defaults", () => {
const result = resetAll();
assertEquals(result.length, defaults.length);
assertEquals(result, defaults);
});
Deno.test("chord creates correct structure", () => {
const c = chord(["Ctrl", "Shift"], "X");
assertEquals(c.modifiers, ["Ctrl", "Shift"]);
assertEquals(c.key, "X");
});
Deno.test("bind creates non-custom binding", () => {
const b = bind(["Ctrl"], "s", "ActionSave");
assertEquals(b.chord.key, "s");
assertEquals(b.action, "ActionSave");
assertEquals(b.custom, false);
});