forked from mtusi2026/mtusi2026lab5
103 lines
3.3 KiB
TypeScript
103 lines
3.3 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
|
import { describe, expect, it } from 'vitest';
|
|
import sharp from 'sharp';
|
|
import { JobTimeoutError } from '../jobs/errors.js';
|
|
import { renderVariants, type RenderedVariant } from './pipelines.js';
|
|
|
|
const sample = readFileSync(
|
|
new URL('../../../tests/fixtures/sample.png', import.meta.url),
|
|
);
|
|
const limits = { maxPixels: 40_000_000, timeoutSeconds: 30 };
|
|
|
|
function byName(
|
|
variants: RenderedVariant[],
|
|
name: 'thumbnail' | 'crop' | 'optimized',
|
|
): RenderedVariant {
|
|
const variant = variants.find((item) => item.name === name);
|
|
if (!variant) throw new Error(`missing ${name}`);
|
|
return variant;
|
|
}
|
|
|
|
describe('renderVariants', () => {
|
|
it('renders the v1 contract geometry and formats', async () => {
|
|
const variants = await renderVariants(sample, 1, limits);
|
|
const thumbnail = byName(variants, 'thumbnail');
|
|
const crop = byName(variants, 'crop');
|
|
const optimized = byName(variants, 'optimized');
|
|
|
|
expect([thumbnail.width, thumbnail.height]).toEqual([256, 160]);
|
|
expect(thumbnail.contentType).toBe('image/jpeg');
|
|
expect((await sharp(thumbnail.data).metadata()).format).toBe('jpeg');
|
|
|
|
expect([crop.width, crop.height]).toEqual([128, 128]);
|
|
expect((await sharp(crop.data).metadata()).format).toBe('jpeg');
|
|
|
|
expect([optimized.width, optimized.height]).toEqual([320, 200]);
|
|
expect(optimized.contentType).toBe('image/webp');
|
|
expect((await sharp(optimized.data).metadata()).format).toBe('webp');
|
|
});
|
|
|
|
it('applies EXIF orientation before fitting', async () => {
|
|
const oriented = await sharp({
|
|
create: {
|
|
width: 300,
|
|
height: 200,
|
|
channels: 3,
|
|
background: { r: 20, g: 80, b: 140 },
|
|
},
|
|
})
|
|
.jpeg()
|
|
.withMetadata({ orientation: 6 })
|
|
.toBuffer();
|
|
const variants = await renderVariants(oriented, 1, limits);
|
|
const thumbnail = byName(variants, 'thumbnail');
|
|
expect([thumbnail.width, thumbnail.height]).toEqual([170, 256]);
|
|
});
|
|
|
|
it('composites transparent pixels onto white', async () => {
|
|
const transparent = await sharp({
|
|
create: {
|
|
width: 10,
|
|
height: 10,
|
|
channels: 4,
|
|
background: { r: 0, g: 0, b: 0, alpha: 0 },
|
|
},
|
|
})
|
|
.png()
|
|
.toBuffer();
|
|
const variants = await renderVariants(transparent, 1, limits);
|
|
const thumbnail = byName(variants, 'thumbnail');
|
|
const { data } = await sharp(thumbnail.data)
|
|
.raw()
|
|
.toBuffer({ resolveWithObject: true });
|
|
expect([...data.subarray(0, 3)]).toEqual([255, 255, 255]);
|
|
});
|
|
|
|
it('is deterministic for the same input and version', async () => {
|
|
const first = await renderVariants(sample, 1, limits);
|
|
const second = await renderVariants(sample, 1, limits);
|
|
for (const name of ['thumbnail', 'crop', 'optimized'] as const)
|
|
expect(byName(first, name).sha256).toBe(
|
|
byName(second, name).sha256,
|
|
);
|
|
});
|
|
|
|
it('honours an aborted job deadline before starting a render', async () => {
|
|
const controller = new AbortController();
|
|
controller.abort(new JobTimeoutError(1));
|
|
await expect(
|
|
renderVariants(sample, 1, {
|
|
...limits,
|
|
signal: controller.signal,
|
|
}),
|
|
).rejects.toBeInstanceOf(JobTimeoutError);
|
|
});
|
|
|
|
it('produces a different v2 thumbnail', async () => {
|
|
const v1 = byName(await renderVariants(sample, 1, limits), 'thumbnail');
|
|
const v2 = byName(await renderVariants(sample, 2, limits), 'thumbnail');
|
|
expect(v1.sha256).not.toBe(v2.sha256);
|
|
expect([v2.width, v2.height]).toEqual([256, 160]);
|
|
});
|
|
});
|