Skip to content

Commit 1602c72

Browse files
committed
refactor(3d): 抽取 tier 迁移策略为纯函数模块 tierPolicy 并补充 BDD 单测
将'如何逐级迁移 tier'的纯逻辑从 PerformanceMonitor 中物理分离(AI 编程规范 §1 副作用隔离、§6 自底向上),stepTier 逐级迁移策略(low↔medium↔high,不跨级跳变)获得 8 个 AAA 单测护栏,为后续以成熟方案重写监控层提供行为基线。本步为纯新增,不改变任何现有行为。
1 parent 8851a1f commit 1602c72

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* tierPolicy 单元测试 — BDD 风格、AAA 模式
3+
*
4+
* @ai-context: 覆盖 stepTier 的全部迁移路径与边界保持,
5+
* 是 PerformanceMonitor 重构(自研监控 → drei 成熟方案)的行为护栏。
6+
*/
7+
import { describe, it, expect } from 'vitest';
8+
import { stepTier } from './tierPolicy';
9+
10+
describe('stepTier(逐级迁移策略)', () => {
11+
describe('升级(up)', () => {
12+
it('low 应升级为 medium', () => {
13+
// Arrange & Act
14+
const result = stepTier('low', 'up');
15+
// Assert
16+
expect(result).toBe('medium');
17+
});
18+
19+
it('medium 应升级为 high', () => {
20+
const result = stepTier('medium', 'up');
21+
expect(result).toBe('high');
22+
});
23+
24+
it('high 已达上限,应保持不变', () => {
25+
const result = stepTier('high', 'up');
26+
expect(result).toBe('high');
27+
});
28+
});
29+
30+
describe('降级(down)', () => {
31+
it('high 应降级为 medium', () => {
32+
const result = stepTier('high', 'down');
33+
expect(result).toBe('medium');
34+
});
35+
36+
it('medium 应降级为 low', () => {
37+
const result = stepTier('medium', 'down');
38+
expect(result).toBe('low');
39+
});
40+
41+
it('low 已达下限,应保持不变', () => {
42+
const result = stepTier('low', 'down');
43+
expect(result).toBe('low');
44+
});
45+
});
46+
47+
describe('逐级性(禁止跨级跳变)', () => {
48+
it('升级时 low 不得直接跳到 high', () => {
49+
expect(stepTier('low', 'up')).not.toBe('high');
50+
});
51+
52+
it('降级时 high 不得直接跳到 low', () => {
53+
expect(stepTier('high', 'down')).not.toBe('low');
54+
});
55+
});
56+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* tierPolicy — 性能分级(tier)逐级迁移策略(纯函数)
3+
*
4+
* 从 PerformanceMonitor 中抽取:将"tier 如何迁移"(纯逻辑)与"如何测量 FPS"
5+
* (R3F 副作用)物理分离,遵循 AI 编程规范 §1(副作用隔离)与 §6(自底向上:
6+
* 先原子纯函数层并附单测,再由系统层装配)。
7+
*
8+
* @ai-context: 纯函数模块,无副作用,可独立单测与安全重构。
9+
* @ai-context: tier 三级抽象(high/medium/low)被 AuroraDomeWorld / DeepSeaWorld /
10+
* ParticleSystem / QualityController 四处消费,改变其语义影响面大,需同步评估。
11+
*/
12+
13+
/** 性能分级:high=全画质,medium=降分辨率,low=关闭后处理特效 */
14+
export type PerformanceTier = 'high' | 'medium' | 'low';
15+
16+
/** tier 调整方向:up=升级,down=降级 */
17+
export type TierTrend = 'up' | 'down';
18+
19+
/**
20+
* 逐级调整 tier(一次只迁移一级,不跨级跳变)。
21+
*
22+
* 为什么逐级:悬崖式变更(high→low)会立即卸载后处理与云层,
23+
* 造成"动画特效突然消失"的强烈视觉波动;逐级迁移使画质降级/恢复平滑。
24+
*
25+
* @param current - 当前 tier
26+
* @param trend - 调整方向
27+
* @returns 调整后的 tier;已处于边界时保持不变(low 不再降、high 不再升)
28+
*/
29+
export function stepTier(current: PerformanceTier, trend: TierTrend): PerformanceTier {
30+
if (trend === 'up') {
31+
return current === 'low' ? 'medium' : 'high';
32+
}
33+
return current === 'high' ? 'medium' : 'low';
34+
}

0 commit comments

Comments
 (0)