Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - tests use POSIX path joining to match the Linux startup boundary.
2import * as NodePath from "node:path";
3import { assert, describe, it } from "@effect/vitest";
4
5import {
6 restoreEarlyLinuxDeviceScaleFactor,
7 resolveEarlyLinuxElectronOptions,
8 resolveEarlyLinuxPasswordStorePreference,
9 resolveLinuxDisableFeaturesSwitch,
10} from "./DesktopEarlyElectronStartup.ts";
11
12describe("DesktopEarlyElectronStartup", () => {
13 const joinPath = NodePath.posix.join;
14
15 it("reads the persisted linux password-store preference before Electron is ready", () => {
16 const preference = resolveEarlyLinuxPasswordStorePreference({
17 env: { T3CODE_HOME: "/home/user/.t3-test" },
18 homeDirectory: "/home/user",
19 joinPath,
20 readFileString: (path) => {
21 assert.equal(path, "/home/user/.t3-test/userdata/desktop-settings.json");
22 return JSON.stringify({ linuxPasswordStore: "kwallet6" });
23 },
24 });
25
26 assert.equal(preference, "kwallet6");
27 });
28
29 it("accepts JSONC in the early desktop settings file", () => {
30 const preference = resolveEarlyLinuxPasswordStorePreference({
31 env: { T3CODE_HOME: "/home/user/.t3-test" },
32 homeDirectory: "/home/user",
33 joinPath,
34 readFileString: () => `{
35 // manually edited setting
36 "linuxPasswordStore": "gnome-libsecret",
37 }`,
38 });
39
40 assert.equal(preference, "gnome-libsecret");
41 });
42
43 it("falls back to auto when the early settings document is missing or invalid", () => {
44 const preference = resolveEarlyLinuxPasswordStorePreference({
45 env: {},
46 homeDirectory: "/home/user",
47 joinPath,
48 readFileString: () => {
49 throw new Error("missing");
50 },
51 });
52
53 assert.equal(preference, "auto");
54 });
55
56 it("preserves absolute root paths when resolving early settings", () => {
57 const preference = resolveEarlyLinuxPasswordStorePreference({
58 env: { T3CODE_HOME: "/" },
59 homeDirectory: "/home/user",
60 joinPath,
61 readFileString: (path) => {
62 assert.equal(path, "/userdata/desktop-settings.json");
63 return JSON.stringify({ linuxPasswordStore: "kwallet6" });
64 },
65 });
66
67 assert.equal(preference, "kwallet6");
68 });
69
70 it("resolves the early linux Electron switches", () => {
71 const options = resolveEarlyLinuxElectronOptions({
72 env: {
73 T3CODE_HOME: "/home/user/.t3-test",
74 XDG_CURRENT_DESKTOP: "niri",
75 VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
76 },
77 homeDirectory: "/home/user",
78 joinPath,
79 readFileString: (path) => {
80 assert.equal(path, "/home/user/.t3-test/userdata/desktop-settings.json");
81 return JSON.stringify({ linuxPasswordStore: "auto" });
82 },
83 });
84
85 assert.deepEqual(options, {
86 isDevelopment: true,
87 linuxWmClass: "t3code-dev",
88 linuxDesktopEntryName: "com.t3tools.T3Code.Development.desktop",
89 passwordStore: "gnome-libsecret",
90 });
91 });
92
93 it("keeps implicit development state under ~/.t3/dev when T3CODE_HOME is unset", () => {
94 const preference = resolveEarlyLinuxPasswordStorePreference({
95 env: {
96 VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
97 },
98 homeDirectory: "/home/user",
99 joinPath,
100 readFileString: (path) => {
101 assert.equal(path, "/home/user/.t3/dev/desktop-settings.json");
102 return JSON.stringify({ linuxPasswordStore: "kwallet" });
103 },
104 });
105
106 assert.equal(preference, "kwallet");
107 });
108
109 it("treats whitespace-only T3CODE_HOME as unconfigured in development", () => {
110 const preference = resolveEarlyLinuxPasswordStorePreference({
111 env: {
112 T3CODE_HOME: " ",
113 VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
114 },
115 homeDirectory: "/home/user",
116 joinPath,
117 readFileString: (path) => {
118 assert.equal(path, "/home/user/.t3/dev/desktop-settings.json");
119 return JSON.stringify({ linuxPasswordStore: "gnome-libsecret" });
120 },
121 });
122
123 assert.equal(preference, "gnome-libsecret");
124 });
125
126 describe("resolveLinuxDisableFeaturesSwitch", () => {
127 const hyprland = { XDG_CURRENT_DESKTOP: "Hyprland", XDG_SESSION_TYPE: "wayland" };
128 const releaseCommits: Record<string, string> = {
129 "0.48.0": "5ee35f914f921e5696030698e74fb5566a804768",
130 "0.48.1": "29e2e59fdbab8ed2cc23a20e3c6043d5decb5cdc",
131 "0.49.0": "9958d297641b5c84dcff93f9039d80a5ad37ab00",
132 "0.50.0": "c4a4c341568944bd4fb9cd503558b2de602c0213",
133 "0.50.1": "4e242d086e20b32951fdc0ebcbfb4d41b5be8dcc",
134 };
135 const hyprctlVersion =
136 (version: string, overrides: Record<string, unknown> = {}) =>
137 () =>
138 JSON.stringify({
139 commit: releaseCommits[version] ?? "0123456789abcdef0123456789abcdef01234567",
140 version,
141 dirty: false,
142 tag: `v${version}`,
143 flags: [],
144 ...overrides,
145 });
146 const brokenHyprland = {
147 env: hyprland,
148 ozonePlatform: "wayland",
149 enableFeatures: null,
150 disableFeatures: null,
151 readHyprctlVersionJson: hyprctlVersion("0.48.1"),
152 };
153
154 it.each(["0.48.0", "0.48.1", "0.49.0", "0.50.0", "0.50.1"])(
155 "disables the color manager on Hyprland %s",
156 (version) => {
157 assert.equal(
158 resolveLinuxDisableFeaturesSwitch({
159 ...brokenHyprland,
160 readHyprctlVersionJson: hyprctlVersion(version),
161 }),
162 "WaylandWpColorManagerV1",
163 );
164 },
165 );
166
167 it.each([
168 { label: "case-insensitive desktop tokens", env: { XDG_CURRENT_DESKTOP: "foo:HYPRLAND" } },
169 {
170 label: "XDG_SESSION_DESKTOP when XDG_CURRENT_DESKTOP is unset",
171 env: { XDG_SESSION_DESKTOP: "hyprland" },
172 },
173 ])("detects Hyprland via $label", ({ env }) => {
174 assert.equal(
175 resolveLinuxDisableFeaturesSwitch({ ...brokenHyprland, env }),
176 "WaylandWpColorManagerV1",
177 );
178 });
179
180 it("keeps existing disabled features", () => {
181 assert.equal(
182 resolveLinuxDisableFeaturesSwitch({
183 ...brokenHyprland,
184 disableFeatures: "Vulkan,WaylandWpColorManagerV1Extra",
185 }),
186 "Vulkan,WaylandWpColorManagerV1Extra,WaylandWpColorManagerV1",
187 );
188 });
189
190 it.each([
191 { label: "Hyprland 0.47.2", readHyprctlVersionJson: hyprctlVersion("0.47.2") },
192 { label: "Hyprland 0.51.0", readHyprctlVersionJson: hyprctlVersion("0.51.0") },
193 { label: "Hyprland 0.56.2", readHyprctlVersionJson: hyprctlVersion("0.56.2") },
194 { label: "an unrecognized version", readHyprctlVersionJson: hyprctlVersion("1.0.0") },
195 {
196 label: "a malformed version",
197 readHyprctlVersionJson: hyprctlVersion("0.48.not-a-version"),
198 },
199 { label: "a two-part version", readHyprctlVersionJson: hyprctlVersion("0.48") },
200 { label: "a prerelease version", readHyprctlVersionJson: hyprctlVersion("0.49.0-rc1") },
201 {
202 label: "a development build after an affected release",
203 readHyprctlVersionJson: hyprctlVersion("0.50.1", { tag: "v0.50.1-87-g4e8875b5" }),
204 },
205 {
206 label: "a later Nix build reporting an affected release tag",
207 readHyprctlVersionJson: hyprctlVersion("0.50.1", {
208 commit: "4e8875b5e9700c81ca4e169dc7b85bb5b3c8cb7a",
209 }),
210 },
211 {
212 label: "an affected version with another release's commit",
213 readHyprctlVersionJson: hyprctlVersion("0.48.0", { commit: releaseCommits["0.48.1"] }),
214 },
215 { label: "an unknown patch release", readHyprctlVersionJson: hyprctlVersion("0.50.2") },
216 {
217 label: "missing commit metadata",
218 readHyprctlVersionJson: () =>
219 JSON.stringify({ version: "0.48.1", tag: "v0.48.1", dirty: false }),
220 },
221 {
222 label: "a build without git metadata",
223 readHyprctlVersionJson: hyprctlVersion("0.48.1", { tag: "" }),
224 },
225 {
226 label: "a dirty build",
227 readHyprctlVersionJson: hyprctlVersion("0.48.1", { dirty: true }),
228 },
229 {
230 label: "output without build metadata",
231 readHyprctlVersionJson: () => JSON.stringify({ version: "0.48.1" }),
232 },
233 { label: "unparseable hyprctl output", readHyprctlVersionJson: () => "ok" },
234 {
235 label: "a missing hyprctl",
236 readHyprctlVersionJson: () => {
237 throw new Error("spawnSync hyprctl ENOENT");
238 },
239 },
240 ])("preserves color management for $label", (overrides) => {
241 assert.isNull(resolveLinuxDisableFeaturesSwitch({ ...brokenHyprland, ...overrides }));
242 });
243
244 it.each([
245 {
246 label: "a stale Hyprland signature in KDE",
247 env: { XDG_CURRENT_DESKTOP: "KDE", HYPRLAND_INSTANCE_SIGNATURE: "abc_123" },
248 },
249 {
250 label: "resolved X11 in a Wayland session",
251 ozonePlatform: "x11",
252 env: { ...hyprland, WAYLAND_DISPLAY: "wayland-1" },
253 },
254 { label: "a missing platform", ozonePlatform: null },
255 { label: "an unrelated platform", ozonePlatform: "headless" },
256 { label: "already disabled features", disableFeatures: "Vulkan, WaylandWpColorManagerV1" },
257 { label: "an explicitly enabled feature", enableFeatures: "Vulkan,WaylandWpColorManagerV1" },
258 {
259 label: "a feature enabled with trial parameters",
260 enableFeatures: "*WaylandWpColorManagerV1<Study.Group:mode/strict",
261 },
262 ])("skips the Hyprland probe for $label", (overrides) => {
263 assert.isNull(
264 resolveLinuxDisableFeaturesSwitch({
265 ...brokenHyprland,
266 ...overrides,
267 readHyprctlVersionJson: () => assert.fail("hyprctl should not run"),
268 }),
269 );
270 });
271 });
272});
273
274describe("Linux device scale persistence failures", () => {
275 const input = {
276 env: { T3CODE_HOME: "/isolated" },
277 homeDirectory: "/home/user",
278 joinPath: NodePath.posix.join,
279 };
280
281 it.each(["0", "Infinity", "garbage"])("ignores invalid persisted scale %j", (value) => {
282 const applied: Array<[string, string]> = [];
283 const writes: Array<string> = [];
284 restoreEarlyLinuxDeviceScaleFactor({
285 ...input,
286 commandLine: {
287 hasSwitch: () => false,
288 getSwitchValue: () => "",
289 appendSwitch: (name, scale) => applied.push([name, scale]),
290 },
291 readFileString: () => value,
292 writeFileString: (_path, contents) => writes.push(contents),
293 });
294 assert.deepEqual(applied, []);
295 assert.deepEqual(writes, []);
296 });
297
298 it("keeps startup available with default scaling when saved state cannot be read", () => {
299 const applied: Array<[string, string]> = [];
300 restoreEarlyLinuxDeviceScaleFactor({
301 ...input,
302 commandLine: {
303 hasSwitch: () => false,
304 getSwitchValue: () => "",
305 appendSwitch: (name, value) => applied.push([name, value]),
306 },
307 readFileString: () => {
308 throw new Error("ENOENT");
309 },
310 writeFileString: () => {},
311 });
312 assert.deepEqual(applied, []);
313 });
314
315 it("keeps an explicit scale when saving it fails, without falling back to saved state", () => {
316 const applied: Array<[string, string]> = [];
317 const reads: Array<string> = [];
318 const writes: Array<string> = [];
319 restoreEarlyLinuxDeviceScaleFactor({
320 ...input,
321 commandLine: {
322 hasSwitch: () => true,
323 getSwitchValue: () => "1",
324 appendSwitch: (name, value) => applied.push([name, value]),
325 },
326 readFileString: (path) => {
327 reads.push(path);
328 return "1.75";
329 },
330 writeFileString: (_path, value) => {
331 writes.push(value);
332 throw new Error("EACCES");
333 },
334 });
335 assert.deepEqual(writes, ["1"]);
336 assert.deepEqual(reads, []);
337 assert.deepEqual(applied, []);
338 });
339});