151 lines
6.1 KiB
JavaScript
151 lines
6.1 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
import { loadTestModule } from './load-test-module.mjs'
|
|
|
|
const { createKeymapRequests, keymapPresetUpdate, keymapStatusHint, keymapUpdate, visibleKeymapPresets } = 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: '' })
|
|
})
|
|
|
|
test('visibleKeymapPresets hides win_to_cmd on modern runtimes unless already selected', () => {
|
|
assert.deepEqual(visibleKeymapPresets('off', false), ['off', 'swap_alt_win', 'custom'])
|
|
assert.deepEqual(visibleKeymapPresets('swap_alt_win', false), ['off', 'swap_alt_win', 'custom'])
|
|
assert.deepEqual(visibleKeymapPresets('custom', false), ['off', 'swap_alt_win', 'custom'])
|
|
assert.deepEqual(visibleKeymapPresets('win_to_cmd', false), ['off', 'win_to_cmd', 'swap_alt_win', 'custom'])
|
|
assert.deepEqual(visibleKeymapPresets('off', true), ['off', 'win_to_cmd', 'swap_alt_win', 'custom'])
|
|
assert.deepEqual(visibleKeymapPresets('swap_alt_win', true), ['off', 'win_to_cmd', 'swap_alt_win', 'custom'])
|
|
})
|
|
|
|
test('keymapStatusHint returns null when both preset and shortcut capture are off', () => {
|
|
const base = {
|
|
preset: 'off',
|
|
bindings: {},
|
|
capture_host_shortcuts: false,
|
|
needs_guest_tools: false,
|
|
guest_connected: true,
|
|
guest_agent_version: 5,
|
|
required_agent_version: 5,
|
|
input_service_available: true,
|
|
guest_keymap_active: true,
|
|
hook_active: true,
|
|
running: true,
|
|
}
|
|
assert.equal(keymapStatusHint(base), null)
|
|
})
|
|
|
|
test('keymapStatusHint resolves priority when only capture_host_shortcuts is enabled', () => {
|
|
const base = {
|
|
preset: 'off',
|
|
bindings: {},
|
|
capture_host_shortcuts: true,
|
|
needs_guest_tools: false,
|
|
guest_connected: false,
|
|
guest_agent_version: 0,
|
|
required_agent_version: 4,
|
|
input_service_available: true,
|
|
guest_keymap_active: false,
|
|
hook_active: true,
|
|
running: true,
|
|
}
|
|
assert.deepEqual(keymapStatusHint(base), { key: 'keymapRunning' })
|
|
assert.deepEqual(keymapStatusHint({ ...base, hook_active: false }), { key: 'keymapInactive' })
|
|
assert.deepEqual(keymapStatusHint({ ...base, running: false }), { key: 'keymapStopped' })
|
|
assert.deepEqual(keymapStatusHint({ ...base, input_service_available: false, running: false }), { key: 'keymapInputServiceRequired' })
|
|
assert.deepEqual(keymapStatusHint({ ...base, needs_guest_tools: true, guest_connected: false }), { key: 'keymapWaiting' })
|
|
assert.deepEqual(
|
|
keymapStatusHint({ ...base, needs_guest_tools: true, guest_connected: true, guest_agent_version: 3, required_agent_version: 4 }),
|
|
{ key: 'keymapUpgrade', vars: { version: 4 } },
|
|
)
|
|
assert.deepEqual(
|
|
keymapStatusHint({ ...base, needs_guest_tools: true, guest_connected: true, guest_agent_version: 4, required_agent_version: 4, guest_keymap_active: false }),
|
|
{ key: 'keymapPending' },
|
|
)
|
|
assert.deepEqual(
|
|
keymapStatusHint({ ...base, needs_guest_tools: true, guest_connected: true, guest_agent_version: 4, required_agent_version: 4, guest_keymap_active: true }),
|
|
{ key: 'keymapRunning' },
|
|
)
|
|
})
|