Skip to content

Commit a1503df

Browse files
committedApr 2, 2025·
test(@invertase/image-processing-api): add vitest as test framework
1 parent d778560 commit a1503df

File tree

3 files changed

+3670
-1346
lines changed

3 files changed

+3670
-1346
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
});

‎extensions/image-processing-api/lib/package-lock.json

+3,529-1,344
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎extensions/image-processing-api/lib/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@
1919
"update-types": "cd ../functions && npm run copy-types",
2020
"prepublishOnly": "npm run update-types && npm run build",
2121
"clean": "rimraf dist",
22-
"test": "echo \"Error: no test specified\" && exit 1"
22+
"test": "vitest run"
2323
},
2424
"author": "Invertase <oss@invertase.io>",
2525
"license": "Apache-2.0",
2626
"devDependencies": {
27+
"@vitest/ui": "^3.1.1",
2728
"rimraf": "^3.0.2",
2829
"rollup": "^2.79.1",
2930
"rollup-plugin-copy": "^3.4.0",
3031
"rollup-plugin-typescript2": "^0.34.0",
31-
"typescript": "^4.8.3"
32+
"typescript": "^4.8.3",
33+
"vitest": "^3.1.1"
3234
},
3335
"dependencies": {
3436
"superstruct": "^0.16.4"

0 commit comments

Comments
 (0)
Please sign in to comment.