Files

34 lines
2.3 KiB
JavaScript

import { test, after, beforeEach } 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'
// Aufbauphase (E9, N28): ohneAboBisher = der Kunde hatte noch nie ein Abo. Gemeinsame Quelle für die Team-Aktivierung und die Abgleiche.
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 { subscriptionDoc, wartungDoc, hostingDoc } = await import('./support/fixtures.js')
const { deleteDocument } = await import('../server/services/appwriteAdmin.js')
const { ohneAboBisher } = await import('../server/services/aboAufbauphase.js')
// aw.docs() liefert eine Kopie: geloescht wird ueber die API
const loescheAbos = async () => {
for (const id of aw.docs('subscriptions').keys()) await deleteDocument('subscriptions', id)
}
beforeEach(loescheAbos)
test('ohneAboBisher: wahr ohne Abo-Dokument und bei abgebrochener Bestellung, falsch bei jedem anderen Status (auch beendet), je Kunde', async () => {
assert.equal(await ohneAboBisher('c1'), true)
for (const status of ['incomplete', 'incomplete_expired']) { await loescheAbos(); aw.seed('subscriptions', subscriptionDoc('sub_x', { ...wartungDoc, status })); assert.equal(await ohneAboBisher('c1'), true, status) }
for (const status of ['active', 'trialing', 'past_due', 'canceled', 'unpaid', 'paused']) { await loescheAbos(); aw.seed('subscriptions', subscriptionDoc('sub_x', { ...wartungDoc, status })); assert.equal(await ohneAboBisher('c1'), false, status) }
await loescheAbos(); aw.seed('subscriptions', subscriptionDoc('sub_h', hostingDoc)) // Hosting zählt als Abo; das Dokument gehört c1
assert.deepEqual([await ohneAboBisher('c1'), await ohneAboBisher('c2')], [false, true])
})
test('ohneAboBisher: Lesefehler werden weitergereicht, leere oder falsche Kunden-ID ist ein TypeError', async () => {
aw.failNext({ collection: 'subscriptions', method: 'GET', status: 500 }); await assert.rejects(() => ohneAboBisher('c1'))
for (const id of ['', undefined, null, 5]) await assert.rejects(() => ohneAboBisher(id), TypeError)
})