Files
api_user/test/app_config.test.js
lik efd4e4444c refactor(app): appId wxapp-escort-admin 更名为 wxapp_companion
与陪诊师小程序工程名对齐;同步注册表、配置白名单、脚本示例与测试。数据库 user_app / user_identity / user.app 旧字段已同步迁移。
2026-11-10 16:33:35 +08:00

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), {});
});
});