|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect } from 'vitest'; |
| 8 | +import { |
| 9 | + calculateToolContentMaxLines, |
| 10 | + calculateShellMaxLines, |
| 11 | + SHELL_CONTENT_OVERHEAD, |
| 12 | +} from './toolLayoutUtils.js'; |
| 13 | +import { CoreToolCallStatus } from '@google/gemini-cli-core'; |
| 14 | +import { |
| 15 | + ACTIVE_SHELL_MAX_LINES, |
| 16 | + COMPLETED_SHELL_MAX_LINES, |
| 17 | +} from '../constants.js'; |
| 18 | + |
| 19 | +describe('toolLayoutUtils', () => { |
| 20 | + describe('calculateToolContentMaxLines', () => { |
| 21 | + interface CalculateToolContentMaxLinesTestCase { |
| 22 | + desc: string; |
| 23 | + options: Parameters<typeof calculateToolContentMaxLines>[0]; |
| 24 | + expected: number | undefined; |
| 25 | + } |
| 26 | + |
| 27 | + const testCases: CalculateToolContentMaxLinesTestCase[] = [ |
| 28 | + { |
| 29 | + desc: 'returns undefined if availableTerminalHeight is undefined', |
| 30 | + options: { |
| 31 | + availableTerminalHeight: undefined, |
| 32 | + isAlternateBuffer: false, |
| 33 | + }, |
| 34 | + expected: undefined, |
| 35 | + }, |
| 36 | + { |
| 37 | + desc: 'returns maxLinesLimit if maxLinesLimit applies but availableTerminalHeight is undefined', |
| 38 | + options: { |
| 39 | + availableTerminalHeight: undefined, |
| 40 | + isAlternateBuffer: false, |
| 41 | + maxLinesLimit: 10, |
| 42 | + }, |
| 43 | + expected: 10, |
| 44 | + }, |
| 45 | + { |
| 46 | + desc: 'returns available space directly in constrained terminal (Standard mode)', |
| 47 | + options: { |
| 48 | + availableTerminalHeight: 2, |
| 49 | + isAlternateBuffer: false, |
| 50 | + }, |
| 51 | + expected: 3, |
| 52 | + }, |
| 53 | + { |
| 54 | + desc: 'returns available space directly in constrained terminal (ASB mode)', |
| 55 | + options: { |
| 56 | + availableTerminalHeight: 4, |
| 57 | + isAlternateBuffer: true, |
| 58 | + }, |
| 59 | + expected: 3, |
| 60 | + }, |
| 61 | + { |
| 62 | + desc: 'returns remaining space if sufficient space exists (Standard mode)', |
| 63 | + options: { |
| 64 | + availableTerminalHeight: 20, |
| 65 | + isAlternateBuffer: false, |
| 66 | + }, |
| 67 | + expected: 17, |
| 68 | + }, |
| 69 | + { |
| 70 | + desc: 'returns remaining space if sufficient space exists (ASB mode)', |
| 71 | + options: { |
| 72 | + availableTerminalHeight: 20, |
| 73 | + isAlternateBuffer: true, |
| 74 | + }, |
| 75 | + expected: 13, |
| 76 | + }, |
| 77 | + ]; |
| 78 | + |
| 79 | + it.each(testCases)('$desc', ({ options, expected }) => { |
| 80 | + const result = calculateToolContentMaxLines(options); |
| 81 | + expect(result).toBe(expected); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('calculateShellMaxLines', () => { |
| 86 | + interface CalculateShellMaxLinesTestCase { |
| 87 | + desc: string; |
| 88 | + options: Parameters<typeof calculateShellMaxLines>[0]; |
| 89 | + expected: number | undefined; |
| 90 | + } |
| 91 | + |
| 92 | + const testCases: CalculateShellMaxLinesTestCase[] = [ |
| 93 | + { |
| 94 | + desc: 'returns undefined when not constrained and is expandable', |
| 95 | + options: { |
| 96 | + status: CoreToolCallStatus.Executing, |
| 97 | + isAlternateBuffer: false, |
| 98 | + isThisShellFocused: false, |
| 99 | + availableTerminalHeight: 20, |
| 100 | + constrainHeight: false, |
| 101 | + isExpandable: true, |
| 102 | + }, |
| 103 | + expected: undefined, |
| 104 | + }, |
| 105 | + { |
| 106 | + desc: 'returns ACTIVE_SHELL_MAX_LINES - SHELL_CONTENT_OVERHEAD for ASB mode when availableTerminalHeight is undefined', |
| 107 | + options: { |
| 108 | + status: CoreToolCallStatus.Executing, |
| 109 | + isAlternateBuffer: true, |
| 110 | + isThisShellFocused: false, |
| 111 | + availableTerminalHeight: undefined, |
| 112 | + constrainHeight: true, |
| 113 | + isExpandable: false, |
| 114 | + }, |
| 115 | + expected: ACTIVE_SHELL_MAX_LINES - SHELL_CONTENT_OVERHEAD, |
| 116 | + }, |
| 117 | + { |
| 118 | + desc: 'returns undefined for Standard mode when availableTerminalHeight is undefined', |
| 119 | + options: { |
| 120 | + status: CoreToolCallStatus.Executing, |
| 121 | + isAlternateBuffer: false, |
| 122 | + isThisShellFocused: false, |
| 123 | + availableTerminalHeight: undefined, |
| 124 | + constrainHeight: true, |
| 125 | + isExpandable: false, |
| 126 | + }, |
| 127 | + expected: undefined, |
| 128 | + }, |
| 129 | + { |
| 130 | + desc: 'handles small availableTerminalHeight gracefully without overflow in Standard mode', |
| 131 | + options: { |
| 132 | + status: CoreToolCallStatus.Executing, |
| 133 | + isAlternateBuffer: false, |
| 134 | + isThisShellFocused: false, |
| 135 | + availableTerminalHeight: 2, |
| 136 | + constrainHeight: true, |
| 137 | + isExpandable: false, |
| 138 | + }, |
| 139 | + expected: 1, |
| 140 | + }, |
| 141 | + { |
| 142 | + desc: 'handles small availableTerminalHeight gracefully without overflow in ASB mode', |
| 143 | + options: { |
| 144 | + status: CoreToolCallStatus.Executing, |
| 145 | + isAlternateBuffer: true, |
| 146 | + isThisShellFocused: false, |
| 147 | + availableTerminalHeight: 6, |
| 148 | + constrainHeight: true, |
| 149 | + isExpandable: false, |
| 150 | + }, |
| 151 | + expected: 4, |
| 152 | + }, |
| 153 | + { |
| 154 | + desc: 'handles negative availableTerminalHeight gracefully', |
| 155 | + options: { |
| 156 | + status: CoreToolCallStatus.Executing, |
| 157 | + isAlternateBuffer: false, |
| 158 | + isThisShellFocused: false, |
| 159 | + availableTerminalHeight: -5, |
| 160 | + constrainHeight: true, |
| 161 | + isExpandable: false, |
| 162 | + }, |
| 163 | + expected: 1, |
| 164 | + }, |
| 165 | + { |
| 166 | + desc: 'returns maxLinesBasedOnHeight for focused ASB shells', |
| 167 | + options: { |
| 168 | + status: CoreToolCallStatus.Executing, |
| 169 | + isAlternateBuffer: true, |
| 170 | + isThisShellFocused: true, |
| 171 | + availableTerminalHeight: 30, |
| 172 | + constrainHeight: false, |
| 173 | + isExpandable: false, |
| 174 | + }, |
| 175 | + expected: 28, |
| 176 | + }, |
| 177 | + { |
| 178 | + desc: 'falls back to COMPLETED_SHELL_MAX_LINES - SHELL_CONTENT_OVERHEAD for completed shells if space allows', |
| 179 | + options: { |
| 180 | + status: CoreToolCallStatus.Success, |
| 181 | + isAlternateBuffer: false, |
| 182 | + isThisShellFocused: false, |
| 183 | + availableTerminalHeight: 100, |
| 184 | + constrainHeight: true, |
| 185 | + isExpandable: false, |
| 186 | + }, |
| 187 | + expected: COMPLETED_SHELL_MAX_LINES - SHELL_CONTENT_OVERHEAD, |
| 188 | + }, |
| 189 | + { |
| 190 | + desc: 'falls back to ACTIVE_SHELL_MAX_LINES - SHELL_CONTENT_OVERHEAD for executing shells if space allows', |
| 191 | + options: { |
| 192 | + status: CoreToolCallStatus.Executing, |
| 193 | + isAlternateBuffer: false, |
| 194 | + isThisShellFocused: false, |
| 195 | + availableTerminalHeight: 100, |
| 196 | + constrainHeight: true, |
| 197 | + isExpandable: false, |
| 198 | + }, |
| 199 | + expected: ACTIVE_SHELL_MAX_LINES - SHELL_CONTENT_OVERHEAD, |
| 200 | + }, |
| 201 | + ]; |
| 202 | + |
| 203 | + it.each(testCases)('$desc', ({ options, expected }) => { |
| 204 | + const result = calculateShellMaxLines(options); |
| 205 | + expect(result).toBe(expected); |
| 206 | + }); |
| 207 | + }); |
| 208 | +}); |
0 commit comments