Spaces:
Runtime error
Runtime error
Download apps/api/src/services/expense/shared/image-processing.test.ts from ClaraSu/llm-router: direct link, hf CLI and curl.
- Browser
- Download file 2 kB
-
https://huggingface.co/spaces/ClaraSu/llm-router/resolve/main/apps/api/src/services/expense/shared/image-processing.test.ts
- Command line
-
hf download hf://spaces/ClaraSu/llm-router/apps/api/src/services/expense/shared/image-processing.test.ts
-
curl -L -o image-processing.test.ts https://huggingface.co/spaces/ClaraSu/llm-router/resolve/main/apps/api/src/services/expense/shared/image-processing.test.ts
2 kB
| import { describe, it, expect } from 'vitest' | |
| import sharp from 'sharp' | |
| import { compressImage, validateImage } from './image-processing.js' | |
| async function makeNoiseImageBase64(width: number, height: number, quality = 100): Promise<string> { | |
| const channels = 3 | |
| const pixels = Buffer.alloc(width * height * channels) | |
| for (let i = 0; i < pixels.length; i++) pixels[i] = Math.floor(Math.random() * 256) | |
| const buf = await sharp(pixels, { raw: { width, height, channels } }).jpeg({ quality }).toBuffer() | |
| return buf.toString('base64') | |
| } | |
| describe('compressImage', () => { | |
| it('resizes an oversized image down to a 1800px long edge and shrinks its byte size', async () => { | |
| const input = await makeNoiseImageBase64(3000, 2000) | |
| const output = await compressImage(input) | |
| const meta = await sharp(Buffer.from(output, 'base64')).metadata() | |
| expect(Math.max(meta.width ?? 0, meta.height ?? 0)).toBe(1800) | |
| expect(output.length).toBeLessThan(input.length) | |
| }) | |
| it('leaves a smaller-than-1800px image unscaled', async () => { | |
| const input = await makeNoiseImageBase64(800, 600) | |
| const output = await compressImage(input) | |
| const meta = await sharp(Buffer.from(output, 'base64')).metadata() | |
| expect(meta.width).toBe(800) | |
| expect(meta.height).toBe(600) | |
| }) | |
| it('re-encodes as JPEG regardless of input format', async () => { | |
| const input = await makeNoiseImageBase64(400, 300) | |
| const output = await compressImage(input) | |
| const meta = await sharp(Buffer.from(output, 'base64')).metadata() | |
| expect(meta.format).toBe('jpeg') | |
| }) | |
| }) | |
| describe('validateImage', () => { | |
| it('resolves for a real, decodable image', async () => { | |
| const input = await makeNoiseImageBase64(100, 100) | |
| await expect(validateImage(input)).resolves.toBeUndefined() | |
| }) | |
| it('rejects bytes that are not a decodable image', async () => { | |
| const garbage = Buffer.from('this is not an image').toString('base64') | |
| await expect(validateImage(garbage)).rejects.toThrow() | |
| }) | |
| }) | |