2022-07-01 15:47:48 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import * as cheerio from 'cheerio';
|
|
|
|
import sizeOf from 'image-size';
|
2022-07-07 17:49:46 +00:00
|
|
|
import { fileURLToPath } from 'url';
|
2022-07-01 15:47:48 +00:00
|
|
|
import { loadFixture } from './test-utils.js';
|
|
|
|
|
2022-08-30 21:09:44 +00:00
|
|
|
describe('SSG images - dev', function () {
|
|
|
|
let fixture;
|
|
|
|
let devServer;
|
|
|
|
let $;
|
2022-08-30 21:12:45 +00:00
|
|
|
|
2022-07-01 15:47:48 +00:00
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({ root: './fixtures/basic-image/' });
|
2022-08-30 21:09:44 +00:00
|
|
|
devServer = await fixture.startDevServer();
|
2022-08-30 21:12:45 +00:00
|
|
|
const html = await fixture.fetch('/').then((res) => res.text());
|
|
|
|
$ = cheerio.load(html);
|
2022-07-01 15:47:48 +00:00
|
|
|
});
|
|
|
|
|
2022-08-30 21:09:44 +00:00
|
|
|
after(async () => {
|
|
|
|
await devServer.stop();
|
|
|
|
});
|
2022-07-01 15:47:48 +00:00
|
|
|
|
2022-09-01 14:58:57 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Local images',
|
|
|
|
id: '#social-jpg',
|
|
|
|
url: '/@astroimage/assets/social.jpg',
|
2022-09-01 15:01:07 +00:00
|
|
|
query: { f: 'jpg', w: '506', h: '253' },
|
2022-09-01 14:58:57 +00:00
|
|
|
},
|
2022-09-01 20:31:53 +00:00
|
|
|
{
|
|
|
|
title: 'Filename with spaces',
|
|
|
|
id: '#spaces',
|
|
|
|
url: '/@astroimage/assets/blog/introducing astro.jpg',
|
|
|
|
query: { f: 'webp', w: '768', h: '414' },
|
|
|
|
},
|
2022-09-01 14:58:57 +00:00
|
|
|
{
|
|
|
|
title: 'Inline imports',
|
|
|
|
id: '#inline',
|
|
|
|
url: '/@astroimage/assets/social.jpg',
|
2022-09-01 15:01:07 +00:00
|
|
|
query: { f: 'jpg', w: '506', h: '253' },
|
2022-09-01 14:58:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#google',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
f: 'webp',
|
|
|
|
w: '544',
|
|
|
|
h: '184',
|
2022-09-01 15:01:07 +00:00
|
|
|
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
|
|
|
|
},
|
2022-09-01 14:58:57 +00:00
|
|
|
},
|
2022-09-07 13:56:07 +00:00
|
|
|
{
|
|
|
|
title: 'Remote without file extension',
|
|
|
|
id: '#ipsum',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
w: '200',
|
|
|
|
h: '300',
|
2022-09-07 13:57:52 +00:00
|
|
|
href: 'https://picsum.photos/200/300',
|
|
|
|
},
|
2022-09-07 13:56:07 +00:00
|
|
|
},
|
2022-09-01 14:58:57 +00:00
|
|
|
{
|
|
|
|
title: 'Public images',
|
|
|
|
id: '#hero',
|
|
|
|
url: '/_image',
|
2022-09-01 15:01:07 +00:00
|
|
|
query: { f: 'webp', w: '768', h: '414', href: '/hero.jpg' },
|
|
|
|
},
|
2022-09-07 17:22:11 +00:00
|
|
|
{
|
|
|
|
title: 'Background color',
|
|
|
|
id: '#bg-color',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
f: 'jpeg',
|
|
|
|
w: '544',
|
|
|
|
h: '184',
|
|
|
|
bg: '#333333',
|
|
|
|
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
|
|
|
|
},
|
|
|
|
},
|
2022-09-01 14:58:57 +00:00
|
|
|
].forEach(({ title, id, url, query }) => {
|
|
|
|
it(title, () => {
|
|
|
|
const image = $(id);
|
2022-07-01 15:47:48 +00:00
|
|
|
|
2022-08-30 21:09:44 +00:00
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
2022-07-01 15:47:48 +00:00
|
|
|
|
2022-09-01 14:58:57 +00:00
|
|
|
expect(route).to.equal(url);
|
2022-07-01 15:47:48 +00:00
|
|
|
|
2022-08-30 21:09:44 +00:00
|
|
|
const searchParams = new URLSearchParams(params);
|
2022-07-01 15:47:48 +00:00
|
|
|
|
2022-09-01 14:58:57 +00:00
|
|
|
for (const [key, value] of Object.entries(query)) {
|
|
|
|
expect(searchParams.get(key)).to.equal(value);
|
|
|
|
}
|
2022-08-30 21:09:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('SSG images with subpath - dev', function () {
|
|
|
|
let fixture;
|
|
|
|
let devServer;
|
|
|
|
let $;
|
2022-08-30 21:12:45 +00:00
|
|
|
|
2022-08-30 21:09:44 +00:00
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({ root: './fixtures/basic-image/', base: '/docs' });
|
|
|
|
devServer = await fixture.startDevServer();
|
2022-08-30 21:12:45 +00:00
|
|
|
const html = await fixture.fetch('/docs/').then((res) => res.text());
|
|
|
|
$ = cheerio.load(html);
|
2022-08-30 21:09:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await devServer.stop();
|
|
|
|
});
|
|
|
|
|
2022-09-01 14:58:57 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Local images',
|
|
|
|
id: '#social-jpg',
|
|
|
|
url: '/@astroimage/assets/social.jpg',
|
2022-09-01 15:01:07 +00:00
|
|
|
query: { f: 'jpg', w: '506', h: '253' },
|
2022-09-01 14:58:57 +00:00
|
|
|
},
|
2022-09-01 20:31:53 +00:00
|
|
|
{
|
|
|
|
title: 'Filename with spaces',
|
|
|
|
id: '#spaces',
|
|
|
|
url: '/@astroimage/assets/blog/introducing astro.jpg',
|
|
|
|
query: { f: 'webp', w: '768', h: '414' },
|
|
|
|
},
|
2022-09-01 14:58:57 +00:00
|
|
|
{
|
|
|
|
title: 'Inline imports',
|
|
|
|
id: '#inline',
|
|
|
|
url: '/@astroimage/assets/social.jpg',
|
2022-09-01 15:01:07 +00:00
|
|
|
query: { f: 'jpg', w: '506', h: '253' },
|
2022-09-01 14:58:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#google',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
f: 'webp',
|
|
|
|
w: '544',
|
|
|
|
h: '184',
|
2022-09-01 15:01:07 +00:00
|
|
|
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
|
|
|
|
},
|
2022-09-01 14:58:57 +00:00
|
|
|
},
|
2022-09-07 13:56:07 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
title: 'Remote without file extension',
|
|
|
|
id: '#ipsum',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
w: '200',
|
|
|
|
h: '300',
|
2022-09-07 13:57:52 +00:00
|
|
|
href: 'https://picsum.photos/200/300',
|
|
|
|
},
|
2022-09-07 13:56:07 +00:00
|
|
|
},
|
2022-09-01 14:58:57 +00:00
|
|
|
{
|
|
|
|
title: 'Public images',
|
|
|
|
id: '#hero',
|
|
|
|
url: '/_image',
|
2022-09-01 15:01:07 +00:00
|
|
|
query: { f: 'webp', w: '768', h: '414', href: '/hero.jpg' },
|
|
|
|
},
|
2022-09-07 17:22:11 +00:00
|
|
|
{
|
|
|
|
title: 'Background color',
|
|
|
|
id: '#bg-color',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
f: 'jpeg',
|
|
|
|
w: '544',
|
|
|
|
h: '184',
|
|
|
|
bg: '#333333',
|
|
|
|
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
|
|
|
|
},
|
|
|
|
},
|
2022-09-01 14:58:57 +00:00
|
|
|
].forEach(({ title, id, url, query }) => {
|
|
|
|
it(title, () => {
|
|
|
|
const image = $(id);
|
2022-07-01 15:47:48 +00:00
|
|
|
|
2022-08-30 21:09:44 +00:00
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
2022-07-01 15:47:48 +00:00
|
|
|
|
2022-09-01 14:58:57 +00:00
|
|
|
expect(route).to.equal(url);
|
2022-07-01 15:47:48 +00:00
|
|
|
|
2022-08-30 21:09:44 +00:00
|
|
|
const searchParams = new URLSearchParams(params);
|
2022-07-01 15:47:48 +00:00
|
|
|
|
2022-09-01 14:58:57 +00:00
|
|
|
for (const [key, value] of Object.entries(query)) {
|
|
|
|
expect(searchParams.get(key)).to.equal(value);
|
|
|
|
}
|
2022-08-30 21:09:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-09-01 14:58:57 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Local images',
|
|
|
|
id: '#social-jpg',
|
2022-09-08 18:41:17 +00:00
|
|
|
regex: /^\/assets\/social.\w{8}_\w{4,10}.jpg/,
|
2022-09-01 15:01:07 +00:00
|
|
|
size: { width: 506, height: 253, type: 'jpg' },
|
2022-09-01 14:58:57 +00:00
|
|
|
},
|
2022-09-01 20:31:53 +00:00
|
|
|
{
|
|
|
|
title: 'Filename with spaces',
|
|
|
|
id: '#spaces',
|
2022-09-08 18:41:17 +00:00
|
|
|
regex: /^\/assets\/introducing astro.\w{8}_\w{4,10}.webp/,
|
2022-09-01 20:31:53 +00:00
|
|
|
size: { width: 768, height: 414, type: 'webp' },
|
|
|
|
},
|
2022-09-01 14:58:57 +00:00
|
|
|
{
|
|
|
|
title: 'Inline imports',
|
|
|
|
id: '#inline',
|
2022-09-08 18:41:17 +00:00
|
|
|
regex: /^\/assets\/social.\w{8}_\w{4,10}.jpg/,
|
2022-09-01 15:01:07 +00:00
|
|
|
size: { width: 506, height: 253, type: 'jpg' },
|
2022-09-01 14:58:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#google',
|
2022-09-08 18:41:17 +00:00
|
|
|
regex: /^\/assets\/googlelogo_color_272x92dp_\w{4,10}.webp/,
|
2022-09-01 15:01:07 +00:00
|
|
|
size: { width: 544, height: 184, type: 'webp' },
|
2022-09-01 14:58:57 +00:00
|
|
|
},
|
2022-09-07 13:56:07 +00:00
|
|
|
{
|
|
|
|
title: 'Remote without file extension',
|
|
|
|
id: '#ipsum',
|
2022-09-08 18:41:17 +00:00
|
|
|
regex: /^\/assets\/300_\w{4,10}/,
|
2022-09-07 13:56:07 +00:00
|
|
|
size: { width: 200, height: 300, type: 'jpg' },
|
|
|
|
},
|
2022-09-01 14:58:57 +00:00
|
|
|
{
|
|
|
|
title: 'Public images',
|
|
|
|
id: '#hero',
|
2022-09-08 18:41:17 +00:00
|
|
|
regex: /^\/assets\/hero_\w{4,10}.webp/,
|
2022-09-01 15:01:07 +00:00
|
|
|
size: { width: 768, height: 414, type: 'webp' },
|
|
|
|
},
|
2022-09-07 17:22:11 +00:00
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#bg-color',
|
2022-09-08 18:41:17 +00:00
|
|
|
regex: /^\/assets\/googlelogo_color_272x92dp_\w{4,10}.jpeg/,
|
2022-09-07 17:22:11 +00:00
|
|
|
size: { width: 544, height: 184, type: 'jpg' },
|
|
|
|
},
|
2022-09-01 14:58:57 +00:00
|
|
|
].forEach(({ title, id, regex, size }) => {
|
|
|
|
it(title, () => {
|
|
|
|
const image = $(id);
|
|
|
|
|
|
|
|
expect(image.attr('src')).to.match(regex);
|
|
|
|
expect(image.attr('width')).to.equal(size.width.toString());
|
|
|
|
expect(image.attr('height')).to.equal(size.height.toString());
|
2022-09-01 15:01:07 +00:00
|
|
|
|
2022-09-01 14:58:57 +00:00
|
|
|
verifyImage(image.attr('src'), size);
|
2022-09-01 15:01:07 +00:00
|
|
|
});
|
|
|
|
});
|
2022-08-30 21:09:44 +00:00
|
|
|
});
|
2022-07-01 15:47:48 +00:00
|
|
|
|
2022-08-30 21:09:44 +00:00
|
|
|
describe('SSG images with subpath - build', function () {
|
|
|
|
let fixture;
|
|
|
|
let $;
|
|
|
|
let html;
|
2022-07-01 15:47:48 +00:00
|
|
|
|
2022-08-30 21:09:44 +00:00
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({ root: './fixtures/basic-image/', base: '/docs' });
|
|
|
|
await fixture.build();
|
2022-08-22 19:45:34 +00:00
|
|
|
|
2022-08-30 21:09:44 +00:00
|
|
|
html = await fixture.readFile('/index.html');
|
|
|
|
$ = cheerio.load(html);
|
|
|
|
});
|
2022-08-22 19:45:34 +00:00
|
|
|
|
2022-08-30 21:09:44 +00: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);
|
|
|
|
}
|
|
|
|
|
2022-09-01 14:58:57 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Local images',
|
|
|
|
id: '#social-jpg',
|
2022-09-08 18:41:17 +00:00
|
|
|
regex: /^\/docs\/assets\/social.\w{8}_\w{4,10}.jpg/,
|
2022-09-01 15:01:07 +00:00
|
|
|
size: { width: 506, height: 253, type: 'jpg' },
|
2022-09-01 14:58:57 +00:00
|
|
|
},
|
2022-09-01 20:31:53 +00:00
|
|
|
{
|
|
|
|
title: 'Filename with spaces',
|
|
|
|
id: '#spaces',
|
2022-09-08 18:41:17 +00:00
|
|
|
regex: /^\/docs\/assets\/introducing astro.\w{8}_\w{4,10}.webp/,
|
2022-09-01 20:31:53 +00:00
|
|
|
size: { width: 768, height: 414, type: 'webp' },
|
|
|
|
},
|
2022-09-01 14:58:57 +00:00
|
|
|
{
|
|
|
|
title: 'Inline imports',
|
|
|
|
id: '#inline',
|
2022-09-08 18:41:17 +00:00
|
|
|
regex: /^\/docs\/assets\/social.\w{8}_\w{4,10}.jpg/,
|
2022-09-01 15:01:07 +00:00
|
|
|
size: { width: 506, height: 253, type: 'jpg' },
|
2022-09-01 14:58:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#google',
|
2022-09-08 18:41:17 +00:00
|
|
|
regex: /^\/docs\/assets\/googlelogo_color_272x92dp_\w{4,10}.webp/,
|
2022-09-01 15:01:07 +00:00
|
|
|
size: { width: 544, height: 184, type: 'webp' },
|
2022-09-01 14:58:57 +00:00
|
|
|
},
|
2022-09-07 13:56:07 +00:00
|
|
|
{
|
|
|
|
title: 'Remote without file extension',
|
|
|
|
id: '#ipsum',
|
2022-09-08 18:41:17 +00:00
|
|
|
regex: /^\/docs\/assets\/300_\w{4,10}/,
|
2022-09-07 13:56:07 +00:00
|
|
|
size: { width: 200, height: 300, type: 'jpg' },
|
|
|
|
},
|
2022-09-01 14:58:57 +00:00
|
|
|
{
|
|
|
|
title: 'Public images',
|
|
|
|
id: '#hero',
|
2022-09-08 18:41:17 +00:00
|
|
|
regex: /^\/docs\/assets\/hero_\w{4,10}.webp/,
|
2022-09-01 15:01:07 +00:00
|
|
|
size: { width: 768, height: 414, type: 'webp' },
|
|
|
|
},
|
2022-09-07 17:22:11 +00:00
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#bg-color',
|
2022-09-08 18:41:17 +00:00
|
|
|
regex: /^\/docs\/assets\/googlelogo_color_272x92dp_\w{4,10}.jpeg/,
|
2022-09-07 17:22:11 +00:00
|
|
|
size: { width: 544, height: 184, type: 'jpg' },
|
|
|
|
},
|
2022-09-01 14:58:57 +00:00
|
|
|
].forEach(({ title, id, regex, size }) => {
|
|
|
|
it(title, () => {
|
|
|
|
const image = $(id);
|
|
|
|
|
|
|
|
expect(image.attr('src')).to.match(regex);
|
|
|
|
expect(image.attr('width')).to.equal(size.width.toString());
|
|
|
|
expect(image.attr('height')).to.equal(size.height.toString());
|
2022-09-01 15:01:07 +00:00
|
|
|
|
2022-09-01 14:58:57 +00:00
|
|
|
verifyImage(image.attr('src').replace('/docs', ''), size);
|
2022-09-01 15:01:07 +00:00
|
|
|
});
|
2022-08-30 21:09:44 +00:00
|
|
|
});
|
2022-07-01 15:47:48 +00:00
|
|
|
});
|