Repository navigation
feat(incott): macro card in the Macro tab #1382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
snekxs
merged 5 commits into
OpenMouse-Project:main
from
vladidraganov:incott-macros-v2
Nov 12, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
489c81a
feat(incott): macro card in the Macro tab
vladidraganov 493a6d4
test(preview): add an Incott G23 Pro fixture
vladidraganov 5edf4b9
Merge remote-tracking branch 'origin/main' into incott-macros-v2
snekxs b8bcd60
fix(incott): drop a staged macro upload on unassign, keep releases at…
snekxs 622f62a
fix(incott): scope macro assignments per mouse and save them once fla…
snekxs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
|
|
||
| import { INCOTT_MACRO_MAX_STEPS, INCOTT_MACRO_MOUSE_KEYS, incottMacroKeyLabel } from "@openmouse/protocol/incott"; | ||
|
|
||
| import { | ||
| INCOTT_MACRO_KEY_CHOICES, | ||
| appendRecordedStep, | ||
| closeHeldKeys, | ||
| emptyMacro, | ||
| incottMacroAssignment, | ||
| incottMacroKeyForCode, | ||
| incottMacroKeyForMouseButton, | ||
| macroDurationMs, | ||
| parseStore, | ||
| stepsForDevice, | ||
| unreleasedKeys, | ||
| type MacroStore, | ||
| } from "./incott-macros.ts"; | ||
|
|
||
| test("macro duration is the sum of its delays", () => { | ||
| assert.equal(macroDurationMs([]), 0); | ||
| assert.equal(macroDurationMs([ | ||
| { key: 0xf0, press: true, delayMs: 40 }, | ||
| { key: 0xf0, press: false, delayMs: 1200 }, | ||
| ]), 1240); | ||
| }); | ||
|
|
||
| test("a fixed delay replaces the recorded timing without destroying it", () => { | ||
| // The mode is stored beside the steps, not written into them, so switching | ||
| // to a fixed gap and back returns the recorded timing intact. | ||
| const macro = { | ||
| ...emptyMacro("click"), | ||
| delay: { mode: "fixed" as const, fixedMs: 25 }, | ||
| steps: [ | ||
| { key: 0xf0, press: true, delayMs: 84 }, | ||
| { key: 0xf0, press: false, delayMs: 252 }, | ||
| ], | ||
| }; | ||
| assert.deepEqual(stepsForDevice(macro).map((step) => step.delayMs), [25, 25]); | ||
| assert.deepEqual(macro.steps.map((step) => step.delayMs), [84, 252], "the recording is untouched"); | ||
| assert.deepEqual( | ||
| stepsForDevice({ ...macro, delay: { mode: "recorded", fixedMs: 25 } }).map((step) => step.delayMs), | ||
| [84, 252], | ||
| ); | ||
| }); | ||
|
|
||
| test("no delay sends zeros, keeping the keys and their order", () => { | ||
| const macro = { | ||
| ...emptyMacro("burst"), | ||
| delay: { mode: "none" as const, fixedMs: 60 }, | ||
| steps: [ | ||
| { key: 0xf0, press: true, delayMs: 84 }, | ||
| { key: 0xf0, press: false, delayMs: 1 }, | ||
| ], | ||
| }; | ||
| const sent = stepsForDevice(macro); | ||
| assert.deepEqual(sent.map((step) => step.delayMs), [0, 0]); | ||
| assert.deepEqual( | ||
| sent.map((step) => [step.key, step.press]), | ||
| macro.steps.map((step) => [step.key, step.press]), | ||
| ); | ||
| }); | ||
|
|
||
| test("a new macro starts empty, playing once, with its recorded timing", () => { | ||
| const macro = emptyMacro("Macro 1"); | ||
| assert.deepEqual(macro.steps, []); | ||
| assert.equal(macro.loop, "cycle"); | ||
| assert.equal(macro.cycles, 1); | ||
| assert.equal(macro.delay.mode, "recorded"); | ||
| }); | ||
|
|
||
| test("a recorded gap closes the previous step, not the new one", () => { | ||
| let steps = appendRecordedStep([], 0x04, true, 9999); | ||
| assert.deepEqual(steps, [{ key: 0x04, press: true, delayMs: 0 }], "the first event has nothing before it to close"); | ||
| steps = appendRecordedStep(steps, 0x04, false, 85); | ||
| assert.deepEqual(steps, [ | ||
| { key: 0x04, press: true, delayMs: 85 }, | ||
| { key: 0x04, press: false, delayMs: 0 }, | ||
| ]); | ||
| }); | ||
|
|
||
| test("recording stops growing at the buffer's step limit and clamps long gaps", () => { | ||
| let steps = appendRecordedStep([], 0x04, true, 0); | ||
| for (let index = 1; index < INCOTT_MACRO_MAX_STEPS + 5; index += 1) { | ||
| steps = appendRecordedStep(steps, 0x04, index % 2 === 0, 70000); | ||
| } | ||
| assert.ok(steps.length <= INCOTT_MACRO_MAX_STEPS); | ||
| assert.equal(steps[0]!.delayMs, 0xffff, "an LE16 field cannot hold more"); | ||
| }); | ||
|
|
||
| test("at the step limit a held key still gets its release", () => { | ||
| let steps = appendRecordedStep([], 0x04, true, 0); | ||
| // Fill with taps of another key until the next press would not leave room. | ||
| for (let index = 0; index < INCOTT_MACRO_MAX_STEPS; index += 1) { | ||
| steps = appendRecordedStep(steps, 0x05, true, 1); | ||
| steps = appendRecordedStep(steps, 0x05, false, 1); | ||
| } | ||
| assert.ok(steps.length < INCOTT_MACRO_MAX_STEPS, "room is held back for the key still down"); | ||
| steps = appendRecordedStep(steps, 0x04, false, 1); | ||
| assert.equal(steps.at(-1)!.key, 0x04); | ||
| assert.equal(steps.at(-1)!.press, false); | ||
| assert.ok(steps.length <= INCOTT_MACRO_MAX_STEPS); | ||
| }); | ||
|
|
||
| test("the store survives garbage and drops assignments to missing macros", () => { | ||
| assert.deepEqual(parseStore(null), { macros: [], assignments: {} }); | ||
| assert.deepEqual(parseStore("{not json"), { macros: [], assignments: {} }); | ||
| assert.deepEqual(parseStore("42"), { macros: [], assignments: {} }); | ||
|
|
||
| const store = parseStore(JSON.stringify({ | ||
| macros: [ | ||
| { id: "a", name: "Tap", loop: "bogus", cycles: -3, steps: [{ key: 4, press: true, delayMs: 90000 }] }, | ||
| { id: "b", name: "Empty", steps: [] }, | ||
| ], | ||
| devices: { "mouse-1": { Forward: "a", Back: "b", Nonsense: "a" }, "mouse-2": { Left: "a" } }, | ||
| }), "mouse-1"); | ||
| assert.equal(store.macros.length, 1, "a macro with no steps is not kept"); | ||
| assert.equal(store.macros[0]!.loop, "cycle"); | ||
| assert.equal(store.macros[0]!.cycles, 1); | ||
| assert.equal(store.macros[0]!.steps[0]!.delayMs, 0xffff); | ||
| assert.deepEqual(store.assignments, { Forward: "a" }); | ||
| }); | ||
|
|
||
| test("assignments belong to one mouse, so another mouse's Macro button is not this one's", () => { | ||
| const raw = JSON.stringify({ | ||
| macros: [{ id: "a", name: "Tap", steps: [{ key: 4, press: true, delayMs: 0 }] }], | ||
| devices: { "mouse-1": { Back: "a" } }, | ||
| }); | ||
| assert.deepEqual(parseStore(raw, "mouse-1").assignments, { Back: "a" }); | ||
| assert.deepEqual(parseStore(raw, "mouse-2").assignments, {}); | ||
| assert.deepEqual(parseStore(raw, null).assignments, {}, "no device, no assignments"); | ||
| assert.equal(parseStore(raw, "mouse-1").macros.length, 1, "the library is shared"); | ||
| }); | ||
|
|
||
| test("a key still down when recording stops is released", () => { | ||
| const steps = appendRecordedStep([], 0x04, true, 0); | ||
| assert.deepEqual(unreleasedKeys(steps), [0x04]); | ||
| const closed = closeHeldKeys(steps, 120); | ||
| assert.deepEqual(closed, [ | ||
| { key: 0x04, press: true, delayMs: 120 }, | ||
| { key: 0x04, press: false, delayMs: 0 }, | ||
| ]); | ||
| assert.deepEqual(unreleasedKeys(closed), []); | ||
| }); | ||
|
|
||
| test("an assignment only counts while the mouse reports the button running a macro", () => { | ||
| const store: MacroStore = { macros: [{ ...emptyMacro("Tap"), id: "a" }], assignments: { Forward: "a" } }; | ||
| assert.equal(incottMacroAssignment(store, "Forward", undefined), "a", "no mapping read: the store is all there is"); | ||
| assert.equal(incottMacroAssignment(store, "Forward", { Forward: "Macro" }), "a"); | ||
| assert.equal( | ||
| incottMacroAssignment(store, "Forward", { Forward: "Forward" }), | ||
| null, | ||
| "remapped elsewhere: the button no longer runs it", | ||
| ); | ||
| assert.equal(incottMacroAssignment(store, "Back", { Back: "Macro" }), "elsewhere"); | ||
| assert.equal( | ||
| incottMacroAssignment(store, "Back", { Back: "Macro (buffer 2)" }), | ||
| null, | ||
| "a binding to another button's buffer never fires, so it is not shown as a macro", | ||
| ); | ||
| }); | ||
|
|
||
| test("the recorder maps browser input onto keys the editor can name", () => { | ||
| assert.equal(incottMacroKeyForCode("KeyA"), 0x04); | ||
| assert.equal(incottMacroKeyForCode("Digit0"), 0x27); | ||
| assert.equal(incottMacroKeyForCode("ShiftLeft"), 0xe1); | ||
| assert.equal(incottMacroKeyForCode("NumpadAdd"), null); | ||
| assert.equal(incottMacroKeyForMouseButton(0), INCOTT_MACRO_MOUSE_KEYS.left); | ||
| assert.equal(incottMacroKeyForMouseButton(2), INCOTT_MACRO_MOUSE_KEYS.right); | ||
| assert.equal(incottMacroKeyForMouseButton(3), null); | ||
| for (const key of [...INCOTT_MACRO_KEY_CHOICES.mouse, ...INCOTT_MACRO_KEY_CHOICES.keyboard]) { | ||
| assert.notEqual(incottMacroKeyLabel(key), null, `key 0x${key.toString(16)} has a name`); | ||
| } | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.