Files
SnapOtter/tests/integration/lqip-placeholder.test.ts
T

126 lines
3.8 KiB
TypeScript
Raw Normal View History

/**
* Integration tests for the lqip-placeholder tool (/api/v1/tools/lqip-placeholder).
*
* Covers LQIP generation, data URI prefix, strategy variants, output size, and schema validation.
*/
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { buildTestApp, createMultipartPayload, loginAsAdmin, type TestApp } from "./test-server.js";
const FIXTURES = join(__dirname, "..", "fixtures");
const PNG = readFileSync(join(FIXTURES, "test-200x150.png"));
let testApp: TestApp;
let app: TestApp["app"];
let adminToken: string;
beforeAll(async () => {
testApp = await buildTestApp();
app = testApp.app;
adminToken = await loginAsAdmin(app);
}, 30_000);
afterAll(async () => {
await testApp.cleanup();
}, 10_000);
describe("LQIP Placeholder", () => {
it("generates a placeholder with default settings", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
{ name: "settings", content: JSON.stringify({}) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/lqip-placeholder",
headers: {
authorization: `Bearer ${adminToken}`,
"content-type": contentType,
},
body,
});
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
expect(result.downloadUrl).toBeDefined();
expect(result.dataUri).toMatch(/^data:image\//);
expect(result.width).toBeGreaterThan(0);
expect(result.height).toBeGreaterThan(0);
expect(result.bytes).toBeGreaterThan(0);
expect(result.strategy).toBe("blur");
expect(result.html).toContain("<img");
expect(result.css).toContain("background-image");
});
it("generates a solid-color placeholder", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
{ name: "settings", content: JSON.stringify({ strategy: "solid" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/lqip-placeholder",
headers: {
authorization: `Bearer ${adminToken}`,
"content-type": contentType,
},
body,
});
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
expect(result.dataUri).toMatch(/^data:image\//);
expect(result.strategy).toBe("solid");
});
it("produces output under 5 KiB", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
{ name: "settings", content: JSON.stringify({ width: 16, blur: 2 }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/lqip-placeholder",
headers: {
authorization: `Bearer ${adminToken}`,
"content-type": contentType,
},
body,
});
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
const dlRes = await app.inject({
method: "GET",
url: result.downloadUrl,
headers: { authorization: `Bearer ${adminToken}` },
});
expect(dlRes.rawPayload.length).toBeLessThan(5 * 1024);
});
it("rejects width above maximum (200)", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
{ name: "settings", content: JSON.stringify({ width: 200 }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/lqip-placeholder",
headers: {
authorization: `Bearer ${adminToken}`,
"content-type": contentType,
},
body,
});
expect(res.statusCode).toBe(400);
});
});