Files
netfelix-audio-fix/server/services/__tests__/verify.test.ts
T

104 lines
3.2 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { type ProbedStream, verifyStreamMetadata } from "../verify";
type ExpectedStream = Parameters<typeof verifyStreamMetadata>[0][number];
function expected(
o: Partial<ExpectedStream> & Pick<ExpectedStream, "id" | "stream_id" | "type" | "stream_index">,
): ExpectedStream {
return {
id: o.id,
item_id: 1,
stream_id: o.stream_id,
plan_id: 1,
action: "keep",
target_index: 0,
custom_title: null,
custom_language: null,
transcode_codec: null,
codec: null,
profile: null,
language: null,
title: null,
is_default: 0,
is_forced: 0,
is_hearing_impaired: 0,
channels: null,
channel_layout: null,
bit_rate: null,
sample_rate: null,
bit_depth: null,
width: null,
height: null,
...o,
};
}
function probed(o: Partial<ProbedStream> & Pick<ProbedStream, "type">): ProbedStream {
return {
codec: null,
language: null,
title: null,
isDefault: 0,
...o,
};
}
describe("verifyStreamMetadata", () => {
test("detects dirty video title metadata", () => {
const mismatch = verifyStreamMetadata(
[expected({ id: 1, stream_id: 1, type: "Video", stream_index: 0, codec: "h264", width: 1920, height: 1080 })],
[probed({ type: "Video", codec: "h264", title: "Movie.Name.1080p.ADS-GRP" })],
);
expect(mismatch?.reason).toContain("video track 0: title");
expect(mismatch?.reason).toContain("1080p - H.264");
});
test("detects dirty audio title metadata", () => {
const mismatch = verifyStreamMetadata(
[
expected({
id: 1,
stream_id: 1,
type: "Audio",
stream_index: 0,
codec: "dts",
language: "eng",
channels: 6,
}),
],
[probed({ type: "Audio", codec: "dts", language: "eng", title: "English DTS ads", isDefault: 1 })],
);
expect(mismatch?.reason).toContain("audio track 0: title");
expect(mismatch?.reason).toContain("ENG - DTS 5.1");
});
test("detects non-canonical language tags and wrong default disposition", () => {
const languageMismatch = verifyStreamMetadata(
[expected({ id: 1, stream_id: 1, type: "Audio", stream_index: 0, codec: "aac", language: "eng" })],
[probed({ type: "Audio", codec: "aac", language: "en", title: "ENG - AAC", isDefault: 1 })],
);
expect(languageMismatch?.reason).toContain("language en ≠ expected eng");
const defaultMismatch = verifyStreamMetadata(
[expected({ id: 1, stream_id: 1, type: "Audio", stream_index: 0, codec: "aac", language: "eng" })],
[probed({ type: "Audio", codec: "aac", language: "eng", title: "ENG - AAC", isDefault: 0 })],
);
expect(defaultMismatch?.reason).toContain("default disposition 0 ≠ expected 1");
});
test("returns null when video and audio metadata already match", () => {
const mismatch = verifyStreamMetadata(
[
expected({ id: 1, stream_id: 1, type: "Video", stream_index: 0, codec: "hevc", width: 3840, height: 2160 }),
expected({ id: 2, stream_id: 2, type: "Audio", stream_index: 1, codec: "eac3", language: "eng", channels: 6 }),
],
[
probed({ type: "Video", codec: "hevc", title: "2160p - HEVC" }),
probed({ type: "Audio", codec: "eac3", language: "eng", title: "ENG - EAC3 5.1", isDefault: 1 }),
],
);
expect(mismatch).toBeNull();
});
});