Files
SnapOtter/tests/integration/tools/audio/audio-channels.test.ts
T
SnapOtter 5ffa1d55ea Merge branch 'worktree-test+suite-overhaul-and-real-fixtures' into chore/consolidate-v2.0.0
# Conflicts:
#	tests/integration/generated/settings-matrix.test.ts
#	tests/integration/platform/api.test.ts
#	tests/integration/platform/concurrent.test.ts
#	tests/integration/platform/factory-multi-input.test.ts
#	tests/integration/security/adversarial-comprehensive.test.ts
#	tests/integration/security/adversarial-coverage-gaps.test.ts
#	tests/integration/security/adversarial-extended.test.ts
#	tests/integration/security/adversarial-final-gaps.test.ts
#	tests/integration/security/adversarial-matrix.test.ts
#	tests/integration/security/adversarial-security.test.ts
#	tests/integration/security/adversarial.test.ts
#	tests/integration/tools/image/color-adjustments.test.ts
2026-06-21 02:18:53 +08:00

93 lines
3.2 KiB
TypeScript

import { mkdtempSync, 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 {
buildTestApp,
createMultipartPayload,
loginAsAdmin,
type TestApp,
} from "../../test-server.js";
const STEREO = readFixture(fixtures.audio.stereo);
const MONO_MP3 = readFixture(fixtures.audio.tiny("mp3"));
let testApp: TestApp;
let adminToken: string;
beforeAll(async () => {
testApp = await buildTestApp();
adminToken = await loginAsAdmin(testApp.app);
}, 30_000);
afterAll(async () => {
await testApp.cleanup();
}, 10_000);
function postTool(
filename: string,
contentTypeHeader: string,
fileContent: Buffer,
settings: Record<string, unknown>,
) {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename, contentType: contentTypeHeader, content: fileContent },
{ name: "settings", content: JSON.stringify(settings) },
]);
return testApp.app.inject({
method: "POST",
url: "/api/v1/tools/audio/audio-channels",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
}
describe.skipIf(!ffmpegAvailable())("audio-channels (requires ffmpeg)", () => {
it("stereo-to-mono on stereo file produces mono output", async () => {
const res = await postTool("tone-stereo.wav", "audio/wav", STEREO, {
mode: "stereo-to-mono",
});
expect(res.statusCode).toBe(200);
const envelope = JSON.parse(res.body);
const dl = await testApp.app.inject({ method: "GET", url: envelope.downloadUrl });
expect(dl.statusCode).toBe(200);
const tmpDir = mkdtempSync(join(tmpdir(), "ch-test-"));
const probeFile = join(tmpDir, "mono.wav");
writeFileSync(probeFile, dl.rawPayload);
const info = await probeMedia(probeFile);
const audio = info.streams.find((s) => s.type === "audio");
expect(audio?.channels).toBe(1);
}, 60_000);
it("swap on stereo file returns 200 with 2 channels", async () => {
const res = await postTool("tone-stereo.wav", "audio/wav", STEREO, { mode: "swap" });
expect(res.statusCode).toBe(200);
const envelope = JSON.parse(res.body);
const dl = await testApp.app.inject({ method: "GET", url: envelope.downloadUrl });
const tmpDir = mkdtempSync(join(tmpdir(), "ch-test-"));
const probeFile = join(tmpDir, "swapped.wav");
writeFileSync(probeFile, dl.rawPayload);
const info = await probeMedia(probeFile);
const audio = info.streams.find((s) => s.type === "audio");
expect(audio?.channels).toBe(2);
}, 60_000);
it("stereo-to-mono on mono input returns 422", async () => {
const res = await postTool("tiny.mp3", "audio/mpeg", MONO_MP3, {
mode: "stereo-to-mono",
});
expect(res.statusCode).toBe(422);
const body = JSON.parse(res.body);
expect(body.details).toMatch(/stereo input/i);
}, 60_000);
it("rejects missing mode (400)", async () => {
const res = await postTool("tone-stereo.wav", "audio/wav", STEREO, {});
expect(res.statusCode).toBe(400);
});
});