The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { assert, describe, it } from "@effect/vitest"; |
| 3 | import { |
| 4 | ProviderDriverKind, |
| 5 | ProviderInstanceId, |
| 6 | type ProviderInstallState, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Deferred from "effect/Deferred"; |
| 11 | import * as SubscriptionRef from "effect/SubscriptionRef"; |
| 12 | import * as FileSystem from "effect/FileSystem"; |
| 13 | import * as Layer from "effect/Layer"; |
| 14 | import * as Path from "effect/Path"; |
| 15 | import * as Queue from "effect/Queue"; |
| 16 | import * as Stream from "effect/Stream"; |
| 17 | |
| 18 | import * as ServerSettings from "../serverSettings.ts"; |
| 19 | import * as CodexInstallation from "./CodexInstallation.ts"; |
| 20 | import * as AntigravityInstallation from "./AntigravityInstallation.ts"; |
| 21 | import { ProviderDriverError } from "@t3tools/provider-core/server/errors"; |
| 22 | import type { ProviderInstance } from "@t3tools/provider-core/server/driver"; |
| 23 | import * as ProviderInstallation from "./providerInstallation.ts"; |
| 24 | import * as ProviderInstanceRegistry from "./ProviderInstanceRegistry.ts"; |
| 25 | import * as ProviderRegistry from "./ProviderRegistry.ts"; |
| 26 | |
| 27 | const instanceId = ProviderInstanceId.make("antigravity"); |
| 28 | const driver = ProviderDriverKind.make("antigravity"); |
| 29 | const state: ProviderInstallState = { |
| 30 | driver, |
| 31 | operationId: null, |
| 32 | phase: "idle", |
| 33 | downloadedBytes: 0, |
| 34 | totalBytes: null, |
| 35 | version: null, |
| 36 | installedVersion: null, |
| 37 | canRemove: false, |
| 38 | message: null, |
| 39 | }; |
| 40 | |
| 41 | function instance(kind = driver, id = instanceId): ProviderInstance { |
| 42 | return { |
| 43 | instanceId: id, |
| 44 | driverKind: kind, |
| 45 | enabled: false, |
| 46 | displayName: undefined, |
| 47 | continuationIdentity: { driverKind: kind, continuationKey: instanceId }, |
| 48 | get orchestrationAdapter(): never { |
| 49 | throw new Error("Installation must not start a provider session."); |
| 50 | }, |
| 51 | get snapshot(): never { |
| 52 | throw new Error("Installation routing must not probe the provider."); |
| 53 | }, |
| 54 | get textGeneration(): never { |
| 55 | throw new Error("Installation must not generate text."); |
| 56 | }, |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | const makeHarness = Effect.fn("providerInstallation.test.makeHarness")(function* ( |
| 61 | input: { |
| 62 | instance?: ProviderInstance; |
| 63 | others?: ReadonlyArray<ProviderInstance>; |
| 64 | settings?: Parameters<typeof ServerSettings.layerTest>[0]; |
| 65 | } = {}, |
| 66 | ) { |
| 67 | const calls: string[] = []; |
| 68 | const refreshed = yield* Deferred.make<void>(); |
| 69 | const lastRefreshes = yield* Queue.unbounded<void>(); |
| 70 | const observed = yield* Deferred.make<void>(); |
| 71 | const listening = yield* Deferred.make<void>(); |
| 72 | const installationState = yield* SubscriptionRef.make(state); |
| 73 | let protectedPaths: ReadonlyArray<string> = []; |
| 74 | const configured = input.instance ?? instance(); |
| 75 | const all = [configured, ...(input.others ?? [])]; |
| 76 | const refreshedInstances: ProviderInstanceId[] = []; |
| 77 | let watches = 0; |
| 78 | const dependencies = Layer.mergeAll( |
| 79 | ServerSettings.layerTest(input.settings), |
| 80 | Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({ |
| 81 | getInstance: (id) => Effect.succeed(all.find((entry) => entry.instanceId === id)), |
| 82 | listInstances: Effect.succeed(all), |
| 83 | }), |
| 84 | Layer.mock(ProviderRegistry.ProviderRegistry)({ |
| 85 | refreshInstance: (id) => |
| 86 | Effect.sync(() => { |
| 87 | calls.push("refresh"); |
| 88 | refreshedInstances.push(id); |
| 89 | return []; |
| 90 | }).pipe( |
| 91 | Effect.tap(() => Deferred.succeed(refreshed, undefined)), |
| 92 | Effect.tap(() => |
| 93 | id === all.at(-1)?.instanceId ? Queue.offer(lastRefreshes, undefined) : Effect.void, |
| 94 | ), |
| 95 | ), |
| 96 | }), |
| 97 | Layer.mock(CodexInstallation.CodexInstallation)({ |
| 98 | managedDirectory: "/unused-managed-codex", |
| 99 | start: Effect.sync(() => { |
| 100 | calls.push("codex-start"); |
| 101 | return { ...state, driver: ProviderDriverKind.make("codex") }; |
| 102 | }), |
| 103 | cancel: () => |
| 104 | Effect.sync(() => { |
| 105 | calls.push("codex-cancel"); |
| 106 | return { ...state, driver: ProviderDriverKind.make("codex") }; |
| 107 | }), |
| 108 | state: Effect.succeed({ ...state, driver: ProviderDriverKind.make("codex") }), |
| 109 | changes: Stream.succeed({ ...state, driver: ProviderDriverKind.make("codex") }), |
| 110 | checkForUpdate: Effect.void, |
| 111 | remove: (paths) => |
| 112 | Effect.sync(() => { |
| 113 | protectedPaths = paths ?? []; |
| 114 | calls.push("codex-remove"); |
| 115 | }), |
| 116 | }), |
| 117 | Layer.mock(AntigravityInstallation.AntigravityInstallation)({ |
| 118 | managedDirectory: "/unused-managed-runtime", |
| 119 | checkForUpdate: Effect.void, |
| 120 | start: Effect.sync(() => { |
| 121 | calls.push("start"); |
| 122 | return { ...state, operationId: "install-1", phase: "downloading" as const }; |
| 123 | }).pipe(Effect.tap((started) => SubscriptionRef.set(installationState, started))), |
| 124 | cancel: () => |
| 125 | Effect.sync(() => { |
| 126 | calls.push("cancel"); |
| 127 | return state; |
| 128 | }), |
| 129 | state: Effect.succeed(state), |
| 130 | changes: all.some((entry) => entry.refreshModels) |
| 131 | ? SubscriptionRef.changes(installationState).pipe( |
| 132 | Stream.onStart( |
| 133 | Effect.sync(() => { |
| 134 | watches++; |
| 135 | }), |
| 136 | ), |
| 137 | Stream.tap(() => Deferred.succeed(listening, undefined)), |
| 138 | Stream.ensuring(Deferred.succeed(observed, undefined)), |
| 139 | ) |
| 140 | : Stream.succeed(state), |
| 141 | remove: (paths) => |
| 142 | Effect.sync(() => { |
| 143 | protectedPaths = paths ?? []; |
| 144 | calls.push("remove"); |
| 145 | }), |
| 146 | }), |
| 147 | ); |
| 148 | const context = yield* Layer.build(ProviderInstallation.layer.pipe(Layer.provide(dependencies))); |
| 149 | const connect = Effect.service(ProviderInstallation.ProviderInstallation).pipe( |
| 150 | Effect.provide(context), |
| 151 | ); |
| 152 | const router = yield* connect; |
| 153 | return { |
| 154 | router, |
| 155 | connect, |
| 156 | listening, |
| 157 | calls, |
| 158 | refreshed, |
| 159 | lastRefreshes, |
| 160 | watches: () => watches, |
| 161 | observed, |
| 162 | installationState, |
| 163 | refreshedInstances, |
| 164 | protectedPaths: () => protectedPaths, |
| 165 | }; |
| 166 | }); |
| 167 | |
| 168 | describe("provider installation routing", () => { |
| 169 | it.effect.each([ |
| 170 | { phase: "succeeded", enabled: true, checkFails: false, expectedChecks: 1 }, |
| 171 | { phase: "succeeded", enabled: true, checkFails: true, expectedChecks: 1 }, |
| 172 | { phase: "succeeded", enabled: false, checkFails: false, expectedChecks: 0 }, |
| 173 | { phase: "cancelled", enabled: true, checkFails: false, expectedChecks: 0 }, |
| 174 | { phase: "failed", enabled: true, checkFails: false, expectedChecks: 0 }, |
| 175 | ] as const)( |
| 176 | "checks account access once after installation is $phase, enabled=$enabled, checkFails=$checkFails", |
| 177 | ({ phase, enabled, checkFails, expectedChecks }) => |
| 178 | Effect.gen(function* () { |
| 179 | let checks = 0; |
| 180 | const harness = yield* makeHarness({ |
| 181 | instance: Object.assign(instance(), { |
| 182 | enabled, |
| 183 | refreshModels: () => |
| 184 | Effect.sync(() => { |
| 185 | checks++; |
| 186 | }).pipe( |
| 187 | Effect.andThen( |
| 188 | checkFails |
| 189 | ? Effect.fail( |
| 190 | new ProviderDriverError({ |
| 191 | driver, |
| 192 | instanceId, |
| 193 | detail: "Sign in required.", |
| 194 | }), |
| 195 | ) |
| 196 | : Effect.void, |
| 197 | ), |
| 198 | ), |
| 199 | }), |
| 200 | }); |
| 201 | yield* Effect.gen(function* () { |
| 202 | const first = yield* harness.connect; |
| 203 | const second = yield* harness.connect; |
| 204 | yield* Effect.all([first.start({ instanceId }), second.start({ instanceId })], { |
| 205 | concurrency: "unbounded", |
| 206 | }); |
| 207 | yield* Deferred.await(harness.listening); |
| 208 | }).pipe(Effect.scoped); |
| 209 | assert.isFalse(yield* Deferred.isDone(harness.observed)); |
| 210 | assert.strictEqual(checks, 0); |
| 211 | yield* SubscriptionRef.set(harness.installationState, { |
| 212 | ...state, |
| 213 | operationId: "install-1", |
| 214 | phase, |
| 215 | installedVersion: phase === "succeeded" ? "1.5.1" : null, |
| 216 | }); |
| 217 | yield* Deferred.await(harness.observed); |
| 218 | if (expectedChecks > 0) yield* Deferred.await(harness.refreshed); |
| 219 | assert.strictEqual(checks, expectedChecks); |
| 220 | assert.deepEqual( |
| 221 | harness.calls, |
| 222 | expectedChecks > 0 ? ["start", "start", "refresh"] : ["start", "start"], |
| 223 | ); |
| 224 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 225 | ); |
| 226 | |
| 227 | it.effect( |
| 228 | "checks every enabled instance on the managed runtime once, whichever instance started the install", |
| 229 | () => |
| 230 | Effect.gen(function* () { |
| 231 | const checked: string[] = []; |
| 232 | const managed = (id: string, enabled: boolean) => |
| 233 | Object.assign(instance(driver, ProviderInstanceId.make(id)), { |
| 234 | enabled, |
| 235 | refreshModels: () => |
| 236 | Effect.sync(() => { |
| 237 | checked.push(id); |
| 238 | }), |
| 239 | }); |
| 240 | const work = managed("antigravity_work", true); |
| 241 | const personal = managed("antigravity_personal", true); |
| 242 | const harness = yield* makeHarness({ |
| 243 | // The disabled default instance is listed first, as server.getConfig lists it. |
| 244 | instance: managed("antigravity", false), |
| 245 | others: [managed("antigravity_custom", true), work, personal], |
| 246 | settings: { |
| 247 | providerInstances: { |
| 248 | [ProviderInstanceId.make("antigravity_custom")]: { |
| 249 | driver, |
| 250 | config: { binaryPath: "/external/agy" }, |
| 251 | }, |
| 252 | }, |
| 253 | }, |
| 254 | }); |
| 255 | yield* Effect.gen(function* () { |
| 256 | const first = yield* harness.connect; |
| 257 | const second = yield* harness.connect; |
| 258 | yield* Effect.all( |
| 259 | [first.start({ instanceId }), second.start({ instanceId: personal.instanceId })], |
| 260 | { concurrency: "unbounded" }, |
| 261 | ); |
| 262 | yield* Deferred.await(harness.listening); |
| 263 | }).pipe(Effect.scoped); |
| 264 | yield* SubscriptionRef.set(harness.installationState, { |
| 265 | ...state, |
| 266 | operationId: "install-1", |
| 267 | phase: "succeeded", |
| 268 | installedVersion: "1.5.8", |
| 269 | }); |
| 270 | yield* Deferred.await(harness.observed); |
| 271 | // Instances refresh in list order, so the last one's refresh ends a watch's pass. |
| 272 | yield* Queue.take(harness.lastRefreshes); |
| 273 | assert.deepEqual(checked, [work.instanceId, personal.instanceId]); |
| 274 | // Let every other watch of this install finish its pass; none may refresh again. |
| 275 | yield* Queue.takeN(harness.lastRefreshes, harness.watches() - 1); |
| 276 | assert.deepEqual(checked, [work.instanceId, personal.instanceId]); |
| 277 | assert.deepEqual(harness.refreshedInstances, [work.instanceId, personal.instanceId]); |
| 278 | assert.strictEqual(harness.watches(), 1); |
| 279 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 280 | ); |
| 281 | |
| 282 | it.effect("keeps external installs manual without hiding shared install status", () => |
| 283 | Effect.gen(function* () { |
| 284 | const harness = yield* makeHarness({ |
| 285 | settings: { |
| 286 | providerInstances: { |
| 287 | [ProviderInstanceId.make("antigravity")]: { |
| 288 | driver: ProviderDriverKind.make("antigravity"), |
| 289 | config: { binaryPath: "/external/agy" }, |
| 290 | }, |
| 291 | }, |
| 292 | }, |
| 293 | }); |
| 294 | const start = yield* Effect.flip(harness.router.start({ instanceId })); |
| 295 | const remove = yield* Effect.flip(harness.router.remove({ instanceId })); |
| 296 | assert.include(start.detail, "custom executable"); |
| 297 | assert.include(remove.detail, "custom executable"); |
| 298 | const observed = yield* Stream.runCollect(harness.router.subscribe({ instanceId })); |
| 299 | assert.deepEqual(Array.from(observed), [state]); |
| 300 | assert.deepEqual(harness.calls, []); |
| 301 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 302 | ); |
| 303 | |
| 304 | it.effect("protects another instance's binary found through its own PATH", () => |
| 305 | Effect.gen(function* () { |
| 306 | const fs = yield* FileSystem.FileSystem; |
| 307 | const path = yield* Path.Path; |
| 308 | const platform = yield* HostProcess.Platform; |
| 309 | const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-provider-install-route-" }); |
| 310 | const binary = platform === "win32" ? "agy-test.exe" : "agy-test"; |
| 311 | const executable = path.join(directory, binary); |
| 312 | yield* fs.writeFileString(executable, "test"); |
| 313 | yield* fs.chmod(executable, 0o755); |
| 314 | const other = ProviderInstanceId.make("antigravity-work"); |
| 315 | const harness = yield* makeHarness({ |
| 316 | settings: { |
| 317 | providerInstances: { |
| 318 | [other]: { |
| 319 | driver, |
| 320 | config: { binaryPath: binary }, |
| 321 | environment: [{ name: "PATH", value: directory, sensitive: false }], |
| 322 | }, |
| 323 | }, |
| 324 | }, |
| 325 | }); |
| 326 | yield* harness.router.remove({ instanceId }); |
| 327 | assert.include(harness.protectedPaths(), executable); |
| 328 | assert.deepEqual(harness.calls, ["remove", "refresh"]); |
| 329 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 330 | ); |
| 331 | }); |