|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 2 | +import * as vscode from "vscode" |
| 3 | +import { focusPanel } from "../focusPanel" |
| 4 | +import { ClineProvider } from "../../core/webview/ClineProvider" |
| 5 | + |
| 6 | +// Mock vscode module |
| 7 | +vi.mock("vscode", () => ({ |
| 8 | + commands: { |
| 9 | + executeCommand: vi.fn(), |
| 10 | + }, |
| 11 | + window: { |
| 12 | + activeTextEditor: undefined, |
| 13 | + visibleTextEditors: [], |
| 14 | + }, |
| 15 | + ViewColumn: { |
| 16 | + Active: 1, |
| 17 | + }, |
| 18 | +})) |
| 19 | + |
| 20 | +// Mock ClineProvider |
| 21 | +vi.mock("../../core/webview/ClineProvider", () => ({ |
| 22 | + ClineProvider: { |
| 23 | + sideBarId: "roo-code.SidebarProvider", |
| 24 | + getVisibleInstance: vi.fn(), |
| 25 | + }, |
| 26 | +})) |
| 27 | + |
| 28 | +// Mock Package |
| 29 | +vi.mock("../../shared/package", () => ({ |
| 30 | + Package: { |
| 31 | + name: "roo-code", |
| 32 | + }, |
| 33 | +})) |
| 34 | + |
| 35 | +describe("focusPanel", () => { |
| 36 | + const mockExecuteCommand = vi.mocked(vscode.commands.executeCommand) |
| 37 | + const mockGetVisibleInstance = vi.mocked(ClineProvider.getVisibleInstance) |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + vi.clearAllMocks() |
| 41 | + // Reset window state |
| 42 | + ;(vscode.window as any).activeTextEditor = undefined |
| 43 | + ;(vscode.window as any).visibleTextEditors = [] |
| 44 | + }) |
| 45 | + |
| 46 | + describe("when panels exist", () => { |
| 47 | + it("should reveal tab panel when it exists but is not active", async () => { |
| 48 | + const mockTabPanel = { |
| 49 | + active: false, |
| 50 | + reveal: vi.fn(), |
| 51 | + } as any |
| 52 | + |
| 53 | + await focusPanel(mockTabPanel, undefined) |
| 54 | + |
| 55 | + expect(mockTabPanel.reveal).toHaveBeenCalledWith(1, false) |
| 56 | + expect(mockExecuteCommand).not.toHaveBeenCalled() |
| 57 | + }) |
| 58 | + |
| 59 | + it("should focus sidebar panel when it exists", async () => { |
| 60 | + const mockSidebarPanel = {} as any |
| 61 | + |
| 62 | + await focusPanel(undefined, mockSidebarPanel) |
| 63 | + |
| 64 | + expect(mockExecuteCommand).toHaveBeenCalledWith("roo-code.SidebarProvider.focus") |
| 65 | + }) |
| 66 | + |
| 67 | + it("should prefer tab panel over sidebar panel when both exist", async () => { |
| 68 | + const mockTabPanel = { |
| 69 | + active: false, |
| 70 | + reveal: vi.fn(), |
| 71 | + } as any |
| 72 | + const mockSidebarPanel = {} as any |
| 73 | + |
| 74 | + await focusPanel(mockTabPanel, mockSidebarPanel) |
| 75 | + |
| 76 | + expect(mockTabPanel.reveal).toHaveBeenCalledWith(1, false) |
| 77 | + expect(mockExecuteCommand).not.toHaveBeenCalled() |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + describe("when no panels exist", () => { |
| 82 | + it("should open sidebar when there is a visible Roo Code instance", async () => { |
| 83 | + mockGetVisibleInstance.mockReturnValue({} as any) |
| 84 | + |
| 85 | + await focusPanel(undefined, undefined) |
| 86 | + |
| 87 | + expect(mockExecuteCommand).toHaveBeenCalledWith("workbench.view.extension.roo-code-ActivityBar") |
| 88 | + }) |
| 89 | + |
| 90 | + it("should open sidebar when there is an active editor (user is working in this window)", async () => { |
| 91 | + mockGetVisibleInstance.mockReturnValue(undefined) |
| 92 | + ;(vscode.window as any).activeTextEditor = { document: { fileName: "test.ts" } } |
| 93 | + |
| 94 | + await focusPanel(undefined, undefined) |
| 95 | + |
| 96 | + expect(mockExecuteCommand).toHaveBeenCalledWith("workbench.view.extension.roo-code-ActivityBar") |
| 97 | + }) |
| 98 | + |
| 99 | + it("should open sidebar when there are visible editors (user is working in this window)", async () => { |
| 100 | + mockGetVisibleInstance.mockReturnValue(undefined) |
| 101 | + ;(vscode.window as any).visibleTextEditors = [{ document: { fileName: "test.ts" } }] |
| 102 | + |
| 103 | + await focusPanel(undefined, undefined) |
| 104 | + |
| 105 | + expect(mockExecuteCommand).toHaveBeenCalledWith("workbench.view.extension.roo-code-ActivityBar") |
| 106 | + }) |
| 107 | + |
| 108 | + it("should NOT open sidebar when no visible instance and no editors (multi-window scenario)", async () => { |
| 109 | + mockGetVisibleInstance.mockReturnValue(undefined) |
| 110 | + // No active editor and no visible editors (default state) |
| 111 | + |
| 112 | + await focusPanel(undefined, undefined) |
| 113 | + |
| 114 | + expect(mockExecuteCommand).not.toHaveBeenCalled() |
| 115 | + }) |
| 116 | + |
| 117 | + it("should open sidebar when detection fails (fallback to existing behavior)", async () => { |
| 118 | + mockGetVisibleInstance.mockImplementation(() => { |
| 119 | + throw new Error("Test error") |
| 120 | + }) |
| 121 | + |
| 122 | + await focusPanel(undefined, undefined) |
| 123 | + |
| 124 | + expect(mockExecuteCommand).toHaveBeenCalledWith("workbench.view.extension.roo-code-ActivityBar") |
| 125 | + }) |
| 126 | + }) |
| 127 | +}) |
0 commit comments