22/*global expect, it, awaitsFor*/
33
44define ( function ( require , exports , module ) {
5- let testWindow , LoginServiceExports , setupProUserMock , performFullLoginFlow ;
5+ let testWindow , LoginServiceExports , setupProUserMock , performFullLoginFlow , EntitlementsExports ;
66
77 async function setupTrialState ( daysRemaining ) {
88 const PromotionExports = testWindow . _test_promo_login_exports ;
@@ -169,11 +169,76 @@ define(function (require, exports, module) {
169169 verifyProfileIconBlanked ( ) ;
170170 }
171171
172- function setup ( _testWindow , _LoginServiceExports , _setupProUserMock , _performFullLoginFlow ) {
172+ // Entitlements test utility functions
173+ async function verifyPlanEntitlements ( expectedPlan , _testDescription ) {
174+ const planDetails = await EntitlementsExports . getPlanDetails ( ) ;
175+
176+ if ( expectedPlan ) {
177+ expect ( planDetails ) . toBeDefined ( ) ;
178+ if ( expectedPlan . paidSubscriber !== undefined ) {
179+ expect ( planDetails . paidSubscriber ) . toBe ( expectedPlan . paidSubscriber ) ;
180+ }
181+ if ( expectedPlan . name ) {
182+ expect ( planDetails . name ) . toBe ( expectedPlan . name ) ;
183+ }
184+ if ( expectedPlan . validTill !== undefined ) {
185+ expect ( planDetails . validTill ) . toBeDefined ( ) ;
186+ }
187+ } else {
188+ expect ( planDetails ) . toBeDefined ( ) ; // Should always return something (fallback)
189+ }
190+ }
191+
192+ async function verifyIsInProTrialEntitlement ( expected , _testDescription ) {
193+ const isInTrial = await EntitlementsExports . isInProTrial ( ) ;
194+ expect ( isInTrial ) . toBe ( expected ) ;
195+ }
196+
197+ async function verifyTrialRemainingDaysEntitlement ( expected , _testDescription ) {
198+ const remainingDays = await EntitlementsExports . getTrialRemainingDays ( ) ;
199+ if ( typeof expected === 'number' ) {
200+ expect ( remainingDays ) . toBe ( expected ) ;
201+ } else {
202+ expect ( remainingDays ) . toBeGreaterThanOrEqual ( 0 ) ;
203+ }
204+ }
205+
206+ async function verifyRawEntitlements ( expected , _testDescription ) {
207+ const rawEntitlements = await EntitlementsExports . getRawEntitlements ( ) ;
208+
209+ if ( expected === null ) {
210+ expect ( rawEntitlements ) . toBeNull ( ) ;
211+ } else if ( expected ) {
212+ expect ( rawEntitlements ) . toBeDefined ( ) ;
213+ if ( expected . plan ) {
214+ expect ( rawEntitlements . plan ) . toBeDefined ( ) ;
215+ }
216+ if ( expected . entitlements ) {
217+ expect ( rawEntitlements . entitlements ) . toBeDefined ( ) ;
218+ }
219+ }
220+ }
221+
222+ async function verifyLiveEditEntitlement ( expected , _testDescription ) {
223+ const liveEditEntitlement = await EntitlementsExports . getLiveEditEntitlement ( ) ;
224+
225+ expect ( liveEditEntitlement ) . toBeDefined ( ) ;
226+ expect ( liveEditEntitlement . activated ) . toBe ( expected . activated ) ;
227+
228+ if ( expected . subscribeURL ) {
229+ expect ( liveEditEntitlement . subscribeURL ) . toBe ( expected . subscribeURL ) ;
230+ }
231+ if ( expected . upgradeToPlan ) {
232+ expect ( liveEditEntitlement . upgradeToPlan ) . toBe ( expected . upgradeToPlan ) ;
233+ }
234+ }
235+
236+ function setup ( _testWindow , _LoginServiceExports , _setupProUserMock , _performFullLoginFlow , _EntitlementsExports ) {
173237 testWindow = _testWindow ;
174238 LoginServiceExports = _LoginServiceExports ;
175239 setupProUserMock = _setupProUserMock ;
176240 performFullLoginFlow = _performFullLoginFlow ;
241+ EntitlementsExports = _EntitlementsExports ;
177242 }
178243
179244 function setupSharedTests ( ) {
@@ -286,10 +351,18 @@ define(function (require, exports, module) {
286351 // Verify initial state (no pro branding)
287352 await verifyProBranding ( false , "no pro branding to start with" ) ;
288353
354+ // Verify entitlements API consistency for logged out state
355+ await verifyIsInProTrialEntitlement ( false , "no trial for logged out user with expired trial" ) ;
356+ await verifyTrialRemainingDaysEntitlement ( 0 , "no trial days remaining" ) ;
357+
289358 // Perform login
290359 await performFullLoginFlow ( ) ;
291360 await verifyProBranding ( true , "pro branding to appear after pro user login" ) ;
292361
362+ // Verify entitlements API consistency for logged in pro user
363+ await verifyIsInProTrialEntitlement ( false , "pro user should not be in trial" ) ;
364+ await verifyPlanEntitlements ( { paidSubscriber : true } , "pro user should have paid subscriber plan" ) ;
365+
293366 // Check profile popup shows pro status (not trial)
294367 const $profileButton = testWindow . $ ( "#user-profile-button" ) ;
295368 $profileButton . trigger ( 'click' ) ;
@@ -308,6 +381,10 @@ define(function (require, exports, module) {
308381 // 1. No server entitlements (logged out)
309382 // 2. Trial is expired (0 days remaining)
310383 await verifyProBranding ( false , "Pro branding to disappear after logout" ) ;
384+
385+ // Verify entitlements API consistency after logout
386+ await verifyRawEntitlements ( null , "no raw entitlements when logged out" ) ;
387+ await verifyIsInProTrialEntitlement ( false , "no trial after logout" ) ;
311388 } ) ;
312389
313390 it ( "should show trial branding for user without pro subscription (active trial)" , async function ( ) {
@@ -320,12 +397,21 @@ define(function (require, exports, module) {
320397 // Verify initial state shows pro branding due to trial
321398 await verifyProBranding ( true , "Trial branding to appear initially" ) ;
322399
400+ // Verify entitlements API consistency for trial user before login
401+ await verifyIsInProTrialEntitlement ( true , "user should be in trial initially" ) ;
402+ await verifyTrialRemainingDaysEntitlement ( 15 , "should have 15 trial days remaining" ) ;
403+ await verifyLiveEditEntitlement ( { activated : true } , "live edit should be active during trial" ) ;
404+
323405 // Perform login
324406 await performFullLoginFlow ( ) ;
325407
326408 // Verify pro branding remains after login
327409 await verifyProBranding ( true , "after trial user login" ) ;
328410
411+ // Verify entitlements API consistency for logged in trial user
412+ await verifyIsInProTrialEntitlement ( true , "user should still be in trial after login" ) ;
413+ await verifyPlanEntitlements ( { paidSubscriber : true } , "trial user should have paidSubscriber true" ) ;
414+
329415 // Check profile popup shows trial status
330416 const $profileButton = testWindow . $ ( "#user-profile-button" ) ;
331417 $profileButton . trigger ( 'click' ) ;
@@ -342,6 +428,10 @@ define(function (require, exports, module) {
342428 // Verify pro branding remains after logout (trial continues)
343429 await verifyProBranding ( true , "Trial branding to remain after logout" ) ;
344430
431+ // Verify entitlements API consistency after logout (trial still active)
432+ await verifyIsInProTrialEntitlement ( true , "trial should persist after logout" ) ;
433+ await verifyRawEntitlements ( null , "no raw entitlements when logged out" ) ;
434+
345435 // Check profile popup still shows trial status
346436 $profileButton . trigger ( 'click' ) ;
347437 await popupToAppear ( SIGNIN_POPUP ) ;
@@ -516,6 +606,49 @@ define(function (require, exports, module) {
516606 // Close popup
517607 $profileButton . trigger ( 'click' ) ;
518608 } ) ;
609+
610+ it ( "should test entitlements event forwarding" , async function ( ) {
611+ console . log ( "Entitlements: Testing event forwarding" ) ;
612+
613+ let entitlementsEventFired = false ;
614+
615+ // Set up event listeners
616+ const entitlementsService = EntitlementsExports . EntitlementsService ;
617+
618+ const entitlementsHandler = ( ) => {
619+ entitlementsEventFired = true ;
620+ } ;
621+
622+ entitlementsService . on ( entitlementsService . EVENT_ENTITLEMENTS_CHANGED , entitlementsHandler ) ;
623+
624+ try {
625+ // Setup basic user mock
626+ setupProUserMock ( false ) ;
627+
628+ // Perform full login flow
629+ await performFullLoginFlow ( ) ;
630+ expect ( LoginServiceExports . LoginService . isLoggedIn ( ) ) . toBe ( true ) ;
631+
632+ // Wait for events to fire
633+ await awaitsFor ( ( ) => entitlementsEventFired , "Entitlements events to fire" ) ;
634+
635+ expect ( entitlementsEventFired ) . toBe ( true ) ;
636+
637+ // Perform a full logout flow and see if entitlement changes are detected
638+ entitlementsEventFired = false ;
639+ await performFullLogoutFlow ( ) ;
640+ expect ( LoginServiceExports . LoginService . isLoggedIn ( ) ) . toBe ( false ) ;
641+ verifyProfileIconBlanked ( ) ;
642+
643+ // Wait for events to fire
644+ await awaitsFor ( ( ) => entitlementsEventFired , "Entitlements events to fire" ) ;
645+
646+ } finally {
647+ // Cleanup event listeners
648+ entitlementsService . off ( entitlementsService . EVENT_ENTITLEMENTS_CHANGED , entitlementsHandler ) ;
649+ await cleanupTrialState ( ) ;
650+ }
651+ } ) ;
519652 }
520653
521654 exports . setup = setup ;
0 commit comments