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