Open History to see the last commit
1import * as NodePath from "@effect/platform-node/NodePath";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, describe, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7
8import * as DesktopEnvironment from "./DesktopEnvironment.ts";
9import * as DesktopConfig from "./DesktopConfig.ts";
10
11const defaultInput = {
12 dirname: "/repo/apps/desktop/dist-electron",
13 homeDirectory: "/Users/alice",
14 platform: "darwin",
15 processArch: "arm64",
16 appVersion: "0.0.22",
17 appPath: "/Applications/T3 Code.app/Contents/Resources/app.asar",
18 isPackaged: false,
19 resourcesPath: "/Applications/T3 Code.app/Contents/Resources",
20 runningUnderArm64Translation: false,
21} satisfies DesktopEnvironment.MakeDesktopEnvironmentInput;
22
23const makeEnvironmentLayer = (
24 overrides: Partial<DesktopEnvironment.MakeDesktopEnvironmentInput> = {},
25 env: Record<string, string | undefined> = {},
26) =>
27 DesktopEnvironment.layer({
28 ...defaultInput,
29 ...overrides,
30 }).pipe(
31 Layer.provide(
32 Layer.mergeAll(NodeServices.layer, NodePath.layerPosix, DesktopConfig.layerTest(env)),
33 ),
34 );
35
36const makeEnvironment = (
37 overrides: Partial<DesktopEnvironment.MakeDesktopEnvironmentInput> = {},
38 env: Record<string, string | undefined> = {},
39) =>
40 DesktopEnvironment.DesktopEnvironment.pipe(Effect.provide(makeEnvironmentLayer(overrides, env)));
41
42describe("DesktopEnvironment", () => {
43 it.effect("derives state paths and development identity inside Effect", () =>
44 Effect.gen(function* () {
45 const environment = yield* makeEnvironment(
46 {},
47 {
48 T3CODE_HOME: " /tmp/t3 ",
49 T3CODE_COMMIT_HASH: " 0123456789abcdef ",
50 T3CODE_PORT: "4949",
51 VITE_DEV_SERVER_URL: "http://localhost:5173",
52 T3CODE_DEV_REMOTE_T3_SERVER_ENTRY_PATH: " /remote/server.mjs ",
53 T3CODE_OTLP_TRACES_URL: " http://127.0.0.1:4318/v1/traces ",
54 T3CODE_OTLP_EXPORT_INTERVAL_MS: "2500",
55 },
56 );
57
58 assert.equal(environment.isDevelopment, true);
59 assert.equal(environment.appDataDirectory, "/Users/alice/Library/Application Support");
60 assert.equal(environment.baseDir, "/tmp/t3");
61 assert.equal(environment.stateDir, "/tmp/t3/userdata");
62 assert.equal(environment.desktopSettingsPath, "/tmp/t3/userdata/desktop-settings.json");
63 assert.equal(environment.clientSettingsPath, "/tmp/t3/userdata/client-settings.json");
64 assert.equal(
65 environment.savedEnvironmentRegistryPath,
66 "/tmp/t3/userdata/saved-environments.json",
67 );
68 assert.equal(environment.serverSettingsPath, "/tmp/t3/userdata/settings.json");
69 assert.equal(environment.logDir, "/tmp/t3/userdata/logs");
70 assert.equal(environment.browserArtifactsDir, "/tmp/t3/userdata/browser-artifacts");
71 assert.equal(environment.rootDir, "/repo");
72 assert.equal(environment.appRoot, "/repo");
73 assert.equal(environment.serverRoot, "/repo");
74 assert.equal(environment.backendEntryPath, "/repo/apps/server/dist/bin.mjs");
75 assert.equal(environment.backendCwd, "/repo");
76 assert.equal(environment.appUserModelId, "com.t3tools.t3code.dev");
77 assert.equal(environment.linuxWmClass, "t3code-dev");
78 assert.equal(environment.linuxDesktopEntryName, "com.t3tools.T3Code.Development.desktop");
79 assert.deepEqual(
80 Option.map(environment.devServerUrl, (url) => url.href),
81 Option.some("http://localhost:5173/"),
82 );
83 assert.deepEqual(environment.devRemoteT3ServerEntryPath, Option.some("/remote/server.mjs"));
84 assert.deepEqual(environment.configuredBackendPort, Option.some(4949));
85 assert.deepEqual(environment.commitHashOverride, Option.some("0123456789abcdef"));
86 assert.deepEqual(environment.otlpTracesUrl, Option.some("http://127.0.0.1:4318/v1/traces"));
87 assert.equal(environment.otlpExportIntervalMs, 2500);
88 }),
89 );
90
91 it.effect("stores production state under userdata in an explicit home", () =>
92 Effect.gen(function* () {
93 const environment = yield* makeEnvironment(
94 {},
95 {
96 T3CODE_HOME: "/tmp/t3",
97 },
98 );
99
100 assert.equal(environment.isDevelopment, false);
101 assert.equal(environment.stateDir, "/tmp/t3/userdata");
102 assert.equal(environment.logDir, "/tmp/t3/userdata/logs");
103 assert.equal(environment.browserArtifactsDir, "/tmp/t3/userdata/browser-artifacts");
104 assert.equal(environment.serverSettingsPath, "/tmp/t3/userdata/settings.json");
105 assert.equal(environment.userDataDirName, "t3code");
106 assert.equal(environment.legacyUserDataDirName, "T3 Code (Alpha)");
107 }),
108 );
109
110 it.effect("uses the packaged Windows server sidecar as the backend root", () =>
111 Effect.gen(function* () {
112 const environment = yield* makeEnvironment({
113 platform: "win32",
114 isPackaged: true,
115 appPath: "/install/resources/app.asar",
116 resourcesPath: "/install/resources",
117 });
118
119 assert.equal(environment.appRoot, "/install/resources/app.asar");
120 assert.equal(environment.serverRoot, "/install/resources/server.asar");
121 assert.equal(
122 environment.backendEntryPath,
123 "/install/resources/server.asar/apps/server/dist/bin.mjs",
124 );
125 }),
126 );
127
128 it.effect("uses the stable desktop entry as the packaged Linux portal identity", () =>
129 Effect.gen(function* () {
130 const environment = yield* makeEnvironment({
131 platform: "linux",
132 isPackaged: true,
133 appPath: "/tmp/.mount_t3code/resources/app.asar",
134 resourcesPath: "/tmp/.mount_t3code/resources",
135 });
136
137 assert.equal(environment.linuxDesktopEntryName, "com.t3tools.T3Code.desktop");
138 }),
139 );
140
141 it.effect("keeps implicit development state separate from production state", () =>
142 Effect.gen(function* () {
143 const development = yield* makeEnvironment(
144 {},
145 { VITE_DEV_SERVER_URL: "http://localhost:5173" },
146 );
147 const production = yield* makeEnvironment();
148
149 assert.equal(development.stateDir, "/Users/alice/.t3/dev");
150 assert.equal(production.stateDir, "/Users/alice/.t3/userdata");
151 }),
152 );
153
154 it.effect("uses a configured app user model id override", () =>
155 Effect.gen(function* () {
156 const environment = yield* makeEnvironment(
157 {},
158 {
159 T3CODE_DESKTOP_APP_USER_MODEL_ID: " com.t3tools.t3code.dev.local ",
160 VITE_DEV_SERVER_URL: "http://localhost:5173",
161 },
162 );
163
164 assert.equal(environment.appUserModelId, "com.t3tools.t3code.dev.local");
165 }),
166 );
167
168 it.effect("resolves picker defaults without nullish sentinels", () =>
169 Effect.gen(function* () {
170 const environment = yield* makeEnvironment();
171
172 assert.deepEqual(environment.resolvePickFolderDefaultPath(null), Option.none());
173 assert.deepEqual(
174 environment.resolvePickFolderDefaultPath({ initialPath: " " }),
175 Option.none(),
176 );
177 assert.deepEqual(
178 environment.resolvePickFolderDefaultPath({ initialPath: "~" }),
179 Option.some("/Users/alice"),
180 );
181 assert.deepEqual(
182 environment.resolvePickFolderDefaultPath({ initialPath: "~/project" }),
183 Option.some("/Users/alice/project"),
184 );
185 }),
186 );
187});