2022-07-08 21:37:55 +00:00
|
|
|
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');
|
2022-07-08 21:40:22 +00:00
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
|
|
|
// TODO: better coverage to verify source props
|
|
|
|
});
|
|
|
|
|
2022-07-19 19:21:58 +00:00
|
|
|
it('includes <img> attributes', () => {
|
2022-07-08 21:37:55 +00:00
|
|
|
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');
|
2022-07-19 19:21:58 +00:00
|
|
|
expect(image.attr('alt')).to.equal('Social image');
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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');
|
2022-07-08 21:40:22 +00:00
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
|
|
|
// TODO: better coverage to verify source props
|
|
|
|
});
|
|
|
|
|
2022-07-19 19:21:58 +00:00
|
|
|
it('includes <img> attributes', () => {
|
2022-07-08 21:37:55 +00:00
|
|
|
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');
|
2022-07-19 19:21:58 +00:00
|
|
|
expect(image.attr('alt')).to.equal('Inline social image');
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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', () => {
|
2022-07-19 04:48:22 +00:00
|
|
|
// Hard-coding in the test! This should never change since the hash is based
|
|
|
|
// on the static `src` string
|
|
|
|
const HASH = 'Z1iI4xW';
|
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
it('includes sources', () => {
|
|
|
|
const sources = $('#google source');
|
2022-07-08 21:40:22 +00:00
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
|
|
|
// TODO: better coverage to verify source props
|
|
|
|
});
|
|
|
|
|
2022-07-19 19:21:58 +00:00
|
|
|
it('includes <img> attributes', () => {
|
2022-07-08 21:37:55 +00:00
|
|
|
const image = $('#google img');
|
|
|
|
|
2022-07-19 04:48:22 +00:00
|
|
|
expect(image.attr('src')).to.equal(`/_image/googlelogo_color_272x92dp-${HASH}_544x184.png`);
|
2022-07-08 21:37:55 +00:00
|
|
|
expect(image.attr('width')).to.equal('544');
|
|
|
|
expect(image.attr('height')).to.equal('184');
|
2022-07-19 19:21:58 +00:00
|
|
|
expect(image.attr('alt')).to.equal('Google logo');
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('built the optimized image', () => {
|
2022-07-19 04:48:22 +00:00
|
|
|
verifyImage(`_image/googlelogo_color_272x92dp-${HASH}_272x92.avif`, {
|
2022-07-08 21:37:55 +00:00
|
|
|
width: 272,
|
|
|
|
height: 92,
|
|
|
|
type: 'avif',
|
|
|
|
});
|
2022-07-19 04:48:22 +00:00
|
|
|
verifyImage(`_image/googlelogo_color_272x92dp-${HASH}_272x92.webp`, {
|
2022-07-08 21:37:55 +00:00
|
|
|
width: 272,
|
|
|
|
height: 92,
|
|
|
|
type: 'webp',
|
|
|
|
});
|
2022-07-19 04:48:22 +00:00
|
|
|
verifyImage(`_image/googlelogo_color_272x92dp-${HASH}_272x92.png`, {
|
2022-07-08 21:37:55 +00:00
|
|
|
width: 272,
|
|
|
|
height: 92,
|
|
|
|
type: 'png',
|
|
|
|
});
|
2022-07-19 04:48:22 +00:00
|
|
|
verifyImage(`_image/googlelogo_color_272x92dp-${HASH}_544x184.avif`, {
|
2022-07-08 21:37:55 +00:00
|
|
|
width: 544,
|
|
|
|
height: 184,
|
|
|
|
type: 'avif',
|
|
|
|
});
|
2022-07-19 04:48:22 +00:00
|
|
|
verifyImage(`_image/googlelogo_color_272x92dp-${HASH}_544x184.webp`, {
|
2022-07-08 21:37:55 +00:00
|
|
|
width: 544,
|
|
|
|
height: 184,
|
|
|
|
type: 'webp',
|
|
|
|
});
|
2022-07-19 04:48:22 +00:00
|
|
|
verifyImage(`_image/googlelogo_color_272x92dp-${HASH}_544x184.png`, {
|
2022-07-08 21:37:55 +00:00
|
|
|
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');
|
2022-07-08 21:40:22 +00:00
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
|
|
|
// TODO: better coverage to verify source props
|
|
|
|
});
|
|
|
|
|
2022-07-19 19:21:58 +00:00
|
|
|
it('includes <img> attributes', () => {
|
2022-07-08 21:37:55 +00:00
|
|
|
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);
|
2022-07-19 19:21:58 +00:00
|
|
|
expect(image.attr('alt')).to.equal('Social image');
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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');
|
2022-07-08 21:40:22 +00:00
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
|
|
|
// TODO: better coverage to verify source props
|
|
|
|
});
|
|
|
|
|
2022-07-19 19:21:58 +00:00
|
|
|
it('includes <img> attributes', () => {
|
2022-07-08 21:37:55 +00:00
|
|
|
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);
|
2022-07-19 19:21:58 +00:00
|
|
|
expect(image.attr('alt')).to.equal('Inline social image');
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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');
|
2022-07-08 21:40:22 +00:00
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
|
|
|
// TODO: better coverage to verify source props
|
|
|
|
});
|
|
|
|
|
2022-07-19 19:21:58 +00:00
|
|
|
it('includes <img> attributes', () => {
|
2022-07-08 21:37:55 +00:00
|
|
|
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'
|
|
|
|
);
|
2022-07-19 19:21:58 +00:00
|
|
|
expect(image.attr('alt')).to.equal('Google logo');
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|