mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-02-03 22:59:14 +00:00
## Summary Improves type safety in test files by replacing unsafe type patterns with proper TypeScript idioms. ## Changes - Define typed `TestWindow` interface extending `Window` for Playwright tests with custom properties - Use `Partial<HTMLElement>` with single type assertion for DOM element mocks - Remove redundant type imports - Fix `console.log` → `console.warn` in test fixture ## Files Changed 16 test files across browser_tests, packages, and src/components ## Test Plan - ✅ `pnpm typecheck` passes - ✅ No new `any` types introduced - ✅ All pre-commit hooks pass ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-8253-refactor-improve-TypeScript-patterns-in-test-files-Group-1-8-2f16d73d365081548f9ece7bcf0525ee) by [Unito](https://www.unito.io) --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
146 lines
5.6 KiB
TypeScript
146 lines
5.6 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
|
|
import { getMediaTypeFromFilename, truncateFilename } from './formatUtil'
|
|
|
|
describe('formatUtil', () => {
|
|
describe('truncateFilename', () => {
|
|
it('should not truncate short filenames', () => {
|
|
expect(truncateFilename('test.png')).toBe('test.png')
|
|
expect(truncateFilename('short.jpg', 10)).toBe('short.jpg')
|
|
})
|
|
|
|
it('should truncate long filenames while preserving extension', () => {
|
|
const longName = 'this-is-a-very-long-filename-that-needs-truncation.png'
|
|
const truncated = truncateFilename(longName, 20)
|
|
expect(truncated).toContain('...')
|
|
expect(truncated.endsWith('.png')).toBe(true)
|
|
expect(truncated.length).toBeLessThanOrEqual(25) // 20 + '...' + extension
|
|
})
|
|
|
|
it('should handle filenames without extensions', () => {
|
|
const longName = 'this-is-a-very-long-filename-without-extension'
|
|
const truncated = truncateFilename(longName, 20)
|
|
expect(truncated).toContain('...')
|
|
expect(truncated.length).toBeLessThanOrEqual(23) // 20 + '...'
|
|
})
|
|
|
|
it('should handle empty strings', () => {
|
|
expect(truncateFilename('')).toBe('')
|
|
expect(truncateFilename('', 10)).toBe('')
|
|
})
|
|
|
|
it('should preserve the start and end of the filename', () => {
|
|
const longName = 'ComfyUI_00001_timestamp_2024_01_01.png'
|
|
const truncated = truncateFilename(longName, 20)
|
|
expect(truncated).toMatch(/^ComfyUI.*01\.png$/)
|
|
expect(truncated).toContain('...')
|
|
})
|
|
|
|
it('should handle files with multiple dots', () => {
|
|
const filename = 'my.file.with.multiple.dots.txt'
|
|
const truncated = truncateFilename(filename, 15)
|
|
expect(truncated.endsWith('.txt')).toBe(true)
|
|
expect(truncated).toContain('...')
|
|
})
|
|
})
|
|
|
|
describe('getMediaTypeFromFilename', () => {
|
|
describe('image files', () => {
|
|
const imageTestCases = [
|
|
{ filename: 'test.png', expected: 'image' },
|
|
{ filename: 'photo.jpg', expected: 'image' },
|
|
{ filename: 'image.jpeg', expected: 'image' },
|
|
{ filename: 'animation.gif', expected: 'image' },
|
|
{ filename: 'web.webp', expected: 'image' },
|
|
{ filename: 'bitmap.bmp', expected: 'image' }
|
|
]
|
|
|
|
it.for(imageTestCases)(
|
|
'should identify $filename as $expected',
|
|
({ filename, expected }) => {
|
|
expect(getMediaTypeFromFilename(filename)).toBe(expected)
|
|
}
|
|
)
|
|
|
|
it('should handle uppercase extensions', () => {
|
|
expect(getMediaTypeFromFilename('test.PNG')).toBe('image')
|
|
expect(getMediaTypeFromFilename('photo.JPG')).toBe('image')
|
|
})
|
|
})
|
|
|
|
describe('video files', () => {
|
|
it('should identify video extensions correctly', () => {
|
|
expect(getMediaTypeFromFilename('video.mp4')).toBe('video')
|
|
expect(getMediaTypeFromFilename('clip.webm')).toBe('video')
|
|
expect(getMediaTypeFromFilename('movie.mov')).toBe('video')
|
|
expect(getMediaTypeFromFilename('film.avi')).toBe('video')
|
|
})
|
|
})
|
|
|
|
describe('audio files', () => {
|
|
it('should identify audio extensions correctly', () => {
|
|
expect(getMediaTypeFromFilename('song.mp3')).toBe('audio')
|
|
expect(getMediaTypeFromFilename('sound.wav')).toBe('audio')
|
|
expect(getMediaTypeFromFilename('music.ogg')).toBe('audio')
|
|
expect(getMediaTypeFromFilename('audio.flac')).toBe('audio')
|
|
})
|
|
})
|
|
|
|
describe('3D files', () => {
|
|
it('should identify 3D file extensions correctly', () => {
|
|
expect(getMediaTypeFromFilename('model.obj')).toBe('3D')
|
|
expect(getMediaTypeFromFilename('scene.fbx')).toBe('3D')
|
|
expect(getMediaTypeFromFilename('asset.gltf')).toBe('3D')
|
|
expect(getMediaTypeFromFilename('binary.glb')).toBe('3D')
|
|
})
|
|
})
|
|
|
|
describe('edge cases', () => {
|
|
it('should handle empty strings', () => {
|
|
expect(getMediaTypeFromFilename('')).toBe('image')
|
|
})
|
|
|
|
it('should handle files without extensions', () => {
|
|
expect(getMediaTypeFromFilename('README')).toBe('image')
|
|
})
|
|
|
|
it('should handle unknown extensions', () => {
|
|
expect(getMediaTypeFromFilename('document.pdf')).toBe('image')
|
|
expect(getMediaTypeFromFilename('data.json')).toBe('image')
|
|
})
|
|
|
|
it('should handle files with multiple dots', () => {
|
|
expect(getMediaTypeFromFilename('my.file.name.png')).toBe('image')
|
|
expect(getMediaTypeFromFilename('archive.tar.gz')).toBe('image')
|
|
})
|
|
|
|
it('should handle paths with directories', () => {
|
|
expect(getMediaTypeFromFilename('/path/to/image.png')).toBe('image')
|
|
expect(getMediaTypeFromFilename('C:\\Windows\\video.mp4')).toBe('video')
|
|
})
|
|
|
|
it('should handle null and undefined gracefully', () => {
|
|
expect(getMediaTypeFromFilename(null)).toBe('image')
|
|
expect(getMediaTypeFromFilename(undefined)).toBe('image')
|
|
})
|
|
|
|
it('should handle special characters in filenames', () => {
|
|
expect(getMediaTypeFromFilename('test@#$.png')).toBe('image')
|
|
expect(getMediaTypeFromFilename('video (1).mp4')).toBe('video')
|
|
expect(getMediaTypeFromFilename('[2024] audio.mp3')).toBe('audio')
|
|
})
|
|
|
|
it('should handle very long filenames', () => {
|
|
const longFilename = 'a'.repeat(1000) + '.png'
|
|
expect(getMediaTypeFromFilename(longFilename)).toBe('image')
|
|
})
|
|
|
|
it('should handle mixed case extensions', () => {
|
|
expect(getMediaTypeFromFilename('test.PnG')).toBe('image')
|
|
expect(getMediaTypeFromFilename('video.Mp4')).toBe('video')
|
|
expect(getMediaTypeFromFilename('audio.WaV')).toBe('audio')
|
|
})
|
|
})
|
|
})
|
|
})
|