Files
simstudioai--sim/apps/sim/lib/billing/core/subscription.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

99 lines
2.9 KiB
TypeScript

/**
* @vitest-environment node
*/
import { dbChainMock, dbChainMockFns, urlsMock } from '@sim/testing'
import { beforeEach, describe, expect, it, vi } from 'vitest'
vi.mock('@sim/db', () => dbChainMock)
vi.mock('@/lib/billing/core/access', () => ({
getEffectiveBillingStatus: vi.fn(),
isOrganizationBillingBlocked: vi.fn(),
}))
vi.mock('@/lib/billing/core/plan', () => ({
getHighestPrioritySubscription: vi.fn(),
}))
vi.mock('@/lib/billing/plan-helpers', () => ({
getPlanTierCredits: vi.fn(),
isPro: vi.fn(),
isTeam: vi.fn(),
}))
vi.mock('@/lib/billing/subscriptions/utils', () => ({
checkEnterprisePlan: vi.fn(),
checkProPlan: vi.fn(),
checkTeamPlan: vi.fn(),
ENTITLED_SUBSCRIPTION_STATUSES: ['active', 'trialing'],
hasUsableSubscriptionAccess: vi.fn(),
USABLE_SUBSCRIPTION_STATUSES: ['active', 'trialing'],
}))
vi.mock('@/lib/core/config/env-flags', () => ({
isAccessControlEnabled: false,
isBillingEnabled: true,
isHosted: true,
isInboxEnabled: false,
isSsoEnabled: false,
}))
vi.mock('@/lib/core/utils/urls', () => urlsMock)
import {
getOrganizationIdForSubscriptionReference,
hasPaidSubscription,
} from '@/lib/billing/core/subscription'
describe('hasPaidSubscription', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('returns true when an entitled subscription exists', async () => {
dbChainMockFns.limit.mockResolvedValueOnce([{ id: 'sub-1' }])
await expect(hasPaidSubscription('org-1')).resolves.toBe(true)
})
it('returns false when no entitled subscription exists', async () => {
dbChainMockFns.limit.mockResolvedValueOnce([])
await expect(hasPaidSubscription('org-1')).resolves.toBe(false)
})
it('fails closed by default when the lookup errors', async () => {
dbChainMockFns.limit.mockRejectedValueOnce(new Error('db unavailable'))
await expect(hasPaidSubscription('org-1')).resolves.toBe(true)
})
it('throws when requested so callers can retry instead of skipping cleanup', async () => {
dbChainMockFns.limit.mockRejectedValueOnce(new Error('db unavailable'))
await expect(hasPaidSubscription('org-1', { onError: 'throw' })).rejects.toThrow(
'db unavailable'
)
})
})
describe('getOrganizationIdForSubscriptionReference', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('returns an organization id directly when the reference already points to one', async () => {
dbChainMockFns.limit.mockResolvedValueOnce([{ id: 'org-1' }])
await expect(getOrganizationIdForSubscriptionReference('org-1')).resolves.toBe('org-1')
})
it('falls back to the admin-owned organization when the reference is still user-scoped', async () => {
dbChainMockFns.limit
.mockResolvedValueOnce([])
.mockResolvedValueOnce([{ organizationId: 'org-1', role: 'owner' }])
await expect(getOrganizationIdForSubscriptionReference('user-1')).resolves.toBe('org-1')
})
})