Union of hero bots.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.2 KiB

/**
* Common公用方法测试用例
*/
import test from 'node:test';
import assert from 'node:assert';
import common from '../common.mjs';
test('Common function sortDict test', (t) => {
let params = {
b: 2,
a: 1
};
const expectRes = {
a: 1,
b: 2
};
assert.deepEqual(common.sortDict(params), expectRes);
});
test('Common function joinDict test', (t) => {
let params = {
b: 2,
a: 1
};
const expectRes = "a=1&b=2";
assert.strictEqual(common.joinDict(common.sortDict(params)), expectRes);
});
test('Common function getConfigFromJsonFile test', async (t) => {
let filename = 'config.json';
let config = await common.getConfigFromJsonFile(filename);
assert.ok(config);
const expectName = 'Hero Union';
assert.strictEqual(config.name, expectName);
});
test('Common function getLocalTimeString test', async (t) => {
let timeString = common.getLocalTimeString('zh-CN', 'Asia/Shanghai');
console.log('北京时间:%s', timeString);
assert.ok(timeString);
timeString = common.getLocalTimeString('zh-HK', 'UTC');
console.log('香港UTC时间:%s', timeString);
assert.ok(timeString);
});
test('Common function log/info/warn/error test', async (t) => {
let string = '测试log输出';
let args = common.log('console.log替换测试:%s', string);
assert.ok(args);
assert.equal(/^\[%s\] /i.test(args[0]), true);
assert.equal(args.length, 3);
assert.equal(args[args.length - 1], string);
args = common.info('console.info替换测试:%s', string);
assert.ok(args);
assert.equal(/^\[%s\] /i.test(args[0]), true);
assert.equal(args.length, 3);
assert.equal(args[args.length - 1], string);
args = common.warn('console.warn替换测试:%s', string);
assert.ok(args);
assert.equal(/^\[%s\] /i.test(args[0]), true);
assert.equal(args.length, 3);
assert.equal(args[args.length - 1], string);
args = common.error('console.error替换测试:%s', string);
assert.ok(args);
assert.equal(/^\[%s\] /i.test(args[0]), true);
assert.equal(args.length, 3);
assert.equal(args[args.length - 1], string);
console.log("插入日期后的参数:\n%s", args);
});