Files
Reims-Go/frontend/scripts/test-keymap.mjs
T
棠渚听潮 7c18815f53 feat(input,ui): add per-VM focus-exclusive host shortcut capture and hook promotion
- Add per-VM `capture_host_shortcuts` option persisted in `.reims-go-keymap.json`
  (independent from modifier preset and backward-compatible with legacy configs
  and Wails callers omitting the field). When enabled, unmapped Win and Alt keys
  route through the existing Guest Tools v8 hidutil carrier (`lcmd`/`rcmd`/`lopt`/
  `ropt`) while the VM is focused, preventing host Win/Alt shortcuts (e.g. PixPin
  Alt+A, Alt+Tab, Alt+F4, Ctrl+Alt+A, Win+...) without modifying explicit custom
  bindings or bumping Guest Tools version.
- Monitor `EVENT_SYSTEM_FOREGROUND` and `EVENT_OBJECT_NAMECHANGE` on the Windows
  keymap hook thread to immediately release held modifiers on focus loss and
  promote `WH_KEYBOARD_LL` back to the top of the hook chain on VM focus entry
  and SDL3 in-window re-capture, with per-key timestamp filtering for delayed
  focus-loss events, swap-window event deduplication, Win64 32-bit parameter
  narrowing, and fallback timer recovery.
- Serialize `SetVMKeymap` read-merge-save under `keymapMu`, expose the toggle in
  Settings with zh-CN/en-US i18n, and add Go + frontend unit tests.
2026-12-02 10:00:09 +08:00

92 lines
3.4 KiB
JavaScript

import assert from 'node:assert/strict'
import test from 'node:test'
import { loadTestModule } from './load-test-module.mjs'
const { createKeymapRequests, keymapPresetUpdate, keymapUpdate } = await loadTestModule(`export * from './lib/vmKeymap'`)
const off = { preset: 'off', bindings: {}, capture_host_shortcuts: false }
const custom = { preset: 'custom', bindings: { lwin: 'lcmd' }, capture_host_shortcuts: true }
test('switching presets preserves shortcut capture and carries bindings into custom', () => {
const preset = { ...custom, preset: 'win_to_cmd' }
assert.equal(keymapPresetUpdate(preset, 'win_to_cmd'), undefined)
assert.deepEqual(keymapPresetUpdate(preset, 'custom'), custom)
assert.deepEqual(keymapPresetUpdate(custom, 'off'), { ...off, capture_host_shortcuts: true })
assert.deepEqual(keymapPresetUpdate(off, 'custom'), { ...off, preset: 'custom' })
})
test('editing one binding preserves the other bindings, preset, and shortcut capture', () => {
assert.deepEqual(keymapUpdate(custom, { bindings: { lalt: 'disabled' } }), {
...custom,
bindings: { lwin: 'lcmd', lalt: 'disabled' },
})
})
test('toggling shortcut capture preserves the preset and bindings', () => {
const captureOff = { ...custom, capture_host_shortcuts: false }
assert.deepEqual(keymapUpdate(captureOff, { capture_host_shortcuts: true }), custom)
assert.deepEqual(keymapUpdate(custom, { capture_host_shortcuts: false }), captureOff)
assert.deepEqual(keymapUpdate(off, { capture_host_shortcuts: true }), { ...off, capture_host_shortcuts: true })
})
test('a pending write for one VM does not block or overwrite another VM', async () => {
const saved = Promise.withResolvers()
const writes = []
let active = 'a'
let state = {}
const requests = createKeymapRequests({
async GetVMKeymap(name) {
assert.equal(name, 'b')
return off
},
SetVMKeymap(name, request) {
writes.push([name, request])
return saved.promise
},
}, () => active, (patch) => Object.assign(state, patch))
const write = requests.write('a', custom)
await requests.write('a', off)
assert.deepEqual(writes, [['a', custom]])
assert.equal(state.busy, true)
active = 'b'
state = requests.initial('b')
await requests.watch('b').refresh()
saved.resolve(custom)
await write
assert.deepEqual(state, { keymap: off, busy: false, error: '' })
assert.equal(requests.initial('a').busy, false)
})
test('a failed keymap read recovers on the next refresh', async () => {
let fail = true
const state = {}
const requests = createKeymapRequests({
async GetVMKeymap() {
if (fail) throw new Error('temporary')
return off
},
}, () => 'vm', (patch) => Object.assign(state, patch))
const watch = requests.watch('vm')
await watch.refresh()
assert.equal(state.error, 'temporary')
fail = false
await watch.refresh()
assert.deepEqual(state, { keymap: off, error: '' })
})
test('a read started before a write cannot restore the old keymap', async () => {
const previousRead = Promise.withResolvers()
const state = {}
const requests = createKeymapRequests({
GetVMKeymap: () => previousRead.promise,
SetVMKeymap: async () => custom,
}, () => 'vm', (patch) => Object.assign(state, patch))
const read = requests.watch('vm').refresh()
await requests.write('vm', custom)
previousRead.resolve(off)
await read
assert.deepEqual(state, { keymap: custom, busy: false, error: '' })
})