Skip to content
Merged
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
37 changes: 29 additions & 8 deletions apps/desktop/src/app/DesktopAppIdentity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as DesktopAppIdentity from "./DesktopAppIdentity.ts";
import * as DesktopAssets from "./DesktopAssets.ts";
import * as DesktopConfig from "./DesktopConfig.ts";
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
import * as DesktopUserData from "./DesktopUserData.ts";

const defaultEnvironmentInput = {
dirname: "/repo/apps/desktop/dist-electron",
Expand Down Expand Up @@ -125,13 +126,14 @@ const withIdentity = <A, E, R>(
return effect.pipe(
Effect.provide(
DesktopAppIdentity.layer.pipe(
Layer.provide(NodePath.layerPosix),
Layer.provideMerge(
FileSystem.layerNoop({
exists: (path) =>
input.legacyPathProbeError
? Effect.fail(input.legacyPathProbeError)
: Effect.succeed(
input.legacyPathExists === true && path.includes("T3 Code (Alpha)"),
input.legacyPathExists === true && /T3 Code \((Alpha|Dev)\)/.test(path),
),
readFileString: () =>
Effect.succeed(input.packageJson ?? '{"t3codeCommitHash":"abcdef1234567890"}'),
Expand All @@ -146,20 +148,36 @@ const withIdentity = <A, E, R>(
};

describe("DesktopAppIdentity", () => {
it.effect("keeps using the legacy userData path when it already exists", () =>
it.effect("isolates the V2 profile even when the legacy V1 profile exists", () =>
withIdentity(
Effect.gen(function* () {
const identity = yield* DesktopAppIdentity.DesktopAppIdentity;
const userDataPath = yield* identity.resolveUserDataPath;

assert.equal(userDataPath, "/Users/alice/Library/Application Support/T3 Code (Alpha)");
assert.equal(userDataPath, "/Users/alice/Library/Application Support/t3code-v2");
}),
{ legacyPathExists: true },
),
);

it.effect("keeps using the legacy development profile", () =>
withIdentity(
Effect.gen(function* () {
const identity = yield* DesktopAppIdentity.DesktopAppIdentity;
assert.equal(
yield* identity.resolveUserDataPath,
"/Users/alice/Library/Application Support/T3 Code (Dev)",
);
}),
{
legacyPathExists: true,
environment: { env: { VITE_DEV_SERVER_URL: "http://localhost:5173" } },
},
),
);

it.effect("preserves failures while inspecting the legacy userData path", () => {
const legacyPath = "/Users/alice/Library/Application Support/T3 Code (Alpha)";
const legacyPath = "/Users/alice/Library/Application Support/T3 Code (Dev)";
const cause = PlatformError.systemError({
_tag: "PermissionDenied",
module: "FileSystem",
Expand All @@ -173,15 +191,18 @@ describe("DesktopAppIdentity", () => {
const identity = yield* DesktopAppIdentity.DesktopAppIdentity;
const error = yield* identity.resolveUserDataPath.pipe(Effect.flip);

assert.instanceOf(error, DesktopAppIdentity.DesktopUserDataPathResolutionError);
assert.equal(error.legacyPath, legacyPath);
assert.instanceOf(error, DesktopUserData.DesktopUserDataInitializationError);
assert.equal(error.resourcePath, legacyPath);
assert.strictEqual(error.cause, cause);
assert.equal(
error.message,
`Failed to inspect legacy desktop user-data path at "${legacyPath}".`,
`Could not initialize Electron user data during inspect at ${legacyPath} (PermissionDenied).`,
);
}),
{ legacyPathProbeError: cause },
{
legacyPathProbeError: cause,
environment: { env: { VITE_DEV_SERVER_URL: "http://localhost:5173" } },
},
);
});

Expand Down
47 changes: 9 additions & 38 deletions apps/desktop/src/app/DesktopAppIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ 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 Ref from "effect/Ref";
import * as Schema from "effect/Schema";

import * as ElectronApp from "../electron/ElectronApp.ts";
import * as DesktopAssets from "./DesktopAssets.ts";
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
import * as DesktopUserData from "./DesktopUserData.ts";

const COMMIT_HASH_PATTERN = /^[0-9a-f]{7,40}$/i;
const COMMIT_HASH_DISPLAY_LENGTH = 12;
Expand All @@ -18,22 +20,13 @@ const AppPackageMetadata = Schema.Struct({
});
const decodeAppPackageMetadata = Schema.decodeEffect(Schema.fromJsonString(AppPackageMetadata));

export class DesktopUserDataPathResolutionError extends Schema.TaggedError<DesktopUserDataPathResolutionError>()(
"DesktopUserDataPathResolutionError",
{
legacyPath: Schema.String,
cause: Schema.Defect(),
},
) {
override get message(): string {
return `Failed to inspect legacy desktop user-data path at "${this.legacyPath}".`;
}
}

export class DesktopAppIdentity extends Context.Service<
DesktopAppIdentity,
{
readonly resolveUserDataPath: Effect.Effect<string, DesktopUserDataPathResolutionError>;
readonly resolveUserDataPath: Effect.Effect<
string,
DesktopUserData.DesktopUserDataInitializationError
>;
readonly configure: Effect.Effect<void>;
}
>()("@t3tools/desktop/app/DesktopAppIdentity") {}
Expand All @@ -45,33 +38,13 @@ const normalizeCommitHash = (value: string): Option.Option<string> => {
: Option.none();
};

export const resolveUserDataPath = Effect.gen(function* () {
const environment = yield* DesktopEnvironment.DesktopEnvironment;
const fileSystem = yield* FileSystem.FileSystem;
const legacyPath = environment.path.join(
environment.appDataDirectory,
environment.legacyUserDataDirName,
);
const legacyPathExists = yield* fileSystem.exists(legacyPath).pipe(
Effect.mapError(
(cause) =>
new DesktopUserDataPathResolutionError({
legacyPath,
cause,
}),
),
);
return legacyPathExists
? legacyPath
: environment.path.join(environment.appDataDirectory, environment.userDataDirName);
}).pipe(Effect.withSpan("desktop.appIdentity.resolveUserDataPath"));

/** @public Service construction is part of the canonical Effect module API. */
export const make = Effect.gen(function* () {
const assets = yield* DesktopAssets.DesktopAssets;
const electronApp = yield* ElectronApp.ElectronApp;
const environment = yield* DesktopEnvironment.DesktopEnvironment;
const fileSystem = yield* FileSystem.FileSystem;
const userDataContext = yield* Effect.context<FileSystem.FileSystem | Path.Path>();
const commitHashCache = yield* Ref.make<Option.Option<Option.Option<string>>>(Option.none());

const resolveEmbeddedCommitHash = Effect.gen(function* () {
Expand Down Expand Up @@ -112,10 +85,8 @@ export const make = Effect.gen(function* () {
return commitHash;
});

const userDataPath = resolveUserDataPath.pipe(
Effect.provide(
yield* Effect.context<DesktopEnvironment.DesktopEnvironment | FileSystem.FileSystem>(),
),
const userDataPath = DesktopUserData.resolveUserDataPath(environment).pipe(
Effect.provide(userDataContext),
);

const configure = Effect.gen(function* () {
Expand Down
5 changes: 2 additions & 3 deletions apps/desktop/src/app/DesktopClerk.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as NodePath from "@effect/platform-node/NodePath";
import { assert, describe, it } from "@effect/vitest";
import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";
Expand Down Expand Up @@ -34,9 +35,6 @@ const makeDesktopClerkLayer = (isDevelopment = true, events: string[] = []) => {
stateDir: "/tmp/t3-state",
isDevelopment,
appDataDirectory: "/tmp/app-data",
userDataDirName: isDevelopment ? "t3code-dev" : "t3code",
legacyUserDataDirName: isDevelopment ? "T3 Code (Dev)" : "T3 Code (Alpha)",
path: { join: (...parts: ReadonlyArray<string>) => parts.join("/") },
} as unknown as DesktopEnvironment.DesktopEnvironment["Service"]);

const electronApp = {
Expand All @@ -49,6 +47,7 @@ const makeDesktopClerkLayer = (isDevelopment = true, events: string[] = []) => {
return DesktopClerk.layer.pipe(
Layer.provide(
Layer.mergeAll(
NodePath.layerPosix,
Layer.succeed(DesktopEnvironment.DesktopEnvironment, environment),
Layer.succeed(ElectronApp.ElectronApp, electronApp),
FileSystem.layerNoop({ exists: () => Effect.succeed(false) }),
Expand Down
11 changes: 3 additions & 8 deletions apps/desktop/src/app/DesktopClerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { clerkFrontendApiHostnameFromPublishableKey } from "@t3tools/shared/rela
import * as ElectronApp from "../electron/ElectronApp.ts";
import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
import * as ElectronWindow from "../electron/ElectronWindow.ts";
import * as DesktopAppIdentity from "./DesktopAppIdentity.ts";
import * as DesktopUserData from "./DesktopUserData.ts";
import * as DesktopEnvironment from "./DesktopEnvironment.ts";

declare const __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: string | undefined;
Expand Down Expand Up @@ -88,13 +88,8 @@ export const make = Effect.gen(function* () {
const environment = yield* DesktopEnvironment.DesktopEnvironment;
const electronApp = yield* ElectronApp.ElectronApp;

// Electron scopes the single-instance lock to the userData directory and
// creates that directory when the lock is acquired. The SDK bridge takes
// the lock at creation, so userData must already point at the real
// directory here — under the default productName-derived path, acquiring
// the lock would create "T3 Code (Alpha)" and make the legacy-install
// detection in resolveUserDataPath match on fresh installs.
const userDataPath = yield* DesktopAppIdentity.resolveUserDataPath;
// The SDK bridge acquires Electron's profile-scoped single-instance lock.
const userDataPath = yield* DesktopUserData.resolveUserDataPath(environment);
yield* electronApp.setPath("userData", userDataPath);

const bridge = yield* Effect.acquireRelease(
Expand Down
2 changes: 0 additions & 2 deletions apps/desktop/src/app/DesktopEnvironment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ describe("DesktopEnvironment", () => {
assert.equal(environment.logDir, "/tmp/t3/userdata/logs");
assert.equal(environment.browserArtifactsDir, "/tmp/t3/userdata/browser-artifacts");
assert.equal(environment.serverSettingsPath, "/tmp/t3/userdata/settings.json");
assert.equal(environment.userDataDirName, "t3code");
assert.equal(environment.legacyUserDataDirName, "T3 Code (Alpha)");
assert.equal(environment.otlpProtocol, "http/json");
}),
);
Expand Down
6 changes: 0 additions & 6 deletions apps/desktop/src/app/DesktopEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ export class DesktopEnvironment extends Context.Service<
readonly linuxWmClass: string;
readonly linuxApplicationsDir: string;
readonly appImagePath: Option.Option<string>;
readonly userDataDirName: string;
readonly legacyUserDataDirName: string;
readonly defaultDesktopSettings: DesktopAppSettings.DesktopSettings;
readonly runtimeInfo: DesktopRuntimeInfo;
readonly resolvePickFolderDefaultPath: (rawOptions: unknown) => Option.Option<string>;
Expand Down Expand Up @@ -184,8 +182,6 @@ const make = Effect.fn("desktop.environment.make")(function* (
joinPath: path.join,
t3Home: config.t3Home,
});
const userDataDirName = isDevelopment ? "t3code-dev" : "t3code";
const legacyUserDataDirName = isDevelopment ? "T3 Code (Dev)" : "T3 Code (Alpha)";
const linuxApplicationsDir = path.join(
Option.getOrElse(config.xdgDataHome, () => path.join(homeDirectory, ".local", "share")),
"applications",
Expand Down Expand Up @@ -239,8 +235,6 @@ const make = Effect.fn("desktop.environment.make")(function* (
linuxWmClass: isDevelopment ? "t3code-dev" : "t3code",
linuxApplicationsDir,
appImagePath: config.appImagePath,
userDataDirName,
legacyUserDataDirName,
defaultDesktopSettings: DesktopAppSettings.resolveDefaultDesktopSettings(input.appVersion),
runtimeInfo: resolveDesktopRuntimeInfo({
platform: input.platform,
Expand Down
75 changes: 75 additions & 0 deletions apps/desktop/src/app/DesktopUserData.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as PlatformError from "effect/PlatformError";

import { resolveUserDataPath } from "./DesktopUserData.ts";

it.effect("identifies a failed source read and preserves its cause", () => {
const sourceState = "/profiles/t3code/Local State";
const cause = PlatformError.systemError({
_tag: "PermissionDenied",
module: "FileSystem",
method: "readFileString",
pathOrDescriptor: sourceState,
});
return Effect.gen(function* () {
const error = yield* resolveUserDataPath({
appDataDirectory: "/profiles",
isDevelopment: false,
platform: "win32",
}).pipe(Effect.flip);
assert.equal(error.operation, "read");
assert.equal(error.resourcePath, sourceState);
assert.equal(error.category, "PermissionDenied");
assert.strictEqual(error.cause, cause);
}).pipe(
Effect.provideService(
FileSystem.FileSystem,
FileSystem.makeNoop({
exists: (path) => Effect.succeed(path === sourceState),
readFileString: () => Effect.fail(cause),
}),
),
Effect.provide(NodeServices.layer),
);
});

for (const sourceName of ["t3code", "T3 Code (Alpha)"]) {
it.effect(
`preserves Windows credential keys from ${sourceName} without copying browser databases`,
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-v2-profile-" });
const source = path.join(directory, sourceName);
const destination = path.join(directory, "t3code-v2");
const state = '{"os_crypt":{"encrypted_key":"test-encrypted-key"}}';
yield* fs.makeDirectory(path.join(directory, "T3 Code (Alpha)"), { recursive: true });
yield* fs.makeDirectory(path.join(source, "IndexedDB"), { recursive: true });
yield* fs.writeFileString(path.join(source, "Local State"), state);
yield* fs.writeFileString(path.join(source, "IndexedDB", "LOCK"), "V1 owns this database");
yield* resolveUserDataPath({
appDataDirectory: directory,
isDevelopment: false,
platform: "win32",
});
assert.equal(yield* fs.readFileString(path.join(destination, "Local State")), state);
assert.equal(yield* fs.readFileString(path.join(source, "Local State")), state);
assert.isFalse(yield* fs.exists(path.join(destination, "IndexedDB")));
yield* fs.writeFileString(path.join(destination, "Local State"), "existing V2 state");
yield* resolveUserDataPath({
appDataDirectory: directory,
isDevelopment: false,
platform: "win32",
});
assert.equal(
yield* fs.readFileString(path.join(destination, "Local State")),
"existing V2 state",
);
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
}
Loading
Loading