|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import * as Effect from "effect/Effect"; |
| 3 | +import * as FileSystem from "effect/FileSystem"; |
| 4 | +import * as HttpClient from "effect/http/HttpClient"; |
| 5 | +import * as HttpClientError from "effect/http/HttpClientError"; |
| 6 | +import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 7 | +import { readGrokAccount } from "./usageLimits.ts"; |
1 | 8 | import { describe, expect, it } from "@effect/vitest"; |
2 | 9 |
|
3 | 10 | import { GROK_COST_USD_TICKS_PER_DOLLAR, parseGrokLine } from "./usage.ts"; |
@@ -314,3 +321,114 @@ describe("parseGrokLine", () => { |
314 | 321 | expect(records[0]?.timestampMs).toBe(1_786_372_566_000); |
315 | 322 | }); |
316 | 323 | }); |
| 324 | + |
| 325 | +it.layer(NodeServices.layer)("readGrokAccount probe failures", (it) => { |
| 326 | + it.effect( |
| 327 | + "reports a missing login as unsupported and malformed credentials as a sanitized failure", |
| 328 | + () => |
| 329 | + Effect.gen(function* () { |
| 330 | + const fs = yield* FileSystem.FileSystem; |
| 331 | + const directory = yield* fs.makeTempDirectoryScoped(); |
| 332 | + const client = HttpClient.make(() => Effect.die("must not request without credentials")); |
| 333 | + const { usageLimits: missing } = yield* readGrokAccount({ HOME: directory }).pipe( |
| 334 | + Effect.provideService(HttpClient.HttpClient, client), |
| 335 | + ); |
| 336 | + expect(missing.unavailable?.reason).toBe("unsupported"); |
| 337 | + for (const contents of [ |
| 338 | + "private-token-invalid-json", |
| 339 | + '{"https://accounts.x.ai/sign-in":{"key":42}}', |
| 340 | + ]) { |
| 341 | + const { usageLimits: malformed } = yield* readGrokAccount({ |
| 342 | + GROK_HOME: directory, |
| 343 | + GROK_AUTH: contents, |
| 344 | + }).pipe(Effect.provideService(HttpClient.HttpClient, client)); |
| 345 | + expect(malformed.unavailable).toEqual({ |
| 346 | + reason: "probeFailed", |
| 347 | + message: "Grok could not read saved credentials.", |
| 348 | + }); |
| 349 | + } |
| 350 | + }).pipe(Effect.scoped), |
| 351 | + ); |
| 352 | + |
| 353 | + it.effect( |
| 354 | + "classifies billing failures without exposing response bodies, keeping the account", |
| 355 | + () => |
| 356 | + Effect.gen(function* () { |
| 357 | + for (const [response, message] of [ |
| 358 | + [ |
| 359 | + new Response("private response", { status: 401 }), |
| 360 | + "Grok sign-in was rejected. Reconnect Grok in provider settings.", |
| 361 | + ], |
| 362 | + [ |
| 363 | + new Response("private response", { status: 403 }), |
| 364 | + "Grok usage limits are unavailable for this account.", |
| 365 | + ], |
| 366 | + [ |
| 367 | + new Response("private response", { status: 429 }), |
| 368 | + "Grok usage-limit checks are rate limited. Try again soon.", |
| 369 | + ], |
| 370 | + [ |
| 371 | + new Response("private response", { status: 503 }), |
| 372 | + "Grok billing is temporarily unavailable.", |
| 373 | + ], |
| 374 | + [new Response("private response", { status: 418 }), "Grok billing returned HTTP 418."], |
| 375 | + [ |
| 376 | + new Response("{", { headers: { "content-type": "application/json" } }), |
| 377 | + "Grok returned an invalid usage-limits response.", |
| 378 | + ], |
| 379 | + [ |
| 380 | + Response.json({ config: { creditUsagePercent: "private-value" } }), |
| 381 | + "Grok returned an invalid usage-limits response.", |
| 382 | + ], |
| 383 | + ] as const) { |
| 384 | + const { email, usageLimits: limits } = yield* readGrokAccount({ |
| 385 | + HOME: "/definitely/not/a/grok-home", |
| 386 | + GROK_AUTH: |
| 387 | + '{"https://accounts.x.ai/sign-in":{"key":"private-token","email":"[email protected]"}}', |
| 388 | + }).pipe( |
| 389 | + Effect.provideService( |
| 390 | + HttpClient.HttpClient, |
| 391 | + HttpClient.make((request) => |
| 392 | + Effect.succeed(HttpClientResponse.fromWeb(request, response)), |
| 393 | + ), |
| 394 | + ), |
| 395 | + ); |
| 396 | + expect(email).toBe("[email protected]"); |
| 397 | + expect(limits.windows).toEqual([]); |
| 398 | + expect(limits.unavailable).toEqual({ |
| 399 | + reason: "probeFailed", |
| 400 | + message, |
| 401 | + }); |
| 402 | + } |
| 403 | + }), |
| 404 | + ); |
| 405 | + |
| 406 | + it.effect("reports a transport failure without exposing its cause", () => |
| 407 | + readGrokAccount({ |
| 408 | + HOME: "/definitely/not/a/grok-home", |
| 409 | + GROK_AUTH: '{"https://accounts.x.ai/sign-in":{"key":"private-token"}}', |
| 410 | + }).pipe( |
| 411 | + Effect.provideService( |
| 412 | + HttpClient.HttpClient, |
| 413 | + HttpClient.make((request) => |
| 414 | + Effect.fail( |
| 415 | + new HttpClientError.HttpClientError({ |
| 416 | + reason: new HttpClientError.TransportError({ |
| 417 | + request, |
| 418 | + cause: new Error("private transport cause"), |
| 419 | + }), |
| 420 | + }), |
| 421 | + ), |
| 422 | + ), |
| 423 | + ), |
| 424 | + Effect.tap(({ usageLimits }) => |
| 425 | + Effect.sync(() => |
| 426 | + expect(usageLimits.unavailable).toEqual({ |
| 427 | + reason: "probeFailed", |
| 428 | + message: "Grok could not connect to the billing service.", |
| 429 | + }), |
| 430 | + ), |
| 431 | + ), |
| 432 | + ), |
| 433 | + ); |
| 434 | +}); |
0 commit comments