also looking at this
fix(app): refuse a license cache this build cannot vouch for
#47
2 files
+158
-7
| @@ -1,6 +1,9 @@ | ||
| 1 | 1 | import type { LicenseState } from '../../../app/Uplink/license' |
| 2 | import { describe, expect, it } from 'bun:test' | |
| 3 | import { checkLicenseKey, describeLicense, isLicensed, keyFromLink, normalizeKey, OFFLINE_GRACE_MS, portalUrl } from '../../../app/Uplink/license' | |
| 2 | import { afterEach, describe, expect, it } from 'bun:test' | |
| 3 | import { mkdtempSync, rmSync } from 'node:fs' | |
| 4 | import { tmpdir } from 'node:os' | |
| 5 | import { join } from 'node:path' | |
| 6 | import { checkLicenseKey, describeLicense, isLicensed, keyFromLink, normalizeKey, OFFLINE_GRACE_MS, parseLicenseState, portalUrl, readLicenseState, writeLicenseState } from '../../../app/Uplink/license' | |
| 4 | 7 | |
| 5 | 8 | const NOW = Date.parse('2026-10-01T12:00:00Z') |
| 6 | 9 | const KEY = 'UPLK-ABCD-EFGH-JKLM-NPQR' |
| @@ -68,3 +71,85 @@ describe('activation links', () => { | ||
| 68 | 71 | expect(normalizeKey('UPLK-0000-1111-2222-3333')).toBeNull() |
| 69 | 72 | }) |
| 70 | 73 | }) |
| 74 | ||
| 75 | const dirs: string[] = [] | |
| 76 | function tempDir(): string { | |
| 77 | const dir = mkdtempSync(join(tmpdir(), 'uplink-license-')) | |
| 78 | dirs.push(dir) | |
| 79 | return dir | |
| 80 | } | |
| 81 | afterEach(() => { | |
| 82 | for (const dir of dirs.splice(0)) | |
| 83 | rmSync(dir, { recursive: true, force: true }) | |
| 84 | }) | |
| 85 | ||
| 86 | describe('the cached answer', () => { | |
| 87 | it('reads back exactly what it wrote', () => { | |
| 88 | const path = join(tempDir(), 'license.json') | |
| 89 | writeLicenseState(state(), path) | |
| 90 | expect(readLicenseState(path)).toEqual(state()) | |
| 91 | ||
| 92 | writeLicenseState(state({ plan: 'lifetime', expiresAt: null, email: null }), path) | |
| 93 | expect(readLicenseState(path)).toEqual(state({ plan: 'lifetime', expiresAt: null, email: null })) | |
| 94 | }) | |
| 95 | ||
| 96 | /** | |
| 97 | * 0.1.4 shipped before the version existed, so a Mac activated then has an | |
| 98 | * unstamped file. Refusing it would ask exactly the wrong people, the ones | |
| 99 | * off-grid, to connect. The shape has not changed, so it is this version. | |
| 100 | */ | |
| 101 | it('accepts the unstamped file 0.1.4 wrote', () => { | |
| 102 | const { version, ...unstamped } = { version: 1, ...state() } | |
| 103 | expect(version).toBe(1) | |
| 104 | expect(parseLicenseState(unstamped)).toEqual(state()) | |
| 105 | }) | |
| 106 | ||
| 107 | it('refuses a version it does not understand rather than reading it', () => { | |
| 108 | expect(parseLicenseState({ ...state(), version: 2 })).toBeNull() | |
| 109 | expect(parseLicenseState({ ...state(), version: 0 })).toBeNull() | |
| 110 | expect(parseLicenseState({ ...state(), version: 'one' })).toBeNull() | |
| 111 | }) | |
| 112 | ||
| 113 | /** | |
| 114 | * The file entitles a Mac for two weeks without reaching the server, so | |
| 115 | * anything it is wrong about makes the whole file not evidence. Null just | |
| 116 | * means the launch asks the server, which it does anyway. | |
| 117 | */ | |
| 118 | it('refuses a file that is wrong about any field', () => { | |
| 119 | expect(parseLicenseState({ ...state(), valid: 'true' })).toBeNull() | |
| 120 | expect(parseLicenseState({ ...state(), checkedAt: '123' })).toBeNull() | |
| 121 | expect(parseLicenseState({ ...state(), checkedAt: Number.NaN })).toBeNull() | |
| 122 | expect(parseLicenseState({ ...state(), plan: 'enterprise' })).toBeNull() | |
| 123 | expect(parseLicenseState({ ...state(), status: 42 })).toBeNull() | |
| 124 | expect(parseLicenseState({ ...state(), endsAtPeriodEnd: 'no' })).toBeNull() | |
| 125 | expect(parseLicenseState({ ...state(), email: 5 })).toBeNull() | |
| 126 | expect(parseLicenseState({ ...state(), expiresAt: 0 })).toBeNull() | |
| 127 | // And the shapes that are not a state at all. | |
| 128 | for (const body of [null, undefined, [], 'UPLK', 7, {}]) | |
| 129 | expect(parseLicenseState(body)).toBeNull() | |
| 130 | }) | |
| 131 | ||
| 132 | it('survives a file that is missing or not JSON', () => { | |
| 133 | const path = join(tempDir(), 'license.json') | |
| 134 | expect(readLicenseState(path)).toBeNull() | |
| 135 | Bun.write(path, '{ not json') | |
| 136 | expect(readLicenseState(path)).toBeNull() | |
| 137 | }) | |
| 138 | ||
| 139 | /** | |
| 140 | * `checkLicenseKey` writes what the server said, so a plan name this build | |
| 141 | * does not know must be refused there rather than stored and refused on the | |
| 142 | * next read, which would cost the Mac its cache for no reason. | |
| 143 | */ | |
| 144 | it('never stores a plan it would refuse to read back', async () => { | |
| 145 | const result = await checkLicenseKey(KEY, answering(200, { valid: true, plan: 'enterprise', status: 'active', email: null, expiresAt: null, endsAtPeriodEnd: false }), NOW) | |
| 146 | expect(result.kind).toBe('answered') | |
| 147 | if (result.kind !== 'answered') | |
| 148 | return | |
| 149 | expect(result.state.plan).toBeNull() | |
| 150 | ||
| 151 | const path = join(tempDir(), 'license.json') | |
| 152 | writeLicenseState(result.state, path) | |
| 153 | expect(readLicenseState(path)).toEqual(result.state) | |
| 154 | }) | |
| 155 | }) | |