Files
SnapOtter/tests/integration/color-palette.test.ts
T
ashim-hq babca4cf97 test: comprehensive test coverage expansion (+965 tests)
Add 42 new test files covering all untested tool routes, image engine
internals, AI sidecar bridge, Zustand stores, and cross-format
compatibility. Expand e2e-docker suite with 7 spec files covering all
48 tools against a real Docker container.

Unit tests:
- Image engine: format detection, MIME mapping, metadata parsing, pipeline
- AI bridge: sidecar lifecycle, all 11 tool functions (mocked)
- Web stores: 14 Zustand stores (collage, settings, features, analytics, etc.)
- API helpers: format decoders, page range, file validation

Integration tests:
- 25 tool routes that had zero dedicated tests
- Cross-format matrix: 17 input formats x 3 tools
- Edge cases: zero-byte files, corrupted headers, path traversal, XSS, SQL injection
- Concurrent request handling and pipeline edge cases

E2E-Docker (Playwright against real container):
- 7 spec files: essential, adjustment, conversion, creative, utility, AI, pipeline
- Custom buildMultipart helper for multi-file tool uploads
- AI tools gracefully skip when sidecar not installed

Fixtures:
- Organized test media: formats/ (18 formats) + content/ (17 content types)
- Reduced from 3.1 GB unorganized samples to 33 MB structured fixtures

Bug fix:
- color-adjustments: gamma exposure used invalid single-param gamma() for
  positive values; fixed to use two-param gamma(gammaIn, gammaOut) form
2026-04-23 17:12:02 +08:00

176 lines
6.1 KiB
TypeScript

/**
* Integration tests for the color-palette tool.
*
* This tool extracts dominant colors from an image and returns JSON
* (not an image). Tests verify response shape, color count, and format handling.
*/
import { readFileSync } from "node:fs";
import { join } from "node:path";
import sharp from "sharp";
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"));
const JPG = readFileSync(join(FIXTURES, "test-100x100.jpg"));
const WEBP = readFileSync(join(FIXTURES, "test-50x50.webp"));
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);
function makeFilePayload(buffer: Buffer, filename: string, contentType: string) {
return createMultipartPayload([{ name: "file", filename, contentType, content: buffer }]);
}
// ── Basic extraction ──────────────────────────────────────────────
describe("Color extraction", () => {
it("extracts palette from PNG and returns colors array", async () => {
const { body: payload, contentType } = makeFilePayload(PNG, "test.png", "image/png");
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/color-palette",
payload,
headers: {
"content-type": contentType,
authorization: `Bearer ${adminToken}`,
},
});
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
expect(result.colors).toBeDefined();
expect(Array.isArray(result.colors)).toBe(true);
expect(result.colors.length).toBeGreaterThan(0);
expect(result.colors.length).toBeLessThanOrEqual(8);
expect(result.count).toBe(result.colors.length);
expect(result.filename).toBeDefined();
});
it("returns hex color strings in #RRGGBB format", async () => {
const { body: payload, contentType } = makeFilePayload(PNG, "test.png", "image/png");
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/color-palette",
payload,
headers: {
"content-type": contentType,
authorization: `Bearer ${adminToken}`,
},
});
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
for (const color of result.colors) {
expect(color).toMatch(/^#[0-9a-f]{6}$/);
}
});
});
// ── Solid color image ─────────────────────────────────────────────
describe("Solid color image", () => {
it("returns a single dominant color for a solid red image", async () => {
const redBuffer = await sharp({
create: { width: 50, height: 50, channels: 3, background: { r: 255, g: 0, b: 0 } },
})
.png()
.toBuffer();
const { body: payload, contentType } = makeFilePayload(redBuffer, "red.png", "image/png");
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/color-palette",
payload,
headers: {
"content-type": contentType,
authorization: `Bearer ${adminToken}`,
},
});
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
expect(result.colors.length).toBe(1);
// The quantized red should be close to #f00000 or #ff0000
expect(result.colors[0]).toMatch(/^#[ef][0f]0000$/);
});
});
// ── Format support ────────────────────────────────────────────────
describe("Multiple input formats", () => {
it("extracts palette from JPEG", async () => {
const { body: payload, contentType } = makeFilePayload(JPG, "test.jpg", "image/jpeg");
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/color-palette",
payload,
headers: {
"content-type": contentType,
authorization: `Bearer ${adminToken}`,
},
});
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
expect(result.colors.length).toBeGreaterThan(0);
});
it("extracts palette from WebP", async () => {
const { body: payload, contentType } = makeFilePayload(WEBP, "test.webp", "image/webp");
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/color-palette",
payload,
headers: {
"content-type": contentType,
authorization: `Bearer ${adminToken}`,
},
});
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
expect(result.colors.length).toBeGreaterThan(0);
});
});
// ── Error handling ────────────────────────────────────────────────
describe("Error handling", () => {
it("returns 400 when no file is provided", async () => {
const { body: payload, contentType } = createMultipartPayload([
{ name: "other", content: "nothing" },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/color-palette",
payload,
headers: {
"content-type": contentType,
authorization: `Bearer ${adminToken}`,
},
});
expect(res.statusCode).toBe(400);
const result = JSON.parse(res.body);
expect(result.error).toBeDefined();
});
it("returns 422 for corrupted image data", async () => {
const badBuffer = Buffer.from("not an image at all");
const { body: payload, contentType } = makeFilePayload(badBuffer, "bad.png", "image/png");
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/color-palette",
payload,
headers: {
"content-type": contentType,
authorization: `Bearer ${adminToken}`,
},
});
expect(res.statusCode).toBe(422);
});
});