|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * Lock-order regression guard: the paid-org join billing transaction must lock |
| 5 | + * the personal Pro subscription BEFORE userStats, matching |
| 6 | + * restoreUserProSubscription's subscription → userStats order. Snapshotting |
| 7 | + * userStats before locking the subscription inverts that pair and deadlocks a |
| 8 | + * concurrent Pro restore for the same user. |
| 9 | + */ |
| 10 | +import { subscription as subscriptionTable, userStats } from '@sim/db/schema' |
| 11 | +import { dbChainMock, dbChainMockFns, resetDbChainMock } from '@sim/testing' |
| 12 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 13 | +import { reapplyPaidOrgJoinBillingForExistingMember } from '@/lib/billing/organizations/membership' |
| 14 | + |
| 15 | +vi.mock('@sim/db', () => dbChainMock) |
| 16 | + |
| 17 | +vi.mock('@/lib/core/outbox/service', () => ({ |
| 18 | + enqueueOutboxEvent: vi.fn(), |
| 19 | +})) |
| 20 | + |
| 21 | +/** |
| 22 | + * A superset row that satisfies every read in the join path: a paid org sub, a |
| 23 | + * still-active personal Pro to pause, non-zero usage to snapshot, and zero |
| 24 | + * storage (so the conditional org storage-transfer write is skipped — the org |
| 25 | + * lock under test is the canonical pre-lock, not the storage update). |
| 26 | + */ |
| 27 | +const GENERIC_ROW = { |
| 28 | + id: 'sub-1', |
| 29 | + plan: 'team', |
| 30 | + referenceId: 'user-1', |
| 31 | + status: 'active', |
| 32 | + cancelAtPeriodEnd: false, |
| 33 | + stripeSubscriptionId: 'stripe-1', |
| 34 | + currentPeriodCost: '5', |
| 35 | + proPeriodCostSnapshot: '0', |
| 36 | + storageUsedBytes: 0, |
| 37 | +} |
| 38 | + |
| 39 | +type LockOp = { op: 'lock' | 'update' | 'insert'; table: unknown } |
| 40 | + |
| 41 | +function createRecordingTx() { |
| 42 | + const ops: LockOp[] = [] |
| 43 | + const select = () => { |
| 44 | + const ctx: { table: unknown } = { table: undefined } |
| 45 | + const chain = { |
| 46 | + from: (table: unknown) => { |
| 47 | + ctx.table = table |
| 48 | + return chain |
| 49 | + }, |
| 50 | + where: () => chain, |
| 51 | + for: () => { |
| 52 | + ops.push({ op: 'lock', table: ctx.table }) |
| 53 | + return chain |
| 54 | + }, |
| 55 | + limit: async () => [GENERIC_ROW], |
| 56 | + } |
| 57 | + return chain |
| 58 | + } |
| 59 | + const tx = { |
| 60 | + select, |
| 61 | + update: (table: unknown) => ({ |
| 62 | + set: () => ({ |
| 63 | + where: async () => { |
| 64 | + ops.push({ op: 'update', table }) |
| 65 | + }, |
| 66 | + }), |
| 67 | + }), |
| 68 | + insert: (table: unknown) => ({ |
| 69 | + values: async () => { |
| 70 | + ops.push({ op: 'insert', table }) |
| 71 | + }, |
| 72 | + }), |
| 73 | + execute: async () => [], |
| 74 | + } |
| 75 | + return { tx, ops } |
| 76 | +} |
| 77 | + |
| 78 | +describe('paid-org join billing lock ordering', () => { |
| 79 | + beforeEach(() => { |
| 80 | + vi.clearAllMocks() |
| 81 | + resetDbChainMock() |
| 82 | + }) |
| 83 | + |
| 84 | + it('locks the personal subscription before mutating userStats', async () => { |
| 85 | + const { tx, ops } = createRecordingTx() |
| 86 | + dbChainMockFns.transaction.mockImplementation(async (cb: (t: unknown) => unknown) => cb(tx)) |
| 87 | + |
| 88 | + await reapplyPaidOrgJoinBillingForExistingMember('user-1', 'org-1') |
| 89 | + |
| 90 | + const firstUserStatsUpdate = ops.findIndex((o) => o.op === 'update' && o.table === userStats) |
| 91 | + const subscriptionLock = ops.findIndex((o) => o.op === 'lock' && o.table === subscriptionTable) |
| 92 | + |
| 93 | + expect(firstUserStatsUpdate).toBeGreaterThanOrEqual(0) |
| 94 | + expect(subscriptionLock).toBeGreaterThanOrEqual(0) |
| 95 | + expect(subscriptionLock).toBeLessThan(firstUserStatsUpdate) |
| 96 | + }) |
| 97 | +}) |
0 commit comments