@@ -331,51 +331,172 @@ contract SchedulerTest is Test, SchedulerEvents, PulseSchedulerTestUtils {
331331 }
332332
333333 function testActivateDeactivateSubscription () public {
334- uint256 subscriptionId = addTestSubscription (
335- scheduler,
336- address (reader)
337- );
338-
339- // Get current params
340- (SchedulerState.SubscriptionParams memory params , ) = scheduler
341- .getSubscription (subscriptionId);
342-
343- // Deactivate subscription using updateSubscription
344- params.isActive = false ;
334+ // Add multiple subscriptions
335+ uint256 subId1 = addTestSubscription (scheduler, address (reader)); // ID 1
336+ uint256 subId2 = addTestSubscription (scheduler, address (reader)); // ID 2
337+ uint256 subId3 = addTestSubscription (scheduler, address (reader)); // ID 3
345338
339+ // --- Verify initial state ---
340+ (uint256 [] memory activeIds , , uint256 totalCount ) = scheduler
341+ .getActiveSubscriptions (0 , 10 );
342+ assertEq (totalCount, 3 , "Initial: Total count should be 3 " );
343+ assertEq (activeIds.length , 3 , "Initial: Active IDs length should be 3 " );
344+ assertEq (activeIds[0 ], subId1, "Initial: ID 1 should be active " );
345+ assertEq (activeIds[1 ], subId2, "Initial: ID 2 should be active " );
346+ assertEq (activeIds[2 ], subId3, "Initial: ID 3 should be active " );
347+
348+ // --- Deactivate the middle subscription (ID 2) ---
349+ (SchedulerState.SubscriptionParams memory params2 , ) = scheduler
350+ .getSubscription (subId2);
351+ params2.isActive = false ;
346352 vm.expectEmit ();
347- emit SubscriptionDeactivated (subscriptionId );
353+ emit SubscriptionDeactivated (subId2 );
348354 vm.expectEmit ();
349- emit SubscriptionUpdated (subscriptionId);
355+ emit SubscriptionUpdated (subId2);
356+ scheduler.updateSubscription (subId2, params2);
350357
351- scheduler.updateSubscription (subscriptionId, params);
358+ // Verify state after deactivating ID 2
359+ (activeIds, , totalCount) = scheduler.getActiveSubscriptions (0 , 10 );
360+ assertEq (totalCount, 2 , "After Deact 2: Total count should be 2 " );
361+ assertEq (
362+ activeIds.length ,
363+ 2 ,
364+ "After Deact 2: Active IDs length should be 2 "
365+ );
366+ assertEq (activeIds[0 ], subId1, "After Deact 2: ID 1 should be active " );
367+ assertEq (
368+ activeIds[1 ],
369+ subId3,
370+ "After Deact 2: ID 3 should be active (moved) "
371+ ); // ID 3 takes the place of ID 2
372+
373+ // --- Deactivate the last subscription (ID 3, now at index 1) ---
374+ (SchedulerState.SubscriptionParams memory params3 , ) = scheduler
375+ .getSubscription (subId3);
376+ params3.isActive = false ;
377+ vm.expectEmit ();
378+ emit SubscriptionDeactivated (subId3);
379+ vm.expectEmit ();
380+ emit SubscriptionUpdated (subId3);
381+ scheduler.updateSubscription (subId3, params3);
352382
353- // Verify subscription was deactivated
354- (
355- SchedulerState.SubscriptionParams memory storedParams ,
356- SchedulerState.SubscriptionStatus memory status
357- ) = scheduler.getSubscription (subscriptionId);
383+ // Verify state after deactivating ID 3
384+ (activeIds, , totalCount) = scheduler.getActiveSubscriptions (0 , 10 );
385+ assertEq (totalCount, 1 , "After Deact 3: Total count should be 1 " );
386+ assertEq (
387+ activeIds.length ,
388+ 1 ,
389+ "After Deact 3: Active IDs length should be 1 "
390+ );
391+ assertEq (
392+ activeIds[0 ],
393+ subId1,
394+ "After Deact 3: Only ID 1 should be active "
395+ );
358396
359- assertFalse (storedParams.isActive, "Subscription should be inactive " );
397+ // --- Reactivate the middle subscription (ID 2) ---
398+ params2.isActive = true ; // Use the params struct from earlier
399+ vm.expectEmit ();
400+ emit SubscriptionActivated (subId2);
401+ vm.expectEmit ();
402+ emit SubscriptionUpdated (subId2);
403+ scheduler.updateSubscription (subId2, params2);
360404
361- // Reactivate subscription using updateSubscription
362- params.isActive = true ;
405+ // Verify state after reactivating ID 2
406+ (activeIds, , totalCount) = scheduler.getActiveSubscriptions (0 , 10 );
407+ assertEq (totalCount, 2 , "After React 2: Total count should be 2 " );
408+ assertEq (
409+ activeIds.length ,
410+ 2 ,
411+ "After React 2: Active IDs length should be 2 "
412+ );
413+ assertEq (activeIds[0 ], subId1, "After React 2: ID 1 should be active " );
414+ assertEq (activeIds[1 ], subId2, "After React 2: ID 2 should be active " ); // ID 2 is added back to the end
363415
416+ // --- Reactivate the last subscription (ID 3) ---
417+ params3.isActive = true ; // Use the params struct from earlier
364418 vm.expectEmit ();
365- emit SubscriptionActivated (subscriptionId );
419+ emit SubscriptionActivated (subId3 );
366420 vm.expectEmit ();
367- emit SubscriptionUpdated (subscriptionId);
421+ emit SubscriptionUpdated (subId3);
422+ scheduler.updateSubscription (subId3, params3);
423+
424+ // Verify final state (all active)
425+ (activeIds, , totalCount) = scheduler.getActiveSubscriptions (0 , 10 );
426+ assertEq (totalCount, 3 , "Final: Total count should be 3 " );
427+ assertEq (activeIds.length , 3 , "Final: Active IDs length should be 3 " );
428+ assertEq (activeIds[0 ], subId1, "Final: ID 1 should be active " );
429+ assertEq (activeIds[1 ], subId2, "Final: ID 2 should be active " );
430+ assertEq (activeIds[2 ], subId3, "Final: ID 3 should be active " ); // ID 3 is added back to the end
431+
432+ // --- Deactivate all remaining subscriptions ---
433+ // Deactivate ID 1 (first element)
434+ (SchedulerState.SubscriptionParams memory params1 , ) = scheduler
435+ .getSubscription (subId1);
436+ params1.isActive = false ;
437+ vm.expectEmit ();
438+ emit SubscriptionDeactivated (subId1);
439+ vm.expectEmit ();
440+ emit SubscriptionUpdated (subId1);
441+ scheduler.updateSubscription (subId1, params1);
368442
369- scheduler.updateSubscription (subscriptionId, params);
443+ // Verify state after deactivating ID 1
444+ (activeIds, , totalCount) = scheduler.getActiveSubscriptions (0 , 10 );
445+ assertEq (totalCount, 2 , "After Deact 1: Total count should be 2 " );
446+ assertEq (
447+ activeIds.length ,
448+ 2 ,
449+ "After Deact 1: Active IDs length should be 2 "
450+ );
451+ assertEq (
452+ activeIds[0 ],
453+ subId3,
454+ "After Deact 1: ID 3 should be at index 0 "
455+ ); // ID 3 moved to front
456+ assertEq (
457+ activeIds[1 ],
458+ subId2,
459+ "After Deact 1: ID 2 should be at index 1 "
460+ );
370461
371- // Verify subscription was reactivated
372- (storedParams, status) = scheduler.getSubscription (subscriptionId);
462+ // Deactivate ID 2 (now last element)
463+ params2.isActive = false ; // Use existing params struct
464+ vm.expectEmit ();
465+ emit SubscriptionDeactivated (subId2);
466+ vm.expectEmit ();
467+ emit SubscriptionUpdated (subId2);
468+ scheduler.updateSubscription (subId2, params2);
373469
374- assertTrue (storedParams.isActive, "Subscription should be active " );
375- assertTrue (
376- storedParams.isActive,
377- "Subscription params should show active "
470+ // Verify state after deactivating ID 2
471+ (activeIds, , totalCount) = scheduler.getActiveSubscriptions (0 , 10 );
472+ assertEq (
473+ totalCount,
474+ 1 ,
475+ "After Deact 2 (again): Total count should be 1 "
476+ );
477+ assertEq (
478+ activeIds.length ,
479+ 1 ,
480+ "After Deact 2 (again): Active IDs length should be 1 "
481+ );
482+ assertEq (
483+ activeIds[0 ],
484+ subId3,
485+ "After Deact 2 (again): Only ID 3 should be active "
378486 );
487+
488+ // Deactivate ID 3 (last remaining element)
489+ params3.isActive = false ; // Use existing params struct
490+ vm.expectEmit ();
491+ emit SubscriptionDeactivated (subId3);
492+ vm.expectEmit ();
493+ emit SubscriptionUpdated (subId3);
494+ scheduler.updateSubscription (subId3, params3);
495+
496+ // Verify final empty state
497+ (activeIds, , totalCount) = scheduler.getActiveSubscriptions (0 , 10 );
498+ assertEq (totalCount, 0 , "Empty: Total count should be 0 " );
499+ assertEq (activeIds.length , 0 , "Empty: Active IDs length should be 0 " );
379500 }
380501
381502 function testAddFunds () public {
0 commit comments