d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
98 lines
3.0 KiB
TypeScript
98 lines
3.0 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*
|
|
* Lock-order regression guard: the paid-org join billing transaction must lock
|
|
* the personal Pro subscription BEFORE userStats, matching
|
|
* restoreUserProSubscription's subscription → userStats order. Snapshotting
|
|
* userStats before locking the subscription inverts that pair and deadlocks a
|
|
* concurrent Pro restore for the same user.
|
|
*/
|
|
import { subscription as subscriptionTable, userStats } from '@sim/db/schema'
|
|
import { dbChainMock, dbChainMockFns, resetDbChainMock } from '@sim/testing'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { reapplyPaidOrgJoinBillingForExistingMember } from '@/lib/billing/organizations/membership'
|
|
|
|
vi.mock('@sim/db', () => dbChainMock)
|
|
|
|
vi.mock('@/lib/core/outbox/service', () => ({
|
|
enqueueOutboxEvent: vi.fn(),
|
|
}))
|
|
|
|
/**
|
|
* A superset row that satisfies every read in the join path: a paid org sub, a
|
|
* still-active personal Pro to pause, non-zero usage to snapshot, and zero
|
|
* storage (so the conditional org storage-transfer write is skipped — the org
|
|
* lock under test is the canonical pre-lock, not the storage update).
|
|
*/
|
|
const GENERIC_ROW = {
|
|
id: 'sub-1',
|
|
plan: 'team',
|
|
referenceId: 'user-1',
|
|
status: 'active',
|
|
cancelAtPeriodEnd: false,
|
|
stripeSubscriptionId: 'stripe-1',
|
|
currentPeriodCost: '5',
|
|
proPeriodCostSnapshot: '0',
|
|
storageUsedBytes: 0,
|
|
}
|
|
|
|
type LockOp = { op: 'lock' | 'update' | 'insert'; table: unknown }
|
|
|
|
function createRecordingTx() {
|
|
const ops: LockOp[] = []
|
|
const select = () => {
|
|
const ctx: { table: unknown } = { table: undefined }
|
|
const chain = {
|
|
from: (table: unknown) => {
|
|
ctx.table = table
|
|
return chain
|
|
},
|
|
where: () => chain,
|
|
for: () => {
|
|
ops.push({ op: 'lock', table: ctx.table })
|
|
return chain
|
|
},
|
|
limit: async () => [GENERIC_ROW],
|
|
}
|
|
return chain
|
|
}
|
|
const tx = {
|
|
select,
|
|
update: (table: unknown) => ({
|
|
set: () => ({
|
|
where: async () => {
|
|
ops.push({ op: 'update', table })
|
|
},
|
|
}),
|
|
}),
|
|
insert: (table: unknown) => ({
|
|
values: async () => {
|
|
ops.push({ op: 'insert', table })
|
|
},
|
|
}),
|
|
execute: async () => [],
|
|
}
|
|
return { tx, ops }
|
|
}
|
|
|
|
describe('paid-org join billing lock ordering', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
resetDbChainMock()
|
|
})
|
|
|
|
it('locks the personal subscription before mutating userStats', async () => {
|
|
const { tx, ops } = createRecordingTx()
|
|
dbChainMockFns.transaction.mockImplementation(async (cb: (t: unknown) => unknown) => cb(tx))
|
|
|
|
await reapplyPaidOrgJoinBillingForExistingMember('user-1', 'org-1')
|
|
|
|
const firstUserStatsUpdate = ops.findIndex((o) => o.op === 'update' && o.table === userStats)
|
|
const subscriptionLock = ops.findIndex((o) => o.op === 'lock' && o.table === subscriptionTable)
|
|
|
|
expect(firstUserStatsUpdate).toBeGreaterThanOrEqual(0)
|
|
expect(subscriptionLock).toBeGreaterThanOrEqual(0)
|
|
expect(subscriptionLock).toBeLessThan(firstUserStatsUpdate)
|
|
})
|
|
})
|