Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions frontend/src/lib/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, it } from 'vitest'

import { formatBytes } from './utils'

describe('formatBytes', () => {
it('formats byte values across supported units', () => {
expect(formatBytes(0)).toBe('0 B')
expect(formatBytes(512)).toBe('512.0 B')
expect(formatBytes(1024)).toBe('1.0 KB')
expect(formatBytes(1024 ** 2)).toBe('1.0 MB')
expect(formatBytes(1024 ** 5)).toBe('1.0 PB')
expect(formatBytes(1024 ** 6)).toBe('1.0 EB')
})

it('handles invalid or out-of-range values without undefined units', () => {
expect(formatBytes(-1)).toBe('–')
expect(formatBytes(Number.NaN)).toBe('–')
expect(formatBytes(Number.POSITIVE_INFINITY)).toBe('–')
expect(formatBytes(0.5)).toBe('0.5 B')
expect(formatBytes(1024 ** 7)).toBe('1024.0 EB')
})
})
5 changes: 3 additions & 2 deletions frontend/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ export function formatDuration(seconds: number): string {
}

export function formatBytes(bytes: number): string {
if (!Number.isFinite(bytes) || bytes < 0) return '–'
if (bytes === 0) return '0 B'
const k = 1024
const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB']
const i = Math.max(0, Math.min(Math.floor(Math.log(bytes) / Math.log(k)), sizes.length - 1))
return `${(bytes / Math.pow(k, i)).toFixed(1)} ${sizes[i]}`
}

Expand Down
1 change: 1 addition & 0 deletions frontend/src/test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@testing-library/jest-dom/vitest'