"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const pako_1 = __importDefault(require("pako"));
const Types_1 = require("@wireio/sdk-core/api/v1/Types");
const Bytes_1 = require("@wireio/sdk-core/chain/Bytes");
const Transaction_1 = require("@wireio/sdk-core/chain/Transaction");
const PackedTransactionCompression_1 = require("@wireio/sdk-core/chain/PackedTransactionCompression");
const serializer_1 = require("@wireio/sdk-core/serializer");
const transactions_js_1 = require("../support/transactions.js");
const ApiPackedTransactionCompression = {
    zlib: "zlib"
};
const InflateLimitMessage = /Packed transaction zlib output exceeds/;
const InflateFailureMessage = /Unable to inflate packed transaction/;
const InvalidLimitMessage = /non-negative safe integer/;
function deflate(bytes) {
    return pako_1.default.deflate(bytes);
}
function truncateLastByte(bytes) {
    return bytes.subarray(0, bytes.byteLength - 1);
}
function createOversizedDeflatedBytes() {
    return deflate(new Uint8Array(PackedTransactionCompression_1.PackedTransactionCompression.DefaultMaxInflatedBytes + 1));
}
describe("packed transaction zlib inflation", () => {
    test("inflates compressed packed transaction bytes", () => {
        const encoded = serializer_1.Serializer.encode({ object: (0, transactions_js_1.createEmptyTransaction)() }), inflated = (0, PackedTransactionCompression_1.inflatePackedTransaction)(deflate(encoded.array));
        expect(inflated.equals(encoded)).toBe(true);
    });
    test("rejects helper output above the configured byte limit", () => {
        const compressed = deflate(new Uint8Array([1, 2, 3, 4]));
        expect(() => (0, PackedTransactionCompression_1.inflatePackedTransaction)(compressed, { maxInflatedBytes: 3 })).toThrow(InflateLimitMessage);
    });
    test("rejects truncated zlib streams before ABI decode", () => {
        const encoded = serializer_1.Serializer.encode({ object: (0, transactions_js_1.createEmptyTransaction)() }), truncated = truncateLastByte(deflate(encoded.array));
        expect(() => (0, PackedTransactionCompression_1.inflatePackedTransaction)(truncated)).toThrow(InflateFailureMessage);
    });
    test.each([Number.NaN, -1, 1.5])("rejects invalid maxInflatedBytes value %s", maxInflatedBytes => {
        expect(() => (0, PackedTransactionCompression_1.inflatePackedTransaction)(new Uint8Array(), { maxInflatedBytes })).toThrow(InvalidLimitMessage);
    });
});
describe("PackedTransaction.getTransaction", () => {
    test("decodes zlib-compressed transactions", () => {
        const signedTransaction = (0, transactions_js_1.createEmptySignedTransaction)(), packedTransaction = Transaction_1.PackedTransaction.fromSigned(signedTransaction, Transaction_1.CompressionType.zlib);
        expect(packedTransaction.getTransaction().equals(signedTransaction.transaction)).toBe(true);
    });
    test("rejects oversized zlib-compressed transactions before ABI decode", () => {
        const packedTransaction = Transaction_1.PackedTransaction.from({
            compression: Transaction_1.CompressionType.zlib,
            packed_trx: createOversizedDeflatedBytes()
        });
        expect(() => packedTransaction.getTransaction()).toThrow(InflateLimitMessage);
    });
    test("honors custom zlib inflate limits", () => {
        const signedTransaction = (0, transactions_js_1.createEmptySignedTransaction)(), packedTransaction = Transaction_1.PackedTransaction.fromSigned(signedTransaction, Transaction_1.CompressionType.zlib);
        expect(() => packedTransaction.getTransaction({ maxInflatedBytes: 1 })).toThrow(InflateLimitMessage);
    });
});
describe("TrxVariant.transaction", () => {
    test("decodes zlib-compressed transactions", () => {
        const transaction = (0, transactions_js_1.createEmptyTransaction)(), packedTrx = Bytes_1.Bytes.from(deflate(serializer_1.Serializer.encode({ object: transaction }).array)), trxVariant = Types_1.TrxVariant.from({
            id: transaction.id,
            compression: ApiPackedTransactionCompression.zlib,
            packed_trx: packedTrx.hexString
        });
        expect(trxVariant.transaction.equals(transaction)).toBe(true);
    });
    test("rejects oversized zlib-compressed transactions before ABI decode", () => {
        const trxVariant = Types_1.TrxVariant.from({
            id: "0".repeat(64),
            compression: ApiPackedTransactionCompression.zlib,
            packed_trx: Bytes_1.Bytes.from(createOversizedDeflatedBytes()).hexString
        });
        expect(() => trxVariant.transaction).toThrow(InflateLimitMessage);
    });
    test("honors custom zlib inflate limits", () => {
        const transaction = (0, transactions_js_1.createEmptyTransaction)(), packedTrx = Bytes_1.Bytes.from(deflate(serializer_1.Serializer.encode({ object: transaction }).array)), trxVariant = Types_1.TrxVariant.from({
            id: transaction.id,
            compression: ApiPackedTransactionCompression.zlib,
            packed_trx: packedTrx.hexString
        });
        expect(() => trxVariant.getTransaction({ maxInflatedBytes: 1 })).toThrow(InflateLimitMessage);
    });
});
//# sourceMappingURL=PackedTransactionCompression.test.js.map