Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

desktopSettings.test.ts

147 lines4.4 KB

1import * as fs from "node:fs";
2import * as os from "node:os";
3import * as path from "node:path";
4​
5import { afterEach, describe, expect, it } from "vitest";
6​
7import {
8 DEFAULT_DESKTOP_SETTINGS,
9 readDesktopSettings,
10 resolveDefaultDesktopSettings,
11 setDesktopServerExposurePreference,
12 setDesktopUpdateChannelPreference,
13 writeDesktopSettings,
14} from "./desktopSettings.ts";
15​
16const tempDirectories: string[] = [];
17​
18afterEach(() => {
19 for (const directory of tempDirectories.splice(0)) {
20 fs.rmSync(directory, { recursive: true, force: true });
21 }
22});
23​
24function makeSettingsPath() {
25 const directory = fs.mkdtempSync(path.join(os.tmpdir(), "t3-desktop-settings-test-"));
26 tempDirectories.push(directory);
27 return path.join(directory, "desktop-settings.json");
29​
30describe("desktopSettings", () => {
31 it("returns defaults when no settings file exists", () => {
32 expect(readDesktopSettings(makeSettingsPath(), "0.0.17")).toEqual(DEFAULT_DESKTOP_SETTINGS);
33 });
34​
35 it("defaults packaged nightly builds to the nightly update channel", () => {
36 expect(resolveDefaultDesktopSettings("0.0.17-nightly.20260415.1")).toEqual({
37 serverExposureMode: "local-only",
38 updateChannel: "nightly",
39 updateChannelConfiguredByUser: false,
40 });
41 });
42​
43 it("persists and reloads the configured server exposure mode", () => {
44 const settingsPath = makeSettingsPath();
45​
46 writeDesktopSettings(settingsPath, {
47 serverExposureMode: "network-accessible",
48 updateChannel: "latest",
49 updateChannelConfiguredByUser: true,
50 });
51​
52 expect(readDesktopSettings(settingsPath, "0.0.17")).toEqual({
53 serverExposureMode: "network-accessible",
54 updateChannel: "latest",
55 updateChannelConfiguredByUser: true,
56 });
57 });
58​
59 it("preserves the requested network-accessible preference across temporary fallback", () => {
60 expect(
61 setDesktopServerExposurePreference(
62 {
63 serverExposureMode: "local-only",
64 updateChannel: "latest",
65 updateChannelConfiguredByUser: false,
66 },
67 "network-accessible",
68 ),
69 ).toEqual({
70 serverExposureMode: "network-accessible",
71 updateChannel: "latest",
72 updateChannelConfiguredByUser: false,
73 });
74 });
75​
76 it("persists the requested nightly update channel", () => {
77 expect(
78 setDesktopUpdateChannelPreference(
79 {
80 serverExposureMode: "local-only",
81 updateChannel: "latest",
82 updateChannelConfiguredByUser: false,
83 },
84 "nightly",
85 ),
86 ).toEqual({
87 serverExposureMode: "local-only",
88 updateChannel: "nightly",
89 updateChannelConfiguredByUser: true,
90 });
91 });
92​
93 it("falls back to defaults when the settings file is malformed", () => {
94 const settingsPath = makeSettingsPath();
95 fs.writeFileSync(settingsPath, "{not-json", "utf8");
96​
97 expect(readDesktopSettings(settingsPath, "0.0.17")).toEqual(DEFAULT_DESKTOP_SETTINGS);
98 });
99​
100 it("falls back to the nightly channel for legacy nightly settings without an update track", () => {
101 const settingsPath = makeSettingsPath();
102 fs.writeFileSync(settingsPath, JSON.stringify({ serverExposureMode: "local-only" }), "utf8");
103​
104 expect(readDesktopSettings(settingsPath, "0.0.17-nightly.20260415.1")).toEqual({
105 serverExposureMode: "local-only",
106 updateChannel: "nightly",
107 updateChannelConfiguredByUser: false,
108 });
109 });
110​
111 it("migrates legacy implicit stable settings to nightly when running a nightly build", () => {
112 const settingsPath = makeSettingsPath();
113 fs.writeFileSync(
114 settingsPath,
115 JSON.stringify({
116 serverExposureMode: "local-only",
117 updateChannel: "latest",
118 }),
119 "utf8",
120 );
121​
122 expect(readDesktopSettings(settingsPath, "0.0.17-nightly.20260415.1")).toEqual({
123 serverExposureMode: "local-only",
124 updateChannel: "nightly",
125 updateChannelConfiguredByUser: false,
126 });
127 });
128​
129 it("preserves an explicit stable choice on nightly builds", () => {
130 const settingsPath = makeSettingsPath();
131 fs.writeFileSync(
132 settingsPath,
133 JSON.stringify({
134 serverExposureMode: "local-only",
135 updateChannel: "latest",
136 updateChannelConfiguredByUser: true,
137 }),
138 "utf8",
139 );
140​
141 expect(readDesktopSettings(settingsPath, "0.0.17-nightly.20260415.1")).toEqual({
142 serverExposureMode: "local-only",
143 updateChannel: "latest",
144 updateChannelConfiguredByUser: true,
145 });
146 });
147});