Skip to content

Commit 15e3901

Browse files
committed
test: expand testActivateDeactivateSubscription
1 parent c8d40f2 commit 15e3901

File tree

2 files changed

+154
-31
lines changed

2 files changed

+154
-31
lines changed

target_chains/ethereum/contracts/contracts/pulse/scheduler/Scheduler.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,8 @@ abstract contract Scheduler is IScheduler, SchedulerState {
784784
// Only add if not already in the list
785785
if (_state.activeSubscriptionIndex[subscriptionId] == 0) {
786786
_state.activeSubscriptionIds.push(subscriptionId);
787+
788+
// Store the index as 1-based, 0 means not in the list
787789
_state.activeSubscriptionIndex[subscriptionId] = _state
788790
.activeSubscriptionIds
789791
.length;

target_chains/ethereum/contracts/forge-test/PulseScheduler.t.sol

Lines changed: 152 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -348,51 +348,172 @@ contract SchedulerTest is Test, SchedulerEvents, PulseSchedulerTestUtils {
348348
}
349349

350350
function testActivateDeactivateSubscription() public {
351-
uint256 subscriptionId = addTestSubscription(
352-
scheduler,
353-
address(reader)
354-
);
355-
356-
// Get current params
357-
(SchedulerState.SubscriptionParams memory params, ) = scheduler
358-
.getSubscription(subscriptionId);
359-
360-
// Deactivate subscription using updateSubscription
361-
params.isActive = false;
351+
// Add multiple subscriptions
352+
uint256 subId1 = addTestSubscription(scheduler, address(reader)); // ID 1
353+
uint256 subId2 = addTestSubscription(scheduler, address(reader)); // ID 2
354+
uint256 subId3 = addTestSubscription(scheduler, address(reader)); // ID 3
362355

356+
// --- Verify initial state ---
357+
(uint256[] memory activeIds, , uint256 totalCount) = scheduler
358+
.getActiveSubscriptions(0, 10);
359+
assertEq(totalCount, 3, "Initial: Total count should be 3");
360+
assertEq(activeIds.length, 3, "Initial: Active IDs length should be 3");
361+
assertEq(activeIds[0], subId1, "Initial: ID 1 should be active");
362+
assertEq(activeIds[1], subId2, "Initial: ID 2 should be active");
363+
assertEq(activeIds[2], subId3, "Initial: ID 3 should be active");
364+
365+
// --- Deactivate the middle subscription (ID 2) ---
366+
(SchedulerState.SubscriptionParams memory params2, ) = scheduler
367+
.getSubscription(subId2);
368+
params2.isActive = false;
363369
vm.expectEmit();
364-
emit SubscriptionDeactivated(subscriptionId);
370+
emit SubscriptionDeactivated(subId2);
365371
vm.expectEmit();
366-
emit SubscriptionUpdated(subscriptionId);
372+
emit SubscriptionUpdated(subId2);
373+
scheduler.updateSubscription(subId2, params2);
367374

368-
scheduler.updateSubscription(subscriptionId, params);
375+
// Verify state after deactivating ID 2
376+
(activeIds, , totalCount) = scheduler.getActiveSubscriptions(0, 10);
377+
assertEq(totalCount, 2, "After Deact 2: Total count should be 2");
378+
assertEq(
379+
activeIds.length,
380+
2,
381+
"After Deact 2: Active IDs length should be 2"
382+
);
383+
assertEq(activeIds[0], subId1, "After Deact 2: ID 1 should be active");
384+
assertEq(
385+
activeIds[1],
386+
subId3,
387+
"After Deact 2: ID 3 should be active (moved)"
388+
); // ID 3 takes the place of ID 2
389+
390+
// --- Deactivate the last subscription (ID 3, now at index 1) ---
391+
(SchedulerState.SubscriptionParams memory params3, ) = scheduler
392+
.getSubscription(subId3);
393+
params3.isActive = false;
394+
vm.expectEmit();
395+
emit SubscriptionDeactivated(subId3);
396+
vm.expectEmit();
397+
emit SubscriptionUpdated(subId3);
398+
scheduler.updateSubscription(subId3, params3);
369399

370-
// Verify subscription was deactivated
371-
(
372-
SchedulerState.SubscriptionParams memory storedParams,
373-
SchedulerState.SubscriptionStatus memory status
374-
) = scheduler.getSubscription(subscriptionId);
400+
// Verify state after deactivating ID 3
401+
(activeIds, , totalCount) = scheduler.getActiveSubscriptions(0, 10);
402+
assertEq(totalCount, 1, "After Deact 3: Total count should be 1");
403+
assertEq(
404+
activeIds.length,
405+
1,
406+
"After Deact 3: Active IDs length should be 1"
407+
);
408+
assertEq(
409+
activeIds[0],
410+
subId1,
411+
"After Deact 3: Only ID 1 should be active"
412+
);
375413

376-
assertFalse(storedParams.isActive, "Subscription should be inactive");
414+
// --- Reactivate the middle subscription (ID 2) ---
415+
params2.isActive = true; // Use the params struct from earlier
416+
vm.expectEmit();
417+
emit SubscriptionActivated(subId2);
418+
vm.expectEmit();
419+
emit SubscriptionUpdated(subId2);
420+
scheduler.updateSubscription(subId2, params2);
377421

378-
// Reactivate subscription using updateSubscription
379-
params.isActive = true;
422+
// Verify state after reactivating ID 2
423+
(activeIds, , totalCount) = scheduler.getActiveSubscriptions(0, 10);
424+
assertEq(totalCount, 2, "After React 2: Total count should be 2");
425+
assertEq(
426+
activeIds.length,
427+
2,
428+
"After React 2: Active IDs length should be 2"
429+
);
430+
assertEq(activeIds[0], subId1, "After React 2: ID 1 should be active");
431+
assertEq(activeIds[1], subId2, "After React 2: ID 2 should be active"); // ID 2 is added back to the end
380432

433+
// --- Reactivate the last subscription (ID 3) ---
434+
params3.isActive = true; // Use the params struct from earlier
381435
vm.expectEmit();
382-
emit SubscriptionActivated(subscriptionId);
436+
emit SubscriptionActivated(subId3);
383437
vm.expectEmit();
384-
emit SubscriptionUpdated(subscriptionId);
438+
emit SubscriptionUpdated(subId3);
439+
scheduler.updateSubscription(subId3, params3);
440+
441+
// Verify final state (all active)
442+
(activeIds, , totalCount) = scheduler.getActiveSubscriptions(0, 10);
443+
assertEq(totalCount, 3, "Final: Total count should be 3");
444+
assertEq(activeIds.length, 3, "Final: Active IDs length should be 3");
445+
assertEq(activeIds[0], subId1, "Final: ID 1 should be active");
446+
assertEq(activeIds[1], subId2, "Final: ID 2 should be active");
447+
assertEq(activeIds[2], subId3, "Final: ID 3 should be active"); // ID 3 is added back to the end
448+
449+
// --- Deactivate all remaining subscriptions ---
450+
// Deactivate ID 1 (first element)
451+
(SchedulerState.SubscriptionParams memory params1, ) = scheduler
452+
.getSubscription(subId1);
453+
params1.isActive = false;
454+
vm.expectEmit();
455+
emit SubscriptionDeactivated(subId1);
456+
vm.expectEmit();
457+
emit SubscriptionUpdated(subId1);
458+
scheduler.updateSubscription(subId1, params1);
385459

386-
scheduler.updateSubscription(subscriptionId, params);
460+
// Verify state after deactivating ID 1
461+
(activeIds, , totalCount) = scheduler.getActiveSubscriptions(0, 10);
462+
assertEq(totalCount, 2, "After Deact 1: Total count should be 2");
463+
assertEq(
464+
activeIds.length,
465+
2,
466+
"After Deact 1: Active IDs length should be 2"
467+
);
468+
assertEq(
469+
activeIds[0],
470+
subId3,
471+
"After Deact 1: ID 3 should be at index 0"
472+
); // ID 3 moved to front
473+
assertEq(
474+
activeIds[1],
475+
subId2,
476+
"After Deact 1: ID 2 should be at index 1"
477+
);
387478

388-
// Verify subscription was reactivated
389-
(storedParams, status) = scheduler.getSubscription(subscriptionId);
479+
// Deactivate ID 2 (now last element)
480+
params2.isActive = false; // Use existing params struct
481+
vm.expectEmit();
482+
emit SubscriptionDeactivated(subId2);
483+
vm.expectEmit();
484+
emit SubscriptionUpdated(subId2);
485+
scheduler.updateSubscription(subId2, params2);
390486

391-
assertTrue(storedParams.isActive, "Subscription should be active");
392-
assertTrue(
393-
storedParams.isActive,
394-
"Subscription params should show active"
487+
// Verify state after deactivating ID 2
488+
(activeIds, , totalCount) = scheduler.getActiveSubscriptions(0, 10);
489+
assertEq(
490+
totalCount,
491+
1,
492+
"After Deact 2 (again): Total count should be 1"
493+
);
494+
assertEq(
495+
activeIds.length,
496+
1,
497+
"After Deact 2 (again): Active IDs length should be 1"
498+
);
499+
assertEq(
500+
activeIds[0],
501+
subId3,
502+
"After Deact 2 (again): Only ID 3 should be active"
395503
);
504+
505+
// Deactivate ID 3 (last remaining element)
506+
params3.isActive = false; // Use existing params struct
507+
vm.expectEmit();
508+
emit SubscriptionDeactivated(subId3);
509+
vm.expectEmit();
510+
emit SubscriptionUpdated(subId3);
511+
scheduler.updateSubscription(subId3, params3);
512+
513+
// Verify final empty state
514+
(activeIds, , totalCount) = scheduler.getActiveSubscriptions(0, 10);
515+
assertEq(totalCount, 0, "Empty: Total count should be 0");
516+
assertEq(activeIds.length, 0, "Empty: Active IDs length should be 0");
396517
}
397518

398519
function testAddFunds() public {

0 commit comments

Comments
 (0)