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

DesktopEnvironment.test.ts

131 lines5.3 KB

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/dev");
57 assert.equal(environment.desktopSettingsPath, "/tmp/t3/dev/desktop-settings.json");
58 assert.equal(environment.clientSettingsPath, "/tmp/t3/dev/client-settings.json");
59 assert.equal(environment.savedEnvironmentRegistryPath, "/tmp/t3/dev/saved-environments.json");
60 assert.equal(environment.serverSettingsPath, "/tmp/t3/dev/settings.json");
61 assert.equal(environment.logDir, "/tmp/t3/dev/logs");
62 assert.equal(environment.browserArtifactsDir, "/tmp/t3/dev/browser-artifacts");
63 assert.equal(environment.rootDir, "/repo");
64 assert.equal(environment.appRoot, "/repo");
65 assert.equal(environment.backendEntryPath, "/repo/apps/server/dist/bin.mjs");
66 assert.equal(environment.backendCwd, "/repo");
67 assert.equal(environment.appUserModelId, "com.t3tools.t3code.dev");
68 assert.equal(environment.linuxWmClass, "t3code-dev");
69 assert.deepEqual(
70 Option.map(environment.devServerUrl, (url) => url.href),
71 Option.some("http://localhost:5173/"),
72 );
73 assert.deepEqual(environment.devRemoteT3ServerEntryPath, Option.some("/remote/server.mjs"));
74 assert.deepEqual(environment.configuredBackendPort, Option.some(4949));
75 assert.deepEqual(environment.commitHashOverride, Option.some("0123456789abcdef"));
76 assert.deepEqual(environment.otlpTracesUrl, Option.some("http://127.0.0.1:4318/v1/traces"));
77 assert.equal(environment.otlpExportIntervalMs, 2500);
78 }),
79 );
80​
81 it.effect("derives production state paths under userdata", () =>
82 Effect.gen(function* () {
83 const environment = yield* makeEnvironment(
84 {},
85 {
86 T3CODE_HOME: "/tmp/t3",
87 },
88 );
89​
90 assert.equal(environment.isDevelopment, false);
91 assert.equal(environment.stateDir, "/tmp/t3/userdata");
92 assert.equal(environment.logDir, "/tmp/t3/userdata/logs");
93 assert.equal(environment.browserArtifactsDir, "/tmp/t3/userdata/browser-artifacts");
94 assert.equal(environment.serverSettingsPath, "/tmp/t3/userdata/settings.json");
95 }),
96 );
97​
98 it.effect("uses a configured app user model id override", () =>
99 Effect.gen(function* () {
100 const environment = yield* makeEnvironment(
101 {},
102 {
103 T3CODE_DESKTOP_APP_USER_MODEL_ID: " com.t3tools.t3code.dev.local ",
104 VITE_DEV_SERVER_URL: "http://localhost:5173",
105 },
106 );
107​
108 assert.equal(environment.appUserModelId, "com.t3tools.t3code.dev.local");
109 }),
110 );
111​
112 it.effect("resolves picker defaults without nullish sentinels", () =>
113 Effect.gen(function* () {
114 const environment = yield* makeEnvironment();
115​
116 assert.deepEqual(environment.resolvePickFolderDefaultPath(null), Option.none());
117 assert.deepEqual(
118 environment.resolvePickFolderDefaultPath({ initialPath: " " }),
119 Option.none(),
120 );
121 assert.deepEqual(
122 environment.resolvePickFolderDefaultPath({ initialPath: "~" }),
123 Option.some("/Users/alice"),
124 );
125 assert.deepEqual(
126 environment.resolvePickFolderDefaultPath({ initialPath: "~/project" }),
127 Option.some("/Users/alice/project"),
128 );
129 }),
130 );
131});