Files
web-terminal/test/keybar.test.ts
Yaojia Wang 4f66016f02 feat(v0.2): multi-tab terminal + titles + Claude Code shortcut bar
Frontend feature (backend unchanged — manager was already multi-session):
- Multi-tab: each tab = independent WS + Terminal + session (terminal-session.ts,
  tabs.ts); tab bar with +, ×, middle-click close; persisted to localStorage
  (migrates v0.1 single-session key)
- Tab titles: auto from xterm onTitleChange (OSC 0/2), double-click to rename
  inline (manual wins over auto); ellipsis + tooltip
- Shortcut key bar now shown on ALL devices (clickable buttons trigger shortcuts),
  reordered for Claude Code (Esc prominent, ⇧Tab, ↑↓, ⏎, ^C, Tab, ←→, /);
  responsive sizing via @media (pointer:coarse) for touch
- Verified in browser: rename, two independent sessions, keybar on desktop +
  narrow viewport. 191 tests green; both typechecks + esbuild build pass.
2026-06-17 12:11:18 +02:00

133 lines
4.1 KiB
TypeScript

/**
* test/keybar.test.ts — Unit tests for keybar module (T10).
*
* Tests the pure KEY_MAP data structure and verifies each key
* is mapped to the correct byte string (ARCHITECTURE §5 / §6.3).
*
* Per task spec: "对纯字节表单测,DOM 部分手动验" —
* We test the byte mappings here. DOM behavior (button creation,
* event binding, touchstart vs click) is left to manual E2E verification
* since jsdom is not in the project dependencies.
*/
import { describe, it, expect } from 'vitest'
import { KEY_MAP, KEYBAR_BUTTONS } from '../public/keybar.js'
describe('keybar', () => {
describe('KEY_MAP — pure byte mappings', () => {
it('exports KEY_MAP as a constant object', () => {
expect(KEY_MAP).toBeDefined()
expect(typeof KEY_MAP).toBe('object')
})
it('Esc maps to \\x1b (0x1B)', () => {
expect(KEY_MAP.esc).toBe('\x1b')
expect(KEY_MAP.esc.charCodeAt(0)).toBe(0x1b)
})
it('Shift+Tab maps to \\x1b[Z', () => {
expect(KEY_MAP.shiftTab).toBe('\x1b[Z')
expect(KEY_MAP.shiftTab).toHaveLength(3)
expect(KEY_MAP.shiftTab.charCodeAt(0)).toBe(0x1b)
expect(KEY_MAP.shiftTab.charCodeAt(1)).toBe('['.charCodeAt(0))
expect(KEY_MAP.shiftTab.charCodeAt(2)).toBe('Z'.charCodeAt(0))
})
it('arrow up maps to \\x1b[A', () => {
expect(KEY_MAP.arrowUp).toBe('\x1b[A')
expect(KEY_MAP.arrowUp).toHaveLength(3)
})
it('arrow down maps to \\x1b[B', () => {
expect(KEY_MAP.arrowDown).toBe('\x1b[B')
expect(KEY_MAP.arrowDown).toHaveLength(3)
})
it('arrow left maps to \\x1b[D', () => {
expect(KEY_MAP.arrowLeft).toBe('\x1b[D')
expect(KEY_MAP.arrowLeft).toHaveLength(3)
})
it('arrow right maps to \\x1b[C', () => {
expect(KEY_MAP.arrowRight).toBe('\x1b[C')
expect(KEY_MAP.arrowRight).toHaveLength(3)
})
it('Enter maps to \\r (0x0D carriage return, not \\n)', () => {
expect(KEY_MAP.enter).toBe('\r')
expect(KEY_MAP.enter).toHaveLength(1)
expect(KEY_MAP.enter.charCodeAt(0)).toBe(0x0d)
expect(KEY_MAP.enter).not.toBe('\n')
expect(KEY_MAP.enter.charCodeAt(0)).not.toBe(0x0a)
})
it('Ctrl+C maps to \\x03 (0x03 ETX)', () => {
expect(KEY_MAP.ctrlC).toBe('\x03')
expect(KEY_MAP.ctrlC).toHaveLength(1)
expect(KEY_MAP.ctrlC.charCodeAt(0)).toBe(0x03)
})
it('Tab maps to \\t (0x09)', () => {
expect(KEY_MAP.tab).toBe('\t')
expect(KEY_MAP.tab).toHaveLength(1)
expect(KEY_MAP.tab.charCodeAt(0)).toBe(0x09)
})
it('slash maps to / (Claude Code slash-command launcher)', () => {
expect(KEY_MAP.slash).toBe('/')
expect(KEY_MAP.slash).toHaveLength(1)
})
it('all 10 keys are defined', () => {
const keys = Object.keys(KEY_MAP)
expect(keys).toEqual([
'esc',
'shiftTab',
'arrowUp',
'arrowDown',
'arrowLeft',
'arrowRight',
'enter',
'ctrlC',
'tab',
'slash',
])
})
it('all mapped bytes are unique strings', () => {
const bytes = Object.values(KEY_MAP)
expect(bytes).toHaveLength(10)
expect(new Set(bytes).size).toBe(10) // all unique
})
it('ANSI arrow sequences use correct format', () => {
// All arrow keys follow \x1b[X pattern where X is A/B/C/D
const arrowKeys = [KEY_MAP.arrowUp, KEY_MAP.arrowDown, KEY_MAP.arrowLeft, KEY_MAP.arrowRight]
arrowKeys.forEach((seq) => {
expect(seq).toMatch(/^\x1b\[./)
expect(seq).toHaveLength(3)
})
})
})
describe('KEYBAR_BUTTONS — layout', () => {
it('every button references a valid KEY_MAP key', () => {
for (const b of KEYBAR_BUTTONS) {
expect(KEY_MAP[b.keyName]).toBeDefined()
}
})
it('Esc is first and marked primary (most-used interrupt)', () => {
expect(KEYBAR_BUTTONS[0]?.keyName).toBe('esc')
expect(KEYBAR_BUTTONS[0]?.primary).toBe(true)
})
it('exposes all Claude Code keys as buttons', () => {
const keys = KEYBAR_BUTTONS.map((b) => b.keyName)
expect(keys).toContain('shiftTab')
expect(keys).toContain('slash')
expect(keys).toContain('arrowUp')
})
})
})