|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { truncateStack, truncateMessage, parseError, sanitizeError } from "../src/v3/errors.js"; |
| 3 | + |
| 4 | +// Helper: build a fake stack with N frames |
| 5 | +function buildStack(messageLines: string[], frameCount: number): string { |
| 6 | + const frames = Array.from( |
| 7 | + { length: frameCount }, |
| 8 | + (_, i) => ` at functionName${i} (/path/to/file${i}.ts:${i + 1}:${i + 10})` |
| 9 | + ); |
| 10 | + return [...messageLines, ...frames].join("\n"); |
| 11 | +} |
| 12 | + |
| 13 | +describe("truncateStack", () => { |
| 14 | + it("returns empty string for undefined", () => { |
| 15 | + expect(truncateStack(undefined)).toBe(""); |
| 16 | + }); |
| 17 | + |
| 18 | + it("returns empty string for empty string", () => { |
| 19 | + expect(truncateStack("")).toBe(""); |
| 20 | + }); |
| 21 | + |
| 22 | + it("preserves a short stack unchanged", () => { |
| 23 | + const stack = buildStack(["Error: something broke"], 10); |
| 24 | + expect(truncateStack(stack)).toBe(stack); |
| 25 | + }); |
| 26 | + |
| 27 | + it("preserves exactly 50 frames", () => { |
| 28 | + const stack = buildStack(["Error: at the limit"], 50); |
| 29 | + const result = truncateStack(stack); |
| 30 | + expect(result).toBe(stack); |
| 31 | + expect(result.split("\n").filter((l) => l.trimStart().startsWith("at ")).length).toBe(50); |
| 32 | + }); |
| 33 | + |
| 34 | + it("truncates to 50 frames when exceeding the limit", () => { |
| 35 | + const stack = buildStack(["Error: too many frames"], 200); |
| 36 | + const result = truncateStack(stack); |
| 37 | + const lines = result.split("\n"); |
| 38 | + |
| 39 | + // Message line + 5 top + 1 omitted notice + 45 bottom = 52 lines |
| 40 | + expect(lines[0]).toBe("Error: too many frames"); |
| 41 | + expect(lines).toContain(" ... 150 frames omitted ..."); |
| 42 | + |
| 43 | + const frameLines = lines.filter((l) => l.trimStart().startsWith("at ")); |
| 44 | + expect(frameLines.length).toBe(50); |
| 45 | + |
| 46 | + // First kept frame is frame 0 (top of stack) |
| 47 | + expect(frameLines[0]).toContain("functionName0"); |
| 48 | + // Last kept frame is the last original frame |
| 49 | + expect(frameLines[frameLines.length - 1]).toContain("functionName199"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("preserves multi-line error messages before frames", () => { |
| 53 | + const stack = buildStack(["TypeError: cannot read property", " caused by: something"], 60); |
| 54 | + const result = truncateStack(stack); |
| 55 | + const lines = result.split("\n"); |
| 56 | + |
| 57 | + expect(lines[0]).toBe("TypeError: cannot read property"); |
| 58 | + expect(lines[1]).toBe(" caused by: something"); |
| 59 | + expect(lines).toContain(" ... 10 frames omitted ..."); |
| 60 | + }); |
| 61 | + |
| 62 | + it("truncates individual lines longer than 1024 chars", () => { |
| 63 | + const longFrame = ` at someFn (${"x".repeat(2000)}:1:1)`; |
| 64 | + const stack = ["Error: long line", longFrame].join("\n"); |
| 65 | + const result = truncateStack(stack); |
| 66 | + const frameLine = result.split("\n")[1]!; |
| 67 | + |
| 68 | + expect(frameLine.length).toBeLessThan(1100); |
| 69 | + expect(frameLine).toContain("...[truncated]"); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe("truncateMessage", () => { |
| 74 | + it("returns empty string for undefined", () => { |
| 75 | + expect(truncateMessage(undefined)).toBe(""); |
| 76 | + }); |
| 77 | + |
| 78 | + it("returns empty string for empty string", () => { |
| 79 | + expect(truncateMessage("")).toBe(""); |
| 80 | + }); |
| 81 | + |
| 82 | + it("preserves a short message", () => { |
| 83 | + expect(truncateMessage("hello")).toBe("hello"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("truncates messages over 1000 chars", () => { |
| 87 | + const long = "x".repeat(5000); |
| 88 | + const result = truncateMessage(long); |
| 89 | + expect(result.length).toBeLessThan(1100); |
| 90 | + expect(result).toContain("...[truncated]"); |
| 91 | + }); |
| 92 | + |
| 93 | + it("preserves a message at exactly 1000 chars", () => { |
| 94 | + const exact = "x".repeat(1000); |
| 95 | + expect(truncateMessage(exact)).toBe(exact); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +describe("parseError truncation", () => { |
| 100 | + it("truncates large stack traces in Error objects", () => { |
| 101 | + const error = new Error("boom"); |
| 102 | + error.stack = buildStack(["Error: boom"], 200); |
| 103 | + const parsed = parseError(error); |
| 104 | + |
| 105 | + expect(parsed.type).toBe("BUILT_IN_ERROR"); |
| 106 | + if (parsed.type === "BUILT_IN_ERROR") { |
| 107 | + const frameLines = parsed.stackTrace.split("\n").filter((l) => l.trimStart().startsWith("at ")); |
| 108 | + expect(frameLines.length).toBe(50); |
| 109 | + expect(parsed.stackTrace).toContain("frames omitted"); |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + it("truncates large error messages", () => { |
| 114 | + const error = new Error("x".repeat(5000)); |
| 115 | + const parsed = parseError(error); |
| 116 | + |
| 117 | + if (parsed.type === "BUILT_IN_ERROR") { |
| 118 | + expect(parsed.message.length).toBeLessThan(1100); |
| 119 | + expect(parsed.message).toContain("...[truncated]"); |
| 120 | + } |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +describe("sanitizeError truncation", () => { |
| 125 | + it("truncates stack traces during sanitization", () => { |
| 126 | + const result = sanitizeError({ |
| 127 | + type: "BUILT_IN_ERROR", |
| 128 | + name: "Error", |
| 129 | + message: "boom", |
| 130 | + stackTrace: buildStack(["Error: boom"], 200), |
| 131 | + }); |
| 132 | + |
| 133 | + if (result.type === "BUILT_IN_ERROR") { |
| 134 | + const frameLines = result.stackTrace.split("\n").filter((l) => l.trimStart().startsWith("at ")); |
| 135 | + expect(frameLines.length).toBe(50); |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + it("strips null bytes and truncates", () => { |
| 140 | + const result = sanitizeError({ |
| 141 | + type: "BUILT_IN_ERROR", |
| 142 | + name: "Error\0", |
| 143 | + message: "hello\0world", |
| 144 | + stackTrace: "Error: hello\0world\n at fn (/path.ts:1:1)", |
| 145 | + }); |
| 146 | + |
| 147 | + if (result.type === "BUILT_IN_ERROR") { |
| 148 | + expect(result.name).toBe("Error"); |
| 149 | + expect(result.message).toBe("helloworld"); |
| 150 | + expect(result.stackTrace).not.toContain("\0"); |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + it("truncates STRING_ERROR raw field", () => { |
| 155 | + const result = sanitizeError({ |
| 156 | + type: "STRING_ERROR", |
| 157 | + raw: "x".repeat(5000), |
| 158 | + }); |
| 159 | + |
| 160 | + if (result.type === "STRING_ERROR") { |
| 161 | + expect(result.raw.length).toBeLessThan(1100); |
| 162 | + expect(result.raw).toContain("...[truncated]"); |
| 163 | + } |
| 164 | + }); |
| 165 | +}); |
0 commit comments