Files
simstudioai--sim/apps/sim/lib/table/billing.test.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
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
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

191 lines
6.8 KiB
TypeScript

/**
* @vitest-environment node
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
const {
mockGetWorkspaceBilledAccountUserId,
mockGetHighestPrioritySubscription,
mockGetPlanTypeForLimits,
mockGetTablePlanLimits,
} = vi.hoisted(() => ({
mockGetWorkspaceBilledAccountUserId: vi.fn(),
mockGetHighestPrioritySubscription: vi.fn(),
mockGetPlanTypeForLimits: vi.fn(),
mockGetTablePlanLimits: vi.fn(),
}))
vi.mock('@/lib/workspaces/utils', () => ({
getWorkspaceBilledAccountUserId: mockGetWorkspaceBilledAccountUserId,
}))
vi.mock('@/lib/billing/core/subscription', () => ({
getHighestPrioritySubscription: mockGetHighestPrioritySubscription,
}))
vi.mock('@/lib/billing/plan-helpers', () => ({
getPlanTypeForLimits: mockGetPlanTypeForLimits,
}))
vi.mock('@/lib/table/constants', () => ({
getTablePlanLimits: mockGetTablePlanLimits,
}))
import {
assertRowCapacity,
getMaxRowsPerTable,
getWorkspaceTableLimits,
notifyTableRowUsage,
TableRowLimitError,
wouldExceedRowLimit,
} from '@/lib/table/billing'
const LIMITS = {
free: { maxTables: 3, maxRowsPerTable: 1000 },
pro: { maxTables: 25, maxRowsPerTable: 5000 },
team: { maxTables: 100, maxRowsPerTable: 10000 },
enterprise: { maxTables: 10000, maxRowsPerTable: 1000000 },
}
// The limits cache is module-level and keyed by workspaceId; a fresh id per test
// keeps one test's cached value from leaking into the next.
let wsCounter = 0
const nextWorkspaceId = () => `ws-${++wsCounter}`
beforeEach(() => {
vi.clearAllMocks()
mockGetTablePlanLimits.mockReturnValue(LIMITS)
mockGetWorkspaceBilledAccountUserId.mockResolvedValue('billed-user')
mockGetHighestPrioritySubscription.mockResolvedValue({ plan: 'pro' })
mockGetPlanTypeForLimits.mockReturnValue('pro')
})
describe('getWorkspaceTableLimits', () => {
it('returns the limits for the workspace subscription plan', async () => {
expect(await getWorkspaceTableLimits(nextWorkspaceId())).toEqual(LIMITS.pro)
})
it('caches the resolved limits within the TTL', async () => {
const ws = nextWorkspaceId()
await getWorkspaceTableLimits(ws)
await getWorkspaceTableLimits(ws)
expect(mockGetWorkspaceBilledAccountUserId).toHaveBeenCalledTimes(1)
})
it('returns free-tier limits when the workspace has no billed account', async () => {
mockGetWorkspaceBilledAccountUserId.mockResolvedValueOnce(null)
expect(await getWorkspaceTableLimits(nextWorkspaceId())).toEqual(LIMITS.free)
})
it('falls back to free tier without caching when the lookup throws', async () => {
const ws = nextWorkspaceId()
mockGetWorkspaceBilledAccountUserId.mockRejectedValueOnce(new Error('db down'))
expect(await getWorkspaceTableLimits(ws)).toEqual(LIMITS.free)
// The fallback is never cached, so the next call re-attempts and resolves the real plan.
expect(await getWorkspaceTableLimits(ws)).toEqual(LIMITS.pro)
})
it('stays bounded under a burst of distinct all-fresh workspaces', async () => {
// Far more distinct workspaces than the cap, all within one TTL window. The Map
// must not grow without limit; eviction keeps it at/under the ceiling.
for (let i = 0; i < 6_000; i++) {
await getWorkspaceTableLimits(`burst-${i}`)
}
// Re-resolving an early (evicted) workspace must re-hit the billing lookup.
mockGetWorkspaceBilledAccountUserId.mockClear()
await getWorkspaceTableLimits('burst-0')
expect(mockGetWorkspaceBilledAccountUserId).toHaveBeenCalledTimes(1)
})
})
describe('getMaxRowsPerTable', () => {
it('returns the plan maxRowsPerTable', async () => {
expect(await getMaxRowsPerTable(nextWorkspaceId())).toBe(5000)
})
})
describe('wouldExceedRowLimit', () => {
it('is false under the limit and at the limit exactly', () => {
expect(wouldExceedRowLimit(1000, 10, 5)).toBe(false)
expect(wouldExceedRowLimit(1000, 999, 1)).toBe(false)
})
it('is true when the sum crosses the limit', () => {
expect(wouldExceedRowLimit(1000, 1000, 1)).toBe(true)
})
it('treats a negative limit as unlimited', () => {
expect(wouldExceedRowLimit(-1, 10_000_000, 1)).toBe(false)
})
it('treats a zero limit as no rows allowed', () => {
expect(wouldExceedRowLimit(0, 0, 1)).toBe(true)
})
})
describe('assertRowCapacity', () => {
it('returns the resolved limit when the write stays under it', async () => {
await expect(
assertRowCapacity({ workspaceId: nextWorkspaceId(), currentRowCount: 10, addedRows: 5 })
).resolves.toBe(5000)
})
it('allows reaching the limit exactly and returns it', async () => {
await expect(
assertRowCapacity({ workspaceId: nextWorkspaceId(), currentRowCount: 4999, addedRows: 1 })
).resolves.toBe(5000)
})
it('throws TableRowLimitError when the write would exceed the limit', async () => {
await expect(
assertRowCapacity({ workspaceId: nextWorkspaceId(), currentRowCount: 5000, addedRows: 1 })
).rejects.toBeInstanceOf(TableRowLimitError)
})
it('names the plan limit in the error message', async () => {
await expect(
assertRowCapacity({ workspaceId: nextWorkspaceId(), currentRowCount: 5000, addedRows: 1 })
).rejects.toThrow(/row limit \(5,000 rows\)/)
})
it('skips the check when the plan is unlimited (-1)', async () => {
mockGetTablePlanLimits.mockReturnValue({
...LIMITS,
pro: { maxTables: 25, maxRowsPerTable: -1 },
})
await expect(
assertRowCapacity({
workspaceId: nextWorkspaceId(),
currentRowCount: 10_000_000,
addedRows: 1,
})
).resolves.toBe(-1)
})
})
describe('notifyTableRowUsage — edge-crossing gate', () => {
beforeEach(() => mockGetWorkspaceBilledAccountUserId.mockClear())
it('fires when an insert crosses UP into the warn band (limit 5000)', () => {
notifyTableRowUsage({ workspaceId: 'ws', currentRowCount: 3990, addedRows: 20, limit: 5000 })
expect(mockGetWorkspaceBilledAccountUserId).toHaveBeenCalledTimes(1)
})
it('fires when an insert crosses UP into the reached band', () => {
notifyTableRowUsage({ workspaceId: 'ws', currentRowCount: 4990, addedRows: 20, limit: 5000 })
expect(mockGetWorkspaceBilledAccountUserId).toHaveBeenCalledTimes(1)
})
it('does NOT fire when already above the band (no crossing)', () => {
notifyTableRowUsage({ workspaceId: 'ws', currentRowCount: 4200, addedRows: 100, limit: 5000 })
expect(mockGetWorkspaceBilledAccountUserId).not.toHaveBeenCalled()
})
it('does NOT fire well below the band', () => {
notifyTableRowUsage({ workspaceId: 'ws', currentRowCount: 100, addedRows: 10, limit: 5000 })
expect(mockGetWorkspaceBilledAccountUserId).not.toHaveBeenCalled()
})
it('does NOT fire for unlimited plans', () => {
notifyTableRowUsage({ workspaceId: 'ws', currentRowCount: 0, addedRows: 10_000, limit: -1 })
expect(mockGetWorkspaceBilledAccountUserId).not.toHaveBeenCalled()
})
})