-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathdiagnosticsHandler.spec.ts
More file actions
188 lines (156 loc) · 5.88 KB
/
Copy pathdiagnosticsHandler.spec.ts
File metadata and controls
188 lines (156 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// npx vitest src/core/webview/__tests__/diagnosticsHandler.spec.ts
import * as path from "path"
// Mock vscode first
vi.mock("vscode", () => {
const showErrorMessage = vi.fn()
const openTextDocument = vi.fn().mockResolvedValue({})
const showTextDocument = vi.fn().mockResolvedValue(undefined)
return {
window: {
showErrorMessage,
showTextDocument,
},
workspace: {
openTextDocument,
},
}
})
// Mock storage utilities
vi.mock("../../../utils/storage", () => ({
getTaskDirectoryPath: vi.fn(async () => "/mock/task-dir"),
}))
// Mock fs utilities
vi.mock("../../../utils/fs", () => ({
fileExistsAtPath: vi.fn(),
}))
// Mock fs/promises
vi.mock("fs/promises", () => {
const mockReadFile = vi.fn()
const mockWriteFile = vi.fn().mockResolvedValue(undefined)
return {
default: {
readFile: mockReadFile,
writeFile: mockWriteFile,
},
readFile: mockReadFile,
writeFile: mockWriteFile,
}
})
import * as vscode from "vscode"
import * as fs from "fs/promises"
import * as fsUtils from "../../../utils/fs"
import { generateErrorDiagnostics } from "../diagnosticsHandler"
describe("generateErrorDiagnostics", () => {
const mockLog = vi.fn()
beforeEach(() => {
vi.clearAllMocks()
})
it("generates a diagnostics file with error metadata and history", async () => {
vi.mocked(fsUtils.fileExistsAtPath).mockResolvedValue(true as any)
vi.mocked(fs.readFile).mockResolvedValue('[{"role": "user", "content": "test"}]' as any)
const result = await generateErrorDiagnostics({
taskId: "test-task-id",
globalStoragePath: "/mock/global/storage",
values: {
timestamp: "2025-01-01T00:00:00.000Z",
version: "1.2.3",
provider: "test-provider",
model: "test-model",
details: "Sample error details",
},
log: mockLog,
})
expect(result.success).toBe(true)
expect(result.filePath).toContain("roo-diagnostics-")
// Verify we attempted to read API history
expect(fs.readFile).toHaveBeenCalledWith(path.join("/mock/task-dir", "api_conversation_history.json"), "utf8")
// Verify we wrote a diagnostics file with the expected content
expect(fs.writeFile).toHaveBeenCalledTimes(1)
const [writtenPath, writtenContent] = vi.mocked(fs.writeFile).mock.calls[0]
// taskId.slice(0, 8) = "test-tas" from "test-task-id"
expect(String(writtenPath)).toContain("roo-diagnostics-test-tas")
expect(String(writtenContent)).toContain(
"// Please share this file with Zoo Code Support (support@zoocode.dev) to diagnose the issue faster",
)
expect(String(writtenContent)).toContain('"error":')
expect(String(writtenContent)).toContain('"history":')
expect(String(writtenContent)).toContain('"version": "1.2.3"')
expect(String(writtenContent)).toContain('"provider": "test-provider"')
expect(String(writtenContent)).toContain('"model": "test-model"')
expect(String(writtenContent)).toContain('"details": "Sample error details"')
// Verify VS Code APIs were used to open the generated file
expect(vscode.workspace.openTextDocument).toHaveBeenCalledTimes(1)
expect(vscode.window.showTextDocument).toHaveBeenCalledTimes(1)
})
it("uses empty history when API history file does not exist", async () => {
vi.mocked(fsUtils.fileExistsAtPath).mockResolvedValue(false as any)
const result = await generateErrorDiagnostics({
taskId: "test-task-id",
globalStoragePath: "/mock/global/storage",
values: {
timestamp: "2025-01-01T00:00:00.000Z",
version: "1.0.0",
provider: "test",
model: "test",
details: "error",
},
log: mockLog,
})
expect(result.success).toBe(true)
// Should not attempt to read file when it doesn't exist
expect(fs.readFile).not.toHaveBeenCalled()
// Verify empty history in output
const [, writtenContent] = vi.mocked(fs.writeFile).mock.calls[0]
expect(String(writtenContent)).toContain('"history": []')
})
it("uses default values when values are not provided", async () => {
vi.mocked(fsUtils.fileExistsAtPath).mockResolvedValue(false as any)
const result = await generateErrorDiagnostics({
taskId: "test-task-id",
globalStoragePath: "/mock/global/storage",
log: mockLog,
})
expect(result.success).toBe(true)
// Verify defaults in output
const [, writtenContent] = vi.mocked(fs.writeFile).mock.calls[0]
expect(String(writtenContent)).toContain('"version": ""')
expect(String(writtenContent)).toContain('"provider": ""')
expect(String(writtenContent)).toContain('"model": ""')
expect(String(writtenContent)).toContain('"details": ""')
})
it("handles JSON parse error gracefully", async () => {
vi.mocked(fsUtils.fileExistsAtPath).mockResolvedValue(true as any)
vi.mocked(fs.readFile).mockResolvedValue("invalid json" as any)
const result = await generateErrorDiagnostics({
taskId: "test-task-id",
globalStoragePath: "/mock/global/storage",
values: {
timestamp: "2025-01-01T00:00:00.000Z",
version: "1.0.0",
provider: "test",
model: "test",
details: "error",
},
log: mockLog,
})
// Should still succeed but with empty history
expect(result.success).toBe(true)
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith("Failed to parse api_conversation_history.json")
// Verify empty history in output
const [, writtenContent] = vi.mocked(fs.writeFile).mock.calls[0]
expect(String(writtenContent)).toContain('"history": []')
})
it("returns error result when file write fails", async () => {
vi.mocked(fsUtils.fileExistsAtPath).mockResolvedValue(false as any)
vi.mocked(fs.writeFile).mockRejectedValue(new Error("Write failed"))
const result = await generateErrorDiagnostics({
taskId: "test-task-id",
globalStoragePath: "/mock/global/storage",
log: mockLog,
})
expect(result.success).toBe(false)
expect(result.error).toBe("Write failed")
expect(mockLog).toHaveBeenCalledWith("Error generating diagnostics: Write failed")
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith("Failed to generate diagnostics: Write failed")
})
})