|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { builder } from '../../src'; // Adjust this path if needed |
| 3 | + |
| 4 | +describe('ImageProcessingApi builder', () => { |
| 5 | + it('should throw if input is missing', () => { |
| 6 | + expect(() => builder().output({ jpeg: {} }).toJSON()).toThrow(); |
| 7 | + }); |
| 8 | + |
| 9 | + it('should throw if output is missing', () => { |
| 10 | + expect(() => |
| 11 | + builder() |
| 12 | + .input({ type: 'url', url: 'https://example.com/image.jpg' }) |
| 13 | + .toJSON(), |
| 14 | + ).toThrow(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should include input and output correctly', () => { |
| 18 | + const ops = builder() |
| 19 | + .input({ type: 'url', url: 'https://example.com/image.jpg' }) |
| 20 | + .output({ jpeg: {} }) |
| 21 | + .toJSON(); |
| 22 | + |
| 23 | + expect(ops[0]).toEqual({ |
| 24 | + operation: 'input', |
| 25 | + type: 'url', |
| 26 | + url: 'https://example.com/image.jpg', |
| 27 | + }); |
| 28 | + |
| 29 | + expect(ops[ops.length - 1]).toEqual({ |
| 30 | + operation: 'output', |
| 31 | + format: 'jpeg', |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should handle all operation types', () => { |
| 36 | + const api = builder() |
| 37 | + .input({ type: 'url', url: 'https://example.com/image.jpg' }) |
| 38 | + .affine({ matrix: [1, 0, 0, 1] }) |
| 39 | + .blur({ sigma: 2 }) |
| 40 | + .clahe({ width: 10, height: 10 }) |
| 41 | + .colorspace({ colorspace: 'b-w' }) |
| 42 | + .composite({ |
| 43 | + input: 'https://example.com/overlay.png', |
| 44 | + blend: 'over', |
| 45 | + density: 300, |
| 46 | + }) |
| 47 | + .convolve({ width: 3, height: 3, kernel: [1, 0, -1, 0, 0, 0, -1, 0, 1] }) |
| 48 | + .extend({ top: 5 }) |
| 49 | + .extract({ width: 100, height: 100, left: 10, top: 10 }) |
| 50 | + .flatten({ background: 'white' }) |
| 51 | + .flip() |
| 52 | + .flop() |
| 53 | + .gamma({ gamma: 2.2 }) |
| 54 | + .grayscale() |
| 55 | + .linear({ a: 2, b: 3 }) |
| 56 | + .median({ size: 5 }) |
| 57 | + .modulate({ brightness: 1.5 }) |
| 58 | + .negate() |
| 59 | + .recomb({ |
| 60 | + matrix: [ |
| 61 | + [0.5, 0.5, 0], |
| 62 | + [0.5, 0.5, 0], |
| 63 | + [0, 0, 1], |
| 64 | + ], |
| 65 | + }) |
| 66 | + .resize({ width: 200, height: 150 }) |
| 67 | + .rotate({ angle: 90 }) |
| 68 | + .sharpen({ sigma: 1.5, flat: 0.5, jagged: 1 }) |
| 69 | + .text({ |
| 70 | + value: 'Hello world', |
| 71 | + blend: 'over', |
| 72 | + font: 'Arial', |
| 73 | + textColor: 'black', |
| 74 | + strokeWidth: 1, |
| 75 | + strokeColor: 'white', |
| 76 | + padding: 5, |
| 77 | + borderWidth: 1, |
| 78 | + borderColor: 'black', |
| 79 | + maxWidth: 300, |
| 80 | + }) |
| 81 | + .threshold({ threshold: 128 }) |
| 82 | + .tint({ rgb: 'blue' }) |
| 83 | + .trim({ threshold: 10 }) |
| 84 | + .output({ webp: {} }); |
| 85 | + |
| 86 | + const operations = api.toJSON(); |
| 87 | + |
| 88 | + expect(operations[0]).toEqual({ |
| 89 | + operation: 'input', |
| 90 | + type: 'url', |
| 91 | + url: 'https://example.com/image.jpg', |
| 92 | + }); |
| 93 | + |
| 94 | + expect(operations.some(op => op.operation === 'grayscale')).toBe(true); |
| 95 | + expect(operations.some(op => op.operation === 'trim')).toBe(true); |
| 96 | + expect(operations[operations.length - 1]).toEqual({ |
| 97 | + operation: 'output', |
| 98 | + format: 'webp', |
| 99 | + }); |
| 100 | + |
| 101 | + expect(() => api.toEncodedString()).not.toThrow(); |
| 102 | + expect(() => api.toString()).not.toThrow(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should handle "path" input', () => { |
| 106 | + const ops = builder() |
| 107 | + .input({ type: 'path', path: '/images/photo.jpg' }) |
| 108 | + .output({ png: {} }) |
| 109 | + .toJSON(); |
| 110 | + |
| 111 | + expect(ops[0]).toEqual({ |
| 112 | + operation: 'input', |
| 113 | + type: 'path', |
| 114 | + path: '/images/photo.jpg', |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should handle "create" input', () => { |
| 119 | + const ops = builder() |
| 120 | + .input({ |
| 121 | + type: 'create', |
| 122 | + width: 100, |
| 123 | + height: 100, |
| 124 | + channels: 3, |
| 125 | + noiseMean: 0, |
| 126 | + format: 'png', |
| 127 | + noiseSigma: 0, |
| 128 | + }) |
| 129 | + .output({ png: {} }) |
| 130 | + .toJSON(); |
| 131 | + |
| 132 | + expect(ops[0]).toEqual({ |
| 133 | + operation: 'input', |
| 134 | + type: 'create', |
| 135 | + }); |
| 136 | + }); |
| 137 | +}); |
0 commit comments