🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { afterEach, describe, expect, it, vi } from 'vitest';
import { getMimeType, resolveMimeType, tryGetMimeType } from './mimeType';
afterEach(() => {
vi.doUnmock('node:path');
vi.resetModules();
});
describe('browser compatibility', () => {
it('detects a pasted image without relying on Node path.extname', async () => {
vi.doMock('node:path', () => ({ default: {} }));
vi.resetModules();
const browserMimeType = await import('./mimeType');
expect(browserMimeType.getMimeType('pasted-image.png')).toBe('image/png');
});
});
describe('getMimeType', () => {
describe('custom code file MIME types', () => {
it('should return correct MIME type for Python files', () => {
expect(getMimeType('script.py')).toBe('text/x-python');
expect(getMimeType('/path/to/script.py')).toBe('text/x-python');
});
it('should return correct MIME type for Elixir files (.ex)', () => {
expect(getMimeType('app.ex')).toBe('text/x-elixir');
});
it('should return correct MIME type for Elixir script files (.exs)', () => {
expect(getMimeType('mix.exs')).toBe('text/x-elixir');
});
});
describe('case insensitivity for extensions', () => {
it('should handle uppercase extensions', () => {
expect(getMimeType('script.PY')).toBe('text/x-python');
expect(getMimeType('main.GO')).toBe('text/x-go');
expect(getMimeType('lib.RS')).toBe('text/x-rust');
});
it('should handle mixed-case extensions', () => {
expect(getMimeType('App.Py')).toBe('text/x-python');
expect(getMimeType('Main.Kt')).toBe('text/x-kotlin');
});
});
describe('standard MIME types via mime package', () => {
it('should return correct MIME type for JavaScript files', () => {
expect(getMimeType('app.js')).toBe('text/javascript');
});
it('should return video/mp2t for .ts files (MPEG-2 Transport Stream per MIME registry)', () => {
// Note: .ts is registered as MPEG-2 Transport Stream, not TypeScript
expect(getMimeType('app.ts')).toBe('video/mp2t');
});
it('should return correct MIME type for JSON files', () => {
expect(getMimeType('config.json')).toBe('application/json');
});
it('should return correct MIME type for PNG images', () => {
expect(getMimeType('photo.png')).toBe('image/png');
});
});
describe('fallback to application/octet-stream', () => {
it('should return application/octet-stream for unknown extensions', () => {
expect(getMimeType('file.unknownext')).toBe('application/octet-stream');
expect(getMimeType('archive.xyz123')).toBe('application/octet-stream');
});
it('should return application/octet-stream for files without extension', () => {
expect(getMimeType('Makefile')).toBe('application/octet-stream');
expect(getMimeType('Dockerfile')).toBe('application/octet-stream');
});
});
describe('path handling', () => {
it('should handle full paths with directories', () => {
expect(getMimeType('/home/user/project/src/main.py')).toBe('text/x-python');
expect(getMimeType('C:/Users/user/docs/file.json')).toBe('application/json');
});
it('should handle paths with dots in directory names', () => {
expect(getMimeType('/path/to/v1.0/script.py')).toBe('text/x-python');
expect(getMimeType('/path.to/files/image.png')).toBe('image/png');
});
it('should use the last extension when filename has multiple dots', () => {
expect(getMimeType('archive.tar.gz')).toBe('application/gzip');
expect(getMimeType('component.test.js')).toBe('text/javascript');
});
});
});
describe('tryGetMimeType', () => {
it('returns the mime with charset for text responses', () => {
expect(tryGetMimeType('/abs/path/style.css')).toBe('text/css; charset=utf-8');
expect(tryGetMimeType('bundle.js')).toBe('text/javascript; charset=utf-8');
});
it('returns the mime without charset for binary responses', () => {
expect(tryGetMimeType('icon.png')).toBe('image/png');
expect(tryGetMimeType('font.woff2')).toBe('font/woff2');
});
it('returns undefined when the extension is unknown', () => {
expect(tryGetMimeType('foo.unknownext')).toBeUndefined();
expect(tryGetMimeType('Makefile')).toBeUndefined();
});
});
describe('resolveMimeType', () => {
it('resolves common web/data extensions via the extension lookup + charset', async () => {
await expect(resolveMimeType('/repo/data.json', Buffer.from('{}'))).resolves.toBe(
'application/json; charset=utf-8',
);
await expect(resolveMimeType('/repo/README.md', Buffer.from('# hi'))).resolves.toBe(
'text/markdown; charset=utf-8',
);
});
it('appends charset to custom code-language mimes', async () => {
// .py → CUSTOM_MIME_TYPES text/x-python → text/ prefix → gains charset.
const py = Buffer.from('print("hi")\n');
await expect(resolveMimeType('/repo/script.py', py)).resolves.toBe(
'text/x-python; charset=utf-8',
);
// .v / .sv resolve to HDL text mimes with charset (not octet-stream).
const vSource = Buffer.from('module adder(); endmodule\n');
await expect(resolveMimeType('/repo/adder.v', vSource)).resolves.toBe(
'text/x-verilog; charset=utf-8',
);
const svSource = Buffer.from('package p; endpackage\n');
await expect(resolveMimeType('/repo/top.sv', svSource)).resolves.toBe(
'text/x-systemverilog; charset=utf-8',
);
});
it('serves preview-only image formats via the extension lookup', async () => {
// Truly binary buffers so the sniff never asks us to downgrade.
await expect(
resolveMimeType('/repo/photo.heic', Buffer.from([0x00, 0xff, 0xd8, 0x00])),
).resolves.toBe('image/heic');
await expect(
resolveMimeType('/repo/diagram.bmp', Buffer.from([0x42, 0x4d, 0x00, 0x01])),
).resolves.toBe('image/bmp');
});
it('trusts magic bytes for binary formats even when the extension is unknown', async () => {
// `.blob` isn't in mime-db, so file-type's magic sniff decides.
const pngBytes = Buffer.from([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44,
0x52,
]);
await expect(resolveMimeType('/repo/mystery.blob', pngBytes)).resolves.toBe('image/png');
});
it('trusts magic bytes even when mime-db would lie about the extension', async () => {
// Real MPEG-TS carries the 0x47 sync byte at 188-byte intervals. File-type
// recognises it and serves video/mp2t regardless of extension.
const mpegTs = Buffer.alloc(376);
mpegTs[0] = 0x47;
mpegTs[188] = 0x47;
await expect(resolveMimeType('/repo/clip.ts', mpegTs)).resolves.toBe('video/mp2t');
});
it('downgrades to text/plain when mime-db claims binary but the buffer is text', async () => {
// The classic .ts ambiguity: mime-db → video/mp2t, but TypeScript source
// has no magic bytes and sniffs as text. The downgrade rule catches this
// without an extension override.
const tsSource = Buffer.from('export const foo = (x: number): number => x + 1;\n');
await expect(resolveMimeType('/repo/module.ts', tsSource)).resolves.toBe(
'text/plain; charset=utf-8',
);
// Same rule saves `.cjs` (mime-db → application/node).
const cjsSource = Buffer.from(`module.exports = { plugins: ['@semantic-release/npm'] };\n`);
await expect(resolveMimeType('/repo/.releaserc.cjs', cjsSource)).resolves.toBe(
'text/plain; charset=utf-8',
);
});
it('keeps image/svg+xml for SVG instead of downgrading it to text/plain', async () => {
// SVG is text, but browsers only render it as an image from image/svg+xml.
const svgSource = Buffer.from(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><path d="M0 10L5 0L10 10z"/></svg>\n',
);
await expect(resolveMimeType('/repo/shape.svg', svgSource)).resolves.toBe('image/svg+xml');
});
it('keeps the text downgrade for other text extensions mime-db maps to non-text types', async () => {
const mtsSource = Buffer.from('export const bar = 1;\n');
await expect(resolveMimeType('/repo/module.mts', mtsSource)).resolves.toBe(
'text/plain; charset=utf-8',
);
// Non-SVG `+xml` types stay on the downgrade so text previews keep working.
const atomFeed = Buffer.from('<feed xmlns="http://www.w3.org/2005/Atom"></feed>\n');
await expect(resolveMimeType('/repo/feed.atom', atomFeed)).resolves.toBe(
'text/plain; charset=utf-8',
);
});
it('prefers the SVG extension over the generic XML match for an `<?xml` prolog', async () => {
// file-type fingerprints the prolog as application/xml, which browsers won't render as an image.
const exportedSvg = Buffer.from(
'<?xml version="1.0" encoding="UTF-8"?>\n<svg xmlns="http://www.w3.org/2000/svg"/>\n',
);
await expect(resolveMimeType('/repo/exported.svg', exportedSvg)).resolves.toBe('image/svg+xml');
// Plain .xml keeps the magic-byte result.
const plainXml = Buffer.from('<?xml version="1.0"?><root/>');
await expect(resolveMimeType('/repo/data.xml', plainXml)).resolves.toBe('application/xml');
});
it('falls back to text/plain for unknown text extensions via the sniff', async () => {
await expect(resolveMimeType('/repo/App.tsx', Buffer.from(''))).resolves.toBe(
'text/plain; charset=utf-8',
);
const editorconfig = Buffer.from('root = true\n[*]\nindent_style = space\n');
await expect(resolveMimeType('/repo/.editorconfig', editorconfig)).resolves.toBe(
'text/plain; charset=utf-8',
);
});
describe('UTF-16 text with a byte order mark', () => {
// Windows PowerShell 5.1 `Tee-Object` / `Out-File` write UTF-16LE with a
// BOM. Its leading 0xFF 0xFE matches file-type's MPEG audio frame sync, so
// CLI evidence uploads were stored as audio/mpeg and sent to the model as
// audio.
const utf16le = (text: string) => Buffer.from(`\uFEFF${text}`, 'utf16le');
const utf16be = (text: string) => utf16le(text).swap16();
const output = 'PS C:\\> backup compare: OK\r\nsqlite integrity_check: ok\r\n';
it('resolves UTF-16LE text as text instead of audio/mpeg', async () => {
await expect(resolveMimeType('backup_compare.txt', utf16le(output))).resolves.toBe(
'text/plain; charset=utf-16le',
);
await expect(resolveMimeType('out.txt', utf16le('ok'))).resolves.toBe(
'text/plain; charset=utf-16le',
);
});
it('keeps text whose surrogate pair straddles the sniff window', async () => {
// BOM + 4094 code units fill bytes 0-8189; the emoji's high surrogate takes
// 8190-8191 and its low surrogate falls outside the 8192-byte sample.
const text = `${'a'.repeat(4094)}😀`;
await expect(resolveMimeType('emoji.txt', utf16le(text))).resolves.toBe(
'text/plain; charset=utf-16le',
);
});
it('resolves UTF-16BE text with its own charset', async () => {
await expect(resolveMimeType('backup_compare.txt', utf16be(output))).resolves.toBe(
'text/plain; charset=utf-16be',
);
});
it('keeps the extension text mime and downgrades binary extension mimes', async () => {
await expect(resolveMimeType('result.json', utf16le('{"ok":true}'))).resolves.toBe(
'application/json; charset=utf-16le',
);
await expect(resolveMimeType('module.ts', utf16le('export {};\n'))).resolves.toBe(
'text/plain; charset=utf-16le',
);
await expect(resolveMimeType('no-extension', utf16le(output))).resolves.toBe(
'text/plain; charset=utf-16le',
);
});
it('keeps MPEG audio whose frame header starts with the same bytes', async () => {
// MPEG-1 Layer I frame header (0xFF 0xFE ...) followed by a silent payload.
const mpegFrame = Buffer.alloc(1024);
mpegFrame.set([0xff, 0xfe, 0x90, 0x00]);
await expect(resolveMimeType('clip.mp1', mpegFrame)).resolves.toBe('audio/mpeg');
// Non-silent frames: a lenient UTF-16 decode reads compressed audio as
// mostly printable text, so the strict decode must reject it.
let seed = 1;
const noisyMpeg = Buffer.alloc(3 * 384);
for (let i = 0; i < noisyMpeg.length; i++) {
seed = (seed * 1_103_515_245 + 12_345) % 2 ** 31;
noisyMpeg[i] = (seed >> 16) & 0xff;
}
for (let frame = 0; frame < 3; frame++) {
noisyMpeg.set([0xff, 0xfe, 0x90, 0x00], frame * 384);
}
await expect(resolveMimeType('clip.mp1', noisyMpeg)).resolves.toBe('audio/mpeg');
// A regular MP3 frame (Layer III) never starts with a BOM.
const mp3Frame = Buffer.alloc(1024);
mp3Frame.set([0xff, 0xfb, 0x90, 0x64]);
await expect(resolveMimeType('song.mp3', mp3Frame)).resolves.toBe('audio/mpeg');
});
});
it('falls back to application/octet-stream when the sniff detects binary data', async () => {
// No known extension and no recognisable magic bytes — the sniff sees
// embedded null bytes and classifies as binary.
const binary = Buffer.from([0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03]);
await expect(resolveMimeType('/repo/strange.blob', binary)).resolves.toBe(
'application/octet-stream',
);
});
});