233 lines
13 KiB
JavaScript
233 lines
13 KiB
JavaScript
'use strict';
|
|
const path = require('path');
|
|
const os = require('os');
|
|
const fs = require('fs');
|
|
|
|
const ROOT = process.cwd();
|
|
let passed = 0, failed = 0;
|
|
function ok(cond, msg) {
|
|
if (cond) { console.log(` [PASS] ${msg}`); passed++; }
|
|
else { console.error(` [FAIL] ${msg}`); failed++; }
|
|
}
|
|
function eq(a, b, msg) { ok(String(a) === String(b), `${msg} (expected "${b}", got "${a}")`); }
|
|
|
|
function makeStream(o) {
|
|
return Object.assign({
|
|
index: 0, codec_type: 'video', codec_name: 'h264',
|
|
r_frame_rate: '24/1', avg_frame_rate: '24/1',
|
|
width: 1920, height: 1080, channels: 2,
|
|
bit_rate: '0', tags: {}, disposition: {}, profile: '',
|
|
}, o);
|
|
}
|
|
function makeArgs(filePath, streams, inputs, variables) {
|
|
const workDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tdarr-logic-'));
|
|
return {
|
|
inputFileObj: { _id: filePath, ffProbeData: { streams }, mediaInfo: { track: [] } },
|
|
variables: Object.assign({}, variables || {}),
|
|
inputs: inputs || {},
|
|
workDir,
|
|
ffmpegPath: 'ffmpeg',
|
|
mkvpropeditPath: 'mkvpropedit',
|
|
jobLog: () => {},
|
|
_workDir: workDir,
|
|
};
|
|
}
|
|
function cleanup(a) { try { fs.rmSync(a._workDir, { recursive: true, force: true }); } catch(_){} }
|
|
|
|
const { plugin: analyzerPlugin } = require(path.join(ROOT, 'LocalFlowPlugins/utility/mediaAnalyzerGOP/1.0.0/index'));
|
|
const { plugin: audioPlugin } = require(path.join(ROOT, 'LocalFlowPlugins/audio/audioSelectorClamping/1.0.0/index'));
|
|
|
|
const ANALYZER_INPUTS = {
|
|
moviePathPatterns: '/Radarr', animeSeries: 'Naruto|One Piece',
|
|
filmBitrate: '3000', seriesBitrate: '3000', animeBitrateHd: '2000', animeBitrateSd: '1000',
|
|
};
|
|
const AUDIO_INPUTS = {
|
|
filmMultiChannelBitrate: '640', seriesMultiChannelThreshold: '384', stereoBitrateMax: '128',
|
|
};
|
|
|
|
(async () => {
|
|
// ── mediaAnalyzerGOP ──────────────────────────────────────────────────────
|
|
console.log('\n── mediaAnalyzerGOP ──────────────────────────────────');
|
|
|
|
{ const a = makeArgs('/media/toshiba/Radarr/Film (2023)/Film.mkv',
|
|
[makeStream({ r_frame_rate:'24000/1001', height:1080 })], ANALYZER_INPUTS);
|
|
const o = await analyzerPlugin(a);
|
|
eq(o.variables.gopKeyint, '96', '23.976fps -> keyint=96 (4 seconds)');
|
|
eq(o.variables.gopMinKeyint, '24', '23.976fps -> min-keyint=24 (1 second)');
|
|
eq(o.variables.gopSceneCut, '40', 'scene-cut threshold is 40');
|
|
eq(o.variables.isMovie, 'true', '/Radarr -> isMovie=true');
|
|
eq(o.variables.isAnimation, 'false', 'no anime -> isAnimation=false');
|
|
eq(o.variables.vBitrate, '3000k', 'film bitrate=3000k');
|
|
eq(o.variables.vMaxRate, '6000k', 'maxrate=2x');
|
|
eq(o.variables.vBufSize, '12000k', 'bufsize=4x');
|
|
eq(o.variables.x265Tune, '', 'live-action -> no tune');
|
|
cleanup(a); }
|
|
|
|
{ const a = makeArgs('/media/toshiba/Sonarr/Naruto S01/S01E01.mkv',
|
|
[makeStream({ r_frame_rate:'25/1', height:720 })], ANALYZER_INPUTS);
|
|
const o = await analyzerPlugin(a);
|
|
eq(o.variables.gopKeyint, '100', '25fps -> keyint=100 (4 seconds)');
|
|
eq(o.variables.isMovie, 'false', '/Sonarr -> isMovie=false');
|
|
eq(o.variables.isAnimation, 'true', 'Naruto -> isAnimation=true');
|
|
eq(o.variables.x265Tune, 'animation', 'anime -> tune=animation');
|
|
eq(o.variables.vBitrate, '2000k', 'anime 720p HD -> 2000k');
|
|
cleanup(a); }
|
|
|
|
{ const a = makeArgs('/media/toshiba/Sonarr/Naruto S01/S01E01.mkv',
|
|
[makeStream({ r_frame_rate:'30000/1001', height:480 })], ANALYZER_INPUTS);
|
|
const o = await analyzerPlugin(a);
|
|
eq(o.variables.gopKeyint, '120', '29.97fps -> keyint=120 (4 seconds)');
|
|
eq(o.variables.vBitrate, '1000k', 'anime SD 480p -> 1000k');
|
|
cleanup(a); }
|
|
|
|
// Windows backslash path normalization
|
|
{ const a = makeArgs('C:\\media\\toshiba\\Radarr\\Film (2023)\\Film.mkv',
|
|
[makeStream({ r_frame_rate:'24/1', height:1080 })], ANALYZER_INPUTS);
|
|
const o = await analyzerPlugin(a);
|
|
eq(o.variables.isMovie, 'true', 'Windows backslash path: /Radarr still matched');
|
|
cleanup(a); }
|
|
|
|
// ── audioSelectorClamping ─────────────────────────────────────────────────
|
|
console.log('\n── audioSelectorClamping ────────────────────────────');
|
|
|
|
// EAC3 wins over AC3 by score
|
|
{ const streams = [
|
|
makeStream({ index:0, codec_type:'video' }),
|
|
makeStream({ index:1, codec_type:'audio', codec_name:'ac3', bit_rate:'448000', channels:6, tags:{language:'ita'} }),
|
|
makeStream({ index:2, codec_type:'audio', codec_name:'eac3', bit_rate:'640000', channels:6, tags:{language:'eng'} }),
|
|
];
|
|
const a = makeArgs('/media/Radarr/Film.mkv', streams, AUDIO_INPUTS, { isMovie:'true' });
|
|
const o = await audioPlugin(a);
|
|
eq(o.outputNumber, 1, 'output 1 = tracks found');
|
|
eq(o.variables.audioItaCodecName, 'ac3', 'ITA=ac3');
|
|
eq(o.variables.audioEngCodecName, 'eac3', 'ENG=eac3');
|
|
eq(o.variables.audioItaOpusBitrate, '448', 'ITA=min(640,448)=448');
|
|
eq(o.variables.audioEngOpusBitrate, '640', 'ENG=min(640,640)=640');
|
|
cleanup(a); }
|
|
|
|
// EAC3 640k vs AC3 680k same lang — EAC3 wins by weight
|
|
{ const streams = [
|
|
makeStream({ index:0, codec_type:'video' }),
|
|
makeStream({ index:1, codec_type:'audio', codec_name:'eac3', bit_rate:'640000', channels:6, tags:{language:'ita'} }),
|
|
makeStream({ index:2, codec_type:'audio', codec_name:'ac3', bit_rate:'680000', channels:6, tags:{language:'ita'} }),
|
|
];
|
|
const a = makeArgs('/media/Radarr/Film.mkv', streams, AUDIO_INPUTS, { isMovie:'true' });
|
|
const o = await audioPlugin(a);
|
|
eq(o.variables.audioItaCodecName, 'eac3', 'EAC3@640k > AC3@680k by codec weight');
|
|
eq(o.variables.audioItaStreamIndex, '1', 'EAC3 at stream index 1');
|
|
cleanup(a); }
|
|
|
|
// Series DTS 768k >= 384 threshold -> 384
|
|
{ const streams = [
|
|
makeStream({ index:0, codec_type:'video' }),
|
|
makeStream({ index:1, codec_type:'audio', codec_name:'dts', bit_rate:'768000', channels:6, tags:{language:'ita'} }),
|
|
];
|
|
const a = makeArgs('/media/Sonarr/Show/ep.mkv', streams, AUDIO_INPUTS, { isMovie:'false' });
|
|
const o = await audioPlugin(a);
|
|
eq(o.variables.audioItaOpusBitrate, '384', 'Series DTS 768k >= 384 -> Opus=384');
|
|
cleanup(a); }
|
|
|
|
// Series AC3 320k < 384 threshold -> 256
|
|
{ const streams = [
|
|
makeStream({ index:0, codec_type:'video' }),
|
|
makeStream({ index:1, codec_type:'audio', codec_name:'ac3', bit_rate:'320000', channels:6, tags:{language:'ita'} }),
|
|
];
|
|
const a = makeArgs('/media/Sonarr/Show/ep.mkv', streams, AUDIO_INPUTS, { isMovie:'false' });
|
|
const o = await audioPlugin(a);
|
|
eq(o.variables.audioItaOpusBitrate, '256', 'Series AC3 320k < 384 -> Opus=256');
|
|
cleanup(a); }
|
|
|
|
// NEW: Series bitrate unknown (0) -> safe default 384k
|
|
{ const streams = [
|
|
makeStream({ index:0, codec_type:'video' }),
|
|
makeStream({ index:1, codec_type:'audio', codec_name:'dts', bit_rate:'0', channels:6, tags:{language:'ita'} }),
|
|
];
|
|
const a = makeArgs('/media/Sonarr/Show/ep.mkv', streams, AUDIO_INPUTS, { isMovie:'false' });
|
|
const o = await audioPlugin(a);
|
|
eq(o.variables.audioItaOpusBitrate, '384', 'Series unknown bitrate (0) -> safe default 384k');
|
|
cleanup(a); }
|
|
|
|
// Stereo: min(128,192)=128
|
|
{ const streams = [
|
|
makeStream({ index:0, codec_type:'video' }),
|
|
makeStream({ index:1, codec_type:'audio', codec_name:'aac', bit_rate:'192000', channels:2, tags:{language:'ita'} }),
|
|
];
|
|
const a = makeArgs('/media/Radarr/Film.mkv', streams, AUDIO_INPUTS, { isMovie:'true' });
|
|
const o = await audioPlugin(a);
|
|
eq(o.variables.audioItaOpusBitrate, '128', 'Stereo 192k -> min(128,192)=128');
|
|
cleanup(a); }
|
|
|
|
// Stereo: no upcoding — min(128,64)=64
|
|
{ const streams = [
|
|
makeStream({ index:0, codec_type:'video' }),
|
|
makeStream({ index:1, codec_type:'audio', codec_name:'aac', bit_rate:'64000', channels:2, tags:{language:'eng'} }),
|
|
];
|
|
const a = makeArgs('/media/Radarr/Film.mkv', streams, AUDIO_INPUTS, { isMovie:'true' });
|
|
const o = await audioPlugin(a);
|
|
eq(o.variables.audioEngOpusBitrate, '64', 'Stereo 64k -> no upcoding, stays 64k');
|
|
cleanup(a); }
|
|
|
|
// No ITA/ENG -> output branch 2
|
|
{ const streams = [
|
|
makeStream({ index:0, codec_type:'video' }),
|
|
makeStream({ index:1, codec_type:'audio', codec_name:'ac3', bit_rate:'448000', channels:6, tags:{language:'fra'} }),
|
|
];
|
|
const a = makeArgs('/media/Radarr/Film.mkv', streams, AUDIO_INPUTS, { isMovie:'true' });
|
|
const o = await audioPlugin(a);
|
|
eq(o.outputNumber, 2, 'No ITA/ENG -> output branch 2');
|
|
cleanup(a); }
|
|
|
|
// DTS-HD MA detection via profile field
|
|
{ const streams = [
|
|
makeStream({ index:0, codec_type:'video' }),
|
|
makeStream({ index:1, codec_type:'audio', codec_name:'dts', profile:'DTS-HD MA', bit_rate:'4000000', channels:8, tags:{language:'ita'} }),
|
|
];
|
|
const a = makeArgs('/media/Radarr/Film.mkv', streams, AUDIO_INPUTS, { isMovie:'true' });
|
|
const o = await audioPlugin(a);
|
|
// DTS-HD MA weight=95, score=95*10000+4000=954000 — should be selected
|
|
eq(o.variables.audioItaCodecName, 'dts', 'DTS-HD MA detected (codec_name=dts, profile contains MA)');
|
|
// source 4000kbps, film target 640 -> Opus=min(640,4000)=640
|
|
eq(o.variables.audioItaOpusBitrate, '640', 'DTS-HD MA 4Mbps film -> clamped to 640k');
|
|
cleanup(a); }
|
|
|
|
// ── buildAudioArgs structure check ────────────────────────────────────────
|
|
console.log('\n── transcode2PassX265 buildAudioArgs structure ──────');
|
|
const { plugin: transcodePlugin } = require(path.join(ROOT, 'LocalFlowPlugins/video/transcode2PassX265/1.0.0/index'));
|
|
// Inspect the buildAudioArgs function by reading the file
|
|
const transcodeCode = require('fs').readFileSync(path.join(ROOT, 'LocalFlowPlugins/video/transcode2PassX265/1.0.0/index.js'), 'utf8');
|
|
ok(transcodeCode.includes('`-c:a:${audioTrackOut}`'), 'buildAudioArgs uses indexed -c:a:N (not global -c:a)');
|
|
ok(transcodeCode.includes('`-b:a:${audioTrackOut}`'), 'buildAudioArgs uses indexed -b:a:N (not global -b:a)');
|
|
ok(transcodeCode.includes('[...mapArgs, ...codecArgs]'), 'buildAudioArgs: maps first, then codec settings');
|
|
ok(!transcodeCode.includes("'-c:a', 'libopus'"), 'no unindexed -c:a in buildAudioArgs');
|
|
ok(transcodeCode.includes('bitrateKbps < copyHevcBelowKbps'), 'HEVC copy requires bitrate strictly below the limit');
|
|
ok(transcodeCode.includes("copyHevcBelowKbps || '3000'"), 'HEVC copy limit defaults to 3000 kbps');
|
|
ok(transcodeCode.includes("'-c:v', 'copy'"), 'compatible HEVC video uses stream copy');
|
|
ok(transcodeCode.includes("normalizedKey === 'bps' || normalizedKey === 'bps-eng'"), 'HEVC copy check falls back to Matroska BPS metadata');
|
|
ok(transcodeCode.includes('containerBitrate - otherStreamBitrate'), 'HEVC copy check estimates video bitrate from container and companion streams');
|
|
ok(!transcodeCode.includes("'-map_metadata -1'"), 'pass 1 separates -map_metadata from its value');
|
|
ok(!transcodeCode.includes("'-map_chapters -1'"), 'pass 1 separates -map_chapters from its value');
|
|
ok(transcodeCode.includes('const statsForX265 = path.basename(statsFile)'), 'x265 stats file is relative to workDir on Windows');
|
|
|
|
const copyPluginCode = require('fs').readFileSync(path.join(ROOT, 'LocalFlowPlugins/utility/copyToWorkDir/1.0.0/index.js'), 'utf8');
|
|
ok(copyPluginCode.includes('[copyToWorkDir] Progress:'), 'copyToWorkDir reports copy progress in the job log');
|
|
const copyPluginV2Code = require('fs').readFileSync(path.join(ROOT, 'LocalFlowPlugins/utility/copyToWorkDir/2.0.0/index.js'), 'utf8');
|
|
ok(copyPluginV2Code.includes('updateProgress: args.updateProgress'), 'copyToWorkDir v2 forwards UI progress');
|
|
const replacePluginCode = require('fs').readFileSync(path.join(ROOT, 'LocalFlowPlugins/utility/replaceOriginalFile/2.0.0/index.js'), 'utf8');
|
|
ok(replacePluginCode.includes('.partial.old'), 'replaceOriginalFile v2 uses a rollback backup');
|
|
ok(replacePluginCode.includes('fs.renameSync(temporary, destination)'), 'replaceOriginalFile v2 atomically installs the verified copy');
|
|
const replacePluginV1Code = require('fs').readFileSync(path.join(ROOT, 'LocalFlowPlugins/utility/replaceOriginalFile/1.0.0/index.js'), 'utf8');
|
|
ok(replacePluginV1Code.includes('copyAndVerify'), 'replaceOriginalFile v1 verifies the copied file');
|
|
ok(!replacePluginV1Code.includes('updateProgress'), 'replaceOriginalFile v1 has no progress callback');
|
|
const copyHelperCode = require('fs').readFileSync(path.join(ROOT, 'FlowHelpers/1.0.0/copyWithProgress.js'), 'utf8');
|
|
ok(copyHelperCode.includes('maxRetries = 2'), 'copy helper retries failed writes');
|
|
ok(copyHelperCode.includes('Copy size mismatch'), 'copy helper verifies destination size');
|
|
ok(copyHelperCode.includes('fs.unlinkSync(destination)'), 'copy helper removes partial copies before retrying');
|
|
ok(copyHelperCode.includes('COPY_BUFFER_SIZE = 4 * 1024 * 1024'), 'copy helper uses large SMB-friendly buffers');
|
|
|
|
// ── Summary ───────────────────────────────────────────────────────────────
|
|
console.log(`\n${'='.repeat(55)}`);
|
|
console.log(` ${passed} passed, ${failed} failed`);
|
|
if (failed > 0) { console.error(' SOME TESTS FAILED'); process.exit(1); }
|
|
else console.log(' ALL LOGIC TESTS PASSED');
|
|
})();
|