Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions apps/desktop/src/app/DesktopApp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, describe, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";

import * as ElectronApp from "../electron/ElectronApp.ts";
import { configureElectronStoragePaths } from "./DesktopApp.ts";

describe("configureElectronStoragePaths", () => {
it.effect("creates a fresh split-layout cache directory before configuring Electron", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const root = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-desktop-electron-storage-",
});
const electronCachePath = `${root}/cache/electron`;
const configuredPaths: Array<readonly [string, string]> = [];
const electronAppLayer = Layer.succeed(
ElectronApp.ElectronApp,
ElectronApp.ElectronApp.of({
setPath: (name, path) =>
name === "cache"
? fileSystem.exists(path).pipe(
Effect.map((exists) => {
assert.isTrue(exists);
configuredPaths.push([name, path]);
}),
)
: Effect.sync(() => {
configuredPaths.push([name, path]);
}),
} as ElectronApp.ElectronApp["Service"]),
);

yield* configureElectronStoragePaths({
userDataPath: `${root}/state/electron`,
storageLayout: "split",
electronCachePath,
}).pipe(Effect.provide(electronAppLayer));

assert.deepEqual(configuredPaths, [
["userData", `${root}/state/electron`],
["cache", electronCachePath],
]);
}).pipe(Effect.provide(NodeServices.layer)),
);
});
24 changes: 23 additions & 1 deletion apps/desktop/src/app/DesktopApp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Option from "effect/Option";
import * as Ref from "effect/Ref";
import * as Schema from "effect/Schema";
Expand Down Expand Up @@ -63,6 +64,23 @@ const { logInfo: logBootstrapInfo, logWarning: logBootstrapWarning } =
const { logInfo: logStartupInfo, logError: logStartupError } =
DesktopObservability.makeComponentLogger("desktop-startup");

export const configureElectronStoragePaths = Effect.fn(
"desktop.startup.configureElectronStoragePaths",
)(function* (input: {
readonly userDataPath: string;
readonly storageLayout: DesktopEnvironment.DesktopEnvironment["Service"]["storageLayout"];
readonly electronCachePath: string;
}) {
const electronApp = yield* ElectronApp.ElectronApp;
const fileSystem = yield* FileSystem.FileSystem;

yield* electronApp.setPath("userData", input.userDataPath);
if (input.storageLayout === "split") {
yield* fileSystem.makeDirectory(input.electronCachePath, { recursive: true });
yield* electronApp.setPath("cache", input.electronCachePath);
}
});

const resolveDesktopBackendPort = Effect.fn("resolveDesktopBackendPort")(function* (
configuredPort: Option.Option<number>,
) {
Expand Down Expand Up @@ -228,7 +246,11 @@ const startup = Effect.gen(function* () {

yield* shellEnvironment.installIntoProcess;
const userDataPath = yield* appIdentity.resolveUserDataPath;
yield* electronApp.setPath("userData", userDataPath);
yield* configureElectronStoragePaths({
userDataPath,
storageLayout: environment.storageLayout,
electronCachePath: environment.electronCachePath,
});
yield* logStartupInfo("runtime logging configured", { logDir: environment.logDir });
yield* desktopSettings.load;

Expand Down
10 changes: 8 additions & 2 deletions apps/desktop/src/app/DesktopAppIdentity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ describe("DesktopAppIdentity", () => {

assert.equal(userDataPath, "/Users/alice/Library/Application Support/T3 Code (Alpha)");
}),
{ legacyPathExists: true },
{
legacyPathExists: true,
environment: { env: { T3CODE_HOME: "/Users/alice/.t3" } },
},
),
);

Expand All @@ -176,7 +179,10 @@ describe("DesktopAppIdentity", () => {
`Failed to inspect legacy desktop user-data path at "${legacyPath}".`,
);
}),
{ legacyPathProbeError: cause },
{
legacyPathProbeError: cause,
environment: { env: { T3CODE_HOME: "/Users/alice/.t3" } },
},
);
});

Expand Down
3 changes: 3 additions & 0 deletions apps/desktop/src/app/DesktopAppIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ export const make = Effect.gen(function* () {
});

const resolveUserDataPath = Effect.gen(function* () {
if (environment.storageLayout === "split") {
return environment.electronUserDataPath;
}
const legacyPath = environment.path.join(
environment.appDataDirectory,
environment.legacyUserDataDirName,
Expand Down
10 changes: 10 additions & 0 deletions apps/desktop/src/app/DesktopConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ const compactEnv = (env: Readonly<Record<string, string | undefined>>): Record<s

export const DesktopConfig = Config.all({
appDataDirectory: trimmedString("APPDATA"),
localAppDataDirectory: trimmedString("LOCALAPPDATA"),
xdgConfigHome: trimmedString("XDG_CONFIG_HOME"),
xdgDataHome: trimmedString("XDG_DATA_HOME"),
xdgStateHome: trimmedString("XDG_STATE_HOME"),
xdgCacheHome: trimmedString("XDG_CACHE_HOME"),
xdgRuntimeDir: trimmedString("XDG_RUNTIME_DIR"),
t3Home: trimmedString("T3CODE_HOME"),
t3ConfigDir: trimmedString("T3CODE_CONFIG_DIR"),
t3DataDir: trimmedString("T3CODE_DATA_DIR"),
t3StateDir: trimmedString("T3CODE_STATE_DIR"),
t3CacheDir: trimmedString("T3CODE_CACHE_DIR"),
t3RuntimeDir: trimmedString("T3CODE_RUNTIME_DIR"),
devServerUrl: Config.url("VITE_DEV_SERVER_URL").pipe(Config.option),
appUserModelIdOverride: trimmedString("T3CODE_DESKTOP_APP_USER_MODEL_ID"),
devRemoteT3ServerEntryPath: trimmedString("T3CODE_DEV_REMOTE_T3_SERVER_ENTRY_PATH"),
Expand Down
178 changes: 176 additions & 2 deletions apps/desktop/src/app/DesktopEnvironment.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, describe, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";

import * as DesktopEnvironment from "./DesktopEnvironment.ts";
import * as DesktopConfig from "./DesktopConfig.ts";
Expand Down Expand Up @@ -106,11 +108,183 @@ describe("DesktopEnvironment", () => {
);
const production = yield* makeEnvironment();

assert.equal(development.stateDir, "/Users/alice/.t3/dev");
assert.equal(production.stateDir, "/Users/alice/.t3/userdata");
assert.equal(
development.stateDir,
"/Users/alice/Library/Application Support/t3code-dev/state",
);
assert.equal(production.stateDir, "/Users/alice/Library/Application Support/t3code/state");
assert.equal(production.configDir, "/Users/alice/Library/Application Support/t3code/config");
assert.equal(production.dataDir, "/Users/alice/Library/Application Support/t3code/data");
assert.equal(production.cacheDir, "/Users/alice/Library/Caches/t3code");
assert.equal(production.runtimeDir, "/tmp/t3code");
}),
);

it.effect("honors all five XDG roots on Linux", () =>
Effect.gen(function* () {
const environment = yield* makeEnvironment(
{
platform: "linux",
homeDirectory: "/home/alice",
temporaryDirectory: "/tmp",
userId: 1000,
},
{
XDG_CONFIG_HOME: "/xdg/config",
XDG_DATA_HOME: "/xdg/data",
XDG_STATE_HOME: "/xdg/state",
XDG_CACHE_HOME: "/xdg/cache",
XDG_RUNTIME_DIR: "/run/user/1000",
},
);

assert.equal(environment.storageLayout, "split");
assert.equal(environment.configDir, "/xdg/config/t3code");
assert.equal(environment.dataDir, "/xdg/data/t3code");
assert.equal(environment.stateDir, "/xdg/state/t3code");
assert.equal(environment.cacheDir, "/xdg/cache/t3code");
assert.equal(environment.runtimeDir, "/run/user/1000/t3code");
assert.equal(environment.serverSettingsPath, "/xdg/config/t3code/settings.json");
assert.equal(environment.browserArtifactsDir, "/xdg/cache/t3code/browser-artifacts");
assert.equal(environment.electronUserDataPath, "/xdg/state/t3code/electron");
}),
);

it.effect("rejects mixing T3CODE_HOME with granular directory overrides", () =>
Effect.gen(function* () {
const error = yield* makeEnvironment(
{ platform: "linux", homeDirectory: "/home/alice" },
{
T3CODE_HOME: "/tmp/legacy-t3",
T3CODE_STATE_DIR: "/tmp/t3-state",
},
).pipe(Effect.flip);

assert.instanceOf(
error,
DesktopEnvironment.DesktopStorageDirectoryConfigurationConflictError,
);
}),
);

it.effect("keeps an initialized legacy installation without copying it", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-desktop-environment-legacy-",
});
const homeDirectory = path.join(root, "home");
const legacyStateDir = path.join(homeDirectory, ".t3", "userdata");
const splitStateHome = path.join(root, "state");
yield* fileSystem.makeDirectory(legacyStateDir, { recursive: true });
yield* fileSystem.makeDirectory(path.join(splitStateHome, "t3code"), {
recursive: true,
});
yield* fileSystem.writeFileString(path.join(legacyStateDir, "state.sqlite"), "legacy");
yield* fileSystem.writeFileString(path.join(splitStateHome, "t3code", "state.sqlite"), "");

const environment = yield* makeEnvironment(
{
platform: "linux",
homeDirectory,
temporaryDirectory: root,
userId: 1000,
},
{ XDG_STATE_HOME: splitStateHome },
);

assert.equal(environment.storageLayout, "legacy");
assert.equal(environment.stateDir, legacyStateDir);
assert.equal(environment.serverSettingsPath, path.join(legacyStateDir, "settings.json"));
}).pipe(Effect.provide(NodeServices.layer)),
);

it.effect("keeps legacy storage when only legacy secrets are initialized", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-desktop-environment-legacy-secrets-",
});
const homeDirectory = path.join(root, "home");
const legacyStateDir = path.join(homeDirectory, ".t3", "userdata");
yield* fileSystem.makeDirectory(path.join(legacyStateDir, "secrets"), {
recursive: true,
});

const environment = yield* makeEnvironment({
platform: "linux",
homeDirectory,
temporaryDirectory: root,
userId: 1000,
});

assert.equal(environment.storageLayout, "legacy");
assert.equal(environment.stateDir, legacyStateDir);
assert.equal(environment.serverSettingsPath, path.join(legacyStateDir, "settings.json"));
}).pipe(Effect.provide(NodeServices.layer)),
);

it.effect.each([
{ artifactName: "desktop settings", fileName: "desktop-settings.json" },
{ artifactName: "client settings", fileName: "client-settings.json" },
{ artifactName: "saved environments", fileName: "saved-environments.json" },
])("keeps legacy storage when only legacy $artifactName are initialized", ({ fileName }) =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-desktop-environment-legacy-settings-",
});
const homeDirectory = path.join(root, "home");
const legacyStateDir = path.join(homeDirectory, ".t3", "userdata");
yield* fileSystem.makeDirectory(legacyStateDir, { recursive: true });
yield* fileSystem.writeFileString(path.join(legacyStateDir, fileName), "{}");

const environment = yield* makeEnvironment({
platform: "linux",
homeDirectory,
temporaryDirectory: root,
userId: 1000,
});

assert.equal(environment.storageLayout, "legacy");
assert.equal(environment.configDir, legacyStateDir);
assert.equal(environment.stateDir, legacyStateDir);
}).pipe(Effect.provide(NodeServices.layer)),
);

it.effect("does not treat Electron preferences as initialized server storage", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-desktop-environment-electron-only-",
});
const homeDirectory = path.join(root, "home");
const electronDirectory = path.join(
homeDirectory,
"Library",
"Application Support",
"T3 Code (Alpha)",
);
yield* fileSystem.makeDirectory(electronDirectory, { recursive: true });
yield* fileSystem.writeFileString(path.join(electronDirectory, "Local State"), "{}");

const environment = yield* makeEnvironment({
homeDirectory,
temporaryDirectory: root,
});

assert.equal(environment.storageLayout, "split");
assert.equal(
environment.stateDir,
path.join(homeDirectory, "Library", "Application Support", "t3code", "state"),
);
}).pipe(Effect.provide(NodeServices.layer)),
);

it.effect("uses a configured app user model id override", () =>
Effect.gen(function* () {
const environment = yield* makeEnvironment(
Expand Down
Loading
Loading