Files
RenewCMS/web/src/lib/__tests__/updateFormat.spec.ts
T

138 lines
3.9 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
AUTO_CHECK_INTERVAL_MS,
browserUpdatesDocsUrl,
displayVersion,
downloadAnnouncement,
formatDownloadProgress,
formatLastChecked,
formatReleased,
formatStartedAt,
formatStartedWithAge,
formatUpdatedAt,
isCheckDue,
} from '@/lib/updateFormat'
describe('displayVersion', () => {
it.each([
['v2.3.0-beta', '2.3.0-beta'],
['2.0.0-beta', '2.0.0-beta'],
['v2.5.0', '2.5.0'],
])('shows %s as %s', (tag, expected) => {
expect(displayVersion(tag)).toBe(expected)
})
})
describe('formatLastChecked', () => {
const now = new Date(2026, 8, 30, 12, 0, 0)
it('reads today at the local time', () => {
expect(formatLastChecked(new Date(2026, 8, 30, 9, 41).toISOString(), now)).toBe(
'Last checked today at 09:41',
)
})
it('reads yesterday at the local time', () => {
expect(formatLastChecked(new Date(2026, 8, 29, 18, 12).toISOString(), now)).toBe(
'Last checked yesterday at 18:12',
)
})
it('reads an older day as an absolute date', () => {
expect(formatLastChecked(new Date(2026, 8, 20, 7, 5).toISOString(), now)).toBe(
'Last checked 20 Sep 2026 at 07:05',
)
})
})
describe('isCheckDue', () => {
const now = Date.parse('2026-09-30T12:00:00Z')
it('is due when never checked', () => {
expect(isCheckDue(null, now)).toBe(true)
})
it('is due once the last check is older than 24 hours', () => {
expect(isCheckDue(new Date(now - AUTO_CHECK_INTERVAL_MS - 1000).toISOString(), now)).toBe(
true,
)
})
it('is not due when the last check is under 24 hours old', () => {
expect(isCheckDue(new Date(now - AUTO_CHECK_INTERVAL_MS + 1000).toISOString(), now)).toBe(
false,
)
})
})
describe('formatReleased', () => {
it('reads the release date as an absolute date', () => {
expect(formatReleased('2026-09-24T08:00:00Z')).toBe('Released 24 Sep 2026')
})
})
describe('formatDownloadProgress', () => {
it('reads the board sample as 42% and 11 of 26 MB', () => {
expect(
formatDownloadProgress({
percent: 42,
downloaded_bytes: 11534336,
total_bytes: 27262976,
}),
).toBe('42% · 11 of 26 MB')
})
})
describe('downloadAnnouncement', () => {
it.each([
[0, 'Downloading version 2.3.0-beta'],
[24, 'Downloading version 2.3.0-beta'],
[25, 'Downloading version 2.3.0-beta, 25 percent'],
[49, 'Downloading version 2.3.0-beta, 25 percent'],
[50, 'Downloading version 2.3.0-beta, 50 percent'],
[75, 'Downloading version 2.3.0-beta, 75 percent'],
[100, 'Downloading version 2.3.0-beta, 75 percent'],
])('reads %i percent as %s', (percent, expected) => {
expect(downloadAnnouncement('2.3.0-beta', percent)).toBe(expected)
})
})
describe('browserUpdatesDocsUrl', () => {
it('points the release url at the Browser updates section of the docs of the same repository', () => {
expect(
browserUpdatesDocsUrl('https://git.example/fsylvain/RenewCMS/releases/tag/v2.3.0-beta'),
).toBe(
'https://git.example/fsylvain/RenewCMS/src/branch/main/docs/deployment.md#browser-updates',
)
})
it('keeps a url it cannot interpret', () => {
expect(browserUpdatesDocsUrl('https://git.example/somewhere')).toBe(
'https://git.example/somewhere',
)
expect(browserUpdatesDocsUrl('not a url')).toBe('not a url')
})
})
describe('restart meta lines', () => {
const startedAt = '2026-09-30T09:51:00Z'
it('reads the start time', () => {
expect(formatStartedAt(startedAt)).toBe('Started at 09:51')
})
it('adds how long ago it was', () => {
expect(formatStartedWithAge(startedAt, new Date('2026-09-30T09:52:30Z'))).toBe(
'Started at 09:51 · 1 minute ago',
)
})
it.each([
['2026-09-30T09:52:00Z', 'Updated today at 09:52'],
['2026-09-29T18:12:00Z', 'Updated yesterday at 18:12'],
['2026-09-20T18:12:00Z', 'Updated 20 Sep 2026 at 18:12'],
])('reads the update time %s as %s', (iso, expected) => {
expect(formatUpdatedAt(iso, new Date('2026-09-30T10:00:00Z'))).toBe(expected)
})
})