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';
|
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
describe('SSG images - dev', function () {
|
|
|
|
let fixture;
|
|
|
|
let devServer;
|
|
|
|
let $;
|
|
|
|
|
2022-07-01 10:47:48 -05:00
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({ root: './fixtures/basic-image/' });
|
2022-08-30 16:09:44 -05:00
|
|
|
devServer = await fixture.startDevServer();
|
|
|
|
const html = await fixture.fetch('/').then((res) => res.text());
|
|
|
|
$ = cheerio.load(html);
|
2022-07-01 10:47:48 -05:00
|
|
|
});
|
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
after(async () => {
|
|
|
|
await devServer.stop();
|
|
|
|
});
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
describe('Local images', () => {
|
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#social-jpg');
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
expect(route).to.equal('/@astroimage/assets/social.jpg');
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
const searchParams = new URLSearchParams(params);
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
expect(searchParams.get('f')).to.equal('jpg');
|
|
|
|
expect(searchParams.get('w')).to.equal('506');
|
|
|
|
expect(searchParams.get('h')).to.equal('253');
|
2022-07-08 16:37:55 -05:00
|
|
|
});
|
2022-08-30 16:09:44 -05:00
|
|
|
});
|
2022-07-08 16:37:55 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
describe('Local images with inline imports', () => {
|
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#inline');
|
2022-07-08 16:37:55 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
expect(route).to.equal('/@astroimage/assets/social.jpg');
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
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');
|
2022-07-01 10:47:48 -05:00
|
|
|
});
|
2022-08-30 16:09:44 -05:00
|
|
|
});
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
describe('Remote images', () => {
|
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#google');
|
2022-07-18 23:48:22 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
expect(route).to.equal('/_image');
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
const searchParams = new URLSearchParams(params);
|
2022-08-22 14:45:34 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
expect(searchParams.get('f')).to.equal('webp');
|
|
|
|
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-01 10:47:48 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
describe('/public images', () => {
|
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#hero');
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
|
|
|
|
|
|
|
expect(route).to.equal('/_image');
|
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(params);
|
|
|
|
|
|
|
|
expect(searchParams.get('href')).to.equal('/hero.jpg');
|
|
|
|
expect(searchParams.get('f')).to.equal('webp');
|
|
|
|
expect(searchParams.get('w')).to.equal('768');
|
|
|
|
expect(searchParams.get('h')).to.equal('414');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('SSG images with subpath - dev', function () {
|
|
|
|
let fixture;
|
|
|
|
let devServer;
|
|
|
|
let $;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({ root: './fixtures/basic-image/', base: '/docs' });
|
|
|
|
devServer = await fixture.startDevServer();
|
|
|
|
const html = await fixture.fetch('/docs/').then((res) => res.text());
|
2022-07-01 10:47:48 -05:00
|
|
|
$ = cheerio.load(html);
|
2022-08-30 16:09:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await devServer.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Local images', () => {
|
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#social-jpg');
|
|
|
|
|
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
|
|
|
|
|
|
|
expect(route).to.equal('/@astroimage/assets/social.jpg');
|
|
|
|
|
|
|
|
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');
|
2022-07-01 10:47:48 -05:00
|
|
|
});
|
2022-08-30 16:09:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Local images with inline imports', () => {
|
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#inline');
|
|
|
|
|
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
|
|
|
|
|
|
|
expect(route).to.equal('/@astroimage/assets/social.jpg');
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
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');
|
2022-07-01 10:47:48 -05:00
|
|
|
});
|
2022-08-30 16:09:44 -05:00
|
|
|
});
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
describe('Remote images', () => {
|
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#google');
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
expect(route).to.equal('/_image');
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
const searchParams = new URLSearchParams(params);
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
expect(searchParams.get('f')).to.equal('webp');
|
|
|
|
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-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
describe('/public images', () => {
|
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#hero');
|
2022-07-01 12:43:26 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
2022-07-01 12:43:26 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
expect(route).to.equal('/_image');
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
const searchParams = new URLSearchParams(params);
|
|
|
|
|
|
|
|
expect(searchParams.get('href')).to.equal('/hero.jpg');
|
|
|
|
expect(searchParams.get('f')).to.equal('webp');
|
|
|
|
expect(searchParams.get('w')).to.equal('768');
|
|
|
|
expect(searchParams.get('h')).to.equal('414');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('SSG images - build', function () {
|
|
|
|
let fixture;
|
|
|
|
let $;
|
|
|
|
let html;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({ root: './fixtures/basic-image/' });
|
|
|
|
await fixture.build();
|
|
|
|
|
|
|
|
html = await fixture.readFile('/index.html');
|
|
|
|
$ = cheerio.load(html);
|
|
|
|
});
|
|
|
|
|
|
|
|
function verifyImage(pathname, expected) {
|
|
|
|
const url = new URL('./fixtures/basic-image/dist/' + pathname, import.meta.url);
|
|
|
|
const dist = fileURLToPath(url);
|
|
|
|
const result = sizeOf(dist);
|
|
|
|
expect(result).to.deep.equal(expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('Local images', () => {
|
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#social-jpg');
|
|
|
|
|
|
|
|
expect(image.attr('src')).to.equal('/social.cece8c77_1zwatP.jpg');
|
|
|
|
expect(image.attr('width')).to.equal('506');
|
|
|
|
expect(image.attr('height')).to.equal('253');
|
2022-07-01 10:47:48 -05:00
|
|
|
});
|
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
it('built the optimized image', () => {
|
|
|
|
verifyImage('social.cece8c77_1zwatP.jpg', { width: 506, height: 253, type: 'jpg' });
|
|
|
|
});
|
|
|
|
});
|
2022-07-08 16:37:55 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
describe('Inline imports', () => {
|
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#inline');
|
2022-07-08 16:37:55 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
expect(image.attr('src')).to.equal('/social.cece8c77_Z2tF99.jpg');
|
|
|
|
expect(image.attr('width')).to.equal('506');
|
|
|
|
expect(image.attr('height')).to.equal('253');
|
|
|
|
});
|
2022-07-08 16:37:55 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
it('built the optimized image', () => {
|
|
|
|
verifyImage('social.cece8c77_Z2tF99.jpg', { width: 506, height: 253, type: 'jpg' });
|
|
|
|
});
|
|
|
|
});
|
2022-07-08 16:37:55 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
describe('Remote images', () => {
|
|
|
|
// Hard-coding in the test! These should never change since the hash is based
|
|
|
|
// on the static `src` string
|
|
|
|
const HASH = 'Z1RBHqs';
|
|
|
|
const HASH_WITH_QUERY = 'Z17oujH';
|
2022-07-08 16:37:55 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#google');
|
2022-07-08 16:37:55 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
expect(image.attr('src')).to.equal(
|
|
|
|
`/googlelogo_color_272x92dp_${HASH}.webp`
|
|
|
|
);
|
|
|
|
expect(image.attr('width')).to.equal('544');
|
|
|
|
expect(image.attr('height')).to.equal('184');
|
|
|
|
});
|
2022-07-08 16:37:55 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
it('built the optimized image', () => {
|
|
|
|
verifyImage(`/googlelogo_color_272x92dp_${HASH}.webp`, {
|
|
|
|
width: 544,
|
|
|
|
height: 184,
|
|
|
|
type: 'webp',
|
|
|
|
});
|
|
|
|
});
|
2022-07-08 16:37:55 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
it('removes query strings', () => {
|
|
|
|
verifyImage(`/googlelogo_color_272x92dp_${HASH_WITH_QUERY}.webp`, {
|
|
|
|
width: 544,
|
|
|
|
height: 184,
|
|
|
|
type: 'webp',
|
2022-07-08 16:37:55 -05:00
|
|
|
});
|
|
|
|
});
|
2022-08-30 16:09:44 -05:00
|
|
|
});
|
2022-07-08 16:37:55 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
describe('/public images', () => {
|
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#hero');
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
expect(image.attr('src')).to.equal('/hero_Z2k1JGN.webp');
|
|
|
|
expect(image.attr('width')).to.equal('768');
|
|
|
|
expect(image.attr('height')).to.equal('414');
|
|
|
|
});
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
it('built the optimized image', () => {
|
|
|
|
verifyImage('hero_Z2k1JGN.webp', { width: 768, height: 414, type: 'webp' });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
describe('SSG images with subpath - build', function () {
|
|
|
|
let fixture;
|
|
|
|
let $;
|
|
|
|
let html;
|
2022-07-01 10:47:48 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({ root: './fixtures/basic-image/', base: '/docs' });
|
|
|
|
await fixture.build();
|
2022-08-22 14:45:34 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
html = await fixture.readFile('/index.html');
|
|
|
|
$ = cheerio.load(html);
|
|
|
|
});
|
2022-08-22 14:45:34 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
function verifyImage(pathname, expected) {
|
|
|
|
const url = new URL('./fixtures/basic-image/dist/' + pathname, import.meta.url);
|
|
|
|
const dist = fileURLToPath(url);
|
|
|
|
const result = sizeOf(dist);
|
|
|
|
expect(result).to.deep.equal(expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('Local images', () => {
|
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#social-jpg');
|
|
|
|
|
|
|
|
expect(image.attr('src')).to.equal('/docs/social.cece8c77_iK4oy.jpg');
|
|
|
|
expect(image.attr('width')).to.equal('506');
|
|
|
|
expect(image.attr('height')).to.equal('253');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('built the optimized image', () => {
|
|
|
|
verifyImage('social.cece8c77_iK4oy.jpg', { width: 506, height: 253, type: 'jpg' });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Inline imports', () => {
|
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#inline');
|
|
|
|
|
|
|
|
expect(image.attr('src')).to.equal('/docs/social.cece8c77_1YIUw1.jpg');
|
|
|
|
expect(image.attr('width')).to.equal('506');
|
|
|
|
expect(image.attr('height')).to.equal('253');
|
|
|
|
});
|
2022-08-22 14:45:34 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
it('built the optimized image', () => {
|
|
|
|
verifyImage('social.cece8c77_1YIUw1.jpg', { width: 506, height: 253, type: 'jpg' });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Remote images', () => {
|
|
|
|
// Hard-coding in the test! These should never change since the hash is based
|
|
|
|
// on the static `src` string
|
|
|
|
const HASH = 'Z1RBHqs';
|
|
|
|
const HASH_WITH_QUERY = 'Z17oujH';
|
|
|
|
|
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#google');
|
2022-08-22 14:45:34 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
expect(image.attr('src')).to.equal(
|
|
|
|
`/docs/googlelogo_color_272x92dp_${HASH}.webp`
|
|
|
|
);
|
|
|
|
expect(image.attr('width')).to.equal('544');
|
|
|
|
expect(image.attr('height')).to.equal('184');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('built the optimized image', () => {
|
|
|
|
verifyImage(`/googlelogo_color_272x92dp_${HASH}.webp`, {
|
|
|
|
width: 544,
|
|
|
|
height: 184,
|
|
|
|
type: 'webp',
|
|
|
|
});
|
|
|
|
});
|
2022-08-22 14:45:34 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
it('removes query strings', () => {
|
|
|
|
verifyImage(`/googlelogo_color_272x92dp_${HASH_WITH_QUERY}.webp`, {
|
|
|
|
width: 544,
|
|
|
|
height: 184,
|
|
|
|
type: 'webp',
|
2022-08-22 14:45:34 -05:00
|
|
|
});
|
2022-07-01 10:47:48 -05:00
|
|
|
});
|
|
|
|
});
|
2022-08-30 16:09:44 -05:00
|
|
|
|
|
|
|
describe('/public images', () => {
|
|
|
|
it('includes <img> attributes', () => {
|
|
|
|
const image = $('#hero');
|
|
|
|
|
|
|
|
expect(image.attr('src')).to.equal('/docs/hero_Z2k1JGN.webp');
|
|
|
|
expect(image.attr('width')).to.equal('768');
|
|
|
|
expect(image.attr('height')).to.equal('414');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('built the optimized image', () => {
|
|
|
|
verifyImage('hero_Z2k1JGN.webp', { width: 768, height: 414, type: 'webp' });
|
|
|
|
});
|
|
|
|
});
|
2022-07-01 10:47:48 -05:00
|
|
|
});
|