Files
SnapOtter/tests/integration/tools/data/chart-maker.test.ts
T
SnapOtterandGitHub d10d0f544f fix: release QA hardening across processing, media, security, and CI gates (#649)
A release-readiness QA pass over the whole product. The commits split into
defects a user would hit and gates that were reporting green while measuring
nothing.

## Fixes that change behaviour

Rate limiting was bypassable on every install: TRUST_PROXY defaulted to true, so
request.ip came from a client-set header and a forged X-Forwarded-For got past
the login limiter. The default is now a private-network trust list.

A transient Postgres outage stranded in-flight jobs, leaving finished output on
disk with no row pointing at it. A reconciler now resolves those rows and adopts
the bytes rather than dropping the work.

A Redis connection that moved to a new address wedged every read-blocked
consumer, so completions stopped signalling while health still answered 200.
Socket timeouts plus subscriber pings recover it.

Installing more than one AI bundle left the shared venv multi-versioned and
silently broke three tools. The installer now reconciles distributions to one
version each.

Converting an image to JXL at quality 1 through 4 returned a 500, because
libjxl 0.7 rejects the distance those values compute. The quality is floored at
what the encoder honours. A missing ffmpeg was also reported to the user as a
corrupt upload; it now says the engine is unavailable.

RAW uploads reached an unpatched LibRaw on arm64, so it is built from source at
0.22.2, and the release scan was split so it can fail on an unfixed critical
instead of hiding it behind ignore-unfixed.

## Gates that could not fail

Two mutation lanes ran zero mutants because Stryker crawled the gitignored docs
build; coverage discarded its whole report on any failing test; the lint gate
skipped root tests, scripts, and two workspaces; and several generated matrices
counted a host missing ffmpeg as a passing tool. Each now measures what it
claims.

Full evidence and the outstanding release items are tracked locally and are not
part of this branch.
2026-07-27 15:37:30 +08:00

283 lines
8.4 KiB
TypeScript

/**
* Integration tests for the chart-maker tool (/api/v1/tools/files/chart-maker).
*
* Factory FILE tool that consumes CSV/JSON and renders hand-rolled SVG
* rasterized to PNG via sharp. Tests cover bar/line/pie kinds, CSV and JSON
* inputs, numeric validation, SVG label escaping, and invalid kind rejection.
*/
import sharp from "sharp";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import {
buildTestApp,
createMultipartPayload,
loginAsAdmin,
type TestApp,
} from "../../test-server.js";
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 makeCsv(rows: [string, number][]): Buffer {
const lines = ["label,value", ...rows.map(([l, v]) => `${l},${v}`)];
return Buffer.from(lines.join("\n"), "utf8");
}
describe("Chart Maker", () => {
it("generates a bar chart from a 4-row CSV", async () => {
const csv = makeCsv([
["Apples", 10],
["Bananas", 25],
["Cherries", 15],
["Dates", 30],
]);
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "data.csv", contentType: "text/csv", content: csv },
{ name: "settings", content: JSON.stringify({ kind: "bar" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/files/chart-maker",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
expect(result.downloadUrl).toBeDefined();
// Download and verify PNG
const dlRes = await app.inject({ method: "GET", url: result.downloadUrl });
expect(dlRes.statusCode).toBe(200);
const meta = await sharp(dlRes.rawPayload).metadata();
expect(meta.format).toBe("png");
expect(meta.width).toBeGreaterThan(0);
});
it("generates a chart from JSON object input", async () => {
const jsonData = JSON.stringify({ Apples: 10, Bananas: 25, Cherries: 15 });
const { body, contentType } = createMultipartPayload([
{
name: "file",
filename: "data.json",
contentType: "application/json",
content: Buffer.from(jsonData),
},
{ name: "settings", content: JSON.stringify({ kind: "bar" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/files/chart-maker",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
expect(result.downloadUrl).toBeDefined();
});
it("generates a pie chart", async () => {
const csv = makeCsv([
["Red", 40],
["Blue", 30],
["Green", 30],
]);
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "data.csv", contentType: "text/csv", content: csv },
{ name: "settings", content: JSON.stringify({ kind: "pie" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/files/chart-maker",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
});
it("generates a line chart", async () => {
const csv = makeCsv([
["Jan", 10],
["Feb", 20],
["Mar", 15],
]);
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "data.csv", contentType: "text/csv", content: csv },
{ name: "settings", content: JSON.stringify({ kind: "line" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/files/chart-maker",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
});
it("rejects CSV with non-numeric column 2", async () => {
const csv = Buffer.from("label,value\nApples,lots\nBananas,many\n", "utf8");
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "bad.csv", contentType: "text/csv", content: csv },
{ name: "settings", content: JSON.stringify({ kind: "bar" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/files/chart-maker",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(422);
const result = JSON.parse(res.body);
expect(`${result.error} ${result.details ?? ""}`).toMatch(/numeric/i);
});
it("escapes SVG-injection labels and produces valid PNG", async () => {
const csv = makeCsv([["<script>alert(1)</script>", 10]]);
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "xss.csv", contentType: "text/csv", content: csv },
{ name: "settings", content: JSON.stringify({ kind: "bar" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/files/chart-maker",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
// Download and verify it decodes as PNG (the escape proof: no SVG parse error)
const dlRes = await app.inject({ method: "GET", url: result.downloadUrl });
const meta = await sharp(dlRes.rawPayload).metadata();
expect(meta.format).toBe("png");
});
it("rejects invalid chart kind", async () => {
const csv = makeCsv([["A", 1]]);
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "data.csv", contentType: "text/csv", content: csv },
{ name: "settings", content: JSON.stringify({ kind: "donut" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/files/chart-maker",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(400);
});
it("uses default kind bar when no kind specified", async () => {
const csv = makeCsv([
["A", 10],
["B", 20],
]);
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "data.csv", contentType: "text/csv", content: csv },
{ name: "settings", content: JSON.stringify({}) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/files/chart-maker",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
});
it("generates a chart from JSON array input", async () => {
const jsonData = JSON.stringify([
{ label: "A", value: 10 },
{ label: "B", value: 20 },
{ label: "C", value: 30 },
]);
const { body, contentType } = createMultipartPayload([
{
name: "file",
filename: "data.json",
contentType: "application/json",
content: Buffer.from(jsonData),
},
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/files/chart-maker",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
});
it("respects custom title", async () => {
const csv = makeCsv([["A", 10]]);
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "data.csv", contentType: "text/csv", content: csv },
{ name: "settings", content: JSON.stringify({ title: "Sales Report" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/files/chart-maker",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
});
it("rejects unauthenticated requests", async () => {
const csv = makeCsv([["A", 10]]);
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "data.csv", contentType: "text/csv", content: csv },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/files/chart-maker",
headers: { "content-type": contentType },
body,
});
expect(res.statusCode).toBe(401);
});
});