import { equal, notEqual, deepEqual,deepEql } from 'assert'
import axios from 'axios'
import moxios from 'moxios'
import sinon from 'sinon'
import ConfigFileService from '@/domain/services/ConfigFileService'
import {
  CONFIG_FILRKEY,
  RESTORY_TO_DEVICEFLAG,
  DEVICEKEYLIST,
  CONFIG_FILRKEY_LIST,
  CONFIGFILE_UPLOAD_OUTPUT,CONFIGFILE_DELETE_OUTPUT,CONFIGFILE_DOWNLOAD_OUTPUT,
  CONFIGFILE_UPLOAD_INPUT,CONFIGFILE_DELETE_INPUT,CONFIGFILE_DOWNLOAD_INPUT,
  GETALL_CONFIGFILE_INPUT,GETALL_CONFIGFILE_OUTPUT,
} from "../../../mock/deviceState/ConfigFileMockService";

describe('moxios', function () {
  it('should install', function () {
    let defaultAdapter = axios.defaults.adapter
    moxios.install()
    notEqual(axios.defaults.adapter, defaultAdapter)
    moxios.uninstall()
  })

  it('should uninstall', function () {
    let defaultAdapter = axios.defaults.adapter
    moxios.install()
    moxios.uninstall()
    equal(axios.defaults.adapter, defaultAdapter)
  })

  describe('ConfigFileService (Rest)', function () {
    let onFulfilled
    let onRejected

    beforeEach(function () {
      moxios.install()
      // sinon.js 中 spy 主要用来监视函数的调用情况，
      // sinon 对待监视的函数进行 wrap 包装，
      // 因此可以通过它清楚的知道，该函数被调用过几次，传入什么参数返回什么结果，甚至是抛出的异常情况。
      onFulfilled = sinon.spy()
      onRejected = sinon.spy()
    })

    afterEach(function () {
      moxios.uninstall()
    })

    // 拦截请求
    it('should intercept requests', function (done) {
      ConfigFileService.getAllConfigFileCollection();
      ConfigFileService.downloadConfigToDevice(CONFIG_FILRKEY, RESTORY_TO_DEVICEFLAG, DEVICEKEYLIST);
      ConfigFileService.batchUploadConfigFile(DEVICEKEYLIST);
      ConfigFileService.batchDeleteConfigFile(CONFIG_FILRKEY_LIST);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        // console.log(moxios.requests);
        equal(moxios.requests.count(), 4);
        done()
      })
    });

    //模拟响应
    it('should getAllConfigFileCollection', function (done) {
      ConfigFileService.getAllConfigFileCollection().then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: GETALL_CONFIGFILE_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          // console.log(response);
          deepEqual(response, GETALL_CONFIGFILE_OUTPUT);
          done()
        })
      })
    });

    it('should downloadConfigToDevice', function (done) {
      ConfigFileService.downloadConfigToDevice(CONFIG_FILRKEY, RESTORY_TO_DEVICEFLAG, DEVICEKEYLIST).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: CONFIGFILE_DOWNLOAD_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          //console.log(response);
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          deepEqual(response, CONFIGFILE_DOWNLOAD_OUTPUT);
          done()
        })
      })
    });

    it('should batchUploadConfigFile', function (done) {
      ConfigFileService.batchUploadConfigFile(DEVICEKEYLIST).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: CONFIGFILE_UPLOAD_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          //console.log(response);
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          deepEqual(response, CONFIGFILE_UPLOAD_OUTPUT);
          done()
        })
      })
    });

    it('should batchDeleteConfigFile', function (done) {
      ConfigFileService.batchDeleteConfigFile(CONFIG_FILRKEY_LIST).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: CONFIGFILE_DELETE_INPUT
        }).then(function () {
          console.log('123',onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          //console.log(response);
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          deepEqual(response, CONFIGFILE_DELETE_OUTPUT);
          done()
        })
      })
    });
  })
});
