forked from Zoo-Code-Org/Zoo-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeActionProvider.spec.ts
More file actions
150 lines (124 loc) · 4.68 KB
/
Copy pathCodeActionProvider.spec.ts
File metadata and controls
150 lines (124 loc) · 4.68 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
import type { Mock } from "vitest"
import * as vscode from "vscode"
import { EditorUtils } from "../../integrations/editor/EditorUtils"
import { CodeActionProvider } from "../CodeActionProvider"
vi.mock("../../i18n", () => ({
t: vi.fn((key: string) => {
const translations: Record<string, string> = {
"common:codeActions.explain": "Explain with Zoo Code",
"common:codeActions.fix": "Fix with Zoo Code",
"common:codeActions.improve": "Improve with Zoo Code",
"common:codeActions.addToContext": "Add to Zoo Code",
}
return translations[key] || key
}),
}))
vi.mock("vscode", () => ({
CodeAction: vi.fn().mockImplementation((title, kind) => ({
title,
kind,
command: undefined,
})),
CodeActionKind: {
QuickFix: { value: "quickfix" },
RefactorRewrite: { value: "refactor.rewrite" },
},
Range: vi.fn().mockImplementation((startLine, startChar, endLine, endChar) => ({
start: { line: startLine, character: startChar },
end: { line: endLine, character: endChar },
})),
DiagnosticSeverity: {
Error: 0,
Warning: 1,
Information: 2,
Hint: 3,
},
workspace: {
getConfiguration: vi.fn().mockReturnValue({
get: vi.fn().mockReturnValue(true),
}),
},
}))
vi.mock("../../integrations/editor/EditorUtils", () => ({
EditorUtils: {
getEffectiveRange: vi.fn(),
getFilePath: vi.fn(),
hasIntersectingRange: vi.fn(),
createDiagnosticData: vi.fn(),
},
}))
describe("CodeActionProvider", () => {
let provider: CodeActionProvider
let mockDocument: any
let mockRange: any
let mockContext: any
beforeEach(() => {
provider = new CodeActionProvider()
mockDocument = {
getText: vi.fn(),
lineAt: vi.fn(),
lineCount: 10,
uri: { fsPath: "/test/file.ts" },
}
mockRange = new vscode.Range(0, 0, 0, 10)
mockContext = { diagnostics: [] }
;(EditorUtils.getEffectiveRange as Mock).mockReturnValue({
range: mockRange,
text: "test code",
})
;(EditorUtils.getFilePath as Mock).mockReturnValue("/test/file.ts")
;(EditorUtils.hasIntersectingRange as Mock).mockReturnValue(true)
;(EditorUtils.createDiagnosticData as Mock).mockImplementation((d) => d)
})
describe("provideCodeActions", () => {
it("should provide explain, improve, fix logic, and add to context actions by default", () => {
const actions = provider.provideCodeActions(mockDocument, mockRange, mockContext)
expect(actions).toHaveLength(3)
expect((actions as any)[0].title).toBe("Add to Zoo Code")
expect((actions as any)[1].title).toBe("Explain with Zoo Code")
expect((actions as any)[2].title).toBe("Improve with Zoo Code")
})
it("should provide fix action instead of fix logic when diagnostics exist", () => {
mockContext.diagnostics = [
{ message: "test error", severity: vscode.DiagnosticSeverity.Error, range: mockRange },
]
const actions = provider.provideCodeActions(mockDocument, mockRange, mockContext)
expect(actions).toHaveLength(2)
expect((actions as any).some((a: any) => a.title === "Fix with Zoo Code")).toBe(true)
expect((actions as any).some((a: any) => a.title === "Add to Zoo Code")).toBe(true)
})
it("should return empty array when no effective range", () => {
;(EditorUtils.getEffectiveRange as Mock).mockReturnValue(null)
const actions = provider.provideCodeActions(mockDocument, mockRange, mockContext)
expect(actions).toEqual([])
})
it("should return empty array when enableCodeActions is disabled", () => {
// Mock the configuration to return false for enableCodeActions
const mockGet = vi.fn().mockReturnValue(false)
const mockGetConfiguration = vi.fn().mockReturnValue({
get: mockGet,
})
;(vscode.workspace.getConfiguration as Mock).mockReturnValue(mockGetConfiguration())
const actions = provider.provideCodeActions(mockDocument, mockRange, mockContext)
expect(actions).toEqual([])
expect(vscode.workspace.getConfiguration).toHaveBeenCalledWith("zoo-code")
expect(mockGet).toHaveBeenCalledWith("enableCodeActions", true)
})
it("should handle errors gracefully", () => {
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {})
// Reset the workspace mock to return true for enableCodeActions
const mockGet = vi.fn().mockReturnValue(true)
const mockGetConfiguration = vi.fn().mockReturnValue({
get: mockGet,
})
;(vscode.workspace.getConfiguration as Mock).mockReturnValue(mockGetConfiguration())
;(EditorUtils.getEffectiveRange as Mock).mockImplementation(() => {
throw new Error("Test error")
})
const actions = provider.provideCodeActions(mockDocument, mockRange, mockContext)
expect(actions).toEqual([])
expect(consoleErrorSpy).toHaveBeenCalledWith("Error providing code actions:", expect.any(Error))
consoleErrorSpy.mockRestore()
})
})
})