|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | + |
| 3 | +import { categorizeForViewerWarning } from './binary-warning' |
| 4 | + |
| 5 | +describe('categorizeForViewerWarning', () => { |
| 6 | + describe('image extensions → "image"', () => { |
| 7 | + it.each(['photo.jpg', 'snap.JPEG', 'logo.png', 'anim.gif', 'pic.webp', 'shot.heic', 'icon.ico', 'art.avif'])( |
| 8 | + '%s → image', |
| 9 | + (name) => { |
| 10 | + expect(categorizeForViewerWarning(name)).toEqual({ shouldWarn: true, label: 'image' }) |
| 11 | + }, |
| 12 | + ) |
| 13 | + }) |
| 14 | + |
| 15 | + describe('document extensions → "document"', () => { |
| 16 | + it.each(['report.pdf', 'contract.docx', 'budget.xlsx', 'slides.pptx', 'notes.pages', 'data.numbers', 'novel.epub'])( |
| 17 | + '%s → document', |
| 18 | + (name) => { |
| 19 | + expect(categorizeForViewerWarning(name)).toEqual({ shouldWarn: true, label: 'document' }) |
| 20 | + }, |
| 21 | + ) |
| 22 | + }) |
| 23 | + |
| 24 | + describe('other binary extensions → uppercased extension', () => { |
| 25 | + it('installer.exe → EXE', () => { |
| 26 | + expect(categorizeForViewerWarning('installer.exe')).toEqual({ shouldWarn: true, label: 'EXE' }) |
| 27 | + }) |
| 28 | + it('archive.zip → ZIP', () => { |
| 29 | + expect(categorizeForViewerWarning('archive.zip')).toEqual({ shouldWarn: true, label: 'ZIP' }) |
| 30 | + }) |
| 31 | + it('video.mp4 → MP4', () => { |
| 32 | + expect(categorizeForViewerWarning('video.mp4')).toEqual({ shouldWarn: true, label: 'MP4' }) |
| 33 | + }) |
| 34 | + it('sound.mp3 → MP3', () => { |
| 35 | + expect(categorizeForViewerWarning('sound.mp3')).toEqual({ shouldWarn: true, label: 'MP3' }) |
| 36 | + }) |
| 37 | + it('font.woff2 → WOFF2', () => { |
| 38 | + expect(categorizeForViewerWarning('font.woff2')).toEqual({ shouldWarn: true, label: 'WOFF2' }) |
| 39 | + }) |
| 40 | + }) |
| 41 | + |
| 42 | + describe('text-like or unknown extensions do NOT warn', () => { |
| 43 | + // Plain text and source code: showing raw bytes is the point. |
| 44 | + it.each([ |
| 45 | + 'README.md', |
| 46 | + 'notes.txt', |
| 47 | + 'config.json', |
| 48 | + 'data.csv', |
| 49 | + 'app.ts', |
| 50 | + 'main.rs', |
| 51 | + 'script.py', |
| 52 | + 'styles.css', |
| 53 | + 'index.html', |
| 54 | + 'icon.svg', // text-based XML |
| 55 | + 'log.log', |
| 56 | + 'Cargo.toml', |
| 57 | + 'Dockerfile.yaml', |
| 58 | + ])('%s → no warning', (name) => { |
| 59 | + expect(categorizeForViewerWarning(name)).toEqual({ shouldWarn: false, label: '' }) |
| 60 | + }) |
| 61 | + |
| 62 | + // Unknown extension we don't classify: better to under-warn than over-warn. |
| 63 | + it('random.xyz → no warning', () => { |
| 64 | + expect(categorizeForViewerWarning('random.xyz')).toEqual({ shouldWarn: false, label: '' }) |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + describe('edge cases', () => { |
| 69 | + it('files with no extension never warn (Makefile, README, etc.)', () => { |
| 70 | + expect(categorizeForViewerWarning('Makefile')).toEqual({ shouldWarn: false, label: '' }) |
| 71 | + expect(categorizeForViewerWarning('README')).toEqual({ shouldWarn: false, label: '' }) |
| 72 | + }) |
| 73 | + |
| 74 | + it('hidden files with no real extension never warn (.bashrc, .gitignore)', () => { |
| 75 | + // ".bashrc" has the dot at index 0; we treat that as "no extension". |
| 76 | + expect(categorizeForViewerWarning('.bashrc')).toEqual({ shouldWarn: false, label: '' }) |
| 77 | + expect(categorizeForViewerWarning('.gitignore')).toEqual({ shouldWarn: false, label: '' }) |
| 78 | + }) |
| 79 | + |
| 80 | + it('trailing dot is treated as no extension', () => { |
| 81 | + expect(categorizeForViewerWarning('name.')).toEqual({ shouldWarn: false, label: '' }) |
| 82 | + }) |
| 83 | + |
| 84 | + it('empty string → no warning, no crash', () => { |
| 85 | + expect(categorizeForViewerWarning('')).toEqual({ shouldWarn: false, label: '' }) |
| 86 | + }) |
| 87 | + |
| 88 | + it('extension is matched case-insensitively', () => { |
| 89 | + expect(categorizeForViewerWarning('PHOTO.JPG')).toEqual({ shouldWarn: true, label: 'image' }) |
| 90 | + expect(categorizeForViewerWarning('Setup.EXE')).toEqual({ shouldWarn: true, label: 'EXE' }) |
| 91 | + }) |
| 92 | + |
| 93 | + it('multi-dot filenames use the last segment', () => { |
| 94 | + expect(categorizeForViewerWarning('archive.tar.gz')).toEqual({ shouldWarn: true, label: 'GZ' }) |
| 95 | + expect(categorizeForViewerWarning('photo.thumbnail.png')).toEqual({ shouldWarn: true, label: 'image' }) |
| 96 | + }) |
| 97 | + }) |
| 98 | +}) |
0 commit comments