File size: 6,448 Bytes
4a58e48
b1876f7
4a58e48
 
 
 
 
 
 
 
 
 
 
 
 
 
daa9f5e
 
 
 
 
4a58e48
daa9f5e
 
4a58e48
b1876f7
 
 
 
 
 
 
 
 
 
 
 
 
4a58e48
b1876f7
 
 
 
 
 
 
4a58e48
 
b1876f7
 
 
 
 
 
 
 
 
 
 
4a58e48
b1876f7
 
 
 
 
 
 
 
 
 
 
4a58e48
 
 
 
 
 
 
 
 
 
b1876f7
4a58e48
daa9f5e
4a58e48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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);
  });
});