85 lines
4.8 KiB
JavaScript
85 lines
4.8 KiB
JavaScript
// Bundle this test entry with the release esbuild externals, then run it beside
|
|||
|
|
// release/vendor in an isolated Linux container. No real DB/app bootstrap.
|
||
|
|
const assert = require('node:assert/strict');
|
||
|
|
const fs = require('node:fs/promises');
|
||
|
|
const path = require('node:path');
|
||
|
|
const os = require('node:os');
|
||
|
|
const sharp = require('sharp');
|
||
|
|
const request = require('supertest');
|
||
|
|
const { Module } = require('@nestjs/common');
|
||
|
|
const { NestFactory } = require('@nestjs/core');
|
||
|
|
const { ImageVariantService } = require('../dist/modules/admin-uploads/image-variants.service');
|
||
|
|
const { AdminUploadsService } = require('../dist/modules/admin-uploads/admin-uploads.service');
|
||
|
|
const { PublicImagesController } = require('../dist/modules/admin-uploads/public-images.controller');
|
||
|
|
const { PublicUploadsController } = require('../dist/modules/admin-uploads/public-uploads.controller');
|
||
|
|
|
||
|
|
async function main() {
|
||
|
|
assert.equal(process.platform, 'linux');
|
||
|
|
assert.ok(require.resolve('sharp').startsWith('/app/vendor/'), 'Must load the shipped codec, not development dependencies');
|
||
|
|
const input = await fs.readFile(process.argv[2]);
|
||
|
|
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'release-image-'));
|
||
|
|
process.env.UPLOAD_DIR = dir;
|
||
|
|
const variants = new ImageVariantService();
|
||
|
|
let app;
|
||
|
|
let stored;
|
||
|
|
try {
|
||
|
|
const persistence = { uploadedFile: { create: async ({ data }) => data } };
|
||
|
|
stored = await new AdminUploadsService(persistence, variants).store(
|
||
|
|
{ originalname: 'fixture.jpg', mimetype: 'image/jpeg', size: input.length, buffer: input }, 'test', null,
|
||
|
|
);
|
||
|
|
assert.equal(stored.mimeType, 'image/webp');
|
||
|
|
assert.ok(stored.sizeBytes < 500 * 1024);
|
||
|
|
console.log('Stored:', JSON.stringify({ url: stored.url, mimeType: stored.mimeType, bytes: stored.sizeBytes }));
|
||
|
|
await Promise.all([200, 400, 600, 1600].map((w) => variants.resolve(path.basename(stored.url), w)));
|
||
|
|
await fs.writeFile(path.join(dir, 'original.jpg'), input);
|
||
|
|
class ImageTestModule {}
|
||
|
|
Module({ controllers: [PublicImagesController, PublicUploadsController], providers: [{ provide: ImageVariantService, useValue: variants }] })(ImageTestModule);
|
||
|
|
app = await NestFactory.create(ImageTestModule, { logger: false });
|
||
|
|
app.setGlobalPrefix('api');
|
||
|
|
app.useStaticAssets(dir, { prefix: '/uploads/', dotfiles: 'deny' });
|
||
|
|
await app.init();
|
||
|
|
async function check(url, mime) {
|
||
|
|
const res = await request(app.getHttpServer()).get(url).timeout({ deadline: 15000 }).expect(200);
|
||
|
|
assert.ok(res.headers['content-type'].includes(mime));
|
||
|
|
assert.equal(Number(res.headers['content-length']), res.body.length);
|
||
|
|
const meta = await sharp(res.body).metadata();
|
||
|
|
await sharp(res.body).raw().toBuffer();
|
||
|
|
console.log('HTTP:', JSON.stringify({ url, status: res.status, bytes: res.body.length, mime, width: meta.width, height: meta.height }));
|
||
|
|
return { res, meta };
|
||
|
|
}
|
||
|
|
const { meta } = await check(stored.url, 'image/webp');
|
||
|
|
assert.ok(Math.max(meta.width, meta.height) <= 2000);
|
||
|
|
await check(`/api/uploads/${path.basename(stored.url)}`, 'image/webp');
|
||
|
|
for (const w of [200, 400, 600, 1600]) {
|
||
|
|
const url = `/api/img/${path.basename(stored.url)}?w=${w}`;
|
||
|
|
const result = await check(url, 'image/webp');
|
||
|
|
assert.equal(result.meta.width, Math.min(w, meta.width));
|
||
|
|
const head = await request(app.getHttpServer()).head(url).expect(200);
|
||
|
|
assert.equal(Number(head.headers['content-length']), result.res.body.length);
|
||
|
|
}
|
||
|
|
// Previously uploaded large JPEGs must work without uploading them again.
|
||
|
|
// Use a separate name so the no-codec checks below cannot hit this cache.
|
||
|
|
await fs.writeFile(path.join(dir, 'legacy.jpg'), input);
|
||
|
|
for (const w of [200, 400, 600, 1600]) {
|
||
|
|
const { res, meta: legacy } = await check(`/api/img/legacy.jpg?w=${w}`, 'image/webp');
|
||
|
|
assert.equal(legacy.width, w);
|
||
|
|
assert.ok(res.body.length < input.length);
|
||
|
|
assert.ok(res.headers['cache-control'].includes('immutable'));
|
||
|
|
}
|
||
|
|
variants.sharp = null;
|
||
|
|
for (const url of ['/uploads/original.jpg', '/api/uploads/original.jpg', '/api/img/original.jpg?w=1600']) {
|
||
|
|
const { res } = await check(url, 'image/jpeg');
|
||
|
|
assert.ok(res.body.equals(input));
|
||
|
|
}
|
||
|
|
for (const url of ['/api/img/missing.jpg?w=400', '/api/uploads/missing.jpg']) {
|
||
|
|
await request(app.getHttpServer()).get(url).timeout({ deadline: 15000 }).expect(404);
|
||
|
|
}
|
||
|
|
console.log('PASS: isolated Linux release upload, all previews, large-original fallback, Content-Length, HEAD and 404');
|
||
|
|
} finally {
|
||
|
|
if (stored) await Promise.all([200, 400, 600, 1600].map((w) => variants.resolve(path.basename(stored.url), w)));
|
||
|
|
if (app) await app.close();
|
||
|
|
await fs.rm(dir, { recursive: true, force: true, maxRetries: 5 });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
main().catch((err) => { console.error(err); process.exitCode = 1; });
|