113 lines
4.7 KiB
JavaScript
113 lines
4.7 KiB
JavaScript
import { test, after } from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { startAppwriteMock } from './support/appwriteMock.js'
|
||
import { startFakeStripe } from './support/fakeStripe.js'
|
||
import { useTestEnv } from './support/testEnv.js'
|
||
import { schemaKeys } from './support/schema.js'
|
||
|
||
const aw = await startAppwriteMock({ schema: { subscriptions: schemaKeys('subscriptions') } })
|
||
const st = await startFakeStripe()
|
||
after(() => Promise.all([aw.close(), st.close()]))
|
||
useTestEnv({ appwriteUrl: aw.url, stripeUrl: st.url })
|
||
const { syncSubscription, ensurePrice, ensureTestCoupon, ensurePortalConfiguration } = await import('../server/services/stripeBilling.js')
|
||
|
||
const sub = (id, items, extra = {}) => ({
|
||
id,
|
||
object: 'subscription',
|
||
status: 'active',
|
||
customer: 'cus_1',
|
||
start_date: 1790000000,
|
||
current_period_end: 1792600000,
|
||
trial_end: null,
|
||
cancel_at_period_end: false,
|
||
cancel_at: null,
|
||
canceled_at: null,
|
||
ended_at: null,
|
||
metadata: { webklarCustomerId: 'c1', email: 'k@example.invalid', termsVersion: '2026-09-25-v3' },
|
||
items: { object: 'list', data: items },
|
||
...extra,
|
||
})
|
||
const item = (id, lookup_key, unit_amount, quantity = 1) => ({
|
||
id: `si_${id}`,
|
||
object: 'subscription_item',
|
||
quantity,
|
||
price: { id: `price_${id}`, object: 'price', lookup_key, unit_amount },
|
||
})
|
||
|
||
test('Setup-Skript legt productKey an', () => {
|
||
assert.ok(schemaKeys('subscriptions').includes('productKey'))
|
||
})
|
||
|
||
test('Spiegel: Hosting-Abo, Zusatzspeicher steht vorne', async () => {
|
||
st.subscriptions.set(
|
||
'sub_h',
|
||
sub('sub_h', [item('s', 'webklar_mailspeicher_5gb_monatlich_100', 100, 2), item('h', 'webklar_hosting_monatlich_1500', 1500)])
|
||
)
|
||
const doc = await syncSubscription('sub_h')
|
||
assert.equal(doc.productKey, 'hosting')
|
||
assert.equal(doc.planKey, 'webklar_hosting_monatlich_1500')
|
||
assert.equal(doc.priceAmount, 1500)
|
||
})
|
||
|
||
test('Spiegel: Wartungs-Abo wie bisher', async () => {
|
||
st.subscriptions.set('sub_w', sub('sub_w', [item('w', 'webklar_wartung_monatlich_5000', 5000)]))
|
||
const doc = await syncSubscription('sub_w')
|
||
assert.deepEqual([doc.productKey, doc.planKey, doc.priceAmount], ['wartung', 'webklar_wartung_monatlich_5000', 5000])
|
||
})
|
||
|
||
test('Spiegel: unbekannter Preis gilt als Wartungs-Abo, Betrag vom ersten Posten', async () => {
|
||
st.subscriptions.set('sub_x', sub('sub_x', [item('x', null, 4900)]))
|
||
const doc = await syncSubscription('sub_x')
|
||
assert.deepEqual([doc.productKey, doc.planKey, doc.priceAmount], ['wartung', 'webklar_wartung_monatlich_5000', 4900])
|
||
})
|
||
|
||
// Stripe-Nachbau braucht dazu: POST /v1/products, POST /v1/prices, GET /v1/coupons/:id, POST /v1/coupons,
|
||
// GET/POST /v1/billing_portal/configurations und POST /v1/billing_portal/configurations/:id (F09)
|
||
test('ensurePrice legt Hosting einmal an', async () => {
|
||
const id1 = await ensurePrice('hosting'); const id2 = await ensurePrice('hosting')
|
||
assert.equal(id1, id2)
|
||
const created = st.calls.filter((c) => c.method === 'POST' && c.path === '/v1/prices')
|
||
assert.equal(created.length, 1)
|
||
assert.equal(created[0].body['lookup_key'], 'webklar_hosting_monatlich_1500')
|
||
assert.equal(created[0].body['unit_amount'], '1500')
|
||
})
|
||
|
||
test('Testrabatt Hosting: 14,50 € Nachlass', async () => {
|
||
assert.equal(await ensureTestCoupon('hosting'), 'WEBKLAR-TEST-050-HOSTING')
|
||
assert.equal(st.calls.find((c) => c.path === '/v1/coupons' && c.method === 'POST').body['amount_off'], '1450')
|
||
})
|
||
|
||
// F09: eine bestehende eigene Konfiguration mit der alten Wartungs-Abo-Überschrift wird angepasst,
|
||
// nicht doppelt angelegt.
|
||
test('Kundenportal: alte Überschrift → configurations.update', async () => {
|
||
st.billingPortalConfigurations.set('bpc_old', {
|
||
id: 'bpc_old',
|
||
object: 'billing_portal.configuration',
|
||
active: true,
|
||
business_profile: { headline: 'Webklar – Wartungs-Abo verwalten' },
|
||
metadata: { webklar: 'wartungs-abo' },
|
||
})
|
||
const id = await ensurePortalConfiguration()
|
||
assert.equal(id, 'bpc_old')
|
||
const updateCall = st.calls.find((c) => c.method === 'POST' && c.path === '/v1/billing_portal/configurations/bpc_old')
|
||
assert.ok(updateCall)
|
||
assert.equal(updateCall.body['business_profile[headline]'], 'WEBklar – Abo verwalten')
|
||
})
|
||
|
||
test('Spiegel: termStartAt aus Metadaten (Wechsel), sonst Leistungsbeginn; storageUnits', async () => {
|
||
st.subscriptions.set(
|
||
'sub_s',
|
||
sub('sub_s', [item('w2', 'webklar_wartung_monatlich_5000', 5000), item('s2', 'webklar_mailspeicher_5gb_monatlich_100', 100, 3)], {
|
||
metadata: {
|
||
webklarCustomerId: 'c1',
|
||
email: 'k@example.invalid',
|
||
termsVersion: '2026-09-25-v3',
|
||
termStartAt: '2026-10-05T10:00:00.000Z',
|
||
},
|
||
})
|
||
)
|
||
const doc = await syncSubscription('sub_s')
|
||
assert.equal(doc.termStartAt, '2026-10-05T10:00:00.000Z')
|
||
assert.equal(doc.storageUnits, 3)
|
||
})
|