|
| 1 | +import { After, Given, Then } from '@cucumber/cucumber' |
| 2 | +import { expect } from 'chai' |
| 3 | +import { randomBytes, randomUUID } from 'crypto' |
| 4 | + |
| 5 | +import { getMasterDbClient } from '../../../../src/database/client' |
| 6 | +import { SettingsStatic } from '../../../../src/utils/settings' |
| 7 | + |
| 8 | +const DAY_MS = 24 * 60 * 60 * 1000 |
| 9 | +const PLAN_INTERVAL_SECONDS = 30 * 24 * 60 * 60 |
| 10 | + |
| 11 | +const readSubscription = async (pubkey: Buffer) => { |
| 12 | + const row = await getMasterDbClient()('user_subscriptions') |
| 13 | + .where('pubkey', pubkey) |
| 14 | + .first('plan_id', 'status', 'current_period_start', 'current_period_end') |
| 15 | + |
| 16 | + expect(row, 'the subscriber has no subscription').to.exist |
| 17 | + |
| 18 | + return row |
| 19 | +} |
| 20 | + |
| 21 | +Given( |
| 22 | + 'the relay sells the plans {string} at {int} sats and {string} at {int} sats', |
| 23 | + function (firstId: string, firstSats: number, secondId: string, secondSats: number) { |
| 24 | + const settings = SettingsStatic._settings as any |
| 25 | + const plan = (id: string, sats: number) => ({ |
| 26 | + id, |
| 27 | + amount: BigInt(sats) * 1000n, |
| 28 | + intervalSeconds: PLAN_INTERVAL_SECONDS, |
| 29 | + graceSeconds: DAY_MS / 1000, |
| 30 | + }) |
| 31 | + |
| 32 | + // The OpenNode callback steps restore the settings they replaced once the scenario ends. |
| 33 | + SettingsStatic._settings = { |
| 34 | + ...settings, |
| 35 | + payments: { |
| 36 | + ...settings.payments, |
| 37 | + subscriptionPlans: [plan(firstId, firstSats), plan(secondId, secondSats)], |
| 38 | + }, |
| 39 | + } |
| 40 | + }, |
| 41 | +) |
| 42 | + |
| 43 | +const createSubscriber = async (world: any, subscription?: { planId: string; endsInDays: number }) => { |
| 44 | + const dbClient = getMasterDbClient() |
| 45 | + const pubkey = randomBytes(32) |
| 46 | + |
| 47 | + await dbClient('users').insert({ pubkey, is_admitted: false }) |
| 48 | + world.parameters.subscriberPubkey = pubkey |
| 49 | + world.parameters.subscriberPubkeys = [...(world.parameters.subscriberPubkeys ?? []), pubkey] |
| 50 | + |
| 51 | + if (!subscription) { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + const now = Date.now() |
| 56 | + const periodEnd = new Date(now + subscription.endsInDays * DAY_MS) |
| 57 | + await dbClient('user_subscriptions').insert({ |
| 58 | + pubkey, |
| 59 | + plan_id: subscription.planId, |
| 60 | + status: 'active', |
| 61 | + current_period_start: new Date(now - (30 - subscription.endsInDays) * DAY_MS), |
| 62 | + current_period_end: periodEnd, |
| 63 | + grace_ends_at: new Date(periodEnd.getTime() + DAY_MS), |
| 64 | + }) |
| 65 | + await dbClient('users').where('pubkey', pubkey).update({ is_admitted: true }) |
| 66 | + |
| 67 | + world.parameters.subscriptionEndBefore = (await readSubscription(pubkey)).current_period_end as Date |
| 68 | +} |
| 69 | + |
| 70 | +Given( |
| 71 | + 'a subscriber has a running {string} plan that ends in {int} days', |
| 72 | + async function (planId: string, days: number) { |
| 73 | + await createSubscriber(this, { planId, endsInDays: days }) |
| 74 | + }, |
| 75 | +) |
| 76 | + |
| 77 | +Given('a subscriber without a subscription', async function () { |
| 78 | + await createSubscriber(this) |
| 79 | +}) |
| 80 | + |
| 81 | +Given( |
| 82 | + 'a pending OpenNode invoice for the subscriber for {int} sats on the plan {string} exists', |
| 83 | + async function (sats: number, planId: string) { |
| 84 | + const invoiceId = `integration-subscription-${randomUUID()}` |
| 85 | + |
| 86 | + await getMasterDbClient()('invoices').insert({ |
| 87 | + id: invoiceId, |
| 88 | + pubkey: this.parameters.subscriberPubkey, |
| 89 | + bolt11: 'lnbc210n1integration', |
| 90 | + amount_requested: String(sats), |
| 91 | + unit: 'sats', |
| 92 | + status: 'pending', |
| 93 | + description: 'subscription plan change integration test', |
| 94 | + plan_id: planId, |
| 95 | + expires_at: new Date(Date.now() + 15 * 60 * 1000), |
| 96 | + updated_at: new Date(), |
| 97 | + created_at: new Date(), |
| 98 | + }) |
| 99 | + |
| 100 | + // The OpenNode callback steps post for, and clean up, this invoice. |
| 101 | + this.parameters.openNodeInvoiceId = invoiceId |
| 102 | + this.parameters.openNodeInvoiceIds = [...(this.parameters.openNodeInvoiceIds ?? []), invoiceId] |
| 103 | + }, |
| 104 | +) |
| 105 | + |
| 106 | +Then('the subscriber is still on the plan {string}', async function (planId: string) { |
| 107 | + const subscription = await readSubscription(this.parameters.subscriberPubkey) |
| 108 | + |
| 109 | + expect(subscription.plan_id).to.equal(planId) |
| 110 | + // The payment was credited to the running plan, so the subscription is neither restarted nor ended. |
| 111 | + expect(subscription.status).to.equal('active') |
| 112 | +}) |
| 113 | + |
| 114 | +Then('the subscriber is on the plan {string}', async function (planId: string) { |
| 115 | + const subscription = await readSubscription(this.parameters.subscriberPubkey) |
| 116 | + |
| 117 | + expect(subscription.plan_id).to.equal(planId) |
| 118 | + expect(subscription.status).to.equal('active') |
| 119 | +}) |
| 120 | + |
| 121 | +Then("the subscriber's plan ends {float} days later than before", async function (days: number) { |
| 122 | + const subscription = await readSubscription(this.parameters.subscriberPubkey) |
| 123 | + const before = this.parameters.subscriptionEndBefore as Date |
| 124 | + |
| 125 | + expect(subscription.current_period_end.getTime() - before.getTime()).to.equal(Math.round(days * DAY_MS)) |
| 126 | +}) |
| 127 | + |
| 128 | +Then('the subscriber has been admitted to the relay', async function () { |
| 129 | + const user = await getMasterDbClient()('users').where('pubkey', this.parameters.subscriberPubkey).first('is_admitted') |
| 130 | + |
| 131 | + expect(user.is_admitted).to.equal(true) |
| 132 | +}) |
| 133 | + |
| 134 | +After({ tags: '@subscription-plan-change' }, async function () { |
| 135 | + const pubkeys = this.parameters.subscriberPubkeys ?? [] |
| 136 | + if (pubkeys.length > 0) { |
| 137 | + // The subscription goes with the user, whose row it references. |
| 138 | + await getMasterDbClient()('users').whereIn('pubkey', pubkeys).delete() |
| 139 | + } |
| 140 | + |
| 141 | + this.parameters.subscriberPubkey = undefined |
| 142 | + this.parameters.subscriberPubkeys = [] |
| 143 | + this.parameters.subscriptionEndBefore = undefined |
| 144 | +}) |
0 commit comments