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

desktopSettings.test.ts

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