Open History to see the last commit
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 setDesktopServerExposurePreference,
11 setDesktopUpdateChannelPreference,
12 writeDesktopSettings,
13} from "./desktopSettings";
14
15const tempDirectories: string[] = [];
16
17afterEach(() => {
18 for (const directory of tempDirectories.splice(0)) {
19 fs.rmSync(directory, { recursive: true, force: true });
20 }
21});
22
23function makeSettingsPath() {
24 const directory = fs.mkdtempSync(path.join(os.tmpdir(), "t3-desktop-settings-test-"));
25 tempDirectories.push(directory);
26 return path.join(directory, "desktop-settings.json");
27}
28
29describe("desktopSettings", () => {
30 it("returns defaults when no settings file exists", () => {
31 expect(readDesktopSettings(makeSettingsPath())).toEqual(DEFAULT_DESKTOP_SETTINGS);
32 });
33
34 it("persists and reloads the configured server exposure mode", () => {
35 const settingsPath = makeSettingsPath();
36
37 writeDesktopSettings(settingsPath, {
38 serverExposureMode: "network-accessible",
39 updateChannel: "latest",
40 });
41
42 expect(readDesktopSettings(settingsPath)).toEqual({
43 serverExposureMode: "network-accessible",
44 updateChannel: "latest",
45 });
46 });
47
48 it("preserves the requested network-accessible preference across temporary fallback", () => {
49 expect(
50 setDesktopServerExposurePreference(
51 {
52 serverExposureMode: "local-only",
53 updateChannel: "latest",
54 },
55 "network-accessible",
56 ),
57 ).toEqual({
58 serverExposureMode: "network-accessible",
59 updateChannel: "latest",
60 });
61 });
62
63 it("persists the requested nightly update channel", () => {
64 expect(
65 setDesktopUpdateChannelPreference(
66 {
67 serverExposureMode: "local-only",
68 updateChannel: "latest",
69 },
70 "nightly",
71 ),
72 ).toEqual({
73 serverExposureMode: "local-only",
74 updateChannel: "nightly",
75 });
76 });
77
78 it("falls back to defaults when the settings file is malformed", () => {
79 const settingsPath = makeSettingsPath();
80 fs.writeFileSync(settingsPath, "{not-json", "utf8");
81
82 expect(readDesktopSettings(settingsPath)).toEqual(DEFAULT_DESKTOP_SETTINGS);
83 });
84});