|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { render } from 'ink-testing-library'; |
| 8 | +import { describe, it, expect } from 'vitest'; |
| 9 | +import { Box } from 'ink'; |
| 10 | +import { AnchoredTodoListDisplay, TodoListDisplay } from './Todo.js'; |
| 11 | +import type { TodoList, TodoStatus } from '@google/gemini-cli-core'; |
| 12 | +import type { UIState } from '../../contexts/UIStateContext.js'; |
| 13 | +import { UIStateContext } from '../../contexts/UIStateContext.js'; |
| 14 | +import type { HistoryItem } from '../../types.js'; |
| 15 | +import { ToolCallStatus } from '../../types.js'; |
| 16 | + |
| 17 | +describe('<TodoListDisplay />', () => { |
| 18 | + it('renders an empty todo list correctly', () => { |
| 19 | + const todos: TodoList = { todos: [] }; |
| 20 | + const { lastFrame } = render(<TodoListDisplay todos={todos} />); |
| 21 | + expect(lastFrame()).toMatchSnapshot(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('renders a todo list with various statuses correctly', () => { |
| 25 | + const todos: TodoList = { |
| 26 | + todos: [ |
| 27 | + { description: 'Task 1', status: 'pending' as TodoStatus }, |
| 28 | + { description: 'Task 2', status: 'in_progress' as TodoStatus }, |
| 29 | + { description: 'Task 3', status: 'completed' as TodoStatus }, |
| 30 | + { description: 'Task 4', status: 'cancelled' as TodoStatus }, |
| 31 | + ], |
| 32 | + }; |
| 33 | + const { lastFrame } = render(<TodoListDisplay todos={todos} />); |
| 34 | + expect(lastFrame()).toMatchSnapshot(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('renders a todo list with long descriptions that wrap', () => { |
| 38 | + const todos: TodoList = { |
| 39 | + todos: [ |
| 40 | + { |
| 41 | + description: |
| 42 | + 'This is a very long description for a pending task that should wrap around multiple lines when the terminal width is constrained.', |
| 43 | + status: 'pending' as TodoStatus, |
| 44 | + }, |
| 45 | + { |
| 46 | + description: |
| 47 | + 'Another completed task with an equally verbose description to test wrapping behavior.', |
| 48 | + status: 'completed' as TodoStatus, |
| 49 | + }, |
| 50 | + ], |
| 51 | + }; |
| 52 | + const { lastFrame } = render( |
| 53 | + <Box width="30"> |
| 54 | + <TodoListDisplay todos={todos} /> |
| 55 | + </Box>, |
| 56 | + ); |
| 57 | + expect(lastFrame()).toMatchSnapshot(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('renders a single todo item', () => { |
| 61 | + const todos: TodoList = { |
| 62 | + todos: [{ description: 'Single task', status: 'pending' as TodoStatus }], |
| 63 | + }; |
| 64 | + const { lastFrame } = render(<TodoListDisplay todos={todos} />); |
| 65 | + expect(lastFrame()).toMatchSnapshot(); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +describe('<AnchoredTodoListDisplay />', () => { |
| 70 | + const mockHistoryItem = { |
| 71 | + type: 'tool_group', |
| 72 | + id: '1', |
| 73 | + tools: [ |
| 74 | + { |
| 75 | + name: 'write_todos_list', |
| 76 | + callId: 'tool-1', |
| 77 | + status: ToolCallStatus.Success, |
| 78 | + resultDisplay: { |
| 79 | + todos: [ |
| 80 | + { description: 'Pending Task', status: 'pending' }, |
| 81 | + { description: 'In Progress Task', status: 'in_progress' }, |
| 82 | + { description: 'Completed Task', status: 'completed' }, |
| 83 | + ], |
| 84 | + }, |
| 85 | + }, |
| 86 | + ], |
| 87 | + } as unknown as HistoryItem; |
| 88 | + |
| 89 | + const renderWithUiState = (uiState: Partial<UIState>) => |
| 90 | + render( |
| 91 | + <UIStateContext.Provider value={uiState as UIState}> |
| 92 | + <AnchoredTodoListDisplay /> |
| 93 | + </UIStateContext.Provider>, |
| 94 | + ); |
| 95 | + |
| 96 | + it('renders null when no todos are in the history', () => { |
| 97 | + const { lastFrame } = renderWithUiState({ history: [] }); |
| 98 | + expect(lastFrame()).toMatchSnapshot(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('renders null when todos exist but none are in progress and full view is off', () => { |
| 102 | + const historyWithNoInProgress = { |
| 103 | + type: 'tool_group', |
| 104 | + id: '1', |
| 105 | + tools: [ |
| 106 | + { |
| 107 | + name: 'write_todos_list', |
| 108 | + callId: 'tool-1', |
| 109 | + status: ToolCallStatus.Success, |
| 110 | + resultDisplay: { |
| 111 | + todos: [ |
| 112 | + { description: 'Pending Task', status: 'pending' }, |
| 113 | + { description: 'In Progress Task', status: 'cancelled' }, |
| 114 | + { description: 'Completed Task', status: 'completed' }, |
| 115 | + ], |
| 116 | + }, |
| 117 | + }, |
| 118 | + ], |
| 119 | + } as unknown as HistoryItem; |
| 120 | + const { lastFrame } = renderWithUiState({ |
| 121 | + history: [historyWithNoInProgress], |
| 122 | + showFullTodos: false, |
| 123 | + }); |
| 124 | + expect(lastFrame()).toMatchSnapshot(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('renders only the in-progress task when full view is off', () => { |
| 128 | + const { lastFrame } = renderWithUiState({ |
| 129 | + history: [mockHistoryItem], |
| 130 | + showFullTodos: false, |
| 131 | + }); |
| 132 | + expect(lastFrame()).toMatchSnapshot(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('renders the full todo list when full view is on', () => { |
| 136 | + const { lastFrame } = renderWithUiState({ |
| 137 | + history: [mockHistoryItem], |
| 138 | + showFullTodos: true, |
| 139 | + }); |
| 140 | + expect(lastFrame()).toMatchSnapshot(); |
| 141 | + }); |
| 142 | +}); |
0 commit comments