"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

var _iterator = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/symbol/iterator"));

var _symbol = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/symbol"));

var _promise = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/promise"));

var _helper = require("./helper");

var __awaiter = void 0 && (void 0).__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["default"]))(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());
  });
};

var __generator = void 0 && (void 0).__generator || function (thisArg, body) {
  var _ = {
    label: 0,
    sent: function sent() {
      if (t[0] & 1) throw t[1];
      return t[1];
    },
    trys: [],
    ops: []
  },
      f,
      y,
      t,
      g;
  return g = {
    next: verb(0),
    "throw": verb(1),
    "return": verb(2)
  }, typeof _symbol["default"] === "function" && (g[_iterator["default"]] = function () {
    return this;
  }), g;

  function verb(n) {
    return function (v) {
      return step([n, v]);
    };
  }

  function step(op) {
    if (f) throw new TypeError("Generator is already executing.");

    while (_) {
      try {
        if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
        if (y = 0, t) op = [op[0] & 2, t.value];

        switch (op[0]) {
          case 0:
          case 1:
            t = op;
            break;

          case 4:
            _.label++;
            return {
              value: op[1],
              done: false
            };

          case 5:
            _.label++;
            y = op[1];
            op = [0];
            continue;

          case 7:
            op = _.ops.pop();

            _.trys.pop();

            continue;

          default:
            if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
              _ = 0;
              continue;
            }

            if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
              _.label = op[1];
              break;
            }

            if (op[0] === 6 && _.label < t[1]) {
              _.label = t[1];
              t = op;
              break;
            }

            if (t && _.label < t[2]) {
              _.label = t[2];

              _.ops.push(op);

              break;
            }

            if (t[2]) _.ops.pop();

            _.trys.pop();

            continue;
        }

        op = body.call(thisArg, _);
      } catch (e) {
        op = [6, e];
        y = 0;
      } finally {
        f = t = 0;
      }
    }

    if (op[0] & 5) throw op[1];
    return {
      value: op[0] ? op[1] : void 0,
      done: true
    };
  }
};

var __values = void 0 && (void 0).__values || function (o) {
  var s = typeof _symbol["default"] === "function" && _iterator["default"],
      m = s && o[s],
      i = 0;
  if (m) return m.call(o);
  if (o && typeof o.length === "number") return {
    next: function next() {
      if (o && i >= o.length) o = void 0;
      return {
        value: o && o[i++],
        done: !o
      };
    }
  };
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};

var __read = void 0 && (void 0).__read || function (o, n) {
  var m = typeof _symbol["default"] === "function" && o[_iterator["default"]];
  if (!m) return o;
  var i = m.call(o),
      r,
      ar = [],
      e;

  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done) {
      ar.push(r.value);
    }
  } catch (error) {
    e = {
      error: error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"])) m.call(i);
    } finally {
      if (e) throw e.error;
    }
  }

  return ar;
};

describe('api function test', function () {
  test('createLocalKey', function () {
    expect((0, _helper.createLocalKey)('test', null, 1024)).toMatch('qiniu_js_sdk_upload_file_name_test_size_1024');
    expect((0, _helper.createLocalKey)('test', 'demo', 1024)).toMatch('qiniu_js_sdk_upload_file_name_test_key_demo_size_1024');
  });
  test('computeMd5', function () {
    return __awaiter(void 0, void 0, void 0, function () {
      var testData, testData_1, testData_1_1, _a, input, expected, testBlob, actual, e_1_1;

      var e_1, _b;

      return __generator(this, function (_c) {
        switch (_c.label) {
          case 0:
            testData = [['message', '78e731027d8fd50ed642340b7c9a63b3'], ['undefined', '5e543256c480ac577d30f76f9120eb74'], ['message áßäöü', '3fc4229d4a54045f5d5b96dd759581d4']];
            _c.label = 1;

          case 1:
            _c.trys.push([1, 6, 7, 8]);

            testData_1 = __values(testData), testData_1_1 = testData_1.next();
            _c.label = 2;

          case 2:
            if (!!testData_1_1.done) return [3
            /*break*/
            , 5];
            _a = __read(testData_1_1.value, 2), input = _a[0], expected = _a[1];
            testBlob = new Blob([input], {
              type: 'text/plain;charset=utf-8'
            });
            return [4
            /*yield*/
            , (0, _helper.computeMd5)(testBlob)];

          case 3:
            actual = _c.sent();
            expect(actual).toStrictEqual(expected);
            _c.label = 4;

          case 4:
            testData_1_1 = testData_1.next();
            return [3
            /*break*/
            , 2];

          case 5:
            return [3
            /*break*/
            , 8];

          case 6:
            e_1_1 = _c.sent();
            e_1 = {
              error: e_1_1
            };
            return [3
            /*break*/
            , 8];

          case 7:
            try {
              if (testData_1_1 && !testData_1_1.done && (_b = testData_1["return"])) _b.call(testData_1);
            } finally {
              if (e_1) throw e_1.error;
            }

            return [7
            /*endfinally*/
            ];

          case 8:
            return [2
            /*return*/
            ];
        }
      });
    });
  });
  test('getPortFromUrl', function () {
    var e_2, _a;

    var testData = [['', ''], ['//loaclhost', ''], ['http://loaclhost', '80'], ['https://loaclhost', '443'], ['http://loaclhost:3030', '3030'], ['https://loaclhost:3030', '3030'], ['http://loaclhost:3030/path', '3030'], ['http://loaclhost:3030/path?test=3232', '3030'], ['http://loaclhost.com:3030/path?test=3232', '3030'], ['http://loaclhost.com.cn:3030/path?test=3232', '3030']];

    try {
      for (var testData_2 = __values(testData), testData_2_1 = testData_2.next(); !testData_2_1.done; testData_2_1 = testData_2.next()) {
        var _b = __read(testData_2_1.value, 2),
            input = _b[0],
            expected = _b[1];

        var actual = (0, _helper.getPortFromUrl)(input);
        expect(actual).toStrictEqual(expected);
      }
    } catch (e_2_1) {
      e_2 = {
        error: e_2_1
      };
    } finally {
      try {
        if (testData_2_1 && !testData_2_1.done && (_a = testData_2["return"])) _a.call(testData_2);
      } finally {
        if (e_2) throw e_2.error;
      }
    }
  });
});