|
| 1 | +import { describe, it, expect, beforeAll, afterAll } from "vitest"; |
| 2 | +import { createServer, serve } from "@emulators/core"; |
| 3 | +import { Autumn } from "autumn-js"; |
| 4 | + |
| 5 | +import { autumnPlugin, seedFromConfig } from "../index.js"; |
| 6 | +import { manifest } from "../manifest.js"; |
| 7 | + |
| 8 | +// Drives the card-required free-trial checkout flow through the real autumn-js |
| 9 | +// SDK (zod-validated responses) plus raw fetch for the hosted checkout page. |
| 10 | +// This is the contract the cloud app's billing UI depends on. |
| 11 | + |
| 12 | +const PORT = 41875; |
| 13 | +const BASE = `http://localhost:${PORT}`; |
| 14 | +const SUCCESS_URL = `${BASE}/back-to-billing`; |
| 15 | + |
| 16 | +let httpServer: ReturnType<typeof serve>; |
| 17 | +let autumn: Autumn; |
| 18 | + |
| 19 | +beforeAll(() => { |
| 20 | + const { app, store } = createServer(autumnPlugin, { |
| 21 | + port: PORT, |
| 22 | + baseUrl: BASE, |
| 23 | + manifest, |
| 24 | + fallbackUser: { login: "am_emulate_admin", id: 1, scopes: [] }, |
| 25 | + }); |
| 26 | + // Mirror the executor plan catalog: a free plan and a card-required Team trial. |
| 27 | + seedFromConfig(store, BASE, { |
| 28 | + plans: [ |
| 29 | + { id: "free", name: "Free", auto_enable: true, items: [{ feature_id: "executions", included: 10000 }] }, |
| 30 | + { |
| 31 | + id: "team", |
| 32 | + name: "Team", |
| 33 | + price: { amount: 150, interval: "month" }, |
| 34 | + free_trial: { duration_length: 14, duration_type: "day", card_required: true }, |
| 35 | + items: [{ feature_id: "executions", included: 250000 }], |
| 36 | + }, |
| 37 | + ], |
| 38 | + }); |
| 39 | + httpServer = serve({ fetch: app.fetch, port: PORT }); |
| 40 | + autumn = new Autumn({ secretKey: "am_test_emulate", serverURL: BASE }); |
| 41 | +}); |
| 42 | + |
| 43 | +afterAll(async () => { |
| 44 | + await new Promise<void>((resolve) => httpServer.close(() => resolve())); |
| 45 | +}); |
| 46 | + |
| 47 | +const teamPlan = async (customerId: string) => { |
| 48 | + const { list } = await autumn.plans.list({ customerId }); |
| 49 | + const team = list.find((p) => p.id === "team")!; |
| 50 | + const free = list.find((p) => p.id === "free")!; |
| 51 | + return { team, free }; |
| 52 | +}; |
| 53 | + |
| 54 | +describe("autumn emulator: card-required free-trial checkout", () => { |
| 55 | + const CUSTOMER = "org_trial"; |
| 56 | + |
| 57 | + it("a fresh customer is offered the Team trial and sits on free", async () => { |
| 58 | + const { team, free } = await teamPlan(CUSTOMER); |
| 59 | + expect(team.freeTrial, "Team advertises a free trial").not.toBeNull(); |
| 60 | + expect(team.customerEligibility?.trialAvailable, "trial is available").toBe(true); |
| 61 | + expect(team.customerEligibility?.attachAction, "not yet on Team").toBe("upgrade"); |
| 62 | + expect(team.customerEligibility?.status, "not the current plan").toBeUndefined(); |
| 63 | + expect(free.customerEligibility?.status, "free is the current plan").toBe("active"); |
| 64 | + }); |
| 65 | + |
| 66 | + it("attach returns a checkout payment URL (card required)", async () => { |
| 67 | + const res = await autumn.billing.attach({ customerId: CUSTOMER, planId: "team", successUrl: SUCCESS_URL }); |
| 68 | + expect(res.paymentUrl, "a checkout URL is returned").toContain("/checkout/"); |
| 69 | + |
| 70 | + // Attaching alone must NOT activate the subscription: the customer is still |
| 71 | + // on free until the checkout completes and the webhook settles. |
| 72 | + const { team } = await teamPlan(CUSTOMER); |
| 73 | + expect(team.customerEligibility?.status, "still not active after attach").toBeUndefined(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("completing checkout redirects to success_url but does not yet activate", async () => { |
| 77 | + const { paymentUrl } = await autumn.billing.attach({ |
| 78 | + customerId: CUSTOMER, |
| 79 | + planId: "team", |
| 80 | + successUrl: SUCCESS_URL, |
| 81 | + }); |
| 82 | + const sessionId = new URL(paymentUrl!).pathname.split("/").pop()!; |
| 83 | + |
| 84 | + const page = await fetch(paymentUrl!); |
| 85 | + expect(page.status).toBe(200); |
| 86 | + expect(await page.text(), "checkout page names the plan").toContain("Team"); |
| 87 | + |
| 88 | + const completed = await fetch(`${BASE}/checkout/${sessionId}/complete`, { |
| 89 | + method: "POST", |
| 90 | + redirect: "manual", |
| 91 | + }); |
| 92 | + expect(completed.status, "completion redirects").toBe(302); |
| 93 | + expect(completed.headers.get("location"), "back to the app").toBe(SUCCESS_URL); |
| 94 | + |
| 95 | + // The webhook has not landed yet: the customer is STILL on free. This is the |
| 96 | + // exact window in which the billing UI shows the stale plan. |
| 97 | + const customer = await autumn.customers.getOrCreate({ customerId: CUSTOMER }); |
| 98 | + expect(customer.subscriptions ?? [], "no active subscription before settle").toHaveLength(0); |
| 99 | + }); |
| 100 | + |
| 101 | + it("settling the checkout activates the Team trial", async () => { |
| 102 | + const res = await fetch(`${BASE}/checkout/settle`, { |
| 103 | + method: "POST", |
| 104 | + headers: { "content-type": "application/json" }, |
| 105 | + body: JSON.stringify({ customer_id: CUSTOMER }), |
| 106 | + }); |
| 107 | + expect(res.ok).toBe(true); |
| 108 | + |
| 109 | + const { team, free } = await teamPlan(CUSTOMER); |
| 110 | + expect(team.customerEligibility?.status, "Team is now the active plan").toBe("active"); |
| 111 | + expect(team.customerEligibility?.trialing, "on a trial").toBe(true); |
| 112 | + expect(team.customerEligibility?.attachAction, "nothing to attach").toBe("none"); |
| 113 | + expect(free.customerEligibility?.attachAction, "free is now a downgrade").toBe("downgrade"); |
| 114 | + |
| 115 | + const customer = await autumn.customers.getOrCreate({ customerId: CUSTOMER }); |
| 116 | + const sub = (customer.subscriptions ?? []).find((s) => s.planId === "team"); |
| 117 | + expect(sub, "customer carries the Team subscription").toBeTruthy(); |
| 118 | + expect(sub?.status).toBe("trialing"); |
| 119 | + }); |
| 120 | +}); |
0 commit comments