summaryrefslogtreecommitdiff
path: root/packages/integrations/image/test/picture-ssg.test.js
blob: 0da1daa1c2f6be7d0ff6c9f9ca251efebb853c90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import fs from 'fs';
import sizeOf from 'image-size';
import { fileURLToPath } from 'url';
import { loadFixture } from './test-utils.js';

let fixture;

describe('SSG pictures', function () {
	before(async () => {
		fixture = await loadFixture({ root: './fixtures/basic-picture/' });
	});

	function verifyImage(pathname, expected) {
		const url = new URL('./fixtures/basic-picture/dist/' + pathname, import.meta.url);
		const dist = fileURLToPath(url);

		// image-size doesn't support AVIF files
		if (expected.type !== 'avif') {
			const result = sizeOf(dist);
			expect(result).to.deep.equal(expected);
		} else {
			expect(fs.statSync(dist)).not.to.be.undefined;
		}
	}

	describe('build', () => {
		let $;
		let html;

		before(async () => {
			await fixture.build();

			html = await fixture.readFile('/index.html');
			$ = cheerio.load(html);
		});

		describe('Local images', () => {
			it('includes sources', () => {
				const sources = $('#social-jpg source');

				expect(sources.length).to.equal(3);

				// TODO: better coverage to verify source props
			});

			it('includes src, width, and height attributes', () => {
				const image = $('#social-jpg img');

				expect(image.attr('src')).to.equal('/_image/assets/social_506x253.jpg');
				expect(image.attr('width')).to.equal('506');
				expect(image.attr('height')).to.equal('253');
			});

			it('built the optimized image', () => {
				verifyImage('_image/assets/social_253x127.avif', { width: 253, height: 127, type: 'avif' });
				verifyImage('_image/assets/social_253x127.webp', { width: 253, height: 127, type: 'webp' });
				verifyImage('_image/assets/social_253x127.jpg', { width: 253, height: 127, type: 'jpg' });
				verifyImage('_image/assets/social_506x253.avif', { width: 506, height: 253, type: 'avif' });
				verifyImage('_image/assets/social_506x253.webp', { width: 506, height: 253, type: 'webp' });
				verifyImage('_image/assets/social_506x253.jpg', { width: 506, height: 253, type: 'jpg' });
			});
		});

		describe('Inline imports', () => {
			it('includes sources', () => {
				const sources = $('#inline source');

				expect(sources.length).to.equal(3);

				// TODO: better coverage to verify source props
			});

			it('includes src, width, and height attributes', () => {
				const image = $('#inline img');

				expect(image.attr('src')).to.equal('/_image/assets/social_506x253.jpg');
				expect(image.attr('width')).to.equal('506');
				expect(image.attr('height')).to.equal('253');
			});

			it('built the optimized image', () => {
				verifyImage('_image/assets/social_253x127.avif', { width: 253, height: 127, type: 'avif' });
				verifyImage('_image/assets/social_253x127.webp', { width: 253, height: 127, type: 'webp' });
				verifyImage('_image/assets/social_253x127.jpg', { width: 253, height: 127, type: 'jpg' });
				verifyImage('_image/assets/social_506x253.avif', { width: 506, height: 253, type: 'avif' });
				verifyImage('_image/assets/social_506x253.webp', { width: 506, height: 253, type: 'webp' });
				verifyImage('_image/assets/social_506x253.jpg', { width: 506, height: 253, type: 'jpg' });
			});
		});

		describe('Remote images', () => {
			// Hard-coding in the test! This should never change since the hash is based
			// on the static `src` string
			const HASH = 'Z1iI4xW';

			it('includes sources', () => {
				const sources = $('#google source');

				expect(sources.length).to.equal(3);

				// TODO: better coverage to verify source props
			});

			it('includes src, width, and height attributes', () => {
				const image = $('#google img');

				expect(image.attr('src')).to.equal(`/_image/googlelogo_color_272x92dp-${HASH}_544x184.png`);
				expect(image.attr('width')).to.equal('544');
				expect(image.attr('height')).to.equal('184');
			});

			it('built the optimized image', () => {
				verifyImage(`_image/googlelogo_color_272x92dp-${HASH}_272x92.avif`, {
					width: 272,
					height: 92,
					type: 'avif',
				});
				verifyImage(`_image/googlelogo_color_272x92dp-${HASH}_272x92.webp`, {
					width: 272,
					height: 92,
					type: 'webp',
				});
				verifyImage(`_image/googlelogo_color_272x92dp-${HASH}_272x92.png`, {
					width: 272,
					height: 92,
					type: 'png',
				});
				verifyImage(`_image/googlelogo_color_272x92dp-${HASH}_544x184.avif`, {
					width: 544,
					height: 184,
					type: 'avif',
				});
				verifyImage(`_image/googlelogo_color_272x92dp-${HASH}_544x184.webp`, {
					width: 544,
					height: 184,
					type: 'webp',
				});
				verifyImage(`_image/googlelogo_color_272x92dp-${HASH}_544x184.png`, {
					width: 544,
					height: 184,
					type: 'png',
				});
			});
		});
	});

	describe('dev', () => {
		let devServer;
		let $;

		before(async () => {
			devServer = await fixture.startDevServer();
			const html = await fixture.fetch('/').then((res) => res.text());
			$ = cheerio.load(html);
		});

		after(async () => {
			await devServer.stop();
		});

		describe('Local images', () => {
			it('includes sources', () => {
				const sources = $('#social-jpg source');

				expect(sources.length).to.equal(3);

				// TODO: better coverage to verify source props
			});

			it('includes src, width, and height attributes', () => {
				const image = $('#social-jpg img');

				const src = image.attr('src');
				const [route, params] = src.split('?');

				expect(route).to.equal('/_image');

				const searchParams = new URLSearchParams(params);

				expect(searchParams.get('f')).to.equal('jpg');
				expect(searchParams.get('w')).to.equal('506');
				expect(searchParams.get('h')).to.equal('253');
				// TODO: possible to avoid encoding the full image path?
				expect(searchParams.get('href').endsWith('/assets/social.jpg')).to.equal(true);
			});

			it('returns the optimized image', async () => {
				const image = $('#social-jpg img');

				const res = await fixture.fetch(image.attr('src'));

				expect(res.status).to.equal(200);
				expect(res.headers.get('Content-Type')).to.equal('image/jpeg');

				// TODO: verify image file? It looks like sizeOf doesn't support ArrayBuffers
			});
		});

		describe('Local images with inline imports', () => {
			it('includes sources', () => {
				const sources = $('#inline source');

				expect(sources.length).to.equal(3);

				// TODO: better coverage to verify source props
			});

			it('includes src, width, and height attributes', () => {
				const image = $('#inline img');

				const src = image.attr('src');
				const [route, params] = src.split('?');

				expect(route).to.equal('/_image');

				const searchParams = new URLSearchParams(params);

				expect(searchParams.get('f')).to.equal('jpg');
				expect(searchParams.get('w')).to.equal('506');
				expect(searchParams.get('h')).to.equal('253');
				// TODO: possible to avoid encoding the full image path?
				expect(searchParams.get('href').endsWith('/assets/social.jpg')).to.equal(true);
			});

			it('returns the optimized image', async () => {
				const image = $('#inline img');

				const res = await fixture.fetch(image.attr('src'));

				expect(res.status).to.equal(200);
				expect(res.headers.get('Content-Type')).to.equal('image/jpeg');

				// TODO: verify image file? It looks like sizeOf doesn't support ArrayBuffers
			});
		});

		describe('Remote images', () => {
			it('includes sources', () => {
				const sources = $('#google source');

				expect(sources.length).to.equal(3);

				// TODO: better coverage to verify source props
			});

			it('includes src, width, and height attributes', () => {
				const image = $('#google img');

				const src = image.attr('src');
				const [route, params] = src.split('?');

				expect(route).to.equal('/_image');

				const searchParams = new URLSearchParams(params);

				expect(searchParams.get('f')).to.equal('png');
				expect(searchParams.get('w')).to.equal('544');
				expect(searchParams.get('h')).to.equal('184');
				expect(searchParams.get('href')).to.equal(
					'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png'
				);
			});
		});
	});
});