'use strict';

/**
 * 辅助方法扩展单元测试
 *
 * @author CaiAoLin
 * @date 2017/10/24
 * @version
 */
const { app, assert } = require('egg-mock/bootstrap');

describe('test/app/extend/helper.test.js', () => {

    it('generateRandomString test', function* () {
        // 创建 ctx
        const ctx = app.mockContext();
        const randomMix = ctx.helper.generateRandomString(4);
        const mixReg = /[A-Za-z0-9]{4}/;
        assert(mixReg.test(randomMix));

        // 测试纯数字
        const randomNumber = ctx.helper.generateRandomString(4, 2);
        const numberReg = /\d{4}/;
        assert(numberReg.test(randomNumber));

        // 测试纯字母
        const randomString = ctx.helper.generateRandomString(4, 3);
        const stringReg = /[A-Za-z]{4}/;
        assert(stringReg.test(randomString));
    });

    it('showSortFlag test', function* () {
        // 创建 ctx
        const ctx = app.mockContext({
            // sort: { test: 1 },
            sort: ['id', 'asc'],
        });
        const flag = ctx.helper.showSortFlag('id');
        assert(flag === '-');
    });

    it('isAjax test', function* () {
        // 创建 ctx
        const ctx = app.mockContext();
        const result = ctx.helper.isAjax(ctx);
        assert(!result);
    });

    it('sendRequest test', function* () {
        // 创建 ctx
        const ctx = app.mockContext();
        const url = 'http://ip.taobao.com/service/getIpInfo.php';
        const responseData = yield ctx.helper.sendRequest(url, { ip: '116.7.222.12' }, 'GET');
        assert(responseData.data.ip === '116.7.222.12');
    });

    it('explode paths', function* () {
        // 创建 ctx
        const ctx = app.mockContext();
        const paths = ['1.2.3.4.5', '1.2.3.5.7'];
        const normal = ctx.helper.explodePath(paths, '.');
        const normalExpect = ['1', '1.2', '1.2.3', '1.2.3.4', '1.2.3.4.5', '1.2.3.5', '1.2.3.5.7'];
        let result = true;
        try {
            for (const index in normal) {
                if (normal[index] !== normalExpect[index]) {
                    throw '数据错误';
                }
            }
        } catch (error) {
            result = false;
        }
        assert(result);
    });

    it('test permission', function* () {
        // 创建 ctx
        const ctx = app.mockContext();
        ctx.session = {
            sessionUser: {
                permission: '1,2,3,4',
            },
        };
        const permission = require('../../../config/permission');
        let result = ctx.helper.hasPermission(permission.permission.VIEW_STAGE_MEASURE);
        assert(!result);

        // 多个权限判断
        result = ctx.helper.hasPermission([permission.permission.CREATE_TENDER, permission.permission.VIEW_ALL_TENDER]);
        assert(result);
    });

    it('test compareCode', function* () {
        const ctx = app.mockContext();
        // 测试项目节
        assert(ctx.helper.compareCode('1-1-1', '1-1-2') < 0);
        assert(ctx.helper.compareCode('1-2-2', '1-3-1') < 0);
        assert(ctx.helper.compareCode('1-2-2', '1-2-2-1') < 0);
        // 测试广东清单编号
        assert(ctx.helper.compareCode('404-1-1', '404-1-2') < 0);
        assert(ctx.helper.compareCode('404-1-2', '404-2-1') < 0);
        assert(ctx.helper.compareCode('404-1', '404-2-a') < 0);
        assert(ctx.helper.compareCode('404', '404-1') < 0);
        assert(ctx.helper.compareCode('404-1-b', '清单编号') < 0);
        // 测试全国清单编号
        assert(ctx.helper.compareCode('404-1-a', '404-1-b') < 0);
        assert(ctx.helper.compareCode('404-1-1', '404-1-a') < 0);
        assert(ctx.helper.compareCode('404-1-b', '404-2-a') < 0);
        assert(ctx.helper.compareCode('404-1-b', '404-a-a') < 0);
        // 测试日期
        assert(ctx.helper.compareCode('2018-03-04', '2018-03-05') < 0);
        assert(ctx.helper.compareCode('2018-02-09', '2018-03-06') < 0);
        assert(ctx.helper.compareCode('2017-05-15', '2018-03-05') < 0);
        // 测试时间
        assert(ctx.helper.compareCode('03:05:20', '03:05:24') < 0);
        assert(ctx.helper.compareCode('04:04:33', '04:05:33') < 0);
        assert(ctx.helper.compareCode('07:20:50', '08:20:50') < 0);
        // 测试层次编号
        assert(ctx.helper.compareCode('1.1.1', '1.1.2', '.') < 0);
        assert(ctx.helper.compareCode('1.1.3', '1.2.2', '.') < 0);
        assert(ctx.helper.compareCode('2.4.3', '3.1.2', '.') < 0);
    });
});
