import assert from 'node:assert/strict' import test from 'node:test' import { loadTestModule } from './load-test-module.mjs' const { creatingVMTasks, currentTask, findActiveOfflineTask, pruneFinishedTasks, taskDetail, 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(findActiveOfflineTask([vm, other, download], '26.7.3'), { primary: download, waitingVMs: ['vm one'], matchedIDs: ['download', 'vm'], }) }) 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('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']) })