import { describe, it, expect } from 'vitest'; import { readFileSync, existsSync, statSync } from 'node:fs'; import { createHash } from 'node:crypto'; import { EXAMPLES, VIDEO_EXAMPLE, MODEL_ID, MODEL_REVISION, VARIANTS, variantBytes, TRANSFORMERS_VERSION, ORT_WEB_VERSION } from '../src/config.js'; const root = new URL('../', import.meta.url).pathname; const attribution = JSON.parse(readFileSync(root + 'public/examples/ATTRIBUTION.json', 'utf8')); const sha = (p) => createHash('sha256').update(readFileSync(p)).digest('hex'); describe('examples & attribution', () => { it('has at least 3 image examples + 1 video example', () => { expect(EXAMPLES.filter((e) => e.kind === 'image').length).toBeGreaterThanOrEqual(3); expect(VIDEO_EXAMPLE.kind).toBe('video'); }); for (const ex of [...EXAMPLES, VIDEO_EXAMPLE]) { it(`${ex.id}: file exists, attribution complete, sha256 matches, prompt present`, () => { const p = root + 'public/examples/' + ex.file; expect(existsSync(p)).toBe(true); const a = attribution.examples.find((e) => e.id === ex.id); expect(a).toBeTruthy(); expect(a.file).toBe(ex.file); for (const k of ['author', 'license', 'licenseUrl', 'sourceUrl']) expect(a[k]).toBeTruthy(); expect(a.license).toBe('CC0 1.0'); expect(a.sourceUrl).toMatch(/^https:\/\/commons\.wikimedia\.org\/wiki\/File:/); expect(statSync(p).size).toBe(a.localBytes); expect(sha(p)).toBe(a.localSha256); expect(ex.points.length + (ex.box ? 1 : 0)).toBeGreaterThan(0); }); } it('video example is synthetic and documented frame-by-frame', () => { const v = JSON.parse(readFileSync(root + 'public/examples/video-pan.json', 'utf8')); expect(v.frames).toHaveLength(40); expect(v.out).toEqual([640, 426]); expect(attribution.examples.find((e) => e.id === 'desk-pan').synthetic).toBe(true); }); }); describe('pins & docs', () => { it('model pins are well-formed and sizes add up', () => { expect(MODEL_ID).toBe('onnx-community/sam3-tracker-ONNX'); expect(MODEL_REVISION).toMatch(/^[0-9a-f]{40}$/); expect(variantBytes('q4f16')).toBe(302295094); expect(variantBytes('q8')).toBe(538740547); for (const v of Object.values(VARIANTS)) for (const [, [b, h]] of Object.entries(v.files)) { expect(b).toBeGreaterThan(0); expect(h).toMatch(/^[0-9a-f]{64}$/); } }); it('package.json pins transformers.js exactly; lockfile pins onnxruntime-web', () => { const pkg = JSON.parse(readFileSync(root + 'package.json', 'utf8')); expect(pkg.dependencies['@huggingface/transformers']).toBe(TRANSFORMERS_VERSION); const lock = JSON.parse(readFileSync(root + 'package-lock.json', 'utf8')); expect(lock.packages['node_modules/onnxruntime-web'].version).toBe(ORT_WEB_VERSION); }); it('README documents pins, sizes, privacy and the licence', () => { const readme = readFileSync(root + 'README.md', 'utf8'); expect(readme).toMatch(/^---\n[\s\S]*sdk: static[\s\S]*app_file: index.html[\s\S]*---/); for (const s of [MODEL_REVISION, TRANSFORMERS_VERSION, ORT_WEB_VERSION, '302 295 094', '538 740 547', 'SAM License', 'no salen', 'facebook/sam3']) expect(readme).toContain(s); }); it('NOTICE + SAM licence copy present', () => { const n = readFileSync(root + 'NOTICE', 'utf8'); for (const s of ['SAM 3', 'SAM License', 'onnxruntime-web', 'Transformers.js', 'Hans Braxmeier', 'Wilfredor', 'Lauren Mancke', 'SYNTHETIC']) expect(n).toContain(s); const lic = readFileSync(root + 'public/SAM_LICENSE.txt', 'utf8'); expect(lic).toMatch(/^SAM License\nLast Updated: November 19, 2025/); }); it('index.html has a CSP restricting network to self + Hugging Face and no third-party CDN', () => { const html = readFileSync(root + 'index.html', 'utf8'); const csp = html.match(/Content-Security-Policy" content="([^"]+)"/)[1]; const connect = csp.split(';').map((s) => s.trim()).find((s) => s.startsWith('connect-src')).split(/\s+/).slice(1); expect(connect.sort()).toEqual(["'self'", 'blob:', 'data:', 'https://*.hf.co', 'https://*.huggingface.co', 'https://huggingface.co'].sort()); expect(html).not.toMatch(/jsdelivr|unpkg|cdnjs/); expect(html).toContain('lang="es"'); }); });