|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { describe, it, expect, beforeEach, vi, type MockedFunction } from "vitest" |
| 3 | +import axios from "axios" |
| 4 | +import * as vscode from "vscode" |
| 5 | + |
| 6 | +import { ShareService } from "../ShareService" |
| 7 | +import type { AuthService } from "../AuthService" |
| 8 | +import type { SettingsService } from "../SettingsService" |
| 9 | + |
| 10 | +// Mock axios |
| 11 | +vi.mock("axios") |
| 12 | +const mockedAxios = axios as any |
| 13 | + |
| 14 | +// Mock vscode |
| 15 | +vi.mock("vscode", () => ({ |
| 16 | + window: { |
| 17 | + showInformationMessage: vi.fn(), |
| 18 | + showErrorMessage: vi.fn(), |
| 19 | + }, |
| 20 | + env: { |
| 21 | + clipboard: { |
| 22 | + writeText: vi.fn(), |
| 23 | + }, |
| 24 | + openExternal: vi.fn(), |
| 25 | + }, |
| 26 | + Uri: { |
| 27 | + parse: vi.fn(), |
| 28 | + }, |
| 29 | + extensions: { |
| 30 | + getExtension: vi.fn(() => ({ |
| 31 | + packageJSON: { version: "1.0.0" }, |
| 32 | + })), |
| 33 | + }, |
| 34 | +})) |
| 35 | + |
| 36 | +// Mock config |
| 37 | +vi.mock("../Config", () => ({ |
| 38 | + getRooCodeApiUrl: () => "https://app.roocode.com", |
| 39 | +})) |
| 40 | + |
| 41 | +// Mock utils |
| 42 | +vi.mock("../utils", () => ({ |
| 43 | + getUserAgent: () => "Roo-Code 1.0.0", |
| 44 | +})) |
| 45 | + |
| 46 | +describe("ShareService", () => { |
| 47 | + let shareService: ShareService |
| 48 | + let mockAuthService: AuthService |
| 49 | + let mockSettingsService: SettingsService |
| 50 | + let mockLog: MockedFunction<(...args: unknown[]) => void> |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + vi.clearAllMocks() |
| 54 | + |
| 55 | + mockLog = vi.fn() |
| 56 | + mockAuthService = { |
| 57 | + hasActiveSession: vi.fn(), |
| 58 | + getSessionToken: vi.fn(), |
| 59 | + isAuthenticated: vi.fn(), |
| 60 | + } as any |
| 61 | + |
| 62 | + mockSettingsService = { |
| 63 | + getSettings: vi.fn(), |
| 64 | + } as any |
| 65 | + |
| 66 | + shareService = new ShareService(mockAuthService, mockSettingsService, mockLog) |
| 67 | + }) |
| 68 | + |
| 69 | + describe("shareTask", () => { |
| 70 | + it("should share task and copy to clipboard", async () => { |
| 71 | + const mockResponse = { |
| 72 | + data: { |
| 73 | + success: true, |
| 74 | + shareUrl: "https://app.roocode.com/share/abc123", |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + ;(mockAuthService.hasActiveSession as any).mockReturnValue(true) |
| 79 | + ;(mockAuthService.getSessionToken as any).mockReturnValue("session-token") |
| 80 | + mockedAxios.post.mockResolvedValue(mockResponse) |
| 81 | + |
| 82 | + const result = await shareService.shareTask("task-123") |
| 83 | + |
| 84 | + expect(result).toBe(true) |
| 85 | + expect(mockedAxios.post).toHaveBeenCalledWith( |
| 86 | + "https://app.roocode.com/api/extension/share", |
| 87 | + { taskId: "task-123" }, |
| 88 | + { |
| 89 | + headers: { |
| 90 | + "Content-Type": "application/json", |
| 91 | + Authorization: "Bearer session-token", |
| 92 | + "User-Agent": "Roo-Code 1.0.0", |
| 93 | + }, |
| 94 | + }, |
| 95 | + ) |
| 96 | + expect(vscode.env.clipboard.writeText).toHaveBeenCalledWith("https://app.roocode.com/share/abc123") |
| 97 | + }) |
| 98 | + |
| 99 | + it("should handle API error response", async () => { |
| 100 | + const mockResponse = { |
| 101 | + data: { |
| 102 | + success: false, |
| 103 | + error: "Task not found", |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + ;(mockAuthService.hasActiveSession as any).mockReturnValue(true) |
| 108 | + ;(mockAuthService.getSessionToken as any).mockReturnValue("session-token") |
| 109 | + mockedAxios.post.mockResolvedValue(mockResponse) |
| 110 | + |
| 111 | + const result = await shareService.shareTask("task-123") |
| 112 | + |
| 113 | + expect(result).toBe(false) |
| 114 | + }) |
| 115 | + |
| 116 | + it("should handle authentication errors", async () => { |
| 117 | + ;(mockAuthService.hasActiveSession as any).mockReturnValue(false) |
| 118 | + |
| 119 | + const result = await shareService.shareTask("task-123") |
| 120 | + |
| 121 | + expect(result).toBe(false) |
| 122 | + expect(mockedAxios.post).not.toHaveBeenCalled() |
| 123 | + }) |
| 124 | + |
| 125 | + it("should handle 403 error for disabled sharing", async () => { |
| 126 | + ;(mockAuthService.hasActiveSession as any).mockReturnValue(true) |
| 127 | + ;(mockAuthService.getSessionToken as any).mockReturnValue("session-token") |
| 128 | + |
| 129 | + const error = { |
| 130 | + isAxiosError: true, |
| 131 | + response: { |
| 132 | + status: 403, |
| 133 | + data: { |
| 134 | + error: "Task sharing is not enabled for this organization", |
| 135 | + }, |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + mockedAxios.isAxiosError.mockReturnValue(true) |
| 140 | + mockedAxios.post.mockRejectedValue(error) |
| 141 | + |
| 142 | + const result = await shareService.shareTask("task-123") |
| 143 | + |
| 144 | + expect(result).toBe(false) |
| 145 | + }) |
| 146 | + |
| 147 | + it("should handle unexpected errors", async () => { |
| 148 | + ;(mockAuthService.hasActiveSession as any).mockReturnValue(true) |
| 149 | + ;(mockAuthService.getSessionToken as any).mockReturnValue("session-token") |
| 150 | + |
| 151 | + mockedAxios.post.mockRejectedValue(new Error("Network error")) |
| 152 | + |
| 153 | + const result = await shareService.shareTask("task-123") |
| 154 | + |
| 155 | + expect(result).toBe(false) |
| 156 | + }) |
| 157 | + }) |
| 158 | + |
| 159 | + describe("canShareTask", () => { |
| 160 | + it("should return true when authenticated and sharing is enabled", async () => { |
| 161 | + ;(mockAuthService.isAuthenticated as any).mockReturnValue(true) |
| 162 | + ;(mockSettingsService.getSettings as any).mockReturnValue({ |
| 163 | + cloudSettings: { |
| 164 | + enableTaskSharing: true, |
| 165 | + }, |
| 166 | + }) |
| 167 | + |
| 168 | + const result = await shareService.canShareTask() |
| 169 | + |
| 170 | + expect(result).toBe(true) |
| 171 | + }) |
| 172 | + |
| 173 | + it("should return false when authenticated but sharing is disabled", async () => { |
| 174 | + ;(mockAuthService.isAuthenticated as any).mockReturnValue(true) |
| 175 | + ;(mockSettingsService.getSettings as any).mockReturnValue({ |
| 176 | + cloudSettings: { |
| 177 | + enableTaskSharing: false, |
| 178 | + }, |
| 179 | + }) |
| 180 | + |
| 181 | + const result = await shareService.canShareTask() |
| 182 | + |
| 183 | + expect(result).toBe(false) |
| 184 | + }) |
| 185 | + |
| 186 | + it("should return false when authenticated and sharing setting is undefined (default)", async () => { |
| 187 | + ;(mockAuthService.isAuthenticated as any).mockReturnValue(true) |
| 188 | + ;(mockSettingsService.getSettings as any).mockReturnValue({ |
| 189 | + cloudSettings: {}, |
| 190 | + }) |
| 191 | + |
| 192 | + const result = await shareService.canShareTask() |
| 193 | + |
| 194 | + expect(result).toBe(false) |
| 195 | + }) |
| 196 | + |
| 197 | + it("should return false when authenticated and no settings available (default)", async () => { |
| 198 | + ;(mockAuthService.isAuthenticated as any).mockReturnValue(true) |
| 199 | + ;(mockSettingsService.getSettings as any).mockReturnValue(undefined) |
| 200 | + |
| 201 | + const result = await shareService.canShareTask() |
| 202 | + |
| 203 | + expect(result).toBe(false) |
| 204 | + }) |
| 205 | + |
| 206 | + it("should return false when not authenticated", async () => { |
| 207 | + ;(mockAuthService.isAuthenticated as any).mockReturnValue(false) |
| 208 | + |
| 209 | + const result = await shareService.canShareTask() |
| 210 | + |
| 211 | + expect(result).toBe(false) |
| 212 | + }) |
| 213 | + |
| 214 | + it("should handle errors gracefully", async () => { |
| 215 | + ;(mockAuthService.isAuthenticated as any).mockImplementation(() => { |
| 216 | + throw new Error("Auth error") |
| 217 | + }) |
| 218 | + |
| 219 | + const result = await shareService.canShareTask() |
| 220 | + |
| 221 | + expect(result).toBe(false) |
| 222 | + }) |
| 223 | + }) |
| 224 | +}) |
0 commit comments