Fix provider binding test mocks

Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
This commit is contained in:
Cursor Agent
2026-08-31 16:53:00 +00:00
co-authored by Simon Shi
parent 66f950f9c5
commit 63c517002e
4 changed files with 12 additions and 30 deletions
+9 -15
View File
@@ -1,3 +1,4 @@
import type { OAuthCredentials } from "@earendil-works/pi-ai";
import { afterEach, describe, expect, it, vi } from "vitest"; import { afterEach, describe, expect, it, vi } from "vitest";
const originalRegion = process.env.QODER_REGION; const originalRegion = process.env.QODER_REGION;
@@ -41,9 +42,8 @@ describe("provider region binding", () => {
expect(providers.get("qoder")?.baseUrl).toBe("https://api3.qoder.sh/"); expect(providers.get("qoder")?.baseUrl).toBe("https://api3.qoder.sh/");
expect(providers.get("qoder-cn")?.baseUrl).toBe("https://gateway.qoder.com.cn/"); expect(providers.get("qoder-cn")?.baseUrl).toBe("https://gateway.qoder.com.cn/");
const fetchMock = vi const fetchMock = vi.fn().mockImplementation(
.fn() async () =>
.mockResolvedValue(
new Response( new Response(
JSON.stringify({ JSON.stringify({
userQuota: { total: 100, used: 1, remaining: 99, percentage: 1, unit: "requests" }, userQuota: { total: 100, used: 1, remaining: 99, percentage: 1, unit: "requests" },
@@ -54,27 +54,21 @@ describe("provider region binding", () => {
}), }),
{ status: 200 }, { status: 200 },
), ),
); );
vi.stubGlobal("fetch", fetchMock); vi.stubGlobal("fetch", fetchMock);
const credentials = { access: "test-token", refresh: "", expires: Date.now() + 3600_000 }; const credentials: OAuthCredentials = { access: "test-token", refresh: "", expires: Date.now() + 3600_000 };
const globalOAuth = providers.get("qoder")?.oauth as { const globalOAuth = providers.get("qoder")?.oauth as {
fetchUsage: (credentials: typeof credentials) => Promise<unknown>; fetchUsage: (credentials: OAuthCredentials) => Promise<unknown>;
}; };
const cnOAuth = providers.get("qoder-cn")?.oauth as { const cnOAuth = providers.get("qoder-cn")?.oauth as {
fetchUsage: (credentials: typeof credentials) => Promise<unknown>; fetchUsage: (credentials: OAuthCredentials) => Promise<unknown>;
}; };
await globalOAuth.fetchUsage(credentials); await globalOAuth.fetchUsage(credentials);
expect(fetchMock).toHaveBeenLastCalledWith( expect(fetchMock).toHaveBeenLastCalledWith("https://openapi.qoder.sh/api/v2/quota/usage", expect.any(Object));
"https://openapi.qoder.sh/api/v2/quota/usage",
expect.any(Object),
);
await cnOAuth.fetchUsage(credentials); await cnOAuth.fetchUsage(credentials);
expect(fetchMock).toHaveBeenLastCalledWith( expect(fetchMock).toHaveBeenLastCalledWith("https://openapi.qoder.com.cn/api/v2/quota/usage", expect.any(Object));
"https://openapi.qoder.com.cn/api/v2/quota/usage",
expect.any(Object),
);
}); });
}); });
+1 -4
View File
@@ -182,10 +182,7 @@ export async function loginQoderForMode(callbacks: OAuthLoginCallbacks, mode: st
return creds; return creds;
} }
export async function refreshQoderTokenForMode( export async function refreshQoderTokenForMode(credentials: OAuthCredentials, mode: string): Promise<OAuthCredentials> {
credentials: OAuthCredentials,
mode: string,
): Promise<OAuthCredentials> {
// PAT-based credentials: re-exchange the stored PAT for a fresh job token. // PAT-based credentials: re-exchange the stored PAT for a fresh job token.
if (isPatRefresh(credentials.refresh)) { if (isPatRefresh(credentials.refresh)) {
const { pat } = decodePatRefresh(credentials.refresh); const { pat } = decodePatRefresh(credentials.refresh);
+1 -7
View File
@@ -12,13 +12,7 @@ import {
type ThinkingContent, type ThinkingContent,
type ToolCall, type ToolCall,
} from "@earendil-works/pi-ai"; } from "@earendil-works/pi-ai";
import { import { buildAuthHeaders, getMachineId, getQoderChatURL, getQoderRegionConfig, isQoderCNMode } from "./cosy.js";
buildAuthHeaders,
getMachineId,
getQoderChatURL,
getQoderRegionConfig,
isQoderCNMode,
} from "./cosy.js";
import { getCachedModelConfig, MAX_OUTPUT_TOKENS } from "./models.js"; import { getCachedModelConfig, MAX_OUTPUT_TOKENS } from "./models.js";
import { resolveQoderIdentity } from "./oauth.js"; import { resolveQoderIdentity } from "./oauth.js";
import { qoderEncodeBody } from "./qoder-encoding.js"; import { qoderEncodeBody } from "./qoder-encoding.js";
+1 -4
View File
@@ -33,10 +33,7 @@ export interface QoderProviderUsage {
raw?: Record<string, unknown>; raw?: Record<string, unknown>;
} }
export async function fetchQoderUsageForMode( export async function fetchQoderUsageForMode(credentials: OAuthCredentials, mode: string): Promise<QoderProviderUsage> {
credentials: OAuthCredentials,
mode: string,
): Promise<QoderProviderUsage> {
const region = getQoderRegionConfig(mode); const region = getQoderRegionConfig(mode);
const response = await fetch(getQoderUsageURL(mode), { const response = await fetch(getQoderUsageURL(mode), {
method: "GET", method: "GET",