|
| 1 | +import { afterEach, beforeEach, expect, spyOn, test } from 'bun:test'; |
| 2 | +import * as childProcess from 'child_process'; |
| 3 | +import { EventEmitter } from 'events'; |
| 4 | +import fs from 'fs'; |
| 5 | +import { |
| 6 | + accountCacheFile, |
| 7 | + printAccountInfo, |
| 8 | + showCurrentAccount, |
| 9 | +} from '../src/account'; |
| 10 | +import * as api from '../src/api'; |
| 11 | + |
| 12 | +const now = Date.parse('2026-09-17T00:00:00Z'); |
| 13 | +const day = 86400000; |
| 14 | +let log: ReturnType<typeof spyOn>; |
| 15 | +let warn: ReturnType<typeof spyOn>; |
| 16 | +let get: ReturnType<typeof spyOn>; |
| 17 | +let session: ReturnType<typeof spyOn>; |
| 18 | +let fork: ReturnType<typeof spyOn>; |
| 19 | +let read: ReturnType<typeof spyOn>; |
| 20 | +let worker: any; |
| 21 | + |
| 22 | +beforeEach(() => { |
| 23 | + worker = Object.assign(new EventEmitter(), { |
| 24 | + send() {}, |
| 25 | + unref() {}, |
| 26 | + channel: { unref() {} }, |
| 27 | + }); |
| 28 | + fork = spyOn(childProcess, 'fork').mockReturnValue(worker); |
| 29 | + read = spyOn(fs, 'readFileSync').mockImplementation(() => { |
| 30 | + throw new Error('no cache'); |
| 31 | + }); |
| 32 | + log = spyOn(console, 'log').mockImplementation(() => {}); |
| 33 | + warn = spyOn(console, 'warn').mockImplementation(() => {}); |
| 34 | + get = spyOn(api, 'get').mockResolvedValue({ |
| 35 | + email: 'test@example.com', |
| 36 | + tier: 'pro', |
| 37 | + }); |
| 38 | + session = spyOn(api, 'getSession').mockReturnValue({ token: 'test-token' }); |
| 39 | +}); |
| 40 | +afterEach(() => { |
| 41 | + fork.mockRestore(); |
| 42 | + read.mockRestore(); |
| 43 | + log.mockRestore(); |
| 44 | + warn.mockRestore(); |
| 45 | + get.mockRestore(); |
| 46 | + session.mockRestore(); |
| 47 | +}); |
| 48 | + |
| 49 | +for (const days of [0, 1, 29, 29.99, 30, 31, -1]) { |
| 50 | + test(`Pushy renewal boundary: ${days} days`, () => { |
| 51 | + printAccountInfo( |
| 52 | + { |
| 53 | + email: 'test@example.com', |
| 54 | + tier: 'pro', |
| 55 | + tierExpiresAt: new Date(now + days * day).toISOString(), |
| 56 | + serverTime: new Date(now).toISOString(), |
| 57 | + }, |
| 58 | + false, |
| 59 | + now + 100 * day, |
| 60 | + ); |
| 61 | + expect(warn).toHaveBeenCalledTimes(days >= 0 && days < 30 ? 1 : 0); |
| 62 | + expect(log.mock.calls[0][0]).toContain('test@example.com'); |
| 63 | + expect(log.mock.calls[0][0]).toContain('pro'); |
| 64 | + expect(log.mock.calls[0][0]).toContain( |
| 65 | + new Date(now + days * day).toISOString(), |
| 66 | + ); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +test('Cresc and free accounts never receive renewal reminders', () => { |
| 71 | + const tierExpiresAt = new Date(now + day).toISOString(); |
| 72 | + printAccountInfo({ tier: 'pro', tierExpiresAt }, true, now); |
| 73 | + printAccountInfo({ tier: 'free', tierExpiresAt }, false, now); |
| 74 | + expect(warn).not.toHaveBeenCalled(); |
| 75 | +}); |
| 76 | + |
| 77 | +test('missing and invalid expiry dates never produce renewal reminders', () => { |
| 78 | + for (const tierExpiresAt of [undefined, null, '', 'invalid']) { |
| 79 | + printAccountInfo({ tier: 'pro', tierExpiresAt }, false, now); |
| 80 | + } |
| 81 | + expect(warn).not.toHaveBeenCalled(); |
| 82 | +}); |
| 83 | + |
| 84 | +test('uses local time when server time is unavailable', () => { |
| 85 | + printAccountInfo( |
| 86 | + { tier: 'custom', tierExpiresAt: new Date(now + day).toISOString() }, |
| 87 | + false, |
| 88 | + now, |
| 89 | + ); |
| 90 | + expect(warn).toHaveBeenCalledTimes(1); |
| 91 | +}); |
| 92 | + |
| 93 | +test('without a local session no account request is made', async () => { |
| 94 | + session.mockReturnValue(undefined); |
| 95 | + await showCurrentAccount(); |
| 96 | + expect(get).not.toHaveBeenCalled(); |
| 97 | +}); |
| 98 | + |
| 99 | +test('refresh starts without awaiting the network', () => { |
| 100 | + expect(showCurrentAccount()).toBeUndefined(); |
| 101 | + expect(fork).toHaveBeenCalledTimes(1); |
| 102 | + expect(get).not.toHaveBeenCalled(); |
| 103 | + worker.emit('message', { |
| 104 | + account: { email: 'fresh@example.com' }, |
| 105 | + fetchedAt: Date.now(), |
| 106 | + }); |
| 107 | + expect(log.mock.calls[0][0]).toContain('fresh@example.com'); |
| 108 | +}); |
| 109 | + |
| 110 | +test('cached result displays immediately and is not printed twice', () => { |
| 111 | + read.mockReturnValue( |
| 112 | + JSON.stringify({ |
| 113 | + account: { email: 'cached@example.com' }, |
| 114 | + fetchedAt: Date.now(), |
| 115 | + }), |
| 116 | + ); |
| 117 | + showCurrentAccount(); |
| 118 | + expect(log.mock.calls[0][0]).toContain('cached@example.com'); |
| 119 | + worker.emit('message', { |
| 120 | + account: { email: 'fresh@example.com' }, |
| 121 | + fetchedAt: Date.now(), |
| 122 | + }); |
| 123 | + expect(log).toHaveBeenCalledTimes(1); |
| 124 | +}); |
| 125 | + |
| 126 | +test('cached server clock advances so renewal reminders remain timely', () => { |
| 127 | + read.mockReturnValue( |
| 128 | + JSON.stringify({ |
| 129 | + account: { |
| 130 | + tier: 'pro', |
| 131 | + serverTime: new Date(now).toISOString(), |
| 132 | + tierExpiresAt: new Date(now + 35 * day).toISOString(), |
| 133 | + }, |
| 134 | + fetchedAt: Date.now() - 10 * day, |
| 135 | + }), |
| 136 | + ); |
| 137 | + showCurrentAccount(); |
| 138 | + expect(warn).toHaveBeenCalledTimes(1); |
| 139 | +}); |
| 140 | + |
| 141 | +test('worker failures do not block the command', () => { |
| 142 | + fork.mockImplementation(() => { |
| 143 | + throw new Error('spawn failed'); |
| 144 | + }); |
| 145 | + expect(showCurrentAccount()).toBeUndefined(); |
| 146 | +}); |
| 147 | + |
| 148 | +test('cache keys isolate credentials and never contain tokens', () => { |
| 149 | + expect(accountCacheFile('one')).not.toBe(accountCacheFile('two')); |
| 150 | + expect(accountCacheFile('one', 'api')).not.toBe(accountCacheFile('one')); |
| 151 | + expect(accountCacheFile('secret-token')).not.toContain('secret-token'); |
| 152 | +}); |
0 commit comments