The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import { assert, describe, it } from "@effect/vitest"; |
| 4 | import { |
| 5 | ClientSettingsSchema, |
| 6 | DEFAULT_CLIENT_SETTINGS, |
| 7 | type ClientSettings, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as FileSystem from "effect/FileSystem"; |
| 11 | import * as Layer from "effect/Layer"; |
| 12 | import * as Option from "effect/Option"; |
| 13 | import * as PlatformError from "effect/PlatformError"; |
| 14 | import * as Schema from "effect/Schema"; |
| 15 | |
| 16 | import * as DesktopConfig from "../app/DesktopConfig.ts"; |
| 17 | import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; |
| 18 | import * as DesktopClientSettings from "./DesktopClientSettings.ts"; |
| 19 | |
| 20 | const clientSettings: ClientSettings = { |
| 21 | ...DEFAULT_CLIENT_SETTINGS, |
| 22 | notificationMode: "notifications-and-sound", |
| 23 | inAppNotificationsEnabled: true, |
| 24 | appearanceContrast: 100, |
| 25 | browserDefaultViewport: { _tag: "preset", width: 1024, height: 600, presetId: "nest-hub" }, |
| 26 | browserDefaultZoomFactor: 1.25, |
| 27 | browserDefaultAppearance: "dark", |
| 28 | browserRecordingFrameRate: 60, |
| 29 | browserLinkTarget: "app", |
| 30 | sideThreadOpenTarget: "main", |
| 31 | browserAutoShowFloatingPreview: false, |
| 32 | browserProfiles: [{ id: "work", name: "Work", kind: "persistent" }], |
| 33 | browserDefaultProfileId: "work", |
| 34 | confirmQuit: "double-click", |
| 35 | confirmThreadArchive: true, |
| 36 | confirmThreadDelete: false, |
| 37 | confirmThreadUnpin: false, |
| 38 | diffFilesCollapsed: true, |
| 39 | diffIgnoreWhitespace: true, |
| 40 | diffLayout: "stacked", |
| 41 | environmentIdentificationMode: "artwork", |
| 42 | favorites: [], |
| 43 | fontFamilyCode: "", |
| 44 | fontFamilyComposer: "", |
| 45 | fontFamilySans: "", |
| 46 | fontFamilyTerminal: "", |
| 47 | fontSizeCode: 13, |
| 48 | fontSizeInterface: 16, |
| 49 | fontSizePrompt: 14, |
| 50 | fontSizeTerminal: 12, |
| 51 | fontSmoothing: true, |
| 52 | glassOpacity: 80, |
| 53 | onboardingCompletedAt: null, |
| 54 | panelAnimationDurationMs: 0, |
| 55 | proactivePanelsEnabled: true, |
| 56 | showSkillsInSlashMenu: false, |
| 57 | persistComposerContextStrip: true, |
| 58 | providerModelPreferences: {}, |
| 59 | sidebarProjectGroupingMode: "repository_path", |
| 60 | sidebarProjectGroupingOverrides: { |
| 61 | "environment-1:/tmp/project-a": "separate", |
| 62 | }, |
| 63 | sidebarProjectSortOrder: "manual", |
| 64 | sidebarThreadSortOrder: "created_at", |
| 65 | boringSidebarEnabled: false, |
| 66 | boringSidebarMultilineRows: false, |
| 67 | threadDetailsUsageEnabled: false, |
| 68 | threadDetailsItems: {}, |
| 69 | sidebarWorkingShelfEnabled: false, |
| 70 | loadBalancingEnabled: false, |
| 71 | loadBalancingWeights: { "environment-1": 75, "environment-2": 0 }, |
| 72 | pullRequestMergeMethodOverrides: {}, |
| 73 | timestampFormat: "24-hour", |
| 74 | wordWrap: true, |
| 75 | }; |
| 76 | |
| 77 | const decodeClientSettingsJson = Schema.decodeEffect(Schema.fromJsonString(ClientSettingsSchema)); |
| 78 | function layer(baseDir: string) { |
| 79 | const layerEnvironment = DesktopEnvironment.layer({ |
| 80 | dirname: "/repo/apps/desktop/src", |
| 81 | homeDirectory: baseDir, |
| 82 | platform: "darwin", |
| 83 | processArch: "x64", |
| 84 | appVersion: "1.2.3", |
| 85 | appPath: "/repo", |
| 86 | isPackaged: true, |
| 87 | resourcesPath: "/missing/resources", |
| 88 | runningUnderArm64Translation: false, |
| 89 | }).pipe( |
| 90 | Layer.provide( |
| 91 | Layer.mergeAll( |
| 92 | NodeServices.layer, |
| 93 | NodeCrypto.layer, |
| 94 | DesktopConfig.layerTest({ T3CODE_HOME: baseDir }), |
| 95 | ), |
| 96 | ), |
| 97 | ); |
| 98 | |
| 99 | return DesktopClientSettings.layer.pipe( |
| 100 | Layer.provideMerge(layerEnvironment), |
| 101 | Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | const withClientSettings = <A, E, R>( |
| 106 | effect: Effect.Effect<A, E, R | DesktopClientSettings.DesktopClientSettings>, |
| 107 | ) => |
| 108 | Effect.gen(function* () { |
| 109 | const fileSystem = yield* FileSystem.FileSystem; |
| 110 | const baseDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 111 | prefix: "t3-desktop-client-settings-test-", |
| 112 | }); |
| 113 | return yield* effect.pipe(Effect.provide(layer(baseDir))); |
| 114 | }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))); |
| 115 | |
| 116 | describe("DesktopClientSettings", () => { |
| 117 | it.effect("saves through a symlinked client settings file without replacing the link", () => |
| 118 | withClientSettings( |
| 119 | Effect.gen(function* () { |
| 120 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 121 | const fileSystem = yield* FileSystem.FileSystem; |
| 122 | const settings = yield* DesktopClientSettings.DesktopClientSettings; |
| 123 | const dotfiles = yield* fileSystem.makeTempDirectoryScoped({ |
| 124 | prefix: "t3-desktop-client-settings-dotfiles-", |
| 125 | }); |
| 126 | const linkedSettingsPath = `${dotfiles}/client-settings.json`; |
| 127 | yield* fileSystem.writeFileString(linkedSettingsPath, "{}\n"); |
| 128 | yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true }); |
| 129 | yield* fileSystem.symlink(linkedSettingsPath, environment.clientSettingsPath); |
| 130 | |
| 131 | yield* settings.set(clientSettings); |
| 132 | |
| 133 | assert.equal( |
| 134 | yield* fileSystem.readLink(environment.clientSettingsPath), |
| 135 | linkedSettingsPath, |
| 136 | ); |
| 137 | assert.deepEqual( |
| 138 | yield* decodeClientSettingsJson(yield* fileSystem.readFileString(linkedSettingsPath)), |
| 139 | clientSettings, |
| 140 | ); |
| 141 | }), |
| 142 | ), |
| 143 | ); |
| 144 | |
| 145 | it.effect("removes its temporary file when replacing the settings file fails", () => |
| 146 | withClientSettings( |
| 147 | Effect.gen(function* () { |
| 148 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 149 | const fileSystem = yield* FileSystem.FileSystem; |
| 150 | const settings = yield* DesktopClientSettings.DesktopClientSettings; |
| 151 | yield* fileSystem.makeDirectory(`${environment.clientSettingsPath}/occupied`, { |
| 152 | recursive: true, |
| 153 | }); |
| 154 | |
| 155 | const error = yield* settings.set(clientSettings).pipe(Effect.flip); |
| 156 | |
| 157 | assert.equal(error.operation, "replace-settings-file"); |
| 158 | assert.deepEqual( |
| 159 | (yield* fileSystem.readDirectory(environment.stateDir)).filter((name) => |
| 160 | name.endsWith(".tmp"), |
| 161 | ), |
| 162 | [], |
| 163 | ); |
| 164 | }), |
| 165 | ), |
| 166 | ); |
| 167 | |
| 168 | it.effect.each([ |
| 169 | { label: "permission", reason: "PermissionDenied" }, |
| 170 | { label: "I/O", reason: "Unknown" }, |
| 171 | ] as const)("preserves saved preferences across $label read failures and retries", (failure) => |
| 172 | withClientSettings( |
| 173 | Effect.gen(function* () { |
| 174 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 175 | const fileSystem = yield* FileSystem.FileSystem; |
| 176 | const settings = yield* DesktopClientSettings.DesktopClientSettings; |
| 177 | const savedSettings = { |
| 178 | ...clientSettings, |
| 179 | onboardingCompletedAt: "2026-09-05T12:00:00.000Z", |
| 180 | }; |
| 181 | yield* settings.set(savedSettings); |
| 182 | const savedContents = yield* fileSystem.readFileString(environment.clientSettingsPath); |
| 183 | const cause = PlatformError.systemError({ |
| 184 | _tag: failure.reason, |
| 185 | module: "FileSystem", |
| 186 | method: "readFileString", |
| 187 | pathOrDescriptor: environment.clientSettingsPath, |
| 188 | }); |
| 189 | let failRead = true; |
| 190 | const retryableSettings = yield* DesktopClientSettings.make.pipe( |
| 191 | Effect.provideService( |
| 192 | FileSystem.FileSystem, |
| 193 | FileSystem.FileSystem.of({ |
| 194 | ...fileSystem, |
| 195 | readFileString: (path) => |
| 196 | Effect.suspend(() => |
| 197 | failRead ? Effect.fail(cause) : fileSystem.readFileString(path), |
| 198 | ), |
| 199 | }), |
| 200 | ), |
| 201 | ); |
| 202 | |
| 203 | const error = yield* retryableSettings.get.pipe(Effect.flip); |
| 204 | assert.instanceOf(error, DesktopClientSettings.DesktopClientSettingsReadError); |
| 205 | assert.equal(error.operation, "read-file"); |
| 206 | assert.equal(error.path, environment.clientSettingsPath); |
| 207 | assert.strictEqual(error.cause, cause); |
| 208 | assert.equal( |
| 209 | yield* fileSystem.readFileString(environment.clientSettingsPath), |
| 210 | savedContents, |
| 211 | ); |
| 212 | |
| 213 | failRead = false; |
| 214 | assert.deepEqual(yield* retryableSettings.get, Option.some(savedSettings)); |
| 215 | }), |
| 216 | ), |
| 217 | ); |
| 218 | |
| 219 | it.effect("loads lenient direct client settings documents", () => |
| 220 | withClientSettings( |
| 221 | Effect.gen(function* () { |
| 222 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 223 | const fileSystem = yield* FileSystem.FileSystem; |
| 224 | const settings = yield* DesktopClientSettings.DesktopClientSettings; |
| 225 | yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true }); |
| 226 | yield* fileSystem.writeFileString( |
| 227 | environment.clientSettingsPath, |
| 228 | `{ |
| 229 | // Matches server settings parsing. |
| 230 | "timestampFormat": "24-hour", |
| 231 | }\n`, |
| 232 | ); |
| 233 | |
| 234 | const persisted = yield* settings.get; |
| 235 | assert.isTrue(Option.isSome(persisted)); |
| 236 | if (Option.isSome(persisted)) { |
| 237 | assert.equal(persisted.value.timestampFormat, "24-hour"); |
| 238 | } |
| 239 | }), |
| 240 | ), |
| 241 | ); |
| 242 | |
| 243 | it.effect("loads legacy wrapped client settings documents", () => |
| 244 | withClientSettings( |
| 245 | Effect.gen(function* () { |
| 246 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 247 | const fileSystem = yield* FileSystem.FileSystem; |
| 248 | const settings = yield* DesktopClientSettings.DesktopClientSettings; |
| 249 | yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true }); |
| 250 | yield* fileSystem.writeFileString( |
| 251 | environment.clientSettingsPath, |
| 252 | `{ |
| 253 | "settings": { |
| 254 | "timestampFormat": "12-hour" |
| 255 | } |
| 256 | }\n`, |
| 257 | ); |
| 258 | |
| 259 | const persisted = yield* settings.get; |
| 260 | assert.isTrue(Option.isSome(persisted)); |
| 261 | if (Option.isSome(persisted)) { |
| 262 | assert.equal(persisted.value.timestampFormat, "12-hour"); |
| 263 | } |
| 264 | }), |
| 265 | ), |
| 266 | ); |
| 267 | |
| 268 | it.effect.each([ |
| 269 | { label: "malformed JSON", contents: "{not-json" }, |
| 270 | { label: "invalid legacy settings", contents: '{"settings":{"fontSizeCode":"large"}}' }, |
| 271 | ])("reports $label without treating the settings file as absent", (document) => |
| 272 | withClientSettings( |
| 273 | Effect.gen(function* () { |
| 274 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 275 | const fileSystem = yield* FileSystem.FileSystem; |
| 276 | const settings = yield* DesktopClientSettings.DesktopClientSettings; |
| 277 | yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true }); |
| 278 | yield* fileSystem.writeFileString(environment.clientSettingsPath, document.contents); |
| 279 | |
| 280 | const error = yield* settings.get.pipe(Effect.flip); |
| 281 | assert.instanceOf(error, DesktopClientSettings.DesktopClientSettingsReadError); |
| 282 | assert.equal(error.operation, "decode-document"); |
| 283 | assert.equal(error.path, environment.clientSettingsPath); |
| 284 | assert.instanceOf(error.cause, Schema.SchemaError); |
| 285 | assert.equal( |
| 286 | yield* fileSystem.readFileString(environment.clientSettingsPath), |
| 287 | document.contents, |
| 288 | ); |
| 289 | }), |
| 290 | ), |
| 291 | ); |
| 292 | }); |