|
| 1 | +# Design Document |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +本项目是一个基于TypeScript+React+D3.js的算法可视化教学网站,用于展示LeetCode第1题"两数之和"的算法执行过程。网站采用单屏幕设计,包含以下核心模块: |
| 6 | + |
| 7 | +- 页面头部:标题、GitHub链接 |
| 8 | +- 数据输入区:自定义输入、预设样例、随机生成 |
| 9 | +- 控制面板:播放控制按钮、进度条 |
| 10 | +- 代码调试器:带行号的代码展示、语法高亮、变量值显示 |
| 11 | +- 交互式画布:数组可视化、HashMap状态、动画效果 |
| 12 | +- 悬浮组件:交流群二维码 |
| 13 | + |
| 14 | +## Architecture |
| 15 | + |
| 16 | +``` |
| 17 | +┌─────────────────────────────────────────────────────────────┐ |
| 18 | +│ App (Root) │ |
| 19 | +├─────────────────────────────────────────────────────────────┤ |
| 20 | +│ ┌─────────────────────────────────────────────────────┐ │ |
| 21 | +│ │ Header │ │ |
| 22 | +│ │ [Title + Link] [GitHub Icon] │ │ |
| 23 | +│ └─────────────────────────────────────────────────────┘ │ |
| 24 | +│ ┌─────────────────────────────────────────────────────┐ │ |
| 25 | +│ │ DataInput │ │ |
| 26 | +│ │ [Input Fields] [Presets] [Random] [Submit] │ │ |
| 27 | +│ └─────────────────────────────────────────────────────┘ │ |
| 28 | +├─────────────────────────────────────────────────────────────┤ |
| 29 | +│ ┌──────────────────────┐ ┌──────────────────────────┐ │ |
| 30 | +│ │ │ │ │ │ |
| 31 | +│ │ CodeDebugger │ │ Canvas │ │ |
| 32 | +│ │ │ │ │ │ |
| 33 | +│ │ - Line numbers │ │ - Array visualization │ │ |
| 34 | +│ │ - Syntax highlight │ │ - HashMap display │ │ |
| 35 | +│ │ - Variable values │ │ - Animations │ │ |
| 36 | +│ │ - Current line │ │ - Pan & Zoom │ │ |
| 37 | +│ │ │ │ │ │ |
| 38 | +│ └──────────────────────┘ └──────────────────────────┘ │ |
| 39 | +├─────────────────────────────────────────────────────────────┤ |
| 40 | +│ ┌─────────────────────────────────────────────────────┐ │ |
| 41 | +│ │ ControlPanel │ │ |
| 42 | +│ │ [Reset] [Prev ←] [Play/Pause ␣] [Next →] │ │ |
| 43 | +│ │ ════════════════════════════════════════════ │ │ |
| 44 | +│ │ Progress Bar (draggable) │ │ |
| 45 | +│ └─────────────────────────────────────────────────────┘ │ |
| 46 | +└─────────────────────────────────────────────────────────────┘ |
| 47 | + [FloatingBall] ↘ |
| 48 | +``` |
| 49 | + |
| 50 | +### 技术栈 |
| 51 | + |
| 52 | +- **框架**: React 18 + TypeScript |
| 53 | +- **构建工具**: Vite |
| 54 | +- **可视化**: D3.js |
| 55 | +- **样式**: CSS Modules |
| 56 | +- **代码高亮**: Prism.js |
| 57 | +- **部署**: GitHub Actions + GitHub Pages |
| 58 | + |
| 59 | +## Components and Interfaces |
| 60 | + |
| 61 | +### 1. App (根组件) |
| 62 | + |
| 63 | +```typescript |
| 64 | +interface AppState { |
| 65 | + inputData: InputData; |
| 66 | + steps: Step[]; |
| 67 | + currentStepIndex: number; |
| 68 | + isPlaying: boolean; |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### 2. Header |
| 73 | + |
| 74 | +```typescript |
| 75 | +interface HeaderProps { |
| 76 | + title: string; |
| 77 | + leetcodeUrl: string; |
| 78 | + githubUrl: string; |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### 3. DataInput |
| 83 | + |
| 84 | +```typescript |
| 85 | +interface DataInputProps { |
| 86 | + onSubmit: (data: InputData) => void; |
| 87 | + presets: PresetData[]; |
| 88 | +} |
| 89 | + |
| 90 | +interface InputData { |
| 91 | + nums: number[]; |
| 92 | + target: number; |
| 93 | +} |
| 94 | + |
| 95 | +interface PresetData { |
| 96 | + label: string; |
| 97 | + nums: number[]; |
| 98 | + target: number; |
| 99 | +} |
| 100 | + |
| 101 | +interface ValidationResult { |
| 102 | + isValid: boolean; |
| 103 | + error?: string; |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +### 4. CodeDebugger |
| 108 | + |
| 109 | +```typescript |
| 110 | +interface CodeDebuggerProps { |
| 111 | + code: string; |
| 112 | + currentLine: number; |
| 113 | + variables: VariableState[]; |
| 114 | +} |
| 115 | + |
| 116 | +interface VariableState { |
| 117 | + name: string; |
| 118 | + value: string; |
| 119 | + line: number; |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +### 5. Canvas |
| 124 | + |
| 125 | +```typescript |
| 126 | +interface CanvasProps { |
| 127 | + step: Step; |
| 128 | + inputData: InputData; |
| 129 | +} |
| 130 | + |
| 131 | +interface CanvasTransform { |
| 132 | + x: number; |
| 133 | + y: number; |
| 134 | + scale: number; |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +### 6. ControlPanel |
| 139 | + |
| 140 | +```typescript |
| 141 | +interface ControlPanelProps { |
| 142 | + currentStep: number; |
| 143 | + totalSteps: number; |
| 144 | + isPlaying: boolean; |
| 145 | + onPrev: () => void; |
| 146 | + onNext: () => void; |
| 147 | + onPlay: () => void; |
| 148 | + onPause: () => void; |
| 149 | + onReset: () => void; |
| 150 | + onSeek: (step: number) => void; |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +### 7. FloatingBall |
| 155 | + |
| 156 | +```typescript |
| 157 | +interface FloatingBallProps { |
| 158 | + qrCodeUrl: string; |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +## Data Models |
| 163 | + |
| 164 | +### Step (算法步骤) |
| 165 | + |
| 166 | +```typescript |
| 167 | +interface Step { |
| 168 | + index: number; |
| 169 | + description: string; |
| 170 | + currentLine: number; |
| 171 | + variables: VariableState[]; |
| 172 | + arrayState: ArrayElementState[]; |
| 173 | + hashMapState: HashMapEntry[]; |
| 174 | + highlightedIndices: number[]; |
| 175 | + annotations: Annotation[]; |
| 176 | +} |
| 177 | + |
| 178 | +interface ArrayElementState { |
| 179 | + index: number; |
| 180 | + value: number; |
| 181 | + isHighlighted: boolean; |
| 182 | + highlightColor?: string; |
| 183 | +} |
| 184 | + |
| 185 | +interface HashMapEntry { |
| 186 | + key: number; |
| 187 | + value: number; |
| 188 | + isNew?: boolean; |
| 189 | +} |
| 190 | + |
| 191 | +interface Annotation { |
| 192 | + targetIndex: number; |
| 193 | + text: string; |
| 194 | + position: 'top' | 'bottom' | 'left' | 'right'; |
| 195 | +} |
| 196 | +``` |
| 197 | + |
| 198 | +### 算法步骤生成器 |
| 199 | + |
| 200 | +```typescript |
| 201 | +function generateSteps(nums: number[], target: number): Step[] { |
| 202 | + // 生成两数之和算法的所有执行步骤 |
| 203 | + // 包括:初始化HashMap、遍历数组、查找补数、更新HashMap、找到结果 |
| 204 | +} |
| 205 | +``` |
| 206 | + |
| 207 | +## Correctness Properties |
| 208 | + |
| 209 | +*A property is a characteristic or behavior that should hold true across all valid executions of a system-essentially, a formal statement about what the system should do. Properties serve as the bridge between human-readable specifications and machine-verifiable correctness guarantees.* |
| 210 | + |
| 211 | +### Property 1: 步骤导航一致性 |
| 212 | + |
| 213 | +*For any* 算法步骤序列和当前步骤索引,执行"下一步"操作后步骤索引应增加1(除非已在最后一步),执行"上一步"操作后步骤索引应减少1(除非已在第一步)。 |
| 214 | + |
| 215 | +**Validates: Requirements 2.1, 2.2** |
| 216 | + |
| 217 | +### Property 2: 重置状态一致性 |
| 218 | + |
| 219 | +*For any* 算法演示状态,执行"重置"操作后,当前步骤索引应为0,播放状态应为暂停。 |
| 220 | + |
| 221 | +**Validates: Requirements 2.5** |
| 222 | + |
| 223 | +### Property 3: 进度条跳转一致性 |
| 224 | + |
| 225 | +*For any* 进度条位置(0到totalSteps-1之间),拖动到该位置后,当前步骤索引应等于该位置值。 |
| 226 | + |
| 227 | +**Validates: Requirements 3.2** |
| 228 | + |
| 229 | +### Property 4: 代码调试器状态一致性 |
| 230 | + |
| 231 | +*For any* 算法步骤,代码调试器高亮的行号应与步骤中指定的currentLine一致,显示的变量值应与步骤中的variables状态一致。 |
| 232 | + |
| 233 | +**Validates: Requirements 4.1, 4.2** |
| 234 | + |
| 235 | +### Property 5: 画布状态一致性 |
| 236 | + |
| 237 | +*For any* 算法步骤,画布上显示的数组元素应与inputData.nums一致,高亮的元素索引应与步骤中的highlightedIndices一致,HashMap显示应与步骤中的hashMapState一致。 |
| 238 | + |
| 239 | +**Validates: Requirements 6.1, 6.2, 6.3, 6.5** |
| 240 | + |
| 241 | +### Property 6: 随机数据合法性 |
| 242 | + |
| 243 | +*For any* 随机生成的测试数据,应满足:数组长度在2到10000之间,数组元素在-10^9到10^9之间,存在唯一有效解。 |
| 244 | + |
| 245 | +**Validates: Requirements 7.4** |
| 246 | + |
| 247 | +### Property 7: 数据验证正确性 |
| 248 | + |
| 249 | +*For any* 输入数据,验证函数应正确判断:数组长度是否在有效范围内,元素值是否在有效范围内,是否存在有效解。对于不合法输入应返回错误信息。 |
| 250 | + |
| 251 | +**Validates: Requirements 7.5, 7.6** |
| 252 | + |
| 253 | +### Property 8: 算法步骤生成正确性 |
| 254 | + |
| 255 | +*For any* 合法的输入数据(nums和target),生成的步骤序列的最后一步应包含正确的结果(两个下标i和j,使得nums[i] + nums[j] === target)。 |
| 256 | + |
| 257 | +**Validates: Requirements 6.1, 6.2, 6.3** |
| 258 | + |
| 259 | +## Error Handling |
| 260 | + |
| 261 | +### 输入验证错误 |
| 262 | + |
| 263 | +- 数组为空或长度不足:显示"数组长度必须至少为2" |
| 264 | +- 数组长度超限:显示"数组长度不能超过10000" |
| 265 | +- 元素值超限:显示"元素值必须在-10^9到10^9之间" |
| 266 | +- 无有效解:显示"输入数据不存在有效解" |
| 267 | +- 格式错误:显示"请输入有效的数组格式,如:[2,7,11,15]" |
| 268 | + |
| 269 | +### 运行时错误 |
| 270 | + |
| 271 | +- 画布渲染失败:显示降级的静态视图 |
| 272 | +- 动画播放失败:跳过动画直接显示最终状态 |
| 273 | + |
| 274 | +## Testing Strategy |
| 275 | + |
| 276 | +### 单元测试 |
| 277 | + |
| 278 | +使用Vitest进行单元测试: |
| 279 | + |
| 280 | +- 数据验证函数测试 |
| 281 | +- 算法步骤生成器测试 |
| 282 | +- 状态管理逻辑测试 |
| 283 | + |
| 284 | +### 属性测试 |
| 285 | + |
| 286 | +使用fast-check进行属性测试: |
| 287 | + |
| 288 | +- 每个属性测试运行至少100次迭代 |
| 289 | +- 测试注释格式:`**Feature: two-sum-visualizer, Property {number}: {property_text}**` |
| 290 | + |
| 291 | +### 集成测试 |
| 292 | + |
| 293 | +使用Playwright进行E2E测试: |
| 294 | + |
| 295 | +- 页面加载测试 |
| 296 | +- 用户交互流程测试 |
| 297 | +- 快捷键功能测试 |
| 298 | + |
| 299 | +### 测试文件结构 |
| 300 | + |
| 301 | +``` |
| 302 | +src/ |
| 303 | +├── components/ |
| 304 | +│ ├── __tests__/ |
| 305 | +│ │ ├── ControlPanel.test.tsx |
| 306 | +│ │ ├── DataInput.test.tsx |
| 307 | +│ │ └── CodeDebugger.test.tsx |
| 308 | +├── utils/ |
| 309 | +│ ├── __tests__/ |
| 310 | +│ │ ├── stepGenerator.test.ts |
| 311 | +│ │ ├── stepGenerator.property.test.ts |
| 312 | +│ │ ├── validation.test.ts |
| 313 | +│ │ └── validation.property.test.ts |
| 314 | +``` |
0 commit comments