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 Effect.gen(function* () {
36 return yield* DesktopEnvironment.DesktopEnvironment;
37 }).pipe(Effect.provide(makeEnvironmentLayer(overrides, env)));
38
39describe("DesktopEnvironment", () => {
40 it.effect("derives state paths and development identity inside Effect", () =>
41 Effect.gen(function* () {
42 const environment = yield* makeEnvironment(
43 {},
44 {
45 T3CODE_HOME: " /tmp/t3 ",
46 T3CODE_COMMIT_HASH: " 0123456789abcdef ",
47 T3CODE_PORT: "4949",
48 VITE_DEV_SERVER_URL: "http://localhost:5173",
49 T3CODE_DEV_REMOTE_T3_SERVER_ENTRY_PATH: " /remote/server.mjs ",
50 T3CODE_OTLP_TRACES_URL: " http://127.0.0.1:4318/v1/traces ",
51 T3CODE_OTLP_EXPORT_INTERVAL_MS: "2500",
52 },
53 );
54
55 assert.equal(environment.isDevelopment, true);
56 assert.equal(environment.appDataDirectory, "/Users/alice/Library/Application Support");
57 assert.equal(environment.baseDir, "/tmp/t3");
58 assert.equal(environment.stateDir, "/tmp/t3/dev");
59 assert.equal(environment.desktopSettingsPath, "/tmp/t3/dev/desktop-settings.json");
60 assert.equal(environment.clientSettingsPath, "/tmp/t3/dev/client-settings.json");
61 assert.equal(environment.savedEnvironmentRegistryPath, "/tmp/t3/dev/saved-environments.json");
62 assert.equal(environment.serverSettingsPath, "/tmp/t3/dev/settings.json");
63 assert.equal(environment.logDir, "/tmp/t3/dev/logs");
64 assert.equal(environment.rootDir, "/repo");
65 assert.equal(environment.appRoot, "/repo");
66 assert.equal(environment.backendEntryPath, "/repo/apps/server/dist/bin.mjs");
67 assert.equal(environment.backendCwd, "/repo");
68 assert.equal(environment.appUserModelId, "com.t3tools.t3code.dev");
69 assert.equal(environment.linuxWmClass, "t3code-dev");
70 assert.deepEqual(
71 Option.map(environment.devServerUrl, (url) => url.href),
72 Option.some("http://localhost:5173/"),
73 );
74 assert.deepEqual(environment.devRemoteT3ServerEntryPath, Option.some("/remote/server.mjs"));
75 assert.deepEqual(environment.configuredBackendPort, Option.some(4949));
76 assert.deepEqual(environment.commitHashOverride, Option.some("0123456789abcdef"));
77 assert.deepEqual(environment.otlpTracesUrl, Option.some("http://127.0.0.1:4318/v1/traces"));
78 assert.equal(environment.otlpExportIntervalMs, 2500);
79 }),
80 );
81
82 it.effect("derives production state paths under userdata", () =>
83 Effect.gen(function* () {
84 const environment = yield* makeEnvironment(
85 {},
86 {
87 T3CODE_HOME: "/tmp/t3",
88 },
89 );
90
91 assert.equal(environment.isDevelopment, false);
92 assert.equal(environment.stateDir, "/tmp/t3/userdata");
93 assert.equal(environment.logDir, "/tmp/t3/userdata/logs");
94 assert.equal(environment.serverSettingsPath, "/tmp/t3/userdata/settings.json");
95 }),
96 );
97
98 it.effect("resolves picker defaults without nullish sentinels", () =>
99 Effect.gen(function* () {
100 const environment = yield* makeEnvironment();
101
102 assert.deepEqual(environment.resolvePickFolderDefaultPath(null), Option.none());
103 assert.deepEqual(
104 environment.resolvePickFolderDefaultPath({ initialPath: " " }),
105 Option.none(),
106 );
107 assert.deepEqual(
108 environment.resolvePickFolderDefaultPath({ initialPath: "~" }),
109 Option.some("/Users/alice"),
110 );
111 assert.deepEqual(
112 environment.resolvePickFolderDefaultPath({ initialPath: "~/project" }),
113 Option.some("/Users/alice/project"),
114 );
115 }),
116 );
117});