test(wave133): BGM混音纯逻辑抽离 + 84单测 #1046

Closed
xiaoxia wants to merge 1 commits from test/wave133-bgm-mixer-pure into develop
Owner

变更内容

新增纯逻辑模块 bgm_mixer_pure.py

bgm_mixer.py 中抽离所有纯函数,0 FFmpeg 依赖,可完全单元测试:

  • 循环判断should_loop_bgm() / calculate_loop_count() — 90%阈值判断 + 余量计算
  • 滤镜链构建build_bgm_filter_chain() — 循环/音量/淡入/淡出/截断/重置时间戳,顺序正确
  • Sidechain计算calculate_sidechain_ratio() / build_sidechain_mix_filter() — 闪避比例→压缩比映射
  • 普通混音build_simple_mix_filter() — amix + volume补偿
  • 配置规范化normalize_bgm_config() — 类型转换 + 边界钳制
  • 配置验证validate_bgm_config() — 参数合法性校验,返回错误列表
  • 工具函数calculate_fade_out_start() / estimate_bgm_processing_duration()

84个单元测试

测试类 数量 覆盖点
TestShouldLoopBGM 10 长短/阈值/禁用/零值/负值
TestCalculateLoopCount 8 整数倍/非整数倍/零值/负值
TestBuildBGMFilterChain 15 音量/循环/淡入/淡出/钳制/边界/完整顺序
TestCalculateSidechainRatio 8 比例映射/上下限/边界值
TestBuildSimpleMixFilter 4 amix/volume补偿/标签/duration
TestBuildSidechainMixFilter 8 threshold/attack/release/knee/amix
TestNormalizeBGMConfig 10 默认值/钳制/字符串转换/真值
TestValidateBGMConfig 8 合法/类型错误/范围错误/多错误
TestCalculateFadeOutStart 6 正常/零值/负值/超长
TestEstimateBGMProcessingDuration 5 循环/不循环/零值
TestBGMPureConfig 2 默认值/自定义值

向后兼容

  • bgm_mixer.py 未修改,不影响现有功能
  • 后续可逐步迁移原模块到使用 pure 模块

纯函数,无外部依赖,本地84测全绿。

## 变更内容 ### 新增纯逻辑模块 `bgm_mixer_pure.py` 从 `bgm_mixer.py` 中抽离所有纯函数,0 FFmpeg 依赖,可完全单元测试: - **循环判断**:`should_loop_bgm()` / `calculate_loop_count()` — 90%阈值判断 + 余量计算 - **滤镜链构建**:`build_bgm_filter_chain()` — 循环/音量/淡入/淡出/截断/重置时间戳,顺序正确 - **Sidechain计算**:`calculate_sidechain_ratio()` / `build_sidechain_mix_filter()` — 闪避比例→压缩比映射 - **普通混音**:`build_simple_mix_filter()` — amix + volume补偿 - **配置规范化**:`normalize_bgm_config()` — 类型转换 + 边界钳制 - **配置验证**:`validate_bgm_config()` — 参数合法性校验,返回错误列表 - **工具函数**:`calculate_fade_out_start()` / `estimate_bgm_processing_duration()` ### 84个单元测试 | 测试类 | 数量 | 覆盖点 | |--------|------|--------| | TestShouldLoopBGM | 10 | 长短/阈值/禁用/零值/负值 | | TestCalculateLoopCount | 8 | 整数倍/非整数倍/零值/负值 | | TestBuildBGMFilterChain | 15 | 音量/循环/淡入/淡出/钳制/边界/完整顺序 | | TestCalculateSidechainRatio | 8 | 比例映射/上下限/边界值 | | TestBuildSimpleMixFilter | 4 | amix/volume补偿/标签/duration | | TestBuildSidechainMixFilter | 8 | threshold/attack/release/knee/amix | | TestNormalizeBGMConfig | 10 | 默认值/钳制/字符串转换/真值 | | TestValidateBGMConfig | 8 | 合法/类型错误/范围错误/多错误 | | TestCalculateFadeOutStart | 6 | 正常/零值/负值/超长 | | TestEstimateBGMProcessingDuration | 5 | 循环/不循环/零值 | | TestBGMPureConfig | 2 | 默认值/自定义值 | ### 向后兼容 - 原 `bgm_mixer.py` 未修改,不影响现有功能 - 后续可逐步迁移原模块到使用 pure 模块 --- 纯函数,无外部依赖,本地84测全绿。
xiaoxia added 1 commit 2026-07-28 09:09:58 +08:00
test(wave133): BGM混音纯逻辑抽离 + 84单测
PR Automation / Auto Approve on CI Green (pull_request) Successful in 5m4s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 30m29s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 20s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 1m10s
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 49s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 1m16s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Validate - Code Quality (pull_request) Failing after 2m33s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 58s
CI/CD Pipeline / PR Build Web Image (pull_request) Successful in 2m4s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 1m59s
AI Code Review / AI Code Review (pull_request) Successful in 2m9s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 7m15s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Unit Tests (pull_request) Successful in 3m52s
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Integration Tests (pull_request) Successful in 5m5s
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Successful in 30s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Has been cancelled
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 19s
7d5426d529
- 新增 bgm_mixer_pure.py:循环判断/滤镜链构建/sidechain计算/配置验证等纯函数
- 84个单测覆盖:should_loop/calculate_loop_count/build_filter_chain/sidechain_ratio/normalize/validate等
- 0 FFmpeg依赖,纯函数可测试

🚀 预览环境已部署

项目 详情
PR号 #1046
预览链接 https://pr-1046.preview.xiaoxiajianji.com
API环境 staging

💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。

🔄 每次提交新代码后预览环境会自动更新。

🗑️ PR 关闭或合并后,预览环境会自动清理。

🚀 **预览环境已部署** | 项目 | 详情 | |------|------| | PR号 | #1046 | | 预览链接 | [https://pr-1046.preview.xiaoxiajianji.com](https://pr-1046.preview.xiaoxiajianji.com) | | API环境 | staging | > 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。 > > 🔄 每次提交新代码后预览环境会自动更新。 > > 🗑️ PR 关闭或合并后,预览环境会自动清理。
Collaborator

Auto merge skipped after multiple 405 errors: PR may have conflicts or unresolved checks. Please review manually. This is not a CI failure.

Auto merge skipped after multiple 405 errors: PR may have conflicts or unresolved checks. Please review manually. This is not a CI failure.
xiaoxia closed this pull request 2026-07-28 09:43:25 +08:00

🗑️ 预览环境已清理

PR #1046 已关闭或合并,对应的预览环境已被清理。

如有需要,可以重新打开 PR 来重新生成预览环境。

🗑️ **预览环境已清理** PR #1046 已关闭或合并,对应的预览环境已被清理。 > 如有需要,可以重新打开 PR 来重新生成预览环境。
xiaoxia reopened this pull request 2026-07-28 12:18:38 +08:00
Collaborator

【阻塞级判定】

  • 是否存在阻塞级问题:否
  • 阻塞级问题数量:0 个

📊 审查概览

  • 整体评价:通过
  • 建议级问题数量:3 个

🔴 阻塞级问题(必须修复)

💡 改进建议(不阻塞合并)

  1. [apps/worker/video_processing/bgm_mixer_pure.py: validate_bgm_config] 浮点数 NaN 验证缺失

    • 具体内容:在 validate_bgm_config 函数中,虽然校验了数值范围,但未检查 NaN(Not a Number)。例如 float('nan') 会通过 isinstance(..., float) 检查,且 nan < 0nan > 1 均为 False,导致验证通过。建议引入 math 模块,增加 math.isnan(value) 检查。
  2. [apps/worker/video_processing/bgm_mixer_pure.py: calculate_loop_count] 极端输入下的循环次数风险

    • 具体内容:当 bgm_duration 极小(如 1e-6)且 target_duration 较大时,计算出的循环次数将非常巨大。虽然 Python 支持大整数,但传递给 FFmpeg 的 aloop 参数可能导致性能问题或异常。建议增加一个合理的上限(如 10000 或 100000),防止数值溢出或资源耗尽。
  3. [apps/worker/video_processing/bgm_mixer_pure.py: validate_bgm_config] 验证覆盖不完整

    • 具体内容:validate_bgm_config 函数仅校验了 volume, fade_in, fade_out, sidechain_ratio,但未校验 sidechain_attack, sidechain_release, sidechain_threshold。虽然 normalize_bgm_config 会修正这些值,但若该验证函数用于拒绝非法用户输入,则存在绕过风险。建议补充剩余参数的类型和范围校验,保持验证逻辑的一致性。

良好实践

  • 代码结构清晰,纯函数设计易于测试和维护。
  • 边界条件处理得当(如负数、零值处理)。
  • 类型注解和文档字符串完整,可读性高。
  • 单元测试覆盖了多种边界情况(包括负值、零值、淡出时长超过总时长等)。

格式检查通过 | 逻辑审查通过 | 性能良好


🤖 由 AI 代码审查机器人自动生成 | 2026-07-28 04:29:29 | 模型:

### 【阻塞级判定】 - 是否存在阻塞级问题:否 - 阻塞级问题数量:0 个 ### 📊 审查概览 - 整体评价:通过 - 建议级问题数量:3 个 ### 🔴 阻塞级问题(必须修复) 无 ### 💡 改进建议(不阻塞合并) 1. **[apps/worker/video_processing/bgm_mixer_pure.py: validate_bgm_config] 浮点数 NaN 验证缺失** - 具体内容:在 `validate_bgm_config` 函数中,虽然校验了数值范围,但未检查 `NaN`(Not a Number)。例如 `float('nan')` 会通过 `isinstance(..., float)` 检查,且 `nan < 0` 和 `nan > 1` 均为 False,导致验证通过。建议引入 `math` 模块,增加 `math.isnan(value)` 检查。 2. **[apps/worker/video_processing/bgm_mixer_pure.py: calculate_loop_count] 极端输入下的循环次数风险** - 具体内容:当 `bgm_duration` 极小(如 1e-6)且 `target_duration` 较大时,计算出的循环次数将非常巨大。虽然 Python 支持大整数,但传递给 FFmpeg 的 `aloop` 参数可能导致性能问题或异常。建议增加一个合理的上限(如 10000 或 100000),防止数值溢出或资源耗尽。 3. **[apps/worker/video_processing/bgm_mixer_pure.py: validate_bgm_config] 验证覆盖不完整** - 具体内容:`validate_bgm_config` 函数仅校验了 `volume`, `fade_in`, `fade_out`, `sidechain_ratio`,但未校验 `sidechain_attack`, `sidechain_release`, `sidechain_threshold`。虽然 `normalize_bgm_config` 会修正这些值,但若该验证函数用于拒绝非法用户输入,则存在绕过风险。建议补充剩余参数的类型和范围校验,保持验证逻辑的一致性。 ### ✅ 良好实践 - 代码结构清晰,纯函数设计易于测试和维护。 - 边界条件处理得当(如负数、零值处理)。 - 类型注解和文档字符串完整,可读性高。 - 单元测试覆盖了多种边界情况(包括负值、零值、淡出时长超过总时长等)。 --- ✅ 格式检查通过 | ✅ 逻辑审查通过 | ✅ 性能良好 --- <sub>🤖 由 AI 代码审查机器人自动生成 | 2026-07-28 04:29:29 | 模型: </sub> <!-- AI_CODE_REVIEW_AUTO_COMMENT -->
xiaoxia closed this pull request 2026-07-28 13:07:26 +08:00

🗑️ 预览环境已清理

PR #1046 已关闭或合并,对应的预览环境已被清理。

如有需要,可以重新打开 PR 来重新生成预览环境。

🗑️ **预览环境已清理** PR #1046 已关闭或合并,对应的预览环境已被清理。 > 如有需要,可以重新打开 PR 来重新生成预览环境。
Some checks are pending
PR Automation / Auto Approve on CI Green (pull_request) Successful in 5m4s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 30m29s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 20s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 1m10s
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 49s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 1m16s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Validate - Code Quality (pull_request) Failing after 2m33s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 58s
CI/CD Pipeline / PR Build Web Image (pull_request) Successful in 2m4s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 1m59s
AI Code Review / AI Code Review (pull_request) Successful in 2m9s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 7m15s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Unit Tests (pull_request) Successful in 3m52s
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Integration Tests (pull_request) Successful in 5m5s
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Successful in 30s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Has been cancelled
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 19s

Pull request closed

Sign in to join this conversation.