|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +/** |
| 7 | + * Drizzle mock for `getHighestPrioritySubscription`. It issues up to four |
| 8 | + * queries keyed by table: |
| 9 | + * - `subscription` for the user's personal subs (parallelized with members) |
| 10 | + * - `member` for the user's org memberships (parallelized with subs) |
| 11 | + * - `organization` for the org-existence follow-up |
| 12 | + * - `subscription` again for the org-scoped subs follow-up |
| 13 | + * |
| 14 | + * The mock routes results by the table object passed to `.from()`, serving the |
| 15 | + * (twice-read) `subscription` table from a FIFO queue (first read = personal, |
| 16 | + * second = org). It records which tables were queried so we can assert the |
| 17 | + * parallelized pair both run and that follow-ups are skipped when appropriate. |
| 18 | + * |
| 19 | + * Table sentinels and shared mock state live inside `vi.hoisted` so the |
| 20 | + * `vi.mock` factories (hoisted to the top of the file) can reference them. |
| 21 | + */ |
| 22 | +const { SUBSCRIPTION_TABLE, MEMBER_TABLE, ORGANIZATION_TABLE, resultsByTable, fromCalls, select } = |
| 23 | + vi.hoisted(() => { |
| 24 | + const SUBSCRIPTION_TABLE = { __table: 'subscription' } |
| 25 | + const MEMBER_TABLE = { __table: 'member' } |
| 26 | + const ORGANIZATION_TABLE = { __table: 'organization' } |
| 27 | + |
| 28 | + const resultsByTable: Record<string, unknown[][]> = { |
| 29 | + subscription: [], |
| 30 | + member: [], |
| 31 | + organization: [], |
| 32 | + } |
| 33 | + const fromCalls: string[] = [] |
| 34 | + |
| 35 | + const select = vi.fn(() => ({ |
| 36 | + from: (table: { __table: string }) => { |
| 37 | + fromCalls.push(table.__table) |
| 38 | + const where = () => { |
| 39 | + const queue = resultsByTable[table.__table] |
| 40 | + const next = queue.length > 0 ? queue.shift() : [] |
| 41 | + return Promise.resolve(next ?? []) |
| 42 | + } |
| 43 | + return { where } |
| 44 | + }, |
| 45 | + })) |
| 46 | + |
| 47 | + return { |
| 48 | + SUBSCRIPTION_TABLE, |
| 49 | + MEMBER_TABLE, |
| 50 | + ORGANIZATION_TABLE, |
| 51 | + resultsByTable, |
| 52 | + fromCalls, |
| 53 | + select, |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | +vi.mock('@sim/db', () => ({ |
| 58 | + db: { select }, |
| 59 | +})) |
| 60 | + |
| 61 | +vi.mock('@sim/db/schema', () => ({ |
| 62 | + subscription: SUBSCRIPTION_TABLE, |
| 63 | + member: MEMBER_TABLE, |
| 64 | + organization: ORGANIZATION_TABLE, |
| 65 | +})) |
| 66 | + |
| 67 | +/** |
| 68 | + * Realistic plan-check predicates so `pickHighestPrioritySubscription` exercises |
| 69 | + * the real Enterprise > Team > Pro priority ordering over the rows we feed it. |
| 70 | + */ |
| 71 | +vi.mock('@/lib/billing/subscriptions/utils', () => ({ |
| 72 | + ENTITLED_SUBSCRIPTION_STATUSES: ['active', 'past_due'], |
| 73 | + checkEnterprisePlan: (s: any) => |
| 74 | + s?.plan === 'enterprise' && ['active', 'past_due'].includes(s?.status), |
| 75 | + checkTeamPlan: (s: any) => s?.plan === 'team' && ['active', 'past_due'].includes(s?.status), |
| 76 | + checkProPlan: (s: any) => s?.plan === 'pro' && ['active', 'past_due'].includes(s?.status), |
| 77 | +})) |
| 78 | + |
| 79 | +import { getHighestPrioritySubscription } from '@/lib/billing/core/plan' |
| 80 | + |
| 81 | +interface SubRow { |
| 82 | + id: string |
| 83 | + referenceId: string |
| 84 | + plan: string |
| 85 | + status: string |
| 86 | +} |
| 87 | + |
| 88 | +function personalPro(userId: string): SubRow { |
| 89 | + return { id: 'sub-personal-pro', referenceId: userId, plan: 'pro', status: 'active' } |
| 90 | +} |
| 91 | + |
| 92 | +function orgEnterprise(orgId: string): SubRow { |
| 93 | + return { id: 'sub-org-enterprise', referenceId: orgId, plan: 'enterprise', status: 'active' } |
| 94 | +} |
| 95 | + |
| 96 | +function queue(table: 'subscription' | 'member' | 'organization', rows: unknown[]) { |
| 97 | + resultsByTable[table].push(rows) |
| 98 | +} |
| 99 | + |
| 100 | +describe('getHighestPrioritySubscription', () => { |
| 101 | + beforeEach(() => { |
| 102 | + vi.clearAllMocks() |
| 103 | + resultsByTable.subscription = [] |
| 104 | + resultsByTable.member = [] |
| 105 | + resultsByTable.organization = [] |
| 106 | + fromCalls.length = 0 |
| 107 | + }) |
| 108 | + |
| 109 | + it('picks the org Enterprise sub over a personal Pro sub (priority order)', async () => { |
| 110 | + queue('subscription', [personalPro('user-1')]) // personalSubs query |
| 111 | + queue('member', [{ organizationId: 'org-1' }]) // memberships query |
| 112 | + queue('organization', [{ id: 'org-1' }]) // org-existence query |
| 113 | + queue('subscription', [orgEnterprise('org-1')]) // org-subscriptions query |
| 114 | + |
| 115 | + const result = await getHighestPrioritySubscription('user-1') |
| 116 | + |
| 117 | + expect(result).not.toBeNull() |
| 118 | + expect(result?.id).toBe('sub-org-enterprise') |
| 119 | + expect(result?.plan).toBe('enterprise') |
| 120 | + }) |
| 121 | + |
| 122 | + it('selection is deterministic regardless of which parallelized query resolves first', async () => { |
| 123 | + queue('subscription', [personalPro('user-1')]) |
| 124 | + queue('member', [{ organizationId: 'org-1' }]) |
| 125 | + queue('organization', [{ id: 'org-1' }]) |
| 126 | + queue('subscription', [orgEnterprise('org-1')]) |
| 127 | + |
| 128 | + const result = await getHighestPrioritySubscription('user-1') |
| 129 | + |
| 130 | + expect(result?.id).toBe('sub-org-enterprise') |
| 131 | + }) |
| 132 | + |
| 133 | + it('issues BOTH the personal-subscriptions and memberships queries (parallelized pair)', async () => { |
| 134 | + queue('subscription', [personalPro('user-1')]) |
| 135 | + queue('member', [{ organizationId: 'org-1' }]) |
| 136 | + queue('organization', [{ id: 'org-1' }]) |
| 137 | + queue('subscription', [orgEnterprise('org-1')]) |
| 138 | + |
| 139 | + await getHighestPrioritySubscription('user-1') |
| 140 | + |
| 141 | + expect(fromCalls).toContain('subscription') |
| 142 | + expect(fromCalls).toContain('member') |
| 143 | + // First two queries are exactly the parallelized pair (in either order). |
| 144 | + expect(fromCalls.slice(0, 2).sort()).toEqual(['member', 'subscription']) |
| 145 | + }) |
| 146 | + |
| 147 | + it('returns the personal sub and skips org follow-ups when there are no memberships', async () => { |
| 148 | + queue('subscription', [personalPro('user-1')]) |
| 149 | + queue('member', []) |
| 150 | + |
| 151 | + const result = await getHighestPrioritySubscription('user-1') |
| 152 | + |
| 153 | + expect(result?.id).toBe('sub-personal-pro') |
| 154 | + expect(result?.plan).toBe('pro') |
| 155 | + // org-existence + org-subscription follow-ups are NOT issued. |
| 156 | + expect(fromCalls).not.toContain('organization') |
| 157 | + expect(fromCalls.filter((t) => t === 'subscription')).toHaveLength(1) |
| 158 | + }) |
| 159 | + |
| 160 | + it('returns null when neither personal nor org subscriptions exist', async () => { |
| 161 | + queue('subscription', []) |
| 162 | + queue('member', []) |
| 163 | + |
| 164 | + const result = await getHighestPrioritySubscription('user-1') |
| 165 | + |
| 166 | + expect(result).toBeNull() |
| 167 | + }) |
| 168 | + |
| 169 | + it('excludes orphaned org memberships whose organization row no longer exists', async () => { |
| 170 | + queue('subscription', []) |
| 171 | + queue('member', [{ organizationId: 'ghost-org' }]) // membership points at a deleted org |
| 172 | + queue('organization', []) |
| 173 | + |
| 174 | + const result = await getHighestPrioritySubscription('user-1') |
| 175 | + |
| 176 | + // Org subs are never fetched (no valid org ids) -> falls back to null. |
| 177 | + expect(result).toBeNull() |
| 178 | + expect(fromCalls).toContain('organization') |
| 179 | + // Only the initial personal-subs read on `subscription`; org-subs query skipped. |
| 180 | + expect(fromCalls.filter((t) => t === 'subscription')).toHaveLength(1) |
| 181 | + }) |
| 182 | + |
| 183 | + it('falls back to the personal sub when the only org is orphaned', async () => { |
| 184 | + queue('subscription', [personalPro('user-1')]) |
| 185 | + queue('member', [{ organizationId: 'ghost-org' }]) |
| 186 | + queue('organization', []) |
| 187 | + |
| 188 | + const result = await getHighestPrioritySubscription('user-1') |
| 189 | + |
| 190 | + expect(result?.id).toBe('sub-personal-pro') |
| 191 | + expect(fromCalls.filter((t) => t === 'subscription')).toHaveLength(1) |
| 192 | + }) |
| 193 | +}) |
0 commit comments