import { Feature } from '@d3ro/core/types' import { normalizeEntitlement } from '../src/lib/entitlement-context' import { resolveWebEntitlement } from '../../web/src/lib/entitlement' describe('normalizeEntitlement', () => { const now = new Date('2026-08-21T00:00:00Z') it('free 계정은 광고 제거 권한을 만들지 않는다', () => { const snapshot = normalizeEntitlement({ tier: 'free', status: 'active', provider: 'none', payment_provider: 'none', overage_credits: 50, }, [{ feature: 'stt_transcribe', count: 3 }], [], now) expect(snapshot.tier).toBe('free') expect(snapshot.adFree).toBe(false) expect(snapshot.overageCredits).toBe(50) expect(snapshot.usageToday.stt_transcribe).toBe(3) expect(snapshot.features).not.toContain(Feature.TEAM_WORKSPACE) }) it('서버가 확인한 유효 paid period만 광고 제거 권한을 준다', () => { const active = normalizeEntitlement({ tier: 'pro', status: 'active', provider: 'google_play', payment_provider: 'google_play', current_period_end: '2026-09-21T00:00:00Z', }, [], [], now) const expired = normalizeEntitlement({ tier: 'pro', status: 'active', provider: 'google_play', payment_provider: 'google_play', current_period_end: '2026-08-20T00:00:00Z', }, [], [], now) expect(active.tier).toBe('pro') expect(active.adFree).toBe(true) expect(active.features).toContain(Feature.TEAM_WORKSPACE) expect(expired.tier).toBe('free') expect(expired.adFree).toBe(false) }) it('취소된 선불 기간과 안전한 구매 요약을 보존한다', () => { const snapshot = normalizeEntitlement({ tier: 'pro_plus', status: 'canceled', provider: 'google_play', payment_provider: 'google_play', current_period_end: '2026-09-21T00:00:00Z', auto_renewing: false, }, [], [{ id: 'purchase-1', platform: 'google_play', product_id: 'd3ro_voice_pro_plus_monthly', purchase_state: 'cancelled', purchase_at: '2026-08-21T00:00:00Z', expires_at: '2026-09-21T00:00:00Z', auto_renewing: false, verified_at: '2026-08-21T00:01:00Z', }, { id: 'invalid-receipt', platform: 'unknown', product_id: 'unknown', purchase_state: 'purchased', verified_at: 'invalid', }], now, 'manager') expect(snapshot.tier).toBe('pro_plus') expect(snapshot.adFree).toBe(true) expect(snapshot.adminRole).toBe('manager') expect(snapshot.autoRenewing).toBe(false) expect(snapshot.purchases).toHaveLength(1) expect(snapshot.purchases[0].productId).toBe('d3ro_voice_pro_plus_monthly') }) it('웹 구독과 역할도 @d3ro/core resolveEntitlement 기준으로 정규화한다', () => { const activeWeb = resolveWebEntitlement({ tier: 'pro_plus', status: 'active', provider: 'none', payment_provider: 'payple', current_period_end: '2026-09-21T00:00:00Z', overage_credits: 120, }, { supabaseRole: 'admin', now }) expect(activeWeb.tier).toBe('pro_plus') expect(activeWeb.provider).toBe('payple') expect(activeWeb.adFree).toBe(true) expect(activeWeb.adminRole).toBe('admin') expect(activeWeb.adminCapabilities.canAdmin).toBe(true) const expiredWeb = resolveWebEntitlement({ tier: 'pro', status: 'active', provider: 'payple', current_period_end: '2026-08-01T00:00:00Z', }, { now }) expect(expiredWeb.tier).toBe('free') expect(expiredWeb.rawTier).toBe('pro') expect(expiredWeb.adFree).toBe(false) }) })