|
| 1 | +import { createHash } from 'node:crypto' |
| 2 | +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' |
| 3 | +import { createServer } from 'node:http' |
| 4 | +import { tmpdir } from 'node:os' |
| 5 | +import { dirname, join } from 'node:path' |
| 6 | +import { fileURLToPath } from 'node:url' |
| 7 | +import { _electron as electron, expect, test } from '@playwright/test' |
| 8 | +import { getErrorMessage } from '@sim/utils/errors' |
| 9 | +import { build } from 'esbuild' |
| 10 | + |
| 11 | +const DESKTOP_DIR = fileURLToPath(new URL('..', import.meta.url)) |
| 12 | + |
| 13 | +test('the real MacUpdater waits for native staging, replaces old builds, and retries failed staging', async () => { |
| 14 | + test.skip(process.platform !== 'darwin', 'Squirrel.Mac lifecycle') |
| 15 | + const directory = mkdtempSync(join(tmpdir(), 'sim-updater-e2e-')) |
| 16 | + const reportPath = |
| 17 | + process.env.DESKTOP_UPDATER_REPORT_PATH ?? test.info().outputPath('updater.json') |
| 18 | + let app: Awaited<ReturnType<typeof electron.launch>> | undefined |
| 19 | + let offeredVersion = '2.0.0' |
| 20 | + const requests: { path: string; status: number }[] = [] |
| 21 | + const checks: { |
| 22 | + name: string |
| 23 | + status: 'passed' | 'failed' |
| 24 | + durationMs: number |
| 25 | + error?: string |
| 26 | + }[] = [] |
| 27 | + const check = async (name: string, run: () => Promise<void>) => { |
| 28 | + const started = Date.now() |
| 29 | + try { |
| 30 | + await run() |
| 31 | + checks.push({ name, status: 'passed', durationMs: Date.now() - started }) |
| 32 | + } catch (error) { |
| 33 | + checks.push({ |
| 34 | + name, |
| 35 | + status: 'failed', |
| 36 | + durationMs: Date.now() - started, |
| 37 | + error: getErrorMessage(error), |
| 38 | + }) |
| 39 | + throw error |
| 40 | + } |
| 41 | + } |
| 42 | + let snapshot: unknown |
| 43 | + const server = createServer((request, response) => { |
| 44 | + const path = new URL(request.url ?? '/', 'http://127.0.0.1').pathname |
| 45 | + requests.push({ path, status: 200 }) |
| 46 | + if (path === '/latest-mac.yml') { |
| 47 | + const archive = Buffer.from(offeredVersion) |
| 48 | + response.end( |
| 49 | + `version: ${offeredVersion}\nfiles:\n - url: Sim-${offeredVersion}-universal.zip\n sha512: ${createHash('sha512').update(archive).digest('base64')}\n size: ${archive.length}\n` |
| 50 | + ) |
| 51 | + } else { |
| 52 | + response.end(/^\/Sim-(.+)-universal.zip$/.exec(path)?.[1] ?? '') |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + try { |
| 57 | + await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve)) |
| 58 | + const address = server.address() |
| 59 | + if (!address || typeof address === 'string') throw new Error('Missing fixture address') |
| 60 | + mkdirSync(join(directory, 'user-data')) |
| 61 | + writeFileSync( |
| 62 | + join(directory, 'package.json'), |
| 63 | + JSON.stringify({ name: 'sim-updater-fixture', version: '1.0.0', main: 'main.cjs' }) |
| 64 | + ) |
| 65 | + await build({ |
| 66 | + entryPoints: [join(DESKTOP_DIR, 'e2e/fixtures/updater.ts')], |
| 67 | + outfile: join(directory, 'main.cjs'), |
| 68 | + bundle: true, |
| 69 | + platform: 'node', |
| 70 | + format: 'cjs', |
| 71 | + external: ['electron'], |
| 72 | + tsconfig: join(DESKTOP_DIR, 'tsconfig.json'), |
| 73 | + plugins: [ |
| 74 | + { |
| 75 | + name: 'approve-fixture-restart', |
| 76 | + setup(builder) { |
| 77 | + builder.onResolve({ filter: /^@\/main\/dialogs$/ }, () => ({ |
| 78 | + path: 'dialog', |
| 79 | + namespace: 'fixture', |
| 80 | + })) |
| 81 | + builder.onLoad({ filter: /.*/, namespace: 'fixture' }, () => ({ |
| 82 | + contents: |
| 83 | + 'export async function showShellDialog() { return { response: 1, checkboxChecked: false } }', |
| 84 | + })) |
| 85 | + }, |
| 86 | + }, |
| 87 | + ], |
| 88 | + }) |
| 89 | + app = await electron.launch({ |
| 90 | + args: [directory, '--use-mock-keychain'], |
| 91 | + env: { |
| 92 | + ...process.env, |
| 93 | + SIM_UPDATER_FIXTURE_DIR: directory, |
| 94 | + SIM_UPDATER_FIXTURE_ORIGIN: `http://127.0.0.1:${address.port}`, |
| 95 | + }, |
| 96 | + }) |
| 97 | + const shell = app |
| 98 | + const read = () => shell.evaluate(() => globalThis.desktopUpdaterFixture.read()) |
| 99 | + await expect |
| 100 | + .poll(() => shell.evaluate(() => Boolean(globalThis.desktopUpdaterFixture))) |
| 101 | + .toBe(true) |
| 102 | + |
| 103 | + await check('first download waits for native staging', async () => { |
| 104 | + await shell.evaluate(() => globalThis.desktopUpdaterFixture.check()) |
| 105 | + await expect.poll(async () => (await read()).nativeArchive).toBe('2.0.0') |
| 106 | + expect((await read()).state).toEqual({ |
| 107 | + status: 'downloading', |
| 108 | + version: '2.0.0', |
| 109 | + percent: 100, |
| 110 | + }) |
| 111 | + await shell.evaluate(() => globalThis.desktopUpdaterFixture.install()) |
| 112 | + expect((await read()).installed).toEqual([]) |
| 113 | + await shell.evaluate(() => globalThis.desktopUpdaterFixture.finishStaging()) |
| 114 | + expect((await read()).state).toEqual({ status: 'ready', version: '2.0.0' }) |
| 115 | + }) |
| 116 | + |
| 117 | + await check('replacement cannot restart into stale native update', async () => { |
| 118 | + offeredVersion = '2.1.0' |
| 119 | + await shell.evaluate(() => globalThis.desktopUpdaterFixture.check()) |
| 120 | + await expect.poll(async () => (await read()).nativeArchive).toBe('2.1.0') |
| 121 | + expect((await read()).state).toEqual({ |
| 122 | + status: 'downloading', |
| 123 | + version: '2.1.0', |
| 124 | + percent: 100, |
| 125 | + }) |
| 126 | + await shell.evaluate(() => globalThis.desktopUpdaterFixture.install()) |
| 127 | + expect((await read()).installed).toEqual([]) |
| 128 | + await shell.evaluate(() => globalThis.desktopUpdaterFixture.failStaging()) |
| 129 | + expect((await read()).state).toEqual({ status: 'error', version: '2.1.0' }) |
| 130 | + }) |
| 131 | + |
| 132 | + await check('cached retry installs only the verified replacement', async () => { |
| 133 | + await shell.evaluate(() => globalThis.desktopUpdaterFixture.check()) |
| 134 | + await expect |
| 135 | + .poll(async () => (await read()).state) |
| 136 | + .toEqual({ status: 'downloading', version: '2.1.0', percent: 100 }) |
| 137 | + await expect.poll(async () => (await read()).nativeArchive).toBe('2.1.0') |
| 138 | + await shell.evaluate(() => globalThis.desktopUpdaterFixture.finishStaging()) |
| 139 | + expect((await read()).state).toEqual({ status: 'ready', version: '2.1.0' }) |
| 140 | + await shell.evaluate(() => globalThis.desktopUpdaterFixture.install()) |
| 141 | + await expect.poll(async () => (await read()).installed).toEqual(['2.1.0']) |
| 142 | + }) |
| 143 | + snapshot = await read() |
| 144 | + await check( |
| 145 | + 'the next process distinguishes an incomplete update from a successful install', |
| 146 | + async () => { |
| 147 | + const checkpoint = readFileSync(join(directory, 'update-install.json'), 'utf8') |
| 148 | + await app?.close() |
| 149 | + app = await electron.launch({ |
| 150 | + args: [directory, '--use-mock-keychain'], |
| 151 | + env: { |
| 152 | + ...process.env, |
| 153 | + SIM_UPDATER_FIXTURE_DIR: directory, |
| 154 | + SIM_UPDATER_FIXTURE_ORIGIN: `http://127.0.0.1:${address.port}`, |
| 155 | + }, |
| 156 | + }) |
| 157 | + await expect |
| 158 | + .poll(() => app?.evaluate(() => globalThis.desktopUpdaterFixture?.read().events)) |
| 159 | + .toContainEqual({ |
| 160 | + name: 'update_install_result', |
| 161 | + data: { expected: '2.1.0', installed: '1.0.0', success: false }, |
| 162 | + }) |
| 163 | + await app.close() |
| 164 | + writeFileSync(join(directory, 'update-install.json'), checkpoint) |
| 165 | + writeFileSync( |
| 166 | + join(directory, 'package.json'), |
| 167 | + JSON.stringify({ name: 'sim-updater-fixture', version: '2.1.0', main: 'main.cjs' }) |
| 168 | + ) |
| 169 | + app = await electron.launch({ |
| 170 | + args: [directory, '--use-mock-keychain'], |
| 171 | + env: { |
| 172 | + ...process.env, |
| 173 | + SIM_UPDATER_FIXTURE_DIR: directory, |
| 174 | + SIM_UPDATER_FIXTURE_ORIGIN: `http://127.0.0.1:${address.port}`, |
| 175 | + }, |
| 176 | + }) |
| 177 | + await expect |
| 178 | + .poll(() => app?.evaluate(() => globalThis.desktopUpdaterFixture?.read().events)) |
| 179 | + .toContainEqual({ |
| 180 | + name: 'update_install_result', |
| 181 | + data: { expected: '2.1.0', installed: '2.1.0', success: true }, |
| 182 | + }) |
| 183 | + } |
| 184 | + ) |
| 185 | + } finally { |
| 186 | + if (!snapshot && app) |
| 187 | + snapshot = await app |
| 188 | + .evaluate(() => globalThis.desktopUpdaterFixture?.read()) |
| 189 | + .catch(() => undefined) |
| 190 | + mkdirSync(dirname(reportPath), { recursive: true }) |
| 191 | + writeFileSync( |
| 192 | + reportPath, |
| 193 | + JSON.stringify( |
| 194 | + { |
| 195 | + passed: checks.length === 4 && checks.every((check) => check.status === 'passed'), |
| 196 | + checks, |
| 197 | + requests, |
| 198 | + snapshot, |
| 199 | + }, |
| 200 | + null, |
| 201 | + 2 |
| 202 | + ) |
| 203 | + ) |
| 204 | + await app?.close() |
| 205 | + server.closeAllConnections() |
| 206 | + await new Promise<void>((resolve) => server.close(() => resolve())) |
| 207 | + rmSync(directory, { recursive: true, force: true }) |
| 208 | + } |
| 209 | +}) |
0 commit comments