"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = __importDefault(require("assert"));
const promises_1 = require("node:fs/promises");
const node_path_1 = __importDefault(require("node:path"));
const BinaryExtractionTask_1 = require("./BinaryExtractionTask");
const ExifTool_1 = require("./ExifTool");
const _chai_spec_1 = require("./_chai.spec");
after(() => (0, _chai_spec_1.end)(ExifTool_1.exiftool));
describe("BinaryExtractionTask", () => {
    describe("parser", () => {
        const sut = BinaryExtractionTask_1.BinaryExtractionTask.for("ThumbnailImage", "", "");
        it("returns success (undefined, no error) from expected input", () => {
            (0, _chai_spec_1.expect)(sut.parse("    1 output files created")).to.eql(undefined);
        });
        it("returns error from expected input", () => {
            (0, _chai_spec_1.expect)(sut.parse("     0 output files created")).to.eql("0 output files created");
        });
        it("throws on empty input", () => {
            (0, _chai_spec_1.expect)(() => sut.parse("")).to.throw(/Missing expected status message/);
        });
        it("extracts the expected error message", () => {
            (0, _chai_spec_1.expect)(() => sut.parse(["Error creating /etc/test.jpg", "1 files could not be read"].join("\n"))).to.throw(/Error creating \/etc\/test.jpg/);
        });
    });
    async function assertExtractThumbnail(str, opts) {
        const dir = node_path_1.default.join((0, _chai_spec_1.tmpdir)(), "binary-" + (0, _chai_spec_1.randomChars)());
        // Make sure the str (which may have non-latin characters) shows up in the
        // directory path and filename:
        const srcDir = node_path_1.default.join(dir, "src");
        await (0, _chai_spec_1.mkdirp)(srcDir);
        const src = node_path_1.default.join(srcDir, str + ".jpg");
        await (0, promises_1.copyFile)(node_path_1.default.join(_chai_spec_1.testDir, "with_thumb.jpg"), src);
        const dest = node_path_1.default.join(dir, "dest", str + ".jpg");
        await ExifTool_1.exiftool.extractThumbnail(src, dest, opts);
        // exiftool test/with_thumb.jpg -b -ThumbnailImage | sha1sum
        (0, _chai_spec_1.expect)(await (0, _chai_spec_1.sha1)(dest)).to.eql("57885e5e16b16599ccf208981a87fe198612d9fb");
    }
    it("extracts expected thumb", () => assertExtractThumbnail("image"));
    for (const { str, desc } of _chai_spec_1.NonAlphaStrings) {
        it("extracts expected thumb with " + desc + " characters", () => assertExtractThumbnail(str));
    }
    it("throws for missing src", async function () {
        this.slow(500);
        const src = node_path_1.default.join(_chai_spec_1.testDir, "nonexistant-file.jpg");
        const dest = (0, _chai_spec_1.tmpname)();
        try {
            await ExifTool_1.exiftool.extractJpgFromRaw(src, dest);
            assert_1.default.fail("expected error to be thrown");
        }
        catch (err) {
            (0, _chai_spec_1.expect)(String(err)).to.match(/File not found/i);
        }
    });
    it("throws for missing thumb", async function () {
        this.slow(500);
        const src = node_path_1.default.join(_chai_spec_1.testDir, "with_thumb.jpg");
        const dest = (0, _chai_spec_1.tmpname)();
        try {
            await ExifTool_1.exiftool.extractJpgFromRaw(src, dest);
            assert_1.default.fail("expected error to be thrown");
        }
        catch (err) {
            (0, _chai_spec_1.expect)(String(err)).to.match(/Error: 0 output files created/i);
        }
    });
    it("throws when output file already exists", async () => {
        const src = node_path_1.default.join(_chai_spec_1.testDir, "with_thumb.jpg");
        const dest = (0, _chai_spec_1.tmpname)();
        await (0, promises_1.writeFile)(dest, "existing file");
        (0, _chai_spec_1.expect)(ExifTool_1.exiftool.extractJpgFromRaw(src, dest)).to.be.rejectedWith(/already exists/i);
    });
    it("does not throw when output file exists if `forceWrite` is true", async () => {
        return assertExtractThumbnail("image", {
            ignoreMinorErrors: true,
            forceWrite: true,
        });
    });
});
//# sourceMappingURL=BinaryExtractionTask.spec.js.map