Skip to content

Commit 98f8546

Browse files
committed
PR feedback
1 parent 7d7ca43 commit 98f8546

File tree

2 files changed

+76
-25
lines changed

2 files changed

+76
-25
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
jest.mock("vscode", () => ({
2+
CodeActionKind: {
3+
QuickFix: { value: "quickfix" },
4+
RefactorRewrite: { value: "refactor.rewrite" },
5+
},
6+
window: {
7+
createTextEditorDecorationType: jest.fn().mockReturnValue({ dispose: jest.fn() }),
8+
},
9+
}))
10+
11+
import * as vscode from "vscode"
12+
import { ClineProvider } from "../../core/webview/ClineProvider"
13+
14+
// Import the helper function from the actual file
15+
import { getVisibleProviderOrLog } from "../registerCommands"
16+
17+
jest.mock("../../core/webview/ClineProvider")
18+
19+
describe("getVisibleProviderOrLog", () => {
20+
let mockOutputChannel: vscode.OutputChannel
21+
22+
beforeEach(() => {
23+
mockOutputChannel = {
24+
appendLine: jest.fn(),
25+
append: jest.fn(),
26+
clear: jest.fn(),
27+
hide: jest.fn(),
28+
name: "mock",
29+
replace: jest.fn(),
30+
show: jest.fn(),
31+
dispose: jest.fn(),
32+
}
33+
jest.clearAllMocks()
34+
})
35+
36+
it("returns the visible provider if found", () => {
37+
const mockProvider = {} as ClineProvider
38+
;(ClineProvider.getVisibleInstance as jest.Mock).mockReturnValue(mockProvider)
39+
40+
const result = getVisibleProviderOrLog(mockOutputChannel)
41+
42+
expect(result).toBe(mockProvider)
43+
expect(mockOutputChannel.appendLine).not.toHaveBeenCalled()
44+
})
45+
46+
it("logs and returns undefined if no provider found", () => {
47+
;(ClineProvider.getVisibleInstance as jest.Mock).mockReturnValue(undefined)
48+
49+
const result = getVisibleProviderOrLog(mockOutputChannel)
50+
51+
expect(result).toBeUndefined()
52+
expect(mockOutputChannel.appendLine).toHaveBeenCalledWith("Cannot find any visible Cline instances.")
53+
})
54+
})

src/activate/registerCommands.ts

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import delay from "delay"
33

44
import { ClineProvider } from "../core/webview/ClineProvider"
55

6+
/**
7+
* Helper to get the visible ClineProvider instance or log if not found.
8+
*/
9+
export function getVisibleProviderOrLog(outputChannel: vscode.OutputChannel): ClineProvider | undefined {
10+
const visibleProvider = ClineProvider.getVisibleInstance()
11+
if (!visibleProvider) {
12+
outputChannel.appendLine("Cannot find any visible Cline instances.")
13+
return undefined
14+
}
15+
return visibleProvider
16+
}
17+
618
import { registerHumanRelayCallback, unregisterHumanRelayCallback, handleHumanRelayResponse } from "./humanRelay"
719
import { handleNewTask } from "./handleTask"
820

@@ -52,47 +64,32 @@ const getCommandsMap = ({ context, outputChannel, provider }: RegisterCommandOpt
5264
return {
5365
"roo-cline.activationCompleted": () => {},
5466
"roo-cline.plusButtonClicked": async () => {
55-
const visibleProvider = ClineProvider.getVisibleInstance()
56-
if (!visibleProvider) {
57-
outputChannel.appendLine("Cannot find any visible Cline instances.")
58-
return
59-
}
67+
const visibleProvider = getVisibleProviderOrLog(outputChannel)
68+
if (!visibleProvider) return
6069
await visibleProvider.removeClineFromStack()
6170
await visibleProvider.postStateToWebview()
6271
await visibleProvider.postMessageToWebview({ type: "action", action: "chatButtonClicked" })
6372
},
6473
"roo-cline.mcpButtonClicked": () => {
65-
const visibleProvider = ClineProvider.getVisibleInstance()
66-
if (!visibleProvider) {
67-
outputChannel.appendLine("Cannot find any visible Cline instances.")
68-
return
69-
}
74+
const visibleProvider = getVisibleProviderOrLog(outputChannel)
75+
if (!visibleProvider) return
7076
visibleProvider.postMessageToWebview({ type: "action", action: "mcpButtonClicked" })
7177
},
7278
"roo-cline.promptsButtonClicked": () => {
73-
const visibleProvider = ClineProvider.getVisibleInstance()
74-
if (!visibleProvider) {
75-
outputChannel.appendLine("Cannot find any visible Cline instances.")
76-
return
77-
}
79+
const visibleProvider = getVisibleProviderOrLog(outputChannel)
80+
if (!visibleProvider) return
7881
visibleProvider.postMessageToWebview({ type: "action", action: "promptsButtonClicked" })
7982
},
8083
"roo-cline.popoutButtonClicked": () => openClineInNewTab({ context, outputChannel }),
8184
"roo-cline.openInNewTab": () => openClineInNewTab({ context, outputChannel }),
8285
"roo-cline.settingsButtonClicked": () => {
83-
const visibleProvider = ClineProvider.getVisibleInstance()
84-
if (!visibleProvider) {
85-
outputChannel.appendLine("Cannot find any visible Cline instances.")
86-
return
87-
}
86+
const visibleProvider = getVisibleProviderOrLog(outputChannel)
87+
if (!visibleProvider) return
8888
visibleProvider.postMessageToWebview({ type: "action", action: "settingsButtonClicked" })
8989
},
9090
"roo-cline.historyButtonClicked": () => {
91-
const visibleProvider = ClineProvider.getVisibleInstance()
92-
if (!visibleProvider) {
93-
outputChannel.appendLine("Cannot find any visible Cline instances.")
94-
return
95-
}
91+
const visibleProvider = getVisibleProviderOrLog(outputChannel)
92+
if (!visibleProvider) return
9693
visibleProvider.postMessageToWebview({ type: "action", action: "historyButtonClicked" })
9794
},
9895
"roo-cline.helpButtonClicked": () => {

0 commit comments

Comments
 (0)