Files
netfelix-audio-fix/server/services/__tests__/ffmpeg.test.ts
Felix Förtsch 874f04b7a5 wire scheduler into queue, add retry, dev-reset cleanup, biome 2.4 migrate
- execute: actually call isInScheduleWindow/waitForWindow/sleepBetweenJobs in runSequential (they were dead code); emit queue_status SSE events (running/paused/sleeping/idle) so the pipeline's existing QueueStatus listener lights up
- review: POST /:id/retry resets an errored plan to approved, wipes old done/error jobs, rebuilds command from current decisions, queues fresh job
- scan: dev-mode DELETE now also wipes jobs + subtitle_files (previously orphaned after every dev reset)
- biome: migrate config to 2.4 schema, autoformat 68 files (strings + indentation), relax opinionated a11y/hooks-deps/index-key rules that don't fit this codebase
- routeTree.gen.ts regenerated after /nodes removal
2026-04-13 07:41:19 +02:00

210 lines
7.2 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import type { MediaItem, MediaStream, StreamDecision } from "../../types";
import { buildCommand, buildPipelineCommand, predictExtractedFiles, shellQuote, sortKeptStreams } from "../ffmpeg";
function stream(o: Partial<MediaStream> & Pick<MediaStream, "id" | "type" | "stream_index">): MediaStream {
return {
item_id: 1,
codec: null,
language: null,
language_display: null,
title: null,
is_default: 0,
is_forced: 0,
is_hearing_impaired: 0,
channels: null,
channel_layout: null,
bit_rate: null,
sample_rate: null,
...o,
};
}
function decision(o: Partial<StreamDecision> & Pick<StreamDecision, "stream_id" | "action">): StreamDecision {
return {
id: 0,
plan_id: 1,
target_index: null,
custom_title: null,
transcode_codec: null,
...o,
};
}
const ITEM: MediaItem = {
id: 1,
jellyfin_id: "x",
type: "Movie",
name: "Test",
series_name: null,
series_jellyfin_id: null,
season_number: null,
episode_number: null,
year: null,
file_path: "/movies/Test.mkv",
file_size: null,
container: "mkv",
original_language: "eng",
orig_lang_source: "jellyfin",
needs_review: 0,
imdb_id: null,
tmdb_id: null,
tvdb_id: null,
scan_status: "scanned",
scan_error: null,
last_scanned_at: null,
created_at: "",
};
describe("shellQuote", () => {
test("wraps plain strings in single quotes", () => {
expect(shellQuote("hello")).toBe("'hello'");
});
test("escapes single quotes safely", () => {
expect(shellQuote("it's")).toBe("'it'\\''s'");
});
test("handles paths with spaces", () => {
expect(shellQuote("/movies/My Movie.mkv")).toBe("'/movies/My Movie.mkv'");
});
});
describe("sortKeptStreams", () => {
test("orders by type priority (Video, Audio, Subtitle, Data), then target_index", () => {
const streams = [
stream({ id: 1, type: "Audio", stream_index: 1 }),
stream({ id: 2, type: "Video", stream_index: 0 }),
stream({ id: 3, type: "Audio", stream_index: 2 }),
];
const decisions = [
decision({ stream_id: 1, action: "keep", target_index: 1 }),
decision({ stream_id: 2, action: "keep", target_index: 0 }),
decision({ stream_id: 3, action: "keep", target_index: 0 }),
];
const sorted = sortKeptStreams(streams, decisions);
expect(sorted.map((k) => k.stream.id)).toEqual([2, 3, 1]);
});
test("drops streams with action remove", () => {
const streams = [stream({ id: 1, type: "Audio", stream_index: 0 })];
const decisions = [decision({ stream_id: 1, action: "remove" })];
expect(sortKeptStreams(streams, decisions)).toEqual([]);
});
});
describe("buildCommand", () => {
test("produces ffmpeg remux with tmp-rename pattern", () => {
const streams = [
stream({ id: 1, type: "Video", stream_index: 0, codec: "h264" }),
stream({ id: 2, type: "Audio", stream_index: 1, codec: "aac", language: "eng" }),
];
const decisions = [
decision({ stream_id: 1, action: "keep", target_index: 0 }),
decision({ stream_id: 2, action: "keep", target_index: 0 }),
];
const cmd = buildCommand(ITEM, streams, decisions);
expect(cmd).toContain("ffmpeg");
expect(cmd).toContain("-map 0:v:0");
expect(cmd).toContain("-map 0:a:0");
expect(cmd).toContain("-c copy");
expect(cmd).toContain("'/movies/Test.tmp.mkv'");
expect(cmd).toContain("mv '/movies/Test.tmp.mkv' '/movies/Test.mkv'");
});
test("uses type-relative specifiers (0:a:N) not absolute stream_index", () => {
const streams = [
stream({ id: 1, type: "Video", stream_index: 0 }),
stream({ id: 2, type: "Audio", stream_index: 1 }),
stream({ id: 3, type: "Audio", stream_index: 2 }),
];
// Keep only the second audio; still mapped as 0:a:1
const decisions = [
decision({ stream_id: 1, action: "keep", target_index: 0 }),
decision({ stream_id: 2, action: "remove" }),
decision({ stream_id: 3, action: "keep", target_index: 0 }),
];
const cmd = buildCommand(ITEM, streams, decisions);
expect(cmd).toContain("-map 0:a:1");
expect(cmd).not.toContain("-map 0:a:2");
});
test("sets first kept audio as default, clears others", () => {
const streams = [
stream({ id: 1, type: "Video", stream_index: 0 }),
stream({ id: 2, type: "Audio", stream_index: 1, language: "eng" }),
stream({ id: 3, type: "Audio", stream_index: 2, language: "deu" }),
];
const decisions = [
decision({ stream_id: 1, action: "keep", target_index: 0 }),
decision({ stream_id: 2, action: "keep", target_index: 0 }),
decision({ stream_id: 3, action: "keep", target_index: 1 }),
];
const cmd = buildCommand(ITEM, streams, decisions);
expect(cmd).toContain("-disposition:a:0 default");
expect(cmd).toContain("-disposition:a:1 0");
});
});
describe("buildPipelineCommand", () => {
test("emits subtitle extraction outputs and final remux in one pass", () => {
const streams = [
stream({ id: 1, type: "Video", stream_index: 0 }),
stream({ id: 2, type: "Audio", stream_index: 1, codec: "aac", language: "eng" }),
stream({ id: 3, type: "Subtitle", stream_index: 2, codec: "subrip", language: "eng" }),
];
const decisions = [
decision({ stream_id: 1, action: "keep", target_index: 0 }),
decision({ stream_id: 2, action: "keep", target_index: 0 }),
decision({ stream_id: 3, action: "remove" }),
];
const { command, extractedFiles } = buildPipelineCommand(ITEM, streams, decisions);
expect(command).toContain("-map 0:s:0");
expect(command).toContain("-c:s copy");
expect(command).toContain("'/movies/Test.en.srt'");
expect(command).toContain("-map 0:v:0");
expect(command).toContain("-map 0:a:0");
expect(extractedFiles).toHaveLength(1);
expect(extractedFiles[0].path).toBe("/movies/Test.en.srt");
});
test("transcodes incompatible audio with per-track codec flag", () => {
const dtsItem = { ...ITEM, container: "mp4", file_path: "/movies/x.mp4" };
const streams = [
stream({ id: 1, type: "Video", stream_index: 0 }),
stream({ id: 2, type: "Audio", stream_index: 1, codec: "dts", language: "eng", channels: 6 }),
];
const decisions = [
decision({ stream_id: 1, action: "keep", target_index: 0 }),
decision({ stream_id: 2, action: "keep", target_index: 0, transcode_codec: "eac3" }),
];
const { command } = buildPipelineCommand(dtsItem, streams, decisions);
expect(command).toContain("-c:a:0 eac3");
expect(command).toContain("-b:a:0 640k"); // 6 channels → 640k
});
});
describe("predictExtractedFiles", () => {
test("predicts sidecar paths matching extraction output", () => {
const streams = [
stream({ id: 1, type: "Subtitle", stream_index: 0, codec: "subrip", language: "eng" }),
stream({ id: 2, type: "Subtitle", stream_index: 1, codec: "subrip", language: "deu", is_forced: 1 }),
];
const files = predictExtractedFiles(ITEM, streams);
expect(files).toHaveLength(2);
expect(files[0].file_path).toBe("/movies/Test.en.srt");
expect(files[1].file_path).toBe("/movies/Test.de.forced.srt");
expect(files[1].is_forced).toBe(true);
});
test("deduplicates paths with a numeric suffix", () => {
const streams = [
stream({ id: 1, type: "Subtitle", stream_index: 0, codec: "subrip", language: "eng" }),
stream({ id: 2, type: "Subtitle", stream_index: 1, codec: "subrip", language: "eng" }),
];
const files = predictExtractedFiles(ITEM, streams);
expect(files[0].file_path).toBe("/movies/Test.en.srt");
expect(files[1].file_path).toBe("/movies/Test.en.2.srt");
});
});