"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("../src/util");
// test("delay", async () => {
//     await delay(2000);
//     console.log('delay complete.');
// });
// test("toHexStr", async () => {
//     let str = crypto.randomUUID();
//     let buf = Buffer.from(str);
//     let hex_str = toHexStr(buf);
//     let hex_str_limit = toHexStr(buf, 10);
//     console.log(hex_str);
//     console.log(hex_str_limit);
//     expect(hex_str.length > hex_str_limit.length).toBe(true);
// });
// test("throttle", async () => {
//     let counter = 0;
//     let fn = () => { counter++; };
//     for (let i = 0; i < 100; i++) {
//         fn();
//     }
//     expect(counter === 100).toBe(true);
//     counter = 0;
//     let throttle_fn = throttle(fn, 1000 * 3);
//     for (let i = 0; i < 100; i++) {
//         throttle_fn();
//     }
//     expect(counter === 1).toBe(true);
// });
test("retry", () => __awaiter(void 0, void 0, void 0, function* () {
    const maxAttempts = 10;
    {
        let counter = 0;
        const fn = function request() {
            return __awaiter(this, void 0, void 0, function* () {
                counter++;
                throw `request error ${counter}`;
            });
        };
        try {
            yield (0, util_1.retry)(fn, maxAttempts, 200);
        }
        catch (error) { }
        expect(counter === 10).toBe(true);
    }
    {
        let counter = 0;
        const fn = function request() {
            return __awaiter(this, void 0, void 0, function* () {
                counter++;
                if (counter === 8) {
                    return 'success';
                }
                throw `request error ${counter}`;
            });
        };
        let res;
        try {
            res = yield (0, util_1.retry)(fn, maxAttempts, 200);
        }
        catch (error) { }
        expect(counter === 8).toBe(true);
        expect(res).toBe('success');
    }
}));
