Files
simstudioai--sim/apps/sim/lib/billing/organizations/seats.test.ts
T
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

212 lines
6.2 KiB
TypeScript

/**
* @vitest-environment node
*/
import { auditMock } from '@sim/testing'
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { mockSyncSubscriptionUsageLimits, enqueueMock, setMock, queryQueue, mockFeatureFlags } =
vi.hoisted(() => ({
mockSyncSubscriptionUsageLimits: vi.fn(),
enqueueMock: vi.fn(),
setMock: vi.fn(),
queryQueue: { value: [] as unknown[][] },
mockFeatureFlags: { isBillingEnabled: true },
}))
vi.mock('@sim/db', () => {
const makeSelectChain = () => {
const chain: Record<string, unknown> = {}
chain.from = () => chain
chain.where = () => chain
chain.for = () => chain
chain.limit = () => Promise.resolve(queryQueue.value.shift() ?? [])
chain.then = (resolve: (rows: unknown[]) => unknown, reject?: (e: unknown) => unknown) =>
Promise.resolve(queryQueue.value.shift() ?? []).then(resolve, reject)
return chain
}
const update = () => ({
set: (values: Record<string, unknown>) => {
setMock(values)
return { where: () => Promise.resolve([]) }
},
})
const txMock = { select: () => makeSelectChain(), update }
const dbMock = {
select: () => makeSelectChain(),
update,
transaction: async (cb: (tx: typeof txMock) => Promise<unknown>) => cb(txMock),
}
return { db: dbMock }
})
vi.mock('@/lib/billing/organization', () => ({
syncSubscriptionUsageLimits: mockSyncSubscriptionUsageLimits,
}))
vi.mock('@/lib/core/outbox/service', () => ({
enqueueOutboxEvent: enqueueMock,
}))
vi.mock('@/lib/billing/webhooks/outbox-handlers', () => ({
OUTBOX_EVENT_TYPES: {
STRIPE_SYNC_SUBSCRIPTION_SEATS: 'stripe.sync-subscription-seats',
},
}))
vi.mock('@/lib/core/config/env-flags', () => ({
get isBillingEnabled() {
return mockFeatureFlags.isBillingEnabled
},
}))
vi.mock('@sim/audit', () => auditMock)
import { reconcileOrganizationSeats } from '@/lib/billing/organizations/seats'
const teamSub = {
id: 'sub-1',
plan: 'team_6000',
status: 'active',
seats: 1,
stripeSubscriptionId: 'stripe_sub',
}
describe('reconcileOrganizationSeats', () => {
beforeEach(() => {
vi.clearAllMocks()
queryQueue.value = []
enqueueMock.mockResolvedValue('evt-1')
mockFeatureFlags.isBillingEnabled = true
})
it('grows seats to the member count and enqueues a Stripe sync', async () => {
queryQueue.value = [[teamSub], [{ value: 2 }]]
const result = await reconcileOrganizationSeats({
organizationId: 'org-1',
reason: 'member-accepted-invite',
})
expect(result).toEqual({
changed: true,
previousSeats: 1,
seats: 2,
reason: undefined,
outboxEventId: 'evt-1',
})
expect(setMock).toHaveBeenCalledWith({ seats: 2 })
expect(enqueueMock).toHaveBeenCalledWith(expect.anything(), 'stripe.sync-subscription-seats', {
subscriptionId: 'sub-1',
reason: 'member-accepted-invite',
})
expect(mockSyncSubscriptionUsageLimits).toHaveBeenCalledWith(
expect.objectContaining({ id: 'sub-1', referenceId: 'org-1', seats: 2 })
)
})
it('still records the seat audit when the post-commit usage-limit sync fails', async () => {
queryQueue.value = [[teamSub], [{ value: 2 }]]
mockSyncSubscriptionUsageLimits.mockRejectedValueOnce(new Error('sync unavailable'))
const result = await reconcileOrganizationSeats({
organizationId: 'org-1',
reason: 'member-accepted-invite',
actorId: 'user-1',
})
expect(result.changed).toBe(true)
expect(setMock).toHaveBeenCalledWith({ seats: 2 })
expect(auditMock.recordAudit).toHaveBeenCalledWith(
expect.objectContaining({
actorId: 'user-1',
action: auditMock.AuditAction.ORG_SEAT_PROVISIONED,
resourceId: 'org-1',
})
)
})
it('reduces seats to the member count on removal', async () => {
queryQueue.value = [[{ ...teamSub, seats: 3 }], [{ value: 2 }]]
const result = await reconcileOrganizationSeats({
organizationId: 'org-1',
reason: 'member-removed',
})
expect(result.changed).toBe(true)
expect(result.seats).toBe(2)
expect(setMock).toHaveBeenCalledWith({ seats: 2 })
expect(enqueueMock).toHaveBeenCalled()
})
it('is a no-op when seats already match the member count', async () => {
queryQueue.value = [[{ ...teamSub, seats: 2 }], [{ value: 2 }]]
const result = await reconcileOrganizationSeats({
organizationId: 'org-1',
reason: 'member-removed',
})
expect(result).toEqual({
changed: false,
previousSeats: 2,
seats: 2,
reason: undefined,
outboxEventId: undefined,
})
expect(setMock).not.toHaveBeenCalled()
expect(enqueueMock).not.toHaveBeenCalled()
expect(mockSyncSubscriptionUsageLimits).not.toHaveBeenCalled()
})
it('never drops below one seat', async () => {
queryQueue.value = [[{ ...teamSub, seats: 3 }], [{ value: 0 }]]
const result = await reconcileOrganizationSeats({
organizationId: 'org-1',
reason: 'member-removed',
})
expect(result.seats).toBe(1)
expect(setMock).toHaveBeenCalledWith({ seats: 1 })
})
it('skips non-Team subscriptions', async () => {
queryQueue.value = [[{ ...teamSub, plan: 'pro_6000' }]]
const result = await reconcileOrganizationSeats({
organizationId: 'org-1',
reason: 'member-accepted-invite',
})
expect(result.changed).toBe(false)
expect(result.reason).toMatch(/Team/)
expect(setMock).not.toHaveBeenCalled()
expect(enqueueMock).not.toHaveBeenCalled()
})
it('skips when the organization has no usable subscription', async () => {
queryQueue.value = [[]]
const result = await reconcileOrganizationSeats({
organizationId: 'org-1',
reason: 'member-accepted-invite',
})
expect(result).toEqual({ changed: false, reason: 'No active subscription found' })
expect(enqueueMock).not.toHaveBeenCalled()
})
it('no-ops when billing is disabled', async () => {
mockFeatureFlags.isBillingEnabled = false
const result = await reconcileOrganizationSeats({
organizationId: 'org-1',
reason: 'member-accepted-invite',
})
expect(result).toEqual({ changed: false, reason: 'Billing is not enabled' })
expect(enqueueMock).not.toHaveBeenCalled()
})
})