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

DesktopAssets.test.ts

97 lines3.7 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as PlatformError from "effect/PlatformError";
8​
9import * as DesktopAssets from "./DesktopAssets.ts";
10import * as DesktopConfig from "./DesktopConfig.ts";
11import * as DesktopEnvironment from "./DesktopEnvironment.ts";
12​
13const environmentLayer = DesktopEnvironment.layer({
14 dirname: "/repo/apps/desktop/dist-electron",
15 homeDirectory: "/Users/alice",
16 platform: "darwin",
17 processArch: "arm64",
18 appVersion: "1.2.3",
19 appPath: "/Applications/T3 Code.app/Contents/Resources/app.asar",
20 isPackaged: true,
21 resourcesPath: "/Applications/T3 Code.app/Contents/Resources",
22 runningUnderArm64Translation: false,
23}).pipe(Layer.provide(Layer.mergeAll(NodeServices.layer, DesktopConfig.layerTest({}))));
24​
25describe("DesktopAssets", () => {
26 it.effect("uses canonical source-tree icons for unpackaged development", () =>
27 Effect.gen(function* () {
28 const developmentEnvironmentLayer = DesktopEnvironment.layer({
29 dirname: "/repo/apps/desktop/dist-electron",
30 homeDirectory: "/Users/alice",
31 platform: "linux",
32 processArch: "x64",
33 appVersion: "1.2.3",
34 appPath: "/repo",
35 isPackaged: false,
36 resourcesPath: "/repo/apps/desktop/resources",
37 runningUnderArm64Translation: false,
38 }).pipe(
39 Layer.provide(
40 Layer.mergeAll(
41 NodeServices.layer,
42 DesktopConfig.layerTest({ VITE_DEV_SERVER_URL: "http://localhost:5733" }),
43 ),
44 ),
45 );
46 const fileSystemLayer = FileSystem.layerNoop({
47 exists: (path) => Effect.succeed(String(path).includes("/assets/dev/")),
48 });
49 const assets = yield* DesktopAssets.DesktopAssets.pipe(
50 Effect.provide(
51 DesktopAssets.layer.pipe(
52 Layer.provide(Layer.merge(fileSystemLayer, developmentEnvironmentLayer)),
53 ),
54 ),
55 );
56​
57 const icons = yield* assets.iconPaths;
58​
59 assert.match(Option.getOrThrow(icons.ico), /assets\/dev\/blueprint-windows\.ico$/);
60 assert.match(Option.getOrThrow(icons.png), /assets\/dev\/blueprint-universal-1024\.png$/);
61 assert.isTrue(Option.isNone(icons.icns));
62 }),
63 );
64​
65 it.effect("preserves the failed asset candidate and filesystem cause", () =>
66 Effect.gen(function* () {
67 const fileName = "custom.bin";
68 const candidatePath = "/repo/apps/desktop/resources/custom.bin";
69 const cause = PlatformError.systemError({
70 _tag: "PermissionDenied",
71 module: "FileSystem",
72 method: "exists",
73 pathOrDescriptor: candidatePath,
74 description: "private filesystem diagnostic",
75 });
76 const fileSystemLayer = FileSystem.layerNoop({
77 exists: (path) => (path === candidatePath ? Effect.fail(cause) : Effect.succeed(false)),
78 });
79 const assetsLayer = DesktopAssets.layer.pipe(
80 Layer.provide(Layer.merge(fileSystemLayer, environmentLayer)),
81 );
82 const assets = yield* DesktopAssets.DesktopAssets.pipe(Effect.provide(assetsLayer));
83​
84 const error = yield* assets.resolveResourcePath(fileName).pipe(Effect.flip);
85​
86 assert.instanceOf(error, DesktopAssets.DesktopAssetProbeError);
87 assert.equal(error.fileName, fileName);
88 assert.equal(error.candidatePath, candidatePath);
89 assert.strictEqual(error.cause, cause);
90 assert.equal(
91 error.message,
92 `Failed to probe desktop asset "${fileName}" at ${candidatePath}.`,
93 );
94 assert.notInclude(error.message, "private filesystem diagnostic");
95 }),
96 );
97});