test: coverage campaign and mutation testing across five packages (#628)

Coverage 83.6 to 87.36% lines, 81.63 to 84.14% branches. Mutation testing across five packages: image-engine 85, media-engine 92, doc-engine 87, shared+enterprise 86, apps/api security and jobs slice. Runs all five lanes weekly. Fixes the silently-broken mutation CI (babel pin), a redact-pdf envelope-shape test bug, an untested enterprise license valid-signature path, and an audit test that only exercised a hand-copied reproduction. Test and config only, no product code changes beyond the babel pin and one test-only oidc export. Full suite: 16,712 pass, 0 fail.
This commit is contained in:
SnapOtter
2026-07-24 17:36:57 +08:00
committed by GitHub
parent eee6f0d470
commit 301e6eb01a
129 changed files with 30900 additions and 276 deletions
@@ -1,4 +1,7 @@
import { ffmpegAvailable } from "@snapotter/media-engine";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { ffmpegAvailable, probeMedia } from "@snapotter/media-engine";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { fixtures, readFixture } from "../../../fixtures/index.js";
import {
@@ -9,6 +12,11 @@ import {
} from "../../test-server.js";
const MP4 = readFixture(fixtures.video.tiny("mp4"));
// A genuinely compressible clip (480x270, ~30s). The tiny fixture is already so
// small that re-encoding cannot beat MP4 container overhead, so the "output is
// smaller than input" invariant is only meaningful on a source with real
// bitrate slack.
const COMPRESSIBLE_MP4 = readFixture(fixtures.video.hero.mp4);
let testApp: TestApp;
let adminToken: string;
@@ -34,9 +42,9 @@ async function pollJob(jobId: string) {
return row;
}
async function runTool(settings: Record<string, unknown>) {
async function runTool(settings: Record<string, unknown>, content: Buffer = MP4) {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "tiny.mp4", contentType: "video/mp4", content: MP4 },
{ name: "file", filename: "tiny.mp4", contentType: "video/mp4", content },
{ name: "settings", content: JSON.stringify(settings) },
]);
return testApp.app.inject({
@@ -49,12 +57,13 @@ async function runTool(settings: Record<string, unknown>) {
describe.skipIf(!ffmpegAvailable())("compress-video (requires ffmpeg)", () => {
it("returns 202 and produces a compressed mp4", async () => {
const res = await runTool({ quality: "balanced" });
const res = await runTool({ quality: "balanced" }, COMPRESSIBLE_MP4);
expect(res.statusCode).toBe(202);
const { jobId } = JSON.parse(res.body);
const row = await pollJob(jobId);
expect(row?.status).toBe("completed");
const outName = (row?.outputRefs as string[])[0].split("/").pop() as string;
const outputRefs = (row?.outputRefs ?? []) as string[];
const outName = outputRefs[0].split("/").pop() as string;
expect(outName).toContain("_compressed.mp4");
const dl = await testApp.app.inject({
method: "GET",
@@ -62,6 +71,23 @@ describe.skipIf(!ffmpegAvailable())("compress-video (requires ffmpeg)", () => {
});
expect(dl.statusCode).toBe(200);
expect(dl.rawPayload.length).toBeGreaterThan(100);
// The entire point of compress-video is to shrink the file: the output must
// be strictly smaller than the input it was handed. Assert the RELATIVE
// change against the measured input fixture, not a hard-coded byte count.
expect(dl.rawPayload.length).toBeLessThan(COMPRESSIBLE_MP4.length);
// Whatever it shrinks to must still decode as a real video, not a stub or a
// silent audio-only remux.
const dir = mkdtempSync(join(tmpdir(), "compress-video-"));
try {
const outPath = join(dir, outName);
writeFileSync(outPath, dl.rawPayload);
const info = await probeMedia(outPath);
expect(info.streams.some((s) => s.type === "video")).toBe(true);
} finally {
rmSync(dir, { recursive: true, force: true });
}
}, 90_000);
it("compresses with 480p resolution", async () => {
@@ -1,4 +1,7 @@
import { ffmpegAvailable } from "@snapotter/media-engine";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { ffmpegAvailable, probeMedia } from "@snapotter/media-engine";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { fixtures, readFixture } from "../../../fixtures/index.js";
import {
@@ -50,7 +53,8 @@ describe.skipIf(!ffmpegAvailable())("convert-video (requires ffmpeg)", () => {
await new Promise((r) => setTimeout(r, 500));
}
expect(row?.status).toBe("completed");
const outName = (row?.outputRefs as string[])[0].split("/").pop() as string;
const outputRefs = (row?.outputRefs ?? []) as string[];
const outName = outputRefs[0].split("/").pop() as string;
const dl = await testApp.app.inject({
method: "GET",
url: `/api/v1/download/${jobId}/${encodeURIComponent(outName)}`,
@@ -58,6 +62,24 @@ describe.skipIf(!ffmpegAvailable())("convert-video (requires ffmpeg)", () => {
expect(dl.statusCode).toBe(200);
expect(outName.endsWith(".webm")).toBe(true);
expect(dl.rawPayload.length).toBeGreaterThan(100);
// Semantic check: a real mp4 -> webm conversion must re-encode the video to
// a VP-family codec inside a webm container. The input fixture is h264 in an
// mp4 container, so an h264 stream here means the container was renamed
// without transcoding (the tool asks for resolveEncoder("vp9") -> libvpx-vp9).
const dir = mkdtempSync(join(tmpdir(), "convert-webm-"));
try {
const outPath = join(dir, outName);
writeFileSync(outPath, dl.rawPayload);
const info = await probeMedia(outPath);
expect(info.container).toContain("webm");
const video = info.streams.find((s) => s.type === "video");
expect(video).toBeDefined();
expect(video?.codec).toMatch(/^vp[89]$/);
expect(video?.codec).not.toBe("h264");
} finally {
rmSync(dir, { recursive: true, force: true });
}
}, 90_000);
it("converts to mp4 (default settings)", async () => {
@@ -73,8 +95,31 @@ describe.skipIf(!ffmpegAvailable())("convert-video (requires ffmpeg)", () => {
await new Promise((r) => setTimeout(r, 500));
}
expect(row?.status).toBe("completed");
const outName = (row?.outputRefs as string[])[0].split("/").pop() as string;
const outputRefs = (row?.outputRefs ?? []) as string[];
const outName = outputRefs[0].split("/").pop() as string;
expect(outName.endsWith(".mp4")).toBe(true);
const dl = await testApp.app.inject({
method: "GET",
url: `/api/v1/download/${jobId}/${encodeURIComponent(outName)}`,
});
expect(dl.statusCode).toBe(200);
// Semantic check: the mp4 target encodes video with resolveEncoder("h264")
// into the mp4 (mov/mp4/m4a) container family. ffprobe reports the container
// as a comma list ("mov,mp4,m4a,3gp,3g2,mj2"), so match on the mp4 member.
const dir = mkdtempSync(join(tmpdir(), "convert-mp4-"));
try {
const outPath = join(dir, outName);
writeFileSync(outPath, dl.rawPayload);
const info = await probeMedia(outPath);
expect(info.container).toContain("mp4");
const video = info.streams.find((s) => s.type === "video");
expect(video).toBeDefined();
expect(video?.codec).toBe("h264");
} finally {
rmSync(dir, { recursive: true, force: true });
}
}, 90_000);
it("rejects a non-video upload", async () => {
@@ -1,4 +1,7 @@
import { ffmpegAvailable } from "@snapotter/media-engine";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { ffmpegAvailable, probeMedia } from "@snapotter/media-engine";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { fixtures, readFixture } from "../../../fixtures/index.js";
import {
@@ -49,6 +52,22 @@ describe.skipIf(!ffmpegAvailable())("extract-audio (requires ffmpeg)", () => {
expect(dl.rawPayload.length).toBeGreaterThan(100);
const outName = envelope.downloadUrl.split("/").pop() as string;
expect(outName.endsWith(".mp3")).toBe(true);
const tmpDir = mkdtempSync(join(tmpdir(), "extract-audio-mp3-"));
try {
const outPath = join(tmpDir, "out.mp3");
writeFileSync(outPath, dl.rawPayload);
const info = await probeMedia(outPath);
// Extract-audio must strip video entirely and keep exactly one audio track.
expect(info.streams.filter((s) => s.type === "video")).toHaveLength(0);
expect(info.streams.filter((s) => s.type === "audio")).toHaveLength(1);
// libmp3lame in an mp3 container: codec_name "mp3", format_name "mp3".
const audio = info.streams.find((s) => s.type === "audio");
expect(audio?.codec).toBe("mp3");
expect(info.container).toContain("mp3");
} finally {
rmSync(tmpDir, { recursive: true, force: true });
}
}, 60_000);
it("extracts audio from mp4 as wav and returns 200", async () => {
@@ -64,5 +83,21 @@ describe.skipIf(!ffmpegAvailable())("extract-audio (requires ffmpeg)", () => {
expect(dl.rawPayload.length).toBeGreaterThan(100);
const outName = envelope.downloadUrl.split("/").pop() as string;
expect(outName.endsWith(".wav")).toBe(true);
const tmpDir = mkdtempSync(join(tmpdir(), "extract-audio-wav-"));
try {
const outPath = join(tmpDir, "out.wav");
writeFileSync(outPath, dl.rawPayload);
const info = await probeMedia(outPath);
// Extract-audio must strip video entirely and keep exactly one audio track.
expect(info.streams.filter((s) => s.type === "video")).toHaveLength(0);
expect(info.streams.filter((s) => s.type === "audio")).toHaveLength(1);
// pcm_s16le in a wav container.
const audio = info.streams.find((s) => s.type === "audio");
expect(audio?.codec).toBe("pcm_s16le");
expect(info.container).toContain("wav");
} finally {
rmSync(tmpDir, { recursive: true, force: true });
}
}, 60_000);
});
@@ -1,4 +1,7 @@
import { ffmpegAvailable } from "@snapotter/media-engine";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { ffmpegAvailable, probeMedia } from "@snapotter/media-engine";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { fixtures, readFixture } from "../../../fixtures/index.js";
import {
@@ -43,6 +46,20 @@ describe.skipIf(!ffmpegAvailable())("mute-video (requires ffmpeg)", () => {
});
expect(dl.statusCode).toBe(200);
expect(dl.rawPayload.length).toBeGreaterThan(100);
// The whole point of the tool: the audio track is gone while the video
// stream survives. The tiny.mp4 fixture ships with both a video and an
// audio stream, so a genuine mute must drop audio and keep video.
const tmpDir = mkdtempSync(join(tmpdir(), "mute-test-"));
try {
const probeFile = join(tmpDir, "muted.mp4");
writeFileSync(probeFile, dl.rawPayload);
const info = await probeMedia(probeFile);
expect(info.streams.some((s) => s.type === "video")).toBe(true);
expect(info.streams.every((s) => s.type !== "audio")).toBe(true);
} finally {
rmSync(tmpDir, { recursive: true, force: true });
}
}, 60_000);
it("also works with explicit empty settings", async () => {
@@ -1,4 +1,5 @@
import { ffmpegAvailable } from "@snapotter/media-engine";
import sharp from "sharp";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { fixtures, readFixture } from "../../../fixtures/index.js";
import {
@@ -50,7 +51,8 @@ describe.skipIf(!ffmpegAvailable())("video-to-gif (requires ffmpeg)", () => {
const { jobId } = JSON.parse(res.body);
const row = await pollJob(jobId);
expect(row?.status).toBe("completed");
const outName = (row?.outputRefs as string[])[0].split("/").pop() as string;
const outputRefs = (row?.outputRefs ?? []) as string[];
const outName = outputRefs[0].split("/").pop() as string;
expect(outName.endsWith(".gif")).toBe(true);
const dl = await testApp.app.inject({
method: "GET",
@@ -60,5 +62,10 @@ describe.skipIf(!ffmpegAvailable())("video-to-gif (requires ffmpeg)", () => {
// GIF files start with GIF8 magic bytes
const magic = dl.rawPayload.subarray(0, 4).toString("ascii");
expect(magic).toBe("GIF8");
// Decode the payload and confirm it is an animated GIF at the requested width.
const meta = await sharp(dl.rawPayload, { animated: true }).metadata();
expect(meta.format).toBe("gif");
expect(meta.width).toBe(120);
expect((meta.pages ?? 1) > 1).toBe(true);
}, 90_000);
});
@@ -1,4 +1,5 @@
import { ffmpegAvailable } from "@snapotter/media-engine";
import sharp from "sharp";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { fixtures, readFixture } from "../../../fixtures/index.js";
import {
@@ -37,7 +38,7 @@ async function runTool(settings: Record<string, unknown>) {
describe.skipIf(!ffmpegAvailable())("video-to-webp (requires ffmpeg)", () => {
it("converts video to animated webp with RIFF/WEBP magic", async () => {
const res = await runTool({});
const res = await runTool({ width: 120 });
expect(res.statusCode).toBe(200);
const envelope = JSON.parse(res.body);
expect(envelope.downloadUrl).toBeDefined();
@@ -55,5 +56,12 @@ describe.skipIf(!ffmpegAvailable())("video-to-webp (requires ffmpeg)", () => {
const webp = buf.subarray(8, 12).toString("ascii");
expect(riff).toBe("RIFF");
expect(webp).toBe("WEBP");
// Decode with sharp to confirm it is a real, animated WebP (multiple frames)
const meta = await sharp(dl.rawPayload, { animated: true }).metadata();
expect(meta.format).toBe("webp");
expect(meta.pages).toBeGreaterThan(1);
// Requested width must be honored by the scale filter
expect(meta.width).toBe(120);
}, 60_000);
});