"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const core_utils_1 = require("../../helpers/core-utils");
const core_utils_2 = require("../../../shared/core-utils");
const image_utils_1 = require("../../../server/helpers/image-utils");
function checkBuffers(path1, path2, equals) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const [buf1, buf2] = yield Promise.all([
            (0, fs_extra_1.readFile)(path1),
            (0, fs_extra_1.readFile)(path2)
        ]);
        if (equals) {
            (0, chai_1.expect)(buf1.equals(buf2)).to.be.true;
        }
        else {
            (0, chai_1.expect)(buf1.equals(buf2)).to.be.false;
        }
    });
}
function hasTitleExif(path) {
    var _a;
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const result = JSON.parse(yield (0, core_utils_1.execPromise)(`exiftool -json ${path}`));
        return ((_a = result[0]) === null || _a === void 0 ? void 0 : _a.Title) === 'should be removed';
    });
}
describe('Image helpers', function () {
    const imageDestDir = (0, path_1.join)((0, core_utils_2.root)(), 'test-images');
    const imageDestJPG = (0, path_1.join)(imageDestDir, 'test.jpg');
    const imageDestPNG = (0, path_1.join)(imageDestDir, 'test.png');
    const thumbnailSize = { width: 223, height: 122 };
    it('Should skip processing if the source image is okay', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const input = (0, core_utils_2.buildAbsoluteFixturePath)('thumbnail.jpg');
            yield (0, image_utils_1.processImage)({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true });
            yield checkBuffers(input, imageDestJPG, true);
        });
    });
    it('Should not skip processing if the source image does not have the appropriate extension', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const input = (0, core_utils_2.buildAbsoluteFixturePath)('thumbnail.png');
            yield (0, image_utils_1.processImage)({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true });
            yield checkBuffers(input, imageDestJPG, false);
        });
    });
    it('Should not skip processing if the source image does not have the appropriate size', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const input = (0, core_utils_2.buildAbsoluteFixturePath)('preview.jpg');
            yield (0, image_utils_1.processImage)({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true });
            yield checkBuffers(input, imageDestJPG, false);
        });
    });
    it('Should not skip processing if the source image does not have the appropriate size', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const input = (0, core_utils_2.buildAbsoluteFixturePath)('thumbnail-big.jpg');
            yield (0, image_utils_1.processImage)({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true });
            yield checkBuffers(input, imageDestJPG, false);
        });
    });
    it('Should strip exif for a jpg file that can not be copied', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const input = (0, core_utils_2.buildAbsoluteFixturePath)('exif.jpg');
            (0, chai_1.expect)(yield hasTitleExif(input)).to.be.true;
            yield (0, image_utils_1.processImage)({ path: input, destination: imageDestJPG, newSize: { width: 100, height: 100 }, keepOriginal: true });
            yield checkBuffers(input, imageDestJPG, false);
            (0, chai_1.expect)(yield hasTitleExif(imageDestJPG)).to.be.false;
        });
    });
    it('Should strip exif for a jpg file that could be copied', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const input = (0, core_utils_2.buildAbsoluteFixturePath)('exif.jpg');
            (0, chai_1.expect)(yield hasTitleExif(input)).to.be.true;
            yield (0, image_utils_1.processImage)({ path: input, destination: imageDestJPG, newSize: thumbnailSize, keepOriginal: true });
            yield checkBuffers(input, imageDestJPG, false);
            (0, chai_1.expect)(yield hasTitleExif(imageDestJPG)).to.be.false;
        });
    });
    it('Should strip exif for png', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const input = (0, core_utils_2.buildAbsoluteFixturePath)('exif.png');
            (0, chai_1.expect)(yield hasTitleExif(input)).to.be.true;
            yield (0, image_utils_1.processImage)({ path: input, destination: imageDestPNG, newSize: thumbnailSize, keepOriginal: true });
            (0, chai_1.expect)(yield hasTitleExif(imageDestPNG)).to.be.false;
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, fs_extra_1.remove)(imageDestDir);
        });
    });
});
//# sourceMappingURL=image.js.map