Skip to content

Commit 4784588

Browse files
committed
test(paymaster): cover paidUntil recharge logic
1 parent 491961f commit 4784588

1 file changed

Lines changed: 58 additions & 5 deletions

File tree

workers/paymaster/tests/index.test.ts

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,11 +481,6 @@ describe('PaymasterWorker', () => {
481481
MockDate.reset();
482482
});
483483

484-
afterAll(async () => {
485-
await connection.close();
486-
MockDate.reset();
487-
});
488-
489484
test('Should send notification if payday is coming for workspace with paidUntil value', async () => {
490485
/**
491486
* Arrange
@@ -542,4 +537,62 @@ describe('PaymasterWorker', () => {
542537

543538
MockDate.reset();
544539
});
540+
541+
test('Should recharge workspace billing period when month passes since last charge date and paidUntil is set to several months in the future', async () => {
542+
/**
543+
* Arrange
544+
*/
545+
const currentDate = new Date();
546+
const lastChargeDate = new Date(currentDate.getTime());
547+
548+
lastChargeDate.setMonth(lastChargeDate.getMonth() - 1); // Set last charge date to 1 month ago
549+
550+
const paidUntil = new Date(currentDate.getTime());
551+
552+
paidUntil.setMonth(paidUntil.getMonth() + 3); // Set paidUntil to 3 months in the future
553+
554+
const plan = createPlanMock({
555+
monthlyCharge: 100,
556+
isDefault: true,
557+
});
558+
const workspace = createWorkspaceMock({
559+
plan,
560+
subscriptionId: null,
561+
lastChargeDate,
562+
isBlocked: false,
563+
billingPeriodEventsCount: 10,
564+
paidUntil,
565+
});
566+
567+
await fillDatabaseWithMockedData({
568+
workspace,
569+
plan,
570+
});
571+
572+
MockDate.set(currentDate);
573+
574+
/**
575+
* Act
576+
*/
577+
const worker = new PaymasterWorker();
578+
579+
await worker.start();
580+
await worker.handle(WORKSPACE_SUBSCRIPTION_CHECK);
581+
await worker.finish();
582+
583+
/**
584+
* Assert
585+
*/
586+
const updatedWorkspace = await workspacesCollection.findOne({ _id: workspace._id });
587+
588+
expect(updatedWorkspace.lastChargeDate).toEqual(currentDate);
589+
expect(updatedWorkspace.billingPeriodEventsCount).toEqual(0);
590+
591+
MockDate.reset();
592+
});
593+
594+
afterAll(async () => {
595+
await connection.close();
596+
MockDate.reset();
597+
});
545598
});

0 commit comments

Comments
 (0)