2026-06-13 10:19:16 +08:00
|
|
|
/**
|
2026-06-21 00:30:10 +08:00
|
|
|
* Integration tests for the lqip-placeholder tool (/api/v1/tools/image/lqip-placeholder).
|
2026-06-13 10:19:16 +08:00
|
|
|
*
|
2026-06-16 15:16:13 +08:00
|
|
|
* Covers LQIP generation, data URI prefix, strategy variants, output size, and schema validation.
|
2026-06-13 10:19:16 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
2026-06-20 05:07:46 +08:00
|
|
|
import { fixtures, readFixture } from "../../../fixtures/index.js";
|
|
|
|
|
import {
|
|
|
|
|
buildTestApp,
|
|
|
|
|
createMultipartPayload,
|
|
|
|
|
loginAsAdmin,
|
|
|
|
|
type TestApp,
|
|
|
|
|
} from "../../test-server.js";
|
2026-06-13 10:19:16 +08:00
|
|
|
|
2026-06-20 04:20:17 +08:00
|
|
|
const PNG = readFixture(fixtures.image.base.png200);
|
2026-06-13 10:19:16 +08:00
|
|
|
|
|
|
|
|
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", () => {
|
2026-06-16 15:16:13 +08:00
|
|
|
it("generates a placeholder with default settings", async () => {
|
2026-06-13 10:19:16 +08:00
|
|
|
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",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/lqip-placeholder",
|
2026-06-13 10:19:16 +08:00
|
|
|
headers: {
|
|
|
|
|
authorization: `Bearer ${adminToken}`,
|
|
|
|
|
"content-type": contentType,
|
|
|
|
|
},
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
|
const result = JSON.parse(res.body);
|
|
|
|
|
expect(result.downloadUrl).toBeDefined();
|
2026-06-16 15:16:13 +08:00
|
|
|
expect(result.dataUri).toMatch(/^data:image\//);
|
2026-06-13 10:19:16 +08:00
|
|
|
expect(result.width).toBeGreaterThan(0);
|
|
|
|
|
expect(result.height).toBeGreaterThan(0);
|
|
|
|
|
expect(result.bytes).toBeGreaterThan(0);
|
2026-06-16 15:16:13 +08:00
|
|
|
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",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/lqip-placeholder",
|
2026-06-16 15:16:13 +08:00
|
|
|
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");
|
2026-06-13 10:19:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/lqip-placeholder",
|
2026-06-13 10:19:16 +08:00
|
|
|
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",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/lqip-placeholder",
|
2026-06-13 10:19:16 +08:00
|
|
|
headers: {
|
|
|
|
|
authorization: `Bearer ${adminToken}`,
|
|
|
|
|
"content-type": contentType,
|
|
|
|
|
},
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
|
|
|
});
|
|
|
|
|
});
|