1import { describe, expect, it } from "vite-plus/test";
2
3import { CursorTransportFailure } from "./transportFailure.ts";
4
5const diagnostic = "Error: RetriableError: WritableIterable is closed";
6
7function failureFor(chunks: string[]) {
8 const reply = new CursorTransportFailure();
9 for (const chunk of chunks) reply.push(chunk);
10 return reply.failure;
11}
12
13describe("CursorTransportFailure", () => {
14 it.each([
15 diagnostic,
16 "Error: ConnectError: [unavailable] transport closed",
17 "Something went wrong communicating with the server. Please try again.",
18 ])("recognizes a terminal diagnostic: %s", (message) => {
19 expect(failureFor([message])).toBe(message);
20 expect(failureFor([...message])).toBe(message);
21 });
22
23 it("recognizes a standalone failure with a stack trace", () => {
24 expect(failureFor(["\n", diagnostic, "\n at send (cli.js:1:2)\n\n"])).toBe(diagnostic);
25 });
26
27 it.each([
28 "The error was " + diagnostic,
29 "```text\n" + diagnostic + "\n```",
30 "Error: ConnectError: [unauthenticated] sign in",
31 "Error: RetriableError: [internal] Failed to run step, exceeded max retries",
32 ])("preserves prose, code and non-transport errors: %s", (message) => {
33 expect(failureFor([...message])).toBeUndefined();
34 });
35
36 it("tracks fences across long lines without retaining the answer", () => {
37 expect(failureFor(["```\n", "x".repeat(100_000), "\n", diagnostic])).toBeUndefined();
38 expect(failureFor(["```", "x".repeat(100_000), "\n", diagnostic])).toBeUndefined();
39 expect(failureFor(["x".repeat(100_000), "\n", diagnostic])).toBeUndefined();
40 });
41
42 it("does not retain a previous failure after new output", () => {
43 const reply = new CursorTransportFailure();
44 reply.push(diagnostic);
45 expect(reply.failure).toBe(diagnostic);
46 reply.push("\nRecovered successfully.");
47 expect(reply.failure).toBeUndefined();
48 });
49});