Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions packages/@emulators/autumn/src/__tests__/autumn-carry-over.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { createServer, serve } from "@emulators/core";
import { createServer, serveOnFreePort, type FreePortServer } from "@emulators/core";

import { autumnPlugin, seedFromConfig } from "../index.js";
import { manifest } from "../manifest.js";
Expand All @@ -10,12 +10,12 @@ import { manifest } from "../manifest.js";
// old plan's Stripe subscription. Raw fetch, since these routes postdate the
// pinned SDK.

const PORT = 41941;
const BASE = `http://localhost:${PORT}`;
let PORT = 0;
let BASE = "";
const PERIOD_END = Date.UTC(2030, 0, 15);
const TRIAL_END = Date.UTC(2030, 0, 8);

let httpServer: ReturnType<typeof serve>;
let httpServer: FreePortServer;

const post = async (path: string, body: unknown) => {
const response = await fetch(`${BASE}/v1/${path}`, {
Expand Down Expand Up @@ -57,7 +57,11 @@ const subscribed = (sub: Record<string, unknown>) => ({
...sub,
});

beforeAll(() => {
beforeAll(async () => {
// A port the system chooses, held from the start (see serveOnFreePort).
httpServer = await serveOnFreePort();
PORT = httpServer.port;
BASE = `http://localhost:${PORT}`;
const { app, store } = createServer(autumnPlugin, {
port: PORT,
baseUrl: BASE,
Expand Down Expand Up @@ -95,11 +99,11 @@ beforeAll(() => {
{ id: "org_taken", subscriptions: [{ plan_id: "free", status: "active" }] },
],
});
httpServer = serve({ fetch: app.fetch, port: PORT });
httpServer.use(app.fetch);
});

afterAll(async () => {
await new Promise<void>((resolve) => httpServer.close(() => resolve()));
await httpServer.close();
});

describe("autumn emulator: carrying a subscription over to another catalog", () => {
Expand Down
18 changes: 11 additions & 7 deletions packages/@emulators/autumn/src/__tests__/autumn-catalog.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { createServer, serve } from "@emulators/core";
import { createServer, serveOnFreePort, type FreePortServer } from "@emulators/core";
import { Autumn } from "autumn-js";

import { autumnPlugin, seedFromConfig } from "../index.js";
Expand All @@ -16,8 +16,8 @@ import { manifest } from "../manifest.js";
// executions feature and a non-consumable members feature, and a paid team
// plan with unlimited executions and a per-seat usage price.

const PORT = 41881;
const BASE = `http://localhost:${PORT}`;
let PORT = 0;
let BASE = "";
const NS = "app-stage";
const FREE = `${NS}-free`;
const TEAM = `${NS}-team`;
Expand All @@ -27,7 +27,7 @@ const MEMBERS = `${NS}-members`;
/** The tiny plan's whole allowance, small enough to exhaust from a test. */
const TINY_INCLUDED = 50;

let httpServer: ReturnType<typeof serve>;
let httpServer: FreePortServer;
let autumn: Autumn;

const post = (path: string, body: unknown) =>
Expand Down Expand Up @@ -77,7 +77,11 @@ const check = (customerId: string, featureId: string, requiredBalance: number, s
send_event: sendEvent,
});

beforeAll(() => {
beforeAll(async () => {
// A port the system chooses, held from the start (see serveOnFreePort).
httpServer = await serveOnFreePort();
PORT = httpServer.port;
BASE = `http://localhost:${PORT}`;
const { app, store } = createServer(autumnPlugin, {
port: PORT,
baseUrl: BASE,
Expand Down Expand Up @@ -122,12 +126,12 @@ beforeAll(() => {
},
],
});
httpServer = serve({ fetch: app.fetch, port: PORT });
httpServer.use(app.fetch);
autumn = new Autumn({ secretKey: "am_test_emulate", serverURL: BASE });
});

afterAll(async () => {
await new Promise<void>((resolve) => httpServer.close(() => resolve()));
await httpServer.close();
});

describe("customers.get_or_create honours auto_enable_plan_id", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { createServer, serve } from "@emulators/core";
import { createServer, serveOnFreePort, type FreePortServer } from "@emulators/core";
import { Autumn } from "autumn-js";

import { autumnPlugin, seedFromConfig } from "../index.js";
Expand All @@ -10,14 +10,18 @@ import { manifest } from "../manifest.js";
// card and redirects back, and the customer's default payment method only
// changes once the Stripe webhook settles.

const PORT = 41885;
const BASE = `http://localhost:${PORT}`;
const SUCCESS_URL = `${BASE}/back-to-billing`;
let PORT = 0;
let BASE = "";
const successUrl = () => `${BASE}/back-to-billing`;

let httpServer: ReturnType<typeof serve>;
let httpServer: FreePortServer;
let autumn: Autumn;

beforeAll(() => {
beforeAll(async () => {
// A port the system chooses, held from the start (see serveOnFreePort).
httpServer = await serveOnFreePort();
PORT = httpServer.port;
BASE = `http://localhost:${PORT}`;
const { app, store } = createServer(autumnPlugin, {
port: PORT,
baseUrl: BASE,
Expand All @@ -35,12 +39,12 @@ beforeAll(() => {
},
],
});
httpServer = serve({ fetch: app.fetch, port: PORT });
httpServer.use(app.fetch);
autumn = new Autumn({ secretKey: "am_test_emulate", serverURL: BASE });
});

afterAll(async () => {
await new Promise<void>((resolve) => httpServer.close(() => resolve()));
await httpServer.close();
});

// The SDK types the expanded field as `any`, so read it through a narrow shape.
Expand All @@ -60,7 +64,7 @@ describe("autumn emulator: setup_payment (change the card on file)", () => {
const CUSTOMER = "org_setup";

it("setup_payment returns a hosted setup URL", async () => {
const res = await autumn.billing.setupPayment({ customerId: CUSTOMER, successUrl: SUCCESS_URL });
const res = await autumn.billing.setupPayment({ customerId: CUSTOMER, successUrl: successUrl() });
expect(res.customerId).toBe(CUSTOMER);
expect(res.url, "a hosted setup URL is returned").toContain("/checkout/setup/");

Expand All @@ -69,7 +73,7 @@ describe("autumn emulator: setup_payment (change the card on file)", () => {
});

it("the hosted setup page renders a save-card form", async () => {
const { url } = await autumn.billing.setupPayment({ customerId: CUSTOMER, successUrl: SUCCESS_URL });
const { url } = await autumn.billing.setupPayment({ customerId: CUSTOMER, successUrl: successUrl() });
const page = await fetch(url);
expect(page.status).toBe(200);
const html = await page.text();
Expand All @@ -78,7 +82,7 @@ describe("autumn emulator: setup_payment (change the card on file)", () => {
});

it("submitting the card redirects to success_url but does not yet replace the card", async () => {
const { url } = await autumn.billing.setupPayment({ customerId: CUSTOMER, successUrl: SUCCESS_URL });
const { url } = await autumn.billing.setupPayment({ customerId: CUSTOMER, successUrl: successUrl() });
const sessionId = new URL(url).pathname.split("/").pop()!;

const completed = await fetch(`${BASE}/checkout/setup/${sessionId}/complete`, {
Expand All @@ -88,7 +92,7 @@ describe("autumn emulator: setup_payment (change the card on file)", () => {
redirect: "manual",
});
expect(completed.status, "completion redirects").toBe(302);
expect(completed.headers.get("location"), "back to the app").toBe(SUCCESS_URL);
expect(completed.headers.get("location"), "back to the app").toBe(successUrl());

// The webhook has not landed: the customer still has the OLD card (none).
expect(await cardOnFile(CUSTOMER), "card unchanged before settle").toBeNull();
Expand Down Expand Up @@ -122,7 +126,7 @@ describe("autumn emulator: setup_payment (change the card on file)", () => {
const before = await cardOnFile(CUSTOMER);
expect(before?.card, "the first setup left a mastercard on file").toMatchObject({ last4: "4444" });

const { url } = await autumn.billing.setupPayment({ customerId: CUSTOMER, successUrl: SUCCESS_URL });
const { url } = await autumn.billing.setupPayment({ customerId: CUSTOMER, successUrl: successUrl() });
const sessionId = new URL(url).pathname.split("/").pop()!;
await fetch(`${BASE}/checkout/setup/${sessionId}/complete`, {
method: "POST",
Expand All @@ -140,7 +144,7 @@ describe("autumn emulator: setup_payment (change the card on file)", () => {

it("paying for a plan leaves a visa on file", async () => {
const PAYING = "org_pays";
const attach = await autumn.billing.attach({ customerId: PAYING, planId: "team", successUrl: SUCCESS_URL });
const attach = await autumn.billing.attach({ customerId: PAYING, planId: "team", successUrl: successUrl() });
const sessionId = new URL(attach.paymentUrl!).pathname.split("/").pop()!;
await fetch(`${BASE}/checkout/${sessionId}/complete`, { method: "POST", redirect: "manual" });
await fetch(`${BASE}/checkout/settle`, {
Expand All @@ -156,17 +160,17 @@ describe("autumn emulator: setup_payment (change the card on file)", () => {

describe("autumn emulator: billing portal (change the card on file)", () => {
const CUSTOMER = "org_portal";
const RETURN_URL = `${BASE}/settings/billing`;
const returnUrl = () => `${BASE}/settings/billing`;

const openPortal = async (): Promise<string> => {
const res = await autumn.billing.openCustomerPortal({ customerId: CUSTOMER, returnUrl: RETURN_URL });
const res = await autumn.billing.openCustomerPortal({ customerId: CUSTOMER, returnUrl: returnUrl() });
expect(res.customerId).toBe(CUSTOMER);
return res.url;
};

it("the portal page shows the card on file and links back to the app", async () => {
// Give the customer a card the only supported way: a first setup session.
const setup = await autumn.billing.setupPayment({ customerId: CUSTOMER, successUrl: SUCCESS_URL });
const setup = await autumn.billing.setupPayment({ customerId: CUSTOMER, successUrl: successUrl() });
const sessionId = new URL(setup.url).pathname.split("/").pop()!;
await fetch(`${BASE}/checkout/setup/${sessionId}/complete`, {
method: "POST",
Expand All @@ -186,7 +190,7 @@ describe("autumn emulator: billing portal (change the card on file)", () => {
expect(html, "the current card brand").toContain("visa");
expect(html, "the current card last4").toContain("4242");
expect(html, "the update form").toContain("Update payment method");
expect(html, "the return_url recorded when the portal was opened").toContain(`href="${RETURN_URL}"`);
expect(html, "the return_url recorded when the portal was opened").toContain(`href="${returnUrl()}"`);
});

it("updating the card in the portal applies immediately, with no settle step", async () => {
Expand Down
26 changes: 15 additions & 11 deletions packages/@emulators/autumn/src/__tests__/autumn-trial.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { createServer, serve } from "@emulators/core";
import { createServer, serveOnFreePort, type FreePortServer } from "@emulators/core";
import { Autumn } from "autumn-js";

import { autumnPlugin, seedFromConfig } from "../index.js";
Expand All @@ -9,14 +9,18 @@ import { manifest } from "../manifest.js";
// SDK (zod-validated responses) plus raw fetch for the hosted checkout page.
// This is the contract the cloud app's billing UI depends on.

const PORT = 41875;
const BASE = `http://localhost:${PORT}`;
const SUCCESS_URL = `${BASE}/back-to-billing`;
let PORT = 0;
let BASE = "";
const successUrl = () => `${BASE}/back-to-billing`;

let httpServer: ReturnType<typeof serve>;
let httpServer: FreePortServer;
let autumn: Autumn;

beforeAll(() => {
beforeAll(async () => {
// A port the system chooses, held from the start (see serveOnFreePort).
httpServer = await serveOnFreePort();
PORT = httpServer.port;
BASE = `http://localhost:${PORT}`;
const { app, store } = createServer(autumnPlugin, {
port: PORT,
baseUrl: BASE,
Expand All @@ -36,12 +40,12 @@ beforeAll(() => {
},
],
});
httpServer = serve({ fetch: app.fetch, port: PORT });
httpServer.use(app.fetch);
autumn = new Autumn({ secretKey: "am_test_emulate", serverURL: BASE });
});

afterAll(async () => {
await new Promise<void>((resolve) => httpServer.close(() => resolve()));
await httpServer.close();
});

const teamPlan = async (customerId: string) => {
Expand All @@ -67,7 +71,7 @@ describe("autumn emulator: card-required free-trial checkout", () => {
});

it("attach returns a checkout payment URL (card required)", async () => {
const res = await autumn.billing.attach({ customerId: CUSTOMER, planId: "team", successUrl: SUCCESS_URL });
const res = await autumn.billing.attach({ customerId: CUSTOMER, planId: "team", successUrl: successUrl() });
expect(res.paymentUrl, "a checkout URL is returned").toContain("/checkout/");

// Attaching alone must NOT activate the subscription: the customer is still
Expand All @@ -80,7 +84,7 @@ describe("autumn emulator: card-required free-trial checkout", () => {
const { paymentUrl } = await autumn.billing.attach({
customerId: CUSTOMER,
planId: "team",
successUrl: SUCCESS_URL,
successUrl: successUrl(),
});
const sessionId = new URL(paymentUrl!).pathname.split("/").pop()!;

Expand All @@ -93,7 +97,7 @@ describe("autumn emulator: card-required free-trial checkout", () => {
redirect: "manual",
});
expect(completed.status, "completion redirects").toBe(302);
expect(completed.headers.get("location"), "back to the app").toBe(SUCCESS_URL);
expect(completed.headers.get("location"), "back to the app").toBe(successUrl());

// The webhook has not landed yet: the customer is STILL on free. This is the
// exact window in which the billing UI shows the stale plan.
Expand Down
21 changes: 11 additions & 10 deletions packages/@emulators/autumn/src/__tests__/autumn.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { createServer, serve } from "@emulators/core";
import { createServer, serveOnFreePort, type FreePortServer } from "@emulators/core";
import { Autumn } from "autumn-js";

import { autumnPlugin, seedFromConfig } from "../index.js";
import { manifest } from "../manifest.js";

// The real autumn-js SDK (zod-validated responses) against the emulator.

// Not 41874: workos.test.ts's TTL scenario binds PORT + 1 = 41874, and the
// package suites run concurrently under turbo, so sharing it is a latent
// EADDRINUSE flake.
const PORT = 41880;
const BASE = `http://localhost:${PORT}`;
let PORT = 0;
let BASE = "";

let httpServer: ReturnType<typeof serve>;
let httpServer: FreePortServer;
let autumn: Autumn;

beforeAll(() => {
beforeAll(async () => {
// A port the system chooses, held from the start (see serveOnFreePort).
httpServer = await serveOnFreePort();
PORT = httpServer.port;
BASE = `http://localhost:${PORT}`;
const { app, store } = createServer(autumnPlugin, {
port: PORT,
baseUrl: BASE,
Expand All @@ -42,12 +43,12 @@ beforeAll(() => {
{ id: "org_seats", subscriptions: [{ plan_id: "scale", status: "active" }] },
],
});
httpServer = serve({ fetch: app.fetch, port: PORT });
httpServer.use(app.fetch);
autumn = new Autumn({ secretKey: "am_test_emulate", serverURL: BASE });
});

afterAll(async () => {
await new Promise<void>((resolve) => httpServer.close(() => resolve()));
await httpServer.close();
});

describe("autumn emulator with the real autumn-js SDK", () => {
Expand Down
Loading
Loading