Loading files
import { mkdtempSync, readFileSync, rmSync } from 'fs'
import { tmpdir } from 'os'
import { join } from 'path'
import { createHash } from 'crypto'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { canary, isLocalDevBuild, semverGt, shouldAutoUpdate, stage, startSelfUpdater } from './selfUpdate.js'
let dirs: string[] = []
function tempDir(): string {
const dir = mkdtempSync(join(tmpdir(), 'machine-adapter-self-update-'))
dirs.push(dir)
return dir
}
describe('selfUpdate packaging', () => {
afterEach(() => {
for (const dir of dirs) rmSync(dir, { recursive: true, force: true })
dirs = []
})
it('canary runs installed cli.js as ESM', () => {
const dir = tempDir()
const cli = Buffer.from('#!/usr/bin/env node\nimport { createRequire } from "module";\nconsole.log(createRequire(import.meta.url) ? "1.2.3" : "nope")\n')
expect(canary(cli, dir)).toBe(true)
})
it('stages the module package metadata next to cli.js', () => {
const dir = tempDir()
stage(dir, Buffer.from('console.log("cli")\n'), Buffer.from('console.log("notify")\n'))
expect(JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'))).toEqual({ type: 'module' })
})
})
describe('shouldAutoUpdate', () => {
it('recognises the labels install-cli.sh and version.ts produce', () => {
expect(isLocalDevBuild('0.1.56-dev.a1b2c3d')).toBe(true)
expect(isLocalDevBuild('0.1.56-dev.a1b2c3d.dirty')).toBe(true)
expect(isLocalDevBuild('0.0.0-dev')).toBe(true)
expect(isLocalDevBuild('0.1.56')).toBe(false)
expect(isLocalDevBuild('0.1.56-rc.1')).toBe(false)
expect(isLocalDevBuild('')).toBe(false)
})
it('never overwrites a local build, however far ahead the release is', () => {
// The regression this exists for: a build labelled with the core it was made level with, then
// silently replaced mid-session by the very next release.
expect(semverGt('0.1.57', '0.1.56-dev.a1b2c3d')).toBe(true)
expect(shouldAutoUpdate('0.1.57', '0.1.56-dev.a1b2c3d')).toBe(false)
expect(shouldAutoUpdate('9.9.9', '0.1.56-dev.a1b2c3d.dirty')).toBe(false)
})
it('leaves a released install on the release train', () => {
expect(shouldAutoUpdate('0.1.57', '0.1.56')).toBe(true)
expect(shouldAutoUpdate('0.1.56', '0.1.56')).toBe(false)
expect(shouldAutoUpdate('0.1.55', '0.1.56')).toBe(false)
})
})
describe('startSelfUpdater', () => {
const cliSource = Buffer.from('#!/usr/bin/env node\nimport { createRequire } from "module";\nconsole.log(createRequire(import.meta.url) ? "9.9.9" : "nope")\n')
const notifySource = Buffer.from('export {}\n')
const sha = (b: Buffer): string => createHash('sha256').update(b).digest('hex')
afterEach(() => {
vi.unstubAllGlobals()
for (const dir of dirs) rmSync(dir, { recursive: true, force: true })
dirs = []
})
function serveUpdate(): void {
vi.stubGlobal('fetch', async (url: string) => {
if (url === 'https://updates.test/metadata.json') {
return new Response(JSON.stringify({
adapter: {
version: '9.9.9',
cli: { url: 'https://updates.test/cli.js', sha256: sha(cliSource) },
notify: { url: 'https://updates.test/notify.mjs', sha256: sha(notifySource) },
},
}))
}
if (url === 'https://updates.test/cli.js') return new Response(cliSource)
if (url === 'https://updates.test/notify.mjs') return new Response(notifySource)
return new Response('', { status: 404 })
})
}
it('swaps the bytes and awaits onStaged inside ONE withLock section', async () => {
serveUpdate()
const dir = tempDir()
const events: string[] = []
let stagedResolve: () => void = () => {}
const stagedDone = new Promise<void>((r) => { stagedResolve = r })
const poller = startSelfUpdater({
currentVersion: '1.0.0',
url: 'https://updates.test/metadata.json',
key: 'adapter',
dir,
intervalMs: 60_000,
withLock: async (fn) => {
events.push('lock')
try { return await fn() } finally { events.push('unlock') }
},
onStaged: async (v) => {
events.push(`staged:${v}`)
expect(readFileSync(join(dir, 'cli.js'))).toEqual(cliSource) // swapped BEFORE the handoff runs
await new Promise((r) => setTimeout(r, 30)) // the handoff takes time…
events.push('handoff-done')
stagedResolve()
},
})
await stagedDone
await new Promise((r) => setTimeout(r, 10))
poller.stop()
expect(events).toEqual(['lock', 'staged:9.9.9', 'handoff-done', 'unlock'])
})
it('releases the lock when onStaged throws', async () => {
serveUpdate()
const dir = tempDir()
const events: string[] = []
let sawThrow: () => void = () => {}
const thrown = new Promise<void>((r) => { sawThrow = r })
const poller = startSelfUpdater({
currentVersion: '1.0.0',
url: 'https://updates.test/metadata.json',
key: 'adapter',
dir,
intervalMs: 60_000,
withLock: async (fn) => {
events.push('lock')
try { return await fn() } finally { events.push('unlock'); sawThrow() }
},
onStaged: async () => { throw new Error('teardown I/O fault') },
})
await thrown
poller.stop()
expect(events).toEqual(['lock', 'unlock'])
})
})
6aee22971This file is pinned to the selected release. Download the original to open it in your design tool.