Ajoute une couche end-to-end où un Chromium drive la SPA + API ensemble
contre une DB Postgres séparée, avec Stripe entièrement mocké au niveau
API. 6 scénarios couverts (signup + onboarding + 4 sur le billing trial).
Architecture :
- DB `rubis_test_e2e` séparée, TRUNCATE entre tests (~50 ms reset)
- Routes test-only `/__test__/*` gated par NODE_ENV=test_e2e
(reset, install Stripe mock, fire webhook, lire org state, last-org)
- Stripe mocké via __setStripeForTests — pas d'appel réseau
- Playwright spawn API + SPA automatiquement (webServer config)
- CORS étendu à test_e2e pour le cross-origin localhost:5173 → :3333
Scénarios :
- signup.spec.ts : signup → onboarding 3 étapes → dashboard (assert rubis hero)
- billing-trial.spec.ts :
• démarrer essai 14j → redirect Stripe Checkout (mock)
• fallback Free 2 factures continue l'onboarding
• webhook checkout.completed → org en trialing + trial_ends_at
• retour ?trial=cancel après abandon
• inspection DB : stripeCustomerId posé après start-trial
Scripts :
- pnpm e2e (headless)
- pnpm e2e:headed (Chromium visible)
- pnpm e2e:ui (mode interactif Playwright)
- pnpm e2e:setup (crée + migre rubis_test_e2e via docker exec)
Documentation : docs/tech/e2e-tests.md — architecture, scénarios,
extensions, CI, troubleshooting.
Limites assumées :
- L'UI Stripe Checkout (3DS, formulaire CB) n'est pas testée — externe.
Pour ça : playbook manuel docs/tech/stripe-trial-e2e-playbook.md.
- Le rendu du banner "Essai Pro" n'est pas asserté en E2E à cause de
TanStack Query staleTime — couvert par les tests vitest à la place.
État global du chantier billing : 127 tests japa + 6 Playwright + 11
vitest = couverture multi-niveaux.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
97 lines
3.2 KiB
TypeScript
97 lines
3.2 KiB
TypeScript
import { request, type APIRequestContext } from '@playwright/test'
|
|
|
|
/**
|
|
* Helpers HTTP pour les tests E2E. Tape directement l'API Adonis pour :
|
|
* - reset la DB entre tests
|
|
* - installer / configurer le mock Stripe
|
|
* - simuler la livraison de webhooks Stripe
|
|
* - inspecter l'état d'une org en DB
|
|
*
|
|
* Tous les endpoints touchés ici sont gated par `NODE_ENV=test_e2e` côté
|
|
* API (cf. apps/api/app/controllers/test_e2e_controller.ts).
|
|
*/
|
|
|
|
const API_URL = process.env.E2E_API_URL ?? 'http://localhost:3333'
|
|
|
|
let _ctx: APIRequestContext | null = null
|
|
|
|
async function ctx(): Promise<APIRequestContext> {
|
|
if (_ctx) return _ctx
|
|
_ctx = await request.newContext({ baseURL: API_URL })
|
|
return _ctx
|
|
}
|
|
|
|
/**
|
|
* Vide les tables applicatives + ré-installe un Stripe mock par défaut.
|
|
* À appeler en `test.beforeEach`.
|
|
*/
|
|
export async function resetDb(): Promise<void> {
|
|
const c = await ctx()
|
|
const r = await c.post('/__test__/reset')
|
|
if (!r.ok()) throw new Error(`reset failed: ${r.status()}`)
|
|
}
|
|
|
|
/**
|
|
* Override le scenario du mock Stripe — `'trial_decline'` simule un user
|
|
* qui ferme Stripe Checkout sans valider (redirige vers /onboarding/billing
|
|
* avec ?trial=cancel). Default = happy path.
|
|
*/
|
|
export async function installStripeMock(scenario?: string): Promise<void> {
|
|
const c = await ctx()
|
|
await c.post('/__test__/stripe/mock', { data: { scenario } })
|
|
}
|
|
|
|
/**
|
|
* Simule la livraison d'un webhook Stripe — déclenche le dispatcher
|
|
* applicatif comme si Stripe avait POST. Évite à Playwright de devoir
|
|
* signer manuellement les payloads.
|
|
*
|
|
* Exemple : await fireStripeWebhook({ type: 'customer.subscription.trial_will_end',
|
|
* data: { object: { customer: 'cus_e2e_mock', ... } } })
|
|
*/
|
|
export async function fireStripeWebhook(event: {
|
|
type: string
|
|
data: { object: Record<string, unknown> }
|
|
}): Promise<void> {
|
|
const c = await ctx()
|
|
const r = await c.post('/__test__/stripe/webhook', { data: event })
|
|
if (!r.ok()) throw new Error(`fire webhook failed: ${r.status()} ${await r.text()}`)
|
|
}
|
|
|
|
/**
|
|
* Lit l'état d'une org directement en DB. Utile pour asserter post-action
|
|
* sans naviguer dans le SPA.
|
|
*/
|
|
export async function getOrgState(orgId: string): Promise<OrgState | null> {
|
|
const c = await ctx()
|
|
const r = await c.get(`/__test__/state/org/${orgId}`)
|
|
if (!r.ok()) throw new Error(`getOrgState failed: ${r.status()}`)
|
|
const json = (await r.json()) as { data: OrgState | null }
|
|
return json.data
|
|
}
|
|
|
|
/**
|
|
* Retourne la dernière org créée (toutes confondues). Comme `resetDb`
|
|
* est appelé en `beforeEach`, la dernière est forcément celle du
|
|
* scénario en cours — pratique pour récupérer l'orgId après signup
|
|
* sans avoir à plonger dans le SPA pour le Bearer token.
|
|
*/
|
|
export async function getLastOrg(): Promise<OrgState | null> {
|
|
const c = await ctx()
|
|
const r = await c.get('/__test__/state/last-org')
|
|
if (!r.ok()) throw new Error(`getLastOrg failed: ${r.status()}`)
|
|
const json = (await r.json()) as { data: OrgState | null }
|
|
return json.data
|
|
}
|
|
|
|
export type OrgState = {
|
|
id: string
|
|
name: string
|
|
plan: 'free' | 'pro' | 'business'
|
|
subscription_status: string | null
|
|
stripe_customer_id: string | null
|
|
stripe_subscription_id: string | null
|
|
trial_ends_at: string | null
|
|
grace_period_ends_at: string | null
|
|
}
|