File size: 3,539 Bytes
9bd422a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
 * 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);
  });
});