The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import { expect, it } from "@effect/vitest"; |
| 4 | import * as Digest from "effect/crypto/Digest"; |
| 5 | import * as Hex from "effect/encoding/Hex"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as FileSystem from "effect/FileSystem"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Path from "effect/Path"; |
| 10 | import { ChildProcessSpawner } from "effect/process"; |
| 11 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 12 | |
| 13 | import * as ServerConfig from "../config.ts"; |
| 14 | import * as NativeAppIconResolver from "./NativeAppIconResolver.ts"; |
| 15 | |
| 16 | it.effect("resolves the exact bundle path without Spotlight and caches the converted icon", () => |
| 17 | Effect.gen(function* () { |
| 18 | const fs = yield* FileSystem.FileSystem; |
| 19 | const path = yield* Path.Path; |
| 20 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-bundle-icon-" }); |
| 21 | const bundle = path.join(root, "Build Output", "My App.app"); |
| 22 | const resources = path.join(bundle, "Contents", "Resources"); |
| 23 | yield* fs.makeDirectory(resources, { recursive: true }); |
| 24 | yield* fs.writeFileString(path.join(resources, "AppIcon.icns"), "icon"); |
| 25 | const commands: string[] = []; |
| 26 | const spawner = Layer.mock(ChildProcessSpawner.ChildProcessSpawner)({ |
| 27 | string: (command) => |
| 28 | Effect.gen(function* () { |
| 29 | if (command._tag !== "StandardCommand") return ""; |
| 30 | commands.push(command.command); |
| 31 | if (command.command === "/usr/bin/plutil") return "AppIcon"; |
| 32 | if (command.command === "/usr/bin/sips") { |
| 33 | expect(command.args.slice(0, 3)).toEqual(["-z", "128", "128"]); |
| 34 | yield* fs.writeFileString(String(command.args.at(-1)), "converted icon"); |
| 35 | } |
| 36 | return ""; |
| 37 | }), |
| 38 | }); |
| 39 | yield* Effect.gen(function* () { |
| 40 | const config = yield* ServerConfig.ServerConfig; |
| 41 | const canonicalBundle = yield* fs.realPath(bundle); |
| 42 | const sourceIcon = path.join(canonicalBundle, "Contents", "Resources", "AppIcon.icns"); |
| 43 | const digest = yield* Digest.Digest; |
| 44 | const legacyKey = yield* digest |
| 45 | .digest( |
| 46 | Digest.sha256, |
| 47 | new TextEncoder().encode(`${canonicalBundle}\0AppIcon\0${sourceIcon}`), |
| 48 | ) |
| 49 | .pipe(Effect.map(Hex.encode), Effect.orDie); |
| 50 | const cacheDirectory = path.join(config.providerStatusCacheDir, "native-app-icons"); |
| 51 | yield* fs.makeDirectory(cacheDirectory, { recursive: true }); |
| 52 | const legacyIcon = path.join(cacheDirectory, `${legacyKey}.png`); |
| 53 | yield* fs.writeFileString(legacyIcon, "legacy 64px icon"); |
| 54 | const resolver = yield* NativeAppIconResolver.make; |
| 55 | const reference = { _tag: "app-path" as const, path: bundle }; |
| 56 | const icon = yield* resolver.resolve(reference); |
| 57 | expect(icon).not.toBeNull(); |
| 58 | if (icon === null) return; |
| 59 | expect(yield* fs.readFileString(icon)).toBe("converted icon"); |
| 60 | expect(icon).not.toBe(legacyIcon); |
| 61 | expect(yield* resolver.resolve(reference)).toBe(icon); |
| 62 | expect(yield* resolver.resolve({ _tag: "app-path", path: "~/Build Output/My App.app" })).toBe( |
| 63 | icon, |
| 64 | ); |
| 65 | expect(commands.filter((command) => command === "/usr/bin/sips")).toHaveLength(1); |
| 66 | expect(commands).not.toContain("/usr/bin/mdfind"); |
| 67 | expect(yield* resolver.resolve({ _tag: "app-path", path: "relative.app" })).toBeNull(); |
| 68 | expect(yield* resolver.resolve({ _tag: "app-path", path: root })).toBeNull(); |
| 69 | }).pipe( |
| 70 | Effect.provide( |
| 71 | Layer.mergeAll( |
| 72 | spawner, |
| 73 | ServerConfig.ServerConfig.layerTest(root, { prefix: "t3-native-icon-config-" }), |
| 74 | ), |
| 75 | ), |
| 76 | Effect.provideService(HostProcess.Platform, "darwin"), |
| 77 | Effect.provideService(HostProcess.HomeDirectory, root), |
| 78 | ); |
| 79 | }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 80 | ); |