|
| 1 | +// pnpm --filter @roo-code/vscode-webview test src/components/chat/__tests__/ChatView.resume-strip-not-streaming.spec.tsx |
| 2 | + |
| 3 | +import fs from "fs" |
| 4 | +import path from "path" |
| 5 | +import { fileURLToPath } from "url" |
| 6 | + |
| 7 | +import React from "react" |
| 8 | +import { renderWithExtensionState, makeExtensionState, waitFor } from "@/utils/test-utils" |
| 9 | + |
| 10 | +import type { ClineMessage } from "@roo-code/types" |
| 11 | + |
| 12 | +import ChatView, { ChatViewProps } from "../ChatView" |
| 13 | + |
| 14 | +// #257: `Task.resumeTaskFromHistory()` trims a streaming tail before hydrating the |
| 15 | +// resumed chat (src/core/task/Task.ts:3154-3184), and the webview derives its |
| 16 | +// streaming lock from the message list alone (ChatView.tsx:566-608). The host spec |
| 17 | +// (Task.persistence.spec.ts:2335-2441) pins the strip; this spec pins the other half |
| 18 | +// of the contract: the shape the strip guarantees must not read as streaming, and |
| 19 | +// the shape it removes must. The strip list is read from the host source instead of |
| 20 | +// being copied here, so widening or narrowing it on the host side fails this spec. |
| 21 | + |
| 22 | +// __tests__ -> chat -> components -> src -> webview-ui -> repo root |
| 23 | +const HOST_TASK_SOURCE_PATH = path.resolve( |
| 24 | + path.dirname(fileURLToPath(import.meta.url)), |
| 25 | + "../../../../../src/core/task/Task.ts", |
| 26 | +) |
| 27 | + |
| 28 | +/** |
| 29 | + * Extracts the `say` types the host resume cleanup pops from the source expression |
| 30 | + * `(lastMessage.say === "reasoning" || lastMessage.say === "text")`. Reading the |
| 31 | + * source keeps this spec coupled to the host strip list; a hand-copied list would |
| 32 | + * let the two sides drift silently, which is the coupling this spec exists to catch. |
| 33 | + */ |
| 34 | +const readHostStrippedPartialSayTypes = (): string[] => { |
| 35 | + const source = fs.readFileSync(HOST_TASK_SOURCE_PATH, "utf8") |
| 36 | + const start = source.indexOf("const isPartialStreamBubble =") |
| 37 | + |
| 38 | + if (start === -1) { |
| 39 | + throw new Error(`Could not find the resume strip expression in ${HOST_TASK_SOURCE_PATH}`) |
| 40 | + } |
| 41 | + |
| 42 | + const end = source.indexOf("lastMessage.partial === true", start) |
| 43 | + |
| 44 | + if (end === -1) { |
| 45 | + throw new Error(`Could not find the end of the resume strip expression in ${HOST_TASK_SOURCE_PATH}`) |
| 46 | + } |
| 47 | + |
| 48 | + return [...source.slice(start, end).matchAll(/lastMessage\.say === "([^"]+)"/g)].map((match) => match[1]!) |
| 49 | +} |
| 50 | + |
| 51 | +const HOST_STRIPPED_PARTIAL_SAY_TYPES = readHostStrippedPartialSayTypes() |
| 52 | + |
| 53 | +/** |
| 54 | + * `say` types the streaming loop can emit with `partial: true` and a text body. |
| 55 | + * Producers: `presentAssistantMessage.ts:443` (text) and `Task.ts:4215` (reasoning). |
| 56 | + * Every one of them must be in the host strip list, otherwise an interrupted stream |
| 57 | + * leaves the bubble behind and the resumed chat re-locks on `partial === true`. |
| 58 | + * |
| 59 | + * This list is the contract the host strip is checked against, so it is written |
| 60 | + * explicitly here instead of being derived from the host source. Deriving both sides |
| 61 | + * from the same expression would let a narrowed host list pass unnoticed. |
| 62 | + */ |
| 63 | +const STREAMING_PARTIAL_SAY_TYPES = ["reasoning", "text"] as const |
| 64 | + |
| 65 | +/** |
| 66 | + * Applies the resume-time cleanup from `Task.resumeTaskFromHistory()` so the |
| 67 | + * post-strip assertions are derived from the pre-strip history the host reads off |
| 68 | + * disk instead of being hand-written twice. |
| 69 | + */ |
| 70 | +const applyHostResumeStrip = (messages: ClineMessage[]): ClineMessage[] => { |
| 71 | + const stripped = [...messages] |
| 72 | + |
| 73 | + while (stripped.length > 0) { |
| 74 | + const lastMessage = stripped[stripped.length - 1] |
| 75 | + |
| 76 | + if ( |
| 77 | + lastMessage?.type !== "say" || |
| 78 | + !HOST_STRIPPED_PARTIAL_SAY_TYPES.includes(lastMessage.say ?? "") || |
| 79 | + lastMessage.partial !== true |
| 80 | + ) { |
| 81 | + break |
| 82 | + } |
| 83 | + |
| 84 | + stripped.pop() |
| 85 | + } |
| 86 | + |
| 87 | + const lastApiReqStartedIndex = stripped.findLastIndex( |
| 88 | + (message) => message.type === "say" && message.say === "api_req_started", |
| 89 | + ) |
| 90 | + |
| 91 | + if (lastApiReqStartedIndex !== -1) { |
| 92 | + const lastApiReqStarted = stripped[lastApiReqStartedIndex] |
| 93 | + const { cost, cancelReason } = JSON.parse(lastApiReqStarted?.text || "{}") |
| 94 | + |
| 95 | + if (cost === undefined && cancelReason === undefined) { |
| 96 | + stripped.splice(lastApiReqStartedIndex, 1) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return stripped |
| 101 | +} |
| 102 | + |
| 103 | +vi.mock("@src/utils/vscode", () => ({ |
| 104 | + vscode: { |
| 105 | + postMessage: vi.fn(), |
| 106 | + }, |
| 107 | +})) |
| 108 | + |
| 109 | +vi.mock("use-sound", () => ({ |
| 110 | + default: vi.fn().mockImplementation(() => [vi.fn()]), |
| 111 | +})) |
| 112 | + |
| 113 | +vi.mock("react-i18next", () => ({ |
| 114 | + useTranslation: () => ({ |
| 115 | + t: (key: string) => key, |
| 116 | + }), |
| 117 | + initReactI18next: { |
| 118 | + type: "3rdParty", |
| 119 | + init: () => {}, |
| 120 | + }, |
| 121 | + Trans: ({ i18nKey, children }: { i18nKey: string; children?: React.ReactNode }) => <>{children || i18nKey}</>, |
| 122 | +})) |
| 123 | + |
| 124 | +vi.mock("../ChatRow", () => ({ |
| 125 | + default: function MockChatRow({ message }: { message: ClineMessage }) { |
| 126 | + return <div data-testid="chat-row">{JSON.stringify(message)}</div> |
| 127 | + }, |
| 128 | +})) |
| 129 | + |
| 130 | +// The send button is the user-visible half of the lock: `isStreaming` swaps Send |
| 131 | +// for Stop and routes input into the queue instead of sending. |
| 132 | +vi.mock("../ChatTextArea", () => ({ |
| 133 | + ChatTextArea: React.forwardRef(function MockChatTextArea( |
| 134 | + { isStreaming }: { isStreaming?: boolean }, |
| 135 | + _ref: React.ForwardedRef<HTMLTextAreaElement>, |
| 136 | + ) { |
| 137 | + return <div data-testid="chat-textarea" data-is-streaming={String(isStreaming === true)} /> |
| 138 | + }), |
| 139 | +})) |
| 140 | + |
| 141 | +vi.mock("../AutoApproveMenu", () => ({ |
| 142 | + default: () => null, |
| 143 | +})) |
| 144 | + |
| 145 | +vi.mock("react-virtuoso", () => ({ |
| 146 | + Virtuoso: function MockVirtuoso({ |
| 147 | + data, |
| 148 | + itemContent, |
| 149 | + }: { |
| 150 | + data: ClineMessage[] |
| 151 | + itemContent: (index: number, item: ClineMessage) => React.ReactNode |
| 152 | + }) { |
| 153 | + return ( |
| 154 | + <div data-testid="virtuoso-item-list"> |
| 155 | + {data.map((item, index) => ( |
| 156 | + <div key={item.ts}>{itemContent(index, item)}</div> |
| 157 | + ))} |
| 158 | + </div> |
| 159 | + ) |
| 160 | + }, |
| 161 | +})) |
| 162 | + |
| 163 | +vi.mock("../../common/VersionIndicator", () => ({ |
| 164 | + default: () => null, |
| 165 | +})) |
| 166 | + |
| 167 | +vi.mock("@src/components/welcome/RooTips", () => ({ |
| 168 | + default: () => null, |
| 169 | +})) |
| 170 | + |
| 171 | +vi.mock("@src/components/welcome/RooHero", () => ({ |
| 172 | + default: () => null, |
| 173 | +})) |
| 174 | + |
| 175 | +vi.mock("../common/TelemetryBanner", () => ({ |
| 176 | + default: () => null, |
| 177 | +})) |
| 178 | + |
| 179 | +const defaultProps: ChatViewProps = { |
| 180 | + isHidden: false, |
| 181 | + showAnnouncement: false, |
| 182 | + hideAnnouncement: () => {}, |
| 183 | +} |
| 184 | + |
| 185 | +const renderWithMessages = (clineMessages: ClineMessage[]) => |
| 186 | + renderWithExtensionState(<ChatView {...defaultProps} />, { |
| 187 | + state: makeExtensionState({ clineMessages }), |
| 188 | + }) |
| 189 | + |
| 190 | +const expectStreaming = async (expected: boolean) => { |
| 191 | + await waitFor(() => { |
| 192 | + expect(document.querySelector('[data-testid="chat-textarea"]')).toHaveAttribute( |
| 193 | + "data-is-streaming", |
| 194 | + String(expected), |
| 195 | + ) |
| 196 | + }) |
| 197 | +} |
| 198 | + |
| 199 | +const taskMessage: ClineMessage = { type: "say", say: "text", ts: 1, text: "Original task" } |
| 200 | +const settledApiReqStarted: ClineMessage = { |
| 201 | + type: "say", |
| 202 | + say: "api_req_started", |
| 203 | + ts: 3, |
| 204 | + text: JSON.stringify({ apiProtocol: "anthropic", cost: 0.002 }), |
| 205 | +} |
| 206 | +const danglingApiReqStarted: ClineMessage = { |
| 207 | + type: "say", |
| 208 | + say: "api_req_started", |
| 209 | + ts: 3, |
| 210 | + text: JSON.stringify({ apiProtocol: "anthropic" }), |
| 211 | +} |
| 212 | + |
| 213 | +describe("ChatView - resume strip leaves a non-streaming chat", () => { |
| 214 | + beforeEach(() => vi.clearAllMocks()) |
| 215 | + |
| 216 | + it("keeps every streaming partial say type in the host strip list", () => { |
| 217 | + // A silent change to the parse target would otherwise leave the assertions |
| 218 | + // below running against an empty list and passing for the wrong reason. |
| 219 | + expect(HOST_STRIPPED_PARTIAL_SAY_TYPES.length).toBeGreaterThan(0) |
| 220 | + // Dropping a say type from the host strip list is exactly the decoupling this |
| 221 | + // spec exists to catch: the bubble survives resume and the chat re-locks. |
| 222 | + expect(HOST_STRIPPED_PARTIAL_SAY_TYPES).toEqual(expect.arrayContaining([...STREAMING_PARTIAL_SAY_TYPES])) |
| 223 | + }) |
| 224 | + |
| 225 | + it.each(STREAMING_PARTIAL_SAY_TYPES)( |
| 226 | + "reads a trailing partial %s bubble as streaming before the host strips it", |
| 227 | + async (say) => { |
| 228 | + // This is the shape that reaches the webview when the host strip list loses a |
| 229 | + // say type: the resumed chat renders "API Request..." with a Stop button. |
| 230 | + renderWithMessages([taskMessage, { type: "say", say, ts: 2, text: "Half a sentence", partial: true }]) |
| 231 | + |
| 232 | + await expectStreaming(true) |
| 233 | + }, |
| 234 | + ) |
| 235 | + |
| 236 | + it("reads a trailing dangling api_req_started as streaming before the host strips it", async () => { |
| 237 | + renderWithMessages([taskMessage, danglingApiReqStarted]) |
| 238 | + |
| 239 | + await expectStreaming(true) |
| 240 | + }) |
| 241 | + |
| 242 | + it.each(STREAMING_PARTIAL_SAY_TYPES)( |
| 243 | + "reads the post-strip shape left by stripping a partial %s tail as not streaming", |
| 244 | + async (say) => { |
| 245 | + const partialTail: ClineMessage = { type: "say", say, ts: 2, text: "Half a sentence", partial: true } |
| 246 | + const postStrip = applyHostResumeStrip([taskMessage, partialTail]) |
| 247 | + |
| 248 | + // If the host strip list loses this say type, the partial tail survives and the |
| 249 | + // resumed chat stays locked, so this assertion is what catches the drift. |
| 250 | + expect(postStrip).toEqual([taskMessage]) |
| 251 | + renderWithMessages(postStrip) |
| 252 | + |
| 253 | + await expectStreaming(false) |
| 254 | + }, |
| 255 | + ) |
| 256 | + |
| 257 | + it("reads the post-strip shape left by stripping a dangling api_req_started as not streaming", async () => { |
| 258 | + const postStrip = applyHostResumeStrip([taskMessage, settledApiReqStarted, danglingApiReqStarted]) |
| 259 | + |
| 260 | + expect(postStrip).toEqual([taskMessage, settledApiReqStarted]) |
| 261 | + renderWithMessages(postStrip) |
| 262 | + |
| 263 | + await expectStreaming(false) |
| 264 | + }) |
| 265 | + |
| 266 | + it("does not stream on a kept partial command_output tail", async () => { |
| 267 | + // The host deliberately keeps partial command output (it records work that |
| 268 | + // already ran). `combineCommandSequences` folds it into the command ask, so it |
| 269 | + // never becomes the last message the streaming check looks at. |
| 270 | + renderWithMessages([ |
| 271 | + taskMessage, |
| 272 | + { type: "ask", ask: "command", ts: 2, text: "npm test", partial: false }, |
| 273 | + { type: "say", say: "command_output", ts: 3, text: "npm test\n", partial: true }, |
| 274 | + ]) |
| 275 | + |
| 276 | + await expectStreaming(false) |
| 277 | + }) |
| 278 | +}) |
0 commit comments