'use strict';

/**
 * 合同支付 模型 单元测试
 *
 * @author Mai
 * @date
 * @version
 */

const { app, assert } = require('egg-mock/bootstrap');
const addData = {
    name: 'test_pay',
    category: null,
};
const mockData = {};
let testPayId;
const _ = require('lodash');

describe('test/app/service/pay.test.js', () => {
    // 准备测试数据，新增测试标段
    before(function* () {
        const ctx = app.mockContext();
        // 模拟登录session
        const postData = {
            account: '734406061@qq.com',
            project: 'T201711273363',
            project_password: 'mai654321',
        };
        ctx.session = {};
        const loginResult = yield ctx.service.projectAccount.accountLogin(postData, 2);
        assert(loginResult);
        mockData.session = ctx.session;
        // 移除旧测试数据
        const testTender = yield ctx.service.tender.getDataByCondition({
            name: addData.name,
            project_id: ctx.session.sessionProject.id,
        });
        if (testTender) {
            const result = yield ctx.service.tender.deleteTenderNoBackup(testTender.id);
            assert(result);
        }
        // 新增测试用标段
        const result = yield ctx.service.tender.add(addData);
        assert(result);
        const tender = yield ctx.service.tender.getDataByCondition({
            name: addData.name,
            project_id: ctx.session.sessionProject.id,
        });
        mockData.tender = {id: tender.id, data: tender};
        ctx.tender = mockData.tender;
        // 新增测试期
        const stage = yield ctx.service.stage.addStage(mockData.tender.id, '2019-06', '2019-06-10 ~ 2019-06-30');
        assert(stage);
        mockData.stage = stage;
        mockData.stage.curTimes = 1;
        mockData.stage.curOrder = 0;
    });
    // 新增
    it('test add', function* () {
        const ctx = app.mockContext(mockData);

        const orgStagePays = yield ctx.service.stagePay.getStageLastestPays(ctx.stage.id);

        const result = yield ctx.service.pay.add();
        assert(result);
        testPayId = result.pid;

        const stagePays = yield ctx.service.stagePay.getStageLastestPays(ctx.stage.id);
        assert(stagePays.length === orgStagePays.length + 1);
    });
    // 删除
    it('test del', function* () {
        const ctx = app.mockContext(mockData);

        const orgStagePays = yield ctx.service.stagePay.getStageLastestPays(ctx.stage.id);

        const result = yield ctx.service.pay.del(testPayId);
        assert(result);

        const stagePays = yield ctx.service.stagePay.getStageLastestPays(ctx.stage.id);
        assert(stagePays.length === orgStagePays.length - 1);
    });
    // 调整顺序
    it('test changeOrder', function* () {
        const ctx = app.mockContext(mockData);

        const node1 = yield ctx.service.pay.add();
        assert(node1);
        const node2 = yield ctx.service.pay.add();
        assert(node2);

        const result = yield ctx.service.pay.changeOrder(node1.pid, node2.pid);
        assert(result);

        const stagePays = yield ctx.service.stagePay.getStageLastestPays(ctx.stage.id);
        const newNode1 = _.find(stagePays, {order: node1.order});
        assert(newNode1.pid === node2.pid);
        const newNode2 = _.find(stagePays, {order: node2.order});
        assert(newNode2.pid === node1.pid);

        testPayId = node1.pid;
    });
    // 名称/扣款项/起扣金额/扣款限额/计提期限/停用/启用 等单元测试在stagePay.test.js中测试
});