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 { assert, describe, it } from "@effect/vitest"; |
| 3 | import * as Clock from "effect/Clock"; |
| 4 | import * as Deferred from "effect/Deferred"; |
| 5 | import * as Duration from "effect/Duration"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Fiber from "effect/Fiber"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Ref from "effect/Ref"; |
| 10 | import * as Result from "effect/Result"; |
| 11 | import * as HttpClient from "effect/http/HttpClient"; |
| 12 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 13 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 14 | import { PRIMARY_LOCAL_ENVIRONMENT_ID } from "@t3tools/contracts"; |
| 15 | import { |
| 16 | DESKTOP_BOOTSTRAP_TOKEN_WINDOW_MS, |
| 17 | currentDesktopBootstrapToken, |
| 18 | } from "@t3tools/shared/desktopBootstrapToken"; |
| 19 | import * as TestClock from "effect/testing/TestClock"; |
| 20 | |
| 21 | import * as DesktopBackendPool from "./DesktopBackendPool.ts"; |
| 22 | import * as DesktopLocalEnvironmentAuth from "./DesktopLocalEnvironmentAuth.ts"; |
| 23 | |
| 24 | const config = { |
| 25 | executablePath: "/electron", |
| 26 | entryPath: "/server/bin.mjs", |
| 27 | cwd: "/server", |
| 28 | env: {}, |
| 29 | bootstrap: { |
| 30 | mode: "desktop", |
| 31 | noBrowser: true, |
| 32 | port: 3773, |
| 33 | t3Home: "/tmp/t3", |
| 34 | host: "127.0.0.1", |
| 35 | desktopBootstrapToken: "desktop-bootstrap-token", |
| 36 | tailscaleServeEnabled: false, |
| 37 | tailscaleServePort: 443, |
| 38 | }, |
| 39 | httpBaseUrl: new URL("http://127.0.0.1:3773"), |
| 40 | captureOutput: true, |
| 41 | }; |
| 42 | |
| 43 | describe("DesktopLocalEnvironmentAuth", () => { |
| 44 | it.effect("exchanges the desktop bootstrap credential only once", () => |
| 45 | Effect.gen(function* () { |
| 46 | const requestCount = yield* Ref.make(0); |
| 47 | const waiting = yield* Deferred.make<void>(); |
| 48 | const ready = yield* Deferred.make<boolean>(); |
| 49 | const layerHttpClient = Layer.succeed( |
| 50 | HttpClient.HttpClient, |
| 51 | HttpClient.make((request) => |
| 52 | Ref.update(requestCount, (count) => count + 1).pipe( |
| 53 | Effect.as( |
| 54 | HttpClientResponse.fromWeb( |
| 55 | request, |
| 56 | new Response( |
| 57 | JSON.stringify({ |
| 58 | access_token: "desktop-bearer-token", |
| 59 | issued_token_type: "urn:ietf:params:oauth:token-type:access_token", |
| 60 | token_type: "Bearer", |
| 61 | expires_in: 3600, |
| 62 | scope: "orchestration:read", |
| 63 | }), |
| 64 | { status: 200, headers: { "content-type": "application/json" } }, |
| 65 | ), |
| 66 | ), |
| 67 | ), |
| 68 | ), |
| 69 | ), |
| 70 | ); |
| 71 | const layerPool = Layer.succeed(DesktopBackendPool.DesktopBackendPool, { |
| 72 | list: Effect.succeed([ |
| 73 | { |
| 74 | id: PRIMARY_LOCAL_ENVIRONMENT_ID, |
| 75 | label: Effect.succeed("Windows"), |
| 76 | currentConfig: Effect.succeedSome(config), |
| 77 | waitForReady: (timeout: Duration.Duration) => |
| 78 | Deferred.succeed(waiting, undefined).pipe( |
| 79 | Effect.andThen(Deferred.await(ready)), |
| 80 | Effect.timeout(timeout), |
| 81 | ), |
| 82 | }, |
| 83 | ]), |
| 84 | } as unknown as DesktopBackendPool.DesktopBackendPool["Service"]); |
| 85 | const layerTest = DesktopLocalEnvironmentAuth.layer.pipe( |
| 86 | Layer.provide(Layer.mergeAll(layerPool, layerHttpClient, NodeCrypto.layerMac)), |
| 87 | ); |
| 88 | |
| 89 | const [first, second] = yield* Effect.gen(function* () { |
| 90 | const auth = yield* DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth; |
| 91 | const tokens = yield* Effect.all([auth.getBearerToken, auth.getBearerToken]).pipe( |
| 92 | Effect.forkChild, |
| 93 | ); |
| 94 | yield* Deferred.await(waiting); |
| 95 | yield* TestClock.adjust(Duration.seconds(16)); |
| 96 | assert.strictEqual(yield* Ref.get(requestCount), 0); |
| 97 | yield* Deferred.succeed(ready, true); |
| 98 | return yield* Fiber.join(tokens); |
| 99 | }).pipe(Effect.provide(layerTest)); |
| 100 | |
| 101 | assert.strictEqual(first, "desktop-bearer-token"); |
| 102 | assert.strictEqual(second, "desktop-bearer-token"); |
| 103 | assert.strictEqual(yield* Ref.get(requestCount), 1); |
| 104 | }).pipe(Effect.provide(TestClock.layer())), |
| 105 | ); |
| 106 | |
| 107 | it.effect( |
| 108 | "exchanges the current window's token when the backend was launched with a secret", |
| 109 | () => |
| 110 | Effect.gen(function* () { |
| 111 | const presented = yield* Ref.make<string | null>(null); |
| 112 | const httpClientLayer = Layer.succeed( |
| 113 | HttpClient.HttpClient, |
| 114 | HttpClient.make((request) => { |
| 115 | const body = |
| 116 | request.body._tag === "Uint8Array" ? new TextDecoder().decode(request.body.body) : ""; |
| 117 | return Ref.set(presented, new URLSearchParams(body).get("subject_token")).pipe( |
| 118 | Effect.as( |
| 119 | HttpClientResponse.fromWeb( |
| 120 | request, |
| 121 | new Response( |
| 122 | JSON.stringify({ |
| 123 | access_token: "desktop-bearer-token", |
| 124 | issued_token_type: "urn:ietf:params:oauth:token-type:access_token", |
| 125 | token_type: "Bearer", |
| 126 | expires_in: 3600, |
| 127 | scope: "orchestration:read", |
| 128 | }), |
| 129 | { status: 200, headers: { "content-type": "application/json" } }, |
| 130 | ), |
| 131 | ), |
| 132 | ), |
| 133 | ); |
| 134 | }), |
| 135 | ); |
| 136 | const poolLayer = Layer.succeed(DesktopBackendPool.DesktopBackendPool, { |
| 137 | list: Effect.succeed([ |
| 138 | { |
| 139 | id: PRIMARY_LOCAL_ENVIRONMENT_ID, |
| 140 | label: Effect.succeed("Windows"), |
| 141 | waitForReady: () => Effect.succeed(true), |
| 142 | currentConfig: Effect.succeedSome({ |
| 143 | ...config, |
| 144 | bootstrap: { ...config.bootstrap, desktopBootstrapSecret: "desktop-secret" }, |
| 145 | }), |
| 146 | }, |
| 147 | ]), |
| 148 | } as unknown as DesktopBackendPool.DesktopBackendPool["Service"]); |
| 149 | |
| 150 | yield* TestClock.setTime(DESKTOP_BOOTSTRAP_TOKEN_WINDOW_MS * 2 + 1); |
| 151 | yield* Effect.gen(function* () { |
| 152 | const auth = yield* DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth; |
| 153 | return yield* auth.getBearerToken; |
| 154 | }).pipe( |
| 155 | Effect.provide( |
| 156 | DesktopLocalEnvironmentAuth.layer.pipe( |
| 157 | Layer.provide(Layer.mergeAll(poolLayer, httpClientLayer, NodeCrypto.layerMac)), |
| 158 | ), |
| 159 | ), |
| 160 | ); |
| 161 | |
| 162 | assert.strictEqual( |
| 163 | yield* Ref.get(presented), |
| 164 | yield* currentDesktopBootstrapToken( |
| 165 | "desktop-secret", |
| 166 | DESKTOP_BOOTSTRAP_TOKEN_WINDOW_MS * 2 + 1, |
| 167 | ).pipe(Effect.provide(NodeCrypto.layerMac)), |
| 168 | ); |
| 169 | }).pipe(Effect.provide(TestClock.layer())), |
| 170 | ); |
| 171 | |
| 172 | const tokenResponse = (request: HttpClientRequest.HttpClientRequest) => |
| 173 | HttpClientResponse.fromWeb( |
| 174 | request, |
| 175 | new Response( |
| 176 | JSON.stringify({ |
| 177 | access_token: "desktop-bearer-token", |
| 178 | issued_token_type: "urn:ietf:params:oauth:token-type:access_token", |
| 179 | token_type: "Bearer", |
| 180 | expires_in: 3600, |
| 181 | scope: "orchestration:read", |
| 182 | }), |
| 183 | { status: 200, headers: { "content-type": "application/json" } }, |
| 184 | ), |
| 185 | ); |
| 186 | const layerPool = Layer.succeed(DesktopBackendPool.DesktopBackendPool, { |
| 187 | list: Effect.succeed([ |
| 188 | { |
| 189 | id: PRIMARY_LOCAL_ENVIRONMENT_ID, |
| 190 | label: Effect.succeed("Windows"), |
| 191 | currentConfig: Effect.succeedSome(config), |
| 192 | waitForReady: () => Effect.succeed(true), |
| 193 | }, |
| 194 | ]), |
| 195 | } as unknown as DesktopBackendPool.DesktopBackendPool["Service"]); |
| 196 | const makeExchange = (failures: number, failure: () => Response) => |
| 197 | Effect.gen(function* () { |
| 198 | const requestCount = yield* Ref.make(0); |
| 199 | const layerHttpClient = Layer.succeed( |
| 200 | HttpClient.HttpClient, |
| 201 | HttpClient.make((request) => |
| 202 | Ref.updateAndGet(requestCount, (count) => count + 1).pipe( |
| 203 | Effect.map((count) => |
| 204 | count <= failures |
| 205 | ? HttpClientResponse.fromWeb(request, failure()) |
| 206 | : tokenResponse(request), |
| 207 | ), |
| 208 | ), |
| 209 | ), |
| 210 | ); |
| 211 | const auth = yield* DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth.pipe( |
| 212 | Effect.provide( |
| 213 | DesktopLocalEnvironmentAuth.layer.pipe( |
| 214 | Layer.provide(Layer.mergeAll(layerPool, layerHttpClient, NodeCrypto.layerMac)), |
| 215 | ), |
| 216 | ), |
| 217 | ); |
| 218 | return { auth, requestCount }; |
| 219 | }); |
| 220 | |
| 221 | it.effect("does not retry a rejected bootstrap credential", () => |
| 222 | Effect.gen(function* () { |
| 223 | const { auth, requestCount } = yield* makeExchange(1, () => |
| 224 | Response.json( |
| 225 | { |
| 226 | _tag: "EnvironmentAuthInvalidError", |
| 227 | code: "auth_invalid", |
| 228 | reason: "invalid_credential", |
| 229 | traceId: "trace-1", |
| 230 | }, |
| 231 | { status: 401 }, |
| 232 | ), |
| 233 | ); |
| 234 | |
| 235 | const fiber = yield* auth.getBearerToken.pipe(Effect.flip, Effect.forkChild); |
| 236 | yield* TestClock.adjust(Duration.seconds(1)); |
| 237 | const error = yield* Fiber.join(fiber); |
| 238 | |
| 239 | assert.strictEqual(error._tag, "DesktopLocalEnvironmentAuthSessionBootstrapError"); |
| 240 | assert.strictEqual(yield* Ref.get(requestCount), 1); |
| 241 | }).pipe(Effect.provide(TestClock.layer())), |
| 242 | ); |
| 243 | |
| 244 | it.effect("waits through a backend that stalls for 30 seconds after it is ready", () => |
| 245 | Effect.gen(function* () { |
| 246 | const requestCount = yield* Ref.make(0); |
| 247 | const layerHttpClient = Layer.succeed( |
| 248 | HttpClient.HttpClient, |
| 249 | HttpClient.make((request) => |
| 250 | Effect.gen(function* () { |
| 251 | yield* Ref.update(requestCount, (count) => count + 1); |
| 252 | const now = yield* Clock.currentTimeMillis; |
| 253 | return now < 30_000 ? yield* Effect.never : tokenResponse(request); |
| 254 | }), |
| 255 | ), |
| 256 | ); |
| 257 | const auth = yield* DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth.pipe( |
| 258 | Effect.provide( |
| 259 | DesktopLocalEnvironmentAuth.layer.pipe( |
| 260 | Layer.provide(Layer.mergeAll(layerPool, layerHttpClient, NodeCrypto.layerMac)), |
| 261 | ), |
| 262 | ), |
| 263 | ); |
| 264 | |
| 265 | const fiber = yield* auth.getBearerToken.pipe(Effect.result, Effect.forkChild); |
| 266 | yield* TestClock.adjust(Duration.seconds(45)); |
| 267 | const result = yield* Fiber.join(fiber); |
| 268 | |
| 269 | assert.isTrue(Result.isSuccess(result)); |
| 270 | assert.strictEqual(yield* Ref.get(requestCount), 4); |
| 271 | }).pipe(Effect.provide(TestClock.layer())), |
| 272 | ); |
| 273 | }); |