first commit

This commit is contained in:
douboer
2026-01-21 13:22:26 +08:00
commit 24452838a1
28 changed files with 7901 additions and 0 deletions

165
tests/constants.test.ts Normal file
View File

@@ -0,0 +1,165 @@
/**
* 常量和配置单元测试
*/
import { describe, it, expect } from 'vitest';
import {
DEFAULT_SIZE,
DEFAULT_TEXT_RADIAL_POSITION,
SECTOR_INSET_DISTANCE,
SECTOR_STROKE_WIDTH,
EXAMPLES,
} from '../src/constants';
describe('DEFAULT_SIZE', () => {
it('应该是一个正数', () => {
expect(DEFAULT_SIZE).toBeGreaterThan(0);
});
it('应该是一个合理的画布尺寸', () => {
expect(DEFAULT_SIZE).toBeGreaterThanOrEqual(200);
expect(DEFAULT_SIZE).toBeLessThanOrEqual(2000);
});
});
describe('DEFAULT_TEXT_RADIAL_POSITION', () => {
it('应该是有效的文字位置值', () => {
expect(['middle', 'centroid']).toContain(DEFAULT_TEXT_RADIAL_POSITION);
});
});
describe('SECTOR_INSET_DISTANCE', () => {
it('应该是一个非负数', () => {
expect(SECTOR_INSET_DISTANCE).toBeGreaterThanOrEqual(0);
});
it('应该是一个合理的内缩距离', () => {
expect(SECTOR_INSET_DISTANCE).toBeLessThan(10);
});
});
describe('SECTOR_STROKE_WIDTH', () => {
it('应该是一个正数', () => {
expect(SECTOR_STROKE_WIDTH).toBeGreaterThan(0);
});
it('应该是一个合理的线宽', () => {
expect(SECTOR_STROKE_WIDTH).toBeLessThan(5);
});
});
describe('EXAMPLES', () => {
it('应该包含至少一个示例', () => {
expect(EXAMPLES.length).toBeGreaterThan(0);
});
it('每个示例应该有名称', () => {
EXAMPLES.forEach((example) => {
expect(example.name).toBeTruthy();
expect(typeof example.name).toBe('string');
});
});
it('每个示例应该有有效的角度数组', () => {
EXAMPLES.forEach((example) => {
expect(Array.isArray(example.angles)).toBe(true);
expect(example.angles.length).toBeGreaterThanOrEqual(2);
});
});
it('每个示例的角度应该从 0 开始', () => {
EXAMPLES.forEach((example) => {
expect(example.angles[0]).toBe(0);
});
});
it('每个示例的角度应该以 360 结束', () => {
EXAMPLES.forEach((example) => {
expect(example.angles[example.angles.length - 1]).toBe(360);
});
});
it('每个示例的角度应该是递增的', () => {
EXAMPLES.forEach((example) => {
for (let i = 1; i < example.angles.length; i++) {
expect(example.angles[i]).toBeGreaterThan(example.angles[i - 1]);
}
});
});
it('每个示例应该有有效的半径数组', () => {
EXAMPLES.forEach((example) => {
expect(Array.isArray(example.radii)).toBe(true);
expect(example.radii.length).toBeGreaterThanOrEqual(1);
});
});
it('每个示例的半径应该是正数', () => {
EXAMPLES.forEach((example) => {
example.radii.forEach((radius) => {
expect(radius).toBeGreaterThan(0);
});
});
});
it('每个示例的半径应该是递增的', () => {
EXAMPLES.forEach((example) => {
for (let i = 1; i < example.radii.length; i++) {
expect(example.radii[i]).toBeGreaterThan(example.radii[i - 1]);
}
});
});
it('应该包含不同类型的示例(等分和不等分)', () => {
const hasEqualDivision = EXAMPLES.some((example) => {
const angles = example.angles;
if (angles.length < 3) return false;
const step = angles[1] - angles[0];
return angles.every((angle, i) => i === 0 || Math.abs(angle - angles[i - 1] - step) < 0.01);
});
const hasUnequalDivision = EXAMPLES.some((example) => {
const angles = example.angles;
if (angles.length < 3) return false;
const firstStep = angles[1] - angles[0];
return angles.some((angle, i) => i > 1 && Math.abs(angle - angles[i - 1] - firstStep) > 0.01);
});
expect(hasEqualDivision).toBe(true);
expect(hasUnequalDivision).toBe(true);
});
it('应该包含不同层数的示例', () => {
const layerCounts = EXAMPLES.map((example) => example.radii.length);
const uniqueLayerCounts = new Set(layerCounts);
expect(uniqueLayerCounts.size).toBeGreaterThan(1);
});
it('应该包含密集和稀疏的扇区分割', () => {
const sectorCounts = EXAMPLES.map((example) => example.angles.length - 1);
const minSectors = Math.min(...sectorCounts);
const maxSectors = Math.max(...sectorCounts);
expect(maxSectors).toBeGreaterThan(minSectors * 2);
});
it('所有示例的半径都不应超过合理范围', () => {
EXAMPLES.forEach((example) => {
const maxRadius = Math.max(...example.radii);
expect(maxRadius).toBeLessThan(500); // 假设最大半径不超过500
});
});
it('应该有12等分3层的标准示例', () => {
const example = EXAMPLES.find((ex) => ex.name.includes('12') && ex.name.includes('3'));
expect(example).toBeDefined();
if (example) {
expect(example.angles.length).toBe(13); // 0 到 36013个点
expect(example.radii.length).toBe(3);
}
});
it('应该有包含大量层数的示例', () => {
const maxLayers = Math.max(...EXAMPLES.map((ex) => ex.radii.length));
expect(maxLayers).toBeGreaterThanOrEqual(6);
});
});