diff --git a/packages/backend/src/models/user.test.js b/packages/backend/src/models/user.test.js index 77204c2d..e02b5b67 100644 --- a/packages/backend/src/models/user.test.js +++ b/packages/backend/src/models/user.test.js @@ -1063,4 +1063,46 @@ describe('User model', () => { expect(await user.withinLimits()).toBe(false); }); }); + + describe('getPlanAndUsage', () => { + it('should return plan and usage', async () => { + const user = await createUser(); + + const subscription = await createSubscription({ userId: user.id }); + + expect(await user.getPlanAndUsage()).toStrictEqual({ + usage: { + task: 0, + }, + plan: { + id: subscription.paddlePlanId, + name: '10k - monthly', + limit: '10,000', + }, + }); + }); + + it('should return trial plan and usage if no subscription exists', async () => { + const user = await createUser(); + + expect(await user.getPlanAndUsage()).toStrictEqual({ + usage: { + task: 0, + }, + plan: { + id: null, + name: 'Free Trial', + limit: null, + }, + }); + }); + + it('should throw not found when the current usage data does not exist', async () => { + vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(false); + + const user = await createUser(); + + expect(() => user.getPlanAndUsage()).rejects.toThrow('NotFoundError'); + }); + }); });