feat(billing): setup sessions use the one-time trial (N11) through the TrialPort

A team that setup creates or picks gets the trial when it binds, with the email,
GitHub user and ip abuse keys. Teams that can't get one go through T3 and
Checkout.
This commit is contained in:
Neil
2026-11-10 06:27:45 -08:00
parent 7c5c00331f
commit 5a877a01d7
3 changed files with 63 additions and 2 deletions
@@ -1,6 +1,7 @@
// The setup session's view of the one-time trial (N11, owned by src/trial.ts). Kept behind this
// port so the setup flow reads one shape whatever the trial module returns.
import type { Env } from './env'
import { TRIAL_INCLUDED_SECONDS, grantTrial, trialRow, trialState } from './trial'
export type SetupTrial = { includedSeconds: number; usedSeconds: number; exhausted: boolean }
@@ -29,3 +30,36 @@ export const noTrialPort: TrialPort = {
current: async () => null,
grant: async () => ({ ok: false, reason: 'trial_unavailable' })
}
/** The trial module (N11) as the setup session sees it. */
export const billingTrialPort: TrialPort = {
offeredSeconds: () => TRIAL_INCLUDED_SECONDS,
async current(env, accountId) {
const row = await trialRow(env.BILLING_DB, accountId)
if (!row || row.revoked_at !== null) {
return null
}
const state = trialState(row)
return { includedSeconds: state.includedSeconds, usedSeconds: state.usedSeconds, exhausted: state.exhausted }
},
async grant(env, input, now) {
const outcome = await grantTrial(
env,
{
accountId: input.accountId,
userId: input.userId,
...(input.email ? { email: input.email } : {}),
...(input.emailVerified !== undefined ? { emailVerified: input.emailVerified } : {}),
...(input.githubUserId ? { githubUserId: String(input.githubUserId) } : {}),
...(input.githubOwnerId ? { githubOwnerId: String(input.githubOwnerId) } : {}),
...(input.ip ? { ip: input.ip } : {})
},
now
)
if (!outcome.ok) {
return { ok: false, reason: outcome.reason }
}
const state = outcome.trial
return { ok: true, trial: { includedSeconds: state.includedSeconds, usedSeconds: state.usedSeconds, exhausted: state.exhausted } }
}
}
+2 -2
View File
@@ -74,7 +74,7 @@ import {
setupGithubInstall,
setupSessionView
} from './cloud-setup'
import { noTrialPort, type TrialPort } from './cloud-setup-trial'
import { billingTrialPort, type TrialPort } from './cloud-setup-trial'
import { grantTrial, isTeamAccount, recordTrialGithub, trialOffer, type TrialInput } from './trial'
import type { BillingCaller } from './auth'
@@ -286,7 +286,7 @@ export async function handleRequest(request: Request, env: Env, ctx: ExecutionCo
const handled = await handleCloudSetup(request, env, url, now, {
stripe,
membership,
trial: deps.trial ?? noTrialPort,
trial: deps.trial ?? billingTrialPort,
fetch: deps.fetch ?? fetch
})
if (handled) {
@@ -249,3 +249,30 @@ describe('team-first checkout (O1/N4)', () => {
expect(call.form.get('success_url')).toContain(`/cloud/setup/next?session=${id}`)
})
})
describe('setup session with the real trial module', () => {
it('grants the one-time trial to the team setup creates, once', async () => {
const h = await createHarness({ MEMBERSHIP_SYNC_TOKEN: SYNC, CLOUD_SETUP_PAGES_URL: 'https://auth.test' })
const sync = { authorization: `Bearer ${SYNC}` }
const auth = { authorization: `Bearer ${await h.desktopToken({ sub: 'u1', email: 'neil@acme.dev' })}` }
const offers = await h.call('GET', '/v1/cloud-setup/offers')
expect(offers.body.trial).toEqual({ includedSeconds: 7200 })
const id = (await h.call('POST', '/v1/cloud-setup/sessions', { body: { needs: ['plan'] }, headers: auth })).body.id
const bound = await h.call('POST', `/v1/internal/cloud-setup/sessions/${id}/team`, {
body: { userId: 'u1', orgId: 'o_new', name: 'acme', created: true, email: 'neil@acme.dev' },
headers: sync
})
expect(bound.body).toEqual({ ok: true, trial: 'granted' })
const view = (await h.call('GET', `/v1/cloud-setup/sessions/${id}`, { headers: auth })).body
expect(view.plan).toBe('trial')
expect(view.trial).toEqual({ includedSeconds: 7200, usedSeconds: 0, exhausted: false })
expect(view.finished).toBe(true)
// Same user, second new team: the per-user abuse key refuses it.
const second = (await h.call('POST', '/v1/cloud-setup/sessions', { body: { needs: ['plan'] }, headers: auth })).body.id
const again = await h.call('POST', `/v1/internal/cloud-setup/sessions/${second}/team`, {
body: { userId: 'u1', orgId: 'o_other', name: 'acme-2', created: true, email: 'neil@acme.dev' },
headers: sync
})
expect(again.body).toEqual({ ok: true, trial: 'unavailable' })
})
})