"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const strict_1 = __importDefault(require("node:assert/strict"));
const node_test_1 = require("node:test");
const radarr_1 = __importDefault(require("../../api/servarr/radarr"));
function buildRadarr() {
    return new radarr_1.default({ url: 'http://localhost:7878/api/v3', apiKey: 'test' });
}
function getAxios(radarr) {
    return radarr.axios;
}
(0, node_test_1.describe)('RadarrAPI removeMovie', () => {
    (0, node_test_1.afterEach)(() => node_test_1.mock.restoreAll());
    (0, node_test_1.it)('removes the movie when it exists in the library', async () => {
        const radarr = buildRadarr();
        node_test_1.mock.method(radarr_1.default.prototype, 'getMovieByTmdbId', async () => ({
            id: 7,
            title: 'Test Movie',
        }));
        const del = node_test_1.mock.method(getAxios(radarr), 'delete', async () => ({}));
        await radarr.removeMovie(550);
        strict_1.default.strictEqual(del.mock.callCount(), 1);
        strict_1.default.strictEqual(del.mock.calls[0].arguments[0], '/movie/7');
    });
    (0, node_test_1.it)('does nothing when the movie is not in the library', async () => {
        const radarr = buildRadarr();
        node_test_1.mock.method(getAxios(radarr), 'get', async () => ({
            data: [{ id: 0, title: 'Fight Club' }],
        }));
        const del = node_test_1.mock.method(getAxios(radarr), 'delete', async () => ({}));
        await strict_1.default.doesNotReject(() => radarr.removeMovie(550));
        strict_1.default.strictEqual(del.mock.callCount(), 0);
    });
    (0, node_test_1.it)('rejects when the tmdbId is unknown to the lookup', async () => {
        const radarr = buildRadarr();
        node_test_1.mock.method(getAxios(radarr), 'get', async () => ({ data: [] }));
        const del = node_test_1.mock.method(getAxios(radarr), 'delete', async () => ({}));
        await strict_1.default.rejects(() => radarr.removeMovie(550), /Movie not found/);
        strict_1.default.strictEqual(del.mock.callCount(), 0);
    });
    (0, node_test_1.it)('ignores a 404 when the movie was already removed in Radarr', async () => {
        const radarr = buildRadarr();
        node_test_1.mock.method(radarr_1.default.prototype, 'getMovieByTmdbId', async () => ({
            id: 7,
            title: 'Test Movie',
        }));
        node_test_1.mock.method(getAxios(radarr), 'delete', async () => {
            throw { response: { status: 404 } };
        });
        await strict_1.default.doesNotReject(() => radarr.removeMovie(550));
    });
    (0, node_test_1.it)('rethrows errors other than 404', async () => {
        const radarr = buildRadarr();
        node_test_1.mock.method(radarr_1.default.prototype, 'getMovieByTmdbId', async () => ({
            id: 7,
            title: 'Test Movie',
        }));
        node_test_1.mock.method(getAxios(radarr), 'delete', async () => {
            throw { response: { status: 500 } };
        });
        await strict_1.default.rejects(() => radarr.removeMovie(550));
    });
    (0, node_test_1.it)('rethrows a 404 from the lookup instead of treating it as removed', async () => {
        const radarr = buildRadarr();
        node_test_1.mock.method(getAxios(radarr), 'get', async () => {
            throw { response: { status: 404 } };
        });
        const del = node_test_1.mock.method(getAxios(radarr), 'delete', async () => ({}));
        await strict_1.default.rejects(() => radarr.removeMovie(550), (e) => e.response?.status === 404);
        strict_1.default.strictEqual(del.mock.callCount(), 0);
    });
});
(0, node_test_1.describe)('RadarrAPI getMovieByTmdbId', () => {
    (0, node_test_1.afterEach)(() => node_test_1.mock.restoreAll());
    (0, node_test_1.it)('rethrows a 401 from the lookup with the status intact', async () => {
        const radarr = buildRadarr();
        node_test_1.mock.method(getAxios(radarr), 'get', async () => {
            throw { response: { status: 401 } };
        });
        await strict_1.default.rejects(() => radarr.getMovieByTmdbId(550), (e) => e.response?.status === 401);
    });
    (0, node_test_1.it)('throws "Movie not found" when the lookup returns no results', async () => {
        const radarr = buildRadarr();
        node_test_1.mock.method(getAxios(radarr), 'get', async () => ({ data: [] }));
        await strict_1.default.rejects(() => radarr.getMovieByTmdbId(550), {
            message: 'Movie not found',
        });
    });
});
