Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { syncMetadataProfiles } from "./metadataProfiles/metadataProfileSyncer";
import { cloneRecyclarrTemplateRepo } from "./recyclarr-importer";
import { loadServerTags } from "./tags";
import { getTelemetryInstance, Telemetry } from "./telemetry";
import { cloneTrashRepo, loadQualityDefinitionFromTrash, transformTrashQDs } from "./trash-guide";
import { checkCustomFormatConflicts, cloneTrashRepo, loadQualityDefinitionFromTrash, transformTrashQDs } from "./trash-guide";
import { ArrType } from "./types/common.types";
import { InputConfigArrInstance, InputConfigSchema } from "./types/config.types";
import { TrashArrSupportedConst, TrashQualityDefinition, TrashQualityDefinitionQuality } from "./types/trashguide.types";
Expand Down Expand Up @@ -57,6 +57,8 @@ const pipeline = async (globalConfig: InputConfigSchema, instanceConfig: InputCo
const idsToManage = calculateCFsToManage(config);
logger.debug(Array.from(idsToManage), `CustomFormats to manage`);

checkCustomFormatConflicts(arrType, idsToManage);

const mergedCFs = await loadCustomFormatDefinitions(idsToManage, arrType, config.customFormatDefinitions || []);

const serverCFMapping = serverCache.cf.reduce((p, c) => {
Expand Down
67 changes: 65 additions & 2 deletions src/trash-guide.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import fs from "node:fs";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { loadAllQDsFromTrash, loadQPFromTrash, transformTrashCFGroups, transformTrashQDs, transformTrashQPCFGroups } from "./trash-guide";
import {
checkCustomFormatConflicts,
loadAllQDsFromTrash,
loadConflictsFromTrash,
loadQPFromTrash,
transformTrashCFGroups,
transformTrashQDs,
transformTrashQPCFGroups,
} from "./trash-guide";
import { InputConfigCustomFormatGroup } from "./types/config.types";
import { TrashCFGroupMapping, TrashQualityDefinition, TrashQP } from "./types/trashguide.types";
import { TrashCFGroupMapping, TrashConflicts, TrashQualityDefinition, TrashQP } from "./types/trashguide.types";
import * as util from "./util";

describe("TrashGuide", async () => {
Expand Down Expand Up @@ -657,4 +665,59 @@ describe("TrashGuide", async () => {
expect(result).toHaveLength(0);
});
});

describe("loadConflictsFromTrash", () => {
test("should return empty map for unsupported arrType", async () => {
const result = await loadConflictsFromTrash("LIDARR");

expect(result).toBeInstanceOf(Map);
expect(result.size).toBe(0);
});

test("should load conflicts from conflicts.json", async () => {
const mockConflicts: TrashConflicts = {
custom_formats: [
{
"9c38ebb7384dada637be8899efa68e6f": { name: "SDR", desc: "" },
"25c12f78430a3a23413652cbd1d48d77": { name: "SDR (no WEBDL)", desc: "" },
},
],
};

vi.spyOn(util, "loadJsonFile").mockReturnValue(mockConflicts);

const result = await loadConflictsFromTrash("RADARR");

expect(result).toBeInstanceOf(Map);
expect(result.size).toBe(1);
});

test("should return empty map when conflicts.json does not exist", async () => {
vi.spyOn(util, "loadJsonFile").mockImplementation(() => {
throw new Error("ENOENT: no such file or directory");
});

const result = await loadConflictsFromTrash("SONARR");

expect(result).toBeInstanceOf(Map);
expect(result.size).toBe(0);
});

test("should return empty map when conflicts.json has no custom_formats", async () => {
const mockConflicts: TrashConflicts = { custom_formats: [] };

vi.spyOn(util, "loadJsonFile").mockReturnValue(mockConflicts);

const result = await loadConflictsFromTrash("SONARR");

expect(result).toBeInstanceOf(Map);
expect(result.size).toBe(0);
});
});

describe("checkCustomFormatConflicts", () => {
test("should return early for unsupported arrType", () => {
expect(() => checkCustomFormatConflicts("LIDARR", new Set(["123"]))).not.toThrow();
});
});
});
86 changes: 85 additions & 1 deletion src/trash-guide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { MergedCustomFormatResource } from "./__generated__/mergedTypes";
import { getConfig } from "./config";
import { logger } from "./logger";
import { interpolateSize } from "./quality-definitions";
import { CFIDToConfigGroup, ConfigarrCF, QualityDefinitionsRadarr, QualityDefinitionsSonarr } from "./types/common.types";
import { ArrType, CFIDToConfigGroup, ConfigarrCF, QualityDefinitionsRadarr, QualityDefinitionsSonarr } from "./types/common.types";
import { ConfigCustomFormat, ConfigQualityProfile, ConfigQualityProfileItem, InputConfigCustomFormatGroup } from "./types/config.types";
import {
TrashArrSupported,
TrashCache,
TrashCF,
TrashCFGroupMapping,
TrashConflicts,
TrashConflictsMapping,
TrashCustomFormatGroups,
TrashQP,
TrashQualityDefinition,
Expand Down Expand Up @@ -44,6 +46,9 @@ const createCache = async () => {
const sonarrQDSeries = await loadQualityDefinitionFromTrash("series", "SONARR");
const sonarrQDAnime = await loadQualityDefinitionFromTrash("anime", "SONARR");

const radarrConflicts = await loadConflictsFromTrash("RADARR");
const sonarrConflicts = await loadConflictsFromTrash("SONARR");

cache = {
SONARR: {
qualityProfiles: sonarrQP,
Expand All @@ -54,6 +59,7 @@ const createCache = async () => {
series: sonarrQDSeries,
},
naming: sonarrNaming,
conflicts: sonarrConflicts,
},
RADARR: {
qualityProfiles: radarrQP,
Expand All @@ -63,6 +69,7 @@ const createCache = async () => {
movie: radarrQDMovie,
},
naming: radarrNaming,
conflicts: radarrConflicts,
},
};

Expand Down Expand Up @@ -323,6 +330,83 @@ export const loadNamingFromTrashRadarr = async (): Promise<TrashRadarrNaming | n
return firstValue;
};

export const loadConflictsFromTrash = async (arrType: ArrType): Promise<TrashConflictsMapping> => {
if (arrType !== "RADARR" && arrType !== "SONARR") {
logger.debug(`Unsupported arrType: ${arrType}. Skipping TrashConflicts.`);

return new Map();
}

if (cacheReady) {
return cache[arrType].conflicts;
}

const conflictsMapping: TrashConflictsMapping = new Map();

const conflictsPath = arrType === "RADARR" ? trashRepoPaths.radarrConflicts : trashRepoPaths.sonarrConflicts;

try {
const conflictsFile = loadJsonFile<TrashConflicts>(conflictsPath);

if (conflictsFile && conflictsFile.custom_formats && conflictsFile.custom_formats.length > 0) {
conflictsFile.custom_formats.forEach((conflictGroup, index) => {
conflictsMapping.set(index, conflictGroup);
});

logger.debug(`(${arrType}) Loaded ${conflictsMapping.size} conflict groups from TRaSH-Guides`);
} else {
logger.debug(`(${arrType}) No conflicts defined in TRaSH-Guides`);
}
} catch (err: any) {
logger.debug(`(${arrType}) Failed loading TRaSH-Guides conflicts: ${err?.message ?? err}. Continuing without conflicts.`);
}

return conflictsMapping;
};

export const checkCustomFormatConflicts = (arrType: ArrType, cfTrashIds: Set<string>) => {
if (arrType !== "RADARR" && arrType !== "SONARR") {
return;
}

if (!cacheReady) {
logger.debug(`(${arrType}) Cache not ready. Cannot check conflicts.`);
return;
}

const conflicts = cache[arrType].conflicts;

if (conflicts.size === 0) {
logger.debug(`(${arrType}) No conflicts defined.`);
return;
}

if (cfTrashIds.size < 2) {
return;
}

let foundConflicts = false;

for (const [_, conflictGroup] of conflicts) {
const conflictingCfs: string[] = [];

for (const [trashId, entry] of Object.entries(conflictGroup)) {
if (cfTrashIds.has(trashId)) {
conflictingCfs.push(entry.name);
}
}

if (conflictingCfs.length > 1) {
foundConflicts = true;
logger.warn(`(${arrType}) Conflicting custom formats found: ${conflictingCfs.join(", ")}`);
}
}

if (!foundConflicts) {
logger.debug(`(${arrType}) No conflicts detected among selected custom formats.`);
}
};

// TODO merge two methods?
export const transformTrashQPToTemplate = (data: TrashQP, useOldQualityOrder: boolean = false): ConfigQualityProfile => {
const items = data.items
Expand Down
14 changes: 14 additions & 0 deletions src/types/trashguide.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export type TrashCache = {
anime: TrashQualityDefinition;
};
naming: TrashSonarrNaming | null;
conflicts: TrashConflictsMapping;
};
RADARR: {
qualityProfiles: Map<string, TrashQP>;
Expand All @@ -113,6 +114,7 @@ export type TrashCache = {
movie: TrashQualityDefinition;
};
naming: TrashRadarrNaming | null;
conflicts: TrashConflictsMapping;
};
};

Expand Down Expand Up @@ -154,3 +156,15 @@ export type TrashCustomFormatGroups = {
};

export type TrashCFGroupMapping = Map<string, TrashCustomFormatGroups>;

// Conflict types for mutually exclusive custom formats
export type TrashConflictEntry = {
name: string;
desc?: string;
};

export type TrashConflicts = {
custom_formats: Record<string, TrashConflictEntry>[];
};

export type TrashConflictsMapping = Map<number, Record<string, TrashConflictEntry>>;
2 changes: 2 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ export const trashRepoPaths = {
sonarrQualitySize: `${trashRepoSonarrRoot}/quality-size`,
sonarrQP: `${trashRepoSonarrRoot}/quality-profiles`,
sonarrNaming: `${trashRepoSonarrRoot}/naming`,
sonarrConflicts: `${trashRepoSonarrRoot}/conflicts.json`,
radarrCF: `${trashRepoRadarrRoot}/cf`,
radarrCFGroups: `${trashRepoRadarrRoot}/cf-groups`,
radarrQualitySize: `${trashRepoRadarrRoot}/quality-size`,
radarrQP: `${trashRepoRadarrRoot}/quality-profiles`,
radarrNaming: `${trashRepoRadarrRoot}/naming`,
radarrConflicts: `${trashRepoRadarrRoot}/conflicts.json`,
};

export const recyclarrRepoPaths = {
Expand Down