model-explorer / js /tests /validators.test.js
mr4's picture
Upload 71 files
9bd422a verified
/**
* Unit tests for Validators - SafeTensors support
* Validates: Requirements 36.1, 36.3, 36.5
*/
import { describe, it, expect, beforeEach } from 'vitest';
// ─── Re-implement the pure validation logic for testability ──
function isValidSafeTensorsFile(file) {
if (!file) {
return { valid: false, error: 'No file provided.' };
}
const name = file.name || '';
if (!name.toLowerCase().endsWith('.safetensors')) {
return {
valid: false,
error: `Invalid file type "${name}". Only .safetensors files are accepted.`,
};
}
if (file.size === 0) {
return { valid: false, error: 'The file is empty.' };
}
return { valid: true };
}
function isValidModelPath(path) {
if (!path || typeof path !== 'string' || path.trim() === '') {
return { valid: false, error: 'Model path must be a non-empty string.' };
}
const lower = path.toLowerCase();
if (!lower.endsWith('.onnx') && !lower.endsWith('.safetensors')) {
return { valid: false, error: 'Model path must point to an .onnx or .safetensors file.' };
}
return { valid: true };
}
// ─── Helper ──
function fakeFile(name, size) {
return { name, size };
}
// ─── Tests ──────────────────────────────────────────────────────────────
describe('isValidSafeTensorsFile', () => {
it('should accept a valid .safetensors file (Req 36.1)', () => {
const result = isValidSafeTensorsFile(fakeFile('model.safetensors', 1024));
expect(result.valid).toBe(true);
});
it('should reject null file', () => {
const result = isValidSafeTensorsFile(null);
expect(result.valid).toBe(false);
expect(result.error).toContain('No file provided');
});
it('should reject non-.safetensors extension (Req 36.3)', () => {
const result = isValidSafeTensorsFile(fakeFile('model.onnx', 1024));
expect(result.valid).toBe(false);
expect(result.error).toContain('Invalid file type');
});
it('should reject empty file', () => {
const result = isValidSafeTensorsFile(fakeFile('model.safetensors', 0));
expect(result.valid).toBe(false);
expect(result.error).toContain('empty');
});
it('should accept large files without size limit', () => {
const hugeSize = 10 * 1024 * 1024 * 1024; // 10 GB
const result = isValidSafeTensorsFile(fakeFile('model.safetensors', hugeSize));
expect(result.valid).toBe(true);
});
it('should be case-insensitive for extension', () => {
const result = isValidSafeTensorsFile(fakeFile('Model.SAFETENSORS', 512));
expect(result.valid).toBe(true);
});
});
describe('isValidModelPath', () => {
it('should accept .onnx paths', () => {
expect(isValidModelPath('models/model.onnx').valid).toBe(true);
});
it('should accept .safetensors paths', () => {
expect(isValidModelPath('models/model.safetensors').valid).toBe(true);
});
it('should reject unsupported extensions', () => {
const result = isValidModelPath('models/model.pt');
expect(result.valid).toBe(false);
expect(result.error).toContain('.onnx or .safetensors');
});
it('should reject empty/null paths', () => {
expect(isValidModelPath('').valid).toBe(false);
expect(isValidModelPath(null).valid).toBe(false);
});
it('should be case-insensitive', () => {
expect(isValidModelPath('model.ONNX').valid).toBe(true);
expect(isValidModelPath('model.SafeTensors').valid).toBe(true);
});
});