diff --git a/packages/shared-ui/src/flow-gen/app-catalyst.ts b/packages/shared-ui/src/flow-gen/app-catalyst.ts index 0ed4cafde0f..dae520945fb 100644 --- a/packages/shared-ui/src/flow-gen/app-catalyst.ts +++ b/packages/shared-ui/src/flow-gen/app-catalyst.ts @@ -35,13 +35,13 @@ export interface AppCatalystG1CreditsResponse { export type CheckAppAccessResponse = | { - canAccess: false; - accessStatus: string; - termsOfService?: { - version: number; - terms: string; - }; - } + canAccess: false; + accessStatus: string; + termsOfService?: { + version: number; + terms: string; + }; + } | { canAccess: true; accessStatus: string }; export interface AppCatalystContentChunk { @@ -99,12 +99,12 @@ export class AppCatalystApiClient { request: AppCatalystG1SubscriptionStatusRequest ): Promise { const url = new URL("v1beta1/getG1SubscriptionStatus", this.#apiBaseUrl); - url.searchParams.set( - "include_credit_data", - String(request.include_credit_data) - ); const response = await this.#fetchWithCreds(url, { - method: "GET", + method: "POST", + headers: { + "content-type": "application/json", + }, + body: JSON.stringify(request), }); if (!response.ok) { throw new Error( @@ -119,7 +119,11 @@ export class AppCatalystApiClient { async getG1Credits(): Promise { const url = new URL("v1beta1/getG1Credits", this.#apiBaseUrl); const response = await this.#fetchWithCreds(url, { - method: "GET", + method: "POST", + headers: { + "content-type": "application/json", + }, + body: "{}", }); if (!response.ok) { throw new Error(`Failed to get G1 credits: ${response.statusText}`); diff --git a/packages/shared-ui/src/tests/app-catalyst.test.ts b/packages/shared-ui/src/tests/app-catalyst.test.ts new file mode 100644 index 00000000000..568c4fa118a --- /dev/null +++ b/packages/shared-ui/src/tests/app-catalyst.test.ts @@ -0,0 +1,56 @@ + +import { test, describe, it, mock } from "node:test"; +import assert from "node:assert"; +import { AppCatalystApiClient } from "../flow-gen/app-catalyst.js"; + +describe("AppCatalystApiClient", () => { + it("should construct correct URL and headers for getG1SubscriptionStatus", async () => { + const fetchMock = mock.fn(async (_url: URL | RequestInfo, _init?: RequestInit) => { + return new Response(JSON.stringify({ is_member: true, remaining_credits: 10 }), { status: 200 }); + }); + + const client = new AppCatalystApiClient(fetchMock as any, "https://api.example.com"); + await client.getG1SubscriptionStatus({ include_credit_data: true }); + + assert.strictEqual(fetchMock.mock.calls.length, 1); + const [url, init] = fetchMock.mock.calls[0].arguments; + + assert.strictEqual(url.toString(), "https://api.example.com/v1beta1/getG1SubscriptionStatus"); + assert.strictEqual(init?.method, "POST"); + assert.deepStrictEqual(init?.headers, { "content-type": "application/json" }); + assert.strictEqual(init?.body, JSON.stringify({ include_credit_data: true })); + }); + + it("should construct correct URL and headers for getG1Credits", async () => { + const fetchMock = mock.fn(async (_url: URL | RequestInfo, _init?: RequestInit) => { + return new Response(JSON.stringify({ remaining_credits: 5 }), { status: 200 }); + }); + + const client = new AppCatalystApiClient(fetchMock as any, "https://api.example.com"); + await client.getG1Credits(); + + assert.strictEqual(fetchMock.mock.calls.length, 1); + const [url, init] = fetchMock.mock.calls[0].arguments; + + assert.strictEqual(url.toString(), "https://api.example.com/v1beta1/getG1Credits"); + assert.strictEqual(init?.method, "POST"); + assert.deepStrictEqual(init?.headers, { "content-type": "application/json" }); + assert.strictEqual(init?.body, "{}"); + }); + + it("should construct correct URL and headers for checkAppAccess", async () => { + const fetchMock = mock.fn(async (_url: URL | RequestInfo, _init?: RequestInit) => { + // Mock the response structure expected by checkAppAccess + return new Response(JSON.stringify({ accessStatus: "ACCESS_STATUS_OK" }), { status: 200 }); + }); + + const client = new AppCatalystApiClient(fetchMock as any, "https://api.example.com"); + await client.checkTos(); + + assert.strictEqual(fetchMock.mock.calls.length, 1); + const [url, init] = fetchMock.mock.calls[0].arguments; + + assert.strictEqual(url.toString(), "https://api.example.com/v1beta1/checkAppAccess"); + assert.ok(!init?.method || init.method === "GET", "Method should be GET or undefined"); + }); +});