Files
RenewCMS/web/src/views/__tests__/updateTestKit.ts
T

165 lines
4.2 KiB
TypeScript

import { vi } from 'vitest'
import type { UpdateStatus } from '@/composables/useUpdate'
export const RUNNING_VERSION = 'v2.2.0-beta'
export const NEW_TAG = 'v2.3.0-beta'
export const RELEASE_URL = 'https://git.example/fsylvain/RenewCMS/releases/tag/v2.3.0-beta'
export const RELEASED_AT = '2026-09-24T08:00:00Z'
export const NOTE_ITEMS = [
'Images upload faster, and large images no longer stall.',
'Fixed a problem where a draft could show an old title after saving.',
'Small improvements to the Users page.',
]
export const STATUS_PATH = '/v1/system/update'
export const CHECK_PATH = '/v1/system/update/check'
export const DOWNLOAD_PATH = '/v1/system/update/download'
export const RESTART_PATH = '/v1/system/update/restart'
export const BOARD_TOTAL_BYTES = 27262976
export const BOARD_DONE_BYTES = 11534336
export function hoursAgo(hours: number) {
return new Date(Date.now() - hours * 60 * 60 * 1000).toISOString()
}
export function upToDate(overrides: Partial<UpdateStatus> = {}): UpdateStatus {
return {
current: RUNNING_VERSION,
latest: RUNNING_VERSION,
available: false,
release_url: RELEASE_URL,
notes: {
version: RUNNING_VERSION,
released_at: RELEASED_AT,
items: [],
},
checked_at: hoursAgo(1),
can_apply: false,
state: 'idle',
progress: null,
error: null,
started_at: null,
finished_at: null,
container: false,
...overrides,
}
}
export function newerAvailable(overrides: Partial<UpdateStatus> = {}): UpdateStatus {
return upToDate({
latest: NEW_TAG,
available: true,
notes: {
version: NEW_TAG,
released_at: RELEASED_AT,
items: NOTE_ITEMS,
},
...overrides,
})
}
export function stubClipboard() {
const writeText = vi.fn().mockResolvedValue(undefined)
Object.defineProperty(navigator, 'clipboard', {
configurable: true,
value: { writeText },
})
return writeText
}
export function visibleButtonLabels(root: ParentNode = document.body) {
return Array.from(root.querySelectorAll('button'))
.map((button) => button.textContent?.trim() ?? '')
.filter((label) => label !== '')
}
export function findButton(label: string, root: ParentNode = document.body) {
return Array.from(root.querySelectorAll('button')).find(
(button) => button.textContent?.trim() === label,
)
}
export function startable(overrides: Partial<UpdateStatus> = {}): UpdateStatus {
return newerAvailable({ can_apply: true, ...overrides })
}
export function downloading(percent = 42): UpdateStatus {
const downloaded =
percent === 42 ? BOARD_DONE_BYTES : Math.floor((BOARD_TOTAL_BYTES * percent) / 100)
return startable({
state: 'downloading',
started_at: '2026-09-30T09:51:00Z',
progress: {
percent,
downloaded_bytes: downloaded,
total_bytes: BOARD_TOTAL_BYTES,
},
})
}
export function verifying(): UpdateStatus {
return startable({ state: 'verifying', started_at: '2026-09-30T09:51:00Z' })
}
export function ready(): UpdateStatus {
return startable({ state: 'ready', started_at: '2026-09-30T09:51:00Z' })
}
export function failedWith(error: 'download' | 'verification'): UpdateStatus {
return startable({
state: 'failed',
error,
started_at: '2026-09-30T09:51:00Z',
})
}
export const RESTART_STARTED_AT = '2026-09-30T09:51:00Z'
export const RESTART_FINISHED_AT = '2026-09-30T09:52:00Z'
export function restarting(): UpdateStatus {
return startable({ state: 'restarting', started_at: RESTART_STARTED_AT })
}
export function updateSucceeded(): UpdateStatus {
return upToDate({
current: NEW_TAG,
latest: NEW_TAG,
available: false,
can_apply: true,
state: 'succeeded',
started_at: RESTART_STARTED_AT,
finished_at: RESTART_FINISHED_AT,
})
}
export function rolledBack(error: 'rolled_back' | 'install' = 'rolled_back'): UpdateStatus {
return startable({
state: 'failed',
error,
started_at: RESTART_STARTED_AT,
finished_at: RESTART_FINISHED_AT,
})
}
export function plainRestartDone(): UpdateStatus {
return upToDate({
can_apply: true,
state: 'idle',
started_at: null,
finished_at: RESTART_FINISHED_AT,
})
}
export const PURGE_SETTINGS_PATH = '/v1/media/purge-settings'
export const DEFAULT_PURGE_SETTINGS = {
auto_purge_enabled: false,
auto_purge_interval_days: 30,
last_purge_at: null,
last_purge_merged: 0,
last_purge_articles_updated: 0,
enabled_at: null,
next_purge_at: null,
}