// Copyright (c) 2026 Gordon Bolton. MIT License. import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; // Mock dependencies vi.mock('../../../server/utils/logger', () => ({ logToFile: vi.fn() })); // Import after mocking const DownloadMatcher = require('../../../server/services/DownloadMatcher'); describe('DownloadMatcher', () => { const ombiBaseUrl = 'http://localhost:5000'; beforeEach(() => { vi.clearAllMocks(); }); describe('addOmbiMatching', () => { it('should return early when ombiBaseUrl is missing', () => { const downloadObj = { type: 'series', title: 'Test Show' }; const series = { tvdbId: '12345', tmdbId: '67890' }; const context = { ombiBaseUrl: null }; DownloadMatcher.addOmbiMatching(downloadObj, series, context); expect(downloadObj.ombiLink).toBeUndefined(); expect(downloadObj.ombiTooltip).toBeUndefined(); }); it('should return early when seriesOrMovie is missing', () => { const downloadObj = { type: 'series', title: 'Test Show' }; const context = { ombiBaseUrl }; DownloadMatcher.addOmbiMatching(downloadObj, null, context); expect(downloadObj.ombiLink).toBeUndefined(); expect(downloadObj.ombiTooltip).toBeUndefined(); }); it('should add ombiLink for series with TMDB ID', () => { const downloadObj = { type: 'series', title: 'Test Show' }; const series = { tmdbId: '67890' }; const context = { ombiBaseUrl }; DownloadMatcher.addOmbiMatching(downloadObj, series, context); expect(downloadObj.ombiLink).toBe('http://localhost:5000/details/tv/67890'); expect(downloadObj.ombiTooltip).toBe('View in Ombi'); }); it('should add ombiLink for movie with TMDB ID', () => { const downloadObj = { type: 'movie', title: 'Test Movie' }; const movie = { tmdbId: '54321' }; const context = { ombiBaseUrl }; DownloadMatcher.addOmbiMatching(downloadObj, movie, context); expect(downloadObj.ombiLink).toBe('http://localhost:5000/details/movie/54321'); expect(downloadObj.ombiTooltip).toBe('View in Ombi'); }); it('should not add ombiLink when TMDB ID is missing', () => { const downloadObj = { type: 'series', title: 'Test Show' }; const series = { tvdbId: '12345' }; const context = { ombiBaseUrl }; DownloadMatcher.addOmbiMatching(downloadObj, series, context); expect(downloadObj.ombiLink).toBeUndefined(); expect(downloadObj.ombiTooltip).toBeUndefined(); }); it('should not add ombiLink for unknown download type', () => { const downloadObj = { type: 'unknown', title: 'Test Unknown' }; const series = { tmdbId: '67890' }; const context = { ombiBaseUrl }; DownloadMatcher.addOmbiMatching(downloadObj, series, context); expect(downloadObj.ombiLink).toBeUndefined(); expect(downloadObj.ombiTooltip).toBeUndefined(); }); }); describe('buildSeriesMapFromRecords', () => { it('should build a map from queue and history records', () => { const queueRecords = [ { seriesId: 1, series: { id: 1, title: 'Series 1' } } ]; const historyRecords = [ { seriesId: 2, series: { id: 2, title: 'Series 2' } } ]; const result = DownloadMatcher.buildSeriesMapFromRecords(queueRecords, historyRecords); expect(result.get(1)).toEqual({ id: 1, title: 'Series 1' }); expect(result.get(2)).toEqual({ id: 2, title: 'Series 2' }); }); it('should not overwrite existing series in map', () => { const queueRecords = [ { seriesId: 1, series: { id: 1, title: 'Series 1' } } ]; const historyRecords = [ { seriesId: 1, series: { id: 1, title: 'Series 1 from History' } } ]; const result = DownloadMatcher.buildSeriesMapFromRecords(queueRecords, historyRecords); expect(result.get(1).title).toBe('Series 1'); }); }); describe('buildMoviesMapFromRecords', () => { it('should build a map from queue and history records', () => { const queueRecords = [ { movieId: 1, movie: { id: 1, title: 'Movie 1' } } ]; const historyRecords = [ { movieId: 2, movie: { id: 2, title: 'Movie 2' } } ]; const result = DownloadMatcher.buildMoviesMapFromRecords(queueRecords, historyRecords); expect(result.get(1)).toEqual({ id: 1, title: 'Movie 1' }); expect(result.get(2)).toEqual({ id: 2, title: 'Movie 2' }); }); }); describe('getSlotStatusAndSpeed', () => { it('should return Paused status when queue is paused', () => { const slot = { status: 'Downloading' }; const result = DownloadMatcher.getSlotStatusAndSpeed(slot, 'Paused', '0', '0'); expect(result.status).toBe('Paused'); expect(result.speed).toBe('0'); }); it('should return slot status when queue is active', () => { const slot = { status: 'Downloading' }; const result = DownloadMatcher.getSlotStatusAndSpeed(slot, 'Active', '1.5 MB/s', '1536'); expect(result.status).toBe('Downloading'); expect(result.speed).toBe('1.5 MB/s'); }); }); });