From 63c517002e72ec346094694a845a145d8e922332 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 31 Aug 2026 16:53:00 +0000 Subject: [PATCH] Fix provider binding test mocks Co-authored-by: Simon Shi --- src/__tests__/providers.test.ts | 24 +++++++++--------------- src/oauth.ts | 5 +---- src/stream.ts | 8 +------- src/usage.ts | 5 +---- 4 files changed, 12 insertions(+), 30 deletions(-) diff --git a/src/__tests__/providers.test.ts b/src/__tests__/providers.test.ts index 8616425..6783cdd 100644 --- a/src/__tests__/providers.test.ts +++ b/src/__tests__/providers.test.ts @@ -1,3 +1,4 @@ +import type { OAuthCredentials } from "@earendil-works/pi-ai"; import { afterEach, describe, expect, it, vi } from "vitest"; 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-cn")?.baseUrl).toBe("https://gateway.qoder.com.cn/"); - const fetchMock = vi - .fn() - .mockResolvedValue( + const fetchMock = vi.fn().mockImplementation( + async () => new Response( JSON.stringify({ userQuota: { total: 100, used: 1, remaining: 99, percentage: 1, unit: "requests" }, @@ -54,27 +54,21 @@ describe("provider region binding", () => { }), { status: 200 }, ), - ); + ); 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 { - fetchUsage: (credentials: typeof credentials) => Promise; + fetchUsage: (credentials: OAuthCredentials) => Promise; }; const cnOAuth = providers.get("qoder-cn")?.oauth as { - fetchUsage: (credentials: typeof credentials) => Promise; + fetchUsage: (credentials: OAuthCredentials) => Promise; }; await globalOAuth.fetchUsage(credentials); - expect(fetchMock).toHaveBeenLastCalledWith( - "https://openapi.qoder.sh/api/v2/quota/usage", - expect.any(Object), - ); + expect(fetchMock).toHaveBeenLastCalledWith("https://openapi.qoder.sh/api/v2/quota/usage", expect.any(Object)); await cnOAuth.fetchUsage(credentials); - expect(fetchMock).toHaveBeenLastCalledWith( - "https://openapi.qoder.com.cn/api/v2/quota/usage", - expect.any(Object), - ); + expect(fetchMock).toHaveBeenLastCalledWith("https://openapi.qoder.com.cn/api/v2/quota/usage", expect.any(Object)); }); }); diff --git a/src/oauth.ts b/src/oauth.ts index 8c89ebb..7f6cbe5 100644 --- a/src/oauth.ts +++ b/src/oauth.ts @@ -182,10 +182,7 @@ export async function loginQoderForMode(callbacks: OAuthLoginCallbacks, mode: st return creds; } -export async function refreshQoderTokenForMode( - credentials: OAuthCredentials, - mode: string, -): Promise { +export async function refreshQoderTokenForMode(credentials: OAuthCredentials, mode: string): Promise { // PAT-based credentials: re-exchange the stored PAT for a fresh job token. if (isPatRefresh(credentials.refresh)) { const { pat } = decodePatRefresh(credentials.refresh); diff --git a/src/stream.ts b/src/stream.ts index 63f238e..59bb3b6 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -12,13 +12,7 @@ import { type ThinkingContent, type ToolCall, } from "@earendil-works/pi-ai"; -import { - buildAuthHeaders, - getMachineId, - getQoderChatURL, - getQoderRegionConfig, - isQoderCNMode, -} from "./cosy.js"; +import { buildAuthHeaders, getMachineId, getQoderChatURL, getQoderRegionConfig, isQoderCNMode } from "./cosy.js"; import { getCachedModelConfig, MAX_OUTPUT_TOKENS } from "./models.js"; import { resolveQoderIdentity } from "./oauth.js"; import { qoderEncodeBody } from "./qoder-encoding.js"; diff --git a/src/usage.ts b/src/usage.ts index 275c32f..14fa94c 100644 --- a/src/usage.ts +++ b/src/usage.ts @@ -33,10 +33,7 @@ export interface QoderProviderUsage { raw?: Record; } -export async function fetchQoderUsageForMode( - credentials: OAuthCredentials, - mode: string, -): Promise { +export async function fetchQoderUsageForMode(credentials: OAuthCredentials, mode: string): Promise { const region = getQoderRegionConfig(mode); const response = await fetch(getQoderUsageURL(mode), { method: "GET",