|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" |
| 2 | +import * as vscode from "vscode" |
| 3 | +import { |
| 4 | + isRemoteEnvironment, |
| 5 | + getRemoteType, |
| 6 | + getRemoteWorkspaceId, |
| 7 | + getEnvironmentStoragePath, |
| 8 | +} from "../remoteEnvironment" |
| 9 | + |
| 10 | +// Mock vscode module |
| 11 | +vi.mock("vscode", () => ({ |
| 12 | + env: { |
| 13 | + remoteName: undefined, |
| 14 | + }, |
| 15 | + workspace: { |
| 16 | + workspaceFolders: undefined, |
| 17 | + }, |
| 18 | +})) |
| 19 | + |
| 20 | +describe("remoteEnvironment", () => { |
| 21 | + let mockVscode: any |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + mockVscode = vi.mocked(vscode) |
| 25 | + }) |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + // Reset mocks after each test |
| 29 | + mockVscode.env.remoteName = undefined |
| 30 | + mockVscode.workspace.workspaceFolders = undefined |
| 31 | + }) |
| 32 | + |
| 33 | + describe("isRemoteEnvironment", () => { |
| 34 | + it("should return false when not in remote environment", () => { |
| 35 | + mockVscode.env.remoteName = undefined |
| 36 | + expect(isRemoteEnvironment()).toBe(false) |
| 37 | + }) |
| 38 | + |
| 39 | + it("should return true when in SSH remote environment", () => { |
| 40 | + mockVscode.env.remoteName = "ssh-remote" |
| 41 | + expect(isRemoteEnvironment()).toBe(true) |
| 42 | + }) |
| 43 | + |
| 44 | + it("should return true when in WSL environment", () => { |
| 45 | + mockVscode.env.remoteName = "wsl" |
| 46 | + expect(isRemoteEnvironment()).toBe(true) |
| 47 | + }) |
| 48 | + |
| 49 | + it("should return true when in dev container environment", () => { |
| 50 | + mockVscode.env.remoteName = "dev-container" |
| 51 | + expect(isRemoteEnvironment()).toBe(true) |
| 52 | + }) |
| 53 | + |
| 54 | + it("should return true when in codespaces environment", () => { |
| 55 | + mockVscode.env.remoteName = "codespaces" |
| 56 | + expect(isRemoteEnvironment()).toBe(true) |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + describe("getRemoteType", () => { |
| 61 | + it("should return undefined when not in remote environment", () => { |
| 62 | + mockVscode.env.remoteName = undefined |
| 63 | + expect(getRemoteType()).toBeUndefined() |
| 64 | + }) |
| 65 | + |
| 66 | + it("should return 'ssh-remote' for SSH remote", () => { |
| 67 | + mockVscode.env.remoteName = "ssh-remote" |
| 68 | + expect(getRemoteType()).toBe("ssh-remote") |
| 69 | + }) |
| 70 | + |
| 71 | + it("should return 'wsl' for WSL", () => { |
| 72 | + mockVscode.env.remoteName = "wsl" |
| 73 | + expect(getRemoteType()).toBe("wsl") |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + describe("getRemoteWorkspaceId", () => { |
| 78 | + it("should return undefined when not in remote environment", () => { |
| 79 | + mockVscode.env.remoteName = undefined |
| 80 | + expect(getRemoteWorkspaceId()).toBeUndefined() |
| 81 | + }) |
| 82 | + |
| 83 | + it("should return remote name when no workspace folders", () => { |
| 84 | + mockVscode.env.remoteName = "ssh-remote" |
| 85 | + mockVscode.workspace.workspaceFolders = undefined |
| 86 | + expect(getRemoteWorkspaceId()).toBe("ssh-remote") |
| 87 | + }) |
| 88 | + |
| 89 | + it("should return remote name when workspace folders is empty", () => { |
| 90 | + mockVscode.env.remoteName = "ssh-remote" |
| 91 | + mockVscode.workspace.workspaceFolders = [] |
| 92 | + expect(getRemoteWorkspaceId()).toBe("ssh-remote") |
| 93 | + }) |
| 94 | + |
| 95 | + it("should include workspace folder name and hash in ID", () => { |
| 96 | + mockVscode.env.remoteName = "ssh-remote" |
| 97 | + mockVscode.workspace.workspaceFolders = [ |
| 98 | + { |
| 99 | + name: "my-project", |
| 100 | + uri: { |
| 101 | + toString: () => "file:///home/user/projects/my-project", |
| 102 | + }, |
| 103 | + }, |
| 104 | + ] |
| 105 | + const id = getRemoteWorkspaceId() |
| 106 | + expect(id).toMatch(/^ssh-remote-my-project-[a-z0-9]+$/) |
| 107 | + }) |
| 108 | + |
| 109 | + it("should create consistent hash for same workspace", () => { |
| 110 | + mockVscode.env.remoteName = "ssh-remote" |
| 111 | + const workspaceUri = "file:///home/user/projects/my-project" |
| 112 | + mockVscode.workspace.workspaceFolders = [ |
| 113 | + { |
| 114 | + name: "my-project", |
| 115 | + uri: { |
| 116 | + toString: () => workspaceUri, |
| 117 | + }, |
| 118 | + }, |
| 119 | + ] |
| 120 | + const id1 = getRemoteWorkspaceId() |
| 121 | + const id2 = getRemoteWorkspaceId() |
| 122 | + expect(id1).toBe(id2) |
| 123 | + }) |
| 124 | + |
| 125 | + it("should create different hashes for different workspaces", () => { |
| 126 | + mockVscode.env.remoteName = "ssh-remote" |
| 127 | + |
| 128 | + // First workspace |
| 129 | + mockVscode.workspace.workspaceFolders = [ |
| 130 | + { |
| 131 | + name: "project1", |
| 132 | + uri: { |
| 133 | + toString: () => "file:///home/user/projects/project1", |
| 134 | + }, |
| 135 | + }, |
| 136 | + ] |
| 137 | + const id1 = getRemoteWorkspaceId() |
| 138 | + |
| 139 | + // Second workspace |
| 140 | + mockVscode.workspace.workspaceFolders = [ |
| 141 | + { |
| 142 | + name: "project2", |
| 143 | + uri: { |
| 144 | + toString: () => "file:///home/user/projects/project2", |
| 145 | + }, |
| 146 | + }, |
| 147 | + ] |
| 148 | + const id2 = getRemoteWorkspaceId() |
| 149 | + |
| 150 | + expect(id1).not.toBe(id2) |
| 151 | + }) |
| 152 | + }) |
| 153 | + |
| 154 | + describe("getEnvironmentStoragePath", () => { |
| 155 | + const basePath = "/home/user/.vscode/extensions/storage" |
| 156 | + |
| 157 | + it("should return base path unchanged when not in remote environment", () => { |
| 158 | + mockVscode.env.remoteName = undefined |
| 159 | + expect(getEnvironmentStoragePath(basePath)).toBe(basePath) |
| 160 | + }) |
| 161 | + |
| 162 | + it("should add remote subdirectory for SSH remote", () => { |
| 163 | + mockVscode.env.remoteName = "ssh-remote" |
| 164 | + mockVscode.workspace.workspaceFolders = [ |
| 165 | + { |
| 166 | + name: "my-project", |
| 167 | + uri: { |
| 168 | + toString: () => "file:///home/user/projects/my-project", |
| 169 | + }, |
| 170 | + }, |
| 171 | + ] |
| 172 | + const result = getEnvironmentStoragePath(basePath) |
| 173 | + expect(result).toMatch( |
| 174 | + /^\/home\/user\/.vscode\/extensions\/storage\/remote\/ssh-remote-my-project-[a-z0-9]+$/, |
| 175 | + ) |
| 176 | + }) |
| 177 | + |
| 178 | + it("should add remote subdirectory for WSL", () => { |
| 179 | + mockVscode.env.remoteName = "wsl" |
| 180 | + mockVscode.workspace.workspaceFolders = [ |
| 181 | + { |
| 182 | + name: "linux-project", |
| 183 | + uri: { |
| 184 | + toString: () => "file:///home/user/linux-project", |
| 185 | + }, |
| 186 | + }, |
| 187 | + ] |
| 188 | + const result = getEnvironmentStoragePath(basePath) |
| 189 | + expect(result).toMatch(/^\/home\/user\/.vscode\/extensions\/storage\/remote\/wsl-linux-project-[a-z0-9]+$/) |
| 190 | + }) |
| 191 | + |
| 192 | + it("should add remote subdirectory for dev containers", () => { |
| 193 | + mockVscode.env.remoteName = "dev-container" |
| 194 | + mockVscode.workspace.workspaceFolders = [ |
| 195 | + { |
| 196 | + name: "container-app", |
| 197 | + uri: { |
| 198 | + toString: () => "file:///workspace/container-app", |
| 199 | + }, |
| 200 | + }, |
| 201 | + ] |
| 202 | + const result = getEnvironmentStoragePath(basePath) |
| 203 | + expect(result).toMatch( |
| 204 | + /^\/home\/user\/.vscode\/extensions\/storage\/remote\/dev-container-container-app-[a-z0-9]+$/, |
| 205 | + ) |
| 206 | + }) |
| 207 | + |
| 208 | + it("should handle Windows paths correctly", () => { |
| 209 | + const windowsBasePath = "C:\\Users\\user\\AppData\\Roaming\\Code\\User\\globalStorage\\roo-cline" |
| 210 | + mockVscode.env.remoteName = "ssh-remote" |
| 211 | + mockVscode.workspace.workspaceFolders = [ |
| 212 | + { |
| 213 | + name: "remote-project", |
| 214 | + uri: { |
| 215 | + toString: () => "file:///home/remote/project", |
| 216 | + }, |
| 217 | + }, |
| 218 | + ] |
| 219 | + const result = getEnvironmentStoragePath(windowsBasePath) |
| 220 | + // The path.join will use forward slashes even on Windows in Node.js context |
| 221 | + expect(result).toMatch( |
| 222 | + /^C:\\Users\\user\\AppData\\Roaming\\Code\\User\\globalStorage\\roo-cline\/remote\/ssh-remote-remote-project-[a-z0-9]+$/, |
| 223 | + ) |
| 224 | + }) |
| 225 | + |
| 226 | + it("should fallback to base path when remote ID cannot be determined", () => { |
| 227 | + mockVscode.env.remoteName = "unknown-remote" |
| 228 | + mockVscode.workspace.workspaceFolders = undefined |
| 229 | + // In this case, getRemoteWorkspaceId returns just "unknown-remote" |
| 230 | + const result = getEnvironmentStoragePath(basePath) |
| 231 | + expect(result).toBe(`${basePath}/remote/unknown-remote`) |
| 232 | + }) |
| 233 | + }) |
| 234 | +}) |
0 commit comments