Skip to content

src/__tests__/credits.test.ts

35 lines · 1.7 KB · TypeScript
Raw
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { readCredits } from '../credits-core.ts'

// The shape of a live answer, with synthetic values.
const answer = (details: unknown[], perPeriod: unknown = 2) => ({
	customer_information: {
		customer_id: 'synthetic-id',
		credits_fulfillment_data: { credits_to_be_received: perPeriod, time_interval: 'Month' },
		subscription: { subscription_details: details },
	},
})

test('reads the next charge and the credits it adds', () => {
	const summary = readCredits(answer([{ next_bill_date: '2026-10-15T07:00:00Z', next_bill_amount: { amount: 29.899999618530273, currency: 'CAD' } }]))
	assert.deepEqual(summary.nextCharge, { date: '2026-10-15', credits: 2, cents: 2990, currency: 'CAD' })
	assert.equal(summary.credits, null)
})

test('a plan without a next bill gives no next charge', () => {
	assert.equal(readCredits(answer([{ status: 'Cancelled', next_bill_amount: { amount: 0 } }])).nextCharge, null)
	assert.equal(readCredits(answer([{ next_bill_date: '2026-10-15', next_bill_amount: { amount: 14.95 } }], 0)).nextCharge, null)
	assert.equal(readCredits(null).nextCharge, null)
})

test('reads a balance under a credit balance key', () => {
	assert.equal(readCredits({ customer_benefits: { credit_summary: { available_credit_count: 3 } } }).credits, 3)
})

test('returns key paths and numbers, never other values', () => {
	const summary = readCredits(answer([{ next_bill_date: '2026-10-15', next_bill_amount: { amount: 14.95 } }]))
	assert.ok(summary.fieldPaths.includes('customer_information.customer_id'))
	assert.equal(summary.numbers['customer_information.credits_fulfillment_data.credits_to_be_received'], 2)
	assert.ok(!JSON.stringify(summary).includes('synthetic-id'))
})