Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import '@testing-library/jest-dom/vitest'
import { act, cleanup, render, waitFor } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { TerminalPreviewApi } from '../../../../preload/api/dashboard-api'
import type { TerminalPreviewConnectResult } from '../../../../shared/terminal-preview'

const terminalHarness = vi.hoisted(() => ({
instances: [] as {
Expand Down Expand Up @@ -144,7 +146,7 @@ describe('AgentTerminalPreview', () => {
const fit = vi.fn(async (_ptyId: string, cols: number, rows: number) => ({ cols, rows }))
const ack = vi.fn(async () => {})
const unsubscribe = vi.fn(async () => {})
const connect = vi.fn()
const connect = vi.fn<TerminalPreviewApi['connect']>()
const readClipboardText = vi.fn(async () => 'clip-text')
const writeClipboardText = vi.fn(async () => {})
const writeTerminalClipboardText = vi.fn(async () => {})
Expand Down Expand Up @@ -503,7 +505,7 @@ describe('AgentTerminalPreview', () => {
it('does not let a redelivered kitty push outlive the TUI pop', async () => {
connect.mockResolvedValueOnce({
snapshot: { data: '\x1b[>1u', cols: 80, rows: 24, seq: 1 },
replay: ['\x1b[>1u']
replay: [{ data: '\x1b[>1u', mode: 'replay' }]
})
render(<AgentTerminalPreview ptyId="pty-1" />)
await waitFor(() => expect(terminalHarness.instances).toHaveLength(1))
Expand Down Expand Up @@ -554,10 +556,7 @@ describe('AgentTerminalPreview', () => {
})

it('keeps the existing terminal visible while a resync snapshot is captured', async () => {
let resolveRefresh!: (value: {
snapshot: { data: string; cols: number; rows: number; seq: number }
replay: string[]
}) => void
let resolveRefresh!: (value: TerminalPreviewConnectResult) => void
connect
.mockResolvedValueOnce({
snapshot: { data: 'first', cols: 80, rows: 24, seq: 1 },
Expand Down
21 changes: 21 additions & 0 deletions src/shared/terminal-kitty-keyboard-mode-tracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ describe('TerminalKittyKeyboardModeTracker', () => {
expect(tracker.flags).toBe(0)
})

// Why: the main-process capture provider is @ts-nocheck and scans a snapshot
// field older providers omit, so a nullish chunk must stay a no-op.
it('treats a nullish chunk as a no-op', () => {
const tracker = new TerminalKittyKeyboardModeTracker()
tracker.scan('\x1b[>5u')
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: reproduces the unchecked call site that passes an absent field.
const absent = undefined as unknown as string
expect(() => tracker.scan(absent)).not.toThrow()
expect(() => tracker.scanReplay(absent)).not.toThrow()
expect(tracker.flags).toBe(5)
expect(tracker.snapshotFlags).toBe(5)
})

it('does not treat CSI u (restore cursor) or the CSI ? u query as kitty state', () => {
const tracker = new TerminalKittyKeyboardModeTracker()
tracker.scan('\x1b[u\x1b[?u')
Expand Down Expand Up @@ -82,6 +95,14 @@ describe('TerminalKittyKeyboardModeTracker', () => {
expect(tracker.flags).toBe(7)
})

it('leaves negotiated state unchanged for plain output chunks', () => {
const tracker = new TerminalKittyKeyboardModeTracker()
tracker.scan('\x1b[>1u')
tracker.scan('shell output '.repeat(100))
expect(tracker.flags).toBe(1)
expect(tracker.snapshotFlags).toBe(1)
})

it('caps the mirrored stack without losing the current flags', () => {
const tracker = new TerminalKittyKeyboardModeTracker()
for (let i = 0; i < 40; i++) {
Expand Down
7 changes: 6 additions & 1 deletion src/shared/terminal-kitty-keyboard-mode-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ export class TerminalKittyKeyboardModeTracker {
}

private scanInternal(data: string, replay: boolean): void {
const input = this.scanTail + data
// Why: unchecked main-process callers can pass a snapshot field that is absent.
const chunk = typeof data === 'string' ? data : ''
if (this.scanTail.length === 0 && !chunk.includes('\x1b') && !chunk.includes('\x9b')) {
return
}
const input = this.scanTail + chunk
this.scanTail = this.extractScanTail(input)
// oxlint-disable-next-line no-control-regex -- terminal escape sequences require control chars
const kittyModeRe = /\x1bc|(?:\x1b\[|\x9b)(?:!p|\?([0-9;]+)([hl])|([<>=])([0-9;]*)u)/g
Expand Down
Loading