Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions tests/provider-quota-opencode-go.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { clearProviderQuotaCache, fetchProviderQuotaReports } from "../src/providers/quota";
import type { OcxConfig } from "../src/types";

const originalFetch = globalThis.fetch;

function openCodeGoConfig(): OcxConfig {
return {
defaultProvider: "opencode-go",
providers: {
"opencode-go": {
adapter: "openai-chat",
authMode: "key",
baseUrl: "https://opencode.ai/zen/v1",
apiKey: "opencode-go-secret",
},
},
} as OcxConfig;
}

beforeEach(() => {
clearProviderQuotaCache();
});

afterEach(() => {
globalThis.fetch = originalFetch;
clearProviderQuotaCache();
});

describe("OpenCode Go provider quota", () => {
test("maps rolling, weekly, and monthly usage from the Go usage endpoint", async () => {
const seen: Array<{ url: string; authorization: string | null; redirect?: RequestRedirect }> = [];
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
const url = String(input);
const headers = new Headers(init?.headers);
seen.push({ url, authorization: headers.get("authorization"), redirect: init?.redirect });
if (url !== "https://opencode.ai/zen/go/v1/usage") {
return new Response("not found", { status: 404 });
}
return new Response(JSON.stringify({
usage: {
rolling: { percent: 12, resetsAt: "2026-08-12T18:00:00Z" },
weekly: { percent: 8, resetsAt: "2026-08-17T00:00:00Z" },
monthly: { percent: 35, resetsAt: "2026-09-01T00:00:00Z" },
},
}), { status: 200, headers: { "content-type": "application/json" } });
}) as typeof fetch;

const result = await fetchProviderQuotaReports(openCodeGoConfig(), true);

expect(seen).toEqual([{

Check failure on line 51 in tests/provider-quota-opencode-go.test.ts

View workflow job for this annotation

GitHub Actions / test 2/4

error: expect(received).toEqual(expected)

- [ - { - "authorization": "***", - "redirect": "error", - "url": "https://opencode.ai/zen/go/v1/usage", - }, - ] + [] - Expected - 7 + Received + 1 at <anonymous> (/home/runner/work/opencodex/opencodex/tests/provider-quota-opencode-go.test.ts:51:18)

Check failure on line 51 in tests/provider-quota-opencode-go.test.ts

View workflow job for this annotation

GitHub Actions / macos

error: expect(received).toEqual(expected)

- [ - { - "authorization": "***", - "redirect": "error", - "url": "https://opencode.ai/zen/go/v1/usage", - }, - ] + [] - Expected - 7 + Received + 1 at <anonymous> (/Users/runner/work/opencodex/opencodex/tests/provider-quota-opencode-go.test.ts:51:18)
url: "https://opencode.ai/zen/go/v1/usage",
authorization: "Bearer opencode-go-secret",
redirect: "error",
}]);
Comment on lines +32 to +55

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Assert the HTTP method for the usage request.

The mock does not capture init?.method. A POST or another non-GET request to the correct URL would pass this test. Capture method and assert "GET" in seen.

Proposed test update
-    const seen: Array<{ url: string; authorization: string | null; redirect?: RequestRedirect }> = [];
+    const seen: Array<{ url: string; method: string; authorization: string | null; redirect?: RequestRedirect }> = [];
     globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
       const url = String(input);
       const headers = new Headers(init?.headers);
-      seen.push({ url, authorization: headers.get("authorization"), redirect: init?.redirect });
+      seen.push({
+        url,
+        method: init?.method ?? "GET",
+        authorization: headers.get("authorization"),
+        redirect: init?.redirect,
+      });
...
       url: "https://opencode.ai/zen/go/v1/usage",
+      method: "GET",
       authorization: "Bearer opencode-go-secret",
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const seen: Array<{ url: string; authorization: string | null; redirect?: RequestRedirect }> = [];
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
const url = String(input);
const headers = new Headers(init?.headers);
seen.push({ url, authorization: headers.get("authorization"), redirect: init?.redirect });
if (url !== "https://opencode.ai/zen/go/v1/usage") {
return new Response("not found", { status: 404 });
}
return new Response(JSON.stringify({
usage: {
rolling: { percent: 12, resetsAt: "2026-08-12T18:00:00Z" },
weekly: { percent: 8, resetsAt: "2026-08-17T00:00:00Z" },
monthly: { percent: 35, resetsAt: "2026-09-01T00:00:00Z" },
},
}), { status: 200, headers: { "content-type": "application/json" } });
}) as typeof fetch;
const result = await fetchProviderQuotaReports(openCodeGoConfig(), true);
expect(seen).toEqual([{
url: "https://opencode.ai/zen/go/v1/usage",
authorization: "Bearer opencode-go-secret",
redirect: "error",
}]);
const seen: Array<{ url: string; method: string; authorization: string | null; redirect?: RequestRedirect }> = [];
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
const url = String(input);
const headers = new Headers(init?.headers);
seen.push({
url,
method: init?.method ?? "GET",
authorization: headers.get("authorization"),
redirect: init?.redirect,
});
if (url !== "https://opencode.ai/zen/go/v1/usage") {
return new Response("not found", { status: 404 });
}
return new Response(JSON.stringify({
usage: {
rolling: { percent: 12, resetsAt: "2026-08-12T18:00:00Z" },
weekly: { percent: 8, resetsAt: "2026-08-17T00:00:00Z" },
monthly: { percent: 35, resetsAt: "2026-09-01T00:00:00Z" },
},
}), { status: 200, headers: { "content-type": "application/json" } });
}) as typeof fetch;
const result = await fetchProviderQuotaReports(openCodeGoConfig(), true);
expect(seen).toEqual([{
url: "https://opencode.ai/zen/go/v1/usage",
method: "GET",
authorization: "Bearer opencode-go-secret",
redirect: "error",
}]);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/provider-quota-opencode-go.test.ts` around lines 32 - 55, Update the
fetch mock and assertion in the quota test around fetchProviderQuotaReports and
the seen request records to capture init?.method, then require the recorded
usage request method to equal "GET" alongside the existing URL, authorization,
and redirect expectations.

expect(result.reports).toHaveLength(1);
expect(result.reports[0]?.provider).toBe("opencode-go");
expect(result.reports[0]?.source).toBe("opencode-go:usage");
expect(result.reports[0]?.quota).toEqual({
fiveHourPercent: 12,
fiveHourResetAt: Date.parse("2026-08-12T18:00:00Z"),
weeklyPercent: 8,
weeklyResetAt: Date.parse("2026-08-17T00:00:00Z"),
monthlyPercent: 35,
monthlyResetAt: Date.parse("2026-09-01T00:00:00Z"),
updatedAt: expect.any(Number),
});
expect(JSON.stringify(result)).not.toContain("opencode-go-secret");
});
});
Loading