2022-07-01 10:47:48 -05:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import * as cheerio from 'cheerio';
|
|
|
|
import sizeOf from 'image-size';
|
2022-09-29 16:20:48 -05:00
|
|
|
import fs from 'fs/promises';
|
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-08-30 16:12:45 -05:00
|
|
|
|
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();
|
2022-08-30 16:12:45 -05:00
|
|
|
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-09-01 09:58:57 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Local images',
|
|
|
|
id: '#social-jpg',
|
|
|
|
url: '/@astroimage/assets/social.jpg',
|
2022-09-01 10:01:07 -05:00
|
|
|
query: { f: 'jpg', w: '506', h: '253' },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
2022-09-30 12:27:56 -05:00
|
|
|
{
|
|
|
|
title: 'Local image no transforms',
|
|
|
|
id: '#no-transforms',
|
|
|
|
url: '/@astroimage/assets/social.jpg',
|
2022-09-30 12:29:32 -05:00
|
|
|
query: {},
|
2022-09-30 12:27:56 -05:00
|
|
|
},
|
2022-09-01 15:31:53 -05:00
|
|
|
{
|
|
|
|
title: 'Filename with spaces',
|
|
|
|
id: '#spaces',
|
|
|
|
url: '/@astroimage/assets/blog/introducing astro.jpg',
|
|
|
|
query: { f: 'webp', w: '768', h: '414' },
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
{
|
|
|
|
title: 'Inline imports',
|
|
|
|
id: '#inline',
|
|
|
|
url: '/@astroimage/assets/social.jpg',
|
2022-09-01 10:01:07 -05:00
|
|
|
query: { f: 'jpg', w: '506', h: '253' },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#google',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
f: 'webp',
|
|
|
|
w: '544',
|
|
|
|
h: '184',
|
2022-09-01 10:01:07 -05:00
|
|
|
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
2022-09-07 08:56:07 -05:00
|
|
|
{
|
|
|
|
title: 'Remote without file extension',
|
|
|
|
id: '#ipsum',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
w: '200',
|
|
|
|
h: '300',
|
2022-09-22 14:48:14 -05:00
|
|
|
href: 'https://dummyimage.com/200x300',
|
2022-09-07 08:57:52 -05:00
|
|
|
},
|
2022-09-07 08:56:07 -05:00
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
{
|
|
|
|
title: 'Public images',
|
|
|
|
id: '#hero',
|
|
|
|
url: '/_image',
|
2022-09-01 10:01:07 -05:00
|
|
|
query: { f: 'webp', w: '768', h: '414', href: '/hero.jpg' },
|
|
|
|
},
|
2022-09-07 12:22:11 -05: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 09:58:57 -05:00
|
|
|
].forEach(({ title, id, url, query }) => {
|
|
|
|
it(title, () => {
|
|
|
|
const image = $(id);
|
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-09-01 09:58:57 -05:00
|
|
|
expect(route).to.equal(url);
|
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-09-01 09:58:57 -05:00
|
|
|
for (const [key, value] of Object.entries(query)) {
|
|
|
|
expect(searchParams.get(key)).to.equal(value);
|
|
|
|
}
|
2022-08-30 16:09:44 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('SSG images with subpath - dev', function () {
|
|
|
|
let fixture;
|
|
|
|
let devServer;
|
|
|
|
let $;
|
2022-08-30 16:12:45 -05:00
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({ root: './fixtures/basic-image/', base: '/docs' });
|
|
|
|
devServer = await fixture.startDevServer();
|
2022-08-30 16:12:45 -05:00
|
|
|
const html = await fixture.fetch('/docs/').then((res) => res.text());
|
|
|
|
$ = cheerio.load(html);
|
2022-08-30 16:09:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await devServer.stop();
|
|
|
|
});
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Local images',
|
|
|
|
id: '#social-jpg',
|
|
|
|
url: '/@astroimage/assets/social.jpg',
|
2022-09-01 10:01:07 -05:00
|
|
|
query: { f: 'jpg', w: '506', h: '253' },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
2022-09-01 15:31:53 -05:00
|
|
|
{
|
|
|
|
title: 'Filename with spaces',
|
|
|
|
id: '#spaces',
|
|
|
|
url: '/@astroimage/assets/blog/introducing astro.jpg',
|
|
|
|
query: { f: 'webp', w: '768', h: '414' },
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
{
|
|
|
|
title: 'Inline imports',
|
|
|
|
id: '#inline',
|
|
|
|
url: '/@astroimage/assets/social.jpg',
|
2022-09-01 10:01:07 -05:00
|
|
|
query: { f: 'jpg', w: '506', h: '253' },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#google',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
f: 'webp',
|
|
|
|
w: '544',
|
|
|
|
h: '184',
|
2022-09-01 10:01:07 -05:00
|
|
|
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
2022-09-07 08:56:07 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
title: 'Remote without file extension',
|
|
|
|
id: '#ipsum',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
w: '200',
|
|
|
|
h: '300',
|
2022-09-22 14:48:14 -05:00
|
|
|
href: 'https://dummyimage.com/200x300',
|
2022-09-07 08:57:52 -05:00
|
|
|
},
|
2022-09-07 08:56:07 -05:00
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
{
|
|
|
|
title: 'Public images',
|
|
|
|
id: '#hero',
|
|
|
|
url: '/_image',
|
2022-09-01 10:01:07 -05:00
|
|
|
query: { f: 'webp', w: '768', h: '414', href: '/hero.jpg' },
|
|
|
|
},
|
2022-09-07 12:22:11 -05: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 09:58:57 -05:00
|
|
|
].forEach(({ title, id, url, query }) => {
|
|
|
|
it(title, () => {
|
|
|
|
const image = $(id);
|
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-09-01 09:58:57 -05:00
|
|
|
expect(route).to.equal(url);
|
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-09-01 09:58:57 -05:00
|
|
|
for (const [key, value] of Object.entries(query)) {
|
|
|
|
expect(searchParams.get(key)).to.equal(value);
|
|
|
|
}
|
2022-08-30 16:09:44 -05: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 09:58:57 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Local images',
|
|
|
|
id: '#social-jpg',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/assets\/social.\w{8}_\w{4,10}.jpg/,
|
2022-09-01 10:01:07 -05:00
|
|
|
size: { width: 506, height: 253, type: 'jpg' },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
2022-09-01 15:31:53 -05:00
|
|
|
{
|
|
|
|
title: 'Filename with spaces',
|
|
|
|
id: '#spaces',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/assets\/introducing astro.\w{8}_\w{4,10}.webp/,
|
2022-09-01 15:31:53 -05:00
|
|
|
size: { width: 768, height: 414, type: 'webp' },
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
{
|
|
|
|
title: 'Inline imports',
|
|
|
|
id: '#inline',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/assets\/social.\w{8}_\w{4,10}.jpg/,
|
2022-09-01 10:01:07 -05:00
|
|
|
size: { width: 506, height: 253, type: 'jpg' },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#google',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/assets\/googlelogo_color_272x92dp_\w{4,10}.webp/,
|
2022-09-01 10:01:07 -05:00
|
|
|
size: { width: 544, height: 184, type: 'webp' },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
2022-09-07 08:56:07 -05:00
|
|
|
{
|
|
|
|
title: 'Remote without file extension',
|
|
|
|
id: '#ipsum',
|
2022-09-22 14:48:14 -05:00
|
|
|
regex: /^\/assets\/200x300_\w{4,10}/,
|
2022-09-07 08:56:07 -05:00
|
|
|
size: { width: 200, height: 300, type: 'jpg' },
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
{
|
|
|
|
title: 'Public images',
|
|
|
|
id: '#hero',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/assets\/hero_\w{4,10}.webp/,
|
2022-09-01 10:01:07 -05:00
|
|
|
size: { width: 768, height: 414, type: 'webp' },
|
|
|
|
},
|
2022-09-07 12:22:11 -05:00
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#bg-color',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/assets\/googlelogo_color_272x92dp_\w{4,10}.jpeg/,
|
2022-09-07 12:22:11 -05:00
|
|
|
size: { width: 544, height: 184, type: 'jpg' },
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
].forEach(({ title, id, regex, size }) => {
|
2022-09-29 16:20:48 -05:00
|
|
|
it(title, async () => {
|
2022-09-01 09:58:57 -05:00
|
|
|
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 10:01:07 -05:00
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
verifyImage(image.attr('src'), size);
|
2022-09-29 16:20:48 -05:00
|
|
|
|
2022-09-29 16:22:49 -05:00
|
|
|
const url = new URL(
|
|
|
|
'./fixtures/basic-image/node_modules/.astro/image' + image.attr('src'),
|
|
|
|
import.meta.url
|
|
|
|
);
|
2022-09-29 16:20:48 -05:00
|
|
|
expect(await fs.stat(url), 'transformed image was cached').to.not.be.undefined;
|
2022-09-01 10:01:07 -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('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);
|
|
|
|
}
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Local images',
|
|
|
|
id: '#social-jpg',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/docs\/assets\/social.\w{8}_\w{4,10}.jpg/,
|
2022-09-01 10:01:07 -05:00
|
|
|
size: { width: 506, height: 253, type: 'jpg' },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
2022-09-01 15:31:53 -05:00
|
|
|
{
|
|
|
|
title: 'Filename with spaces',
|
|
|
|
id: '#spaces',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/docs\/assets\/introducing astro.\w{8}_\w{4,10}.webp/,
|
2022-09-01 15:31:53 -05:00
|
|
|
size: { width: 768, height: 414, type: 'webp' },
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
{
|
|
|
|
title: 'Inline imports',
|
|
|
|
id: '#inline',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/docs\/assets\/social.\w{8}_\w{4,10}.jpg/,
|
2022-09-01 10:01:07 -05:00
|
|
|
size: { width: 506, height: 253, type: 'jpg' },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#google',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/docs\/assets\/googlelogo_color_272x92dp_\w{4,10}.webp/,
|
2022-09-01 10:01:07 -05:00
|
|
|
size: { width: 544, height: 184, type: 'webp' },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
2022-09-07 08:56:07 -05:00
|
|
|
{
|
|
|
|
title: 'Remote without file extension',
|
|
|
|
id: '#ipsum',
|
2022-09-22 14:48:14 -05:00
|
|
|
regex: /^\/docs\/assets\/200x300_\w{4,10}/,
|
2022-09-07 08:56:07 -05:00
|
|
|
size: { width: 200, height: 300, type: 'jpg' },
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
{
|
|
|
|
title: 'Public images',
|
|
|
|
id: '#hero',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/docs\/assets\/hero_\w{4,10}.webp/,
|
2022-09-01 10:01:07 -05:00
|
|
|
size: { width: 768, height: 414, type: 'webp' },
|
|
|
|
},
|
2022-09-07 12:22:11 -05:00
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#bg-color',
|
2022-09-08 13:41:17 -05:00
|
|
|
regex: /^\/docs\/assets\/googlelogo_color_272x92dp_\w{4,10}.jpeg/,
|
2022-09-07 12:22:11 -05:00
|
|
|
size: { width: 544, height: 184, type: 'jpg' },
|
|
|
|
},
|
2022-09-01 09:58:57 -05: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 10:01:07 -05:00
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
verifyImage(image.attr('src').replace('/docs', ''), size);
|
2022-09-01 10:01:07 -05:00
|
|
|
});
|
2022-08-30 16:09:44 -05:00
|
|
|
});
|
2022-07-01 10:47:48 -05:00
|
|
|
});
|