import type { AdaptyProfile, AdaptyFlow, AdaptyPaywallProduct, AdaptyOnboarding, AdaptyAccessLevel, AdaptyPurchaseResult, AdaptyPrice, AdaptySubscriptionPeriod, AdaptySubscriptionPricingTerms, VendorStore, } from '@/types'; // Mock Product IDs export const MOCK_VENDOR_PRODUCT_ID_ANNUAL = 'mock.product.annual'; export const MOCK_VENDOR_PRODUCT_ID_MONTHLY = 'mock.product.monthly'; export const MOCK_ADAPTY_PRODUCT_ID_ANNUAL = 'mock.adapty.annual'; export const MOCK_ADAPTY_PRODUCT_ID_MONTHLY = 'mock.adapty.monthly'; // Access Levels export const MOCK_ACCESS_LEVEL_PREMIUM = 'premium'; // Profile IDs export const MOCK_PROFILE_ID = 'mock.profile.id'; export const MOCK_CUSTOMER_USER_ID = 'mock.customer.user.id'; // Time periods in milliseconds /** Default subscription duration: 1 year */ export const MOCK_SUBSCRIPTION_DURATION_MS = 365 * 24 * 60 * 60 * 1000; /** Time until unsubscribe after activation: 1 day */ export const MOCK_UNSUBSCRIBE_DELAY_MS = 24 * 60 * 60 * 1000; /** * Helper functions for date calculations */ /** * Adds milliseconds to a date */ export function addMilliseconds(date: Date, ms: number): Date { return new Date(date.getTime() + ms); } /** * Creates subscription date set with default values * @returns Object containing now, expiresAt, and unsubscribedAt dates */ export function createSubscriptionDates(): { now: Date; expiresAt: Date; unsubscribedAt: Date; } { const now = new Date(); return { now, expiresAt: addMilliseconds(now, MOCK_SUBSCRIPTION_DURATION_MS), unsubscribedAt: addMilliseconds(now, MOCK_UNSUBSCRIBE_DELAY_MS), }; } /** * Creates a default mock profile without subscriptions */ export function createMockProfile( overrides?: Partial, ): AdaptyProfile { return { profileId: MOCK_PROFILE_ID, customerUserId: MOCK_CUSTOMER_USER_ID, accessLevels: {}, subscriptions: {}, nonSubscriptions: {}, customAttributes: {}, ...overrides, }; } /** * Creates a premium access level for mock profile */ export function createMockPremiumAccessLevel( accessLevelId: string = MOCK_ACCESS_LEVEL_PREMIUM, ): AdaptyAccessLevel { const { now, expiresAt, unsubscribedAt } = createSubscriptionDates(); return { id: accessLevelId, isActive: true, vendorProductId: MOCK_VENDOR_PRODUCT_ID_ANNUAL, store: 'adapty' as VendorStore, activatedAt: now, renewedAt: now, expiresAt, unsubscribedAt, isLifetime: false, isInGracePeriod: false, isRefund: false, willRenew: false, startsAt: now, android: {}, }; } /** * Creates a default mock flow */ export function createMockFlow( placementId: string, overrides?: Partial, ): AdaptyFlow { const placement = { id: placementId, abTestName: placementId, audienceName: 'All Users', revision: 0, audienceVersionId: 'b7f6a19e-4384-4732-815d-5ad6610b695f', isTrackingPurchases: true, }; // `products` is kept at runtime so `coder.encode(flow)` (called by // getPaywallProducts / createFlowView) still serializes it. It is not on the // public AdaptyFlowPaywall type anymore — hence the assertion. const variation = { placement, id: `mock-paywall-${placementId}`, name: placementId, variationId: 'mock_variation_id', products: [ { vendorId: MOCK_VENDOR_PRODUCT_ID_ANNUAL, adaptyId: MOCK_ADAPTY_PRODUCT_ID_ANNUAL, accessLevelId: MOCK_ACCESS_LEVEL_PREMIUM, productType: 'annual', ios: {}, android: {}, }, { vendorId: MOCK_VENDOR_PRODUCT_ID_MONTHLY, adaptyId: MOCK_ADAPTY_PRODUCT_ID_MONTHLY, accessLevelId: MOCK_ACCESS_LEVEL_PREMIUM, productType: 'subscription', ios: {}, android: {}, }, ], productIdentifiers: [ { vendorProductId: MOCK_VENDOR_PRODUCT_ID_ANNUAL, adaptyProductId: MOCK_ADAPTY_PRODUCT_ID_ANNUAL, }, { vendorProductId: MOCK_VENDOR_PRODUCT_ID_MONTHLY, adaptyProductId: MOCK_ADAPTY_PRODUCT_ID_MONTHLY, }, ], webPurchaseUrl: `http://paywalls-mock.adapty.io/${placementId}`, } as AdaptyFlow['paywalls'][number]; return { placement, id: `mock-flow-${placementId}`, name: placementId, variationId: 'mock_variation_id', variationName: 'mock_variation_name', // A mock flow stands in for a placement with a no-code view configured, // so it carries the renderer fields that make `hasViewConfiguration` true // rather than just asserting the flag. `createMockPaywall` did the same in // v3 by shipping a `paywallBuilder`, and `createMockOnboarding` still does // it with `onboardingBuilder`. It keeps `AdaptyFlowView`'s Expo Go / web // placeholder reachable for apps that gate on the flag. hasViewConfiguration: true, flowVersionId: 'mock_flow_version_id', uiSchema: { layouts: [{ flowLayoutId: 'mock_flow_layout' }], grids: [{ platforms: 'all', devices: 'all', cells: [0] }], }, paywalls: [variation], responseCreatedAt: Date.now(), ...overrides, }; } /** * Default `up_front` pricing terms iOS reports when no other billing plan * applies: billed in full each period. */ export function createMockUpFrontPricingTerms( price: AdaptyPrice, period: AdaptySubscriptionPeriod, ): AdaptySubscriptionPricingTerms { return { billingPlan: 'up_front', billingPrice: price, billingPeriod: period, commitmentInfo: { price, period }, }; } /** * Creates default mock products for a flow */ export function createMockProducts(flow: AdaptyFlow): AdaptyPaywallProduct[] { // Shared with pricingTerms: up_front terms always equal the product price and period const annualPrice: AdaptyPrice = { amount: 99.99, currencyCode: 'USD', currencySymbol: '$', localizedString: '$99.99', }; const annualPeriod: AdaptySubscriptionPeriod = { numberOfUnits: 1, unit: 'year', }; const monthlyPrice: AdaptyPrice = { amount: 9.99, currencyCode: 'USD', currencySymbol: '$', localizedString: '$9.99', }; const monthlyPeriod: AdaptySubscriptionPeriod = { numberOfUnits: 1, unit: 'month', }; return [ { vendorProductId: MOCK_VENDOR_PRODUCT_ID_ANNUAL, adaptyId: MOCK_ADAPTY_PRODUCT_ID_ANNUAL, localizedTitle: 'Premium Annual', localizedDescription: 'Get premium access for 1 year', regionCode: 'US', paywallName: flow.name, paywallABTestName: flow.placement.abTestName, variationId: flow.variationId, accessLevelId: MOCK_ACCESS_LEVEL_PREMIUM, productType: 'subscription', price: annualPrice, webPurchaseUrl: flow.paywalls[0]?.webPurchaseUrl, paywallProductIndex: 0, subscription: { subscriptionPeriod: annualPeriod, localizedSubscriptionPeriod: '1 year', offer: { identifier: { type: 'introductory', }, phases: [ { localizedNumberOfPeriods: '1 month', localizedSubscriptionPeriod: '1 month', numberOfPeriods: 1, paymentMode: 'free_trial', price: { amount: 0, currencyCode: 'USD', localizedString: '$0.00', }, subscriptionPeriod: { unit: 'month', numberOfUnits: 1, }, }, ], }, ios: { subscriptionGroupIdentifier: '20770576', pricingTerms: createMockUpFrontPricingTerms( annualPrice, annualPeriod, ), }, }, ios: { isFamilyShareable: false, }, }, { vendorProductId: MOCK_VENDOR_PRODUCT_ID_MONTHLY, adaptyId: MOCK_ADAPTY_PRODUCT_ID_MONTHLY, localizedTitle: 'Premium Monthly', localizedDescription: 'Get premium access for 1 month', regionCode: 'US', paywallName: flow.name, paywallABTestName: flow.placement.abTestName, variationId: flow.variationId, accessLevelId: MOCK_ACCESS_LEVEL_PREMIUM, productType: 'subscription', price: monthlyPrice, webPurchaseUrl: flow.paywalls[0]?.webPurchaseUrl, paywallProductIndex: 1, subscription: { subscriptionPeriod: monthlyPeriod, localizedSubscriptionPeriod: '1 month', offer: { identifier: { type: 'introductory', }, phases: [ { localizedNumberOfPeriods: '1 month', localizedSubscriptionPeriod: '1 month', numberOfPeriods: 1, paymentMode: 'pay_up_front', price: { amount: 1.99, currencyCode: 'USD', localizedString: '$1.99', }, subscriptionPeriod: { unit: 'month', numberOfUnits: 1, }, }, ], }, ios: { subscriptionGroupIdentifier: '20770576', pricingTerms: createMockUpFrontPricingTerms( monthlyPrice, monthlyPeriod, ), }, }, ios: { isFamilyShareable: false, }, }, ]; } /** * Creates a successful purchase result with updated profile */ export function createMockPurchaseResult( profile: AdaptyProfile, ): AdaptyPurchaseResult { return { type: 'success', profile, }; } /** * Creates a default mock onboarding */ export function createMockOnboarding( placementId: string, overrides?: Partial, ): AdaptyOnboarding { return { id: `mock_onboarding_${placementId}`, placement: { id: placementId, abTestName: 'Mock Onboarding A/B Test', audienceName: 'All Users', revision: 1, audienceVersionId: 'mock_onboarding_audience_v1', isTrackingPurchases: true, }, hasViewConfiguration: true, name: `Mock Onboarding for ${placementId}`, variationId: 'mock_onboarding_variation_id', version: 1, requestLocale: 'en', remoteConfig: { lang: 'en', data: { screens: ['Welcome', 'Features', 'Pricing'] }, dataString: '{"screens":["Welcome","Features","Pricing"]}', }, onboardingBuilder: { url: `https://mock.adapty.io/onboardings/${placementId}`, }, ...overrides, }; }