|
| 1 | +import type { AddressInfo } from 'node:net' |
| 2 | +import { mkdtemp, readFile } from 'node:fs/promises' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { afterEach, describe, expect, it } from 'vitest' |
| 6 | +import { |
| 7 | + fetchKofiSponsors, |
| 8 | + parseKofiWebhookBody, |
| 9 | + startKofiWebhookServer, |
| 10 | + storeKofiEvent, |
| 11 | +} from './kofi.ts' |
| 12 | + |
| 13 | +const servers: Awaited<ReturnType<typeof startKofiWebhookServer>>[] = [] |
| 14 | + |
| 15 | +afterEach(async () => { |
| 16 | + await Promise.all(servers.splice(0).map(server => new Promise<void>((resolve, reject) => { |
| 17 | + server.close(error => error ? reject(error) : resolve()) |
| 18 | + }))) |
| 19 | +}) |
| 20 | + |
| 21 | +describe('ko-fi provider', () => { |
| 22 | + it('verifies and sanitizes webhook payloads', () => { |
| 23 | + const event = parseKofiWebhookBody(createBody(), 'correct-token') |
| 24 | + |
| 25 | + expect(event).toMatchObject({ |
| 26 | + messageId: 'message-1', |
| 27 | + timestamp: '2026-07-27T05:00:00.000Z', |
| 28 | + type: 'Subscription', |
| 29 | + isPublic: true, |
| 30 | + fromName: 'Ada', |
| 31 | + amount: 5, |
| 32 | + currency: 'USD', |
| 33 | + isSubscriptionPayment: true, |
| 34 | + tierName: 'Gold', |
| 35 | + }) |
| 36 | + expect(event).not.toHaveProperty('verification_token') |
| 37 | + expect(event).not.toHaveProperty('email') |
| 38 | + }) |
| 39 | + |
| 40 | + it('rejects a webhook with the wrong verification token', () => { |
| 41 | + expect(() => parseKofiWebhookBody(createBody(), 'wrong-token')) |
| 42 | + .toThrow('Invalid Ko-fi verification token') |
| 43 | + }) |
| 44 | + |
| 45 | + it('scrubs the identity of private payments', () => { |
| 46 | + const event = parseKofiWebhookBody(createBody({ |
| 47 | + is_public: false, |
| 48 | + from_name: 'Private Name', |
| 49 | + email: 'private@example.com', |
| 50 | + }), 'correct-token') |
| 51 | + |
| 52 | + expect(event.fromName).toBe('Private Sponsor') |
| 53 | + expect(event.isPublic).toBe(false) |
| 54 | + }) |
| 55 | + |
| 56 | + it('groups private subscription renewals without exposing their identity', async () => { |
| 57 | + const directory = await mkdtemp(join(tmpdir(), 'sponsorkit-kofi-private-')) |
| 58 | + const dataFile = join(directory, 'events.json') |
| 59 | + const firstPayment = parseKofiWebhookBody(createBody({ |
| 60 | + message_id: 'private-payment-1', |
| 61 | + timestamp: '2026-07-01T00:00:00Z', |
| 62 | + is_public: false, |
| 63 | + from_name: 'Private Name', |
| 64 | + email: 'Private@Example.com', |
| 65 | + }), 'correct-token') |
| 66 | + const renewal = parseKofiWebhookBody(createBody({ |
| 67 | + message_id: 'private-payment-2', |
| 68 | + timestamp: '2026-07-20T00:00:00Z', |
| 69 | + is_public: false, |
| 70 | + from_name: 'Private Name', |
| 71 | + email: 'private@example.com', |
| 72 | + is_first_subscription_payment: false, |
| 73 | + }), 'correct-token') |
| 74 | + |
| 75 | + expect(firstPayment.sponsorKey).toBe(renewal.sponsorKey) |
| 76 | + await storeKofiEvent(firstPayment, dataFile) |
| 77 | + await storeKofiEvent(renewal, dataFile) |
| 78 | + |
| 79 | + const sponsors = await fetchKofiSponsors( |
| 80 | + { dataFile }, |
| 81 | + Date.parse('2026-07-27T00:00:00Z'), |
| 82 | + ) |
| 83 | + expect(sponsors).toHaveLength(1) |
| 84 | + expect(sponsors[0]).toMatchObject({ |
| 85 | + monthlyDollars: 5, |
| 86 | + privacyLevel: 'PRIVATE', |
| 87 | + sponsor: { name: 'Private Sponsor' }, |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + it('does not merge unrelated anonymous payments', () => { |
| 92 | + const firstPayment = parseKofiWebhookBody(createBody({ |
| 93 | + message_id: 'anonymous-payment-1', |
| 94 | + type: 'Tip', |
| 95 | + from_name: '', |
| 96 | + email: '', |
| 97 | + is_subscription_payment: false, |
| 98 | + }), 'correct-token') |
| 99 | + const secondPayment = parseKofiWebhookBody(createBody({ |
| 100 | + message_id: 'anonymous-payment-2', |
| 101 | + type: 'Tip', |
| 102 | + from_name: '', |
| 103 | + email: '', |
| 104 | + is_subscription_payment: false, |
| 105 | + }), 'correct-token') |
| 106 | + |
| 107 | + expect(firstPayment.fromName).toBe('Anonymous') |
| 108 | + expect(firstPayment.sponsorKey).not.toBe(secondPayment.sponsorKey) |
| 109 | + }) |
| 110 | + |
| 111 | + it('keeps public and private payments from the same identity separate', () => { |
| 112 | + const publicPayment = parseKofiWebhookBody(createBody(), 'correct-token') |
| 113 | + const privatePayment = parseKofiWebhookBody(createBody({ |
| 114 | + message_id: 'private-payment', |
| 115 | + is_public: false, |
| 116 | + }), 'correct-token') |
| 117 | + |
| 118 | + expect(publicPayment.sponsorKey).not.toBe(privatePayment.sponsorKey) |
| 119 | + }) |
| 120 | + |
| 121 | + it('deduplicates retries and aggregates recent payments', async () => { |
| 122 | + const directory = await mkdtemp(join(tmpdir(), 'sponsorkit-kofi-')) |
| 123 | + const dataFile = join(directory, 'events.json') |
| 124 | + const subscription = parseKofiWebhookBody(createBody({ |
| 125 | + timestamp: '2026-07-01T00:00:00Z', |
| 126 | + }), 'correct-token') |
| 127 | + const tip = parseKofiWebhookBody(createBody({ |
| 128 | + message_id: 'message-2', |
| 129 | + timestamp: '2026-07-05T00:00:00Z', |
| 130 | + type: 'Tip', |
| 131 | + amount: '2', |
| 132 | + is_subscription_payment: false, |
| 133 | + is_first_subscription_payment: false, |
| 134 | + tier_name: undefined, |
| 135 | + }), 'correct-token') |
| 136 | + |
| 137 | + expect(await storeKofiEvent(subscription, dataFile)).toBe(true) |
| 138 | + expect(await storeKofiEvent(subscription, dataFile)).toBe(false) |
| 139 | + expect(await storeKofiEvent(tip, dataFile)).toBe(true) |
| 140 | + |
| 141 | + const sponsors = await fetchKofiSponsors( |
| 142 | + { dataFile }, |
| 143 | + Date.parse('2026-07-20T00:00:00Z'), |
| 144 | + ) |
| 145 | + expect(sponsors).toHaveLength(1) |
| 146 | + expect(sponsors[0]).toMatchObject({ |
| 147 | + monthlyDollars: 7, |
| 148 | + privacyLevel: 'PUBLIC', |
| 149 | + isOneTime: false, |
| 150 | + sponsor: { |
| 151 | + name: 'Ada', |
| 152 | + avatarUrl: '', |
| 153 | + }, |
| 154 | + }) |
| 155 | + }) |
| 156 | + |
| 157 | + it('receives a live-shaped HTTP form post and persists it', async () => { |
| 158 | + const directory = await mkdtemp(join(tmpdir(), 'sponsorkit-kofi-http-')) |
| 159 | + const dataFile = join(directory, 'events.json') |
| 160 | + const server = await startKofiWebhookServer({ |
| 161 | + verificationToken: 'correct-token', |
| 162 | + dataFile, |
| 163 | + host: '127.0.0.1', |
| 164 | + port: 0, |
| 165 | + }) |
| 166 | + servers.push(server) |
| 167 | + const { port } = server.address() as AddressInfo |
| 168 | + |
| 169 | + const response = await fetch(`http://127.0.0.1:${port}/kofi`, { |
| 170 | + method: 'POST', |
| 171 | + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, |
| 172 | + body: createBody(), |
| 173 | + }) |
| 174 | + |
| 175 | + expect(response.status).toBe(200) |
| 176 | + expect(await response.json()).toEqual({ ok: true, stored: true }) |
| 177 | + const stored = JSON.parse(await readFile(dataFile, 'utf8')) |
| 178 | + expect(stored.events).toHaveLength(1) |
| 179 | + expect(stored.events[0]).not.toHaveProperty('verification_token') |
| 180 | + }) |
| 181 | + |
| 182 | + it('provides a browser-friendly health check', async () => { |
| 183 | + const server = await startKofiWebhookServer({ |
| 184 | + verificationToken: 'correct-token', |
| 185 | + host: '127.0.0.1', |
| 186 | + port: 0, |
| 187 | + }) |
| 188 | + servers.push(server) |
| 189 | + const { port } = server.address() as AddressInfo |
| 190 | + |
| 191 | + const response = await fetch(`http://127.0.0.1:${port}/kofi`) |
| 192 | + |
| 193 | + expect(response.status).toBe(200) |
| 194 | + await expect(response.json()).resolves.toMatchObject({ |
| 195 | + ok: true, |
| 196 | + message: expect.stringContaining('receiver is ready'), |
| 197 | + }) |
| 198 | + }) |
| 199 | +}) |
| 200 | + |
| 201 | +function createBody(overrides: Record<string, unknown> = {}) { |
| 202 | + const payload = { |
| 203 | + verification_token: 'correct-token', |
| 204 | + message_id: 'message-1', |
| 205 | + timestamp: '2026-07-27T05:00:00Z', |
| 206 | + type: 'Subscription', |
| 207 | + is_public: true, |
| 208 | + from_name: 'Ada', |
| 209 | + message: 'Thank you', |
| 210 | + amount: '5', |
| 211 | + url: 'https://ko-fi.com/example', |
| 212 | + email: 'ada@example.com', |
| 213 | + currency: 'USD', |
| 214 | + is_subscription_payment: true, |
| 215 | + is_first_subscription_payment: true, |
| 216 | + tier_name: 'Gold', |
| 217 | + kofi_transaction_id: 'transaction-1', |
| 218 | + ...overrides, |
| 219 | + } |
| 220 | + return new URLSearchParams({ data: JSON.stringify(payload) }).toString() |
| 221 | +} |
0 commit comments