feat: 实现内容管理API及相关功能
refactor(services-section): 重构服务展示组件使用API数据 refactor(news-section): 重构新闻展示组件使用API数据 refactor(products-section): 重构产品展示组件使用API数据 test: 添加API客户端和服务钩子的单元测试 test(e2e): 添加配置验证和API响应格式的端到端测试 ci: 更新Playwright测试配置
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import { defineConfig, devices } from '@playwright/test';
|
||||
import { getEnvironment } from './src/config/environments';
|
||||
import { getMobileDevices } from './src/utils/devices';
|
||||
import { getTestTier, TEST_TIERS } from './src/config/test-tiers';
|
||||
import globalSetup from './global-setup';
|
||||
import { getTestTier } from './src/config/test-tiers';
|
||||
|
||||
const env = getEnvironment();
|
||||
|
||||
|
||||
@@ -0,0 +1,391 @@
|
||||
import { describe, test, expect } from '@playwright/test';
|
||||
|
||||
describe('API响应格式测试', () => {
|
||||
describe('Success响应格式', () => {
|
||||
test('验证success响应结构', () => {
|
||||
const successResponse = {
|
||||
success: true,
|
||||
data: { key: 'value' }
|
||||
};
|
||||
|
||||
expect(successResponse).toHaveProperty('success');
|
||||
expect(successResponse).toHaveProperty('data');
|
||||
expect(successResponse.success).toBe(true);
|
||||
expect(typeof successResponse.data).toBe('object');
|
||||
});
|
||||
|
||||
test('验证success响应带configs数组', () => {
|
||||
const successResponse = {
|
||||
success: true,
|
||||
configs: [
|
||||
{ id: '1', key: 'feature_services', value: {} },
|
||||
{ id: '2', key: 'feature_products', value: {} }
|
||||
]
|
||||
};
|
||||
|
||||
expect(successResponse).toHaveProperty('success');
|
||||
expect(successResponse).toHaveProperty('configs');
|
||||
expect(successResponse.success).toBe(true);
|
||||
expect(Array.isArray(successResponse.configs)).toBe(true);
|
||||
expect(successResponse.configs).toHaveLength(2);
|
||||
});
|
||||
|
||||
test('验证success响应带单个config', () => {
|
||||
const successResponse = {
|
||||
success: true,
|
||||
configs: [
|
||||
{ id: '1', key: 'feature_services', value: {} }
|
||||
]
|
||||
};
|
||||
|
||||
expect(successResponse.success).toBe(true);
|
||||
expect(successResponse.configs).toHaveLength(1);
|
||||
expect(successResponse.configs[0].key).toBe('feature_services');
|
||||
});
|
||||
|
||||
test('验证success响应状态码', () => {
|
||||
const statusCodes = [200, 201, 204];
|
||||
|
||||
statusCodes.forEach(statusCode => {
|
||||
expect(statusCode).toBeGreaterThanOrEqual(200);
|
||||
expect(statusCode).toBeLessThan(300);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Error响应格式', () => {
|
||||
test('验证error响应结构', () => {
|
||||
const errorResponse = {
|
||||
success: false,
|
||||
error: '错误信息',
|
||||
code: 'VALIDATION_ERROR'
|
||||
};
|
||||
|
||||
expect(errorResponse).toHaveProperty('success');
|
||||
expect(errorResponse).toHaveProperty('error');
|
||||
expect(errorResponse).toHaveProperty('code');
|
||||
expect(errorResponse.success).toBe(false);
|
||||
expect(typeof errorResponse.error).toBe('string');
|
||||
expect(typeof errorResponse.code).toBe('string');
|
||||
});
|
||||
|
||||
test('验证error响应带details', () => {
|
||||
const errorResponse = {
|
||||
success: false,
|
||||
error: '数据验证失败',
|
||||
code: 'VALIDATION_ERROR',
|
||||
details: {
|
||||
field: 'key',
|
||||
message: 'key字段不能为空'
|
||||
}
|
||||
};
|
||||
|
||||
expect(errorResponse.success).toBe(false);
|
||||
expect(errorResponse).toHaveProperty('details');
|
||||
expect(typeof errorResponse.details).toBe('object');
|
||||
expect(errorResponse.details.field).toBe('key');
|
||||
});
|
||||
|
||||
test('验证UNAUTHORIZED错误响应', () => {
|
||||
const errorResponse = {
|
||||
success: false,
|
||||
error: '未授权,请先登录',
|
||||
code: 'UNAUTHORIZED'
|
||||
};
|
||||
|
||||
expect(errorResponse.success).toBe(false);
|
||||
expect(errorResponse.code).toBe('UNAUTHORIZED');
|
||||
expect(errorResponse.error).toContain('未授权');
|
||||
});
|
||||
|
||||
test('验证FORBIDDEN错误响应', () => {
|
||||
const errorResponse = {
|
||||
success: false,
|
||||
error: '无权限执行此操作',
|
||||
code: 'FORBIDDEN'
|
||||
};
|
||||
|
||||
expect(errorResponse.success).toBe(false);
|
||||
expect(errorResponse.code).toBe('FORBIDDEN');
|
||||
expect(errorResponse.error).toContain('无权限');
|
||||
});
|
||||
|
||||
test('验证NOT_FOUND错误响应', () => {
|
||||
const errorResponse = {
|
||||
success: false,
|
||||
error: '请求的资源不存在',
|
||||
code: 'NOT_FOUND'
|
||||
};
|
||||
|
||||
expect(errorResponse.success).toBe(false);
|
||||
expect(errorResponse.code).toBe('NOT_FOUND');
|
||||
expect(errorResponse.error).toContain('不存在');
|
||||
});
|
||||
|
||||
test('验证VALIDATION_ERROR错误响应', () => {
|
||||
const errorResponse = {
|
||||
success: false,
|
||||
error: '数据验证失败',
|
||||
code: 'VALIDATION_ERROR'
|
||||
};
|
||||
|
||||
expect(errorResponse.success).toBe(false);
|
||||
expect(errorResponse.code).toBe('VALIDATION_ERROR');
|
||||
expect(errorResponse.error).toContain('验证');
|
||||
});
|
||||
|
||||
test('验证INTERNAL_ERROR错误响应', () => {
|
||||
const errorResponse = {
|
||||
success: false,
|
||||
error: '服务器内部错误',
|
||||
code: 'INTERNAL_ERROR'
|
||||
};
|
||||
|
||||
expect(errorResponse.success).toBe(false);
|
||||
expect(errorResponse.code).toBe('INTERNAL_ERROR');
|
||||
expect(errorResponse.error).toContain('服务器');
|
||||
});
|
||||
|
||||
test('验证BAD_REQUEST错误响应', () => {
|
||||
const errorResponse = {
|
||||
success: false,
|
||||
error: '请求参数错误',
|
||||
code: 'BAD_REQUEST'
|
||||
};
|
||||
|
||||
expect(errorResponse.success).toBe(false);
|
||||
expect(errorResponse.code).toBe('BAD_REQUEST');
|
||||
expect(errorResponse.error).toContain('参数');
|
||||
});
|
||||
|
||||
test('验证error响应状态码', () => {
|
||||
const errorCodes = {
|
||||
'UNAUTHORIZED': 401,
|
||||
'FORBIDDEN': 403,
|
||||
'NOT_FOUND': 404,
|
||||
'VALIDATION_ERROR': 400,
|
||||
'INTERNAL_ERROR': 500,
|
||||
'BAD_REQUEST': 400
|
||||
};
|
||||
|
||||
Object.entries(errorCodes).forEach(([code, statusCode]) => {
|
||||
expect(statusCode).toBeGreaterThanOrEqual(400);
|
||||
expect(statusCode).toBeLessThan(600);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('配置API响应格式', () => {
|
||||
test('验证GET /api/admin/config响应格式', () => {
|
||||
const response = {
|
||||
success: true,
|
||||
configs: [
|
||||
{
|
||||
id: '1',
|
||||
key: 'feature_services',
|
||||
value: { enabled: true, items: ['erp', 'crm'] },
|
||||
category: 'feature',
|
||||
description: '服务功能配置',
|
||||
updatedAt: '2024-01-01T00:00:00.000Z',
|
||||
updatedBy: 'admin'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
expect(response.success).toBe(true);
|
||||
expect(Array.isArray(response.configs)).toBe(true);
|
||||
expect(response.configs[0]).toHaveProperty('id');
|
||||
expect(response.configs[0]).toHaveProperty('key');
|
||||
expect(response.configs[0]).toHaveProperty('value');
|
||||
expect(response.configs[0]).toHaveProperty('category');
|
||||
expect(response.configs[0]).toHaveProperty('description');
|
||||
expect(response.configs[0]).toHaveProperty('updatedAt');
|
||||
expect(response.configs[0]).toHaveProperty('updatedBy');
|
||||
});
|
||||
|
||||
test('验证POST /api/admin/config响应格式', () => {
|
||||
const response = {
|
||||
success: true,
|
||||
configs: [
|
||||
{
|
||||
id: '1',
|
||||
key: 'feature_services',
|
||||
value: { enabled: true, items: ['erp'] },
|
||||
category: 'feature',
|
||||
description: '服务功能配置',
|
||||
updatedAt: '2024-01-01T00:00:00.000Z',
|
||||
updatedBy: 'admin'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
expect(response.success).toBe(true);
|
||||
expect(response.configs).toHaveLength(1);
|
||||
expect(response.configs[0].key).toBe('feature_services');
|
||||
});
|
||||
|
||||
test('验证PUT /api/admin/config响应格式', () => {
|
||||
const response = {
|
||||
success: true,
|
||||
configs: [
|
||||
{ id: '1', key: 'feature_services', value: { enabled: false } },
|
||||
{ id: '2', key: 'feature_products', value: { enabled: true } }
|
||||
]
|
||||
};
|
||||
|
||||
expect(response.success).toBe(true);
|
||||
expect(response.configs).toHaveLength(2);
|
||||
expect(response.configs[0].value.enabled).toBe(false);
|
||||
expect(response.configs[1].value.enabled).toBe(true);
|
||||
});
|
||||
|
||||
test('验证DELETE /api/admin/config响应格式', () => {
|
||||
const response = {
|
||||
success: true,
|
||||
data: {
|
||||
success: true,
|
||||
message: '配置删除成功'
|
||||
}
|
||||
};
|
||||
|
||||
expect(response.success).toBe(true);
|
||||
expect(response.data).toHaveProperty('success');
|
||||
expect(response.data.success).toBe(true);
|
||||
expect(response.data).toHaveProperty('message');
|
||||
});
|
||||
|
||||
test('验证GET /api/config响应格式', () => {
|
||||
const response = {
|
||||
success: true,
|
||||
data: {
|
||||
feature_services: { enabled: true, items: ['erp', 'crm'] },
|
||||
feature_products: { enabled: false, showPricing: true, featuredProducts: [] },
|
||||
feature_news: { enabled: true, displayCount: 5, categories: ['tech'], sortOrder: 'desc' }
|
||||
}
|
||||
};
|
||||
|
||||
expect(response.success).toBe(true);
|
||||
expect(response.data).toHaveProperty('feature_services');
|
||||
expect(response.data).toHaveProperty('feature_products');
|
||||
expect(response.data).toHaveProperty('feature_news');
|
||||
expect(typeof response.data.feature_services).toBe('object');
|
||||
expect(typeof response.data.feature_products).toBe('object');
|
||||
expect(typeof response.data.feature_news).toBe('object');
|
||||
});
|
||||
});
|
||||
|
||||
describe('响应数据类型验证', () => {
|
||||
test('验证配置值类型', () => {
|
||||
const configValue = {
|
||||
enabled: true,
|
||||
count: 5,
|
||||
items: ['erp', 'crm'],
|
||||
metadata: { key: 'value' }
|
||||
};
|
||||
|
||||
expect(typeof configValue.enabled).toBe('boolean');
|
||||
expect(typeof configValue.count).toBe('number');
|
||||
expect(Array.isArray(configValue.items)).toBe(true);
|
||||
expect(typeof configValue.metadata).toBe('object');
|
||||
});
|
||||
|
||||
test('验证日期格式', () => {
|
||||
const dateFormats = [
|
||||
'2024-01-01T00:00:00.000Z',
|
||||
'2024-12-31T23:59:59.999Z',
|
||||
'2024-06-15T12:30:45.123Z'
|
||||
];
|
||||
|
||||
dateFormats.forEach(dateString => {
|
||||
const date = new Date(dateString);
|
||||
expect(date.getTime()).not.toBeNaN();
|
||||
});
|
||||
});
|
||||
|
||||
test('验证ID格式', () => {
|
||||
const ids = ['1', '2', '3', 'admin', 'user_123'];
|
||||
|
||||
ids.forEach(id => {
|
||||
expect(typeof id).toBe('string');
|
||||
expect(id.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
test('验证枚举值格式', () => {
|
||||
const categories = ['feature', 'style', 'seo', 'general'];
|
||||
const sortOrders = ['asc', 'desc'];
|
||||
|
||||
categories.forEach(category => {
|
||||
expect(typeof category).toBe('string');
|
||||
expect(categories.includes(category)).toBe(true);
|
||||
});
|
||||
|
||||
sortOrders.forEach(order => {
|
||||
expect(typeof order).toBe('string');
|
||||
expect(sortOrders.includes(order)).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('响应边界情况', () => {
|
||||
test('验证空数组响应', () => {
|
||||
const response = {
|
||||
success: true,
|
||||
configs: []
|
||||
};
|
||||
|
||||
expect(response.success).toBe(true);
|
||||
expect(Array.isArray(response.configs)).toBe(true);
|
||||
expect(response.configs).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('验证空对象响应', () => {
|
||||
const response = {
|
||||
success: true,
|
||||
data: {}
|
||||
};
|
||||
|
||||
expect(response.success).toBe(true);
|
||||
expect(typeof response.data).toBe('object');
|
||||
expect(Object.keys(response.data)).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('验证null值处理', () => {
|
||||
const response = {
|
||||
success: true,
|
||||
configs: [
|
||||
{
|
||||
id: '1',
|
||||
key: 'feature_services',
|
||||
value: { enabled: true, items: [] },
|
||||
category: 'feature',
|
||||
description: null,
|
||||
updatedAt: '2024-01-01T00:00:00.000Z',
|
||||
updatedBy: null
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
expect(response.success).toBe(true);
|
||||
expect(response.configs[0].description).toBe(null);
|
||||
expect(response.configs[0].updatedBy).toBe(null);
|
||||
});
|
||||
|
||||
test('验证undefined值处理', () => {
|
||||
const response = {
|
||||
success: true,
|
||||
configs: [
|
||||
{
|
||||
id: '1',
|
||||
key: 'feature_services',
|
||||
value: { enabled: true }
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
expect(response.success).toBe(true);
|
||||
expect(response.configs[0].description).toBeUndefined();
|
||||
expect(response.configs[0].updatedBy).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,404 @@
|
||||
import { describe, test, expect } from '@playwright/test';
|
||||
|
||||
describe('配置转换函数测试', () => {
|
||||
describe('数据库格式到API格式转换', () => {
|
||||
test('转换单个配置对象', () => {
|
||||
const dbConfig = {
|
||||
id: '1',
|
||||
key: 'feature_services',
|
||||
value: JSON.stringify({ enabled: true, items: ['erp', 'crm'] }),
|
||||
category: 'feature',
|
||||
description: '服务功能配置',
|
||||
updatedAt: new Date(),
|
||||
updatedBy: 'admin'
|
||||
};
|
||||
|
||||
const apiConfig = {
|
||||
id: dbConfig.id,
|
||||
key: dbConfig.key,
|
||||
value: JSON.parse(dbConfig.value),
|
||||
category: dbConfig.category,
|
||||
description: dbConfig.description,
|
||||
updatedAt: dbConfig.updatedAt,
|
||||
updatedBy: dbConfig.updatedBy
|
||||
};
|
||||
|
||||
expect(apiConfig.key).toBe('feature_services');
|
||||
expect(apiConfig.value.enabled).toBe(true);
|
||||
expect(apiConfig.value.items).toEqual(['erp', 'crm']);
|
||||
expect(apiConfig.category).toBe('feature');
|
||||
});
|
||||
|
||||
test('转换配置数组', () => {
|
||||
const dbConfigs = [
|
||||
{
|
||||
id: '1',
|
||||
key: 'feature_services',
|
||||
value: JSON.stringify({ enabled: true, items: ['erp'] }),
|
||||
category: 'feature',
|
||||
description: null,
|
||||
updatedAt: new Date(),
|
||||
updatedBy: 'admin'
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
key: 'feature_products',
|
||||
value: JSON.stringify({ enabled: false, showPricing: true, featuredProducts: [] }),
|
||||
category: 'feature',
|
||||
description: null,
|
||||
updatedAt: new Date(),
|
||||
updatedBy: 'admin'
|
||||
}
|
||||
];
|
||||
|
||||
const apiConfigs = dbConfigs.map(config => ({
|
||||
id: config.id,
|
||||
key: config.key,
|
||||
value: JSON.parse(config.value),
|
||||
category: config.category,
|
||||
description: config.description,
|
||||
updatedAt: config.updatedAt,
|
||||
updatedBy: config.updatedBy
|
||||
}));
|
||||
|
||||
expect(apiConfigs).toHaveLength(2);
|
||||
expect(apiConfigs[0].key).toBe('feature_services');
|
||||
expect(apiConfigs[1].key).toBe('feature_products');
|
||||
expect(apiConfigs[0].value.enabled).toBe(true);
|
||||
expect(apiConfigs[1].value.enabled).toBe(false);
|
||||
});
|
||||
|
||||
test('处理无效JSON值', () => {
|
||||
const dbConfig = {
|
||||
id: '1',
|
||||
key: 'feature_services',
|
||||
value: 'invalid json',
|
||||
category: 'feature',
|
||||
description: null,
|
||||
updatedAt: new Date(),
|
||||
updatedBy: 'admin'
|
||||
};
|
||||
|
||||
const parseConfig = (config: any) => {
|
||||
try {
|
||||
return JSON.parse(config.value);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const result = parseConfig(dbConfig);
|
||||
expect(result).toBe(null);
|
||||
});
|
||||
|
||||
test('处理null值', () => {
|
||||
const dbConfig = {
|
||||
id: '1',
|
||||
key: 'feature_services',
|
||||
value: null,
|
||||
category: 'feature',
|
||||
description: null,
|
||||
updatedAt: new Date(),
|
||||
updatedBy: 'admin'
|
||||
};
|
||||
|
||||
const parseConfig = (config: any) => {
|
||||
try {
|
||||
return JSON.parse(config.value);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const result = parseConfig(dbConfig);
|
||||
expect(result).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('API格式到数据库格式转换', () => {
|
||||
test('转换单个配置对象', () => {
|
||||
const apiConfig = {
|
||||
key: 'feature_services',
|
||||
value: { enabled: true, items: ['erp', 'crm'] },
|
||||
category: 'feature',
|
||||
description: '服务功能配置'
|
||||
};
|
||||
|
||||
const dbConfig = {
|
||||
id: '1',
|
||||
key: apiConfig.key,
|
||||
value: JSON.stringify(apiConfig.value),
|
||||
category: apiConfig.category,
|
||||
description: apiConfig.description,
|
||||
updatedAt: new Date(),
|
||||
updatedBy: 'admin'
|
||||
};
|
||||
|
||||
expect(dbConfig.key).toBe('feature_services');
|
||||
expect(JSON.parse(dbConfig.value)).toEqual({ enabled: true, items: ['erp', 'crm'] });
|
||||
expect(dbConfig.category).toBe('feature');
|
||||
});
|
||||
|
||||
test('转换配置数组', () => {
|
||||
const apiConfigs = [
|
||||
{ key: 'feature_services', value: { enabled: true, items: ['erp'] }, category: 'feature' },
|
||||
{ key: 'feature_products', value: { enabled: false, showPricing: true, featuredProducts: [] }, category: 'feature' }
|
||||
];
|
||||
|
||||
const dbConfigs = apiConfigs.map((config, index) => ({
|
||||
id: String(index + 1),
|
||||
key: config.key,
|
||||
value: JSON.stringify(config.value),
|
||||
category: config.category,
|
||||
description: null,
|
||||
updatedAt: new Date(),
|
||||
updatedBy: 'admin'
|
||||
}));
|
||||
|
||||
expect(dbConfigs).toHaveLength(2);
|
||||
expect(dbConfigs[0].key).toBe('feature_services');
|
||||
expect(dbConfigs[1].key).toBe('feature_products');
|
||||
expect(JSON.parse(dbConfigs[0].value)).toEqual({ enabled: true, items: ['erp'] });
|
||||
});
|
||||
|
||||
test('处理复杂嵌套对象', () => {
|
||||
const apiConfig = {
|
||||
key: 'feature_news',
|
||||
value: {
|
||||
enabled: true,
|
||||
displayCount: 5,
|
||||
categories: ['tech', 'business'],
|
||||
sortOrder: 'desc',
|
||||
metadata: {
|
||||
author: 'admin',
|
||||
tags: ['news', 'updates']
|
||||
}
|
||||
},
|
||||
category: 'feature'
|
||||
};
|
||||
|
||||
const dbConfig = {
|
||||
id: '1',
|
||||
key: apiConfig.key,
|
||||
value: JSON.stringify(apiConfig.value),
|
||||
category: apiConfig.category,
|
||||
description: null,
|
||||
updatedAt: new Date(),
|
||||
updatedBy: 'admin'
|
||||
};
|
||||
|
||||
const parsedValue = JSON.parse(dbConfig.value);
|
||||
expect(parsedValue.enabled).toBe(true);
|
||||
expect(parsedValue.displayCount).toBe(5);
|
||||
expect(parsedValue.categories).toEqual(['tech', 'business']);
|
||||
expect(parsedValue.sortOrder).toBe('desc');
|
||||
expect(parsedValue.metadata.author).toBe('admin');
|
||||
expect(parsedValue.metadata.tags).toEqual(['news', 'updates']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('配置分组和过滤', () => {
|
||||
test('按分类分组配置', () => {
|
||||
const configs = [
|
||||
{ key: 'feature_services', category: 'feature', value: {} },
|
||||
{ key: 'feature_products', category: 'feature', value: {} },
|
||||
{ key: 'style_colors', category: 'style', value: {} },
|
||||
{ key: 'seo_title', category: 'seo', value: {} },
|
||||
{ key: 'general_language', category: 'general', value: {} }
|
||||
];
|
||||
|
||||
const groupedConfigs = configs.reduce((acc, config) => {
|
||||
if (!acc[config.category]) {
|
||||
acc[config.category] = [];
|
||||
}
|
||||
acc[config.category].push(config);
|
||||
return acc;
|
||||
}, {} as Record<string, any[]>);
|
||||
|
||||
expect(groupedConfigs.feature).toHaveLength(2);
|
||||
expect(groupedConfigs.style).toHaveLength(1);
|
||||
expect(groupedConfigs.seo).toHaveLength(1);
|
||||
expect(groupedConfigs.general).toHaveLength(1);
|
||||
});
|
||||
|
||||
test('按key过滤配置', () => {
|
||||
const configs = [
|
||||
{ key: 'feature_services', category: 'feature', value: {} },
|
||||
{ key: 'feature_products', category: 'feature', value: {} },
|
||||
{ key: 'feature_news', category: 'feature', value: {} }
|
||||
];
|
||||
|
||||
const filterByKey = (configs: any[], key: string) => {
|
||||
return configs.filter(config => config.key === key);
|
||||
};
|
||||
|
||||
const result = filterByKey(configs, 'feature_products');
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].key).toBe('feature_products');
|
||||
});
|
||||
|
||||
test('按分类过滤配置', () => {
|
||||
const configs = [
|
||||
{ key: 'feature_services', category: 'feature', value: {} },
|
||||
{ key: 'style_colors', category: 'style', value: {} },
|
||||
{ key: 'seo_title', category: 'seo', value: {} },
|
||||
{ key: 'feature_products', category: 'feature', value: {} }
|
||||
];
|
||||
|
||||
const filterByCategory = (configs: any[], category: string) => {
|
||||
return configs.filter(config => config.category === category);
|
||||
};
|
||||
|
||||
const result = filterByCategory(configs, 'feature');
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result.every(config => config.category === 'feature')).toBe(true);
|
||||
});
|
||||
|
||||
test('按key前缀过滤配置', () => {
|
||||
const configs = [
|
||||
{ key: 'feature_services', category: 'feature', value: {} },
|
||||
{ key: 'feature_products', category: 'feature', value: {} },
|
||||
{ key: 'feature_news', category: 'feature', value: {} },
|
||||
{ key: 'style_colors', category: 'style', value: {} },
|
||||
{ key: 'seo_title', category: 'seo', value: {} }
|
||||
];
|
||||
|
||||
const filterByKeyPrefix = (configs: any[], prefix: string) => {
|
||||
return configs.filter(config => config.key.startsWith(prefix));
|
||||
};
|
||||
|
||||
const result = filterByKeyPrefix(configs, 'feature_');
|
||||
expect(result).toHaveLength(3);
|
||||
expect(result.every(config => config.key.startsWith('feature_'))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('配置合并和更新', () => {
|
||||
test('合并配置对象', () => {
|
||||
const baseConfig = {
|
||||
enabled: true,
|
||||
items: ['erp', 'crm']
|
||||
};
|
||||
|
||||
const updateConfig = {
|
||||
enabled: false,
|
||||
count: 5
|
||||
};
|
||||
|
||||
const mergedConfig = { ...baseConfig, ...updateConfig };
|
||||
|
||||
expect(mergedConfig.enabled).toBe(false);
|
||||
expect(mergedConfig.items).toEqual(['erp', 'crm']);
|
||||
expect(mergedConfig.count).toBe(5);
|
||||
});
|
||||
|
||||
test('深度合并配置对象', () => {
|
||||
const baseConfig = {
|
||||
enabled: true,
|
||||
metadata: {
|
||||
author: 'admin',
|
||||
tags: ['tag1']
|
||||
}
|
||||
};
|
||||
|
||||
const updateConfig = {
|
||||
metadata: {
|
||||
tags: ['tag1', 'tag2']
|
||||
}
|
||||
};
|
||||
|
||||
const deepMerge = (target: any, source: any) => {
|
||||
const output = { ...target };
|
||||
for (const key in source) {
|
||||
if (source[key] instanceof Object && !Array.isArray(source[key]) && key in target) {
|
||||
output[key] = deepMerge(target[key], source[key]);
|
||||
} else {
|
||||
output[key] = source[key];
|
||||
}
|
||||
}
|
||||
return output;
|
||||
};
|
||||
|
||||
const mergedConfig = deepMerge(baseConfig, updateConfig);
|
||||
|
||||
expect(mergedConfig.enabled).toBe(true);
|
||||
expect(mergedConfig.metadata.author).toBe('admin');
|
||||
expect(mergedConfig.metadata.tags).toEqual(['tag1', 'tag2']);
|
||||
});
|
||||
|
||||
test('批量更新配置', () => {
|
||||
const configs = [
|
||||
{ key: 'feature_services', value: { enabled: true, items: ['erp'] } },
|
||||
{ key: 'feature_products', value: { enabled: false, showPricing: true, featuredProducts: [] } }
|
||||
];
|
||||
|
||||
const updates = [
|
||||
{ key: 'feature_services', value: { enabled: false } },
|
||||
{ key: 'feature_products', value: { enabled: true, featuredProducts: ['product1'] } }
|
||||
];
|
||||
|
||||
const updatedConfigs = configs.map(config => {
|
||||
const update = updates.find(u => u.key === config.key);
|
||||
if (update) {
|
||||
return {
|
||||
...config,
|
||||
value: { ...config.value, ...update.value }
|
||||
};
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
expect(updatedConfigs[0].value.enabled).toBe(false);
|
||||
expect(updatedConfigs[0].value.items).toEqual(['erp']);
|
||||
expect(updatedConfigs[1].value.enabled).toBe(true);
|
||||
expect(updatedConfigs[1].value.featuredProducts).toEqual(['product1']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('配置序列化和反序列化', () => {
|
||||
test('序列化配置为JSON', () => {
|
||||
const config = {
|
||||
key: 'feature_services',
|
||||
value: { enabled: true, items: ['erp', 'crm'] },
|
||||
category: 'feature'
|
||||
};
|
||||
|
||||
const serialized = JSON.stringify(config);
|
||||
const deserialized = JSON.parse(serialized);
|
||||
|
||||
expect(deserialized.key).toBe('feature_services');
|
||||
expect(deserialized.value.enabled).toBe(true);
|
||||
expect(deserialized.value.items).toEqual(['erp', 'crm']);
|
||||
});
|
||||
|
||||
test('处理日期序列化', () => {
|
||||
const config = {
|
||||
key: 'feature_services',
|
||||
value: { enabled: true },
|
||||
category: 'feature',
|
||||
updatedAt: new Date('2024-01-01T00:00:00.000Z')
|
||||
};
|
||||
|
||||
const serialized = JSON.stringify(config);
|
||||
const deserialized = JSON.parse(serialized);
|
||||
|
||||
expect(typeof deserialized.updatedAt).toBe('string');
|
||||
expect(new Date(deserialized.updatedAt).toISOString()).toBe(config.updatedAt.toISOString());
|
||||
});
|
||||
|
||||
test('处理特殊字符序列化', () => {
|
||||
const config = {
|
||||
key: 'feature_services',
|
||||
value: { items: ['erp', 'crm', 'mes'] },
|
||||
category: 'feature',
|
||||
description: '服务配置 <script>alert("test")</script>'
|
||||
};
|
||||
|
||||
const serialized = JSON.stringify(config);
|
||||
const deserialized = JSON.parse(serialized);
|
||||
|
||||
expect(deserialized.description).toContain('<script>');
|
||||
expect(deserialized.value.items).toEqual(['erp', 'crm', 'mes']);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,300 @@
|
||||
import { describe, test, expect } from '@playwright/test';
|
||||
|
||||
describe('配置验证逻辑测试', () => {
|
||||
describe('配置类型验证', () => {
|
||||
test('验证布尔类型配置', () => {
|
||||
const validBooleans = [true, false];
|
||||
const invalidBooleans = ['true', 'false', 1, 0, null, undefined, ''];
|
||||
|
||||
validBooleans.forEach(value => {
|
||||
expect(typeof value === 'boolean').toBe(true);
|
||||
});
|
||||
|
||||
invalidBooleans.forEach(value => {
|
||||
expect(typeof value === 'boolean').toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
test('验证数字类型配置', () => {
|
||||
const validNumbers = [0, 1, 100, -1, 3.14];
|
||||
const invalidNumbers = ['100', 'abc', null, undefined, NaN];
|
||||
|
||||
validNumbers.forEach(value => {
|
||||
expect(typeof value === 'number' && !isNaN(value)).toBe(true);
|
||||
});
|
||||
|
||||
invalidNumbers.forEach(value => {
|
||||
expect(typeof value === 'number' && !isNaN(value)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
test('验证字符串类型配置', () => {
|
||||
const validStrings = ['test', 'hello world', '123', 'true'];
|
||||
const invalidStrings = [null, undefined, 123, true, {}];
|
||||
|
||||
validStrings.forEach(value => {
|
||||
expect(typeof value === 'string').toBe(true);
|
||||
});
|
||||
|
||||
invalidStrings.forEach(value => {
|
||||
expect(typeof value === 'string').toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
test('验证数组类型配置', () => {
|
||||
const validArrays = [[], [1, 2, 3], ['a', 'b', 'c']];
|
||||
const invalidArrays = [null, undefined, 'array', { length: 3 }];
|
||||
|
||||
validArrays.forEach(value => {
|
||||
expect(Array.isArray(value)).toBe(true);
|
||||
});
|
||||
|
||||
invalidArrays.forEach(value => {
|
||||
expect(Array.isArray(value)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
test('验证对象类型配置', () => {
|
||||
const validObjects = [{}, { key: 'value' }, { a: 1, b: 2 }];
|
||||
const invalidObjects = [null, undefined, 'object', [], 123];
|
||||
|
||||
validObjects.forEach(value => {
|
||||
expect(typeof value === 'object' && value !== null && !Array.isArray(value)).toBe(true);
|
||||
});
|
||||
|
||||
invalidObjects.forEach(value => {
|
||||
expect(typeof value === 'object' && value !== null && !Array.isArray(value)).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('配置范围验证', () => {
|
||||
test('验证数字范围', () => {
|
||||
const validateRange = (value: number, min: number, max: number) => {
|
||||
return typeof value === 'number' && !isNaN(value) && value >= min && value <= max;
|
||||
};
|
||||
|
||||
expect(validateRange(5, 0, 10)).toBe(true);
|
||||
expect(validateRange(0, 0, 10)).toBe(true);
|
||||
expect(validateRange(10, 0, 10)).toBe(true);
|
||||
expect(validateRange(-1, 0, 10)).toBe(false);
|
||||
expect(validateRange(11, 0, 10)).toBe(false);
|
||||
expect(validateRange(NaN, 0, 10)).toBe(false);
|
||||
});
|
||||
|
||||
test('验证数组长度范围', () => {
|
||||
const validateArrayLength = (value: unknown[], min: number, max: number) => {
|
||||
return Array.isArray(value) && value.length >= min && value.length <= max;
|
||||
};
|
||||
|
||||
expect(validateArrayLength([1, 2, 3], 1, 10)).toBe(true);
|
||||
expect(validateArrayLength([], 0, 10)).toBe(true);
|
||||
expect(validateArrayLength([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 1, 10)).toBe(true);
|
||||
expect(validateArrayLength([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 1, 10)).toBe(false);
|
||||
expect(validateArrayLength([], 1, 10)).toBe(false);
|
||||
});
|
||||
|
||||
test('验证字符串长度范围', () => {
|
||||
const validateStringLength = (value: string, min: number, max: number) => {
|
||||
return typeof value === 'string' && value.length >= min && value.length <= max;
|
||||
};
|
||||
|
||||
expect(validateStringLength('test', 1, 10)).toBe(true);
|
||||
expect(validateStringLength('t', 1, 10)).toBe(true);
|
||||
expect(validateStringLength('1234567890', 1, 10)).toBe(true);
|
||||
expect(validateStringLength('', 1, 10)).toBe(false);
|
||||
expect(validateStringLength('12345678901', 1, 10)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('配置格式验证', () => {
|
||||
test('验证枚举值', () => {
|
||||
const validateEnum = (value: string, allowedValues: string[]) => {
|
||||
return allowedValues.includes(value);
|
||||
};
|
||||
|
||||
const categories = ['feature', 'style', 'seo', 'general'];
|
||||
const sortOrders = ['asc', 'desc'];
|
||||
|
||||
expect(validateEnum('feature', categories)).toBe(true);
|
||||
expect(validateEnum('style', categories)).toBe(true);
|
||||
expect(validateEnum('invalid', categories)).toBe(false);
|
||||
expect(validateEnum('asc', sortOrders)).toBe(true);
|
||||
expect(validateEnum('desc', sortOrders)).toBe(true);
|
||||
expect(validateEnum('invalid', sortOrders)).toBe(false);
|
||||
});
|
||||
|
||||
test('验证URL格式', () => {
|
||||
const validateUrl = (url: string) => {
|
||||
if (!url || typeof url !== 'string') {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
new URL(url);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
expect(validateUrl('https://example.com')).toBe(true);
|
||||
expect(validateUrl('http://example.com')).toBe(true);
|
||||
expect(validateUrl('https://example.com/path')).toBe(true);
|
||||
expect(validateUrl('https://example.com?query=1')).toBe(true);
|
||||
expect(validateUrl('invalid-url')).toBe(false);
|
||||
expect(validateUrl('')).toBe(false);
|
||||
expect(validateUrl(null as any)).toBe(false);
|
||||
});
|
||||
|
||||
test('验证JSON格式', () => {
|
||||
const validateJson = (value: string) => {
|
||||
if (!value || typeof value !== 'string') {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
JSON.parse(value);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
expect(validateJson('{"key":"value"}')).toBe(true);
|
||||
expect(validateJson('[]')).toBe(true);
|
||||
expect(validateJson('null')).toBe(true);
|
||||
expect(validateJson('123')).toBe(true);
|
||||
expect(validateJson('"string"')).toBe(true);
|
||||
expect(validateJson('{invalid}')).toBe(false);
|
||||
expect(validateJson('')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('配置结构验证', () => {
|
||||
test('验证配置对象结构', () => {
|
||||
const validateConfigStructure = (config: any, requiredFields: string[]) => {
|
||||
if (!config || typeof config !== 'object') {
|
||||
return false;
|
||||
}
|
||||
return requiredFields.every(field => field in config);
|
||||
};
|
||||
|
||||
const validConfig = { key: 'test', value: { enabled: true }, category: 'feature' };
|
||||
const invalidConfig1 = { key: 'test', value: { enabled: true } };
|
||||
const invalidConfig2 = { key: 'test', category: 'feature' };
|
||||
|
||||
expect(validateConfigStructure(validConfig, ['key', 'value', 'category'])).toBe(true);
|
||||
expect(validateConfigStructure(invalidConfig1, ['key', 'value', 'category'])).toBe(false);
|
||||
expect(validateConfigStructure(invalidConfig2, ['key', 'value', 'category'])).toBe(false);
|
||||
});
|
||||
|
||||
test('验证嵌套配置结构', () => {
|
||||
const validateNestedConfig = (config: any, structure: Record<string, any>) => {
|
||||
if (!config || typeof config !== 'object') {
|
||||
return false;
|
||||
}
|
||||
for (const [key, type] of Object.entries(structure)) {
|
||||
if (!(key in config)) {
|
||||
return false;
|
||||
}
|
||||
if (typeof config[key] !== type) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
const config = {
|
||||
enabled: true,
|
||||
count: 5,
|
||||
items: ['a', 'b', 'c'],
|
||||
nested: { key: 'value' }
|
||||
};
|
||||
|
||||
const structure = {
|
||||
enabled: 'boolean',
|
||||
count: 'number',
|
||||
items: 'object',
|
||||
nested: 'object'
|
||||
};
|
||||
|
||||
expect(validateNestedConfig(config, structure)).toBe(true);
|
||||
expect(validateNestedConfig({ enabled: 'true' }, structure)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('配置业务规则验证', () => {
|
||||
test('验证feature_services配置', () => {
|
||||
const validateServicesConfig = (config: any) => {
|
||||
if (!config || typeof config !== 'object') {
|
||||
return false;
|
||||
}
|
||||
return (
|
||||
typeof config.enabled === 'boolean' &&
|
||||
Array.isArray(config.items) &&
|
||||
config.items.every((item: string) => typeof item === 'string')
|
||||
);
|
||||
};
|
||||
|
||||
const validConfig = { enabled: true, items: ['erp', 'crm', 'mes'] };
|
||||
const invalidConfig1 = { enabled: 'true', items: ['erp'] };
|
||||
const invalidConfig2 = { enabled: true, items: [1, 2, 3] };
|
||||
const invalidConfig3 = { enabled: true };
|
||||
|
||||
expect(validateServicesConfig(validConfig)).toBe(true);
|
||||
expect(validateServicesConfig(invalidConfig1)).toBe(false);
|
||||
expect(validateServicesConfig(invalidConfig2)).toBe(false);
|
||||
expect(validateServicesConfig(invalidConfig3)).toBe(false);
|
||||
});
|
||||
|
||||
test('验证feature_products配置', () => {
|
||||
const validateProductsConfig = (config: any) => {
|
||||
if (!config || typeof config !== 'object') {
|
||||
return false;
|
||||
}
|
||||
return (
|
||||
typeof config.enabled === 'boolean' &&
|
||||
typeof config.showPricing === 'boolean' &&
|
||||
Array.isArray(config.featuredProducts) &&
|
||||
config.featuredProducts.every((item: string) => typeof item === 'string')
|
||||
);
|
||||
};
|
||||
|
||||
const validConfig = { enabled: true, showPricing: true, featuredProducts: ['product1', 'product2'] };
|
||||
const invalidConfig1 = { enabled: 'true', showPricing: true, featuredProducts: [] };
|
||||
const invalidConfig2 = { enabled: true, showPricing: 'true', featuredProducts: [] };
|
||||
const invalidConfig3 = { enabled: true, showPricing: true };
|
||||
|
||||
expect(validateProductsConfig(validConfig)).toBe(true);
|
||||
expect(validateProductsConfig(invalidConfig1)).toBe(false);
|
||||
expect(validateProductsConfig(invalidConfig2)).toBe(false);
|
||||
expect(validateProductsConfig(invalidConfig3)).toBe(false);
|
||||
});
|
||||
|
||||
test('验证feature_news配置', () => {
|
||||
const validateNewsConfig = (config: any) => {
|
||||
if (!config || typeof config !== 'object') {
|
||||
return false;
|
||||
}
|
||||
return (
|
||||
typeof config.enabled === 'boolean' &&
|
||||
typeof config.displayCount === 'number' &&
|
||||
config.displayCount >= 0 &&
|
||||
config.displayCount <= 100 &&
|
||||
Array.isArray(config.categories) &&
|
||||
config.categories.every((item: string) => typeof item === 'string') &&
|
||||
['asc', 'desc'].includes(config.sortOrder)
|
||||
);
|
||||
};
|
||||
|
||||
const validConfig = { enabled: true, displayCount: 5, categories: ['tech', 'business'], sortOrder: 'desc' };
|
||||
const invalidConfig1 = { enabled: 'true', displayCount: 5, categories: [], sortOrder: 'desc' };
|
||||
const invalidConfig2 = { enabled: true, displayCount: -1, categories: [], sortOrder: 'desc' };
|
||||
const invalidConfig3 = { enabled: true, displayCount: 5, categories: [], sortOrder: 'invalid' };
|
||||
|
||||
expect(validateNewsConfig(validConfig)).toBe(true);
|
||||
expect(validateNewsConfig(invalidConfig1)).toBe(false);
|
||||
expect(validateNewsConfig(invalidConfig2)).toBe(false);
|
||||
expect(validateNewsConfig(invalidConfig3)).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user