|
| 1 | +import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"; |
| 2 | +import * as platform from "@/utils/platform"; |
| 3 | +import { fetchPortalUrl } from "./billingApi"; |
| 4 | +import { billingReturnOrigin } from "./returnOrigin"; |
| 5 | + |
| 6 | +describe("billing browser returns", () => { |
| 7 | + const previousBillingApi = process.env.VITE_MAPLE_BILLING_API_URL; |
| 8 | + const previousWindow = Object.getOwnPropertyDescriptor(globalThis, "window"); |
| 9 | + const cleanup: Array<() => void> = []; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + const native = spyOn(platform, "isTauri").mockReturnValue(true); |
| 13 | + const mobile = spyOn(platform, "isMobile").mockReturnValue(false); |
| 14 | + const android = spyOn(platform, "isAndroid").mockReturnValue(false); |
| 15 | + cleanup.push( |
| 16 | + () => native.mockRestore(), |
| 17 | + () => mobile.mockRestore(), |
| 18 | + () => android.mockRestore() |
| 19 | + ); |
| 20 | + Object.defineProperty(globalThis, "window", { |
| 21 | + configurable: true, |
| 22 | + writable: true, |
| 23 | + value: { location: { origin: "http://tauri.localhost" } } |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + cleanup |
| 29 | + .splice(0) |
| 30 | + .reverse() |
| 31 | + .forEach((restore) => restore()); |
| 32 | + if (previousBillingApi === undefined) delete process.env.VITE_MAPLE_BILLING_API_URL; |
| 33 | + else process.env.VITE_MAPLE_BILLING_API_URL = previousBillingApi; |
| 34 | + if (previousWindow) Object.defineProperty(globalThis, "window", previousWindow); |
| 35 | + else Reflect.deleteProperty(globalThis, "window"); |
| 36 | + }); |
| 37 | + |
| 38 | + for (const [api, origin] of [ |
| 39 | + ["https://billing-dev.opensecret.cloud", "https://app-dev.maple.ai"], |
| 40 | + ["https://billing-dev.opensecret.cloud/", "https://app-dev.maple.ai"], |
| 41 | + ["https://billing.opensecret.cloud", "https://app.maple.ai"] |
| 42 | + ]) { |
| 43 | + test(`desktop portal with ${api} returns directly to ${origin}`, async () => { |
| 44 | + process.env.VITE_MAPLE_BILLING_API_URL = api; |
| 45 | + const request = spyOn(globalThis, "fetch").mockResolvedValue( |
| 46 | + Response.json({ portal_url: "https://billing.stripe.com/p/session/test" }) |
| 47 | + ); |
| 48 | + cleanup.push(() => request.mockRestore()); |
| 49 | + |
| 50 | + expect(billingReturnOrigin()).toBe(origin); |
| 51 | + await fetchPortalUrl("fixture-token"); |
| 52 | + expect(request).toHaveBeenCalledTimes(1); |
| 53 | + expect(JSON.parse(String(request.mock.calls[0][1]?.body))).toEqual({ |
| 54 | + return_url: origin |
| 55 | + }); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + for (const origin of [ |
| 60 | + "https://app.maple.ai", |
| 61 | + "https://app-dev.maple.ai", |
| 62 | + "https://app-dev.trymaple.ai", |
| 63 | + "http://127.0.0.1:5173" |
| 64 | + ]) { |
| 65 | + test(`web preserves its actual origin ${origin}`, () => { |
| 66 | + spyOn(platform, "isTauri").mockReturnValue(false); |
| 67 | + Object.defineProperty(window.location, "origin", { value: origin, configurable: true }); |
| 68 | + process.env.VITE_MAPLE_BILLING_API_URL = "https://billing-dev.opensecret.cloud"; |
| 69 | + expect(billingReturnOrigin()).toBe(origin); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + test("mobile keeps the existing associated-link host", () => { |
| 74 | + spyOn(platform, "isMobile").mockReturnValue(true); |
| 75 | + process.env.VITE_MAPLE_BILLING_API_URL = "https://billing-dev.opensecret.cloud"; |
| 76 | + expect(billingReturnOrigin()).toBe("https://trymaple.ai"); |
| 77 | + }); |
| 78 | +}); |
0 commit comments