Skip to content
Open
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
20 changes: 20 additions & 0 deletions frontend/src/lib/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, it } from 'vitest'

import { formatBytes } from './utils'

describe('formatBytes', () => {
it('formats zero and whole-byte values', () => {
expect(formatBytes(0)).toBe('0 B')
expect(formatBytes(512)).toBe('512.0 B')
})

it('keeps fractional bytes in the byte unit', () => {
expect(formatBytes(0.5)).toBe('0.5 B')
})

it('handles invalid sizes without rendering undefined units', () => {
expect(formatBytes(-1)).toBe('0 B')
expect(formatBytes(Number.NaN)).toBe('0 B')
expect(formatBytes(Number.POSITIVE_INFINITY)).toBe('0 B')
})
})
4 changes: 2 additions & 2 deletions frontend/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export function formatDuration(seconds: number): string {
}

export function formatBytes(bytes: number): string {
if (bytes === 0) return '0 B'
if (!Number.isFinite(bytes) || 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 i = Math.min(Math.max(Math.floor(Math.log(bytes) / Math.log(k)), 0), 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'