dsh-better-chat/tests/chat/key-hint.test.ts

174 lines
6.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import assert from 'node:assert/strict'
import { test } from 'vitest'
import {
HINT_ATTR,
hintParts,
hintSignature,
installKeyHints,
isMacPlatform,
} from '../../src/client/chat/key-hint.ts'
test('hintParts: Ctrl+Enter only appears when the always button exists', () => {
const withAlways = hintParts({ allow: '允许(单次)', always: '始终允许', reject: '拒绝' }, false)
assert.deepEqual(withAlways.map((part) => part.key), ['Enter', 'Ctrl+Enter', 'Esc'])
const mac = hintParts({ allow: '允许(单次)', always: '始终允许', reject: '拒绝' }, true)
assert.equal(mac[1]?.key, 'Cmd+Enter')
const withoutAlways = hintParts({ allow: '允许(单次)', reject: '拒绝' }, false)
assert.deepEqual(withoutAlways.map((part) => part.key), ['Enter', 'Esc'])
})
test('hintSignature changes with labels and platform keys', () => {
const a = hintParts({ allow: '允许', reject: '拒绝' }, false)
const b = hintParts({ allow: '允许', reject: '拒绝' }, false)
const c = hintParts({ allow: 'Allow', reject: 'Reject' }, false)
assert.equal(hintSignature(a), hintSignature(b))
assert.notEqual(hintSignature(a), hintSignature(c))
})
test('isMacPlatform detects Apple platforms only', () => {
assert.equal(isMacPlatform('MacIntel'), true)
assert.equal(isMacPlatform('iPhone'), true)
assert.equal(isMacPlatform('Win32'), false)
assert.equal(isMacPlatform('Linux x86_64'), false)
assert.equal(isMacPlatform(undefined), false)
})
// --- 极小假 DOM:只实现提示模块用到的表面 ---
function matches(el: any, selector: string): boolean {
return selector.split(',').some((part) => {
const s = part.trim()
if (s.startsWith('[')) {
const m = s.match(/^\[([a-zA-Z-]+)(?:="([^"]*)")?\]$/)
if (!m) return false
const value = el.attrs[m[1]]
return m[2] === undefined ? value !== undefined : value === m[2]
}
return el.tag === s
})
}
class FakeEl {
tag: string
attrs: Record<string, string> = {}
children: FakeEl[] = []
parent: FakeEl | null = null
style = { cssText: '' }
disabled = false
textContent = ''
get parentElement(): FakeEl | null { return this.parent }
constructor(tag: string) { this.tag = tag }
setAttribute(name: string, value: string) { this.attrs[name] = value }
getAttribute(name: string) { return this.attrs[name] ?? null }
append(...nodes: FakeEl[]) { for (const node of nodes) { node.parent = this; this.children.push(node) } return this }
insertBefore(node: FakeEl, ref: FakeEl | null) {
node.parent = this
const index = ref ? this.children.indexOf(ref) : -1
if (index < 0) this.children.unshift(node)
else this.children.splice(index, 0, node)
return node
}
remove() {
if (!this.parent) return
const index = this.parent.children.indexOf(this)
if (index >= 0) this.parent.children.splice(index, 1)
this.parent = null
}
closest(selector: string): FakeEl | null {
let current: FakeEl | null = this
while (current) {
if (matches(current, selector)) return current
current = current.parent
}
return null
}
querySelectorAll(selector: string): FakeEl[] {
const out: FakeEl[] = []
const walk = (node: FakeEl) => {
for (const child of node.children) {
if (matches(child, selector)) out.push(child)
walk(child)
}
}
walk(this)
return out
}
querySelector(selector: string): FakeEl | null {
return this.querySelectorAll(selector)[0] ?? null
}
}
class FakeDoc {
root = new FakeEl('div')
documentElement = this.root
defaultView = { navigator: { platform: 'Win32' } }
createElement(tag: string) { return new FakeEl(tag) }
querySelectorAll(selector: string) { return this.root.querySelectorAll(selector) }
}
let observerFire: (() => void) | null = null
;(globalThis as any).MutationObserver = class {
constructor(callback: () => void) { observerFire = callback }
observe() {}
disconnect() { observerFire = null }
}
function button(text: string): FakeEl {
const el = new FakeEl('button')
el.textContent = text
return el
}
function cardWithButtons(labels: string[]): { doc: FakeDoc; card: FakeEl; row: FakeEl } {
const doc = new FakeDoc()
const card = new FakeEl('div')
card.setAttribute('data-approval-key', 'k1')
const row = new FakeEl('div')
for (const label of labels) row.append(button(label))
card.append(row)
doc.root.append(card)
return { doc, card, row }
}
function textOf(node: FakeEl): string {
const own = node.textContent
return own + node.children.map(textOf).join('')
}
test('installKeyHints inserts the hint as the first child of the action row', () => {
const { doc, card, row } = cardWithButtons(['拒绝', '允许(单次)', '始终允许'])
const dispose = installKeyHints(doc as unknown as Document, 'Win32')
const hint = card.querySelector(`[${HINT_ATTR}]`) as FakeEl | null
assert.ok(hint, 'hint must be inserted')
assert.equal(row.children[0], hint, 'hint sits before the buttons')
const text = textOf(hint)
for (const expected of ['Enter', '允许(单次)', 'Ctrl+Enter', '始终允许', 'Esc', '拒绝']) {
assert.ok(text.includes(expected), `hint should contain ${expected}; got ${text}`)
}
dispose()
assert.equal(card.querySelector(`[${HINT_ATTR}]`), null, 'dispose removes the hint')
})
test('installKeyHints omits Ctrl+Enter when there is no always button', () => {
const { doc, card } = cardWithButtons(['拒绝', '允许'])
const dispose = installKeyHints(doc as unknown as Document, 'Win32')
const hint = card.querySelector(`[${HINT_ATTR}]`) as FakeEl
assert.equal(textOf(hint).includes('Ctrl+Enter'), false)
dispose()
})
test('installKeyHints uses Cmd on Apple platforms', () => {
const { doc, card } = cardWithButtons(['拒绝', '允许', '始终允许'])
const dispose = installKeyHints(doc as unknown as Document, 'MacIntel')
const hint = card.querySelector(`[${HINT_ATTR}]`) as FakeEl
assert.ok(textOf(hint).includes('Cmd+Enter'))
dispose()
})
test('installKeyHints removes the hint once the card is answered', () => {
const { doc, card } = cardWithButtons(['拒绝', '允许', '始终允许'])
const dispose = installKeyHints(doc as unknown as Document, 'Win32')
assert.ok(card.querySelector(`[${HINT_ATTR}]`))
card.setAttribute('aria-busy', 'true')
observerFire?.()
assert.equal(card.querySelector(`[${HINT_ATTR}]`), null)
dispose()
})