|
| 1 | +import type { Meta, StoryObj } from '@storybook/vue3-vite' |
| 2 | + |
| 3 | +import type { SubscriptionInfo } from '@/composables/billing/types' |
| 4 | +import { i18n } from '@/i18n' |
| 5 | +import type { PreviewSubscribeResponse } from '@/platform/workspace/api/workspaceApi' |
| 6 | +import { setBillingContextMock } from '@/storybook/mocks/useBillingContext' |
| 7 | + |
| 8 | +import SubscriptionTransitionPreviewWorkspace from './SubscriptionTransitionPreviewWorkspace.vue' |
| 9 | + |
| 10 | +type PreviewPlanInfo = PreviewSubscribeResponse['new_plan'] |
| 11 | + |
| 12 | +/** |
| 13 | + * The reactivation-disclosure banner on the single-plan change preview: a |
| 14 | + * cancelled-but-not-lapsed subscription resumes silently on any plan change, |
| 15 | + * so the banner discloses that plus the exact charge (and, above the current |
| 16 | + * plan's monthly total, a consent checkbox gating the confirm button). The |
| 17 | + * banner reads `subscription`/`isInitialized` from `useBillingContext`, not |
| 18 | + * from `previewData`, so each story drives it through the Storybook stub |
| 19 | + * (`setBillingContextMock`) rather than props. |
| 20 | + */ |
| 21 | +const meta: Meta<typeof SubscriptionTransitionPreviewWorkspace> = { |
| 22 | + title: 'Components/SubscriptionTransitionPreviewWorkspace', |
| 23 | + component: SubscriptionTransitionPreviewWorkspace, |
| 24 | + tags: ['autodocs'], |
| 25 | + parameters: { layout: 'centered' }, |
| 26 | + decorators: [ |
| 27 | + (story) => ({ |
| 28 | + components: { story }, |
| 29 | + template: |
| 30 | + '<div class="mx-auto flex h-[680px] w-[460px] flex-col rounded-2xl border border-border-default bg-secondary-background p-12"><story /></div>' |
| 31 | + }) |
| 32 | + ] |
| 33 | +} |
| 34 | + |
| 35 | +export default meta |
| 36 | +type Story = StoryObj<typeof meta> |
| 37 | + |
| 38 | +const TODAY = '2026-08-01T00:00:00Z' |
| 39 | +// The date the pre-existing cancellation was set to lapse; still active until |
| 40 | +// then, which is what makes a plan change on this subscription a reactivation. |
| 41 | +const CANCEL_DATE = '2026-08-25T00:00:00Z' |
| 42 | +const NEXT_MONTHLY_RENEWAL = '2026-09-01T00:00:00Z' |
| 43 | +const NEXT_ANNUAL_RENEWAL = '2027-08-01T00:00:00Z' |
| 44 | +const NEXT_MONTHLY_AFTER_CANCEL = '2026-09-25T00:00:00Z' |
| 45 | + |
| 46 | +function plan( |
| 47 | + tier: PreviewPlanInfo['tier'], |
| 48 | + duration: PreviewPlanInfo['duration'], |
| 49 | + priceCents: number, |
| 50 | + periodEnd: string |
| 51 | +): PreviewPlanInfo { |
| 52 | + return { |
| 53 | + slug: `${tier.toLowerCase()}-${duration.toLowerCase()}`, |
| 54 | + tier, |
| 55 | + duration, |
| 56 | + price_cents: priceCents, |
| 57 | + credits_cents: 0, |
| 58 | + seat_summary: { |
| 59 | + seat_count: 1, |
| 60 | + total_cost_cents: priceCents, |
| 61 | + total_credits_cents: 0 |
| 62 | + }, |
| 63 | + period_end: periodEnd |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +function cancelledSubscription( |
| 68 | + tier: NonNullable<SubscriptionInfo['tier']>, |
| 69 | + duration: NonNullable<SubscriptionInfo['duration']> = 'MONTHLY' |
| 70 | +): SubscriptionInfo { |
| 71 | + return { |
| 72 | + isActive: true, |
| 73 | + tier, |
| 74 | + duration, |
| 75 | + planSlug: `${tier.toLowerCase()}-${duration.toLowerCase()}`, |
| 76 | + renewalDate: null, |
| 77 | + endDate: CANCEL_DATE, |
| 78 | + isCancelled: true, |
| 79 | + hasFunds: true |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +const notCancelledSubscription: SubscriptionInfo = { |
| 84 | + isActive: true, |
| 85 | + tier: 'STANDARD', |
| 86 | + duration: 'MONTHLY', |
| 87 | + planSlug: 'standard-monthly', |
| 88 | + renewalDate: NEXT_MONTHLY_RENEWAL, |
| 89 | + endDate: null, |
| 90 | + isCancelled: false, |
| 91 | + hasFunds: true |
| 92 | +} |
| 93 | + |
| 94 | +function story( |
| 95 | + previewData: PreviewSubscribeResponse, |
| 96 | + subscription: SubscriptionInfo |
| 97 | +): Story { |
| 98 | + return { |
| 99 | + args: { previewData }, |
| 100 | + beforeEach() { |
| 101 | + // Dates render through both a local Intl formatter and vue-i18n's n(), |
| 102 | + // so pin the locale rather than inherit the developer's. |
| 103 | + i18n.global.locale.value = 'en' |
| 104 | + setBillingContextMock({ subscription }) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Ordinary upgrade, subscription not cancelled — the reactivation banner |
| 111 | + * must not leak into a normal plan change. |
| 112 | + */ |
| 113 | +export const NotCancelled: Story = story( |
| 114 | + { |
| 115 | + allowed: true, |
| 116 | + transition_type: 'upgrade', |
| 117 | + effective_at: TODAY, |
| 118 | + is_immediate: true, |
| 119 | + cost_today_cents: 1500, |
| 120 | + cost_next_period_cents: 3500, |
| 121 | + credits_today_cents: 0, |
| 122 | + credits_next_period_cents: 0, |
| 123 | + current_plan: plan('STANDARD', 'MONTHLY', 2000, NEXT_MONTHLY_RENEWAL), |
| 124 | + new_plan: plan('CREATOR', 'MONTHLY', 3500, NEXT_MONTHLY_RENEWAL) |
| 125 | + } satisfies PreviewSubscribeResponse, |
| 126 | + notCancelledSubscription |
| 127 | +) |
| 128 | + |
| 129 | +/** |
| 130 | + * Cancelled, immediate upgrade — exact-cents charge ($54.54), guarding the |
| 131 | + * regression where this path rounded the charge shown to the user. |
| 132 | + */ |
| 133 | +export const ReactivatingUpgrade: Story = story( |
| 134 | + { |
| 135 | + allowed: true, |
| 136 | + transition_type: 'upgrade', |
| 137 | + effective_at: TODAY, |
| 138 | + is_immediate: true, |
| 139 | + cost_today_cents: 5454, |
| 140 | + cost_next_period_cents: 10_000, |
| 141 | + credits_today_cents: 0, |
| 142 | + credits_next_period_cents: 0, |
| 143 | + current_plan: plan('CREATOR', 'MONTHLY', 3500, CANCEL_DATE), |
| 144 | + new_plan: plan('PRO', 'MONTHLY', 10_000, NEXT_MONTHLY_RENEWAL) |
| 145 | + } satisfies PreviewSubscribeResponse, |
| 146 | + cancelledSubscription('CREATOR') |
| 147 | +) |
| 148 | + |
| 149 | +/** |
| 150 | + * Cancelled, scheduled downgrade — $0 today. The most important state: no |
| 151 | + * money moves, so the copy alone must make the reactivation unmissable. |
| 152 | + */ |
| 153 | +export const ReactivatingDowngrade: Story = story( |
| 154 | + { |
| 155 | + allowed: true, |
| 156 | + transition_type: 'downgrade', |
| 157 | + effective_at: CANCEL_DATE, |
| 158 | + is_immediate: false, |
| 159 | + cost_today_cents: 0, |
| 160 | + cost_next_period_cents: 3500, |
| 161 | + credits_today_cents: 0, |
| 162 | + credits_next_period_cents: 0, |
| 163 | + current_plan: plan('PRO', 'MONTHLY', 10_000, CANCEL_DATE), |
| 164 | + new_plan: plan('CREATOR', 'MONTHLY', 3500, NEXT_MONTHLY_AFTER_CANCEL) |
| 165 | + } satisfies PreviewSubscribeResponse, |
| 166 | + cancelledSubscription('PRO') |
| 167 | +) |
| 168 | + |
| 169 | +/** Cancelled, monthly-to-annual — the full year is billed today. */ |
| 170 | +export const ReactivatingMonthlyToAnnual: Story = story( |
| 171 | + { |
| 172 | + allowed: true, |
| 173 | + transition_type: 'duration_change', |
| 174 | + effective_at: TODAY, |
| 175 | + is_immediate: true, |
| 176 | + cost_today_cents: 33_600, |
| 177 | + cost_next_period_cents: 33_600, |
| 178 | + credits_today_cents: 0, |
| 179 | + credits_next_period_cents: 0, |
| 180 | + current_plan: plan('CREATOR', 'MONTHLY', 3500, CANCEL_DATE), |
| 181 | + new_plan: plan('CREATOR', 'ANNUAL', 33_600, NEXT_ANNUAL_RENEWAL) |
| 182 | + } satisfies PreviewSubscribeResponse, |
| 183 | + cancelledSubscription('CREATOR') |
| 184 | +) |
| 185 | + |
| 186 | +/** |
| 187 | + * Cancelled, annual-to-monthly — the other cadence direction, which used to |
| 188 | + * wrongly show the annual copy and now has its own wording. |
| 189 | + */ |
| 190 | +export const ReactivatingAnnualToMonthly: Story = story( |
| 191 | + { |
| 192 | + allowed: true, |
| 193 | + transition_type: 'duration_change', |
| 194 | + effective_at: TODAY, |
| 195 | + is_immediate: true, |
| 196 | + cost_today_cents: 1234, |
| 197 | + cost_next_period_cents: 3500, |
| 198 | + credits_today_cents: 0, |
| 199 | + credits_next_period_cents: 0, |
| 200 | + current_plan: plan('CREATOR', 'ANNUAL', 33_600, CANCEL_DATE), |
| 201 | + new_plan: plan('CREATOR', 'MONTHLY', 3500, NEXT_MONTHLY_RENEWAL) |
| 202 | + } satisfies PreviewSubscribeResponse, |
| 203 | + cancelledSubscription('CREATOR', 'ANNUAL') |
| 204 | +) |
| 205 | + |
| 206 | +/** |
| 207 | + * Cancelled, charge above the current plan's monthly total — the consent |
| 208 | + * checkbox renders and the confirm button stays disabled until it's ticked. |
| 209 | + */ |
| 210 | +export const ReactivatingAboveThreshold: Story = story( |
| 211 | + { |
| 212 | + allowed: true, |
| 213 | + transition_type: 'upgrade', |
| 214 | + effective_at: TODAY, |
| 215 | + is_immediate: true, |
| 216 | + cost_today_cents: 8000, |
| 217 | + cost_next_period_cents: 10_000, |
| 218 | + credits_today_cents: 0, |
| 219 | + credits_next_period_cents: 0, |
| 220 | + current_plan: plan('STANDARD', 'MONTHLY', 2000, CANCEL_DATE), |
| 221 | + new_plan: plan('PRO', 'MONTHLY', 10_000, NEXT_MONTHLY_RENEWAL) |
| 222 | + } satisfies PreviewSubscribeResponse, |
| 223 | + cancelledSubscription('STANDARD') |
| 224 | +) |
0 commit comments