Skip to content

Commit e3b533d

Browse files
jieyuexingcodex
andcommitted
test(server): cover Grok usage-limit probe failures
Co-Authored-By: Codex (gpt-6-astra) <[email protected]>
1 parent 00a802b commit e3b533d

3 files changed

Lines changed: 127 additions & 71 deletions

File tree

‎apps/server/src/provider/GrokProvider.test.ts‎

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -699,33 +699,6 @@ it.layer(NodeServices.layer)("readGrokAccount", (it) => {
699699
}),
700700
);
701701

702-
it.effect(
703-
"reports a missing login as unsupported and malformed credentials as a sanitized failure",
704-
() =>
705-
Effect.gen(function* () {
706-
const fs = yield* FileSystem.FileSystem;
707-
const directory = yield* fs.makeTempDirectoryScoped();
708-
const client = HttpClient.make(() => Effect.die("must not request without credentials"));
709-
const { usageLimits: missing } = yield* readGrokAccount({ HOME: directory }).pipe(
710-
Effect.provideService(HttpClient.HttpClient, client),
711-
);
712-
expect(missing.unavailable?.reason).toBe("unsupported");
713-
for (const contents of [
714-
"private-token-invalid-json",
715-
'{"https://accounts.x.ai/sign-in":{"key":42}}',
716-
]) {
717-
const { usageLimits: malformed } = yield* readGrokAccount({
718-
GROK_HOME: directory,
719-
GROK_AUTH: contents,
720-
}).pipe(Effect.provideService(HttpClient.HttpClient, client));
721-
expect(malformed.unavailable).toEqual({
722-
reason: "probeFailed",
723-
message: "Grok could not read saved credentials.",
724-
});
725-
}
726-
}).pipe(Effect.scoped),
727-
);
728-
729702
it.effect(
730703
"does not request subscription limits for custom account or endpoint configuration",
731704
() =>
@@ -757,46 +730,6 @@ it.layer(NodeServices.layer)("readGrokAccount", (it) => {
757730
}
758731
}),
759732
);
760-
761-
it.effect("classifies billing failures without exposing response bodies, keeping the account", () =>
762-
Effect.gen(function* () {
763-
for (const [response, message] of [
764-
[
765-
new Response("private response", { status: 401 }),
766-
"Grok sign-in was rejected. Reconnect Grok in provider settings.",
767-
],
768-
[new Response("private response", { status: 403 }), "Grok usage limits are unavailable for this account."],
769-
[
770-
new Response("private response", { status: 429 }),
771-
"Grok usage-limit checks are rate limited. Try again soon.",
772-
],
773-
[new Response("private response", { status: 503 }), "Grok billing is temporarily unavailable."],
774-
[
775-
Response.json({ config: { creditUsagePercent: "private-value" } }),
776-
"Grok returned an invalid usage-limits response.",
777-
],
778-
] as const) {
779-
const { email, usageLimits: limits } = yield* readGrokAccount({
780-
HOME: "/definitely/not/a/grok-home",
781-
GROK_AUTH:
782-
'{"https://accounts.x.ai/sign-in":{"key":"private-token","email":"[email protected]"}}',
783-
}).pipe(
784-
Effect.provideService(
785-
HttpClient.HttpClient,
786-
HttpClient.make((request) =>
787-
Effect.succeed(HttpClientResponse.fromWeb(request, response)),
788-
),
789-
),
790-
);
791-
expect(email).toBe("[email protected]");
792-
expect(limits.windows).toEqual([]);
793-
expect(limits.unavailable).toEqual({
794-
reason: "probeFailed",
795-
message,
796-
});
797-
}
798-
}),
799-
);
800733
});
801734

802735
it.layer(NodeServices.layer)("readGrokAccount email", (it) => {

‎packages/provider-grok/src/server/usage.test.ts‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
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";
18
import { describe, expect, it } from "@effect/vitest";
29

310
import { GROK_COST_USD_TICKS_PER_DOLLAR, parseGrokLine } from "./usage.ts";
@@ -314,3 +321,114 @@ describe("parseGrokLine", () => {
314321
expect(records[0]?.timestampMs).toBe(1_786_372_566_000);
315322
});
316323
});
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+
});

‎packages/provider-grok/src/server/usageLimits.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as Option from "effect/Option";
66
import * as Path from "effect/Path";
77
import * as Schema from "effect/Schema";
88
import * as HttpClient from "effect/http/HttpClient";
9+
import * as HttpClientError from "effect/http/HttpClientError";
910
import * as HttpClientRequest from "effect/http/HttpClientRequest";
1011
import * as HttpClientResponse from "effect/http/HttpClientResponse";
1112
import {
@@ -44,11 +45,17 @@ function unavailableGrokUsageLimits(checkedAt: string, message: string) {
4445

4546
function grokUsageProbeFailureMessage(error: unknown): string {
4647
if (typeof error === "object" && error !== null && "_tag" in error) {
47-
if (error._tag === "TimeoutException") return "Grok usage-limit check timed out.";
48+
if (error._tag === "TimeoutError") return "Grok usage-limit check timed out.";
4849
if (error._tag === "ParseError" || error._tag === "SchemaError") {
4950
return "Grok returned an invalid usage-limits response.";
5051
}
5152
}
53+
if (
54+
HttpClientError.isHttpClientError(error) &&
55+
(error.reason._tag === "DecodeError" || error.reason._tag === "EmptyBodyError")
56+
) {
57+
return "Grok returned an invalid usage-limits response.";
58+
}
5259
return "Grok could not connect to the billing service.";
5360
}
5461

@@ -194,9 +201,7 @@ export const readGrokAccount = Effect.fn("readGrokAccount")(function* (
194201
if (response.status < 200 || response.status >= 300) {
195202
return unavailableGrokUsageLimits(checkedAt, grokUsageHttpFailureMessage(response.status));
196203
}
197-
const body = yield* HttpClientResponse.schemaBodyJson(GrokUsageResponse)(
198-
response,
199-
);
204+
const body = yield* HttpClientResponse.schemaBodyJson(GrokUsageResponse)(response);
200205
return grokUsageResponseToLimits(body, checkedAt);
201206
}).pipe(
202207
Effect.timeout("10 seconds"),

0 commit comments

Comments
 (0)