Merge branch 'miao/reverse-gate' into miao/journey-test

This commit is contained in:
Miao Yang
2026-10-29 23:34:38 +08:00
2 changed files with 11 additions and 3 deletions
+7 -2
View File
@@ -2,13 +2,17 @@ import { afterEach, describe, expect, it, vi } from "vitest";
vi.mock("../cloud/auth.js", () => ({
resolveCloudAuthHeaders: async () => ({ "x-api-key": "k" }),
resolveCloudBaseUrl: () => "https://api.test",
resolveCloudBaseUrl: () => "https://api.dev.heygen.com",
}));
const { readPlan } = await import("./plan.js");
const asked: string[] = [];
const answer = (data: unknown) =>
vi.stubGlobal("fetch", async () => new Response(JSON.stringify({ code: 100, data })));
vi.stubGlobal("fetch", async (url: string) => {
asked.push(url);
return new Response(JSON.stringify({ code: 100, data }));
});
afterEach(() => vi.unstubAllGlobals());
@@ -19,6 +23,7 @@ describe("readPlan", () => {
usage: { remaining_units: 25000 },
});
expect(await readPlan()).toEqual({ plan: "pro", credits: 25000 });
expect(asked.at(-1)).toBe("https://api2.dev.heygen.com/v1/payment/hyperframes/subscription");
answer({ subscription: null, usage: { remaining_units: 0 } });
expect(await readPlan()).toEqual({ plan: "none", credits: 0 });
answer({
+4 -1
View File
@@ -21,10 +21,13 @@ function mockPlan(): Plan | null {
return { plan, credits: Number(credits) || 0, mock: true };
}
// Payment routes live on api2, beside the OAuth ones; the v3 generation routes stay on api.
const billingBaseUrl = () => resolveCloudBaseUrl().replace("://api.", "://api2.");
/** The plan from `/v1/payment/hyperframes/subscription`, else the mock, else null. */
export async function readPlan(): Promise<Plan | null> {
try {
const res = await fetch(`${resolveCloudBaseUrl()}/v1/payment/hyperframes/subscription`, {
const res = await fetch(`${billingBaseUrl()}/v1/payment/hyperframes/subscription`, {
headers: { ...(await resolveCloudAuthHeaders()), accept: "application/json" },
signal: AbortSignal.timeout(15_000),
});