"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 node_crypto_1 = __importDefault(require("node:crypto"));
const promises_1 = require("node:fs/promises");
const node_path_1 = __importDefault(require("node:path"));
const BinaryToBufferTask_1 = require("./BinaryToBufferTask");
const ExifTool_1 = require("./ExifTool");
const _chai_spec_1 = require("./_chai.spec");
after(() => (0, _chai_spec_1.end)(ExifTool_1.exiftool));
describe("BinaryToBufferTask", () => {
    describe("parser", () => {
        const sut = BinaryToBufferTask_1.BinaryToBufferTask.for("ThumbnailImage", "");
        it("returns success (undefined, no error) from expected input", () => {
            const result = sut.parse(JSON.stringify([
                { SourceFile: "test.jpg", ThumbnailImage: "base64:aGVsbG8gd29ybGQ=" },
            ]));
            (0, _chai_spec_1.expect)(result.toString()).to.eql("hello world");
        });
        it("returns error from unexpected input", () => {
            (0, _chai_spec_1.expect)(sut.parse("invalid").toString()).to.match(/invalid/);
        });
        it("throws on empty input", () => {
            (0, _chai_spec_1.expect)(sut.parse("").toString()).to.match(/Unexpected end of JSON input/i);
        });
        it("returns any provided errors", () => {
            const err = new Error(node_crypto_1.default.randomBytes(3).toString("hex"));
            (0, _chai_spec_1.expect)(sut.parse("", err).toString()).to.include(err.message);
        });
    });
    async function assertExtractThumbnail(str) {
        // 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((0, _chai_spec_1.tmpdir)(), "src-" + str);
        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 buf = await ExifTool_1.exiftool.extractBinaryTagToBuffer("ThumbnailImage", src);
        // exiftool test/with_thumb.jpg -b -ThumbnailImage | sha1sum
        (0, _chai_spec_1.expect)((0, _chai_spec_1.sha1buffer)(buf)).to.eql("57885e5e16b16599ccf208981a87fe198612d9fb");
    }
    it("extracts expected thumb", async function () {
        this.slow(500);
        await 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");
        try {
            await ExifTool_1.exiftool.extractBinaryTagToBuffer("JpgFromRaw", src);
            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");
        try {
            await ExifTool_1.exiftool.extractBinaryTagToBuffer("JpgFromRaw", src);
            assert_1.default.fail("expected error to be thrown");
        }
        catch (err) {
            (0, _chai_spec_1.expect)(String(err)).to.match(/JpgFromRaw not found/i);
        }
    });
    it("rejects argument-injection via tagname", async () => {
        const src = node_path_1.default.join(_chai_spec_1.testDir, "with_thumb.jpg");
        return (0, _chai_spec_1.expect)(ExifTool_1.exiftool.extractBinaryTagToBuffer("ThumbnailImage\n-p\n/etc/passwd", src)).to.be.rejectedWith(/Invalid tag name|control character/);
    });
});
//# sourceMappingURL=BinaryToBufferTask.spec.js.map