2022-07-01 10:47:48 -05:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import * as cheerio from 'cheerio';
|
|
|
|
import sizeOf from 'image-size';
|
2022-07-07 12:49:46 -05:00
|
|
|
import { fileURLToPath } from 'url';
|
2022-07-01 10:47:48 -05:00
|
|
|
import { loadFixture } from './test-utils.js';
|
|
|
|
|
|
|
|
let fixture;
|
|
|
|
|
|
|
|
describe('SSG images', function () {
|
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({ root: './fixtures/basic-image/' });
|
|
|
|
});
|
|
|
|
|
|
|
|
function verifyImage(pathname, expected) {
|
2022-07-07 12:49:46 -05:00
|
|
|
const url = new URL('./fixtures/basic-image/dist/' + pathname, import.meta.url);
|
|
|
|
const dist = fileURLToPath(url);
|
2022-07-01 10:47:48 -05:00
|
|
|
const result = sizeOf(dist);
|
|
|
|
expect(result).to.deep.equal(expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('build', () => {
|
|
|
|
let $;
|
|
|
|
let html;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
await fixture.build();
|
|
|
|
|
|
|
|
html = await fixture.readFile('/index.html');
|
|
|
|
$ = cheerio.load(html);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Local images', () => {
|
2022-07-22 18:01:56 -05:00
|
|
|
it('includes <img> attributes', () => {
|
2022-07-01 10:47:48 -05:00
|
|
|
const image = $('#social-jpg');
|
|
|
|
|
|
|
|
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-08 16:37:55 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Inline imports', () => {
|
2022-07-22 18:01:56 -05:00
|
|
|
it('includes <img> attributes', () => {
|
2022-07-08 16:37:55 -05:00
|
|
|
const image = $('#inline');
|
|
|
|
|
|
|
|
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-01 10:47:48 -05:00
|
|
|
|
|
|
|
it('built the optimized image', () => {
|
|
|
|
verifyImage('_image/assets/social_506x253.jpg', { width: 506, height: 253, type: 'jpg' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('dist includes original image', () => {
|
|
|
|
verifyImage('assets/social.jpg', { width: 2024, height: 1012, type: 'jpg' });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Remote images', () => {
|
2022-07-18 23:48:22 -05: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-22 18:01:56 -05:00
|
|
|
it('includes <img> attributes', () => {
|
2022-07-01 10:47:48 -05:00
|
|
|
const image = $('#google');
|
|
|
|
|
2022-07-18 23:50:06 -05:00
|
|
|
expect(image.attr('src')).to.equal(
|
|
|
|
`/_image/googlelogo_color_272x92dp-${HASH}_544x184.webp`
|
|
|
|
);
|
2022-07-01 10:47:48 -05:00
|
|
|
expect(image.attr('width')).to.equal('544');
|
|
|
|
expect(image.attr('height')).to.equal('184');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('built the optimized image', () => {
|
2022-07-18 23:48:22 -05:00
|
|
|
verifyImage(`_image/googlelogo_color_272x92dp-${HASH}_544x184.webp`, {
|
2022-07-01 12:43:26 -05:00
|
|
|
width: 544,
|
|
|
|
height: 184,
|
|
|
|
type: 'webp',
|
|
|
|
});
|
2022-07-01 10:47:48 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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', () => {
|
2022-07-22 18:01:56 -05:00
|
|
|
it('includes <img> attributes', () => {
|
2022-07-01 10:47:48 -05:00
|
|
|
const image = $('#social-jpg');
|
|
|
|
|
|
|
|
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');
|
2022-07-01 12:43:26 -05:00
|
|
|
|
2022-07-01 10:47:48 -05:00
|
|
|
const res = await fixture.fetch(image.attr('src'));
|
2022-07-01 12:43:26 -05:00
|
|
|
|
2022-07-01 10:47:48 -05:00
|
|
|
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
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-07-08 16:37:55 -05:00
|
|
|
describe('Local images with inline imports', () => {
|
2022-07-22 18:01:56 -05:00
|
|
|
it('includes <img> attributes', () => {
|
2022-07-08 16:37:55 -05:00
|
|
|
const image = $('#inline');
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-07-01 10:47:48 -05:00
|
|
|
describe('Remote images', () => {
|
2022-07-22 18:01:56 -05:00
|
|
|
it('includes <img> attributes', () => {
|
2022-07-01 10:47:48 -05:00
|
|
|
const image = $('#google');
|
|
|
|
|
|
|
|
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('webp');
|
|
|
|
expect(searchParams.get('w')).to.equal('544');
|
|
|
|
expect(searchParams.get('h')).to.equal('184');
|
2022-07-01 12:43:26 -05:00
|
|
|
expect(searchParams.get('href')).to.equal(
|
|
|
|
'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png'
|
|
|
|
);
|
2022-07-01 10:47:48 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|