|
1 | 1 | import { test } from "node:test"; |
2 | 2 | import assert from "node:assert/strict"; |
| 3 | +import stringWidth from "string-width"; |
3 | 4 | import { buildLoadingText } from "../ui"; |
4 | 5 |
|
5 | 6 | test("buildLoadingText returns plain Thinking... when no progress", () => { |
@@ -109,3 +110,60 @@ test("buildLoadingText falls back to Thinking... when timestamp is unparseable", |
109 | 110 | }); |
110 | 111 | assert.equal(text, "Thinking..."); |
111 | 112 | }); |
| 113 | + |
| 114 | +const previewProgress = { |
| 115 | + requestId: "preview", |
| 116 | + startedAt: "2026-04-28T00:00:00.000Z", |
| 117 | + estimatedTokens: 1501, |
| 118 | + formattedTokens: "1.5k", |
| 119 | + phase: "update" as const, |
| 120 | + previewText: "latest text", |
| 121 | +}; |
| 122 | +const previewNow = Date.parse(previewProgress.startedAt) + 5000; |
| 123 | +const previewStatus = "Thinking... (5s) · ↓ 1.5k tokens"; |
| 124 | + |
| 125 | +test("loading preview requires more than 1500 tokens and preserves status priority", () => { |
| 126 | + const input = { progress: previewProgress, now: previewNow, screenWidth: 100 }; |
| 127 | + assert.equal(buildLoadingText(input), `${previewStatus} [latest text]`); |
| 128 | + assert.equal(buildLoadingText({ ...input, progress: { ...previewProgress, estimatedTokens: 1500 } }), previewStatus); |
| 129 | + assert.equal(buildLoadingText({ ...input, progress: { ...previewProgress, previewText: "" } }), previewStatus); |
| 130 | + assert.equal(buildLoadingText({ ...input, now: previewNow - 4000 }), "Thinking..."); |
| 131 | + assert.equal( |
| 132 | + buildLoadingText({ |
| 133 | + ...input, |
| 134 | + processes: new Map([["p", { startTime: previewProgress.startedAt, command: "cmd" }]]), |
| 135 | + }), |
| 136 | + "(5s) cmd" |
| 137 | + ); |
| 138 | + assert.equal( |
| 139 | + buildLoadingText({ ...input, retry: { requestId: "r", error: "err", attempt: 1, maxRetries: 5, delayMs: 800 } }), |
| 140 | + "Reconnecting... 1/5 (esc to interrupt)" |
| 141 | + ); |
| 142 | +}); |
| 143 | + |
| 144 | +test("loading preview keeps the newest complete graphemes within the reserved boundary", () => { |
| 145 | + const previewText = "old ".repeat(100) + "中文👨👩👧👦é"; |
| 146 | + for (const screenWidth of [35, 60, 70, 80, 100, 200]) { |
| 147 | + const text = buildLoadingText({ progress: { ...previewProgress, previewText }, now: previewNow, screenWidth }); |
| 148 | + if (text !== previewStatus) { |
| 149 | + assert.ok(stringWidth(text) <= screenWidth - 28); |
| 150 | + assert.ok(text.startsWith(`${previewStatus} [...`)); |
| 151 | + assert.ok(text.endsWith("é]")); |
| 152 | + const tail = text.slice(previewStatus.length + 5, -1); |
| 153 | + assert.ok(previewText.endsWith(tail)); |
| 154 | + assert.ok(!tail.startsWith("\u200d")); |
| 155 | + } |
| 156 | + } |
| 157 | + assert.equal(buildLoadingText({ progress: previewProgress, now: previewNow, screenWidth: 40 }), previewStatus); |
| 158 | +}); |
| 159 | + |
| 160 | +test("loading preview hides below 80 columns and returns when the terminal grows", () => { |
| 161 | + const input = { progress: previewProgress, now: previewNow }; |
| 162 | + for (const screenWidth of [40, 60, 70, 79, 0]) { |
| 163 | + assert.equal(buildLoadingText({ ...input, screenWidth }), previewStatus); |
| 164 | + } |
| 165 | + assert.equal(buildLoadingText(input), previewStatus); |
| 166 | + for (const screenWidth of [80, 100, 160]) { |
| 167 | + assert.equal(buildLoadingText({ ...input, screenWidth }), `${previewStatus} [latest text]`); |
| 168 | + } |
| 169 | +}); |
0 commit comments