import { equal, notEqual, deepEqual,deepEql } from 'assert'
import axios from 'axios'
import moxios from 'moxios'
import sinon from 'sinon'
import DeviceService from '@/domain/services/DeviceService'
import {
  DEVICE_ADD_CLIENT,
  DEVICE_ADD_INPUT,
  DEVICE_ADD_OUTPUT,
  DEVICE_DELETE_CLIENT,
  DEVICE_DELETE_INPUT,
  DEVICE_DELETE_OUTPUT,
  DEVICE_GETALL_INPUT,
  DEVICE_GETALL_OUTPUT,
  DEVICE_MODIFY_CLIENT,
  DEVICE_MODIFY_INPUT,
  DEVICE_MODIFY_OUTPUT,
  DEVICE_QUERY_CLIENT,
  DEVICE_QUERY_INPUT, DEVICE_QUERY_OUTPUT
} from "../../../mock/deviceState/DevcieServiceMock";

describe('moxios', function () {
  this.timeout(5000);
  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('DeviceService (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) {
      DeviceService.getAllDeviceCollection();
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        // console.log(moxios.requests);
        equal(moxios.requests.count(), 1);
        done()
      })
    });

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

    it('should addDevice', function (done) {
      DeviceService.addDevice(DEVICE_ADD_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: DEVICE_ADD_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          deepEqual(response, DEVICE_ADD_OUTPUT);
          done()
        })
      })
    });

    it('should batchDeleteDevice', function (done) {
      DeviceService.batchDeleteDevice(DEVICE_DELETE_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: DEVICE_DELETE_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          deepEqual(response, DEVICE_DELETE_OUTPUT);
          done()
        })
      })
    });

    it('should modifyDevice', function (done) {
      DeviceService.modifyDevice(DEVICE_MODIFY_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: DEVICE_MODIFY_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          deepEqual(response, DEVICE_MODIFY_OUTPUT);
          done()
        })
      })
    });

    it('should conditionQueryDevice', function (done) {

      let res=DeviceService.conditionQueryDevice(DEVICE_QUERY_CLIENT).then(onFulfilled);

      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: DEVICE_QUERY_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          //let response = onFulfilled.getCall(0);
          //equal(onFulfilled.called, true);
          // equal(response.status, 200);
          deepEqual(res, DEVICE_QUERY_OUTPUT);
          done()
        })
      })
      done()
    });
  })
});

// describe('DeviceService (Websocket)', () => {
//   it('should subscribeAddDevice', () => {
//     const fakeURL = 'ws://localhost:8080'
//     const mockServer = new Server(fakeURL)
//
//     let addDeviceMsg = {
//       objectType: 0,
//       objectOption: 1,
//       returnObj: {
//         deviceKey: '123',
//         deviceName: 'device1'
//       }
//     }
//
//     mockServer.on('connection', socket => {
//       socket.on('message', data => {
//         socket.send(JSON.stringify(addDeviceMsg))
//       })
//     })
//
//     let devices = {}
//     let subnets = {}
//     let topologys = {}
//
//     let ws = WebSocketService(DeviceStateSocketApi, {URL: fakeURL})
//     let deviceSvc = DeviceService(ws)
//     deviceSvc.subscribeAddDevice(devices, subnets, topologys)
//
//     ws.open()
//     setTimeout(() => {
//       mockServer.stop()
//     }, 100)
//
//     console.log('device is :', devices)
//   })
// })
