| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { assert, it } from "@effect/vitest"; |
| 3 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Exit from "effect/Exit"; |
| 6 | import * as FileSystem from "effect/FileSystem"; |
| 7 | import * as Path from "effect/Path"; |
| 8 | |
| 9 | import * as DesktopPreReadyFileSystem from "./DesktopPreReadyFileSystem.ts"; |
| 10 | import * as DesktopUserData from "./DesktopUserData.ts"; |
| 11 | |
| 12 | const resolveWindowsUserData = (appDataDirectory: string) => |
| 13 | DesktopUserData.resolveUserDataPath({ |
| 14 | appDataDirectory, |
| 15 | isDevelopment: false, |
| 16 | platform: "win32", |
| 17 | }).pipe(Effect.provide(DesktopPreReadyFileSystem.layer)); |
| 18 | |
| 19 | it.layer(NodeServices.layer)("DesktopPreReadyFileSystem", (it) => { |
| 20 | it.effect("migrates the legacy Windows profile state", () => |
| 21 | Effect.gen(function* () { |
| 22 | const fileSystem = yield* FileSystem.FileSystem; |
| 23 | const path = yield* Path.Path; |
| 24 | const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-pre-ready-fs-" }); |
| 25 | yield* fileSystem.makeDirectory(path.join(root, "T3 Code (Alpha)")); |
| 26 | yield* fileSystem.writeFileString(path.join(root, "T3 Code (Alpha)", "Local State"), "keys"); |
| 27 | |
| 28 | const userData = yield* resolveWindowsUserData(root); |
| 29 | |
| 30 | assert.equal(userData, path.join(root, "t3code-v2")); |
| 31 | assert.equal(yield* fileSystem.readFileString(path.join(userData, "Local State")), "keys"); |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32" || process.getuid?.() === 0)( |
| 36 | "fails instead of treating an unreadable profile as missing", |
| 37 | () => |
| 38 | Effect.gen(function* () { |
| 39 | const fileSystem = yield* FileSystem.FileSystem; |
| 40 | const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-pre-ready-fs-" }); |
| 41 | yield* fileSystem.chmod(root, 0o000); |
| 42 | yield* Effect.addFinalizer(() => fileSystem.chmod(root, 0o700).pipe(Effect.orDie)); |
| 43 | |
| 44 | const exit = yield* Effect.exit(resolveWindowsUserData(root)); |
| 45 | |
| 46 | assert.isTrue(Exit.isFailure(exit)); |
| 47 | }), |
| 48 | ); |
| 49 | }); |