|
1 | 1 | import { describe, expect, it } from "@effect/vitest"; |
2 | 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
3 | 3 | import type { SimlockDeviceHostConfig } from "@t3tools/contracts"; |
| 4 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
4 | 5 | import * as Effect from "effect/Effect"; |
5 | 6 | import * as Fiber from "effect/Fiber"; |
6 | 7 | import * as Deferred from "effect/Deferred"; |
@@ -62,14 +63,16 @@ const fakePool = ( |
62 | 63 | const commands: Array<{ readonly args: ReadonlyArray<string>; readonly cwd?: string }> = []; |
63 | 64 | let leases = 0; |
64 | 65 | let renewStatus = 200; |
| 66 | + let catalog: unknown = CATALOG; |
65 | 67 | const http = HttpClient.make((request) => |
66 | 68 | Effect.sync(() => { |
67 | 69 | const url = new URL(request.url); |
68 | 70 | requests.push(`${request.method} ${url.pathname}`); |
69 | | - expect(request.headers.authorization).toBe("Bearer agent-token"); |
70 | 71 | const json = (body: unknown, status = 200) => |
71 | 72 | HttpClientResponse.fromWeb(request, Response.json(body, { status })); |
72 | | - if (url.pathname === "/v1/catalog") return json(CATALOG); |
| 73 | + if (request.headers.authorization !== "Bearer agent-token") |
| 74 | + return json({ error: { code: "UNAUTHENTICATED", message: "Unknown bearer token" } }, 401); |
| 75 | + if (url.pathname === "/v1/catalog") return json(catalog); |
73 | 76 | if (url.pathname.endsWith("/renew")) return json({}, renewStatus); |
74 | 77 | if (request.method === "DELETE") return json({}, 202); |
75 | 78 | return json({}, 404); |
@@ -134,6 +137,9 @@ const fakePool = ( |
134 | 137 | endLeases: () => { |
135 | 138 | renewStatus = 404; |
136 | 139 | }, |
| 140 | + setCatalog: (next: unknown) => { |
| 141 | + catalog = next; |
| 142 | + }, |
137 | 143 | }; |
138 | 144 | }); |
139 | 145 |
|
@@ -257,6 +263,84 @@ describe("Simlock device host", () => { |
257 | 263 | }).pipe(Effect.scoped, Effect.provide(layer)), |
258 | 264 | ); |
259 | 265 |
|
| 266 | + it.effect("reports its platforms unavailable when the gateway refuses the agent token", () => |
| 267 | + Effect.gen(function* () { |
| 268 | + const pool = yield* fakePool(); |
| 269 | + const scope = yield* Scope.make(); |
| 270 | + const host = yield* makeHost(pool, config({ agentToken: "revoked" }), scope); |
| 271 | + expect(yield* host.platformAvailability("ios")).toEqual({ |
| 272 | + platform: "ios", |
| 273 | + available: false, |
| 274 | + reason: "The Simlock gateway refused the agent token for Device pool.", |
| 275 | + }); |
| 276 | + expect((yield* host.summary).platforms.every((platform) => !platform.available)).toBe(true); |
| 277 | + yield* Scope.close(scope, Exit.void); |
| 278 | + }).pipe(Effect.scoped, Effect.provide(layer)), |
| 279 | + ); |
| 280 | + |
| 281 | + it.effect("sends saved and machine tokens only to the gateway each belongs to", () => |
| 282 | + Effect.gen(function* () { |
| 283 | + const pool = yield* fakePool(); |
| 284 | + const availability = (baseUrl: string) => |
| 285 | + Effect.gen(function* () { |
| 286 | + const scope = yield* Scope.make(); |
| 287 | + const host = yield* makeHost(pool, config({ agentToken: "" }), scope).pipe( |
| 288 | + Effect.provideService(HostProcess.Environment, { |
| 289 | + AGENT_DEVICE_DAEMON_BASE_URL: baseUrl, |
| 290 | + AGENT_DEVICE_DAEMON_AUTH_TOKEN: "agent-token", |
| 291 | + }), |
| 292 | + ); |
| 293 | + const result = yield* host.platformAvailability("ios"); |
| 294 | + yield* Scope.close(scope, Exit.void); |
| 295 | + return result; |
| 296 | + }); |
| 297 | + |
| 298 | + expect( |
| 299 | + SimlockDeviceHost.resolveSimlockToken( |
| 300 | + "https://elsewhere.example:4700", |
| 301 | + config({ agentToken: "saved-token" }), |
| 302 | + {}, |
| 303 | + ), |
| 304 | + ).toBeNull(); |
| 305 | + |
| 306 | + expect(yield* availability(`${GATEWAY}/v1/agent-device`)).toEqual({ |
| 307 | + platform: "ios", |
| 308 | + available: true, |
| 309 | + }); |
| 310 | + const requests = pool.requests.length; |
| 311 | + expect(yield* availability("https://elsewhere.example:4700/v1/agent-device")).toEqual({ |
| 312 | + platform: "ios", |
| 313 | + available: false, |
| 314 | + reason: "Add an agent token for Device pool in Settings.", |
| 315 | + }); |
| 316 | + expect(pool.requests).toHaveLength(requests); |
| 317 | + }).pipe(Effect.scoped, Effect.provide(layer)), |
| 318 | + ); |
| 319 | + |
| 320 | + it.effect("lists a platform again as soon as its workers return", () => |
| 321 | + Effect.gen(function* () { |
| 322 | + const pool = yield* fakePool(); |
| 323 | + const scope = yield* Scope.make(); |
| 324 | + const host = yield* makeHost(pool, config(), scope); |
| 325 | + const ready = yield* host.ensureReady(() => Effect.void); |
| 326 | + pool.setCatalog({ platforms: [] }); |
| 327 | + expect((yield* ready.devices.list).devices).toEqual([]); |
| 328 | + // Mid-reconnect, the gateway knows the Android models but no worker that makes them. |
| 329 | + pool.setCatalog({ |
| 330 | + platforms: [ |
| 331 | + CATALOG.platforms[0]!, |
| 332 | + { ...CATALOG.platforms[1]!, modelWorkers: { "Pixel 9": [], "Pixel 8": [] } }, |
| 333 | + ], |
| 334 | + }); |
| 335 | + const during = (yield* ready.devices.list).devices; |
| 336 | + expect(during.some((device) => device.platform === "android")).toBe(false); |
| 337 | + pool.setCatalog(CATALOG); |
| 338 | + const after = (yield* ready.devices.list).devices; |
| 339 | + expect(after.some((device) => device.platform === "android")).toBe(true); |
| 340 | + yield* Scope.close(scope, Exit.void); |
| 341 | + }).pipe(Effect.scoped, Effect.provide(layer)), |
| 342 | + ); |
| 343 | + |
260 | 344 | it.effect("closes the session of a failed open so no lease is left behind", () => |
261 | 345 | Effect.gen(function* () { |
262 | 346 | const pool = yield* fakePool({ failOpen: true }); |
|
0 commit comments