Skip to content

Commit 15852fb

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

2 files changed

Lines changed: 88 additions & 42 deletions

File tree

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

Lines changed: 79 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { describe, expect, it } from "@effect/vitest";
77
import * as Effect from "effect/Effect";
88
import * as FileSystem from "effect/FileSystem";
99
import * as Schema from "effect/Schema";
10-
import { HttpClient, HttpClientResponse } from "effect/http";
10+
import { HttpClient, HttpClientError, HttpClientResponse } from "effect/http";
1111
import { GrokSettings } from "@t3tools/contracts";
1212

1313
import {
@@ -758,44 +758,86 @@ it.layer(NodeServices.layer)("readGrokAccount", (it) => {
758758
}),
759759
);
760760

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)),
761+
it.effect(
762+
"classifies billing failures without exposing response bodies, keeping the account",
763+
() =>
764+
Effect.gen(function* () {
765+
for (const [response, message] of [
766+
[
767+
new Response("private response", { status: 401 }),
768+
"Grok sign-in was rejected. Reconnect Grok in provider settings.",
769+
],
770+
[
771+
new Response("private response", { status: 403 }),
772+
"Grok usage limits are unavailable for this account.",
773+
],
774+
[
775+
new Response("private response", { status: 429 }),
776+
"Grok usage-limit checks are rate limited. Try again soon.",
777+
],
778+
[
779+
new Response("private response", { status: 503 }),
780+
"Grok billing is temporarily unavailable.",
781+
],
782+
[new Response("private response", { status: 418 }), "Grok billing returned HTTP 418."],
783+
[
784+
new Response("{", { headers: { "content-type": "application/json" } }),
785+
"Grok returned an invalid usage-limits response.",
786+
],
787+
[
788+
Response.json({ config: { creditUsagePercent: "private-value" } }),
789+
"Grok returned an invalid usage-limits response.",
790+
],
791+
] as const) {
792+
const { email, usageLimits: limits } = yield* readGrokAccount({
793+
HOME: "/definitely/not/a/grok-home",
794+
GROK_AUTH:
795+
'{"https://accounts.x.ai/sign-in":{"key":"private-token","email":"[email protected]"}}',
796+
}).pipe(
797+
Effect.provideService(
798+
HttpClient.HttpClient,
799+
HttpClient.make((request) =>
800+
Effect.succeed(HttpClientResponse.fromWeb(request, response)),
801+
),
788802
),
803+
);
804+
expect(email).toBe("[email protected]");
805+
expect(limits.windows).toEqual([]);
806+
expect(limits.unavailable).toEqual({
807+
reason: "probeFailed",
808+
message,
809+
});
810+
}
811+
}),
812+
);
813+
814+
it.effect("reports a transport failure without exposing its cause", () =>
815+
readGrokAccount({
816+
HOME: "/definitely/not/a/grok-home",
817+
GROK_AUTH: '{"https://accounts.x.ai/sign-in":{"key":"private-token"}}',
818+
}).pipe(
819+
Effect.provideService(
820+
HttpClient.HttpClient,
821+
HttpClient.make((request) =>
822+
Effect.fail(
823+
new HttpClientError.HttpClientError({
824+
reason: new HttpClientError.TransportError({
825+
request,
826+
cause: new Error("private transport cause"),
827+
}),
828+
}),
789829
),
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-
}),
830+
),
831+
),
832+
Effect.tap(({ usageLimits }) =>
833+
Effect.sync(() =>
834+
expect(usageLimits.unavailable).toEqual({
835+
reason: "probeFailed",
836+
message: "Grok could not connect to the billing service.",
837+
}),
838+
),
839+
),
840+
),
799841
);
800842
});
801843

‎apps/server/src/provider/grokUsageLimits.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as FileSystem from "effect/FileSystem";
66
import * as Option from "effect/Option";
77
import * as Path from "effect/Path";
88
import * as Schema from "effect/Schema";
9-
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
9+
import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http";
1010
import {
1111
clampPercent,
1212
makeUnavailableUsageLimits,
@@ -42,11 +42,17 @@ function unavailableGrokUsageLimits(checkedAt: string, message: string) {
4242

4343
function grokUsageProbeFailureMessage(error: unknown): string {
4444
if (typeof error === "object" && error !== null && "_tag" in error) {
45-
if (error._tag === "TimeoutException") return "Grok usage-limit check timed out.";
45+
if (error._tag === "TimeoutError") return "Grok usage-limit check timed out.";
4646
if (error._tag === "ParseError" || error._tag === "SchemaError") {
4747
return "Grok returned an invalid usage-limits response.";
4848
}
4949
}
50+
if (
51+
HttpClientError.isHttpClientError(error) &&
52+
(error.reason._tag === "DecodeError" || error.reason._tag === "EmptyBodyError")
53+
) {
54+
return "Grok returned an invalid usage-limits response.";
55+
}
5056
return "Grok could not connect to the billing service.";
5157
}
5258

@@ -189,9 +195,7 @@ export const readGrokAccount = Effect.fn("readGrokAccount")(function* (
189195
if (response.status < 200 || response.status >= 300) {
190196
return unavailableGrokUsageLimits(checkedAt, grokUsageHttpFailureMessage(response.status));
191197
}
192-
const body = yield* HttpClientResponse.schemaBodyJson(GrokUsageResponse)(
193-
response,
194-
);
198+
const body = yield* HttpClientResponse.schemaBodyJson(GrokUsageResponse)(response);
195199
return grokUsageResponseToLimits(body, checkedAt);
196200
}).pipe(
197201
Effect.timeout("10 seconds"),

0 commit comments

Comments
 (0)