84 lines
2.4 KiB
JavaScript
84 lines
2.4 KiB
JavaScript
import { describe, it } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {
|
|
hasConfigSchema,
|
|
validateAppConfig,
|
|
resolveConfig,
|
|
flattenLeaves,
|
|
plainConfig,
|
|
} from '../models/app_config.js';
|
|
|
|
describe('validateAppConfig', () => {
|
|
it('接受已声明字段', () => {
|
|
const value = validateAppConfig('wxapp-escort', {
|
|
theme: 'dark',
|
|
notifications: { marketing: false },
|
|
});
|
|
assert.equal(value.theme, 'dark');
|
|
assert.equal(value.notifications.marketing, false);
|
|
});
|
|
|
|
it('拒绝未声明 key(不能借 config 改权限)', () => {
|
|
assert.throws(
|
|
() => validateAppConfig('wxapp-escort', { role: ['admin'] }),
|
|
/not allowed/
|
|
);
|
|
});
|
|
|
|
it('拒绝非法枚举值', () => {
|
|
assert.throws(() => validateAppConfig('wxapp-escort', { theme: 'blue' }));
|
|
});
|
|
|
|
it('未配置 schema 的应用抛错', () => {
|
|
assert.equal(hasConfigSchema('ttapp-escort'), false);
|
|
assert.throws(() => validateAppConfig('ttapp-escort', { theme: 'dark' }));
|
|
});
|
|
});
|
|
|
|
describe('resolveConfig', () => {
|
|
it('空配置返回默认值', () => {
|
|
assert.deepEqual(resolveConfig('wxapp-escort', {}), {
|
|
theme: 'system',
|
|
language: 'zh-CN',
|
|
notifications: { orderStatus: true, marketing: false },
|
|
});
|
|
});
|
|
|
|
it('嵌套对象做一层深合并', () => {
|
|
const config = resolveConfig('wxapp-escort', {
|
|
notifications: { marketing: true },
|
|
});
|
|
assert.equal(config.notifications.marketing, true);
|
|
assert.equal(config.notifications.orderStatus, true);
|
|
});
|
|
|
|
it('兼容 Map 存储', () => {
|
|
const config = resolveConfig('wxapp_companion', new Map([['pageSize', 50]]));
|
|
assert.equal(config.pageSize, 50);
|
|
assert.equal(config.soundAlert, true);
|
|
});
|
|
});
|
|
|
|
describe('flattenLeaves', () => {
|
|
it('展开嵌套对象为叶子路径', () => {
|
|
assert.deepEqual(flattenLeaves({ notifications: { marketing: false } }), {
|
|
'notifications.marketing': false,
|
|
});
|
|
});
|
|
|
|
it('数组和标量整体替换', () => {
|
|
assert.deepEqual(flattenLeaves({ list: [1, 2], theme: 'dark' }), {
|
|
list: [1, 2],
|
|
theme: 'dark',
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('plainConfig', () => {
|
|
it('Map 与普通对象都返回普通对象', () => {
|
|
assert.deepEqual(plainConfig(new Map([['a', 1]])), { a: 1 });
|
|
assert.deepEqual(plainConfig({ a: 1 }), { a: 1 });
|
|
assert.deepEqual(plainConfig(null), {});
|
|
});
|
|
});
|