Download source/tests/unit/pro.test.ts from WilburDev/earshelf: direct link, hf CLI and curl.
- Browser
- Download file 6.45 kB
-
https://huggingface.co/spaces/WilburDev/earshelf/resolve/main/source/tests/unit/pro.test.ts
- Command line
-
hf download hf://spaces/WilburDev/earshelf/source/tests/unit/pro.test.ts
-
curl -L -o pro.test.ts https://huggingface.co/spaces/WilburDev/earshelf/resolve/main/source/tests/unit/pro.test.ts
6.45 kB
| import { describe, it, expect, beforeEach } from 'vitest'; | |
| import { PRO, FREE_MINUTES, BONUS_SECONDS, addTrial, counts, freeSeconds, grantBonus, isPro, mayListen, noteGate, paywallOn, recheck, storeLicense, trialLeft, trialUsed, verifyKey } from '../../src/pro'; | |
| // tiny localStorage for node | |
| const mem = new Map<string, string>(); | |
| (globalThis as any).localStorage = { | |
| getItem: (k: string) => (mem.has(k) ? mem.get(k)! : null), | |
| setItem: (k: string, v: string) => void mem.set(k, String(v)), | |
| removeItem: (k: string) => void mem.delete(k), | |
| }; | |
| const reply = (body: unknown, status = 200) => (async () => new Response(JSON.stringify(body), { status })) as unknown as typeof fetch; | |
| beforeEach(() => mem.clear()); | |
| describe('flag and gate', () => { | |
| it('is switched on with the real Gumroad product, and the storage flag can still turn it off', () => { | |
| expect(PRO.enabled).toBe(true); | |
| expect(paywallOn()).toBe(true); | |
| expect(PRO.productId).toBe('ZWjOGDzBDsua-Kv6BhBvaA=='); | |
| expect(PRO.checkoutUrl).toBe('https://webixtech.gumroad.com/l/earshelf-pro'); | |
| expect(PRO.price).toBe('US$29'); | |
| localStorage.setItem('earshelf.pro', 'off'); | |
| expect(paywallOn()).toBe(false); | |
| }); | |
| }); | |
| describe('free minutes (time gate)', () => { | |
| const book = { format: 'epub' }; | |
| it('gives 7 minutes of listening per device, then stops', () => { | |
| expect(FREE_MINUTES).toBe(7); | |
| expect(freeSeconds()).toBe(420); | |
| expect(mayListen(book)).toBe(true); | |
| addTrial(419); | |
| expect(mayListen(book)).toBe(true); | |
| addTrial(1); | |
| expect(trialLeft()).toBe(0); | |
| expect(mayListen(book)).toBe(false); | |
| }); | |
| it('the sample, full access and the off switch are never stopped', () => { | |
| addTrial(10_000); | |
| expect(mayListen({ format: 'sample' })).toBe(true); | |
| expect(counts({ format: 'sample' })).toBe(false); | |
| localStorage.setItem('earshelf.pro', 'off'); | |
| expect(mayListen(book)).toBe(true); | |
| localStorage.removeItem('earshelf.pro'); | |
| storeLicense({ key: 'K', email: 'a@b.c', verifiedAt: Date.now() }); | |
| expect(isPro()).toBe(true); | |
| expect(mayListen(book)).toBe(true); | |
| expect(counts(book)).toBe(false); | |
| }); | |
| it('the count only goes up, and the larger copy wins', () => { | |
| addTrial(30); | |
| addTrial(-50); | |
| expect(trialUsed()).toBe(30); | |
| addTrial(0, 200); // the IndexedDB copy was further along | |
| expect(trialUsed()).toBe(200); | |
| addTrial(0, 10); | |
| expect(trialUsed()).toBe(200); | |
| }); | |
| it('a day after the stop, 2 bonus minutes once', () => { | |
| const t = 1_800_000_000_000; | |
| addTrial(500); // over the limit | |
| expect(grantBonus(t)).toBe(false); // never stopped yet | |
| noteGate(t); | |
| expect(grantBonus(t + 3_600_000)).toBe(false); // too soon | |
| expect(grantBonus(t + 86_400_000)).toBe(true); | |
| expect(trialLeft()).toBe(BONUS_SECONDS); | |
| addTrial(BONUS_SECONDS); | |
| expect(trialLeft()).toBe(0); | |
| expect(grantBonus(t + 9 * 86_400_000)).toBe(false); // only once | |
| }); | |
| }); | |
| describe('verifyKey', () => { | |
| it('sends the product id, does not count a use, and accepts a valid key', async () => { | |
| let sent = ''; | |
| const f = (async (_u: string, init: RequestInit) => { | |
| sent = String(init.body); | |
| return new Response(JSON.stringify({ success: true, purchase: { email: 'reader@example.com' } })); | |
| }) as unknown as typeof fetch; | |
| const r = await verifyKey(' ABC-\n123 ', f); // pasted from an email with a line break | |
| expect(r).toMatchObject({ ok: true, license: { key: 'ABC-123', email: 'reader@example.com' } }); | |
| expect(sent).toContain('product_id=' + encodeURIComponent(PRO.productId)); | |
| expect(sent).toContain('increment_uses_count=false'); | |
| }); | |
| it('rejects unknown, refunded and empty keys; reports network failures apart', async () => { | |
| expect(await verifyKey('x', reply({ success: false, message: 'That license does not exist' }, 404))).toEqual({ ok: false, reason: 'invalid' }); | |
| expect(await verifyKey('x', (async () => new Response('<html>portal</html>')) as unknown as typeof fetch)).toEqual({ ok: false, reason: 'network' }); | |
| expect(await verifyKey('x', reply({ success: true, purchase: { refunded: true } }))).toEqual({ ok: false, reason: 'refunded' }); | |
| expect(await verifyKey('x', reply({ success: true, purchase: { chargebacked: true } }))).toEqual({ ok: false, reason: 'refunded' }); | |
| expect(await verifyKey(' ', reply({ success: true }))).toEqual({ ok: false, reason: 'invalid' }); | |
| const down = (async () => { | |
| throw new TypeError('Failed to fetch'); | |
| }) as unknown as typeof fetch; | |
| expect(await verifyKey('x', down)).toEqual({ ok: false, reason: 'network' }); | |
| expect(await verifyKey('x', (async () => new Response('bad gateway', { status: 502 })) as unknown as typeof fetch)).toEqual({ ok: false, reason: 'network' }); | |
| }); | |
| }); | |
| describe('recheck', () => { | |
| const day = 86_400_000; | |
| it('skips a fresh key, keeps Pro when offline, removes it only on a definite no', async () => { | |
| const now = Date.now(); | |
| storeLicense({ key: 'K', email: '', verifiedAt: now - 2 * day }); | |
| expect(await recheck(now, reply({ success: false }))).toBe('skipped'); | |
| storeLicense({ key: 'K', email: '', verifiedAt: now - 40 * day }); | |
| const down = (async () => { | |
| throw new TypeError('offline'); | |
| }) as unknown as typeof fetch; | |
| expect(await recheck(now, down)).toBe('kept'); | |
| expect(isPro()).toBe(true); | |
| expect(await recheck(now, reply({ success: true, purchase: { email: 'e' } }))).toBe('kept'); | |
| // one definite "no" is not enough (could be a glitch): asked again a day later, the second removes it | |
| expect(await recheck(now + 40 * day, reply({ success: false }, 404))).toBe('kept'); | |
| expect(isPro()).toBe(true); | |
| expect(await recheck(now + 40 * day + 2 * day, reply({ success: false }, 404))).toBe('removed'); | |
| expect(isPro()).toBe(false); | |
| }); | |
| it('a Wi-Fi login page or rate limit never removes Pro', async () => { | |
| const now = Date.now(); | |
| storeLicense({ key: 'K', email: '', verifiedAt: now - 40 * day }); | |
| const portal = (async () => new Response('<html>Sign in to Wi-Fi</html>', { status: 200 })) as unknown as typeof fetch; | |
| expect(await recheck(now, portal)).toBe('kept'); | |
| expect(await recheck(now, reply({ error: 'slow down' }, 429))).toBe('kept'); | |
| expect(await recheck(now, (async () => new Response('forbidden', { status: 403 })) as unknown as typeof fetch)).toBe('kept'); | |
| expect(isPro()).toBe(true); | |
| }); | |
| }); | |