Files
orca/src/shared/terminal-quick-commands.test.ts
T
48a1923756 fix(terminal): flatten multiline quick commands into shell list (#2903)
Multiline quick commands should behave like a shell command list, not a series of already-queued PTY lines. When a foreground command reads stdin, raw newline delivery can be consumed before the shell sees later cd/pwd lines; terminal paste avoids that but shows the whole block as pasted input before execution.

Flatten newline-separated quick commands into a semicolon-delimited command list before active-pane dispatch and new-tab startup queuing. The shell parses the list once, so later commands run after earlier foreground commands exit and directory changes remain in the active shell.

Fixes #2844

Co-authored-by: Prethish-Complyance <prethish@complyance.io>
Co-authored-by: Orca <help@stably.ai>
2026-05-27 01:06:47 -07:00

224 lines
5.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
buildTerminalQuickCommandInput,
flattenTerminalQuickCommand,
getDefaultTerminalQuickCommands,
normalizeTerminalQuickCommands,
terminalQuickCommandMatchesRepo
} from './terminal-quick-commands'
describe('terminal quick commands', () => {
it('returns safe defaults when persisted settings are missing', () => {
expect(normalizeTerminalQuickCommands(undefined)).toEqual([])
expect(getDefaultTerminalQuickCommands()).toEqual([])
})
it('keeps an intentionally empty command list', () => {
expect(normalizeTerminalQuickCommands([])).toEqual([])
})
it('removes quick commands from the abandoned preset rollout', () => {
expect(
normalizeTerminalQuickCommands([
{
id: 'default-pwd',
label: 'Print Working Directory',
command: 'pwd',
appendEnter: true
},
{
id: 'default-git-status',
label: 'Git Status',
command: 'git status',
appendEnter: true
}
])
).toEqual([])
})
it('drops malformed entries and normalizes valid commands and drafts', () => {
expect(
normalizeTerminalQuickCommands([
null,
{ id: 'status', label: ' Status ', command: 'git status\n', appendEnter: false },
{ id: 'empty-command', label: 'Empty', command: ' ' },
{ id: 'status', label: 'Duplicate', command: 'pwd' },
{ label: 'No ID', command: 'date' }
])
).toEqual([
{
id: 'status',
label: 'Status',
command: 'git status',
appendEnter: false,
scope: { type: 'global' }
},
{
id: 'empty-command',
label: 'Empty',
command: '',
appendEnter: true,
scope: { type: 'global' }
},
{
id: 'status-2',
label: 'Duplicate',
command: 'pwd',
appendEnter: true,
scope: { type: 'global' }
},
{
id: 'quick-command-4',
label: 'No ID',
command: 'date',
appendEnter: true,
scope: { type: 'global' }
}
])
})
it('normalizes repository scoped commands and falls back to global for invalid scopes', () => {
expect(
normalizeTerminalQuickCommands([
{
id: 'repo-dev',
label: 'Dev',
command: 'pnpm dev',
scope: { type: 'repo', repoId: ' repo-1 ' }
},
{
id: 'bad-repo',
label: 'Bad',
command: 'echo bad',
scope: { type: 'repo', repoId: ' ' }
}
])
).toEqual([
{
id: 'repo-dev',
label: 'Dev',
command: 'pnpm dev',
appendEnter: true,
scope: { type: 'repo', repoId: 'repo-1' }
},
{
id: 'bad-repo',
label: 'Bad',
command: 'echo bad',
appendEnter: true,
scope: { type: 'global' }
}
])
})
it('matches global commands everywhere and repo commands only in their repo', () => {
expect(
terminalQuickCommandMatchesRepo(
{
id: 'global',
label: 'Global',
command: 'date',
appendEnter: true,
scope: { type: 'global' }
},
null
)
).toBe(true)
expect(
terminalQuickCommandMatchesRepo(
{
id: 'repo',
label: 'Repo',
command: 'pnpm dev',
appendEnter: true,
scope: { type: 'repo', repoId: 'repo-1' }
},
'repo-1'
)
).toBe(true)
expect(
terminalQuickCommandMatchesRepo(
{
id: 'repo',
label: 'Repo',
command: 'pnpm dev',
appendEnter: true,
scope: { type: 'repo', repoId: 'repo-1' }
},
'repo-2'
)
).toBe(false)
})
it('formats terminal input without assuming shell semantics', () => {
expect(
buildTerminalQuickCommandInput({
id: 'status',
label: 'Status',
command: 'git status',
appendEnter: true
})
).toBe('git status\r')
expect(
buildTerminalQuickCommandInput({
id: 'status',
label: 'Status',
command: 'git status',
appendEnter: false
})
).toBe('git status')
})
})
describe('flattenTerminalQuickCommand', () => {
it('returns the same object when there are no line breaks', () => {
const command = {
id: 'test',
label: 'Test',
command: 'git status',
appendEnter: true
} as const
expect(flattenTerminalQuickCommand(command)).toBe(command)
})
it('replaces newlines with semicolons and spaces', () => {
const result = flattenTerminalQuickCommand({
id: 'test',
label: 'Test',
command: 'cd packages\nbun run build\ncd ..',
appendEnter: true
})
expect(result.command).toBe('cd packages; bun run build; cd ..')
})
it('collapses consecutive newlines into a single separator', () => {
const result = flattenTerminalQuickCommand({
id: 'test',
label: 'Test',
command: 'echo one\n\n\necho two',
appendEnter: true
})
expect(result.command).toBe('echo one; echo two')
})
it('handles Windows-style CRLF endings', () => {
const result = flattenTerminalQuickCommand({
id: 'test',
label: 'Test',
command: 'echo one\r\necho two',
appendEnter: true
})
expect(result.command).toBe('echo one; echo two')
})
it('drops empty edge lines without leaving dangling separators', () => {
const result = flattenTerminalQuickCommand({
id: 'test',
label: 'Test',
command: '\n echo one \n\n echo two\n',
appendEnter: true
})
expect(result.command).toBe('echo one; echo two')
})
})