import assert from 'node:assert/strict' import test from 'node:test' import { loadTestModule } from './load-test-module.mjs' const { creatingVMTasks, currentTask, findActiveCachedTask, findActiveInstallerTask, findRunningRestoreTask, groupUnmatchedInstallerTasks, pruneFinishedTasks, taskDetail, taskStopAction, taskTitle, upsertTask } = await loadTestModule(`export * from './lib/tasks'`) const task = (id, overrides = {}) => ({ id, kind: 'vm-create', subject: id, status: 'running', started_at: 10, updated_at: 20, installer_kind: 'offline', installer_version: '26.7.3', installer_phase: 'downloading', ...overrides, }) test('late progress cannot replace a newer completed task', () => { const completed = task('vm', { status: 'done', updated_at: 30 }) const tasks = { vm: completed } assert.deepEqual(upsertTask(tasks, task('vm')), tasks) assert.deepEqual(pruneFinishedTasks(tasks, 30), tasks) assert.deepEqual(pruneFinishedTasks(tasks, 31), {}) }) test('the status bar favors active work over a newer queued download', () => { const vm = task('vm') const queued = task('queued', { installer_phase: 'queued', updated_at: 25 }) const finished = task('done', { status: 'done', updated_at: 30 }) assert.deepEqual(currentTask({ queued, vm, finished }), { task: vm, running: 2 }) }) test('one installer download represents the VM creations waiting for it', () => { const download = task('download', { kind: 'installer-download', started_at: 15 }) const vm = task('vm', { vm: 'vm one' }) const other = task('other', { installer_version: '15.8.3' }) assert.deepEqual(findActiveInstallerTask([vm, other, download], 'offline', '26.7.3'), { primary: download, waitingVMs: ['vm one'], matchedIDs: ['download', 'vm'], }) }) test('native and Windows installers of the same version keep separate download state', () => { const win = task('win', { kind: 'installer-download' }) const mac = task('mac', { kind: 'installer-download', installer_kind: 'ipsw' }) const other = task('other', { kind: 'installer-download', installer_kind: 'ipsw', installer_version: '27.0' }) const tasks = [win, mac, other] assert.equal(findActiveInstallerTask(tasks, 'ipsw', '26.7.3')?.primary, mac) assert.equal(findActiveInstallerTask(tasks, 'offline', '26.7.3')?.primary, win) assert.equal(findActiveCachedTask(tasks, { kind: 'ipsw', version: '26.7.3', partial: true })?.primary, mac) assert.deepEqual(groupUnmatchedInstallerTasks(tasks, new Set()).map((match) => match.primary.id), ['win', 'mac', 'other']) assert.deepEqual(taskStopAction('en-US', mac), { kind: 'pause', label: 'Pause download' }) }) test('taskDetail formats download speeds under 1 MB/s in KB/s', () => { assert.equal(taskDetail(task('slow', { done: 5e6, total: 50e6, rate: 512e3 })), '5 MB / 50 MB · 512 KB/s') assert.equal(taskDetail(task('tiny', { done: 1e6, total: 10e6, rate: 200 })), '1 MB / 10 MB · 1 KB/s') assert.equal(taskDetail(task('fast', { done: 2e9, total: 10e9, rate: 12.5e6 })), '2.0 GB / 10.0 GB · 12.5 MB/s') assert.equal(taskDetail(task('gig', { done: 2e9, total: 10e9, rate: 1.2e9 })), '2.0 GB / 10.0 GB · 1.2 GB/s') }) test('guest restore is labeled as installing and can be canceled, not paused', () => { const restore = task('macOS', { kind: 'vm-restore', installer_kind: '', installer_phase: '' }) assert.equal(taskTitle('en-US', restore), 'Installing macOS on macOS') assert.deepEqual(taskStopAction('en-US', restore), { kind: 'cancel', label: 'Cancel creation' }) }) test('restore progress and cancel target the matching driver and case-preserved VM name', () => { const running = task('native', { kind: 'vm-restore', vm: 'builtin.driver.apple-vz:Test VM' }) const otherDriver = { ...running, id: 'other', vm: 'example.driver:Test VM' } const finished = { ...running, id: 'finished', status: 'done' } const tasks = [otherDriver, finished, running] assert.equal(findRunningRestoreTask(tasks, 'builtin.driver.apple-vz', ' Test VM '), running) assert.equal(findRunningRestoreTask(tasks, 'example.driver', 'Test VM'), otherDriver) assert.equal(findRunningRestoreTask(tasks, 'builtin.driver.apple-vz', 'test vm'), undefined) assert.equal(findRunningRestoreTask(tasks, '', 'Test VM'), undefined) }) test('creatingVMTasks lists only running vm-create tasks not yet present in vms, oldest first', () => { const first = task('t1', { subject: 'macos-a', started_at: 10 }) const second = task('t2', { subject: 'macos-b', started_at: 20 }) const registered = task('t3', { subject: 'macos-ready', started_at: 5 }) const finished = task('t4', { subject: 'macos-done', status: 'done', started_at: 1 }) const download = task('t5', { kind: 'installer-download', subject: '26.7.3', started_at: 2 }) const got = creatingVMTasks({ second, first, registered, finished, download }, new Set(['macos-ready'])) assert.deepEqual(got.map((x) => x.subject), ['macos-a', 'macos-b']) })