1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 applyWslEnvironmentConfiguration,
5 parseWslUncPath,
6 resolveProjectPickerTarget,
7 resolveWslProjectSelection,
8} from "./wslPaths";
9
10describe("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("rejects non-WSL paths and invalid distro names", () => {
19 expect(parseWslUncPath("C:\\Users\\Josh\\repo")).toBeNull();
20 expect(parseWslUncPath("\\\\wsl.localhost\\bad!name\\home")).toBeNull();
21 });
22});
23
24describe("resolveWslProjectSelection", () => {
25 it("routes a UNC path to the matching WSL backend", () => {
26 expect(
27 resolveWslProjectSelection("\\\\wsl.localhost\\Ubuntu\\home\\theo\\repo", [
28 { environmentId: "env-debian", backendId: "wsl:Debian", runningDistro: null },
29 { environmentId: "env-ubuntu", backendId: "wsl:Ubuntu", runningDistro: null },
30 ]),
31 ).toEqual({
32 distro: "Ubuntu",
33 environmentId: "env-ubuntu",
34 linuxPath: "/home/theo/repo",
35 });
36 });
37
38 it("does not route to a sole WSL backend for a different distro", () => {
39 expect(
40 resolveWslProjectSelection("\\\\wsl.localhost\\Debian\\home\\theo\\repo", [
41 { environmentId: "env-ubuntu", backendId: "wsl:Ubuntu", runningDistro: null },
42 ]),
43 ).toBeNull();
44 });
45
46 it("routes a default backend only to the distro used by its running process", () => {
47 const candidates = [
48 { environmentId: "env-wsl", backendId: "wsl:default", runningDistro: "Debian" },
49 ];
50
51 expect(
52 resolveWslProjectSelection("\\\\wsl.localhost\\Debian\\home\\theo\\repo", candidates),
53 ).toEqual({
54 distro: "Debian",
55 environmentId: "env-wsl",
56 linuxPath: "/home/theo/repo",
57 });
58 expect(
59 resolveWslProjectSelection("\\\\wsl.localhost\\Ubuntu\\home\\theo\\repo", candidates),
60 ).toBeNull();
61 });
62});
63
64describe("applyWslEnvironmentConfiguration", () => {
65 const ubuntuConfiguration = {
66 enabled: true,
67 wslOnly: false,
68 distro: null,
69 distros: [
70 { name: "Debian", isDefault: false },
71 { name: "Ubuntu", isDefault: true },
72 ],
73 };
74
75 it("does not replace a live default backend's running distro from current configuration", () => {
76 const candidates = applyWslEnvironmentConfiguration(
77 [{ environmentId: "env-wsl", backendId: "wsl:default", runningDistro: "Debian" }],
78 "env-primary",
79 ubuntuConfiguration,
80 );
81
82 expect(
83 resolveWslProjectSelection("\\\\wsl.localhost\\Ubuntu\\home\\theo\\repo", candidates),
84 ).toBeNull();
85 expect(
86 resolveWslProjectSelection("\\\\wsl.localhost\\Debian\\home\\theo\\repo", candidates),
87 ).toEqual({
88 distro: "Debian",
89 environmentId: "env-wsl",
90 linuxPath: "/home/theo/repo",
91 });
92 });
93
94 it("represents an explicitly configured WSL-only primary by its distro", () => {
95 expect(
96 applyWslEnvironmentConfiguration([], "env-primary", {
97 ...ubuntuConfiguration,
98 wslOnly: true,
99 distro: "ubuntu",
100 }),
101 ).toEqual([{ environmentId: "env-primary", backendId: "wsl:Ubuntu", runningDistro: "Ubuntu" }]);
102 });
103
104 it("uses the live primary distro for a default-tracking WSL-only primary", () => {
105 const candidates = applyWslEnvironmentConfiguration(
106 [],
107 "env-primary",
108 {
109 ...ubuntuConfiguration,
110 wslOnly: true,
111 distros: [],
112 },
113 "Ubuntu",
114 );
115
116 expect(candidates).toEqual([
117 { environmentId: "env-primary", backendId: "wsl:default", runningDistro: "Ubuntu" },
118 ]);
119 expect(
120 resolveWslProjectSelection("\\\\wsl.localhost\\Ubuntu\\home\\theo\\repo", candidates),
121 ).toEqual({
122 distro: "Ubuntu",
123 environmentId: "env-primary",
124 linuxPath: "/home/theo/repo",
125 });
126 });
127});
128
129describe("resolveProjectPickerTarget", () => {
130 const ubuntuConfiguration = {
131 enabled: true,
132 wslOnly: true,
133 distro: "Ubuntu-22.04",
134 distros: [
135 { name: "Debian", isDefault: true },
136 { name: "Ubuntu-22.04", isDefault: false },
137 ],
138 };
139
140 it("routes a WSL-only primary picker to its configured distro", () => {
141 expect(
142 resolveProjectPickerTarget({
143 browseEnvironmentId: "env-primary",
144 primaryEnvironmentId: "env-primary",
145 desktopInstanceId: null,
146 wslConfiguration: ubuntuConfiguration,
147 }),
148 ).toBe("wsl:Ubuntu-22.04");
149 });
150
151 it("uses installed casing when discovery finds the configured distro", () => {
152 expect(
153 resolveProjectPickerTarget({
154 browseEnvironmentId: "env-primary",
155 primaryEnvironmentId: "env-primary",
156 desktopInstanceId: null,
157 wslConfiguration: { ...ubuntuConfiguration, distro: "ubuntu-22.04" },
158 }),
159 ).toBe("wsl:Ubuntu-22.04");
160 });
161
162 it("routes a default-tracking WSL-only primary picker through the live sentinel", () => {
163 expect(
164 resolveProjectPickerTarget({
165 browseEnvironmentId: "env-primary",
166 primaryEnvironmentId: "env-primary",
167 desktopInstanceId: null,
168 wslConfiguration: { ...ubuntuConfiguration, distro: null },
169 }),
170 ).toBe("wsl:default");
171 });
172
173 it("preserves combo-mode routing for primary and WSL backends", () => {
174 const comboConfiguration = { ...ubuntuConfiguration, wslOnly: false };
175
176 expect(
177 resolveProjectPickerTarget({
178 browseEnvironmentId: "env-primary",
179 primaryEnvironmentId: "env-primary",
180 desktopInstanceId: null,
181 wslConfiguration: comboConfiguration,
182 }),
183 ).toBeNull();
184 expect(
185 resolveProjectPickerTarget({
186 browseEnvironmentId: "env-wsl",
187 primaryEnvironmentId: "env-primary",
188 desktopInstanceId: "wsl:Ubuntu-22.04",
189 wslConfiguration: comboConfiguration,
190 }),
191 ).toBe("wsl:Ubuntu-22.04");
192 });
193});