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

134 lines
4.1 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { initializeLocale, setLocale } from '@/i18n'
import type { WorkspaceSettings } from '@/composables/useSettings'
import { recentWhenLabel } from '../activityTime'
import {
formatLogSize,
helperCopy,
isShorter,
isSmaller,
sizeHelperCopy,
lastCleanupLine,
nextCleanupLine,
shorteningDescription,
} from '../activityRetention'
const NOW = new Date(2026, 9, 6, 15, 0)
function at(day: number, hour: number, minute: number) {
return new Date(2026, 9, day, hour, minute).toISOString()
}
function settings(overrides: Partial<WorkspaceSettings> = {}): WorkspaceSettings {
return {
version: 'v2.2.0-beta',
activity_retention_days: 90,
activity_retention_max_gb: 0,
activity_log_size_bytes: 0,
activity_last_cleanup_at: at(6, 3, 0),
activity_last_cleanup_removed: 142,
activity_next_cleanup_at: at(7, 3, 0),
...overrides,
}
}
beforeEach(() => {
localStorage.clear()
initializeLocale()
})
afterEach(() => {
localStorage.clear()
initializeLocale()
})
describe('recentWhenLabel follows the day', () => {
it.each([
[at(6, 14, 58), '2 min ago'],
[at(6, 12, 0), '3 h ago'],
[at(5, 14, 20), 'Yesterday, 14:20'],
[at(3, 14, 20), '3 Oct 2026, 14:20'],
])('%s reads %s', (iso, label) => {
expect(recentWhenLabel(iso, NOW)).toBe(label)
})
it('reads French labels in French', () => {
setLocale('fr')
expect(recentWhenLabel(at(5, 14, 20), NOW)).toBe('Hier, 14:20')
expect(recentWhenLabel(at(3, 14, 20), NOW)).toBe('3 oct. 2026, 14:20')
})
})
describe('retention periods', () => {
it('ranks 30 < 90 < 365 < Forever', () => {
expect(isShorter(30, 90)).toBe(true)
expect(isShorter(365, 0)).toBe(true)
expect(isShorter(0, 365)).toBe(false)
expect(isShorter(90, 90)).toBe(false)
})
})
describe('retention lines', () => {
it('reads the last cleanup with its count', () => {
expect(lastCleanupLine(settings())).toBe('6 Oct 2026, 03:00 · 142 events removed')
expect(lastCleanupLine(settings({ activity_last_cleanup_removed: 1 }))).toBe(
'6 Oct 2026, 03:00 · 1 event removed',
)
})
it('reads Never before the first cleanup and Not scheduled for Forever', () => {
expect(lastCleanupLine(settings({ activity_last_cleanup_at: null }))).toBe('Never')
expect(nextCleanupLine(settings({ activity_next_cleanup_at: null }))).toBe('Not scheduled')
expect(nextCleanupLine(settings())).toBe('7 Oct 2026, 03:00')
})
it('words the helper and the warning', () => {
expect(helperCopy(90)).toBe('Older events are removed automatically.')
expect(helperCopy(0)).toBe('Nothing is removed automatically. The log grows over time.')
expect(shorteningDescription(settings())).toBe(
"This happens at the next cleanup on 7 Oct 2026, 03:00. It can't be undone.",
)
})
})
describe('retention size limit', () => {
it('ranks 1 < 5 < 10 < 25 < 50 < No size limit', () => {
expect(isSmaller(1, 5)).toBe(true)
expect(isSmaller(5, 0)).toBe(true)
expect(isSmaller(0, 50)).toBe(false)
expect(isSmaller(5, 5)).toBe(false)
expect(isSmaller(10, 50)).toBe(true)
})
it.each([
[0, 'Less than 0.1 GB'],
[53687091, 'Less than 0.1 GB'],
[107374183, '0.1 GB'],
[1073741824, '1.0 GB'],
[3435973837, '3.2 GB'],
[5368709120, '5.0 GB'],
])('formats %s bytes as %s on a 1024^3 base', (bytes, shown) => {
expect(formatLogSize(bytes)).toBe(shown)
})
it('words the helpers for each combination', () => {
expect(helperCopy(90, 5)).toBe('Events older than this are removed.')
expect(helperCopy(0, 5)).toBe('No age limit. Only the size limit applies.')
expect(helperCopy(0, 0)).toBe('Nothing is removed automatically. The log grows over time.')
expect(sizeHelperCopy(0, 99)).toBe('Optional. Logs can grow without a size cap.')
expect(sizeHelperCopy(5, 3435973837)).toBe(
'When logs pass 5 GB, the oldest events are removed. Currently 3.2 GB.',
)
})
it('words the helpers in French with Go and a decimal comma', () => {
setLocale('fr')
expect(sizeHelperCopy(5, 3435973837)).toBe(
'Au-delà de 5 Go, les événements les plus anciens sont supprimés. Actuellement 3,2 Go.',
)
expect(formatLogSize(0)).toBe('Moins de 0,1 Go')
})
})