"use strict";

var assert = require("assert");

var SolidBucket = require("../index");

var _require = require("./utils"),
  resolveUniversalApiWorkflow = _require.resolveUniversalApiWorkflow,
  simulateObjectCorruptionWorkflow = _require.simulateObjectCorruptionWorkflow,
  uploadBigFileWorkflow = _require.uploadBigFileWorkflow,
  simulateBigFileUploadErrorWorkflow =
    _require.simulateBigFileUploadErrorWorkflow,
  rejectMissingParametersWorkflow = _require.rejectMissingParametersWorkflow,
  rejectUniversalApiWorkflow = _require.rejectUniversalApiWorkflow;

var accountId = process.env.B2_ACCOUNTID;
var applicationKey = process.env.B2_APPLICATIONKEY;

describe("Backblaze B2", function() {
  this.timeout(15000);

  describe("Universal API", function() {
    it("should test the happy path", function() {
      var bucketName =
        String(
          Math.floor(Math.random() * 200000000) *
            Math.floor(Math.random() * 200000000)
        ) + "tests";
      var filePath = "lib/test/testdata/binary.bin";
      var filenameTxt = "generated-txt.txt";

      new SolidBucket("b2", {
        accountId: accountId,
        applicationKey: applicationKey
      })
        .then(function(provider) {
          resolveUniversalApiWorkflow(
            provider,
            bucketName,
            filePath,
            filenameTxt
          );
        })
        .catch(function(err) {
          console.log(err);
        });
    });

    it("should test against missing parameters", function() {
      var bucketName = "";
      var filename = "";

      new SolidBucket("b2", {
        accountId: accountId,
        applicationKey: applicationKey
      })
        .then(function(provider) {
          rejectMissingParametersWorkflow(provider, bucketName, filename);
        })
        .catch(function(err) {
          console.log(err);
        });
    });

    it("should test against missing auth options", function() {
      assert.throws(function() {
        new SolidBucket("b2", {
          accountId: "",
          applicationKey: ""
        });
      }, Error);
    });

    it("should test object upload corruptions", function() {
      var bucketName =
        String(
          Math.floor(Math.random() * 200000000) *
            Math.floor(Math.random() * 200000000)
        ) + "tests";
      var filename = "generated-during-tests.txt";

      new SolidBucket("b2", {
        accountId: accountId,
        applicationKey: applicationKey
      })
        .then(function(provider) {
          simulateObjectCorruptionWorkflow(provider, bucketName, filename);
        })
        .catch(function(err) {
          console.log(err);
        });
    });

    it("should test big file upload", function() {
      var bucketName =
        String(
          Math.floor(Math.random() * 200000000) *
            Math.floor(Math.random() * 200000000)
        ) + "tests";
      var filePath = "lib/test/testdata/bigbinary.bin";

      new SolidBucket("b2", {
        accountId: accountId,
        applicationKey: applicationKey
      })
        .then(function(provider) {
          uploadBigFileWorkflow(provider, bucketName, filePath);
        })
        .catch(function(err) {
          console.log(err);
        });
    });

    it("should test big file upload errors", function() {
      var bucketName =
        String(
          Math.floor(Math.random() * 200000000) *
            Math.floor(Math.random() * 200000000)
        ) + "tests";
      var filePath = "lib/test/testdata/bigbinary.bin";

      new SolidBucket("b2", {
        accountId: accountId,
        applicationKey: applicationKey
      })
        .then(function(provider) {
          simulateBigFileUploadErrorWorkflow(provider, bucketName, filePath);
        })
        .catch(function(err) {
          console.log(err);
        });
    });

    it("should test the provider rejections", function() {
      var bucketName = "backup";
      var filename = "generated-during-tests.txt";

      var provider = new SolidBucket("b2", {
        accountId: accountId,
        applicationKey: applicationKey
      })
        .then(function(provider) {
          rejectUniversalApiWorkflow(provider, bucketName, filename);
        })
        .catch(function(err) {
          console.log(err);
        });
    });
  });
});
