88 lines
4.6 KiB
TypeScript
88 lines
4.6 KiB
TypeScript
import assert from 'node:assert/strict'
|
|
import { test } from 'vitest'
|
|
import {
|
|
DEFAULT_BINDINGS,
|
|
bindingKeycap,
|
|
bindingMatches,
|
|
bindingsEqual,
|
|
isPlainBinding,
|
|
normalizeBindings,
|
|
validateBinding,
|
|
type ApprovalBindings,
|
|
} from '../../src/client/chat/bindings.ts'
|
|
|
|
test('defaults: Enter allow, Ctrl+Enter always, Esc reject', () => {
|
|
assert.deepEqual(DEFAULT_BINDINGS.allow, { key: 'Enter', primary: false, shift: false, alt: false })
|
|
assert.deepEqual(DEFAULT_BINDINGS.always, { key: 'Enter', primary: true, shift: false, alt: false })
|
|
assert.deepEqual(DEFAULT_BINDINGS.reject, { key: 'Escape', primary: false, shift: false, alt: false })
|
|
assert.equal(bindingKeycap(DEFAULT_BINDINGS.allow, false), 'Enter')
|
|
assert.equal(bindingKeycap(DEFAULT_BINDINGS.always, false), 'Ctrl+Enter')
|
|
assert.equal(bindingKeycap(DEFAULT_BINDINGS.always, true), 'Cmd+Enter')
|
|
assert.equal(bindingKeycap(DEFAULT_BINDINGS.reject, false), 'Esc')
|
|
assert.equal(bindingKeycap({ key: 'Enter', primary: true, shift: true, alt: true }, false), 'Ctrl+Shift+Alt+Enter')
|
|
})
|
|
|
|
test('bindingMatches requires an exact modifier set and treats Ctrl/Meta as primary', () => {
|
|
const always = DEFAULT_BINDINGS.always!
|
|
assert.equal(bindingMatches(always, { key: 'Enter', ctrlKey: true }), true)
|
|
assert.equal(bindingMatches(always, { key: 'Enter', metaKey: true }), true)
|
|
assert.equal(bindingMatches(always, { key: 'Enter' }), false)
|
|
assert.equal(bindingMatches(always, { key: 'Enter', ctrlKey: true, shiftKey: true }), false)
|
|
assert.equal(bindingMatches(DEFAULT_BINDINGS.allow, { key: 'Enter' }), true)
|
|
assert.equal(bindingMatches(DEFAULT_BINDINGS.allow, { key: 'Enter', altKey: true }), false)
|
|
assert.equal(isPlainBinding(DEFAULT_BINDINGS.allow), true)
|
|
assert.equal(isPlainBinding(DEFAULT_BINDINGS.always), false)
|
|
assert.equal(isPlainBinding(null), false)
|
|
})
|
|
|
|
test('normalizeBindings falls back for missing, invalid, conflicting or modifier-less values', () => {
|
|
assert.deepEqual(normalizeBindings(undefined), DEFAULT_BINDINGS)
|
|
const custom = normalizeBindings({
|
|
allow: { key: 'Escape', primary: false, shift: false, alt: false }, // 允许不能用 Esc
|
|
always: { key: 'Enter', primary: false, shift: false, alt: false }, // 没有修饰键
|
|
reject: { key: 'Escape', primary: true, shift: false, alt: false },
|
|
})
|
|
assert.equal(custom.allow.key, 'Enter')
|
|
assert.equal(custom.always?.primary, true)
|
|
assert.equal(custom.reject.primary, true)
|
|
|
|
const disabled = normalizeBindings({ ...DEFAULT_BINDINGS, always: null })
|
|
assert.equal(disabled.always, null)
|
|
|
|
const conflict = normalizeBindings({
|
|
allow: { key: 'Enter', primary: true, shift: false, alt: false },
|
|
always: { key: 'Enter', primary: true, shift: false, alt: false },
|
|
reject: { key: 'Escape', primary: false, shift: false, alt: false },
|
|
})
|
|
assert.equal(bindingsEqual(conflict.allow, conflict.always), false)
|
|
})
|
|
|
|
test('validateBinding: base key, modifier requirement, and conflicts', () => {
|
|
const current: ApprovalBindings = normalizeBindings(undefined)
|
|
assert.equal(validateBinding('allow', { key: 'Enter', primary: false, shift: true, alt: false }, current), 'ok')
|
|
assert.equal(validateBinding('reject', { key: 'Escape', primary: false, shift: false, alt: true }, current), 'ok')
|
|
assert.equal(validateBinding('allow', { key: 'Escape', primary: false, shift: false, alt: false }, current), 'key')
|
|
assert.equal(validateBinding('always', { key: 'Enter', primary: false, shift: false, alt: false }, current), 'modifier')
|
|
assert.equal(validateBinding('allow', current.allow, current), 'ok')
|
|
assert.equal(validateBinding('reject', current.reject, current), 'ok')
|
|
assert.equal(validateBinding('always', current.always!, current), 'ok')
|
|
|
|
const custom = normalizeBindings({
|
|
allow: { key: 'Enter', primary: false, shift: true, alt: false },
|
|
always: { key: 'Enter', primary: true, shift: false, alt: false },
|
|
reject: { key: 'Escape', primary: false, shift: false, alt: false },
|
|
})
|
|
assert.equal(validateBinding('always', custom.allow, custom), 'conflict')
|
|
assert.equal(validateBinding('allow', custom.always, custom), 'conflict')
|
|
})
|
|
|
|
test('validateBinding accepts a shifted Enter as a custom allow binding without colliding', () => {
|
|
const current = normalizeBindings({
|
|
allow: { key: 'Enter', primary: false, shift: true, alt: false },
|
|
always: null,
|
|
reject: { key: 'Escape', primary: false, shift: false, alt: false },
|
|
})
|
|
assert.equal(validateBinding('reject', { key: 'Escape', primary: false, shift: true, alt: false }, current), 'ok')
|
|
assert.equal(validateBinding('allow', { key: 'Enter', primary: false, shift: true, alt: false }, current), 'ok')
|
|
})
|