import { equal, notEqual, deepEqual,deepEql } from 'assert'
import axios from 'axios'
import moxios from 'moxios'
import sinon from 'sinon'
import PortListService from '@/domain/services/PortListService'
import {
  PORT_DELETE_CLIENT, PORT_DELETE_INPUT, PORT_DELETE_OUTPUT,
  PORT_QUERY_CLIENT,
  PORT_QUERY_INPUT,
  PORT_QUERY_OUTPUT
} from "../../../mock/deviceState/PortListServiceMock";
import PerformanceDataQuaryService from "../../../../src/domain/services/PerformanceDataQuaryService";
import {
  PAGING_END, PAGING_INPUT, PAGING_OUTPUT, PAGING_SELECTLIST,
  PAGING_START,
  PERF_TOTAL_CLIENT,
  PERF_TOTAL_INPUT,
  PERF_TOTAL_OUTPUT
} from "../../../mock/deviceState/PerformanceDataQuaryServiceMock";
import PerformanceMonitorService from "../../../../src/domain/services/PerformanceMonitorService";
import {
  MONI_GETALL_INPUT,
  MONI_GETALL_OUTPUT,
  MONI_MODI_CLIENT
} from "../../../mock/deviceState/PerformanceMonitorServiceMock";
import PerformanceStatisticService from "../../../../src/domain/services/PerformanceStatisticService";
import {
  PSS_CONDITION_CLIENT,
  PSS_INPUT, PSS_OUTPUT,
  PSS_VAL_CLIENT
} from "../../../mock/deviceState/PerformanceStatisticServiceMock";



describe('PerformanceStatisticService_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('PerformanceStatisticService (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) {
      PerformanceStatisticService.queryPerformStatisticsData(PSS_VAL_CLIENT,PSS_CONDITION_CLIENT);

      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        // console.log(moxios.requests);
        equal(moxios.requests.count(), 1);
        done()
      })
    });

    // 模拟响应
    it('should queryPerformStatisticsData', function (done) {
      PerformanceStatisticService.queryPerformStatisticsData(PSS_VAL_CLIENT,PSS_CONDITION_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: PSS_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, PSS_OUTPUT);
          done()
        })
      })
    });

  })
});
