|
| 1 | +/** |
| 2 | + * Persistent session-status formatting tests. |
| 3 | + * |
| 4 | + * Run: bun run src/components/PromptInput/sessionStatus.test.ts |
| 5 | + */ |
| 6 | + |
| 7 | +import path from 'path' |
| 8 | +import { stringWidth } from '../../ink/stringWidth.js' |
| 9 | +import { |
| 10 | + calculateConsumedContextPercentage, |
| 11 | + formatSessionStatus, |
| 12 | + formatTokenCount, |
| 13 | + shortenSessionCwd, |
| 14 | + type SessionStatusInfo, |
| 15 | +} from './sessionStatus.js' |
| 16 | + |
| 17 | +let passed = 0 |
| 18 | +let failed = 0 |
| 19 | + |
| 20 | +function test(name: string, fn: () => void): void { |
| 21 | + try { |
| 22 | + fn() |
| 23 | + passed++ |
| 24 | + console.log(` ok ${name}`) |
| 25 | + } catch (error: any) { |
| 26 | + failed++ |
| 27 | + console.log(` FAIL ${name}: ${error?.message ?? String(error)}`) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function assert(condition: unknown, message: string): asserts condition { |
| 32 | + if (!condition) throw new Error(message) |
| 33 | +} |
| 34 | + |
| 35 | +const baseInfo: SessionStatusInfo = { |
| 36 | + cwd: '~/work/tau', |
| 37 | + provider: 'Anthropic', |
| 38 | + model: 'Claude Sonnet 4.6', |
| 39 | + usedContextTokens: 36_000, |
| 40 | + contextWindowTokens: 200_000, |
| 41 | + consumedContextPercentage: 18, |
| 42 | +} |
| 43 | + |
| 44 | +console.log('session status:') |
| 45 | + |
| 46 | +test('collapses the home directory and its descendants', () => { |
| 47 | + const home = path.resolve(path.sep, 'Users', 'tau-user') |
| 48 | + const project = path.join(home, 'work', 'tau') |
| 49 | + |
| 50 | + assert(shortenSessionCwd(home, home, path) === '~', 'home should become ~') |
| 51 | + assert( |
| 52 | + shortenSessionCwd(project, home, path) === path.join('~', 'work', 'tau'), |
| 53 | + 'home descendant should retain its relative path', |
| 54 | + ) |
| 55 | +}) |
| 56 | + |
| 57 | +test('does not collapse a sibling which only shares the home prefix', () => { |
| 58 | + const parent = path.resolve(path.sep, 'Users') |
| 59 | + const home = path.join(parent, 'tau') |
| 60 | + const sibling = path.join(parent, 'tau-backup') |
| 61 | + |
| 62 | + assert( |
| 63 | + shortenSessionCwd(sibling, home, path) === sibling, |
| 64 | + 'sibling path must remain absolute', |
| 65 | + ) |
| 66 | +}) |
| 67 | + |
| 68 | +test('abbreviates token counts without reading as zero', () => { |
| 69 | + assert(formatTokenCount(0) === '0', 'zero should stay exact') |
| 70 | + assert(formatTokenCount(840) === '840', 'sub-thousand counts stay exact') |
| 71 | + assert(formatTokenCount(16_000) === '16K', 'thousands abbreviate to K') |
| 72 | + assert(formatTokenCount(1_200) === '1K', 'a partial thousand rounds to K') |
| 73 | + assert(formatTokenCount(999_600) === '1M', 'never renders as 1000K') |
| 74 | + assert(formatTokenCount(1_000_000) === '1M', 'a round million drops the .0') |
| 75 | + assert(formatTokenCount(1_500_000) === '1.5M', 'millions keep one decimal') |
| 76 | + assert(formatTokenCount(200_000) === '200K', 'window sizes abbreviate too') |
| 77 | + assert(formatTokenCount(Number.NaN) === '0', 'invalid counts degrade to 0') |
| 78 | +}) |
| 79 | + |
| 80 | +test('shows tokens used, the window, and the percentage beside the bar', () => { |
| 81 | + const status = formatSessionStatus( |
| 82 | + { ...baseInfo, usedContextTokens: 16_000, contextWindowTokens: 1_000_000, consumedContextPercentage: 1.6 }, |
| 83 | + 120, |
| 84 | + ) |
| 85 | + assert( |
| 86 | + status === |
| 87 | + '~/work/tau · Anthropic / Claude Sonnet 4.6 · Context ░░░░░░░░░░ 16K/1M (2%)', |
| 88 | + `unexpected wide status: ${status}`, |
| 89 | + ) |
| 90 | +}) |
| 91 | + |
| 92 | +test('uses the descriptive format when the terminal has room', () => { |
| 93 | + const status = formatSessionStatus(baseInfo, 120) |
| 94 | + assert( |
| 95 | + status === |
| 96 | + '~/work/tau · Anthropic / Claude Sonnet 4.6 · Context ██░░░░░░░░ 36K/200K (18%)', |
| 97 | + `unexpected wide status: ${status}`, |
| 98 | + ) |
| 99 | +}) |
| 100 | + |
| 101 | +test('shows an unknown context until usage has been measured', () => { |
| 102 | + const status = formatSessionStatus( |
| 103 | + { |
| 104 | + ...baseInfo, |
| 105 | + usedContextTokens: null, |
| 106 | + consumedContextPercentage: null, |
| 107 | + }, |
| 108 | + 120, |
| 109 | + ) |
| 110 | + assert( |
| 111 | + status.endsWith('Context ░░░░░░░░░░ --'), |
| 112 | + `unexpected context label: ${status}`, |
| 113 | + ) |
| 114 | +}) |
| 115 | + |
| 116 | +test('omits the window size when the model does not report one', () => { |
| 117 | + const status = formatSessionStatus( |
| 118 | + { ...baseInfo, contextWindowTokens: 0 }, |
| 119 | + 120, |
| 120 | + ) |
| 121 | + assert( |
| 122 | + status.endsWith('Context ██░░░░░░░░ 18%'), |
| 123 | + `unexpected context label: ${status}`, |
| 124 | + ) |
| 125 | +}) |
| 126 | + |
| 127 | +test('keeps the token counts on a moderately narrow row', () => { |
| 128 | + const columns = 64 |
| 129 | + const status = formatSessionStatus(baseInfo, columns) |
| 130 | + |
| 131 | + assert(stringWidth(status) <= columns - 4, 'status exceeds padded width') |
| 132 | + assert( |
| 133 | + status.endsWith('36K/200K (18%)'), |
| 134 | + `token counts should survive: ${status}`, |
| 135 | + ) |
| 136 | + assert(status.includes('Anthropic'), 'provider should remain identifiable') |
| 137 | +}) |
| 138 | + |
| 139 | +test('keeps cwd, provider/model, and context on one narrow row', () => { |
| 140 | + const columns = 42 |
| 141 | + const status = formatSessionStatus(baseInfo, columns) |
| 142 | + |
| 143 | + assert(stringWidth(status) <= columns - 4, 'status exceeds padded width') |
| 144 | + assert(status.includes('·'), 'status should retain field separators') |
| 145 | + assert(status.includes('Anthropic'), 'provider should remain identifiable') |
| 146 | + assert(status.endsWith('█░░░░░ 18%'), 'context bar should remain visible') |
| 147 | +}) |
| 148 | + |
| 149 | +test('measures only supplied conversation tokens against the full window', () => { |
| 150 | + const percentage = calculateConsumedContextPercentage(20_000, 200_000) |
| 151 | + assert(percentage === 10, `unexpected consumed percentage: ${percentage}`) |
| 152 | + assert( |
| 153 | + calculateConsumedContextPercentage(-500, 200_000) === 0, |
| 154 | + 'negative estimates should clamp to zero', |
| 155 | + ) |
| 156 | + assert( |
| 157 | + calculateConsumedContextPercentage(250_000, 200_000) === 100, |
| 158 | + 'usage should clamp to the context window', |
| 159 | + ) |
| 160 | + assert( |
| 161 | + calculateConsumedContextPercentage(20_000, 0) === null, |
| 162 | + 'invalid context windows should remain unknown', |
| 163 | + ) |
| 164 | +}) |
| 165 | + |
| 166 | +test('is display-width safe for unicode and pathological widths', () => { |
| 167 | + const unicodeInfo: SessionStatusInfo = { |
| 168 | + cwd: '~/项目/非常长的目录名称', |
| 169 | + provider: '提供商', |
| 170 | + model: '模型-超长名称', |
| 171 | + usedContextTokens: 202_000, |
| 172 | + contextWindowTokens: 200_000, |
| 173 | + consumedContextPercentage: 101.4, |
| 174 | + } |
| 175 | + |
| 176 | + for (const columns of [5, 12, 16, 24, 40, 56]) { |
| 177 | + const status = formatSessionStatus(unicodeInfo, columns) |
| 178 | + assert( |
| 179 | + stringWidth(status) <= Math.max(0, columns - 4), |
| 180 | + `status exceeds width at ${columns} columns: ${status}`, |
| 181 | + ) |
| 182 | + } |
| 183 | + assert( |
| 184 | + formatSessionStatus(unicodeInfo, 160).endsWith( |
| 185 | + 'Context ██████████ 202K/200K (100%)', |
| 186 | + ), |
| 187 | + 'percentage should clamp to 100 while the counts stay honest', |
| 188 | + ) |
| 189 | + assert( |
| 190 | + formatSessionStatus(unicodeInfo, Number.NaN) === '', |
| 191 | + 'invalid terminal width should not produce layout output', |
| 192 | + ) |
| 193 | +}) |
| 194 | + |
| 195 | +console.log(`\n${passed} passed, ${failed} failed`) |
| 196 | +if (failed > 0) process.exit(1) |
0 commit comments