import { describe, expect, it } from 'vitest' import { buildCompactMetaLabel, filenameFromUrl, formatFileSize, getFileExtensionLabel, } from './fileMeta' describe('formatFileSize', () => { it('formats sub-kilobyte sizes as bytes', () => { expect(formatFileSize(0)).toBe('0 B') expect(formatFileSize(512)).toBe('512 B') }) it('formats kilobytes / megabytes / gigabytes with sensible precision', () => { expect(formatFileSize(1024)).toBe('1.0 KB') expect(formatFileSize(388_658)).toBe('379.5 KB') expect(formatFileSize(2_457_600)).toBe('2.34 MB') expect(formatFileSize(1024 ** 3)).toBe('1.00 GB') }) it('returns empty string for invalid input', () => { expect(formatFileSize(NaN)).toBe('') expect(formatFileSize(Infinity)).toBe('') expect(formatFileSize(-1)).toBe('') }) it('handles unit boundaries without crossing into the next unit early', () => { expect(formatFileSize(1023)).toBe('1023 B') expect(formatFileSize(1024 * 1024 - 1)).toBe('1024.0 KB') expect(formatFileSize(1024 * 1024)).toBe('1.00 MB') expect(formatFileSize(1024 ** 3 - 1)).toBe('1024.00 MB') expect(formatFileSize(1024 ** 3)).toBe('1.00 GB') }) }) describe('getFileExtensionLabel', () => { it('prefers the filename extension when present', () => { expect(getFileExtensionLabel(undefined, 'notes.pdf')).toBe('PDF') expect(getFileExtensionLabel('application/pdf', 'oops.docx')).toBe('DOCX') }) it('falls back to MIME-derived label when filename has no extension', () => { expect(getFileExtensionLabel('application/pdf')).toBe('PDF') expect(getFileExtensionLabel('application/zip')).toBe('ZIP') expect(getFileExtensionLabel('application/json')).toBe('JSON') }) it('returns undefined for unknown / generic MIME without filename', () => { expect(getFileExtensionLabel('application/octet-stream')).toBeUndefined() expect(getFileExtensionLabel()).toBeUndefined() }) it('handles dot boundaries, extension length, and MIME fallbacks', () => { expect(getFileExtensionLabel('text/plain', '.gitignore')).toBe('TXT') expect(getFileExtensionLabel('text/plain', 'notes.')).toBe('TXT') expect(getFileExtensionLabel('image/webp', 'photo.webp')).toBe('WEBP') expect(getFileExtensionLabel('image/webp', 'photo.abcdef')).toBe('WEBP') expect(getFileExtensionLabel('application/json', 'data.12345')).toBe( '12345' ) expect(getFileExtensionLabel('image/*')).toBeUndefined() expect(getFileExtensionLabel('application/octet-stream')).toBeUndefined() }) }) describe('buildCompactMetaLabel', () => { it('joins extension and size with a middle dot', () => { expect(buildCompactMetaLabel('application/pdf', 'notes.pdf', 388_658)).toBe( 'PDF ยท 379.5 KB' ) }) it('drops the size when missing', () => { expect(buildCompactMetaLabel('application/pdf', 'notes.pdf')).toBe('PDF') }) it('returns undefined when nothing is renderable', () => { expect(buildCompactMetaLabel()).toBeUndefined() }) it('drops non-positive and non-number sizes while retaining size-only labels', () => { expect(buildCompactMetaLabel('application/pdf', 'report.pdf', 0)).toBe( 'PDF' ) expect(buildCompactMetaLabel('application/pdf', 'report.pdf', -1)).toBe( 'PDF' ) expect(buildCompactMetaLabel(undefined, undefined, 1024)).toBe('1.0 KB') expect( buildCompactMetaLabel( 'application/octet-stream', undefined, '1024' as never ) ).toBeUndefined() }) }) describe('filenameFromUrl', () => { it('extracts the last path segment', () => { expect(filenameFromUrl('https://cdn.example.com/folder/notes.pdf')).toBe( 'notes.pdf' ) }) it('decodes URI-encoded segments', () => { expect( filenameFromUrl('https://cdn.example.com/folder/My%20Notes.pdf') ).toBe('My Notes.pdf') }) it('falls back to a default when the URL cannot be parsed', () => { expect(filenameFromUrl('not a url at all')).toBe('download') }) it('handles trailing slashes, deep paths, and query strings', () => { expect(filenameFromUrl('https://cdn.example.com/folder/')).toBe('download') expect( filenameFromUrl('https://cdn.example.com/a/b/c/report.pdf?download=1') ).toBe('report.pdf') }) })