Bind Qoder providers to fixed regions
Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
This commit is contained in:
co-authored by
Simon Shi
parent
b1d66d6eb4
commit
66f950f9c5
@@ -0,0 +1,80 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const originalRegion = process.env.QODER_REGION;
|
||||
const patEnvNames = [
|
||||
"QODER_API_KEY",
|
||||
"QODER_PERSONAL_ACCESS_TOKEN",
|
||||
"QODER_PAT",
|
||||
"QODERCN_API_KEY",
|
||||
"QODERCN_PERSONAL_ACCESS_TOKEN",
|
||||
"QODERCN_PAT",
|
||||
] as const;
|
||||
const originalPats = Object.fromEntries(patEnvNames.map((name) => [name, process.env[name]]));
|
||||
|
||||
afterEach(() => {
|
||||
if (originalRegion === undefined) delete process.env.QODER_REGION;
|
||||
else process.env.QODER_REGION = originalRegion;
|
||||
for (const name of patEnvNames) {
|
||||
const value = originalPats[name];
|
||||
if (value === undefined) delete process.env[name];
|
||||
else process.env[name] = value;
|
||||
}
|
||||
vi.unstubAllGlobals();
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
describe("provider region binding", () => {
|
||||
it("keeps qoder global and qoder-cn in CN when QODER_REGION=cn", async () => {
|
||||
process.env.QODER_REGION = "cn";
|
||||
for (const name of patEnvNames) delete process.env[name];
|
||||
const providers = new Map<string, Record<string, unknown>>();
|
||||
const pi = {
|
||||
registerProvider(providerID: string, config: Record<string, unknown>) {
|
||||
providers.set(providerID, config);
|
||||
},
|
||||
on: vi.fn(),
|
||||
};
|
||||
|
||||
const { default: registerProviders } = await import("../index.js");
|
||||
await registerProviders(pi as never);
|
||||
|
||||
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(
|
||||
new Response(
|
||||
JSON.stringify({
|
||||
userQuota: { total: 100, used: 1, remaining: 99, percentage: 1, unit: "requests" },
|
||||
orgResourcePackage: { total: 0, used: 0, remaining: 0, percentage: 0, unit: "requests" },
|
||||
totalUsagePercentage: 1,
|
||||
isQuotaExceeded: false,
|
||||
expiresAt: Date.now() + 3600_000,
|
||||
}),
|
||||
{ status: 200 },
|
||||
),
|
||||
);
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
|
||||
const credentials = { access: "test-token", refresh: "", expires: Date.now() + 3600_000 };
|
||||
const globalOAuth = providers.get("qoder")?.oauth as {
|
||||
fetchUsage: (credentials: typeof credentials) => Promise<unknown>;
|
||||
};
|
||||
const cnOAuth = providers.get("qoder-cn")?.oauth as {
|
||||
fetchUsage: (credentials: typeof credentials) => Promise<unknown>;
|
||||
};
|
||||
|
||||
await globalOAuth.fetchUsage(credentials);
|
||||
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),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -93,8 +93,8 @@ function mockFetch(body: string): typeof fetch {
|
||||
return vi.fn(async () => response) as unknown as typeof fetch;
|
||||
}
|
||||
|
||||
function makeModel(): Model<Api> {
|
||||
return { id: "ultimate", api: "qoder-api" as Api, provider: "qoder" } as Model<Api>;
|
||||
function makeModel(provider = "qoder"): Model<Api> {
|
||||
return { id: "ultimate", api: "qoder-api" as Api, provider } as Model<Api>;
|
||||
}
|
||||
|
||||
function makeContext(): Context {
|
||||
@@ -116,8 +116,11 @@ async function consume(stream: AssistantMessageEventStream): Promise<AssistantMe
|
||||
|
||||
describe("streamQoder", () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
const originalCnPat = process.env.QODERCN_PERSONAL_ACCESS_TOKEN;
|
||||
afterEach(() => {
|
||||
globalThis.fetch = originalFetch;
|
||||
if (originalCnPat === undefined) delete process.env.QODERCN_PERSONAL_ACCESS_TOKEN;
|
||||
else process.env.QODERCN_PERSONAL_ACCESS_TOKEN = originalCnPat;
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
@@ -134,6 +137,24 @@ describe("streamQoder", () => {
|
||||
expect(text && "text" in text ? text.text : "").toBe("OK");
|
||||
});
|
||||
|
||||
it("binds chat hosts to provider ids even when only a CN PAT is set", async () => {
|
||||
process.env.QODERCN_PERSONAL_ACCESS_TOKEN = "pt-cn-only";
|
||||
|
||||
globalThis.fetch = mockFetch(SUCCESS_SSE);
|
||||
await consume(streamQoder(makeModel("qoder"), makeContext(), { apiKey: "fake" }));
|
||||
expect(globalThis.fetch).toHaveBeenCalledWith(
|
||||
expect.stringMatching(/^https:\/\/api3\.qoder\.sh\//),
|
||||
expect.any(Object),
|
||||
);
|
||||
|
||||
globalThis.fetch = mockFetch(SUCCESS_SSE);
|
||||
await consume(streamQoder(makeModel("qoder-cn"), makeContext(), { apiKey: "fake" }));
|
||||
expect(globalThis.fetch).toHaveBeenCalledWith(
|
||||
expect.stringMatching(/^https:\/\/gateway\.qoder\.com\.cn\//),
|
||||
expect.any(Object),
|
||||
);
|
||||
});
|
||||
|
||||
it("surfaces an upstream 406 'Session blocked' as an error event, not a silent stop", async () => {
|
||||
globalThis.fetch = mockFetch(BLOCKED_SSE);
|
||||
const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" });
|
||||
|
||||
Reference in New Issue
Block a user