Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/auth/tests/auth.integration.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ describe('Auth integration tests:', () => {
expect(result.body.data.billing).toBeDefined();
expect(typeof result.body.data.billing.enabled).toBe('boolean');
expect(typeof result.body.data.billing.meterMode).toBe('boolean');
expect(result.body.data.billing.equivalences).toBeNull();
expect(result.body.data.billing.equivalences).toEqual(config.billing?.equivalences ?? null);
});

test('should reflect billing.meterMode=true when enabled (authenticated)', async () => {
Expand Down
26 changes: 18 additions & 8 deletions modules/billing/tests/billing.lifecycle.integration.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,29 @@ describe('Billing meter lifecycle integration tests:', () => {
});

test('plan.changed webhook updates active week quota snapshot mid-week', async () => {
// Pick two distinct plan ids from the project's enum so the test runs on any downstream
// (upstream defaults expose no plans → fall back to legacy 'starter'/'pro').
Comment thread
PierreBrisorgueil marked this conversation as resolved.
Outdated
const plans = Array.isArray(config.billing.plans) && config.billing.plans.length >= 2
Comment thread
PierreBrisorgueil marked this conversation as resolved.
Outdated
? config.billing.plans
: ['starter', 'pro'];
const initialPlan = plans[0];
const upgradePlan = plans[plans.length - 1];
Comment thread
PierreBrisorgueil marked this conversation as resolved.
const initialVersion = `${initialPlan}-v1`;
const upgradeVersion = `${upgradePlan}-v2`;

config.billing.planDefinitions = [
{ planId: 'starter', version: 'starter-v1', meterQuota: 100, ratios: { scrap: 1 } },
{ planId: 'pro', version: 'pro-v2', meterQuota: 1000, ratios: { scrap: 1 } },
{ planId: initialPlan, version: initialVersion, meterQuota: 100, ratios: { scrap: 1 } },
{ planId: upgradePlan, version: upgradeVersion, meterQuota: 1000, ratios: { scrap: 1 } },
];

const organizationId = new mongoose.Types.ObjectId();
const weekKey = isoWeekKey(new Date());
await Organization.create({ _id: organizationId, name: 'Lifecycle Org', slug: 'lifecycle-org', plan: 'starter' });
await Organization.create({ _id: organizationId, name: 'Lifecycle Org', slug: 'lifecycle-org', plan: initialPlan });
await Subscription.create({
organization: organizationId,
stripeCustomerId: 'cus_lifecycle',
stripeSubscriptionId: 'sub_lifecycle',
plan: 'starter',
plan: initialPlan,
status: 'active',
});
await BillingUsage.create({
Expand All @@ -92,7 +102,7 @@ describe('Billing meter lifecycle integration tests:', () => {
counters: {},
meterUsed: 25,
meterQuota: 100,
planVersion: 'starter-v1',
planVersion: initialVersion,
meterBreakdown: { scrap: 25 },
consumedAttributionKeys: [],
});
Expand All @@ -104,20 +114,20 @@ describe('Billing meter lifecycle integration tests:', () => {
current_period_end: Math.floor(Date.now() / 1000) + 30 * 24 * 60 * 60,
current_period_start: Math.floor(Date.now() / 1000) - 24 * 60 * 60,
cancel_at_period_end: false,
items: { data: [{ price: { metadata: { planId: 'pro' } } }] },
items: { data: [{ price: { metadata: { planId: upgradePlan } } }] },
},
{
data: {
previous_attributes: {
items: { data: [{ price: { metadata: { planId: 'starter' } } }] },
items: { data: [{ price: { metadata: { planId: initialPlan } } }] },
},
},
},
);

const usage = await BillingUsage.findOne({ organizationId, weekKey }).lean();
expect(usage.meterQuota).toBe(1000);
expect(usage.planVersion).toBe('pro-v2');
expect(usage.planVersion).toBe(upgradeVersion);
expect(usage.meterUsed).toBe(25);
expect(usage.meterBreakdown).toEqual({ scrap: 25 });
});
Expand Down
Loading