|
1 | | -import sharp from 'sharp'; |
2 | 1 | import { describe, expect, it } from 'vitest'; |
3 | 2 |
|
4 | | -import { getValidBackgroundColor } from './common'; |
5 | | -import { PresetOnlyStrategy } from './config/preset-only-strategy'; |
6 | | -import { Dimensions, Point, resizeToFocalPoint, transformImage } from './transform-image'; |
| 3 | +import { Dimensions, Point, resizeToFocalPoint } from './transform-image'; |
7 | 4 |
|
8 | 5 | describe('resizeToFocalPoint', () => { |
9 | 6 | it('no resize, crop left', () => { |
@@ -70,189 +67,3 @@ describe('resizeToFocalPoint', () => { |
70 | 67 | }); |
71 | 68 | }); |
72 | 69 | }); |
73 | | - |
74 | | -describe('getValidBackgroundColor', () => { |
75 | | - it('accepts 6-char hex without #', () => { |
76 | | - expect(getValidBackgroundColor('ffffff')).toBe('#ffffff'); |
77 | | - }); |
78 | | - |
79 | | - it('accepts 6-char hex with #', () => { |
80 | | - expect(getValidBackgroundColor('#F0A703')).toBe('#f0a703'); |
81 | | - }); |
82 | | - |
83 | | - it('accepts 3-char shorthand hex', () => { |
84 | | - expect(getValidBackgroundColor('fff')).toBe('#fff'); |
85 | | - }); |
86 | | - |
87 | | - it('accepts 8-char hex with alpha', () => { |
88 | | - expect(getValidBackgroundColor('ffffff80')).toBe('#ffffff80'); |
89 | | - }); |
90 | | - |
91 | | - it('accepts 4-char shorthand hex with alpha', () => { |
92 | | - expect(getValidBackgroundColor('fff8')).toBe('#fff8'); |
93 | | - }); |
94 | | - |
95 | | - it('returns undefined for empty string', () => { |
96 | | - expect(getValidBackgroundColor('')).toBeUndefined(); |
97 | | - }); |
98 | | - |
99 | | - it('returns undefined for non-string input', () => { |
100 | | - expect(getValidBackgroundColor(undefined)).toBeUndefined(); |
101 | | - expect(getValidBackgroundColor(123)).toBeUndefined(); |
102 | | - expect(getValidBackgroundColor(null)).toBeUndefined(); |
103 | | - }); |
104 | | - |
105 | | - it('returns undefined for invalid hex characters', () => { |
106 | | - expect(getValidBackgroundColor('gggggg')).toBeUndefined(); |
107 | | - expect(getValidBackgroundColor('xyz')).toBeUndefined(); |
108 | | - }); |
109 | | - |
110 | | - it('returns undefined for wrong length hex', () => { |
111 | | - expect(getValidBackgroundColor('ff')).toBeUndefined(); |
112 | | - expect(getValidBackgroundColor('fffff')).toBeUndefined(); |
113 | | - expect(getValidBackgroundColor('fffffffff')).toBeUndefined(); |
114 | | - }); |
115 | | -}); |
116 | | - |
117 | | -describe('transformImage with backgroundColor', () => { |
118 | | - function createTestPngWithAlpha(width: number, height: number): Promise<Buffer> { |
119 | | - return sharp({ |
120 | | - create: { |
121 | | - width, |
122 | | - height, |
123 | | - channels: 4, |
124 | | - background: { r: 255, g: 0, b: 0, alpha: 0.5 }, |
125 | | - }, |
126 | | - }) |
127 | | - .png() |
128 | | - .toBuffer(); |
129 | | - } |
130 | | - |
131 | | - it('flattens alpha channel with specified background color', async () => { |
132 | | - const input = await createTestPngWithAlpha(10, 10); |
133 | | - const result = await transformImage(input, { |
134 | | - width: 10, |
135 | | - height: 10, |
136 | | - mode: 'resize', |
137 | | - quality: undefined, |
138 | | - format: 'png', |
139 | | - fpx: undefined, |
140 | | - fpy: undefined, |
141 | | - preset: undefined, |
142 | | - backgroundColor: '#ffffff', |
143 | | - }); |
144 | | - const buffer = await result.toBuffer(); |
145 | | - const { channels, hasAlpha } = await sharp(buffer).metadata(); |
146 | | - expect(channels).toBe(3); |
147 | | - expect(hasAlpha).toBe(false); |
148 | | - }); |
149 | | - |
150 | | - it('preserves alpha channel when no backgroundColor is set', async () => { |
151 | | - const input = await createTestPngWithAlpha(10, 10); |
152 | | - const result = await transformImage(input, { |
153 | | - width: 10, |
154 | | - height: 10, |
155 | | - mode: 'resize', |
156 | | - quality: undefined, |
157 | | - format: 'png', |
158 | | - fpx: undefined, |
159 | | - fpy: undefined, |
160 | | - preset: undefined, |
161 | | - backgroundColor: undefined, |
162 | | - }); |
163 | | - const buffer = await result.toBuffer(); |
164 | | - const { channels, hasAlpha } = await sharp(buffer).metadata(); |
165 | | - expect(channels).toBe(4); |
166 | | - expect(hasAlpha).toBe(true); |
167 | | - }); |
168 | | -}); |
169 | | - |
170 | | -describe('PresetOnlyStrategy permittedBackgroundColors', () => { |
171 | | - const presets = [{ name: 'thumb', width: 100, height: 100, mode: 'crop' as const }]; |
172 | | - const baseInput = { |
173 | | - width: undefined, |
174 | | - height: undefined, |
175 | | - mode: undefined, |
176 | | - quality: undefined, |
177 | | - format: undefined, |
178 | | - fpx: undefined, |
179 | | - fpy: undefined, |
180 | | - preset: 'thumb', |
181 | | - backgroundColor: '#ffffff', |
182 | | - }; |
183 | | - |
184 | | - it('drops backgroundColor when permittedBackgroundColors is omitted', () => { |
185 | | - const strategy = new PresetOnlyStrategy({ defaultPreset: 'thumb' }); |
186 | | - const result = strategy.getImageTransformParameters({ |
187 | | - input: baseInput, |
188 | | - availablePresets: presets, |
189 | | - req: {} as any, |
190 | | - }); |
191 | | - expect(result.backgroundColor).toBeUndefined(); |
192 | | - }); |
193 | | - |
194 | | - it('allows any backgroundColor when set to "any"', () => { |
195 | | - const strategy = new PresetOnlyStrategy({ |
196 | | - defaultPreset: 'thumb', |
197 | | - permittedBackgroundColors: 'any', |
198 | | - }); |
199 | | - const result = strategy.getImageTransformParameters({ |
200 | | - input: baseInput, |
201 | | - availablePresets: presets, |
202 | | - req: {} as any, |
203 | | - }); |
204 | | - expect(result.backgroundColor).toBe('#ffffff'); |
205 | | - }); |
206 | | - |
207 | | - it('allows a whitelisted backgroundColor', () => { |
208 | | - const strategy = new PresetOnlyStrategy({ |
209 | | - defaultPreset: 'thumb', |
210 | | - permittedBackgroundColors: ['#ffffff', '#000000'], |
211 | | - }); |
212 | | - const result = strategy.getImageTransformParameters({ |
213 | | - input: baseInput, |
214 | | - availablePresets: presets, |
215 | | - req: {} as any, |
216 | | - }); |
217 | | - expect(result.backgroundColor).toBe('#ffffff'); |
218 | | - }); |
219 | | - |
220 | | - it('drops a non-whitelisted backgroundColor', () => { |
221 | | - const strategy = new PresetOnlyStrategy({ |
222 | | - defaultPreset: 'thumb', |
223 | | - permittedBackgroundColors: ['#000000'], |
224 | | - }); |
225 | | - const result = strategy.getImageTransformParameters({ |
226 | | - input: baseInput, |
227 | | - availablePresets: presets, |
228 | | - req: {} as any, |
229 | | - }); |
230 | | - expect(result.backgroundColor).toBeUndefined(); |
231 | | - }); |
232 | | - |
233 | | - it('whitelist comparison is case-insensitive', () => { |
234 | | - const strategy = new PresetOnlyStrategy({ |
235 | | - defaultPreset: 'thumb', |
236 | | - permittedBackgroundColors: ['#FFFFFF'], |
237 | | - }); |
238 | | - const result = strategy.getImageTransformParameters({ |
239 | | - input: { ...baseInput, backgroundColor: '#ffffff' }, |
240 | | - availablePresets: presets, |
241 | | - req: {} as any, |
242 | | - }); |
243 | | - expect(result.backgroundColor).toBe('#ffffff'); |
244 | | - }); |
245 | | - |
246 | | - it('whitelist comparison is case-insensitive with # prefix', () => { |
247 | | - const strategy = new PresetOnlyStrategy({ |
248 | | - defaultPreset: 'thumb', |
249 | | - permittedBackgroundColors: ['#ffffff'], |
250 | | - }); |
251 | | - const result = strategy.getImageTransformParameters({ |
252 | | - input: { ...baseInput, backgroundColor: '#ffffff' }, |
253 | | - availablePresets: presets, |
254 | | - req: {} as any, |
255 | | - }); |
256 | | - expect(result.backgroundColor).toBe('#ffffff'); |
257 | | - }); |
258 | | -}); |
0 commit comments