| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | applyWslEnvironmentConfiguration, |
| 5 | parseWslUncPath, |
| 6 | resolveProjectPickerTarget, |
| 7 | resolveWslProjectSelection, |
| 8 | } from "./wslPaths"; |
| 9 | |
| 10 | describe("parseWslUncPath", () => { |
| 11 | it("parses wsl.localhost UNC paths into distro and POSIX path", () => { |
| 12 | expect(parseWslUncPath("\\\\wsl.localhost\\Ubuntu-22.04\\home\\josh\\repo")).toEqual({ |
| 13 | distro: "Ubuntu-22.04", |
| 14 | linuxPath: "/home/josh/repo", |
| 15 | }); |
| 16 | }); |
| 17 | |
| 18 | it("parses wsl$ UNC roots as distro root", () => { |
| 19 | expect(parseWslUncPath("\\\\wsl$\\Debian")).toEqual({ |
| 20 | distro: "Debian", |
| 21 | linuxPath: "/", |
| 22 | }); |
| 23 | }); |
| 24 | |
| 25 | it("rejects non-WSL paths and invalid distro names", () => { |
| 26 | expect(parseWslUncPath("C:\\Users\\Josh\\repo")).toBeNull(); |
| 27 | expect(parseWslUncPath("\\\\wsl.localhost\\bad!name\\home")).toBeNull(); |
| 28 | }); |
| 29 | }); |
| 30 | |
| 31 | describe("resolveWslProjectSelection", () => { |
| 32 | it("routes a UNC path to the matching WSL backend", () => { |
| 33 | expect( |
| 34 | resolveWslProjectSelection("\\\\wsl.localhost\\Ubuntu\\home\\theo\\repo", [ |
| 35 | { environmentId: "env-debian", backendId: "wsl:Debian", runningDistro: null }, |
| 36 | { environmentId: "env-ubuntu", backendId: "wsl:Ubuntu", runningDistro: null }, |
| 37 | ]), |
| 38 | ).toEqual({ |
| 39 | distro: "Ubuntu", |
| 40 | environmentId: "env-ubuntu", |
| 41 | linuxPath: "/home/theo/repo", |
| 42 | }); |
| 43 | }); |
| 44 | |
| 45 | it("does not route to the only WSL backend when its distro is unknown", () => { |
| 46 | expect( |
| 47 | resolveWslProjectSelection("\\\\wsl.localhost\\Ubuntu\\home\\theo\\repo", [ |
| 48 | { environmentId: "env-wsl", backendId: "wsl:default", runningDistro: null }, |
| 49 | ]), |
| 50 | ).toBeNull(); |
| 51 | }); |
| 52 | |
| 53 | it("does not route to a sole WSL backend for a different distro", () => { |
| 54 | expect( |
| 55 | resolveWslProjectSelection("\\\\wsl.localhost\\Debian\\home\\theo\\repo", [ |
| 56 | { environmentId: "env-ubuntu", backendId: "wsl:Ubuntu", runningDistro: null }, |
| 57 | ]), |
| 58 | ).toBeNull(); |
| 59 | }); |
| 60 | |
| 61 | it("does not guess when multiple WSL backends fail to match", () => { |
| 62 | expect( |
| 63 | resolveWslProjectSelection("\\\\wsl.localhost\\Fedora\\home\\theo\\repo", [ |
| 64 | { environmentId: "env-debian", backendId: "wsl:Debian", runningDistro: null }, |
| 65 | { environmentId: "env-ubuntu", backendId: "wsl:Ubuntu", runningDistro: null }, |
| 66 | ]), |
| 67 | ).toBeNull(); |
| 68 | }); |
| 69 | |
| 70 | it("routes a default backend only to the distro used by its running process", () => { |
| 71 | const candidates = [ |
| 72 | { environmentId: "env-wsl", backendId: "wsl:default", runningDistro: "Debian" }, |
| 73 | ]; |
| 74 | |
| 75 | expect( |
| 76 | resolveWslProjectSelection("\\\\wsl.localhost\\Debian\\home\\theo\\repo", candidates), |
| 77 | ).toEqual({ |
| 78 | distro: "Debian", |
| 79 | environmentId: "env-wsl", |
| 80 | linuxPath: "/home/theo/repo", |
| 81 | }); |
| 82 | expect( |
| 83 | resolveWslProjectSelection("\\\\wsl.localhost\\Ubuntu\\home\\theo\\repo", candidates), |
| 84 | ).toBeNull(); |
| 85 | }); |
| 86 | }); |
| 87 | |
| 88 | describe("applyWslEnvironmentConfiguration", () => { |
| 89 | const ubuntuConfiguration = { |
| 90 | enabled: true, |
| 91 | wslOnly: false, |
| 92 | distro: null, |
| 93 | distros: [ |
| 94 | { name: "Debian", isDefault: false }, |
| 95 | { name: "Ubuntu", isDefault: true }, |
| 96 | ], |
| 97 | }; |
| 98 | |
| 99 | it("preserves a live default-distro backend instance id", () => { |
| 100 | expect( |
| 101 | applyWslEnvironmentConfiguration( |
| 102 | [ |
| 103 | { |
| 104 | environmentId: "env-wsl", |
| 105 | backendId: "wsl:default", |
| 106 | runningDistro: "Debian", |
| 107 | }, |
| 108 | ], |
| 109 | "env-primary", |
| 110 | ubuntuConfiguration, |
| 111 | ), |
| 112 | ).toEqual([{ environmentId: "env-wsl", backendId: "wsl:default", runningDistro: "Debian" }]); |
| 113 | }); |
| 114 | |
| 115 | it("does not replace a live default backend's running distro from current configuration", () => { |
| 116 | const candidates = applyWslEnvironmentConfiguration( |
| 117 | [{ environmentId: "env-wsl", backendId: "wsl:default", runningDistro: "Debian" }], |
| 118 | "env-primary", |
| 119 | ubuntuConfiguration, |
| 120 | ); |
| 121 | |
| 122 | expect( |
| 123 | resolveWslProjectSelection("\\\\wsl.localhost\\Ubuntu\\home\\theo\\repo", candidates), |
| 124 | ).toBeNull(); |
| 125 | expect( |
| 126 | resolveWslProjectSelection("\\\\wsl.localhost\\Debian\\home\\theo\\repo", candidates), |
| 127 | ).toEqual({ |
| 128 | distro: "Debian", |
| 129 | environmentId: "env-wsl", |
| 130 | linuxPath: "/home/theo/repo", |
| 131 | }); |
| 132 | }); |
| 133 | |
| 134 | it("represents an explicitly configured WSL-only primary by its distro", () => { |
| 135 | expect( |
| 136 | applyWslEnvironmentConfiguration([], "env-primary", { |
| 137 | ...ubuntuConfiguration, |
| 138 | wslOnly: true, |
| 139 | distro: "ubuntu", |
| 140 | }), |
| 141 | ).toEqual([{ environmentId: "env-primary", backendId: "wsl:Ubuntu", runningDistro: "Ubuntu" }]); |
| 142 | }); |
| 143 | |
| 144 | it("preserves default tracking for a WSL-only primary", () => { |
| 145 | expect( |
| 146 | applyWslEnvironmentConfiguration([], "env-primary", { |
| 147 | ...ubuntuConfiguration, |
| 148 | wslOnly: true, |
| 149 | }), |
| 150 | ).toEqual([{ environmentId: "env-primary", backendId: "wsl:default", runningDistro: null }]); |
| 151 | }); |
| 152 | |
| 153 | it("uses the live primary distro for a default-tracking WSL-only primary", () => { |
| 154 | const candidates = applyWslEnvironmentConfiguration( |
| 155 | [], |
| 156 | "env-primary", |
| 157 | { |
| 158 | ...ubuntuConfiguration, |
| 159 | wslOnly: true, |
| 160 | distros: [], |
| 161 | }, |
| 162 | "Ubuntu", |
| 163 | ); |
| 164 | |
| 165 | expect(candidates).toEqual([ |
| 166 | { environmentId: "env-primary", backendId: "wsl:default", runningDistro: "Ubuntu" }, |
| 167 | ]); |
| 168 | expect( |
| 169 | resolveWslProjectSelection("\\\\wsl.localhost\\Ubuntu\\home\\theo\\repo", candidates), |
| 170 | ).toEqual({ |
| 171 | distro: "Ubuntu", |
| 172 | environmentId: "env-primary", |
| 173 | linuxPath: "/home/theo/repo", |
| 174 | }); |
| 175 | }); |
| 176 | |
| 177 | it("keeps a configured distro authoritative when discovery does not contain it", () => { |
| 178 | expect( |
| 179 | applyWslEnvironmentConfiguration([], "env-primary", { |
| 180 | ...ubuntuConfiguration, |
| 181 | wslOnly: true, |
| 182 | distro: "Fedora", |
| 183 | }), |
| 184 | ).toEqual([{ environmentId: "env-primary", backendId: "wsl:Fedora", runningDistro: "Fedora" }]); |
| 185 | }); |
| 186 | |
| 187 | it("does not synthesize a backend for an empty configured distro name", () => { |
| 188 | expect( |
| 189 | applyWslEnvironmentConfiguration([], "env-primary", { |
| 190 | ...ubuntuConfiguration, |
| 191 | wslOnly: true, |
| 192 | distro: " ", |
| 193 | }), |
| 194 | ).toEqual([]); |
| 195 | }); |
| 196 | }); |
| 197 | |
| 198 | describe("resolveProjectPickerTarget", () => { |
| 199 | const ubuntuConfiguration = { |
| 200 | enabled: true, |
| 201 | wslOnly: true, |
| 202 | distro: "Ubuntu-22.04", |
| 203 | distros: [ |
| 204 | { name: "Debian", isDefault: true }, |
| 205 | { name: "Ubuntu-22.04", isDefault: false }, |
| 206 | ], |
| 207 | }; |
| 208 | |
| 209 | it("routes a WSL-only primary picker to its configured distro", () => { |
| 210 | expect( |
| 211 | resolveProjectPickerTarget({ |
| 212 | browseEnvironmentId: "env-primary", |
| 213 | primaryEnvironmentId: "env-primary", |
| 214 | desktopInstanceId: null, |
| 215 | wslConfiguration: ubuntuConfiguration, |
| 216 | }), |
| 217 | ).toBe("wsl:Ubuntu-22.04"); |
| 218 | }); |
| 219 | |
| 220 | it("routes a configured distro while discovery is temporarily empty", () => { |
| 221 | expect( |
| 222 | resolveProjectPickerTarget({ |
| 223 | browseEnvironmentId: "env-primary", |
| 224 | primaryEnvironmentId: "env-primary", |
| 225 | desktopInstanceId: null, |
| 226 | wslConfiguration: { ...ubuntuConfiguration, distro: "ubuntu-22.04", distros: [] }, |
| 227 | }), |
| 228 | ).toBe("wsl:ubuntu-22.04"); |
| 229 | }); |
| 230 | |
| 231 | it("uses installed casing when discovery finds the configured distro", () => { |
| 232 | expect( |
| 233 | resolveProjectPickerTarget({ |
| 234 | browseEnvironmentId: "env-primary", |
| 235 | primaryEnvironmentId: "env-primary", |
| 236 | desktopInstanceId: null, |
| 237 | wslConfiguration: { ...ubuntuConfiguration, distro: "ubuntu-22.04" }, |
| 238 | }), |
| 239 | ).toBe("wsl:Ubuntu-22.04"); |
| 240 | }); |
| 241 | |
| 242 | it("routes a default-tracking WSL-only primary picker through the live sentinel", () => { |
| 243 | expect( |
| 244 | resolveProjectPickerTarget({ |
| 245 | browseEnvironmentId: "env-primary", |
| 246 | primaryEnvironmentId: "env-primary", |
| 247 | desktopInstanceId: null, |
| 248 | wslConfiguration: { ...ubuntuConfiguration, distro: null }, |
| 249 | }), |
| 250 | ).toBe("wsl:default"); |
| 251 | }); |
| 252 | |
| 253 | it("routes a default-tracking picker when the distro catalog has no default", () => { |
| 254 | expect( |
| 255 | resolveProjectPickerTarget({ |
| 256 | browseEnvironmentId: "env-primary", |
| 257 | primaryEnvironmentId: "env-primary", |
| 258 | desktopInstanceId: null, |
| 259 | wslConfiguration: { |
| 260 | ...ubuntuConfiguration, |
| 261 | distro: null, |
| 262 | distros: [{ name: "Ubuntu-22.04", isDefault: false }], |
| 263 | }, |
| 264 | }), |
| 265 | ).toBe("wsl:default"); |
| 266 | |
| 267 | expect( |
| 268 | resolveProjectPickerTarget({ |
| 269 | browseEnvironmentId: "env-primary", |
| 270 | primaryEnvironmentId: "env-primary", |
| 271 | desktopInstanceId: null, |
| 272 | wslConfiguration: { ...ubuntuConfiguration, distro: null, distros: [] }, |
| 273 | }), |
| 274 | ).toBe("wsl:default"); |
| 275 | }); |
| 276 | |
| 277 | it("preserves combo-mode routing for primary and WSL backends", () => { |
| 278 | const comboConfiguration = { ...ubuntuConfiguration, wslOnly: false }; |
| 279 | |
| 280 | expect( |
| 281 | resolveProjectPickerTarget({ |
| 282 | browseEnvironmentId: "env-primary", |
| 283 | primaryEnvironmentId: "env-primary", |
| 284 | desktopInstanceId: null, |
| 285 | wslConfiguration: comboConfiguration, |
| 286 | }), |
| 287 | ).toBeNull(); |
| 288 | expect( |
| 289 | resolveProjectPickerTarget({ |
| 290 | browseEnvironmentId: "env-wsl", |
| 291 | primaryEnvironmentId: "env-primary", |
| 292 | desktopInstanceId: "wsl:Ubuntu-22.04", |
| 293 | wslConfiguration: comboConfiguration, |
| 294 | }), |
| 295 | ).toBe("wsl:Ubuntu-22.04"); |
| 296 | }); |
| 297 | }); |