import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'
import { afterEach, describe, expect, it, vi } from 'vitest'
vi.mock('../../pages/EmptySession', () => ({
EmptySession: () =>
,
}))
vi.mock('../../pages/ActiveSession', () => ({
ActiveSession: () => ,
}))
vi.mock('../../pages/ScheduledTasks', () => ({
ScheduledTasks: () => ,
}))
vi.mock('../../pages/Settings', () => ({
Settings: () => ,
}))
vi.mock('../../pages/TerminalSettings', () => ({
TerminalSettings: ({ active, cwd, onNewTerminal, testId }: { active: boolean; cwd?: string; onNewTerminal: () => void; testId: string }) => (
),
}))
import { ContentRouter } from './ContentRouter'
import { useTabStore } from '../../stores/tabStore'
describe('ContentRouter terminal tabs', () => {
afterEach(() => {
cleanup()
useTabStore.setState({ tabs: [], activeTabId: null })
})
it('renders the active terminal tab as main content', () => {
useTabStore.setState({
tabs: [{ sessionId: '__terminal__1', title: 'Terminal 1', type: 'terminal', status: 'idle', terminalCwd: '/tmp/project' }],
activeTabId: '__terminal__1',
})
render()
expect(screen.getByTestId('terminal-host-__terminal__1')).toHaveAttribute('data-active', 'true')
expect(screen.getByTestId('terminal-host-__terminal__1')).toHaveAttribute('data-cwd', '/tmp/project')
expect(screen.queryByTestId('active-session')).not.toBeInTheDocument()
})
it('keeps terminal tabs mounted while chat content is active', () => {
useTabStore.setState({
tabs: [
{ sessionId: '__terminal__1', title: 'Terminal 1', type: 'terminal', status: 'idle' },
{ sessionId: 'session-1', title: 'Chat', type: 'session', status: 'idle' },
],
activeTabId: 'session-1',
})
render()
expect(screen.getByTestId('terminal-host-__terminal__1')).toHaveAttribute('data-active', 'false')
expect(screen.getByTestId('active-session')).toBeInTheDocument()
})
it('can open another terminal tab from a terminal page', () => {
useTabStore.setState({
tabs: [{ sessionId: '__terminal__1', title: 'Terminal 1', type: 'terminal', status: 'idle', terminalCwd: '/tmp/project' }],
activeTabId: '__terminal__1',
})
render()
fireEvent.click(screen.getByRole('button', { name: 'New Terminal' }))
expect(useTabStore.getState().tabs.filter((tab) => tab.type === 'terminal')).toHaveLength(2)
expect(useTabStore.getState().activeTabId).not.toBe('__terminal__1')
expect(useTabStore.getState().tabs.find((tab) => tab.sessionId === useTabStore.getState().activeTabId)?.terminalCwd).toBe('/tmp/project')
})
})