diff --git "a/.autopilot/knowledge/inbox/2026-09-28-autopilot-\350\260\223\350\257\215artifact\347\273\235\345\257\271\350\267\257\345\276\204\344\270\216gate\346\201\242\345\244\215.md" "b/.autopilot/knowledge/inbox/2026-09-28-autopilot-\350\260\223\350\257\215artifact\347\273\235\345\257\271\350\267\257\345\276\204\344\270\216gate\346\201\242\345\244\215.md" new file mode 100644 index 0000000..8e12197 --- /dev/null +++ "b/.autopilot/knowledge/inbox/2026-09-28-autopilot-\350\260\223\350\257\215artifact\347\273\235\345\257\271\350\267\257\345\276\204\344\270\216gate\346\201\242\345\244\215.md" @@ -0,0 +1,20 @@ +# autopilot 谓词 artifact 必须绝对路径 + §5.7 清 gate 后必须显式恢复 + + + +## 谓词 artifact 路径是字面 `-f` 检查(相对路径必挂) + +[2026-09-28] `## 验收场景` 谓词行写 `artifact: s1p1.out`(相对名)→ stop-hook §5.7 `validate_predicate_artifacts` 对该字面路径做 `-f` 存在性检查 → 仓库根下不存在 → 32 条全部 PRED-ARTIFACT-MISSING,即使文件真实存在于 `/tmp/autopilot-artifacts/`。 + +- **Lesson**:hook 的机械校验读的是 state.md 里的**字面字符串**,不做路径拼接/环境推断。 +- **How to apply**:冻结 `## 验收场景` 时 artifact 字段直接写绝对路径 `/tmp/autopilot-artifacts/.out`;测试内 writeArtifact 与之同源。worktree 多任务共享该目录,旧任务同名文件会混入——本次靠时间戳批次 + 内容匹配甄别。 + +## §5.7/§5.7b 校验失败会自动清 gate——修复后必须重设,否则空 gate 直落 §9 假性循环 + +- **Lesson**:PRED-ARTIFACT-MISSING / AC-FIELD-INVALID 等 block 都伴随 `set_field gate ""`;修复根因后若忘记重设 `gate: review-accept`,下一轮 hook 以空 gate 跳过全部 §5.5-§5.7 检查直落 §8/§9,报出**泛化的 phase 提示**(无任何具体信号),极易误判为「还有场景没做」而空转。 +- **How to apply**:每次修复 §5.7/§5.7b 类 block 后,同轮 Edit 恢复 gate;debug 时先读 frontmatter 的 gate 实际值,再对信号——**没有具体 PRED-*/AC-* 前缀的 qa 提示 = gate 为空的症状,不是新问题**。 +- 分级字段三元组(`e2e_status`/`leftover_critical`/`unexecuted_core_paths`)缺一即清 gate;`partial + >0` 时 hook 放行停等用户审批(`/autopilot approve`),这是设计行为不是 bug——auto-approve 的自动 merge 只认 `verified ∧ 0 ∧ 0`。 + +## 红蓝对抗冲突以设计 SSOT 裁决(红队测试=设计意图的代码化) + +红队谓词断言「护栏失败必须 reject(CLI exit≠0)」vs 蓝队测试断言「任何失败不抛(旁路容错)」——正解回设计文档找明文(D2「不匹配即 throw」只约束护栏),落地为错误类别区分(`DimensionAssertError` 传播/其余旁路),双方测试各自对齐。设计文档没写死的内部符号名(如函数改名)不构成蓝队偏差,红队按字面符号名断言属过度指定,改红队引用(E1 类)+ 留痕。 diff --git "a/.autopilot/knowledge/inbox/2026-09-28-\345\243\201\347\272\270\350\247\206\351\242\221\345\215\225\350\205\277\345\216\237\347\224\237\346\257\224\344\276\213.md" "b/.autopilot/knowledge/inbox/2026-09-28-\345\243\201\347\272\270\350\247\206\351\242\221\345\215\225\350\205\277\345\216\237\347\224\237\346\257\224\344\276\213.md" new file mode 100644 index 0000000..224f8c9 --- /dev/null +++ "b/.autopilot/knowledge/inbox/2026-09-28-\345\243\201\347\272\270\350\247\206\351\242\221\345\215\225\350\205\277\345\216\237\347\224\237\346\257\224\344\276\213.md" @@ -0,0 +1,27 @@ +# 壁纸视频单腿原生比例改版(v4)——固定画布 vs 原图比例多样取景崩坏 + + + +## 固定两档画布 vs 原图比例多样 = 构图崩坏(用户实测三周后推翻 v3 的第三层) + +[2026-09-28] 画布 v3(736×1600 对齐手机屏)落地后用户实测仍「很多视频很奇怪」:09-27 六人全家福(6000×4000 横 3:2)竖版裁得只剩爸爸一人、09-28 方形原图视频与静态壁纸取景对不上。 + +- **Lesson**:屏幕适配型固定画布(0.46/16:9)对多人合影/异形比例原图必然产生「最大脸窗口」式灾难性取景;预裁窗口策略(人脸/居中/attention)只是灾难程度的调节,不是解。构图忠实优先于屏幕铺满。 +- **Choice**:单腿原生比例——`computeNativeCanvas(origW, origH)` 纯函数 SSOT(`lib/wallpaper/native-canvas.ts`):R=clamp(max/min,1,2.4) 归一化 + 朝向解耦 + 像素预算 1177600(=旧竖版 token 等价)+ 短边≥704 + 32 取整;画廊 smart-fill 天然兼容任意比例(零改动);mac Aerial 仅画布 ratio∈[1.5,1.9] 微裁出 1920×1080 .mov,否则当日回退静态(`WallpaperCoordinator` aerial-fallback 链路天然承接)。GPU 时间减半。 +- **Evidence**:7 组参考 fixture 全命中;QA 像素级形变/裁切率检测;对话框 `dialog: 20260928-直接实现`。 + +## mmh3turbo 画布硬约束三条(写画布逻辑前必读) + +- **`--width/--height` 逐轴覆盖档位**(lmedia 已透传,须 32 倍数;`-r` 仅缺轴回退 + stdout 回执语义要保留)。 +- **锚定帧 LANCZOS 直接拉伸到画布**(`generate.py` resize,不裁剪)——预裁剪比例必须精确等于画布比例,否则首帧即刻变形。 +- **EXIF orientation 5-8 宽高互换**是画布朝向的前置(iPhone 竖拍主场景)——`readOrientedDimensions` 必须先归一化再喂 `computeNativeCanvas`。 + +## 失败语义按类别区分(护栏 vs 旁路) + +- **Lesson**:「任一环节失败都不抛」的旁路容错会把尺寸护栏的语义吞掉(rerun CLI exit=0、错尺寸静默);反过来全量重抛又破坏既有旁路(生成失败当日静态回退)。 +- **Choice**:`DimensionAssertError` 专用错误类——护栏失败传播为 job 失败(worker 标记失败 / rerun exit≠0,红队 SSOT 5.P1),其余失败(spawn/上传/DB)维持旁路吞掉(契约 8)。两条转码+护栏全过才上传/写列,结构上杜绝半成品日。 +- **How to apply**:护栏顺序在转码后、上传/写列前;错误分类靠类型不靠 message 字符串匹配。 + +## 历史两腿产物不重生成、不 pad 修复(用户拍板) + +内容取景问题(裁人)像素在生成时定死,转码不可逆;旧 704×1216 的画廊裁字问题可选 CPU 级 blur-pad 修复但默认不做(09-10~09-25 共 15 条)。新链路兼容:DB 加列 `wallpaper_video_native_url`、COS 新 key `_native.mp4`、manifest 新字段条件展开、画廊读取 native>portrait>landscape。 diff --git a/CLAUDE.md b/CLAUDE.md index e13a79e..0966654 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -123,7 +123,7 @@ packages/shared/ # 共享类型、Zod Schema、API 路由常量 - Worker 进程 (`src/workers/index.ts`) 独立于 API 服务运行 - 扫描流程 (`scan-storage.ts`): 增量扫描 — 用 mtime+size 快速跳过未变更文件,仅对新文件/修改文件做 SHA256 + 缩略图生成,最后入队 analyze-photo;扫描结束后调用 `detectBursts` 识别连拍组(时间窗口 ≤3s + dHash 汉明距离 ≤10),写入 `bursts` 表并标记每组代表 - 分析流程 (`analyze-photo.ts`): 读文件 base64 → 调 AI 视觉模型 → 解析 JSON 响应 → 写入 tags/photoTags/photoAnalyses(幂等设计,重复分析会 UPDATE 而非 INSERT);分析完成后调用 `calibrateBurstRepresentative` 在组内竞争代表位(选评分最高者) -- 精选流程 (`daily-selection.ts`): 多条目并行流水线 — `buildCandidatePool`(4 源混采 + 可选 recent 第 5 源 + 跨表去重 + 主力源美学下限 `minAestheticScorePrimary` 默认 ≥7.0、fillUp ≥7.5;recent 源 = 近 `DAILY_RECENT_SOURCE_DAYS`(默认 30)天拍摄、同美学门槛、源内按美学分竞争、quota 保底 1 席,`DAILY_RECENT_SOURCE` 默认关——平权改版后近期照片在源内 aes 竞争中被历史同月高分照挤出 LIMIT,dry run 验证 recent 源使近 30 天照片从 0.25 → 6.0 席/天,真实代码 A/B 见 `src/cli/dry-run-real-pool.ts`)→ **select AI 评选阶段**(`runSelectStage`:文本模型从候选摘要重排 hero,`weightedScore` 降序为兜底;5 路 fallback 保序:`dailySelectEnabled===false`/候选<2 零 AI/抛错/解析失败/越界)→ pLimit 并发为每张独立执行 narrate(vision)+select members(text),生成各自 title/narrative/members;db.transaction 批量 DELETE+INSERT 写入 `dailyPickEntries`(幂等覆盖,UNIQUE(dailyPickId,rank));entries[0] 同步作为 dailyPicks 主记录;阶段3 调 Satori 合成杂志版 DailyHero 壁纸(5K 16:9,基于 entries[0])落盘,路径写入 `dailyPicks.composedImagePath`,并追加合成手机竖版壁纸(1290×2796,B 方案全屏照片+底部渐变压白字,cacheKey `1290x2796` 与路由/推送三方闭合,独立 try/catch 不阻塞主流程);**阶段4 动态视频壁纸**(`DAILY_WALLPAPER_VIDEO` 默认关):开关开 ∧ hero 非视频 ∧ composedImagePath 非空 → 链式 one-off enqueue `wallpaper-video` job(独立 try/catch 旁路,失败不影响精选)→ 串行「横生成→横转码→竖生成→竖转码」(spawn honeydo 双锚定伪循环 `--first-frame`/`--last-frame` 同图,sharp 预裁剪对齐生成画布 1280×704 / 736×1600(竖版经 lmedia `--width/--height` 逐轴覆盖 portrait 档,比例 0.46 对齐静态竖版壁纸 1290×2796,画廊手机端 cover 裁切 ~20%→<0.5%);**prompt 解析链**:`daily_picks.motion_prompt`(2026-09-25 起由 hero-only 两步链路生成:本地 qwen vision 产**中立画面事实记录**(严禁运动词)→ 外部 deepseek 纯文本写 30-50 字运动描述 + Audio 环境音指引(准则逐字沿用原 narrate 版,`v2/daily/motion-facts/` + `v2/daily/motion/`,lib 接缝 `lib/motion/generate.ts`);narrate 契约已收窄不再产 motionPrompt;任一步失败不写库回退分层默认 + console.warn)→ 无则按有无人脸分层默认 `WALLPAPER_VIDEO_PROMPT_PERSON/SCENE`(2026-09-13 修订:废除全场景单一收敛句式);横转 HEVC hvc1 .mov 1920×1080 带音轨 aac 128k +faststart 供 Aerial 注入(双轨均保留音轨),竖转 H.264 mp4 736×1600 带音轨 +faststart 供画廊;**Remotion 横版两栏布局**(视频居左 contain + 右侧文字栏,几何/字号/配色逐字对齐静态模版 dailyHeroJSX,composition 1920×1080=Aerial 原生 16:9)+ 竖版全屏铺底维持 v2;footer 拍摄时刻与 web/静态壁纸同源 `formatPhotoCaptureTime`(后端算好 `captureDateline` 传入 props,takenAt 缺失 footer 不渲染))→ COS 上传(回执非空串才写 `daily_picks.wallpaper_video_landscape_url/portrait_url` 两列)→ `syncDayToGallery` 重建 manifest(视频字段条件展开:DB 列空 → JSON 字段缺省);手动重跑 `npm run wallpaper-video:rerun -- --pickDate=YYYY-MM-DD`;**候选池排序**:`weightedScore = aestheticScore`(2026-09-04 起年代完全平权,无年份限制、无 ageBonus——此前乘法 1.6× → 加法封顶 +0.3 → 完全移除;新照片与老照片同台,只比美学质量);**家人主角**:候选摘要与 narrate 均注入 `peopleNicknames`(画面家人称呼),select prompt 置最高优先级规则「家人出镜优先于纯宠物/风景」;**定时任务自愈**:`daily-selection-cron` job 触发时先按升序补跑最近 `DAILY_AUTO_HEAL_DAYS`(默认 7)天缺失的 dailyPicks(内层 job name=`auto-heal`,单日失败不中断),再跑今天——宕机几天可自动恢复,超大历史缺口仍用手动 `backfill:daily-picks` CLI(`--enqueue` + worker 慢慢消化) +- 精选流程 (`daily-selection.ts`): 多条目并行流水线 — `buildCandidatePool`(4 源混采 + 可选 recent 第 5 源 + 跨表去重 + 主力源美学下限 `minAestheticScorePrimary` 默认 ≥7.0、fillUp ≥7.5;recent 源 = 近 `DAILY_RECENT_SOURCE_DAYS`(默认 30)天拍摄、同美学门槛、源内按美学分竞争、quota 保底 1 席,`DAILY_RECENT_SOURCE` 默认关——平权改版后近期照片在源内 aes 竞争中被历史同月高分照挤出 LIMIT,dry run 验证 recent 源使近 30 天照片从 0.25 → 6.0 席/天,真实代码 A/B 见 `src/cli/dry-run-real-pool.ts`)→ **select AI 评选阶段**(`runSelectStage`:文本模型从候选摘要重排 hero,`weightedScore` 降序为兜底;5 路 fallback 保序:`dailySelectEnabled===false`/候选<2 零 AI/抛错/解析失败/越界)→ pLimit 并发为每张独立执行 narrate(vision)+select members(text),生成各自 title/narrative/members;db.transaction 批量 DELETE+INSERT 写入 `dailyPickEntries`(幂等覆盖,UNIQUE(dailyPickId,rank));entries[0] 同步作为 dailyPicks 主记录;阶段3 调 Satori 合成杂志版 DailyHero 壁纸(5K 16:9,基于 entries[0])落盘,路径写入 `dailyPicks.composedImagePath`,并追加合成手机竖版壁纸(1290×2796,B 方案全屏照片+底部渐变压白字,cacheKey `1290x2796` 与路由/推送三方闭合,独立 try/catch 不阻塞主流程);**阶段4 动态视频壁纸**(`DAILY_WALLPAPER_VIDEO` 默认关):开关开 ∧ hero 非视频 ∧ composedImagePath 非空 → 链式 one-off enqueue `wallpaper-video` job(独立 try/catch 旁路,失败不影响精选)→ **单腿原生比例生成**(20260928 起替代旧固定横竖两腿;画布 SSOT `computeNativeCanvas`(`lib/wallpaper/native-canvas.ts`)跟随 hero 原图 EXIF 旋转后比例:32 取整 + 像素预算 1177600 封顶 + 短边 ≥704 + 长短轴比 clamp [1,2.4],朝向由原图独立决定)(spawn honeydo 双锚定伪循环 `--first-frame`/`--last-frame` 同图 + `--width/--height` 画布显式覆盖,sharp 预裁仅做比例对齐 cover 微裁、已去除 v2 人脸窗口重构图;**prompt 解析链**:`daily_picks.motion_prompt`(2026-09-25 起由 hero-only 两步链路生成:本地 qwen vision 产**中立画面事实记录**(严禁运动词)→ 外部 deepseek 纯文本写 30-50 字运动描述 + Audio 环境音指引(准则逐字沿用原 narrate 版,`v2/daily/motion-facts/` + `v2/daily/motion/`,lib 接缝 `lib/motion/generate.ts`);narrate 契约已收窄不再产 motionPrompt;任一步失败不写库回退分层默认 + console.warn)→ 无则按有无人脸分层默认 `WALLPAPER_VIDEO_PROMPT_PERSON/SCENE`(2026-09-13 修订:废除全场景单一收敛句式);双转码:画廊原生 H.264 mp4 带音轨(分辨率==画布,恒产出)+ 条件 Aerial HEVC hvc1 .mov 1920×1080 带音轨(仅画布比例 ∈[1.5,1.9] 时 16:9 居中微裁产出,窗口外 landscape 列留空 → mac App 当日静态回退零改动);**尺寸护栏** `assertVideoDimensions`(转码后/上传写列前 ffprobe 断言尺寸,`DimensionAssertError` 传播为 job 失败(rerun exit≠0),其余失败维持旁路当日回退静态;两条转码+护栏全过才上传/写列,杜绝半成品日);**Remotion 文字层**按画布比例选 comp(≥0.9 横版两栏 / <0.9 竖版全屏,props 携画布宽高 calculateMetadata 动态尺寸;横版几何逐字对齐静态模版 dailyHeroJSX);footer 拍摄时刻与 web/静态壁纸同源 `formatPhotoCaptureTime`(后端算好 `captureDateline` 传入 props,takenAt 缺失 footer 不渲染))→ COS 上传(回执非空串才写 `daily_picks.wallpaper_video_native_url`(20260928 新列)与 `wallpaper_video_landscape_url`(语义收窄为「16:9 适配版」);`wallpaper_video_portrait_url` 为 legacy 不再新写、存量保留)→ `syncDayToGallery` 重建 manifest(视频字段条件展开:DB 列空 → JSON 字段缺省);手动重跑 `npm run wallpaper-video:rerun -- --pickDate=YYYY-MM-DD`;**候选池排序**:`weightedScore = aestheticScore`(2026-09-04 起年代完全平权,无年份限制、无 ageBonus——此前乘法 1.6× → 加法封顶 +0.3 → 完全移除;新照片与老照片同台,只比美学质量);**家人主角**:候选摘要与 narrate 均注入 `peopleNicknames`(画面家人称呼),select prompt 置最高优先级规则「家人出镜优先于纯宠物/风景」;**定时任务自愈**:`daily-selection-cron` job 触发时先按升序补跑最近 `DAILY_AUTO_HEAL_DAYS`(默认 7)天缺失的 dailyPicks(内层 job name=`auto-heal`,单日失败不中断),再跑今天——宕机几天可自动恢复,超大历史缺口仍用手动 `backfill:daily-picks` CLI(`--enqueue` + worker 慢慢消化) **AI 层** (`src/ai/`): - `client.ts` — OpenAI 兼容的 AI 客户端,使用 `openai` npm 包,禁用 qwen3.6 的 thinking 模式确保 JSON 输出在 `content` 字段;`chatWithModel`(2026-09-25)支持 per-call 覆盖 model/baseUrl/apiKey 供外部 deepseek 运动描述通路使用,thinking 方言参数按 provider 条件化(仅本地 qwen 端点注入 `chat_template_kwargs`) @@ -222,10 +222,11 @@ packages/shared/ # 共享类型、Zod Schema、API 路由常量 ↓ 阶段4: 动态视频壁纸 (DAILY_WALLPAPER_VIDEO 默认关) 链式 enqueue wallpaper-video job → honeydo 微动化 hero 照片 - (双锚定伪循环,横 1280×704 / 竖 736×1600 画布预裁剪) - → 横转 HEVC hvc1 .mov 1920×1080 / 竖转 H.264 mp4 736×1600 - (双轨均带音轨 aac +faststart)→ COS 上传 → 写 daily_picks 两列 → 画廊同步 - 失败当日回退静态(旁路容错,不影响精选主流程) + (双锚定伪循环,单腿原生画布 computeNativeCanvas: + 跟随原图比例,32 取整 + 像素预算 1177600 封顶 + 短边 ≥704) + → 双转码:画廊原生 mp4(恒产出)/ Aerial 16:9 微裁 .mov(画布比例∈[1.5,1.9]) + (尺寸护栏断言后上传)→ COS → 写 daily_picks native/landscape 列 → 画廊同步 + 失败当日回退静态(护栏 DimensionAssertError 传播 job 失败,其余旁路容错) ↓ API: GET /api/daily/today → DailyPick { entries: DailyPickEntry[] } ↓ @@ -241,11 +242,13 @@ packages/shared/ # 共享类型、Zod Schema、API 路由常量 daily-selection 阶段 3.5(阶段 4 壁纸视频完成后经 syncDayToGallery 幂等重建)/ daily-video 步骤 4.5 → syncDayToGallery / syncVideoToGallery(独立 try/catch 旁路)→ 产物上传腾讯云 COS(公有读;壁纸视频 - key = `relight/wallpaper-videos/{pickDate}_landscape.mov` / `_portrait.mp4`, + key = `relight/wallpaper-videos/{pickDate}_native.mp4`(20260928 单腿原生)/ + `_landscape.mov`(仅 16:9 兼容日产出;`_portrait.mp4` 为 legacy 不再新写), manifest 视频字段条件展开——DB 回执列空则字段缺省)+ buildManifest → scp+ssh mv 原子推 manifest.json 到 VPS → 静态站拉 manifest 渲染 `#/` 沉浸流 + `#/video/` 视频深链 + `#/?date=&rank=` 日期/照片深链 - (date 级无 rank 深链优先落当日壁纸卡,动态变体自动播放;OKLCH 品牌色单页站)。沉浸流每日单元顺序 = 日期分隔卡 → 壁纸卡(含动态视频变体, + (date 级无 rank 深链优先落当日壁纸卡,动态变体自动播放;OKLCH 品牌色单页站)。沉浸流每日单元顺序 = 日期分隔卡 → 壁纸卡(含动态视频变体,变体 URL 优先级 + native > portrait(legacy) > landscape(legacy),皆空回退静态卡, 主钮「保存视频」+「更多」菜单收静态/电脑版)→ 主题视频卡 → 照片;壁纸/视频卡展示区 按 cropRatio 智能拉通(≤0.3 cover,否则 contain + 模糊垫底)。daily-video 推送 URL 改用 `config.galleryPublicUrl + /#/video/`(修原 localhost bug)。 diff --git a/apps/backend/package.json b/apps/backend/package.json index 2ca9c84..324f9f8 100644 --- a/apps/backend/package.json +++ b/apps/backend/package.json @@ -1,6 +1,6 @@ { "name": "@relight/backend", - "version": "0.11.0", + "version": "0.12.0", "private": true, "type": "module", "scripts": { diff --git a/apps/backend/src/__tests__/helpers/test-schema.ts b/apps/backend/src/__tests__/helpers/test-schema.ts index bfbc378..1c630d0 100644 --- a/apps/backend/src/__tests__/helpers/test-schema.ts +++ b/apps/backend/src/__tests__/helpers/test-schema.ts @@ -133,6 +133,7 @@ export function setupTestSchema(sqlite: Database.Database, opts: SetupOptions = members TEXT NOT NULL DEFAULT '[]', wallpaper_video_landscape_url TEXT, wallpaper_video_portrait_url TEXT, + wallpaper_video_native_url TEXT, motion_prompt TEXT, created_at TEXT NOT NULL ); diff --git a/apps/backend/src/__tests__/wallpaper-video-canvas-contract.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-canvas-contract.acceptance.test.ts index e3de6fb..3d7e019 100644 --- a/apps/backend/src/__tests__/wallpaper-video-canvas-contract.acceptance.test.ts +++ b/apps/backend/src/__tests__/wallpaper-video-canvas-contract.acceptance.test.ts @@ -6,11 +6,15 @@ * 非法(非 32 倍数)→ exit 2。R4 机器可读验证:dist 产物含 `--width`; * `honeydo video gen … --width 700` → exit 2 且 stderr 含 `--width`(700=21.875×32 非法)。 * - C2 relight spawn:`HoneydoVideoOptions` 可选 `width?/height?`;`spawnHoneydoVideo` - * 仅在传入时追加 `--width`/`--height`;两条腿均传(竖 736×1600 / 横 1280×704)。 - * - C3 画布常量:`WALLPAPER_VIDEO_PORTRAIT_CANVAS = { width: 736, height: 1600 }`; - * R3 假绿陷阱:旧字面量 704×1216 必须从实现(含注释)中全部清除。 - * - C4 Remotion composition:`wallpaper-overlay-portrait` = width 736 / height 1600; - * props 契约 {videoPath,pickDate,title,narrative,captureDateline} 不变。 + * 仅在传入时追加 `--width`/`--height`;单腿显式传画布(computeNativeCanvas 产出)。 + * - C3 画布 SSOT(20260928 单腿原生改版):`native-canvas.ts` 导出 + * `computeNativeCanvas(origW, origH)` + `NATIVE_CANVAS_PIXEL_BUDGET = 1177600` + + * `NATIVE_CANVAS_MIN_SHORT = 704`;固定画布常量 + * `WALLPAPER_VIDEO_LANDSCAPE_CANVAS` / `WALLPAPER_VIDEO_PORTRAIT_CANVAS` 必须从 + * backend src 全部清除(R3 假绿陷阱:旧字面量 704×1216 不再作为现行画布)。 + * - C4 Remotion composition:`calculateMetadata` 从 props `canvasWidth/canvasHeight` + * 动态返回 width/height(尺寸参数化);props 契约(20260928 契约 7 纯超集扩展) + * {videoPath,pickDate,title,narrative,captureDateline,canvasWidth,canvasHeight}。 * - C5 DB 列:`daily_picks.motion_prompt` 列名与语义不变,不新增列。 * - C7 prompt 目录:`v2/daily/motion-facts/{system,user}.txt` 与 `v2/daily/motion/{system,user}.txt`。 * - C8 配置:`config.ai.motionBaseUrl/motionApiKey/motionModel`(env `AI_MOTION_BASE_URL/ @@ -148,29 +152,39 @@ dHoneydo("C1:honeydo CLI 透传 --width/--height(R4 机器可读验证)", // C2/C3:relight spawn 契约字面量 + 旧画布字面量清除(R3 假绿陷阱) // ============================================================================ -describe("C2/C3:relight 侧画布常量与 spawn 契约字面量", () => { +describe("C2/C3:relight 侧画布 SSOT(native-canvas.ts)与 spawn 契约字面量", () => { const corpus = collectCorpus(BACKEND_SRC); + const nativeCanvasSrc = readText( + path.join(BACKEND_SRC, "lib/wallpaper/native-canvas.ts"), + "lib/wallpaper/native-canvas.ts", + ); + + it("含画布 SSOT computeNativeCanvas 导出(尺寸计算入口,场景谓词实现绑定)", () => { + expect(nativeCanvasSrc).toMatch(/export\s+function\s+computeNativeCanvas/); + expect(nativeCanvasSrc).toMatch(/origW:\s*number/); + expect(nativeCanvasSrc).toMatch(/origH:\s*number/); + }); + + it("像素预算 BUDGET=1177600 与短轴下限 MIN_SHORT=704 落在 native-canvas.ts(SSOT 逐字)", () => { + expect(nativeCanvasSrc).toMatch(/NATIVE_CANVAS_PIXEL_BUDGET\s*=\s*1_177_600/); + expect(nativeCanvasSrc).toMatch(/NATIVE_CANVAS_MIN_SHORT\s*=\s*704/); + // 比例 clamp 上界 2.40(design D1 逐字) + expect(nativeCanvasSrc).toMatch(/NATIVE_CANVAS_MAX_RATIO\s*=\s*2\.4/); + }); - it("含新画布常量 WALLPAPER_VIDEO_PORTRAIT_CANVAS 且值落在 736×1600", () => { - expect(corpus, "backend src 未声明 WALLPAPER_VIDEO_PORTRAIT_CANVAS").toMatch( + it("固定画布常量已删除(R3 假绿陷阱:20260928 单腿原生,横竖两档画布不再是现行契约)", () => { + expect(corpus, "WALLPAPER_VIDEO_LANDSCAPE_CANVAS 残留").not.toMatch( + /WALLPAPER_VIDEO_LANDSCAPE_CANVAS/, + ); + expect(corpus, "WALLPAPER_VIDEO_PORTRAIT_CANVAS 残留").not.toMatch( /WALLPAPER_VIDEO_PORTRAIT_CANVAS/, ); - // C3 逐字:{ width: 736, height: 1600 }——锚定「定义处」(含类型标注),避免命中 import/引用点 - const defAnchor = /WALLPAPER_VIDEO_PORTRAIT_CANVAS(\s*:\s*[^=]+)?\s*=\s*\{/; - expect(corpus, "未找到 WALLPAPER_VIDEO_PORTRAIT_CANVAS 的定义(= { 形态)").toMatch(defAnchor); - const constBlock = regionFrom(corpus, defAnchor, 300); - expect(constBlock).toMatch(/736/); - expect(constBlock).toMatch(/1600/); - // No-op kill:旧值 704/1216 若仍作为竖版画布常量出现,此处即红 - expect(constBlock).not.toMatch(/704/); - expect(constBlock).not.toMatch(/1216/); + expect(corpus, "WALLPAPER_VIDEO_PORTRAIT_RES 残留").not.toMatch(/WALLPAPER_VIDEO_PORTRAIT_RES/); }); it("旧画布字面量 704×1216 不再被表述为现行画布(R3:实现注释也必须手改)", () => { // R3 假绿陷阱的字面量面:只打击「把旧画布当现行」的表述—— - // 行内仅出现旧值(704+1216)而不同时出现新值(736/1600)= 陈旧声明,红; - // 新旧同行的迁移说明(C2 要求的横竖版决策注释)不算残留。 - // 横版 1280×704 不含 1216,天然不命中。 + // 行内仅出现旧值(704+1216)= 陈旧声明,红。 const staleLines: string[] = []; const walk = (d: string): void => { for (const entry of fs.readdirSync(d, { withFileTypes: true })) { @@ -182,8 +196,7 @@ describe("C2/C3:relight 侧画布常量与 spawn 契约字面量", () => { const src = fs.readFileSync(p, "utf-8"); src.split("\n").forEach((line, i) => { const mentionsOld = line.includes("704") && line.includes("1216"); - const mentionsNew = line.includes("736") || line.includes("1600"); - if (mentionsOld && !mentionsNew) { + if (mentionsOld) { staleLines.push(`${path.relative(REPO_ROOT, p)}:${i + 1}: ${line.trim()}`); } }); @@ -195,8 +208,17 @@ describe("C2/C3:relight 侧画布常量与 spawn 契约字面量", () => { expect(staleLines, `旧竖版画布字面量残留:\n${staleLines.join("\n")}`).toHaveLength(0); }); + it("job 层从 native-canvas 引入画布 SSOT(computeNativeCanvas import 锚定)", () => { + const job = readText( + path.join(BACKEND_SRC, "jobs/wallpaper-video.ts"), + "jobs/wallpaper-video.ts", + ); + expect(job).toMatch(/computeNativeCanvas/); + expect(job).toMatch(/native-canvas/); + }); + it("spawn 侧含 --width/--height args 追加与 HoneydoVideoOptions 可选 width?/height?", () => { - // C2 逐字:HoneydoVideoOptions 新增可选 width?/height? + // C2 逐字:HoneydoVideoOptions 可选 width?/height? expect(corpus).toMatch(/HoneydoVideoOptions/); const typeBlock = regionFrom(corpus, /HoneydoVideoOptions/, 1600); expect(typeBlock, "HoneydoVideoOptions 未声明可选 width?").toMatch(/width\?/); @@ -205,15 +227,9 @@ describe("C2/C3:relight 侧画布常量与 spawn 契约字面量", () => { expect(corpus).toMatch(/spawnHoneydoVideo/); expect(corpus).toContain("--width"); expect(corpus).toContain("--height"); - // 两条腿均传:竖 736×1600、横 1280×704(1280 与 704 须以横版组合出现) - expect(corpus).toMatch(/1280/); }); }); -// ============================================================================ -// C4:Remotion composition 尺寸与 props 契约 -// ============================================================================ - // overlay 工程在 .autopilot/runtime/ 下(不入库)——CI 无此目录,capability-gate 惯例同前 const OVERLAY_PRESENT = fs.existsSync(OVERLAY_DIR); if (!OVERLAY_PRESENT) { @@ -223,7 +239,7 @@ if (!OVERLAY_PRESENT) { } const dOverlay = OVERLAY_PRESENT ? describe : describe.skip; -dOverlay("C4:Remotion wallpaper-overlay-portrait composition 契约", () => { +dOverlay("C4:Remotion comp 尺寸参数化(calculateMetadata + props 画布契约)", () => { const overlayCorpus = (() => { // describe.skip 仍会执行本收集回调——目录缺失(CI)时直接返回空 corpus 防 ENOENT // (2026-09-26 CI 实证:探针 WARN 打了,readdirSync 照样崩) @@ -244,19 +260,33 @@ dOverlay("C4:Remotion wallpaper-overlay-portrait composition 契约", () => { return out.join("\n"); })(); - it("composition id wallpaper-overlay-portrait 存在且尺寸为 736×1600", () => { + it("两个 comp id 存在且挂 calculateMetadata(尺寸参数化,kill 写死)", () => { + expect(overlayCorpus).toMatch(/wallpaper-overlay-landscape/); expect(overlayCorpus).toMatch(/wallpaper-overlay-portrait/); - expect(overlayCorpus).toMatch(/736/); - expect(overlayCorpus).toMatch(/1600/); - // 旧尺寸清除(同 C3 理由) + expect(overlayCorpus).toMatch(/calculateMetadata/); + expect(overlayCorpus).toMatch(/canvasWidth/); + expect(overlayCorpus).toMatch(/canvasHeight/); + // 旧固定档清除:comp 声明不再含历史画布字面量 expect(overlayCorpus).not.toMatch(/1216/); }); - it("props 契约五字段逐字不变:{videoPath,pickDate,title,narrative,captureDateline}", () => { - for (const prop of ["videoPath", "pickDate", "title", "narrative", "captureDateline"]) { + it("props 契约七字段逐字(20260928 契约 7 纯超集扩展)", () => { + for (const prop of [ + "videoPath", + "pickDate", + "title", + "narrative", + "captureDateline", + "canvasWidth", + "canvasHeight", + ]) { expect(overlayCorpus, `overlay 工程缺 props 字段 ${prop}`).toContain(prop); } }); + + it("布局分支按 props 画布比例判定(≥0.9 两栏横版 / <0.9 竖版全屏)", () => { + expect(overlayCorpus).toMatch(/0\.9/); + }); }); // ============================================================================ diff --git a/apps/backend/src/__tests__/wallpaper-video-cli.test.ts b/apps/backend/src/__tests__/wallpaper-video-cli.test.ts index 6ae2a22..27857ce 100644 --- a/apps/backend/src/__tests__/wallpaper-video-cli.test.ts +++ b/apps/backend/src/__tests__/wallpaper-video-cli.test.ts @@ -1,10 +1,11 @@ /** * 单测:wallpaper-video rerun CLI 纯函数(任务 4 契约测试兜底) * - * 契约(state.md ## 契约规约 CLI(rerun)契约): + * 契约(state.md ## 契约规约 CLI(rerun)契约;20260928 单腿原生改版): * 命令:npm run wallpaper-video:rerun -- --pickDate=YYYY-MM-DD * 参数 --pickDate(必填,isValidYmd 校验) - * 末行 JSON {pickDate, landscape: , portrait: } + * 末行 JSON {pickDate, native: , landscape: } + * (native=单腿原生 mp4 回执;landscape=16:9 兼容 .mov 回执,仅兼容日非空) */ import { describe, expect, it } from "vitest"; import { formatResultLine, isValidYmd, parsePickDateArg } from "../cli/wallpaper-video"; @@ -40,22 +41,22 @@ describe("isValidYmd", () => { }); describe("formatResultLine", () => { - it("末行 JSON 契约:成功", () => { + it("末行 JSON 契约:成功(兼容日 native + landscape 双回执)", () => { const line = formatResultLine( "2026-09-12", + "https://b.cos.ap-shanghai.myqcloud.com/relight/wallpaper-videos/2026-09-12_native.mp4", "https://b.cos.ap-shanghai.myqcloud.com/relight/wallpaper-videos/2026-09-12_landscape.mov", - "https://b.cos.ap-shanghai.myqcloud.com/relight/wallpaper-videos/2026-09-12_portrait.mp4", ); const parsed = JSON.parse(line) as Record; expect(parsed.pickDate).toBe("2026-09-12"); + expect(parsed.native).toContain("_native.mp4"); expect(parsed.landscape).toContain("_landscape.mov"); - expect(parsed.portrait).toContain("_portrait.mp4"); - expect(Object.keys(parsed).sort()).toEqual(["landscape", "pickDate", "portrait"]); + expect(Object.keys(parsed).sort()).toEqual(["landscape", "native", "pickDate"]); }); it("末行 JSON 契约:失败为空串(非 null/缺省)", () => { const parsed = JSON.parse(formatResultLine("2026-09-12", "", "")) as Record; + expect(parsed.native).toBe(""); expect(parsed.landscape).toBe(""); - expect(parsed.portrait).toBe(""); }); }); diff --git a/apps/backend/src/__tests__/wallpaper-video-contract-literals.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-contract-literals.acceptance.test.ts index 5e33792..490c023 100644 --- a/apps/backend/src/__tests__/wallpaper-video-contract-literals.acceptance.test.ts +++ b/apps/backend/src/__tests__/wallpaper-video-contract-literals.acceptance.test.ts @@ -55,15 +55,19 @@ function regionFrom(source: string, anchor: RegExp, chars: number): string { // 三方字段名(规则 6 的三方:manifest 产出 ↔ 画廊消费 ↔ API 暴露) const MANIFEST_FIELD_LANDSCAPE = "wallpaperVideoLandscape"; const MANIFEST_FIELD_PORTRAIT = "wallpaperVideoPortrait"; +const MANIFEST_FIELD_NATIVE = "wallpaperVideoNative"; const GALLERY_CONSUMED_PORTRAIT = "wallpaperVideoPortrait"; +const GALLERY_CONSUMED_NATIVE = "wallpaperVideoNative"; const API_FIELD_URL = "wallpaperVideoUrl"; // DB 列名(snake_case,§后端设计 §2 逐字) const DB_COL_LANDSCAPE = "wallpaper_video_landscape_url"; const DB_COL_PORTRAIT = "wallpaper_video_portrait_url"; +const DB_COL_NATIVE = "wallpaper_video_native_url"; // COS key / contentType(§契约规约 逐字) const COS_KEY_DIR = "wallpaper-videos/"; const COS_KEY_LANDSCAPE_SUFFIX = "_landscape.mov"; const COS_KEY_PORTRAIT_SUFFIX = "_portrait.mp4"; +const COS_KEY_NATIVE_SUFFIX = "_native.mp4"; const CONTENT_TYPE_LANDSCAPE = "video/quicktime"; const CONTENT_TYPE_PORTRAIT = "video/mp4"; // 开关(§后端设计 §1 逐字) @@ -160,7 +164,7 @@ describe("队列注册契约:wallpaperVideoQueue 显式 defaultJobOptions { at // ============================================================================ describe("COS key 与 contentType 契约字面量(§契约规约 逐字)", () => { - it("manifest.ts 含 wallpaper-videos/ 目录与 _landscape.mov / _portrait.mp4 key 字面量", () => { + it("manifest.ts 含 wallpaper-videos/ 目录与 _landscape.mov / _portrait.mp4 / _native.mp4 key 字面量", () => { const src = readText( path.join(BACKEND_SRC, "lib/gallery/manifest.ts"), "lib/gallery/manifest.ts", @@ -168,6 +172,7 @@ describe("COS key 与 contentType 契约字面量(§契约规约 逐字)", ( expect(src).toContain(COS_KEY_DIR); expect(src).toContain(COS_KEY_LANDSCAPE_SUFFIX); expect(src).toContain(COS_KEY_PORTRAIT_SUFFIX); + expect(src).toContain(COS_KEY_NATIVE_SUFFIX); }); it("contentType 字面量 video/quicktime 与 video/mp4 存在于 job/视频库/manifest 之一", () => { @@ -184,10 +189,11 @@ describe("COS key 与 contentType 契约字面量(§契约规约 逐字)", ( expect(corpus).toContain(CONTENT_TYPE_PORTRAIT); }); - it("schema.ts 含两列 snake_case 列名(nullable TEXT 语义由列名锚定)", () => { + it("schema.ts 含三列 snake_case 列名(native 20260928 新增;nullable TEXT 语义由列名锚定)", () => { const src = readText(path.join(BACKEND_SRC, "db/schema.ts"), "db/schema.ts"); expect(src).toContain(DB_COL_LANDSCAPE); expect(src).toContain(DB_COL_PORTRAIT); + expect(src).toContain(DB_COL_NATIVE); }); }); @@ -196,17 +202,24 @@ describe("COS key 与 contentType 契约字面量(§契约规约 逐字)", ( // ============================================================================ describe("规则 6:三方字段名逐字一致(manifest ↔ 画廊 ↔ API)", () => { - it("manifest 产出侧(lib/gallery/manifest.ts)含 wallpaperVideoLandscape / wallpaperVideoPortrait 两个逐字字段名", () => { + it("manifest 产出侧(lib/gallery/manifest.ts)含 wallpaperVideoLandscape / wallpaperVideoPortrait / wallpaperVideoNative 逐字字段名", () => { const src = readText(path.join(BACKEND_SRC, "lib/gallery/manifest.ts"), "manifest.ts"); expect(src).toContain(MANIFEST_FIELD_LANDSCAPE); expect(src).toContain(MANIFEST_FIELD_PORTRAIT); + expect(src).toContain(MANIFEST_FIELD_NATIVE); }); - it("画廊消费侧(apps/gallery/app.js)消费的字段名与 manifest 产出侧逐字相同(wallpaperVideoPortrait)", () => { + it("画廊消费侧(apps/gallery/app.js)消费的字段名与 manifest 产出侧逐字相同(native 优先 + portrait legacy 回退)", () => { const src = readText(path.join(REPO_ROOT, "apps/gallery/app.js"), "apps/gallery/app.js"); expect(src).toContain(GALLERY_CONSUMED_PORTRAIT); + expect(src).toContain(GALLERY_CONSUMED_NATIVE); // 逐字一致(同源字面量比较——名称漂移在此即红) expect(GALLERY_CONSUMED_PORTRAIT).toBe(MANIFEST_FIELD_PORTRAIT); + expect(GALLERY_CONSUMED_NATIVE).toBe(MANIFEST_FIELD_NATIVE); + // 20260928 优先级:native > portrait > landscape(URL 取值链逐字) + expect(src).toMatch( + /wallpaperVideoNative\s*\|\|\s*day\.wallpaperVideoPortrait\s*\|\|\s*day\.wallpaperVideoLandscape/, + ); }); it("API 侧(routes/daily.ts)含 wallpaperVideoUrl 字段名与 wallpaper-video 路由字面量", () => { @@ -254,8 +267,13 @@ describe("设计文档 SSOT 字面量冻结(场景谓词 assert 字段取值 ); expect(impl).toContain("hvc1"); expect(impl).toContain("1920×1080"); - // [2026-09-25] 竖版画布改版:portrait 档经 --width/--height 逐轴覆盖 → 736×1600 - expect(impl).toContain("736×1600"); + // [2026-09-28] 单腿原生:736×1600 档声明随固定画布常量一并退役,像素预算锚点在画布 SSOT + const nativeCanvas = readText( + path.join(BACKEND_SRC, "lib/wallpaper/native-canvas.ts"), + "lib/wallpaper/native-canvas.ts", + ); + expect(nativeCanvas).toContain("1_177_600"); + expect(nativeCanvas).toContain("736×1600"); const queues = readText(path.join(BACKEND_SRC, "jobs/queues.ts"), "jobs/queues.ts"); expect(queues).toContain("attempts: 1"); // 文档侧声明(capability-gate:runtime/ 不入库,CI 无 state.md) diff --git a/apps/backend/src/__tests__/wallpaper-video-job.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-job.acceptance.test.ts index cd88a0e..a82f787 100644 --- a/apps/backend/src/__tests__/wallpaper-video-job.acceptance.test.ts +++ b/apps/backend/src/__tests__/wallpaper-video-job.acceptance.test.ts @@ -2,37 +2,28 @@ import { spawnSync } from "node:child_process"; /** * 验收测试(红队):动态视频壁纸 — wallpaper-video job 行为契约 *(开关默认关零 spawn / spawn 失败回退静态 / COS 回执空串不写库 / 成功路径回执写库) - *【v2 增量】串接顺序 preprocess → spawn → buildLoop → renderTextOverlay → 双转码 + *【20260928 单腿原生比例改版】单腿编排 + 原生画布 + 条件 Aerial * - * 设计文档(state.md)对应谓词与契约: + * 设计文档(state.md ## 设计文档 D2 / ## 契约规约 / ## 验收场景)对应谓词与契约: * - 场景 4.P1/P3(代码化):开关关 → job skip 返回(零 honeydo spawn、零上传、零同步) * → manifest 无视频字段、静态字段完好 - * - 场景 5.P2/P3(代码化):视频生成失败 → 当日交付回退静态壁纸(manifest 静态字段完好) - * 且 job 不抛到调度层(终态 != failed——BullMQ worker 只有 throw 才标记 failed) - * - §总体架构(v2)步骤 1-5【v2】:preprocess(人脸构图裁剪)→ spawn honeydo video gen ×1 - * → palindrome buildLoop → 双轨(无文字母版 / Remotion 文字成品)→ 双转码 - * - §后端设计 §4:回执非空串才写 DB 列;syncDayToGallery(pickDate) 复用 - * - §契约规约 计算契约:spawnHoneydoVideo → {outPath, duration, stdout}; - * buildLoop(src, targetSeconds) → {loopPath, segments}【v2】; - * renderTextOverlay(videoPath, meta) → {overlaidPath}【v2】; - * transcodeForAerial 输入 = Remotion 合成后的最终成品【v2 逐字】; - * DB 列 wallpaper_video_landscape_url / wallpaper_video_portrait_url + * - 场景 5.P1/P2(代码化):生成/护栏失败 → 当日交付回退静态壁纸(manifest 静态字段完好) + * 且 job 不抛到调度层(终态 != failed——BullMQ worker 只有 throw 才标记 failed); + * 尺寸护栏阻断 → native/landscape 两列均空(契约 8) + * - 场景 1.P4(DB 侧代码化):成功日 wallpaper_video_native_url 非空; + * 窗外日(画布 w/h<1.5)landscape 列保持空(场景 4.P4) + * - 场景 7.P2(manifest 侧):native 列非空 → wallpaperVideoNative 展开为 native URL + * - §设计文档 D2 逐字:单腿 produceNativeSide:preprocess(cover 微裁)→ spawn(-r 720p + + * --width/--height 画布覆盖)→ buildLoop → renderTextOverlay(props 携画布宽高)→ + * 转码 A(画廊原生,分辨率==画布)→ 尺寸护栏 → 转码 B(条件 Aerial:w/h ∈ [1.5,1.9] + * 才产 16:9 微裁 .mov)→ COS 上传(回执非空串才写列)→ syncDayToGallery 复用 + * - DB 列:wallpaper_video_native_url(新)/ wallpaper_video_landscape_url(16:9 适配版) * - * 红队铁律:本文件仅依据设计文档编写,不读蓝队实现代码(jobs/wallpaper-video.ts、 - * lib/wallpaper/video.ts 等一律未读逻辑;只把契约声明的函数名当注入点)。 - * 注入点只用契约已声明接口名:config.wallpaperVideoEnabled / preprocessHeroFrame / - * spawnHoneydoVideo / buildLoop / renderTextOverlay / transcodeForAerial / - * transcodeForGallery / safeUploadFile / uploadFile / syncDayToGallery。 - * spawn mock 断言调用次数(场景 4.P3「honeydo 调用次数 == 0」;【v2】每腿 spawn ×1)。 - * - * // CONTRACT_AMBIGUOUS: §总体架构 v2 步骤 2「spawn honeydo video gen ×1」与横竖双画布 - * //(边界值【v2】:横版 1280×704、竖版 704×1216)及两条产物 key(_landscape.mov ∧ - * // _portrait.mp4)并读——「×1」按「每腿 ×1」落(横/竖两腿串行,每腿各自 - * // preprocess → spawn → buildLoop → renderTextOverlay → 转码 → 上传;与 §后端设计 §4 - * // v1 串行惯例一致)。若蓝队改为单腿单 spawn(一步产出双画布),次数断言红 → QA 对齐。 - * // CONTRACT_AMBIGUOUS: transcodeForGallery 的输入源(无文字微动母版 vs Remotion 文字 - * // 成品)契约未逐字固定(transcodeForAerial 有「输入改为最终成品」逐字标注,Gallery - * // 没有)——本文件不断言其 src 指向,只断言它发生一次且在 renderTextOverlay 之后。 + * 红队铁律:本文件仅依据设计文档编写;注入点只用契约已声明接口名:config.wallpaperVideoEnabled / + * readOrientedDimensions / preprocessHeroFrame / spawnHoneydoVideo / buildLoop / + * renderTextOverlay / assertVideoDimensions / transcodeForAerialNative / transcodeForGallery / + * uploadFile / syncDayToGallery。computeNativeCanvas 为真实纯函数(画布 SSOT 黑盒直跑)。 + * spawn mock 断言调用次数(场景 4.P3「honeydo 调用次数 == 0」;单腿 spawn ×1)。 */ import fs from "node:fs"; import os from "node:os"; @@ -47,7 +38,7 @@ import { setupTestSchema } from "./helpers/test-schema"; // CI 转绿且 skip 可见,本机装 honeydo 即自动恢复全量真跑) const HONEYDO_AVAILABLE = spawnSync("which", ["honeydo"], { encoding: "utf8" }).status === 0; if (!HONEYDO_AVAILABLE) { - console.warn("[wallpaper-video-job] honeydo 不可用——5 组用例 capability-gate skip"); + console.warn("[wallpaper-video-job] honeydo 不可用——6 组用例 capability-gate skip"); } const dJob = HONEYDO_AVAILABLE ? describe : describe.skip; @@ -62,22 +53,25 @@ const holder = vi.hoisted(() => ({ enabled: false, /** COS 回执模式:url = 回执公网 URL;empty = 空串(上传失败语义,不 throw) */ receiptMode: "url" as "url" | "empty", - /** config.wallpaperVideoSeconds(job 透传给 spawnHoneydoVideo.opts.seconds;默认 4【v2】) */ + /** config.wallpaperVideoSeconds(job 透传给 spawnHoneydoVideo.opts.seconds;默认 4) */ seconds: 4, - /** config.wallpaperVideoLoopSeconds(job 透传给 buildLoop.targetSeconds;默认 8【v2】) */ + /** config.wallpaperVideoLoopSeconds(job 透传给 buildLoop.targetSeconds;默认 8) */ loopSeconds: 8, + /** hero 原图尺寸(readOrientedDimensions mock 返回值;默认 4:3 → 画布 1248×928 窗外) */ + heroDims: { width: 4000, height: 3000 } as { width: number; height: number }, + /** 尺寸护栏模式:pass(放行)/ fail(throw——场景 5 护栏阻断) */ + guardMode: "pass" as "pass" | "fail", /** honeydo 绝对路径(beforeAll 解析;assertVideoSpawnPrerequisites 需要 access 到真实文件) */ honeydoPath: "/usr/bin/false", firstFramePath: "/tmp/relight-none/first.png", - landscapeProductPath: "/tmp/relight-none/landscape-src.mp4", - portraitProductPath: "/tmp/relight-none/portrait-src.mp4", + productPath: "/tmp/relight-none/native-src.mp4", /** buildLoop 产物路径(mock 落盘目标) */ loopPath: "/tmp/relight-none/loop-master.mp4", - /** renderTextOverlay 产物路径(mock 落盘目标;transcodeForAerial 输入断言用) */ + /** renderTextOverlay 产物路径(mock 落盘目标;转码 mock 的 copy 源) */ overlaidPath: "/tmp/relight-none/overlaid.mp4", })); -/** 串接顺序游标:每个契约函数 mock 被调用时按序 push 标签(【v2】顺序断言用) */ +/** 串接顺序游标:每个契约函数 mock 被调用时按序 push 标签(顺序断言用) */ const pipelineSeq: string[] = []; // ============================================================================ @@ -193,25 +187,30 @@ vi.mock("../lib/gallery/sync", async (importOriginal) => { }); // ============================================================================ -// Mock:lib/wallpaper/video(契约声明函数名作注入点;assertVideoSpawnPrerequisites 留真实) +// Mock:lib/wallpaper/video(契约声明函数名作注入点;fs/sharp/ffmpeg 边界全部 mock; +// assertVideoSpawnPrerequisites 留真实) // ============================================================================ +const mockReadOrientedDimensions = vi.hoisted(() => vi.fn()); const mockPreprocessHeroFrame = vi.hoisted(() => vi.fn()); const mockSpawnHoneydoVideo = vi.hoisted(() => vi.fn()); const mockBuildLoop = vi.hoisted(() => vi.fn()); const mockRenderTextOverlay = vi.hoisted(() => vi.fn()); -const mockTranscodeForAerial = vi.hoisted(() => vi.fn()); +const mockAssertVideoDimensions = vi.hoisted(() => vi.fn()); +const mockTranscodeForAerialNative = vi.hoisted(() => vi.fn()); const mockTranscodeForGallery = vi.hoisted(() => vi.fn()); vi.mock("../lib/wallpaper/video", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, + readOrientedDimensions: mockReadOrientedDimensions, preprocessHeroFrame: mockPreprocessHeroFrame, spawnHoneydoVideo: mockSpawnHoneydoVideo, buildLoop: mockBuildLoop, renderTextOverlay: mockRenderTextOverlay, - transcodeForAerial: mockTranscodeForAerial, + assertVideoDimensions: mockAssertVideoDimensions, + transcodeForAerialNative: mockTranscodeForAerialNative, transcodeForGallery: mockTranscodeForGallery, }; }); @@ -236,14 +235,14 @@ vi.mock("../jobs/queues", () => ({ import { buildManifest } from "../lib/gallery/manifest"; // ============================================================================ -// 契约字面量(§契约规约 逐字) +// 契约字面量(## 契约规约 逐字) // ============================================================================ const PICK_DATE = "2026-09-12"; +const NATIVE_KEY = `${TEST_COS.prefix}/wallpaper-videos/${PICK_DATE}_native.mp4`; const LANDSCAPE_KEY = `${TEST_COS.prefix}/wallpaper-videos/${PICK_DATE}_landscape.mov`; -const PORTRAIT_KEY = `${TEST_COS.prefix}/wallpaper-videos/${PICK_DATE}_portrait.mp4`; +const NATIVE_URL = `https://${TEST_COS.bucket}.cos.${TEST_COS.region}.myqcloud.com/${NATIVE_KEY}`; const LANDSCAPE_URL = `https://${TEST_COS.bucket}.cos.${TEST_COS.region}.myqcloud.com/${LANDSCAPE_KEY}`; -const PORTRAIT_URL = `https://${TEST_COS.bucket}.cos.${TEST_COS.region}.myqcloud.com/${PORTRAIT_KEY}`; // ============================================================================ // 临时环境 + fixture @@ -262,6 +261,9 @@ function ensureWallpaperVideoColumns(db: Database.Database): void { if (!cols.includes("wallpaper_video_portrait_url")) { db.exec("ALTER TABLE daily_picks ADD COLUMN wallpaper_video_portrait_url TEXT"); } + if (!cols.includes("wallpaper_video_native_url")) { + db.exec("ALTER TABLE daily_picks ADD COLUMN wallpaper_video_native_url TEXT"); + } } /** 生成 1 秒真实 H.264 mp4(spawn mock 的「生成产物」+ 转码 mock 的 copy 源) */ @@ -294,10 +296,10 @@ function seedTodayPick(overrides: { composedImagePath?: string | null } = {}): v .prepare( `INSERT INTO daily_picks (id, photo_id, pick_date, title, narrative, score, composed_image_path, members, created_at, - wallpaper_video_landscape_url, wallpaper_video_portrait_url) + wallpaper_video_landscape_url, wallpaper_video_portrait_url, wallpaper_video_native_url) VALUES ('pick-wv', 'photo-wv', ?, '金色黄昏', '五年前的今天,你在海边捕捉到了这张温暖的照片。夕阳染成金橙色,海浪轻抚沙滩。', - 8.5, ?, '[]', '2026-09-12T06:00:00.000Z', NULL, NULL)`, + 8.5, ?, '[]', '2026-09-12T06:00:00.000Z', NULL, NULL, NULL)`, ) .run(PICK_DATE, overrides.composedImagePath ?? "daily-composed/2026-09-12.jpg"); } @@ -305,16 +307,19 @@ function seedTodayPick(overrides: { composedImagePath?: string | null } = {}): v function getPickRow(): { wallpaper_video_landscape_url: string | null; wallpaper_video_portrait_url: string | null; + wallpaper_video_native_url: string | null; composed_image_path: string | null; } { return sqlite .prepare( - `SELECT wallpaper_video_landscape_url, wallpaper_video_portrait_url, composed_image_path + `SELECT wallpaper_video_landscape_url, wallpaper_video_portrait_url, + wallpaper_video_native_url, composed_image_path FROM daily_picks WHERE pick_date = ?`, ) .get(PICK_DATE) as { wallpaper_video_landscape_url: string | null; wallpaper_video_portrait_url: string | null; + wallpaper_video_native_url: string | null; composed_image_path: string | null; }; } @@ -325,6 +330,7 @@ interface ManifestDayShape { wallpaperPortrait: string; wallpaperVideoLandscape?: string; wallpaperVideoPortrait?: string; + wallpaperVideoNative?: string; } async function buildManifestDays(): Promise { @@ -340,11 +346,13 @@ function uploadCallCount(): number { /** 统一(重)接线 mock 实现——beforeEach 必须 mockReset + 重放,防止上一用例的 * mockRejectedValue 等持久实现泄漏到下一用例(mockClear 只清调用记录不清实现) */ function wireMocks(): void { + mockReadOrientedDimensions.mockReset(); mockPreprocessHeroFrame.mockReset(); mockSpawnHoneydoVideo.mockReset(); mockBuildLoop.mockReset(); mockRenderTextOverlay.mockReset(); - mockTranscodeForAerial.mockReset(); + mockAssertVideoDimensions.mockReset(); + mockTranscodeForAerialNative.mockReset(); mockTranscodeForGallery.mockReset(); mockUploadFile.mockReset(); mockSafeUpload.mockReset(); @@ -354,6 +362,8 @@ function wireMocks(): void { mockCosSliceUploadFile.mockImplementation(async () => ({})); pipelineSeq.length = 0; + // dims mock:返回 holder.heroDims(4:3 → 画布 1248×928 窗外;16:9 → 1440×800 窗内) + mockReadOrientedDimensions.mockImplementation(async () => ({ ...holder.heroDims })); // preprocess 返回真实 png;spawn 在 job 请求的 outPath 落真实 mp4 产物(契约:产物存在于 outPath) mockPreprocessHeroFrame.mockImplementation(async () => { pipelineSeq.push("preprocess"); @@ -361,35 +371,43 @@ function wireMocks(): void { }); mockSpawnHoneydoVideo.mockImplementation(async (opts: { outPath?: string }) => { pipelineSeq.push("spawn"); - const out = opts?.outPath ?? holder.landscapeProductPath; + const out = opts?.outPath ?? holder.productPath; fs.mkdirSync(path.dirname(out), { recursive: true }); - fs.copyFileSync(holder.landscapeProductPath, out); + fs.copyFileSync(holder.productPath, out); return { outPath: out, duration: 1, - stdout: `{"out":"${out}","duration":1,"res":"1280x704"}`, + stdout: `{"out":"${out}","duration":1,"res":"720p"}`, }; }); - // buildLoop(契约【v2】:(src, targetSeconds) → {loopPath, segments})——落真实产物 + // buildLoop(契约:(src, targetSeconds) → {loopPath, segments})——落真实产物 mockBuildLoop.mockImplementation(async (src: string, _targetSeconds: number) => { pipelineSeq.push("buildLoop"); fs.mkdirSync(path.dirname(holder.loopPath), { recursive: true }); fs.copyFileSync(src, holder.loopPath); return { loopPath: holder.loopPath, segments: 2 }; }); - // renderTextOverlay(契约【v2】:(videoPath, meta) → {overlaidPath})——落真实产物 + // renderTextOverlay(契约:(videoPath, meta) → {overlaidPath})——落真实产物 mockRenderTextOverlay.mockImplementation(async (videoPath: string, _meta: unknown) => { pipelineSeq.push("overlay"); fs.mkdirSync(path.dirname(holder.overlaidPath), { recursive: true }); fs.copyFileSync(videoPath, holder.overlaidPath); return { overlaidPath: holder.overlaidPath }; }); + // 尺寸护栏(契约:不匹配即 throw;guardMode=fail 模拟错尺寸产物被拦截) + mockAssertVideoDimensions.mockImplementation(async () => { + if (holder.guardMode === "fail") { + throw new dimensionErrorCtor( + "转码产物尺寸断言失败: 期望 1920×1080,实际 1918×1080(禁静默失败)", + ); + } + }); // 转码 mock:copy 落 dst(src 为上游产物,真实存在) const copyToDst = async (src: string, dst: string) => { fs.mkdirSync(path.dirname(dst), { recursive: true }); fs.copyFileSync(src, dst); }; - mockTranscodeForAerial.mockImplementation((src: string, dst: string) => { + mockTranscodeForAerialNative.mockImplementation((src: string, dst: string) => { pipelineSeq.push("aerial"); return copyToDst(src, dst); }); @@ -410,7 +428,8 @@ function wireMocks(): void { // beforeAll / beforeEach / afterAll // ============================================================================ -let runWallpaperVideo: (pickDate: string) => Promise; +let runWallpaperVideo: (pickDate: string, log?: (m: string) => void) => Promise; +let dimensionErrorCtor: new (m: string) => Error; beforeAll(async () => { // ffmpeg 是 job 测试 fixture 的硬前置(红队铁律:fixture 失败即真红,不静默跳过) @@ -455,7 +474,7 @@ beforeAll(async () => { process.env.DATABASE_PATH = dbPath; process.env.STORAGE_ROOT = storageRoot; - // fixture 媒体:预裁剪首帧 png(1x1 最小合法 PNG)+ 两条「生成产物」mp4 + // fixture 媒体:预裁剪首帧 png(1x1 最小合法 PNG)+ 「生成产物」mp4 holder.firstFramePath = path.join(tmpRoot, "first-frame.png"); fs.writeFileSync( holder.firstFramePath, @@ -464,17 +483,15 @@ beforeAll(async () => { "base64", ), ); - holder.landscapeProductPath = path.join(tmpRoot, "landscape-product.mp4"); - holder.portraitProductPath = path.join(tmpRoot, "portrait-product.mp4"); - makeTinyMp4(holder.landscapeProductPath); - makeTinyMp4(holder.portraitProductPath); - // 【v2】buildLoop / renderTextOverlay 产物路径(mock 落盘目标) + holder.productPath = path.join(tmpRoot, "native-product.mp4"); + makeTinyMp4(holder.productPath); + // buildLoop / renderTextOverlay 产物路径(mock 落盘目标) holder.loopPath = path.join(tmpRoot, "loop-master.mp4"); holder.overlaidPath = path.join(tmpRoot, "overlaid.mp4"); - // 【v2】Remotion 运行时工作区指向本测试 tmpRoot(与 storageRoot 同源隔离,防 /tmp 固定路径互扰) + // Remotion 运行时工作区指向本测试 tmpRoot(与 storageRoot 同源隔离,防 /tmp 固定路径互扰) holder.videoWorkspacePath = path.join(tmpRoot, "video-workspace"); - // 【v2】Remotion 运行时脚手架(assertVideoSpawnPrerequisites 会 access 校验 + // Remotion 运行时脚手架(assertVideoSpawnPrerequisites 会 access 校验 // videoWorkspacePath/node_modules/.bin/remotion、Chrome Headless Shell、 // wallpaper-overlay 工程入口与文字层字体;job 的 spawn/转码/render 均被 mock, // 这里只需让 fs access 前置校验通过——路径以真实前置缺失报错为锚) @@ -482,7 +499,7 @@ beforeAll(async () => { for (const rel of [ path.join("wallpaper-overlay", "package.json"), path.join("wallpaper-overlay", "src", "index.ts"), - // 文字层字体(Satori 模板同源:Fraunces / Noto Serif SC,§后端设计 §1【v2】) + // 文字层字体(Satori 模板同源:Fraunces / Noto Serif SC) path.join("wallpaper-overlay", "public", "fonts", "NotoSerifSC-Regular.otf"), path.join("wallpaper-overlay", "public", "fonts", "NotoSerifSC-Bold.otf"), path.join("wallpaper-overlay", "public", "fonts", "Fraunces-Regular.otf"), @@ -515,12 +532,20 @@ beforeAll(async () => { runWallpaperVideo: (pickDate: string) => Promise; }; runWallpaperVideo = mod.runWallpaperVideo; + // DimensionAssertError 经 vi.mock 的 ...actual spread 透传,类身份一致(instanceof 可用) + dimensionErrorCtor = ( + (await import("../lib/wallpaper/video")) as { + DimensionAssertError: new (m: string) => Error; + } + ).DimensionAssertError; }, 30000); beforeEach(() => { holder.enabled = false; holder.receiptMode = "url"; - // 【v2】config 时长字段默认(§后端设计 §1【v2】:seconds 默认 4 / loopSeconds 默认 8) + holder.heroDims = { width: 4000, height: 3000 }; + holder.guardMode = "pass"; + // config 时长字段默认(§后端设计:seconds 默认 4 / loopSeconds 默认 8) holder.seconds = 4; holder.loopSeconds = 8; wireMocks(); @@ -555,6 +580,7 @@ dJob("场景 4(代码化):开关关闭 → job skip、零 spawn、manifest const row = getPickRow(); expect(row.wallpaper_video_landscape_url).toBeNull(); expect(row.wallpaper_video_portrait_url).toBeNull(); + expect(row.wallpaper_video_native_url).toBeNull(); }); it("开关关闭时 manifest 无视频字段(in === false)且静态字段完好", async () => { @@ -569,20 +595,21 @@ dJob("场景 4(代码化):开关关闭 → job skip、零 spawn、manifest // 场景 4.P2 代码化:视频字段 exists == false;静态竖图字段 exists 且非空 expect("wallpaperVideoLandscape" in (day ?? {})).toBe(false); expect("wallpaperVideoPortrait" in (day ?? {})).toBe(false); + expect("wallpaperVideoNative" in (day ?? {})).toBe(false); expect((day as ManifestDayShape)?.wallpaperPortrait.length ?? 0).toBeGreaterThan(0); expect((day as ManifestDayShape)?.wallpaperLandscape.length ?? 0).toBeGreaterThan(0); }); }); // ============================================================================ -// 场景 5:生成失败 → 回退静态、job 不抛到调度层(终态 != failed) +// 场景 5:生成失败/护栏阻断 → 回退静态、job 不抛到调度层(终态 != failed) // ============================================================================ -dJob("场景 5(代码化):spawn 失败 → job 不抛、回退静态、DB 列 null", () => { +dJob("场景 5(代码化):spawn 失败/护栏阻断 → job 不抛、回退静态、DB 列 null", () => { it("spawnHoneydoVideo 拒绝(HoneydoSpawnError 语义)→ runWallpaperVideo 正常完成不 throw", async () => { holder.enabled = true; seedTodayPick(); - // 当日视频生成失败:spawn 拒绝(HoneydoSpawnError 语义;job 内部按腿容错回退,不向调度层抛) + // 当日视频生成失败:spawn 拒绝(HoneydoSpawnError 语义;job 旁路容错,不向调度层抛) mockSpawnHoneydoVideo.mockRejectedValue( new Error("HoneydoSpawnError: exit code 1 :: stdout tail"), ); @@ -597,7 +624,7 @@ dJob("场景 5(代码化):spawn 失败 → job 不抛、回退静态、DB expect(rejected).toBe(false); }); - it("失败后 DB 两列保持 null(回执未写)且 composedImagePath 零改动", async () => { + it("失败后 DB 列保持 null(回执未写)且 composedImagePath 零改动", async () => { holder.enabled = true; seedTodayPick(); mockSpawnHoneydoVideo.mockRejectedValue(new Error("HoneydoSpawnError: timeout")); @@ -607,6 +634,7 @@ dJob("场景 5(代码化):spawn 失败 → job 不抛、回退静态、DB const row = getPickRow(); expect(row.wallpaper_video_landscape_url).toBeNull(); expect(row.wallpaper_video_portrait_url).toBeNull(); + expect(row.wallpaper_video_native_url).toBeNull(); // 静态链路零改动(副作用清单:不触碰静态壁纸合成) expect(row.composed_image_path).toBe("daily-composed/2026-09-12.jpg"); }); @@ -627,6 +655,38 @@ dJob("场景 5(代码化):spawn 失败 → job 不抛、回退静态、DB // 视频字段缺省 expect("wallpaperVideoLandscape" in day).toBe(false); expect("wallpaperVideoPortrait" in day).toBe(false); + expect("wallpaperVideoNative" in day).toBe(false); + }); + + it("场景 5.P1(护栏阻断代码化):尺寸护栏 throw → native/landscape 两列均空(契约 8 fail-safe)", async () => { + holder.enabled = true; + holder.heroDims = { width: 3840, height: 2160 }; // 16:9 兼容窗口(护栏路径含 aerial 腿) + holder.guardMode = "fail"; // 护栏模拟错尺寸产物(1918×1080 语义) + seedTodayPick(); + + const logs: string[] = []; + const warnSpy = vi.spyOn(console, "log").mockImplementation((m: string) => logs.push(m)); + try { + let rejected = false; + try { + await runWallpaperVideo(PICK_DATE, (m: string) => logs.push(m)); + } catch { + rejected = true; + } + // 设计 D2/红队 SSOT 5.P1:尺寸护栏失败必须传播为 job 失败(rerun CLI exit≠0); + // 两列均空由「护栏先于上传/写列」保证(契约 8 fail-safe 语义不变) + expect(rejected, "尺寸护栏失败必须传播为 job 失败").toBe(true); + + const row = getPickRow(); + expect(row.wallpaper_video_native_url).toBeNull(); + expect(row.wallpaper_video_landscape_url).toBeNull(); + // 零上传(护栏在任一回执列写入/上传之前执行) + expect(uploadCallCount()).toBe(0); + // 场景 5.P2:失败路径显式含尺寸断言语义(禁静默失败) + expect(logs.join("\n")).toContain("尺寸断言失败"); + } finally { + warnSpy.mockRestore(); + } }); }); @@ -635,7 +695,7 @@ dJob("场景 5(代码化):spawn 失败 → job 不抛、回退静态、DB // ============================================================================ dJob("COS 回执契约:uploadFile 回执空串 → DB 列不被写入、manifest 无视频字段", () => { - it("回执为空串(上传失败语义)→ 两列保持 null", async () => { + it("回执为空串(上传失败语义)→ 列保持 null", async () => { holder.enabled = true; holder.receiptMode = "empty"; seedTodayPick(); @@ -645,8 +705,9 @@ dJob("COS 回执契约:uploadFile 回执空串 → DB 列不被写入、manife const row = getPickRow(); expect(row.wallpaper_video_landscape_url).toBeNull(); expect(row.wallpaper_video_portrait_url).toBeNull(); - // 生成与上传确实发生过(排除「没跑所以没写」的假绿;【v2】横/竖两腿各 spawn ×1) - expect(mockSpawnHoneydoVideo).toHaveBeenCalledTimes(2); + expect(row.wallpaper_video_native_url).toBeNull(); + // 生成与上传确实发生过(排除「没跑所以没写」的假绿;单腿 spawn ×1) + expect(mockSpawnHoneydoVideo).toHaveBeenCalledTimes(1); expect(uploadCallCount()).toBeGreaterThanOrEqual(1); }); @@ -659,7 +720,7 @@ dJob("COS 回执契约:uploadFile 回执空串 → DB 列不被写入、manife const days = await buildManifestDays(); const day = days.find((d) => d.pickDate === PICK_DATE) as ManifestDayShape; expect("wallpaperVideoLandscape" in day).toBe(false); - expect("wallpaperVideoPortrait" in day).toBe(false); + expect("wallpaperVideoNative" in day).toBe(false); expect(day.wallpaperPortrait.length).toBeGreaterThan(0); }); }); @@ -668,142 +729,149 @@ dJob("COS 回执契约:uploadFile 回执空串 → DB 列不被写入、manife // 成功路径:回执非空 → 写 DB 列 → syncDayToGallery → manifest 暴露视频字段 // ============================================================================ -dJob("成功路径:回执非空串 → DB 列写入回执 URL → manifest 暴露视频字段(场景 1.P2 DB 侧)", () => { - it("DB 两列 == 回执 URL 逐字(横 .mov / 竖 .mp4),且 syncDayToGallery 以 pickDate 调用", async () => { +dJob("成功路径(窗外 4:3):native 列写入回执 URL、landscape 列不产出(场景 1.P4 / 4.P4)", () => { + it("DB native 列 == 回执 URL 逐字(_native.mp4),窗外日 landscape 列 null,syncDayToGallery 以 pickDate 调用", async () => { holder.enabled = true; holder.receiptMode = "url"; + holder.heroDims = { width: 4000, height: 3000 }; // 4:3 → 1248×928,w/h=1.345 <1.5 窗外 seedTodayPick(); await runWallpaperVideo(PICK_DATE); const row = getPickRow(); - expect(row.wallpaper_video_landscape_url).toBe(LANDSCAPE_URL); - expect(row.wallpaper_video_portrait_url).toBe(PORTRAIT_URL); + expect(row.wallpaper_video_native_url).toBe(NATIVE_URL); + // 场景 4.P4:窗外日 landscape 列空 → mac App 拿空列走静态回退 + expect(row.wallpaper_video_landscape_url).toBeNull(); expect(mockSyncDayToGallery).toHaveBeenCalledTimes(1); expect(mockSyncDayToGallery.mock.calls[0]?.[0]).toBe(PICK_DATE); }); - it("【v2】双腿串行:每腿 spawn ×1 → buildLoop ×1 → renderTextOverlay ×1;aerial/gallery 转码各 ×1;横竖各上传一次;manifest 两字段 == 回执 URL 且满足 1.P2 字面量", async () => { + it("单腿编排:spawn ×1 → buildLoop ×1 → renderTextOverlay ×1 → gallery 转码 ×1;仅 native 上传一次;manifest 只展开 native 字段", async () => { holder.enabled = true; + holder.heroDims = { width: 4000, height: 3000 }; seedTodayPick(); await runWallpaperVideo(PICK_DATE); - // 【v2】横/竖两腿(边界值【v2】双画布 + 两条产物 key),每腿各自 - // spawn → buildLoop → renderTextOverlay(CONTRACT_AMBIGUOUS 见文件头:每腿 ×1) - expect(mockPreprocessHeroFrame).toHaveBeenCalledTimes(2); - expect(mockSpawnHoneydoVideo).toHaveBeenCalledTimes(2); - expect(mockBuildLoop).toHaveBeenCalledTimes(2); - expect(mockRenderTextOverlay).toHaveBeenCalledTimes(2); - expect(mockTranscodeForAerial).toHaveBeenCalledTimes(1); + // 单腿 ×1(20260928:不再有横竖两腿) + expect(mockReadOrientedDimensions).toHaveBeenCalledTimes(1); + expect(mockPreprocessHeroFrame).toHaveBeenCalledTimes(1); + expect(mockSpawnHoneydoVideo).toHaveBeenCalledTimes(1); + expect(mockBuildLoop).toHaveBeenCalledTimes(1); + expect(mockRenderTextOverlay).toHaveBeenCalledTimes(1); expect(mockTranscodeForGallery).toHaveBeenCalledTimes(1); - // 横竖各上传一次(safeUploadFile / uploadFile 单一路径合计) + expect(mockTranscodeForAerialNative).toHaveBeenCalledTimes(0); + expect(uploadCallCount()).toBe(1); + + const days = await buildManifestDays(); + const day = days.find((d) => d.pickDate === PICK_DATE) as ManifestDayShape; + // 场景 7.P2:native 展开为 native URL(非 legacy) + expect("wallpaperVideoNative" in day).toBe(true); + expect(day.wallpaperVideoNative).toBe(NATIVE_URL); + expect((day.wallpaperVideoNative as string).endsWith("_native.mp4")).toBe(true); + expect(day.wallpaperVideoNative).toContain("myqcloud.com"); + // 窗外日 legacy 字段缺省 + expect("wallpaperVideoLandscape" in day).toBe(false); + expect("wallpaperVideoPortrait" in day).toBe(false); + }); +}); + +dJob("成功路径(窗内 16:9):native + landscape 双列写入(16:9 微裁 .mov 语义收窄)", () => { + it("hero 16:9 → 画布 1440×800(w/h=1.8 窗内)→ aerial 转码 ×1 + landscape 列写入 legacy key", async () => { + holder.enabled = true; + holder.heroDims = { width: 3840, height: 2160 }; + seedTodayPick(); + + await runWallpaperVideo(PICK_DATE); + + expect(mockTranscodeForAerialNative).toHaveBeenCalledTimes(1); expect(uploadCallCount()).toBe(2); + const row = getPickRow(); + expect(row.wallpaper_video_native_url).toBe(NATIVE_URL); + expect(row.wallpaper_video_landscape_url).toBe(LANDSCAPE_URL); + const days = await buildManifestDays(); const day = days.find((d) => d.pickDate === PICK_DATE) as ManifestDayShape; - expect("wallpaperVideoLandscape" in day).toBe(true); - expect("wallpaperVideoPortrait" in day).toBe(true); + expect(day.wallpaperVideoNative).toBe(NATIVE_URL); expect(day.wallpaperVideoLandscape).toBe(LANDSCAPE_URL); - expect(day.wallpaperVideoPortrait).toBe(PORTRAIT_URL); - // 场景 1.P2 断言字面量 - const lv = day.wallpaperVideoLandscape; - const pv = day.wallpaperVideoPortrait; - expect(typeof lv).toBe("string"); - expect(typeof pv).toBe("string"); - expect((lv as string).endsWith("_landscape.mov")).toBe(true); - expect((pv as string).endsWith("_portrait.mp4")).toBe(true); - expect(lv).toContain("myqcloud.com"); + expect((day.wallpaperVideoLandscape as string).endsWith("_landscape.mov")).toBe(true); }); }); // ============================================================================ -// 【v2】job 串接顺序:preprocess → spawn → buildLoop → renderTextOverlay → 双转码 +// job 串接顺序:preprocess → spawn → buildLoop → renderTextOverlay → 转码(单腿) // ============================================================================ dJob( - "【v2】job 串接顺序(§总体架构 v2 步骤 1-5 × 横竖两腿):preprocess → spawn → buildLoop → renderTextOverlay → 双转码", + "job 串接顺序(D2 单腿逐字):preprocess → spawn → buildLoop → renderTextOverlay → gallery 转码 → aerial 转码(窗内)", () => { - it("每腿调用顺序逐字 + 双腿串行 + config 时长/loop 参数透传(非默认值证明 wiring,杀硬编码 mutation)", async () => { + it("调用顺序逐字 + 画布由 computeNativeCanvas 派生 + config 时长/loop 参数透传(非默认值证明 wiring)", async () => { holder.enabled = true; - // 非默认值:证明 job 读取 config 并透传(若 job 硬编码 4/8/15,此处即红) + // 非默认值:证明 job 读取 config 并透传(若 job 硬编码 4/8,此处即红) holder.seconds = 5; holder.loopSeconds = 9; + holder.heroDims = { width: 3840, height: 2160 }; // 窗内(含 aerial 腿的完整顺序) seedTodayPick(); await runWallpaperVideo(PICK_DATE); - // 次数(横/竖两腿:每腿 preprocess/spawn/buildLoop/overlay 各 ×1;CONTRACT_AMBIGUOUS - // 见文件头——「spawn ×1」按每腿落) - expect(mockPreprocessHeroFrame).toHaveBeenCalledTimes(2); - expect(mockSpawnHoneydoVideo).toHaveBeenCalledTimes(2); - expect(mockBuildLoop).toHaveBeenCalledTimes(2); - expect(mockRenderTextOverlay).toHaveBeenCalledTimes(2); - expect(mockTranscodeForAerial).toHaveBeenCalledTimes(1); + // 次数(单腿:preprocess/spawn/buildLoop/overlay 各 ×1;双转码各 ×1) + expect(mockPreprocessHeroFrame).toHaveBeenCalledTimes(1); + expect(mockSpawnHoneydoVideo).toHaveBeenCalledTimes(1); + expect(mockBuildLoop).toHaveBeenCalledTimes(1); + expect(mockRenderTextOverlay).toHaveBeenCalledTimes(1); expect(mockTranscodeForGallery).toHaveBeenCalledTimes(1); - - // 参数透传:spawnHoneydoVideo.opts.seconds === config.wallpaperVideoSeconds(两腿皆然); - // buildLoop.targetSeconds === config.wallpaperVideoLoopSeconds(两腿皆然) - for (const call of mockSpawnHoneydoVideo.mock.calls) { - expect( - (call?.[0] as { seconds?: number } | undefined)?.seconds, - "spawn.seconds 必须 === config.wallpaperVideoSeconds", - ).toBe(5); - } + expect(mockTranscodeForAerialNative).toHaveBeenCalledTimes(1); + + // 参数透传:spawnHoneydoVideo.opts.seconds === config.wallpaperVideoSeconds; + // buildLoop.targetSeconds === config.wallpaperVideoLoopSeconds + const spawnOpts = mockSpawnHoneydoVideo.mock.calls[0]?.[0] as { + seconds?: number; + width?: number; + height?: number; + res?: string; + }; + expect(spawnOpts.seconds, "spawn.seconds 必须 === config.wallpaperVideoSeconds").toBe(5); + // 契约 6:-r 720p 保留 + 画布两轴显式覆盖(computeNativeCanvas(3840,2160)=1440×800,真实纯函数) + expect(spawnOpts.res).toBe("720p"); + expect(spawnOpts.width).toBe(1440); + expect(spawnOpts.height).toBe(800); for (const call of mockBuildLoop.mock.calls) { expect(call?.[1], "buildLoop.targetSeconds 必须 === config.wallpaperVideoLoopSeconds").toBe( 9, ); } - // 双画布预裁剪(边界值逐字):两腿 preprocess 画布 == {1280×704, 736×1600} - const canvasSet = new Set( - mockPreprocessHeroFrame.mock.calls.map((c) => `${c?.[1]}x${c?.[2]}`), - ); - expect(canvasSet, "preprocess 必须分别按横版 1280×704 与竖版 736×1600 画布裁剪").toEqual( - new Set(["1280x704", "736x1600"]), - ); + // preprocess 画布 == computeNativeCanvas(heroDims)(真实纯函数派生) + expect(mockPreprocessHeroFrame.mock.calls[0]?.[1]).toBe(1440); + expect(mockPreprocessHeroFrame.mock.calls[0]?.[2]).toBe(800); - // 顺序(§总体架构 v2 步骤 1-5 逐字,每腿内 preprocess → spawn → buildLoop → overlay - // → 转码;双腿串行:横腿(aerial)整体先于竖腿 spawn;首事件 preprocess、末事件 gallery 上传腿) + // 顺序(D2 逐字,单腿内 preprocess → spawn → buildLoop → overlay → 转码) const seq = pipelineSeq; - expect(seq[0], "首事件必须是 preprocess(人脸构图先于生成)").toBe("preprocess"); - expect(seq[seq.length - 1], "末事件必须是 gallery 转码(竖版转码收尾)").toBe("gallery"); - const leg1Spawn = seq.indexOf("spawn"); - const leg2Spawn = seq.indexOf("spawn", leg1Spawn + 1); - expect(leg1Spawn, "腿1 spawn 缺失").toBeGreaterThan(0); - expect(leg2Spawn, "腿2 spawn 缺失").toBeGreaterThan(leg1Spawn); - for (const [spawnIdx, legTag] of [ - [leg1Spawn, "leg1"], - [leg2Spawn, "leg2"], - ] as Array<[number, string]>) { - const buildIdx = seq.indexOf("buildLoop", spawnIdx); - const overlayIdx = seq.indexOf("overlay", buildIdx); - expect( - buildIdx, - `${legTag}: spawn 之后必须先 buildLoop(palindrome)再 renderTextOverlay`, - ).toBeGreaterThan(spawnIdx); - expect(overlayIdx, `${legTag}: buildLoop 之后必须 renderTextOverlay`).toBeGreaterThan( - buildIdx, - ); - } - // 横腿转码(aerial)在腿1 overlay 之后、腿2 spawn 之前(串行不并行) - const aerialIdx = seq.indexOf("aerial"); - expect(aerialIdx, "aerial 转码必须在腿1 renderTextOverlay 之后").toBeGreaterThan( - seq.indexOf("overlay", leg1Spawn), - ); - expect(aerialIdx, "aerial 转码必须在腿2 spawn 之前(双腿串行)").toBeLessThan(leg2Spawn); - // 竖腿转码(gallery)在腿2 overlay 之后 + expect(seq[0], "首事件必须是 preprocess(预裁剪先于生成)").toBe("preprocess"); + const spawnIdx = seq.indexOf("spawn"); + const buildIdx = seq.indexOf("buildLoop", spawnIdx); + const overlayIdx = seq.indexOf("overlay", buildIdx); + const galleryIdx = seq.indexOf("gallery", overlayIdx); + const aerialIdx = seq.indexOf("aerial", galleryIdx); + expect(spawnIdx, "spawn 在 preprocess 后").toBeGreaterThan(0); expect( - seq.indexOf("gallery"), - "gallery 转码必须在腿2 renderTextOverlay 之后", - ).toBeGreaterThan(seq.indexOf("overlay", leg2Spawn)); - - // 契约 §3【v2】逐字:transcodeForAerial 输入 = Remotion 合成后的最终成品 - expect(mockTranscodeForAerial.mock.calls[0]?.[0]).toBe(holder.overlaidPath); - // CONTRACT_AMBIGUOUS: transcodeForGallery 的输入源(无文字母版 vs 文字成品)契约未 - // 逐字固定——不断言其 src,只断言它发生一次且在 renderTextOverlay 之后(上一断言)。 + buildIdx, + "spawn 之后必须先 buildLoop(palindrome)再 renderTextOverlay", + ).toBeGreaterThan(spawnIdx); + expect(overlayIdx, "buildLoop 之后必须 renderTextOverlay").toBeGreaterThan(buildIdx); + expect(galleryIdx, "overlay 之后必须 gallery 原生转码(转码 A)").toBeGreaterThan(overlayIdx); + expect(aerialIdx, "gallery 之后才 aerial 条件转码(转码 B)").toBeGreaterThan(galleryIdx); + + // 契约 7:renderTextOverlay meta 携画布宽高(comp 选择依据) + const overlayMeta = mockRenderTextOverlay.mock.calls[0]?.[1] as { + canvasWidth?: number; + canvasHeight?: number; + }; + expect(overlayMeta.canvasWidth).toBe(1440); + expect(overlayMeta.canvasHeight).toBe(800); }); }, ); diff --git a/apps/backend/src/__tests__/wallpaper-video-job.test.ts b/apps/backend/src/__tests__/wallpaper-video-job.test.ts index d079dcf..c0073d9 100644 --- a/apps/backend/src/__tests__/wallpaper-video-job.test.ts +++ b/apps/backend/src/__tests__/wallpaper-video-job.test.ts @@ -1,19 +1,25 @@ /** - * 单测:wallpaper-video job 主流程(任务 4 + v2 增量任务 12/15) + * 单测:wallpaper-video job 主流程(任务 4 + v2 增量任务 12/15;20260928 单腿原生比例改版) * - * 契约(state.md ## 后端设计 4 / ## 契约规约;v2 修订): + * 契约(state.md ## 后端设计 4 / ## 契约规约;20260928 D2): * runWallpaperVideo(pickDate): * - 开关关 → log skip 返回(零 honeydo 调用) * - 无记录 / hero.isVideo / composedImagePath null → skip - * - faces 表取 hero 最大 bbox → preprocess(人脸构图;无脸 → 中心构图回退) - * - 每侧串接:spawn 生成 → buildLoop(palindrome)→ renderTextOverlay(Remotion 文字层) - * → 转码(横 Aerial hvc1 .mov 无音轨 / 竖 Gallery libx264+aac mp4 带音轨,输入=Remotion 成品) - * - COS 上传(video/quicktime 与 video/mp4),回执非空串才写 DB 列 + * - sharp 读 hero EXIF 旋转后尺寸 → computeNativeCanvas 单腿原生画布 + * - faces 表取 hero 最大 bbox(仅 prompt 分层默认;预裁剪已无人脸窗口) + * - 单腿串接:spawn 生成(-r 720p + --width/--height 画布覆盖)→ buildLoop(palindrome) + * → renderTextOverlay(props 携画布宽高)→ 转码 A(画廊原生 mp4,分辨率==画布) + * → 尺寸护栏 → 转码 B(条件 Aerial:画布 w/h ∈ [1.5,1.9] 才产 16:9 微裁 .mov) + * → COS 上传(native key `_native.mp4` video/mp4;aerial key `_landscape.mov` + * video/quicktime),回执非空串才写 DB 列 * - syncDayToGallery 复用 - * 产物路径:{STORAGE_ROOT}/wallpaper-videos/{pickDate}-landscape.mov / {pickDate}-portrait.mp4 + * 失败语义(契约 8):任一环节 throw(含尺寸护栏)→ native/landscape 两列均空 → 静态回退 + * 返回 {native, landscape} * * 测试策略:mock ../db(真实 drizzle schema 列引用 + stub select/update)、 - * mock ../lib/wallpaper/video、../lib/cos/upload、../lib/gallery/sync、../lib/config。 + * mock ../lib/wallpaper/video(fs/sharp/ffmpeg 边界:readOrientedDimensions / + * assertVideoDimensions 一并 mock)、../lib/cos/upload、../lib/gallery/sync、../lib/config; + * computeNativeCanvas 用真实纯函数(native-canvas.ts 无依赖)。 */ import { beforeEach, describe, expect, it, vi } from "vitest"; @@ -27,21 +33,20 @@ const state = vi.hoisted(() => ({ photosTable: {} as object, facesTable: {} as object, calls: { + dims: [] as string[], spawn: [] as Record[], buildLoop: [] as { src: string; targetSeconds: number }[], renderTextOverlay: [] as { videoPath: string; meta: Record }[], - transcodeAerial: [] as unknown[][], + assertDims: [] as { filePath: string; w: number; h: number }[], + transcodeAerialNative: [] as unknown[][], transcodeGallery: [] as unknown[][], upload: [] as { localPath: string; cosKey: string; contentType: string }[], syncDay: [] as unknown[], - preprocess: [] as { - photoPath: string; - width: number; - height: number; - opts: Record; - }[], + preprocess: [] as { photoPath: string; width: number; height: number }[], }, uploadReturn: "" as string, + /** sharp 读出的 hero 尺寸(mock readOrientedDimensions 返回值,按用例设置) */ + heroDims: { width: 4000, height: 3000 } as { width: number; height: number }, })); vi.mock("../db", async () => { @@ -111,12 +116,14 @@ vi.mock("../lib/wallpaper/video", async (importOriginal) => { const actual = (await importOriginal()) as Record; return { ...actual, - preprocessHeroFrame: vi.fn( - async (photoPath: string, width: number, height: number, opts: Record) => { - state.calls.preprocess.push({ photoPath, width, height, opts }); - return `/tmp/wv-frame-${width}x${height}.png`; - }, - ), + readOrientedDimensions: vi.fn(async (photoPath: string) => { + state.calls.dims.push(photoPath); + return state.heroDims; + }), + preprocessHeroFrame: vi.fn(async (photoPath: string, width: number, height: number) => { + state.calls.preprocess.push({ photoPath, width, height }); + return `/tmp/wv-frame-${width}x${height}.png`; + }), assertVideoSpawnPrerequisites: vi.fn(async () => undefined), spawnHoneydoVideo: vi.fn(async (opts: Record) => { state.calls.spawn.push(opts); @@ -130,8 +137,12 @@ vi.mock("../lib/wallpaper/video", async (importOriginal) => { state.calls.renderTextOverlay.push({ videoPath, meta }); return { overlaidPath: videoPath.replace(/\.mp4$/, "-overlay.mp4") }; }), - transcodeForAerial: vi.fn(async (src: string, dst: string) => { - state.calls.transcodeAerial.push([src, dst]); + // 尺寸护栏:mock 边界(真实 ffprobe 判别在 transcode 单测覆盖) + assertVideoDimensions: vi.fn(async (filePath: string, w: number, h: number) => { + state.calls.assertDims.push({ filePath, w, h }); + }), + transcodeForAerialNative: vi.fn(async (src: string, dst: string) => { + state.calls.transcodeAerialNative.push([src, dst]); const { writeFileSync } = await import("node:fs"); writeFileSync(dst, "fake-mov"); }), @@ -183,10 +194,13 @@ beforeEach(() => { state.faceRows = []; state.updates = []; state.uploadReturn = ""; + state.heroDims = { width: 4000, height: 3000 }; + state.calls.dims = []; state.calls.spawn = []; state.calls.buildLoop = []; state.calls.renderTextOverlay = []; - state.calls.transcodeAerial = []; + state.calls.assertDims = []; + state.calls.transcodeAerialNative = []; state.calls.transcodeGallery = []; state.calls.upload = []; state.calls.syncDay = []; @@ -194,7 +208,7 @@ beforeEach(() => { vi.clearAllMocks(); }); -describe("runWallpaperVideo", () => { +describe("runWallpaperVideo(20260928 单腿原生)", () => { it("开关关 → skip,零 honeydo 调用", async () => { const { config } = await import("../lib/config"); const saved = config.wallpaperVideoEnabled; @@ -202,7 +216,7 @@ describe("runWallpaperVideo", () => { try { const logs: string[] = []; const res = await runWallpaperVideo("2026-09-12", (m) => logs.push(m)); - expect(res).toEqual({ landscape: "", portrait: "" }); + expect(res).toEqual({ native: "", landscape: "" }); expect(state.calls.spawn).toHaveLength(0); expect(logs.join("\n")).toContain("skip"); } finally { @@ -212,7 +226,7 @@ describe("runWallpaperVideo", () => { it("无当日记录 → skip", async () => { const res = await runWallpaperVideo("2026-09-12", () => {}); - expect(res).toEqual({ landscape: "", portrait: "" }); + expect(res).toEqual({ native: "", landscape: "" }); expect(state.calls.spawn).toHaveLength(0); }); @@ -220,7 +234,7 @@ describe("runWallpaperVideo", () => { state.pickRows = [PICK]; state.photoRows = [{ ...PHOTO, mediaType: "video" }]; const res = await runWallpaperVideo("2026-09-12", () => {}); - expect(res).toEqual({ landscape: "", portrait: "" }); + expect(res).toEqual({ native: "", landscape: "" }); expect(state.calls.spawn).toHaveLength(0); }); @@ -228,11 +242,11 @@ describe("runWallpaperVideo", () => { state.pickRows = [{ ...PICK, composedImagePath: null }]; state.photoRows = [PHOTO]; const res = await runWallpaperVideo("2026-09-12", () => {}); - expect(res).toEqual({ landscape: "", portrait: "" }); + expect(res).toEqual({ native: "", landscape: "" }); expect(state.calls.spawn).toHaveLength(0); }); - it("happy path(v2):生成→buildLoop→renderTextOverlay→双转码(输入=Remotion 成品),COS 契约,回执写 DB,syncDayToGallery 复用", async () => { + it("happy path(4:3 hero,窗外):单腿 spawn,画布 1248×928,仅 native 转码/上传/写列,无 .mov", async () => { state.pickRows = [PICK]; state.photoRows = [PHOTO]; state.faceRows = [{ x: 100, y: 200, w: 400, h: 500 }]; @@ -240,132 +254,176 @@ describe("runWallpaperVideo", () => { const res = await runWallpaperVideo("2026-09-12", () => {}); - // 人脸构图:preprocess 收到 faces 最大 bbox(两侧共用同一 bbox) - expect(state.calls.preprocess).toHaveLength(2); - expect(state.calls.preprocess[0]?.opts).toEqual({ - faceBbox: { x: 100, y: 200, w: 400, h: 500 }, - }); - expect(state.calls.preprocess[1]?.opts).toEqual({ - faceBbox: { x: 100, y: 200, w: 400, h: 500 }, - }); + // 单腿:dims 读取 ×1、preprocess ×1(cover 微裁至 1248×928,20260928 三参签名无人脸窗口) + expect(state.calls.dims).toHaveLength(1); + expect(state.calls.preprocess).toHaveLength(1); + expect(state.calls.preprocess[0]?.width).toBe(1248); + expect(state.calls.preprocess[0]?.height).toBe(928); - // 两次 spawn:横(720p)在前,竖(portrait)在后;双锚定同图;seconds 透传 config(4) - expect(state.calls.spawn).toHaveLength(2); + // spawn ×1:-r 720p + 画布逐轴覆盖(computeNativeCanvas(4000,3000) = 1248×928) + expect(state.calls.spawn).toHaveLength(1); expect(state.calls.spawn[0]?.res).toBe("720p"); - expect(state.calls.spawn[1]?.res).toBe("portrait"); expect(state.calls.spawn[0]?.firstFrame).toBe(state.calls.spawn[0]?.lastFrame); expect(state.calls.spawn[0]?.seconds).toBe(4); - // 画布逐轴覆盖(C2):两条腿均显式传 width/height(横 1280×704 与 720p 档等价、竖 736×1600 覆盖 portrait 档) - expect(state.calls.spawn[0]?.width).toBe(1280); - expect(state.calls.spawn[0]?.height).toBe(704); - expect(state.calls.spawn[1]?.width).toBe(736); - expect(state.calls.spawn[1]?.height).toBe(1600); - - // buildLoop:palindrome 目标时长 = config.wallpaperVideoLoopSeconds(8),输入=raw 生成 - expect(state.calls.buildLoop).toHaveLength(2); - expect(String(state.calls.buildLoop[0]?.src)).toMatch(/2026-09-12-landscape-raw\.mp4$/); - expect(state.calls.buildLoop[0]?.targetSeconds).toBe(8); + expect(state.calls.spawn[0]?.width).toBe(1248); + expect(state.calls.spawn[0]?.height).toBe(928); - // renderTextOverlay:输入=loop 产物,meta 透传 pickDate/title/narrative/takenAt - expect(state.calls.renderTextOverlay).toHaveLength(2); - expect(String(state.calls.renderTextOverlay[0]?.videoPath)).toMatch( - /2026-09-12-landscape-raw-loop\.mp4$/, - ); + // buildLoop / renderTextOverlay 各 ×1;meta 携画布宽高(comp 按比例选) + expect(state.calls.buildLoop).toHaveLength(1); + expect(String(state.calls.buildLoop[0]?.src)).toMatch(/2026-09-12-native-raw\.mp4$/); + expect(state.calls.buildLoop[0]?.targetSeconds).toBe(8); + expect(state.calls.renderTextOverlay).toHaveLength(1); expect(state.calls.renderTextOverlay[0]?.meta).toEqual({ pickDate: "2026-09-12", title: "巷口的猫", narrative: "午后的光落在墙沿。", takenAt: "2016-07-18T14:35:53.000Z", + canvasWidth: 1248, + canvasHeight: 928, }); - // 双轨转码输入 = Remotion 成品(*-loop-overlay.mp4);横 → .mov(aerial),竖 → .mp4(gallery) - expect(state.calls.transcodeAerial).toHaveLength(1); - expect(String(state.calls.transcodeAerial[0]?.[0])).toMatch( - /2026-09-12-landscape-raw-loop-overlay\.mp4$/, - ); - expect(String(state.calls.transcodeAerial[0]?.[1])).toMatch(/2026-09-12-landscape\.mov$/); + // 转码 A(画廊原生)×1 + 尺寸护栏断言画布;转码 B(Aerial)不发生(1.34 < 1.5 窗外) expect(state.calls.transcodeGallery).toHaveLength(1); - expect(String(state.calls.transcodeGallery[0]?.[0])).toMatch( - /2026-09-12-portrait-raw-loop-overlay\.mp4$/, - ); - expect(String(state.calls.transcodeGallery[0]?.[1])).toMatch(/2026-09-12-portrait\.mp4$/); + expect(String(state.calls.transcodeGallery[0]?.[1])).toMatch(/2026-09-12-native\.mp4$/); + expect(state.calls.assertDims).toHaveLength(1); + expect(state.calls.assertDims[0]).toEqual({ + filePath: expect.stringMatching(/2026-09-12-native\.mp4$/), + w: 1248, + h: 928, + }); + expect(state.calls.transcodeAerialNative).toHaveLength(0); - // COS 上传契约:key + contentType - expect(state.calls.upload).toHaveLength(2); - expect(state.calls.upload[0]?.cosKey).toBe("relight/wallpaper-videos/2026-09-12_landscape.mov"); - expect(state.calls.upload[0]?.contentType).toBe("video/quicktime"); - expect(state.calls.upload[1]?.cosKey).toBe("relight/wallpaper-videos/2026-09-12_portrait.mp4"); - expect(state.calls.upload[1]?.contentType).toBe("video/mp4"); + // COS 上传 ×1:native key + video/mp4;landscape 列不写 + expect(state.calls.upload).toHaveLength(1); + expect(state.calls.upload[0]?.cosKey).toBe("relight/wallpaper-videos/2026-09-12_native.mp4"); + expect(state.calls.upload[0]?.contentType).toBe("video/mp4"); - // 回执非空 → 写 DB 两列 + // 回执非空 → 仅写 native 列(一次 UPDATE 合并) const written = state.updates.map((u) => u.vals); - expect(written.some((v) => "wallpaperVideoLandscapeUrl" in v)).toBe(true); - expect(written.some((v) => "wallpaperVideoPortraitUrl" in v)).toBe(true); + expect(written).toHaveLength(1); + expect(written[0]?.wallpaperVideoNativeUrl).toBe(state.uploadReturn); + expect("wallpaperVideoLandscapeUrl" in (written[0] ?? {})).toBe(false); - // syncDayToGallery 复用 + // syncDayToGallery 复用;返回 {native, landscape} expect(state.calls.syncDay).toHaveLength(1); + expect(res.native).toBe(state.uploadReturn); + expect(res.landscape).toBe(""); + }); + + it("happy path(16:9 hero,兼容窗口):native + aerial 双产物双列", async () => { + state.pickRows = [PICK]; + state.photoRows = [PHOTO]; + state.heroDims = { width: 3840, height: 2160 }; // 16:9 → 画布 1440×800(w/h=1.8 ∈ [1.5,1.9]) + state.uploadReturn = "https://b.cos.ap-shanghai.myqcloud.com/relight/x"; + + const res = await runWallpaperVideo("2026-09-12", () => {}); + expect(state.calls.preprocess[0]?.width).toBe(1440); + expect(state.calls.preprocess[0]?.height).toBe(800); + expect(state.calls.spawn[0]?.width).toBe(1440); + expect(state.calls.spawn[0]?.height).toBe(800); + // 双转码 + 双护栏(native 画布 + aerial 1920×1080) + expect(state.calls.transcodeGallery).toHaveLength(1); + expect(state.calls.transcodeAerialNative).toHaveLength(1); + expect(String(state.calls.transcodeAerialNative[0]?.[1])).toMatch(/2026-09-12-landscape\.mov$/); + expect(state.calls.assertDims).toHaveLength(2); + expect(state.calls.assertDims[1]).toEqual({ + filePath: expect.stringMatching(/2026-09-12-landscape\.mov$/), + w: 1920, + h: 1080, + }); + // 双上传:native `_native.mp4` + aerial `_landscape.mov` + expect(state.calls.upload).toHaveLength(2); + expect(state.calls.upload[0]?.cosKey).toBe("relight/wallpaper-videos/2026-09-12_native.mp4"); + expect(state.calls.upload[1]?.cosKey).toBe("relight/wallpaper-videos/2026-09-12_landscape.mov"); + expect(state.calls.upload[1]?.contentType).toBe("video/quicktime"); + // 双列写入(一次 UPDATE 合并两列) + const written = state.updates.map((u) => u.vals); + expect(written).toHaveLength(1); + expect(written[0]?.wallpaperVideoNativeUrl).toBe(state.uploadReturn); + expect(written[0]?.wallpaperVideoLandscapeUrl).toBe(state.uploadReturn); + expect(res.native).toBe(state.uploadReturn); expect(res.landscape).toBe(state.uploadReturn); - expect(res.portrait).toBe(state.uploadReturn); }); - it("无 faces 记录 → faceBbox null(中心构图回退),链路照常", async () => { + it("无 faces 记录 → faceBbox null(风景默认 prompt),链路照常", async () => { state.pickRows = [PICK]; state.photoRows = [PHOTO]; state.faceRows = []; state.uploadReturn = "https://b.cos.ap-shanghai.myqcloud.com/relight/x"; const res = await runWallpaperVideo("2026-09-12", () => {}); - - expect(state.calls.preprocess).toHaveLength(2); - expect(state.calls.preprocess[0]?.opts).toEqual({ faceBbox: null }); - expect(res.landscape).toBe(state.uploadReturn); + expect(state.calls.spawn).toHaveLength(1); + expect(res.native).toBe(state.uploadReturn); }); - it("上传回执空串 → 不写 DB 列(但另一侧成功仍写)", async () => { + it("上传回执空串 → 不写 DB 列", async () => { state.pickRows = [PICK]; state.photoRows = [PHOTO]; - // 第一次调用(横)返回空串,第二次(竖)返回 URL —— 模拟横版上传失败 - const { uploadFile } = await import("../lib/cos/upload"); - const mockUpload = vi.mocked(uploadFile); - let n = 0; - mockUpload.mockImplementation(async () => { - n += 1; - return n === 1 ? "" : "https://b.cos.ap-shanghai.myqcloud.com/relight/x"; - }); + state.uploadReturn = ""; // 上传失败语义(空串回执) const res = await runWallpaperVideo("2026-09-12", () => {}); - const tables = state.updates.map((u) => ({ - field: Object.keys(u.vals)[0], - })); - expect(tables.some((t) => t.field === "wallpaperVideoLandscapeUrl")).toBe(false); - expect(tables.some((t) => t.field === "wallpaperVideoPortraitUrl")).toBe(true); - expect(res.landscape).toBe(""); + expect(state.calls.upload).toHaveLength(1); + expect(state.updates).toHaveLength(0); + expect(res.native).toBe(""); }); - it("单侧 buildLoop 失败 → 该侧空串旁路,另一侧照常", async () => { + it("生成失败(spawn throw)→ 两列均空旁路 log,不向调度层抛(契约 8)", async () => { state.pickRows = [PICK]; state.photoRows = [PHOTO]; state.uploadReturn = "https://b.cos.ap-shanghai.myqcloud.com/relight/x"; - const { buildLoop } = await import("../lib/wallpaper/video"); - vi.mocked(buildLoop).mockImplementationOnce(async () => { - throw new Error("buildLoop ffmpeg 拼接失败"); + const { spawnHoneydoVideo } = await import("../lib/wallpaper/video"); + vi.mocked(spawnHoneydoVideo).mockImplementationOnce(async () => { + throw new Error("honeydo video gen 退出码 1"); }); const logs: string[] = []; const res = await runWallpaperVideo("2026-09-12", (m) => logs.push(m)); + expect(res).toEqual({ native: "", landscape: "" }); + expect(state.updates).toHaveLength(0); + expect(logs.join("\n")).toContain("native 腿失败"); + }); - expect(res.landscape).toBe(""); - expect(res.portrait).toBe(state.uploadReturn); - expect(logs.join("\n")).toContain("横版失败"); - // 横侧失败后竖侧仍完整走完 - expect(state.calls.renderTextOverlay).toHaveLength(1); - expect(String(state.calls.renderTextOverlay[0]?.videoPath)).toMatch(/portrait-raw-loop\.mp4$/); + it("尺寸护栏阻断(转码后 assertVideoDimensions throw)→ 两列均空(场景 5:禁静默失败)", async () => { + state.pickRows = [PICK]; + state.photoRows = [PHOTO]; + state.heroDims = { width: 3840, height: 2160 }; // 兼容窗口:aerial 护栏在 native 列写入前执行 + state.uploadReturn = "https://b.cos.ap-shanghai.myqcloud.com/relight/x"; + const { assertVideoDimensions, DimensionAssertError } = await import("../lib/wallpaper/video"); + const dimMock = vi.mocked(assertVideoDimensions); + const prevImpl = dimMock.getMockImplementation(); + dimMock.mockImplementation(async () => { + throw new DimensionAssertError("转码产物尺寸断言失败: 期望 1920×1080,实际 1918×1080"); + }); + + const logs: string[] = []; + try { + // 设计 D2/红队 SSOT 5.P1:尺寸护栏失败必须传播为 job 失败(rerun CLI exit≠0) + let rejected = false; + try { + await runWallpaperVideo("2026-09-12", (m) => logs.push(m)); + } catch { + rejected = true; + } + expect(rejected, "尺寸护栏失败必须传播为 job 失败").toBe(true); + // 护栏在任一回执列写入之前 → 两列均空(契约 8 / 场景 5.P1) + expect(state.updates).toHaveLength(0); + expect(state.calls.upload).toHaveLength(0); + expect(logs.join("\n")).toContain("尺寸断言失败"); + } finally { + // 恢复工厂默认实现,防泄漏到后续测试(worker 透传等) + dimMock.mockImplementation( + prevImpl ?? + (async (filePath: string, w: number, h: number) => { + state.calls.assertDims.push({ filePath, w, h }); + }), + ); + } }); }); describe("getLargestFaceBbox", () => { - it("faces 空表 → null(中心构图回退)", async () => { + it("faces 空表 → null(风景默认 prompt)", async () => { state.faceRows = []; await expect(getLargestFaceBbox("photo-1")).resolves.toBeNull(); }); @@ -382,7 +440,7 @@ describe("getLargestFaceBbox", () => { }); describe("wallpaperVideoWorker", () => { - it("job.data.pickDate 透传 runWallpaperVideo", async () => { + it("job.data.pickDate 透传 runWallpaperVideo(单腿 spawn ×1)", async () => { state.pickRows = [PICK]; state.photoRows = [PHOTO]; const job = { @@ -390,6 +448,6 @@ describe("wallpaperVideoWorker", () => { log: (m: string) => undefined, }; await wallpaperVideoWorker(job as never); - expect(state.calls.spawn).toHaveLength(2); + expect(state.calls.spawn).toHaveLength(1); }); }); diff --git a/apps/backend/src/__tests__/wallpaper-video-manifest.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-manifest.acceptance.test.ts index 4a02a79..f4efbd2 100644 --- a/apps/backend/src/__tests__/wallpaper-video-manifest.acceptance.test.ts +++ b/apps/backend/src/__tests__/wallpaper-video-manifest.acceptance.test.ts @@ -91,6 +91,8 @@ import { buildManifest } from "../lib/gallery/manifest"; const LANDSCAPE_URL = `https://${TEST_COS.bucket}.cos.${TEST_COS.region}.myqcloud.com/${TEST_COS.prefix}/wallpaper-videos/2026-09-12_landscape.mov`; /** COS key:{prefix}/wallpaper-videos/{pickDate}_portrait.mp4 → 公网 URL(回执形态) */ const PORTRAIT_URL = `https://${TEST_COS.bucket}.cos.${TEST_COS.region}.myqcloud.com/${TEST_COS.prefix}/wallpaper-videos/2026-09-12_portrait.mp4`; +/** COS key:{prefix}/wallpaper-videos/{pickDate}_native.mp4 → 公网 URL(20260928 单腿原生,回执形态) */ +const NATIVE_URL = `https://${TEST_COS.bucket}.cos.${TEST_COS.region}.myqcloud.com/${TEST_COS.prefix}/wallpaper-videos/2026-09-12_native.mp4`; // ============================================================================ // 临时 DB fixture @@ -131,6 +133,9 @@ function ensureWallpaperVideoColumns(sqlite: Database.Database): void { if (!cols.includes("wallpaper_video_portrait_url")) { sqlite.exec("ALTER TABLE daily_picks ADD COLUMN wallpaper_video_portrait_url TEXT"); } + if (!cols.includes("wallpaper_video_native_url")) { + sqlite.exec("ALTER TABLE daily_picks ADD COLUMN wallpaper_video_native_url TEXT"); + } } interface SeedPick { @@ -138,6 +143,7 @@ interface SeedPick { composedImagePath: string | null; landscapeUrl: string | null; portraitUrl: string | null; + nativeUrl?: string | null; } function seedDailyPick(sqlite: Database.Database, p: SeedPick): void { @@ -151,8 +157,8 @@ function seedDailyPick(sqlite: Database.Database, p: SeedPick): void { .prepare( `INSERT INTO daily_picks (id, photo_id, pick_date, title, narrative, score, composed_image_path, members, created_at, - wallpaper_video_landscape_url, wallpaper_video_portrait_url) - VALUES (?, ?, ?, ?, ?, 8.5, ?, '[]', '2026-09-12T06:00:00.000Z', ?, ?)`, + wallpaper_video_landscape_url, wallpaper_video_portrait_url, wallpaper_video_native_url) + VALUES (?, ?, ?, ?, ?, 8.5, ?, '[]', '2026-09-12T06:00:00.000Z', ?, ?, ?)`, ) .run( `pick-${p.pickDate}`, @@ -163,6 +169,7 @@ function seedDailyPick(sqlite: Database.Database, p: SeedPick): void { p.composedImagePath, p.landscapeUrl, p.portraitUrl, + p.nativeUrl ?? null, ); } @@ -185,6 +192,7 @@ interface ManifestDayShape { wallpaperPortrait: string; wallpaperVideoLandscape?: string; wallpaperVideoPortrait?: string; + wallpaperVideoNative?: string; } function findDay(days: ManifestDayShape[], pickDate: string): ManifestDayShape { @@ -332,3 +340,58 @@ describe("manifest 视频字段条件展开(场景 1.P2 / 4.P2 代码化,§ expect("wallpaperVideoPortrait" in day).toBe(false); }); }); + +// ============================================================================ +// 20260928 单腿原生:wallpaperVideoNative 条件展开(场景 6.P5 / 7.P2 代码化) +// ============================================================================ + +describe("manifest wallpaperVideoNative 条件展开(20260928 场景 6.P5 / 7.P2)", () => { + beforeEach(() => { + mockCosPutObject.mockClear(); + mockCosSliceUploadFile.mockClear(); + }); + + it("native 列非空 → wallpaperVideoNative 展开为 native URL(非 legacy URL,场景 7.P2)", async () => { + const env = createTestEnv(); + const sqlite = openDb(env.dbPath); + seedDailyPick(sqlite, { + pickDate: "2026-09-12", + composedImagePath: "daily-composed/2026-09-12.jpg", + landscapeUrl: LANDSCAPE_URL, + portraitUrl: PORTRAIT_URL, + nativeUrl: NATIVE_URL, + }); + sqlite.close(); + process.env.DATABASE_PATH = env.dbPath; + + const manifest = { days: await buildManifestDays() }; + const day = findDay(manifest.days, "2026-09-12"); + expect("wallpaperVideoNative" in day).toBe(true); + expect(day.wallpaperVideoNative).toBe(NATIVE_URL); + expect((day.wallpaperVideoNative as string).endsWith("_native.mp4")).toBe(true); + expect(day.wallpaperVideoNative).not.toBe(LANDSCAPE_URL); + expect(day.wallpaperVideoNative).not.toBe(PORTRAIT_URL); + }); + + it("native 列为空(legacy-only 日)→ 无 wallpaperVideoNative 字段;landscape/portrait 照常展开(场景 6.P2/6.P5)", async () => { + const env = createTestEnv(); + const sqlite = openDb(env.dbPath); + seedDailyPick(sqlite, { + pickDate: "2026-09-11", + composedImagePath: "daily-composed/2026-09-11.jpg", + landscapeUrl: LANDSCAPE_URL, + portraitUrl: PORTRAIT_URL, + nativeUrl: null, + }); + sqlite.close(); + process.env.DATABASE_PATH = env.dbPath; + + const manifest = { days: await buildManifestDays() }; + const day = findDay(manifest.days, "2026-09-11"); + // 场景 6.P5:native 列空 → 字段缺省(JSON 键不存在,非空串) + expect(Object.prototype.hasOwnProperty.call(day, "wallpaperVideoNative")).toBe(false); + // 场景 6.P2:历史日 legacy 两字段照常展开非空 + expect(day.wallpaperVideoLandscape).toBe(LANDSCAPE_URL); + expect(day.wallpaperVideoPortrait).toBe(PORTRAIT_URL); + }); +}); diff --git a/apps/backend/src/__tests__/wallpaper-video-manifest.test.ts b/apps/backend/src/__tests__/wallpaper-video-manifest.test.ts index a42a33d..e8f0a48 100644 --- a/apps/backend/src/__tests__/wallpaper-video-manifest.test.ts +++ b/apps/backend/src/__tests__/wallpaper-video-manifest.test.ts @@ -1,12 +1,15 @@ /** * 单测:manifest 视频字段(任务 3)— COS key 函数 + ManifestDay 条件展开 * - * 契约(state.md ## 契约规约 数据结构): + * 契约(state.md ## 契约规约 数据结构;20260928 单腿原生): * COS key:`{config.cos.prefix}/wallpaper-videos/{pickDate}_landscape.mov`、 - * `{config.cos.prefix}/wallpaper-videos/{pickDate}_portrait.mp4` - * ManifestDay 增可选字段 `wallpaperVideoLandscape?` / `wallpaperVideoPortrait?` + * `{config.cos.prefix}/wallpaper-videos/{pickDate}_portrait.mp4`(legacy)、 + * `{config.cos.prefix}/wallpaper-videos/{pickDate}_native.mp4`(新) + * ManifestDay 增可选字段 `wallpaperVideoLandscape?` / `wallpaperVideoPortrait?` / + * `wallpaperVideoNative?`(20260928 新增) * 条件展开:仅当 DB 回执列非空时字段存在于 JSON(列 null/空串 → 字段缺省,不输出空串—— - * 冻结场景 4.P2 要求开关关闭时字段不存在)。 + * 冻结场景 4.P2 要求开关关闭时字段不存在);场景 6.P5:native 列为空 → 无 + * wallpaperVideoNative 字段;场景 7.P2:native 优先展开为 native URL。 */ import fs from "node:fs"; import os from "node:os"; @@ -38,9 +41,8 @@ vi.mock("../lib/config", () => ({ }, })); -const { wallpaperVideoLandscapeCosKey, wallpaperVideoPortraitCosKey } = await import( - "../lib/gallery/manifest" -); +const { wallpaperVideoLandscapeCosKey, wallpaperVideoPortraitCosKey, wallpaperVideoNativeCosKey } = + await import("../lib/gallery/manifest"); const { buildManifest } = await import("../lib/gallery/manifest"); // ============================================================================ @@ -80,6 +82,7 @@ interface InsertPickOpts { composedImagePath?: string | null; landscapeUrl?: string | null; portraitUrl?: string | null; + nativeUrl?: string | null; } function insertPick(opts: InsertPickOpts): void { @@ -88,14 +91,15 @@ function insertPick(opts: InsertPickOpts): void { `INSERT INTO daily_picks (id, photo_id, pick_date, title, narrative, score, composed_image_path, members, wallpaper_video_landscape_url, wallpaper_video_portrait_url, - created_at) - VALUES (?, 'photo-0', ?, '标题', '叙述', 0, ?, '[]', ?, ?, ?)`, + wallpaper_video_native_url, created_at) + VALUES (?, 'photo-0', ?, '标题', '叙述', 0, ?, '[]', ?, ?, ?, ?)`, ).run( `pick-${opts.pickDate}`, opts.pickDate, opts.composedImagePath ?? null, opts.landscapeUrl ?? null, opts.portraitUrl ?? null, + opts.nativeUrl ?? null, new Date().toISOString(), ); db.close(); @@ -133,6 +137,12 @@ describe("wallpaperVideo COS key 函数", () => { "relight/wallpaper-videos/2026-09-12_portrait.mp4", ); }); + + it("native key = {prefix}/wallpaper-videos/{pickDate}_native.mp4(20260928 新增)", () => { + expect(wallpaperVideoNativeCosKey("2026-09-12")).toBe( + "relight/wallpaper-videos/2026-09-12_native.mp4", + ); + }); }); // ============================================================================ diff --git a/apps/backend/src/__tests__/wallpaper-video-native-canvas.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-native-canvas.acceptance.test.ts new file mode 100644 index 0000000..4b945d2 --- /dev/null +++ b/apps/backend/src/__tests__/wallpaper-video-native-canvas.acceptance.test.ts @@ -0,0 +1,173 @@ +/** + * 验收测试(红队):单腿原生壁纸视频 — 画布计算纯函数 computeNativeCanvas + * + * 设计文档(state.md §设计文档 D1 / §验收场景)对应谓词: + * - 场景 2.P1 [det-machine]:样本集(1:1、4:3、3:2、16:9、9:16、21:9、20000×10000) + * ∀ w%32==0 && h%32==0 + * assert: 每个样本输出的 width 与 height 都是 32 的倍数 + * - 场景 2.P2 [det-machine]:∀ width*height ≤ 1177600 && ≥ 1177600/2(= 588800) + * - 场景 2.P3 [det-machine]:16:9 输入 → 输出比例偏差 ≤2% + * - 场景 2.P4 [det-machine]:9:16 输入 → 输出仍竖版(h>w)且比例偏差 ≤2% + * (kill 固定横版画布 No-op) + * + * 追加设计锁定(D1 逐字推导,非红队自由发挥): + * - D1 参考输出精确 fixture:1:1→1056×1056;3:2→1312×864;16:9→1440×800; + * 9:16→800×1440;4:3→1248×928;3:4→928×1248;2.35:1→1664×704 + * (「触 R clamp 边界的样本由精确 fixture 断言锁定」——本文件的精确 fixture 均为 + * D1 参考输出逐字,未触 R clamp) + * - 朝向由原图决定(D1:landscape = origW >= origH,与 R 解耦):2:3 → 864×1312 + * - 极端比例(R 触 2.40 上限,如 10000×3000)走 D1 兜底分支: + * L = round32(min(MIN_SHORT*R, BUDGET/MIN_SHORT))、s = MIN_SHORT=704 + * → 横 1664×704 / 竖 704×1664(兜底公式确定性推导,短边恒 704) + * - 质量纪律下限:任意样本输出短边 ≥ 704(主循环 s ≥ MIN_SHORT 门 + 兜底 s = MIN_SHORT) + * + * 绑定表(§验收场景 实现绑定表):尺寸计算入口 = native-canvas.ts 导出的 + * computeNativeCanvas(origW, origH);像素预算 pixel_budget = 1177600。 + * + * 红队铁律:不读蓝队实现代码(native-canvas.ts 按契约导出名黑盒 import);纯函数直调, + * 无环境依赖——不 skip、硬断言,import 失败即红(TDD 红灯)。 + */ +import fs from "node:fs"; +import path from "node:path"; +import { describe, expect, it } from "vitest"; +import { computeNativeCanvas } from "../lib/wallpaper/native-canvas"; + +const ARTIFACT_DIR = "/tmp/autopilot-artifacts"; + +function writeArtifact(id: string, content: string): void { + fs.mkdirSync(ARTIFACT_DIR, { recursive: true }); + fs.writeFileSync(path.join(ARTIFACT_DIR, `${id}.out`), content); +} + +// 契约字面量(§设计文档 D1 逐字) +const BUDGET = 1_177_600; +const BUDGET_HALF = BUDGET / 2; // 588800(绑定表:下限语义 ≥ budget/2) +const MIN_SHORT = 704; + +/** D1 参考输出精确 fixture(逐字):[origW, origH, expectW, expectH] */ +const REFERENCE_FIXTURES: Array<[number, number, number, number]> = [ + [2000, 2000, 1056, 1056], // 1:1 → 1056×1056 + [3000, 2000, 1312, 864], // 3:2 → 1312×864 + [2000, 3000, 864, 1312], // 2:3(3:2 竖拍镜像,朝向由原图决定) + [1920, 1080, 1440, 800], // 16:9 → 1440×800 + [1080, 1920, 800, 1440], // 9:16 → 800×1440 + [4000, 3000, 1248, 928], // 4:3 → 1248×928 + [3000, 4000, 928, 1248], // 3:4 → 928×1248 + [2350, 1000, 1664, 704], // 2.35:1 → 1664×704(D1 参考输出逐字) +]; + +/** 场景 2.P1 样本集(谓词逐字):1:1、4:3、3:2、16:9、9:16、21:9、20000×10000 */ +const SAMPLE_SET: Array<[number, number]> = [ + [2000, 2000], // 1:1 + [4000, 3000], // 4:3 + [3000, 2000], // 3:2 + [1920, 1080], // 16:9 + [1080, 1920], // 9:16 + [2100, 900], // 21:9 + [20000, 10000], // 2:1 超大图 +]; + +/** 比例偏差(相对原比例),供 P3/P4 的 ≤2% 判定 */ +function ratioDeviation(origW: number, origH: number, outW: number, outH: number): number { + const src = origW / origH; + const out = outW / outH; + return Math.abs(out - src) / src; +} + +describe("场景 2:computeNativeCanvas 画布尺寸纯逻辑(kill No-op 核心)", () => { + it("场景 2.P1 [det-machine]:样本集 ∀ 输出 w%32==0 && h%32==0,且短边 ≥ 704(质量纪律)", () => { + const results = SAMPLE_SET.map(([w, h]) => { + const out = computeNativeCanvas(w, h); + return { input: [w, h], ...out }; + }); + writeArtifact("s2p1", JSON.stringify(results, null, 2)); + + for (const r of results) { + expect(r.width % 32, `${r.input} → width ${r.width} 必须是 32 的倍数`).toBe(0); + expect(r.height % 32, `${r.input} → height ${r.height} 必须是 32 的倍数`).toBe(0); + expect( + Math.min(r.width, r.height), + `${r.input} → 短边 ${Math.min(r.width, r.height)} 必须 ≥ MIN_SHORT(704)`, + ).toBeGreaterThanOrEqual(MIN_SHORT); + } + }); + + it("场景 2.P2 [det-machine]:样本集 ∀ width*height ≤ 1177600 && ≥ 1177600/2", () => { + const results = SAMPLE_SET.map(([w, h]) => { + const out = computeNativeCanvas(w, h); + return { input: [w, h], ...out, pixels: out.width * out.height }; + }); + writeArtifact("s2p2", JSON.stringify(results, null, 2)); + + for (const r of results) { + expect( + r.pixels, + `${r.input} → ${r.width}x${r.height} = ${r.pixels}px 必须 ≤ BUDGET(1177600)`, + ).toBeLessThanOrEqual(BUDGET); + expect( + r.pixels, + `${r.input} → ${r.width}x${r.height} = ${r.pixels}px 必须 ≥ BUDGET/2(588800)`, + ).toBeGreaterThanOrEqual(BUDGET_HALF); + } + }); + + it("场景 2.P3 [det-machine]:16:9 输入 → 输出比例偏差 ≤2%", () => { + const out = computeNativeCanvas(1920, 1080); + const dev = ratioDeviation(1920, 1080, out.width, out.height); + writeArtifact("s2p3", JSON.stringify({ input: [1920, 1080], ...out, deviation: dev })); + expect( + dev, + `16:9 → ${out.width}x${out.height} 比例偏差 ${(dev * 100).toFixed(2)}%`, + ).toBeLessThanOrEqual(0.02); + }); + + it("场景 2.P4 [det-machine]:9:16 输入 → 仍竖版(h>w)且比例偏差 ≤2%(kill 固定横版 No-op)", () => { + const out = computeNativeCanvas(1080, 1920); + const dev = ratioDeviation(1080, 1920, out.width, out.height); + writeArtifact("s2p4", JSON.stringify({ input: [1080, 1920], ...out, deviation: dev })); + expect(out.height, `9:16 → ${out.width}x${out.height} 必须保持竖版(h>w)`).toBeGreaterThan( + out.width, + ); + expect( + dev, + `9:16 → ${out.width}x${out.height} 比例偏差 ${(dev * 100).toFixed(2)}%`, + ).toBeLessThanOrEqual(0.02); + }); + + it("D1 参考输出精确 fixture:7 组比例逐字锁定(含朝向镜像 2:3)", () => { + const results = REFERENCE_FIXTURES.map(([w, h, ew, eh]) => { + const out = computeNativeCanvas(w, h); + return { input: [w, h], expected: [ew, eh], actual: [out.width, out.height] }; + }); + writeArtifact("s2-fixtures", JSON.stringify(results, null, 2)); + + for (const [w, h, ew, eh] of REFERENCE_FIXTURES) { + const out = computeNativeCanvas(w, h); + expect( + `${out.width}x${out.height}`, + `computeNativeCanvas(${w}, ${h}) 必须等于 D1 参考输出 ${ew}x${eh}`, + ).toBe(`${ew}x${eh}`); + } + }); + + it("D1 兜底分支:极端比例(R 触 2.40 clamp)→ 短边 = 704、像素 ≤ BUDGET、朝向保持", () => { + // 10000×3000:R = 3.333 → clamp 2.40;兜底 L = round32(min(704*R, BUDGET/704)) = 1664 + const landscape = computeNativeCanvas(10000, 3000); + expect( + `${landscape.width}x${landscape.height}`, + "极端横图兜底必须为 1664×704(D1 兜底公式确定性产物)", + ).toBe("1664x704"); + + // 3000×10000:同 R,朝向镜像 → 704×1664 + const portrait = computeNativeCanvas(3000, 10000); + expect(`${portrait.width}x${portrait.height}`, "极端竖图兜底必须按朝向定向为 704×1664").toBe( + "704x1664", + ); + + // 兜底产物同样满足像素预算与短边纪律 + expect(landscape.width * landscape.height).toBeLessThanOrEqual(BUDGET); + expect(portrait.width * portrait.height).toBeLessThanOrEqual(BUDGET); + expect(Math.min(landscape.width, landscape.height)).toBe(MIN_SHORT); + expect(Math.min(portrait.width, portrait.height)).toBe(MIN_SHORT); + }); +}); diff --git a/apps/backend/src/__tests__/wallpaper-video-native-canvas.test.ts b/apps/backend/src/__tests__/wallpaper-video-native-canvas.test.ts new file mode 100644 index 0000000..cde6836 --- /dev/null +++ b/apps/backend/src/__tests__/wallpaper-video-native-canvas.test.ts @@ -0,0 +1,178 @@ +/** + * 单测:lib/wallpaper/native-canvas.ts — computeNativeCanvas(20260928 单腿原生比例 T1) + * + * 契约(state.md ## 设计文档 D1 / ## 契约规约 实现绑定表): + * computeNativeCanvas(origW, origH) → {width, height} + * - R = clamp(max/min, 1.0, 2.40)(竖图横图统一处理,朝向由原图决定与 R 解耦) + * - BUDGET = 1177600(= 736×1600 注意力 token 等价预算);MIN_SHORT = 704 + * - 主循环 L 从 1600 按 32 递减:s = round32(L/R),L*s ≤ BUDGET 且 s ≥ MIN_SHORT 即返回 + * - 兜底(极端比例):L = round32(min(704R, BUDGET/704)),s = 704,保证像素 ≤ BUDGET + * - 尺寸恒为 32 倍数;参考输出(design 逐字): + * 1:1→1056×1056;3:2→1312×864;16:9→1440×800;9:16→800×1440; + * 4:3→1248×928;3:4→928×1248;2.35:1→1664×704 + * 场景 2.P1-P4 谓词(SSOT): + * - P1 样本集(1:1、4:3、3:2、16:9、9:16、21:9、20000×10000)∀ w%32==0 && h%32==0 + * - P2 ∀ w*h ≤ 1177600 && ≥ 1177600/2 + * - P3 16:9 输入输出比例偏差 ≤2% + * - P4 9:16 输入输出仍竖版(h>w)且比例偏差 ≤2%(kill 固定横版画布 No-op) + * 设计注:≤2% 偏差性质断言仅适用于未触 R clamp 边界的样本(触边界样本由精确 fixture 断言锁定)。 + */ +import { describe, expect, it } from "vitest"; +import { computeNativeCanvas } from "../lib/wallpaper/native-canvas"; + +const BUDGET = 1_177_600; +const BUDGET_HALF = BUDGET / 2; + +describe("computeNativeCanvas 精确 fixture 表(design D1 参考输出逐字)", () => { + const exactCases: Array<{ + name: string; + origW: number; + origH: number; + width: number; + height: number; + }> = [ + { name: "1:1 正方形", origW: 3000, origH: 3000, width: 1056, height: 1056 }, + { name: "3:2 横图", origW: 3000, origH: 2000, width: 1312, height: 864 }, + { name: "16:9 横图", origW: 1920, origH: 1080, width: 1440, height: 800 }, + { name: "9:16 竖图(朝向保持竖版)", origW: 1080, origH: 1920, width: 800, height: 1440 }, + { name: "4:3 横图", origW: 4000, origH: 3000, width: 1248, height: 928 }, + { name: "3:4 竖图", origW: 3000, origH: 4000, width: 928, height: 1248 }, + { name: "2.35:1 超宽(兜底分支)", origW: 2350, origH: 1000, width: 1664, height: 704 }, + { name: "R clamp 下边界 2.40", origW: 2400, origH: 1000, width: 1664, height: 704 }, + { + name: "R clamp 竖版边界(原比例 0.40 → R 钳 2.40,朝向仍竖版)", + origW: 1000, + origH: 2500, + width: 704, + height: 1664, + }, + { name: "21:9 超宽(场景 2.P1 样本)", origW: 2520, origH: 1080, width: 1632, height: 704 }, + { + name: "20000×10000 大图(场景 2.P1 样本,R=2.0;752/32=23.5 四舍五入入 24 档)", + origW: 20000, + origH: 10000, + width: 1504, + height: 768, + }, + ]; + + for (const c of exactCases) { + it(`${c.name}: ${c.origW}×${c.origH} → ${c.width}×${c.height}`, () => { + expect(computeNativeCanvas(c.origW, c.origH)).toEqual({ + width: c.width, + height: c.height, + }); + }); + } +}); + +describe("场景 2.P1-P4 谓词(SSOT 逐字)", () => { + /** 场景 2.P1 样本集(谓词 SSOT 逐字:1:1、4:3、3:2、16:9、9:16、21:9、20000×10000) */ + const samples: Array<[number, number]> = [ + [1000, 1000], + [4000, 3000], + [3000, 2000], + [1920, 1080], + [1080, 1920], + [2520, 1080], + [20000, 10000], + ]; + + it("场景2.P1: ∀ 样本 w%32==0 && h%32==0", () => { + for (const [w, h] of samples) { + const { width, height } = computeNativeCanvas(w, h); + expect(width % 32, `样本 ${w}×${h} → ${width}×${height},宽非 32 倍数`).toBe(0); + expect(height % 32, `样本 ${w}×${h} → ${width}×${height},高非 32 倍数`).toBe(0); + } + }); + + it("场景2.P2: ∀ w*h ≤ 1177600 && ≥ 1177600/2", () => { + for (const [w, h] of samples) { + const { width, height } = computeNativeCanvas(w, h); + const pixels = width * height; + expect( + pixels, + `样本 ${w}×${h} → ${width}×${height} 像素 ${pixels} 超预算`, + ).toBeLessThanOrEqual(BUDGET); + expect( + pixels, + `样本 ${w}×${h} → ${width}×${height} 像素 ${pixels} 低于下限`, + ).toBeGreaterThanOrEqual(BUDGET_HALF); + } + }); + + it("场景2.P3: 16:9 输入输出比例偏差 ≤2%", () => { + const { width, height } = computeNativeCanvas(1920, 1080); + const deviation = Math.abs(width / height - 16 / 9) / (16 / 9); + expect(deviation).toBeLessThanOrEqual(0.02); + }); + + it("场景2.P4: 9:16 输入输出仍竖版(h>w)且比例偏差 ≤2%(kill 固定横版画布 No-op)", () => { + const { width, height } = computeNativeCanvas(1080, 1920); + expect(height).toBeGreaterThan(width); + const deviation = Math.abs(width / height - 9 / 16) / (9 / 16); + expect(deviation).toBeLessThanOrEqual(0.02); + }); +}); + +describe("computeNativeCanvas 性质与边界", () => { + it("多比例性质扫描(未触 clamp 边界):32 倍数 + 像素预算 + 比例偏差 ≤2% + 朝向保持", () => { + const ratios: Array<[number, number]> = [ + [1, 1], + [5, 4], + [4, 3], + [3, 2], + [16, 10], + [16, 9], + [9, 16], + [10, 16], + [3, 4], + [2, 3], + [9, 21], + [7, 5], + [1.85, 1], + [1.5, 1], + [1.9, 1], + ]; + for (const [w, h] of ratios) { + const { width, height } = computeNativeCanvas(w, h); + expect(width % 32).toBe(0); + expect(height % 32).toBe(0); + expect(width * height).toBeLessThanOrEqual(BUDGET); + expect(width * height).toBeGreaterThanOrEqual(BUDGET_HALF); + // 朝向保持:横图出横画布、竖图出竖画布、正方出正方 + if (w > h) expect(width).toBeGreaterThan(height); + if (w < h) expect(height).toBeGreaterThan(width); + if (w === h) expect(width).toBe(height); + // 未触 R clamp 边界的样本:比例偏差 ≤2%(design 注) + const r = Math.max(w, h) / Math.min(w, h); + if (r >= 1.0 && r <= 2.4) { + const outR = Math.max(width, height) / Math.min(width, height); + expect( + Math.abs(outR - r) / r, + `${w}:${h} → ${width}×${height} 偏差超 2%`, + ).toBeLessThanOrEqual(0.02); + } + } + }); + + it("MIN_SHORT 纪律:短轴恒 ≥704(R≤2.4 全域)", () => { + for (let rNum = 10; rNum <= 24; rNum++) { + const { width, height } = computeNativeCanvas(rNum * 100, 1000); + expect(Math.min(width, height)).toBeGreaterThanOrEqual(704); + } + }); + + it("极端比例(R>2.4 clamp)像素 ≤ BUDGET(兜底分支预算保证)", () => { + for (const [w, h] of [ + [1000, 4000], + [1000, 10000], + [5000, 1000], + ] as Array<[number, number]>) { + const { width, height } = computeNativeCanvas(w, h); + expect(width * height).toBeLessThanOrEqual(BUDGET); + expect(width % 32).toBe(0); + expect(height % 32).toBe(0); + } + }); +}); diff --git a/apps/backend/src/__tests__/wallpaper-video-native-job.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-native-job.acceptance.test.ts new file mode 100644 index 0000000..dee34fe --- /dev/null +++ b/apps/backend/src/__tests__/wallpaper-video-native-job.acceptance.test.ts @@ -0,0 +1,752 @@ +/** + * 验收测试(红队):单腿原生壁纸视频 — job 行为契约(mock spawn 面,无 GPU) + * + * 设计文档(state.md §设计文档 D2 / §验收场景)对应谓词: + * - 场景 1.P2 [det-machine](job 级代码化):生成画布跟随原图比例 —— job 以 + * computeNativeCanvas(hero 原图 dims) 的画布调 preprocessHeroFrame,并把 W/H 透传 + * spawnHoneydoVideo(契约 6:spawn argv --width W --height H 两轴显式覆盖档位) + * - 场景 1.P3 [det-machine](job 级代码化):画布像素 ≤ 1177600 && ≥ /2 + * - 场景 1.P4 [det-machine]:DB 当日行 wallpaper_video_native_url != "" + * && wallpaper_video_landscape_url == "" && wallpaper_video_portrait_url == "" + * (DB 列「空」在本套件惯例为 NULL——与既有 wallpaper-video-job 验收同语义) + * - 场景 3.P3 [det-machine](COS HEAD 前置):护栏通过后 wallpaper_video_landscape_url + * != ""(兼容日;真实 COS HEAD 200 落 realprocess 文件) + * - 场景 4.P1 [det-machine](job 级代码化):非兼容日产物零 .mov —— transcodeForAerial + * 调用次数 == 0 且无 _landscape.mov 上传 + * - 场景 4.P4 [det-machine]:非兼容日 wallpaper_video_landscape_url == ""(未上传) + * - 场景 5.P1 [real-process→job 级代码化]:注入错尺寸转码产物 → 护栏阻断: + * job 失败(runWallpaperVideo reject → rerun CLI exit != 0 的进程级前提)且回执列空 + * - 场景 5.P2 [det-machine]:失败日志显式含尺寸断言语义(禁静默失败) + * - 场景 5.P3 [det-machine]:对照组正确尺寸 → 护栏放行、回执写库(kill 永远拒绝 No-op) + * - 场景 7 [det-machine DB 侧]:兼容日 native 与 landscape 并存(302 路由 302→landscape、 + * manifest 取 native 的断言落 route/manifest 两个验收文件) + * + * 设计契约(D2 逐字):produceNativeSide 单腿——单腿 = 全程恰 1 次 honeydo spawn(kill + * 双腿 No-op);转码A(画廊原生,沿用 transcodeForGallery)恒发生;转码B(条件 Aerial) + * 仅画布 ratio ∈ [1.5, 1.9] 发生;legacy 竖腿删除 → _portrait.mp4 不再新写(契约 2)。 + * + * // CONTRACT_AMBIGUOUS: 尺寸护栏的实现位置(job 级 post-transcode probe vs 转码函数内部) + * // 契约未声明导出名——本文件按 D2「每条转码产物 probe 断言尺寸,不匹配即 throw」在 job + * // 边界注入(mock 转码产物落错尺寸文件),护栏无论住在哪里都必须拦下错尺寸产物;真实 + * // ffmpeg 转码自身的产物 invariant 落 wallpaper-video-native-transcode 验收文件。 + * // CONTRACT_AMBIGUOUS: 失败日志的措辞未契约化——5.P2 断言捕获的 console 输出包含注入的 + * // 错误宽度字面量(日志必须能定位「什么尺寸不匹配」),不锁定措辞。 + * + * 红队铁律:不读蓝队实现代码;注入点只用契约已声明接口名:computeNativeCanvas(绑定表 + * SSOT)/ preprocessHeroFrame / spawnHoneydoVideo / buildLoop / renderTextOverlay / + * transcodeForAerial / transcodeForGallery / uploadFile / safeUploadFile / syncDayToGallery。 + */ +import { spawnSync } from "node:child_process"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import Database from "better-sqlite3"; +import sharp from "sharp"; +import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { setupTestSchema } from "./helpers/test-schema"; + +// [capability-gate 惯例,同 wallpaper-video-job.acceptance.test.ts]:job 前置校验会 +// access honeydo 路径——CI 无 honeydo 时整组 skip(本机装 honeydo 即自动真跑)。 +// 纯函数/manifest/route/transcode 级断言在其它验收文件中无条件可跑。 +const HONEYDO_AVAILABLE = spawnSync("which", ["honeydo"], { encoding: "utf8" }).status === 0; +if (!HONEYDO_AVAILABLE) { + console.warn("[wallpaper-video-native-job] honeydo 不可用——job 级用例 capability-gate skip"); +} +const dJob = HONEYDO_AVAILABLE ? describe : describe.skip; + +// ============================================================================ +// hoisted holder + mocks +// ============================================================================ + +const holder = vi.hoisted(() => ({ + dbPath: ":memory:", + storageRoot: "/tmp/relight-none", + videoWorkspacePath: "/tmp/relight-none/video-workspace", + enabled: true, + receiptMode: "url" as "url" | "empty", + seconds: 4, + loopSeconds: 8, + honeydoPath: "/usr/bin/false", + firstFramePath: "/tmp/relight-none/first.png", + /** spawn mock 产物登记表:`${width}x${height}` → 源 mp4 路径 */ + productByCanvas: new Map(), + loopPath: "/tmp/relight-none/loop-master.mp4", + overlaidPath: "/tmp/relight-none/overlaid.mp4", + /** transcodeForGallery 产物模式:pass=复制源 / badsz=落错尺寸文件 */ + galleryMode: "pass" as "pass" | "badsz", + /** transcodeForAerial 产物模式:pass=落 1920×1080 / badsz=落 1918×1080 */ + aerialMode: "pass" as "pass" | "badsz", + galleryBadPath: "/tmp/relight-none/bad-1310x864.mp4", + aerialBadPath: "/tmp/relight-none/bad-1918x1080.mp4", + aerialOkPath: "/tmp/relight-none/ok-1920x1080.mp4", +})); + +/** 串接顺序游标 */ +const pipelineSeq: string[] = []; + +const TEST_COS = vi.hoisted(() => ({ + bucket: "little-bee-assets-1324334992", + region: "ap-shanghai", + prefix: "relight", +})); + +vi.mock("../lib/config", () => ({ + config: { + get databasePath() { + return process.env.DATABASE_PATH ?? holder.dbPath; + }, + get storageRoot() { + return holder.storageRoot; + }, + get wallpaperVideoEnabled() { + return holder.enabled; + }, + get wallpaperVideoSeconds() { + return holder.seconds; + }, + get wallpaperVideoLoopSeconds() { + return holder.loopSeconds; + }, + wallpaperVideoSpawnTimeoutMs: 5400000, + wallpaperVideoPromptPerson: + "人物保持自然状态,轻轻侧头微笑,发丝和衣角随风轻扬,手部小幅度轻柔互动,光影缓缓流动", + wallpaperVideoPromptScene: + "镜头极缓慢推近,光影柔和流动,云影水波轻轻变幻,花草树叶随风微动,画面宁静而生动", + get videoWorkspacePath() { + return holder.videoWorkspacePath; + }, + get honeydoCliPath() { + return holder.honeydoPath; + }, + repoRoot: "/tmp/relight-none", + port: 3000, + redisUrl: "redis://localhost:6379", + bullmqPrefix: "bull-wvn-test", + dailySelectionConcurrency: 1, + dailyAutoHealDays: 0, + dailySelectEnabled: false, + ai: { + baseUrl: "", + apiKey: "", + visionModel: "", + model: "", + promptVersion: "v2", + motion: { enabled: false, baseUrl: "", apiKey: "", model: "" }, + }, + video: { enabled: true, frameCount: 6, ffmpegPath: "ffmpeg", ffprobePath: "ffprobe" }, + daily: { cronTime: "0 0 * * *", maxCandidates: 20, timezone: "Asia/Shanghai" }, + cos: { + secretId: "test-id", + secretKey: "test-key", + bucket: TEST_COS.bucket, + region: TEST_COS.region, + prefix: TEST_COS.prefix, + }, + galleryPublicUrl: "https://gallery.stringzhao.life", + gallery: { vpsHost: "127.0.0.1", vpsUser: "test", vpsKey: "/tmp/test-key", vpsPath: "/tmp/g" }, + }, +})); + +vi.mock("../db", async () => { + const actualSchema = await import("../db/schema"); + const Database = (await import("better-sqlite3")).default; + const { drizzle } = await import("drizzle-orm/better-sqlite3"); + const sqlite = new Database(holder.dbPath); + sqlite.pragma("journal_mode = WAL"); + sqlite.pragma("foreign_keys = ON"); + return { db: drizzle(sqlite, { schema: actualSchema }), schema: actualSchema }; +}); + +const mockCosPutObject = vi.hoisted(() => vi.fn(async () => ({}))); +const mockCosSliceUploadFile = vi.hoisted(() => vi.fn(async () => ({}))); + +vi.mock("cos-nodejs-sdk-v5", () => { + const S3 = vi.fn(() => ({ + putObject: mockCosPutObject, + sliceUploadFile: mockCosSliceUploadFile, + getObjectUrl: vi.fn(), + })); + return { default: S3 }; +}); + +const mockUploadFile = vi.hoisted(() => vi.fn()); +const mockSafeUpload = vi.hoisted(() => vi.fn()); + +vi.mock("../lib/cos/upload", async (importOriginal) => { + const actual = await importOriginal(); + return { ...actual, uploadFile: mockUploadFile }; +}); + +const mockSyncDayToGallery = vi.hoisted(() => vi.fn(async (_pickDate: string) => {})); + +vi.mock("../lib/gallery/sync", async (importOriginal) => { + const actual = await importOriginal(); + return { ...actual, syncDayToGallery: mockSyncDayToGallery, safeUploadFile: mockSafeUpload }; +}); + +const mockPreprocessHeroFrame = vi.hoisted(() => vi.fn()); +const mockSpawnHoneydoVideo = vi.hoisted(() => vi.fn()); +const mockBuildLoop = vi.hoisted(() => vi.fn()); +const mockRenderTextOverlay = vi.hoisted(() => vi.fn()); +const mockTranscodeForAerial = vi.hoisted(() => vi.fn()); +const mockTranscodeForGallery = vi.hoisted(() => vi.fn()); + +vi.mock("../lib/wallpaper/video", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + preprocessHeroFrame: mockPreprocessHeroFrame, + spawnHoneydoVideo: mockSpawnHoneydoVideo, + buildLoop: mockBuildLoop, + renderTextOverlay: mockRenderTextOverlay, + transcodeForAerialNative: mockTranscodeForAerial, + transcodeForGallery: mockTranscodeForGallery, + }; +}); + +vi.mock("../jobs/queues", () => ({ + scanQueue: { add: vi.fn(async () => ({ id: "mock" })) }, + analyzeQueue: { add: vi.fn(async () => ({ id: "mock" })) }, + dailyQueue: { add: vi.fn(async () => ({ id: "mock" })) }, + dailyPushQueue: { add: vi.fn(async () => ({ id: "mock" })) }, + dailyVideoQueue: { add: vi.fn(async () => ({ id: "mock" })) }, + wallpaperVideoQueue: { add: vi.fn(async () => ({ id: "mock" })) }, +})); + +// ============================================================================ +// 契约字面量(§契约规约 逐字) +// ============================================================================ + +const PREFIX = TEST_COS.prefix; +/** 契约 2:COS key = relight/wallpaper-videos/{pickDate}_native.mp4(新) */ +function nativeKey(pickDate: string): string { + return `${PREFIX}/wallpaper-videos/${pickDate}_native.mp4`; +} +function landscapeKey(pickDate: string): string { + return `${PREFIX}/wallpaper-videos/${pickDate}_landscape.mov`; +} +function receiptFor(cosKey: string): string { + if (holder.receiptMode === "empty") return ""; + return `https://${TEST_COS.bucket}.cos.${TEST_COS.region}.myqcloud.com/${cosKey}`; +} + +/** 兼容日:hero 3000×2000(3:2,画布 ratio 1312/864≈1.5185 ∈ [1.5, 1.9] → 出 .mov) */ +const PICK_COMPAT = "2026-09-20"; +/** 非兼容日:hero 2000×2000(1:1,画布 ratio 1.0 < 1.5 → 无 .mov,mac 静态回退) */ +const PICK_SQUARE = "2026-09-21"; + +// ============================================================================ +// 临时环境 + fixture +// ============================================================================ + +let tmpRoot = ""; +let sqlite: Database.Database; + +function ensureWallpaperVideoColumns(db: Database.Database): void { + const cols = (db.prepare("PRAGMA table_info(daily_picks)").all() as { name: string }[]).map( + (c) => c.name, + ); + if (!cols.includes("wallpaper_video_landscape_url")) { + db.exec("ALTER TABLE daily_picks ADD COLUMN wallpaper_video_landscape_url TEXT"); + } + if (!cols.includes("wallpaper_video_portrait_url")) { + db.exec("ALTER TABLE daily_picks ADD COLUMN wallpaper_video_portrait_url TEXT"); + } + // 契约 1:daily_picks 新增列 wallpaper_video_native_url TEXT NULL(红队 fixture 兜底: + // helper DDL 若未含此列则补齐;蓝队 schema 落地后此分支不再触发) + if (!cols.includes("wallpaper_video_native_url")) { + db.exec("ALTER TABLE daily_picks ADD COLUMN wallpaper_video_native_url TEXT"); + } +} + +/** 真实 JPEG fixture(D2:sharp 读 hero 原图 dims —— 照片文件必须可解码) */ +async function makeHeroJpeg(outPath: string, width: number, height: number): Promise { + await sharp({ create: { width, height, channels: 3, background: "#2288cc" } }) + .jpeg() + .toFile(outPath); +} + +/** 1 秒真实 H.264 mp4(spawn mock 产物 / 护栏注入样本) */ +function makeTinyMp4(outPath: string, size: string): void { + fs.mkdirSync(path.dirname(outPath), { recursive: true }); + const r = spawnSync( + "ffmpeg", + [ + "-y", + "-f", + "lavfi", + "-i", + `testsrc2=size=${size}:rate=12:duration=1`, + "-c:v", + "libx264", + "-pix_fmt", + "yuv420p", + "-an", + outPath, + ], + { encoding: "utf-8", timeout: 30000 }, + ); + if (r.status !== 0 || !fs.existsSync(outPath)) { + throw new Error(`fixture ffmpeg 造样片失败(${size}): ${r.stderr}`); + } +} + +function seedPick(pickDate: string, photoId: string): void { + sqlite + .prepare( + `INSERT INTO daily_picks + (id, photo_id, pick_date, title, narrative, score, composed_image_path, members, created_at, + wallpaper_video_landscape_url, wallpaper_video_portrait_url, wallpaper_video_native_url) + VALUES (?, ?, ?, '金色黄昏', '五年前的今天,你在海边捕捉到了这张温暖的照片。', 8.5, + ?, '[]', '2026-09-12T06:00:00.000Z', NULL, NULL, NULL)`, + ) + .run(`pick-${pickDate}`, photoId, pickDate, `daily-composed/${pickDate}.jpg`); +} + +function getPickRow(pickDate: string): { + wallpaper_video_native_url: string | null; + wallpaper_video_landscape_url: string | null; + wallpaper_video_portrait_url: string | null; +} { + return sqlite + .prepare( + `SELECT wallpaper_video_native_url, wallpaper_video_landscape_url, wallpaper_video_portrait_url + FROM daily_picks WHERE pick_date = ?`, + ) + .get(pickDate) as { + wallpaper_video_native_url: string | null; + wallpaper_video_landscape_url: string | null; + wallpaper_video_portrait_url: string | null; + }; +} + +/** 上传边界调用总次数(safeUploadFile 与 uploadFile 两条路径合计),并汇总全部 cosKey */ +function uploadCalls(): Array<{ localPath: string; cosKey: string }> { + const calls: Array<{ localPath: string; cosKey: string }> = []; + for (const c of [...mockSafeUpload.mock.calls, ...mockUploadFile.mock.calls]) { + calls.push({ localPath: String(c?.[0]), cosKey: String(c?.[1]) }); + } + return calls; +} + +function wireMocks(): void { + mockPreprocessHeroFrame.mockReset(); + mockSpawnHoneydoVideo.mockReset(); + mockBuildLoop.mockReset(); + mockRenderTextOverlay.mockReset(); + mockTranscodeForAerial.mockReset(); + mockTranscodeForGallery.mockReset(); + mockUploadFile.mockReset(); + mockSafeUpload.mockReset(); + mockSyncDayToGallery.mockReset(); + pipelineSeq.length = 0; + holder.galleryMode = "pass"; + holder.aerialMode = "pass"; + + mockPreprocessHeroFrame.mockImplementation(async () => { + pipelineSeq.push("preprocess"); + return holder.firstFramePath; + }); + // spawn:按 job 请求的画布 `${width}x${height}` 落对应尺寸的真实 mp4 产物(契约:产物在 outPath) + mockSpawnHoneydoVideo.mockImplementation( + async (opts: { + outPath?: string; + width?: number; + height?: number; + }) => { + pipelineSeq.push("spawn"); + const out = opts?.outPath ?? "/tmp/relight-none/out.mp4"; + const src = holder.productByCanvas.get(`${opts?.width}x${opts?.height}`); + if (!src) { + throw new Error( + `spawn mock 未登记画布 ${opts?.width}x${opts?.height}——job 未按 computeNativeCanvas 画布请求`, + ); + } + fs.mkdirSync(path.dirname(out), { recursive: true }); + fs.copyFileSync(src, out); + return { + outPath: out, + duration: 1, + stdout: `{"out":"${out}","duration":1,"res":"${opts?.width}x${opts?.height}"}`, + }; + }, + ); + mockBuildLoop.mockImplementation(async (src: string, _targetSeconds: number) => { + pipelineSeq.push("buildLoop"); + fs.mkdirSync(path.dirname(holder.loopPath), { recursive: true }); + fs.copyFileSync(src, holder.loopPath); + return { loopPath: holder.loopPath, segments: 2 }; + }); + mockRenderTextOverlay.mockImplementation(async (videoPath: string, _meta: unknown) => { + pipelineSeq.push("overlay"); + fs.mkdirSync(path.dirname(holder.overlaidPath), { recursive: true }); + fs.copyFileSync(videoPath, holder.overlaidPath); + return { overlaidPath: holder.overlaidPath }; + }); + mockTranscodeForGallery.mockImplementation((src: string, dst: string) => { + pipelineSeq.push("gallery"); + fs.mkdirSync(path.dirname(dst), { recursive: true }); + fs.copyFileSync(holder.galleryMode === "badsz" ? holder.galleryBadPath : src, dst); + }); + mockTranscodeForAerial.mockImplementation((src: string, dst: string) => { + pipelineSeq.push("aerial"); + fs.mkdirSync(path.dirname(dst), { recursive: true }); + fs.copyFileSync( + holder.aerialMode === "badsz" ? holder.aerialBadPath : holder.aerialOkPath, + dst, + ); + }); + mockUploadFile.mockImplementation(async (_localPath: string, cosKey: string) => + receiptFor(cosKey), + ); + mockSafeUpload.mockImplementation(async (_localPath: string, cosKey: string) => + receiptFor(cosKey), + ); + mockSyncDayToGallery.mockImplementation(async () => {}); +} + +// ============================================================================ +// beforeAll / beforeEach / afterAll +// ============================================================================ + +let runWallpaperVideo: (pickDate: string) => Promise; + +beforeAll(async () => { + const ff = spawnSync("ffmpeg", ["-version"], { encoding: "utf-8", timeout: 10000 }); + if (ff.status !== 0) { + throw new Error("ffmpeg 不可用——本验收要求真实 ffmpeg 环境"); + } + + tmpRoot = fs.mkdtempSync(path.join(os.homedir(), ".relight-test-wvnjob-")); + const dbPath = path.join(tmpRoot, "test.db"); + const storageRoot = path.join(tmpRoot, "storage"); + fs.mkdirSync(storageRoot, { recursive: true }); + + sqlite = new Database(dbPath); + sqlite.pragma("journal_mode = WAL"); + sqlite.pragma("foreign_keys = ON"); + setupTestSchema(sqlite); + ensureWallpaperVideoColumns(sqlite); + + sqlite + .prepare( + `INSERT INTO storage_sources (id, name, type, root_path, enabled) + VALUES ('src-wvn', '测试存储源', 'local', ?, 1)`, + ) + .run(storageRoot); + + // hero 照片:真实 JPEG(job 会 sharp 读原图 dims → computeNativeCanvas) + const heroCompat = path.join(tmpRoot, "hero-compat.jpg"); + const heroSquare = path.join(tmpRoot, "hero-square.jpg"); + await makeHeroJpeg(heroCompat, 3000, 2000); // 3:2 + await makeHeroJpeg(heroSquare, 2000, 2000); // 1:1 + for (const [pid, p] of [ + ["photo-compat", heroCompat], + ["photo-square", heroSquare], + ] as Array<[string, string]>) { + sqlite + .prepare( + `INSERT INTO photos (id, storage_source_id, file_path, file_hash, width, height, file_size, created_at) + VALUES (?, 'src-wvn', ?, ?, 3000, 2000, 1024, '2026-01-01T00:00:00.000Z')`, + ) + .run(pid, p, `hash-${pid}`); + } + seedPick(PICK_COMPAT, "photo-compat"); + seedPick(PICK_SQUARE, "photo-square"); + + holder.dbPath = dbPath; + holder.storageRoot = storageRoot; + const honeydoWhich = spawnSync("which", ["honeydo"], { encoding: "utf8" }); + holder.honeydoPath = honeydoWhich.status === 0 ? String(honeydoWhich.stdout).trim() : ""; + process.env.DATABASE_PATH = dbPath; + process.env.STORAGE_ROOT = storageRoot; + + // fixture 媒体:首帧 png(1×1 最小合法 PNG)+ 画布尺寸产物 + 护栏注入样本 + holder.firstFramePath = path.join(tmpRoot, "first-frame.png"); + fs.writeFileSync( + holder.firstFramePath, + Buffer.from( + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==", + "base64", + ), + ); + // 生成产物按画布登记(3:2 hero → 1312×864(D1 参考输出);1:1 hero → 1056×1056) + const compatProduct = path.join(tmpRoot, "product-1312x864.mp4"); + const squareProduct = path.join(tmpRoot, "product-1056x1056.mp4"); + makeTinyMp4(compatProduct, "1312x864"); + makeTinyMp4(squareProduct, "1056x1056"); + holder.productByCanvas.set("1312x864", compatProduct); + holder.productByCanvas.set("1056x1056", squareProduct); + // 护栏对照/注入样本(场景 5:1918×1080 注入 → 拒绝;1920×1080 对照 → 放行) + holder.aerialOkPath = path.join(tmpRoot, "ok-1920x1080.mp4"); + holder.aerialBadPath = path.join(tmpRoot, "bad-1918x1080.mp4"); + holder.galleryBadPath = path.join(tmpRoot, "bad-1310x864.mp4"); + makeTinyMp4(holder.aerialOkPath, "1920x1080"); + makeTinyMp4(holder.aerialBadPath, "1918x1080"); + makeTinyMp4(holder.galleryBadPath, "1310x864"); + holder.loopPath = path.join(tmpRoot, "loop-master.mp4"); + holder.overlaidPath = path.join(tmpRoot, "overlaid.mp4"); + holder.videoWorkspacePath = path.join(tmpRoot, "video-workspace"); + + // Remotion 运行时脚手架(job 前置 fs access 校验;渲染本身被 mock—— + // 惯例同 wallpaper-video-job.acceptance.test.ts) + const ws = holder.videoWorkspacePath; + for (const rel of [ + path.join("wallpaper-overlay", "package.json"), + path.join("wallpaper-overlay", "src", "index.ts"), + path.join("wallpaper-overlay", "public", "fonts", "NotoSerifSC-Regular.otf"), + path.join("wallpaper-overlay", "public", "fonts", "NotoSerifSC-Bold.otf"), + path.join("wallpaper-overlay", "public", "fonts", "Fraunces-Regular.otf"), + path.join("wallpaper-overlay", "public", "fonts", "Fraunces-Bold.otf"), + path.join( + "node_modules", + ".remotion", + "chrome-headless-shell", + "mac-arm64", + "chrome-headless-shell-mac-arm64", + "chrome-headless-shell", + ), + ]) { + const p = path.join(ws, rel); + fs.mkdirSync(path.dirname(p), { recursive: true }); + fs.writeFileSync(p, "// mock workspace scaffold\n"); + } + const binDir = path.join(ws, "node_modules", ".bin"); + const remotionBin = path.join(binDir, "remotion"); + fs.mkdirSync(binDir, { recursive: true }); + fs.writeFileSync(remotionBin, "#!/bin/sh\n"); + fs.chmodSync(remotionBin, 0o755); + + wireMocks(); + + const mod = (await import("../jobs/wallpaper-video")) as { + runWallpaperVideo: (pickDate: string) => Promise; + }; + runWallpaperVideo = mod.runWallpaperVideo; +}, 60000); + +beforeEach(() => { + holder.enabled = true; + holder.receiptMode = "url"; + holder.seconds = 4; + holder.loopSeconds = 8; + wireMocks(); +}); + +afterAll(() => { + try { + sqlite?.close(); + } catch { + // ignore + } + if (tmpRoot) fs.rmSync(tmpRoot, { recursive: true, force: true }); +}); + +// ============================================================================ +// 场景 1.P4 + 4.P1/P4:非兼容日(1:1 hero)——单腿原生,无 .mov +// ============================================================================ + +dJob("场景 1.P4/4.P1/4.P4:非兼容日(画布 ratio<1.5)单腿原生——无 .mov、回执仅 native", () => { + it("恰 1 次 spawn(kill 双腿 No-op)+ transcodeForAerial 零调用 + 仅 native key 上传", async () => { + await runWallpaperVideo(PICK_SQUARE); + + // 场景 1.P1 的 job 级代码化:单腿 = 全程恰 1 次 honeydo spawn + expect(mockSpawnHoneydoVideo).toHaveBeenCalledTimes(1); + // 场景 4.P1 的 job 级代码化:非兼容日零 Aerial 转码(产物目录无 1920×1080 .mov 的因) + expect(mockTranscodeForAerial).toHaveBeenCalledTimes(0); + expect(mockTranscodeForGallery).toHaveBeenCalledTimes(1); + + const uploads = uploadCalls(); + expect(uploads.length, "非兼容日仅 1 次(native)上传").toBe(1); + expect(uploads[0]?.cosKey).toBe(nativeKey(PICK_SQUARE)); + expect( + uploads.some((u) => u.cosKey.includes("_landscape.mov")), + "非兼容日不得上传 _landscape.mov", + ).toBe(false); + expect( + uploads.some((u) => u.cosKey.includes("_portrait.mp4")), + "legacy 竖腿已删除——不得再上传 _portrait.mp4(契约 2)", + ).toBe(false); + }); + + it("画布跟随原图:preprocess 按 computeNativeCanvas(2000,2000) 画布、spawn 显式携带 W/H(契约 6)", async () => { + const { computeNativeCanvas } = await import("../lib/wallpaper/native-canvas"); + await runWallpaperVideo(PICK_SQUARE); + + const canvas = computeNativeCanvas(2000, 2000); + // 场景 1.P3 字面量(画布像素预算窗口) + expect(canvas.width * canvas.height).toBeLessThanOrEqual(1_177_600); + expect(canvas.width * canvas.height).toBeGreaterThanOrEqual(1_177_600 / 2); + + // preprocess 被以画布 W/H 调用(第 2/3 参) + const pp = mockPreprocessHeroFrame.mock.calls[0] as unknown[]; + expect(`${pp[1]}x${pp[2]}`, "preprocess 画布必须 == computeNativeCanvas(2000,2000)").toBe( + `${canvas.width}x${canvas.height}`, + ); + // 契约 6:spawn argv --width W --height H(spawnHoneydoVideo opts 两轴显式覆盖) + const sp = mockSpawnHoneydoVideo.mock.calls[0]?.[0] as { width?: number; height?: number }; + expect(sp.width, "spawn.opts.width 必须 == 画布宽").toBe(canvas.width); + expect(sp.height, "spawn.opts.height 必须 == 画布高").toBe(canvas.height); + }); + + it("场景 1.P4 [det-machine]:DB 行 native != 空 && landscape == 空 && portrait == 空", async () => { + await runWallpaperVideo(PICK_SQUARE); + + const row = getPickRow(PICK_SQUARE); + expect(row.wallpaper_video_native_url, "native 回执列必须非空(回执 URL 逐字)").toBe( + receiptFor(nativeKey(PICK_SQUARE)), + ); + expect(row.wallpaper_video_landscape_url, "非兼容日 landscape 列必须为空").toBeNull(); + expect(row.wallpaper_video_portrait_url, "portrait 列必须为空(legacy 不再新写)").toBeNull(); + expect(mockSyncDayToGallery).toHaveBeenCalledTimes(1); + expect(mockSyncDayToGallery.mock.calls[0]?.[0]).toBe(PICK_SQUARE); + }); +}); + +// ============================================================================ +// 场景 3.P3(DB 侧)+ 场景 7 DB 共存:兼容日(3:2 hero,ratio ∈ [1.5,1.9]) +// ============================================================================ + +dJob("场景 3.P3 DB 侧 / 7:兼容日——native 与 landscape 并存、portrait 恒空", () => { + it("transcodeForAerial 恰 1 次(输入=文字成品)+ native/_landscape.mov 双 key 上传", async () => { + await runWallpaperVideo(PICK_COMPAT); + + expect(mockSpawnHoneydoVideo).toHaveBeenCalledTimes(1); + expect(mockTranscodeForGallery).toHaveBeenCalledTimes(1); + expect( + mockTranscodeForAerial, + "兼容日(3:2 画布 ratio≈1.52 ∈ [1.5,1.9])必须产出 Aerial", + ).toHaveBeenCalledTimes(1); + // v2 契约沿承:transcodeForAerial 输入 = Remotion 合成后的最终成品 + expect(mockTranscodeForAerial.mock.calls[0]?.[0]).toBe(holder.overlaidPath); + + const uploads = uploadCalls(); + const keys = uploads.map((u) => u.cosKey); + expect(keys, "兼容日恰 2 次上传(native + _landscape.mov)").toHaveLength(2); + expect(keys).toContain(nativeKey(PICK_COMPAT)); + expect(keys).toContain(landscapeKey(PICK_COMPAT)); + }); + + it("护栏通过后 DB:native != 空 且 wallpaper_video_landscape_url != 空(场景 3.P3 前置)且 portrait 空", async () => { + await runWallpaperVideo(PICK_COMPAT); + + const row = getPickRow(PICK_COMPAT); + expect(row.wallpaper_video_native_url).toBe(receiptFor(nativeKey(PICK_COMPAT))); + expect( + row.wallpaper_video_landscape_url, + "场景 3.P3:护栏通过后 landscape 列必须非空(真实 COS HEAD 落 realprocess 文件)", + ).toBe(receiptFor(landscapeKey(PICK_COMPAT))); + expect(row.wallpaper_video_portrait_url).toBeNull(); + }); + + it("D2 画布联动:preprocess 画布 == computeNativeCanvas(3000,2000)(=1312×864,D1 参考输出)", async () => { + const { computeNativeCanvas } = await import("../lib/wallpaper/native-canvas"); + await runWallpaperVideo(PICK_COMPAT); + + const canvas = computeNativeCanvas(3000, 2000); + expect(`${canvas.width}x${canvas.height}`).toBe("1312x864"); // D1 参考输出锁 + const pp = mockPreprocessHeroFrame.mock.calls[0] as unknown[]; + expect(`${pp[1]}x${pp[2]}`).toBe("1312x864"); + const sp = mockSpawnHoneydoVideo.mock.calls[0]?.[0] as { width?: number; height?: number }; + expect(sp.width).toBe(1312); + expect(sp.height).toBe(864); + }); +}); + +// ============================================================================ +// 场景 5:尺寸护栏拦截错尺寸产物(注入 / 日志 / 对照) +// ============================================================================ + +dJob("场景 5:尺寸护栏——错尺寸产物阻断(回执列空 + 日志显式),正确尺寸放行", () => { + // 隔离:场景 3.P3 兼容日成功路径已为 PICK_COMPAT 写列,场景 5 断言「两列空」前必须先清列 + beforeEach(() => { + sqlite + .prepare( + "UPDATE daily_picks SET wallpaper_video_native_url = NULL, wallpaper_video_landscape_url = NULL, wallpaper_video_portrait_url = NULL", + ) + .run(); + }); + + it("场景 5.P1:注入 1918×1080 Aerial 产物 → 护栏阻断:job 失败(reject)且回执列空", async () => { + holder.aerialMode = "badsz"; + const consoleSpy: string[] = []; + const e = vi.spyOn(console, "error").mockImplementation((...a: unknown[]) => { + consoleSpy.push(a.map(String).join(" ")); + }); + const w = vi.spyOn(console, "warn").mockImplementation((...a: unknown[]) => { + consoleSpy.push(a.map(String).join(" ")); + }); + + let rejected = false; + try { + await runWallpaperVideo(PICK_COMPAT); + } catch { + rejected = true; + } + e.mockRestore(); + w.mockRestore(); + + // P1 字面量「exit != 0」的 job 级前提:护栏 throw 必须传播(被旁路吞掉则 CLI exit==0) + expect(rejected, "尺寸护栏必须使 job 失败(rerun CLI exit != 0 的前提)").toBe(true); + // 回执列空(契约 8:任一环节失败 → 两列均空) + const row = getPickRow(PICK_COMPAT); + expect(row.wallpaper_video_native_url).toBeNull(); + expect(row.wallpaper_video_landscape_url).toBeNull(); + expect(row.wallpaper_video_portrait_url).toBeNull(); + }); + + it("场景 5.P1(native 腿):注入 1310×864 画廊产物(≠画布 1312×864)→ 阻断 + 两列空", async () => { + holder.galleryMode = "badsz"; + let rejected = false; + try { + await runWallpaperVideo(PICK_COMPAT); + } catch { + rejected = true; + } + expect(rejected).toBe(true); + const row = getPickRow(PICK_COMPAT); + expect(row.wallpaper_video_native_url).toBeNull(); + expect(row.wallpaper_video_landscape_url).toBeNull(); + }); + + it("场景 5.P2 [det-machine]:失败日志显式含尺寸断言语义(输出含注入错误宽度 1918,禁静默失败)", async () => { + holder.aerialMode = "badsz"; + const consoleSpy: string[] = []; + const e = vi.spyOn(console, "error").mockImplementation((...a: unknown[]) => { + consoleSpy.push(a.map(String).join(" ")); + }); + const w = vi.spyOn(console, "warn").mockImplementation((...a: unknown[]) => { + consoleSpy.push(a.map(String).join(" ")); + }); + // 实现的失败日志经 log 回调(默认 console.log)输出,须一并捕获 + const l = vi.spyOn(console, "log").mockImplementation((...a: unknown[]) => { + consoleSpy.push(a.map(String).join(" ")); + }); + try { + await runWallpaperVideo(PICK_COMPAT); + } catch { + // 预期 reject(5.P1 已断言);此处只采集日志 + } + e.mockRestore(); + w.mockRestore(); + l.mockRestore(); + + const log = consoleSpy.join("\n"); + // CONTRACT_AMBIGUOUS(措辞不锁定):日志必须能定位「什么尺寸不匹配」→ 含注入错误宽度 + expect( + log, + `失败日志必须显式含尺寸断言语义(含注入宽度 1918),实际日志:\n${log.slice(0, 2000)}`, + ).toContain("1918"); + }); + + it("场景 5.P3 [det-machine]:对照组 1920×1080 → 护栏放行、landscape 回执写库(kill 永远拒绝 No-op)", async () => { + holder.aerialMode = "pass"; // mock 落 1920×1080 正确尺寸 + await runWallpaperVideo(PICK_COMPAT); + + const row = getPickRow(PICK_COMPAT); + expect(row.wallpaper_video_landscape_url, "对照组正确尺寸必须放行并写回执").toBe( + receiptFor(landscapeKey(PICK_COMPAT)), + ); + expect(row.wallpaper_video_native_url).toBe(receiptFor(nativeKey(PICK_COMPAT))); + }); +}); diff --git a/apps/backend/src/__tests__/wallpaper-video-native-manifest.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-native-manifest.acceptance.test.ts new file mode 100644 index 0000000..db78750 --- /dev/null +++ b/apps/backend/src/__tests__/wallpaper-video-native-manifest.acceptance.test.ts @@ -0,0 +1,325 @@ +/** + * 验收测试(红队):单腿原生壁纸视频 — manifest 契约(wallpaperVideoNative 条件展开 / 优先级) + * + * 设计文档(state.md §契约规约 3 / §验收场景)对应谓词: + * - 场景 6.P2 [det-machine]:manifest 历史日(legacy-only)landscape/portrait 字段照常展开非空 + * - 场景 6.P5 [det-machine]:native 列为空的日 → manifest 无 wallpaperVideoNative 字段 + * (条件展开缺省分支,与既有两字段同语义:in === false,非 null 非空串) + * - 场景 7.P2 [det-machine]:兼容日(native 与 legacy 并存)manifest 该日视频字段取 + * native 值优先——wallpaperVideoNative 展开为 native URL(非 legacy URL) + * - 契约 3:manifest 新增可选字段 wallpaperVideoNative(native 列非空才展开); + * wallpaperVideoLandscape/Portrait 字段与语义不变;历史兼容(D4):存量行不动 + * + * 惯例沿 wallpaper-video-manifest.acceptance.test.ts:真实 SQLite(better-sqlite3 临时文件) + * + mock ../lib/config(databasePath 用 getter 动态读 env)+ mock cos-nodejs-sdk-v5; + * buildManifest 直接对 DB 列求值——fixture 直接种回执列值,断言 JSON 字段形态。 + * 红队铁律:不读 lib/gallery/manifest.ts 蓝队新改动;不 skip、硬断言。 + */ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import Database from "better-sqlite3"; +import { afterAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { setupTestSchema } from "./helpers/test-schema"; + +// ============================================================================ +// Mock:cos-nodejs-sdk-v5(buildManifest 不应调 COS,URL 用回执值本地拼) +// ============================================================================ + +const mockCosPutObject = vi.hoisted(() => vi.fn(async () => ({}))); +const mockCosSliceUploadFile = vi.hoisted(() => vi.fn(async () => ({}))); + +vi.mock("cos-nodejs-sdk-v5", () => { + const S3 = vi.fn(() => ({ + putObject: mockCosPutObject, + sliceUploadFile: mockCosSliceUploadFile, + getObjectUrl: vi.fn(), + })); + return { default: S3 }; +}); + +const TEST_COS = vi.hoisted(() => ({ + bucket: "little-bee-assets-1324334992", + region: "ap-shanghai", + prefix: "relight", +})); + +vi.mock("../lib/config", () => ({ + config: { + get port() { + return 3000; + }, + get storageRoot() { + return process.env.STORAGE_ROOT ?? "/tmp/test-storage"; + }, + get databasePath() { + return process.env.DATABASE_PATH ?? "/tmp/test.db"; + }, + cos: { + secretId: "test-id", + secretKey: "test-key", + bucket: TEST_COS.bucket, + region: TEST_COS.region, + prefix: TEST_COS.prefix, + }, + galleryPublicUrl: "https://gallery.stringzhao.life", + gallery: { + vpsHost: "127.0.0.1", + vpsUser: "test", + vpsKey: "/tmp/test-key", + vpsPath: "/tmp/gallery", + }, + }, +})); + +import { buildManifest } from "../lib/gallery/manifest"; + +// ============================================================================ +// 契约字面量(§契约规约 2 逐字 key → 回执 URL 形态) +// ============================================================================ + +const PICK_NATIVE_ONLY = "2026-09-20"; // 新 era:仅 native 列非空 +const PICK_LEGACY_ONLY = "2026-09-10"; // 历史 legacy:landscape/portrait 非空、native NULL +const PICK_COMPAT = "2026-09-19"; // 兼容日:native + landscape 并存 + +const NATIVE_URL = (d: string) => + `https://${TEST_COS.bucket}.cos.${TEST_COS.region}.myqcloud.com/${TEST_COS.prefix}/wallpaper-videos/${d}_native.mp4`; +const LANDSCAPE_URL = (d: string) => + `https://${TEST_COS.bucket}.cos.${TEST_COS.region}.myqcloud.com/${TEST_COS.prefix}/wallpaper-videos/${d}_landscape.mov`; +const PORTRAIT_URL = (d: string) => + `https://${TEST_COS.bucket}.cos.${TEST_COS.region}.myqcloud.com/${TEST_COS.prefix}/wallpaper-videos/${d}_portrait.mp4`; + +// ============================================================================ +// 临时 DB fixture +// ============================================================================ + +const activeEnvs: string[] = []; + +function createTestEnv(): { tmpRoot: string; dbPath: string } { + const tmpRoot = fs.mkdtempSync(path.join(os.homedir(), ".relight-test-wvnmanifest-")); + const dbPath = path.join(tmpRoot, "test.db"); + const sqlite = new Database(dbPath); + sqlite.pragma("journal_mode = WAL"); + sqlite.pragma("foreign_keys = ON"); + setupTestSchema(sqlite); + ensureWallpaperVideoColumns(sqlite); + sqlite + .prepare( + `INSERT INTO storage_sources (id, name, type, root_path, enabled) + VALUES ('src-test', '测试存储源', 'local', '${tmpRoot}/storage', 1)`, + ) + .run(); + sqlite.close(); + activeEnvs.push(tmpRoot); + return { tmpRoot, dbPath }; +} + +/** 红队 fixture 兜底:契约 1 声明的 native 列(含既有两列)若不在 helper DDL 中则补齐 */ +function ensureWallpaperVideoColumns(sqlite: Database.Database): void { + const cols = (sqlite.prepare("PRAGMA table_info(daily_picks)").all() as { name: string }[]).map( + (c) => c.name, + ); + if (!cols.includes("wallpaper_video_landscape_url")) { + sqlite.exec("ALTER TABLE daily_picks ADD COLUMN wallpaper_video_landscape_url TEXT"); + } + if (!cols.includes("wallpaper_video_portrait_url")) { + sqlite.exec("ALTER TABLE daily_picks ADD COLUMN wallpaper_video_portrait_url TEXT"); + } + if (!cols.includes("wallpaper_video_native_url")) { + sqlite.exec("ALTER TABLE daily_picks ADD COLUMN wallpaper_video_native_url TEXT"); + } +} + +interface SeedPick { + pickDate: string; + nativeUrl: string | null; + landscapeUrl: string | null; + portraitUrl: string | null; +} + +function seedDailyPick(sqlite: Database.Database, p: SeedPick): void { + sqlite + .prepare( + `INSERT INTO photos (id, storage_source_id, file_path, file_hash, width, height, file_size, created_at) + VALUES (?, 'src-test', ?, ?, 4000, 3000, 1024, '2026-01-01T00:00:00.000Z')`, + ) + .run(`photo-${p.pickDate}`, `${p.pickDate}.jpg`, `hash-${p.pickDate}`); + sqlite + .prepare( + `INSERT INTO daily_picks + (id, photo_id, pick_date, title, narrative, score, composed_image_path, members, created_at, + wallpaper_video_native_url, wallpaper_video_landscape_url, wallpaper_video_portrait_url) + VALUES (?, ?, ?, ?, ?, 8.5, ?, '[]', '2026-09-12T06:00:00.000Z', ?, ?, ?)`, + ) + .run( + `pick-${p.pickDate}`, + `photo-${p.pickDate}`, + p.pickDate, + "金色黄昏", + "五年前的今天,你在海边捕捉到了这张温暖的照片。夕阳将天空染成金橙色,海浪轻抚沙滩。", + `daily-composed/${p.pickDate}.jpg`, + p.nativeUrl, + p.landscapeUrl, + p.portraitUrl, + ); +} + +function openDb(dbPath: string): Database.Database { + const sqlite = new Database(dbPath); + sqlite.pragma("foreign_keys = ON"); + return sqlite; +} + +afterAll(() => { + while (activeEnvs.length > 0) { + const dir = activeEnvs.pop(); + if (dir) fs.rmSync(dir, { recursive: true, force: true }); + } +}); + +interface ManifestDayShape { + pickDate: string; + wallpaperLandscape: string; + wallpaperPortrait: string; + wallpaperVideoNative?: string; + wallpaperVideoLandscape?: string; + wallpaperVideoPortrait?: string; +} + +function findDay(days: ManifestDayShape[], pickDate: string): ManifestDayShape { + const day = days.find((d) => d.pickDate === pickDate); + expect(day, `manifest.days 应含 pickDate=${pickDate}`).toBeTruthy(); + return day as ManifestDayShape; +} + +async function buildManifestDays(): Promise { + const manifest = (await buildManifest()) as unknown as { days: ManifestDayShape[] }; + return manifest.days; +} + +// ============================================================================ +// 测试 +// ============================================================================ + +describe("manifest wallpaperVideoNative 条件展开(契约 3 / 场景 6.P5 / 7.P2)", () => { + beforeEach(() => { + mockCosPutObject.mockClear(); + mockCosSliceUploadFile.mockClear(); + }); + + it("场景 6.P5 [det-machine]:native 列为空的日 → manifest 无 wallpaperVideoNative 字段(缺省分支)", async () => { + const env = createTestEnv(); + const sqlite = openDb(env.dbPath); + seedDailyPick(sqlite, { + pickDate: PICK_NATIVE_ONLY, + nativeUrl: NATIVE_URL(PICK_NATIVE_ONLY), + landscapeUrl: null, + portraitUrl: null, + }); + // 同库放一个「无视频日」验证 native 缺省与其他列缺省同型 + seedDailyPick(sqlite, { + pickDate: "2026-09-18", + nativeUrl: null, + landscapeUrl: null, + portraitUrl: null, + }); + sqlite.close(); + process.env.DATABASE_PATH = env.dbPath; + + const days = await buildManifestDays(); + const nativeDay = findDay(days, PICK_NATIVE_ONLY); + // native-only 日:native 字段展开,legacy 两字段缺省 + expect("wallpaperVideoNative" in nativeDay).toBe(true); + expect(nativeDay.wallpaperVideoNative).toBe(NATIVE_URL(PICK_NATIVE_ONLY)); + expect("wallpaperVideoLandscape" in nativeDay).toBe(false); + expect("wallpaperVideoPortrait" in nativeDay).toBe(false); + + const noVideoDay = findDay(days, "2026-09-18"); + expect("wallpaperVideoNative" in noVideoDay).toBe(false); + expect("wallpaperVideoLandscape" in noVideoDay).toBe(false); + expect("wallpaperVideoPortrait" in noVideoDay).toBe(false); + }); + + it("场景 6.P2 [det-machine]:历史 legacy-only 日 → landscape/portrait 字段照常展开非空(历史兼容,D4)", async () => { + const env = createTestEnv(); + const sqlite = openDb(env.dbPath); + seedDailyPick(sqlite, { + pickDate: PICK_LEGACY_ONLY, + nativeUrl: null, + landscapeUrl: LANDSCAPE_URL(PICK_LEGACY_ONLY), + portraitUrl: PORTRAIT_URL(PICK_LEGACY_ONLY), + }); + sqlite.close(); + process.env.DATABASE_PATH = env.dbPath; + + const days = await buildManifestDays(); + const day = findDay(days, PICK_LEGACY_ONLY); + + expect("wallpaperVideoLandscape" in day).toBe(true); + expect("wallpaperVideoPortrait" in day).toBe(true); + expect(day.wallpaperVideoLandscape).toBe(LANDSCAPE_URL(PICK_LEGACY_ONLY)); + expect(day.wallpaperVideoPortrait).toBe(PORTRAIT_URL(PICK_LEGACY_ONLY)); + expect(day.wallpaperVideoLandscape?.endsWith("_landscape.mov")).toBe(true); + expect(day.wallpaperVideoPortrait?.endsWith("_portrait.mp4")).toBe(true); + // native 列为空 → 无 native 字段(场景 6.P5 同源断言) + expect("wallpaperVideoNative" in day).toBe(false); + }); + + it("场景 7.P2 [det-machine]:兼容日(native+landscape 并存)→ wallpaperVideoNative 取 native URL(非 legacy)", async () => { + const env = createTestEnv(); + const sqlite = openDb(env.dbPath); + seedDailyPick(sqlite, { + pickDate: PICK_COMPAT, + nativeUrl: NATIVE_URL(PICK_COMPAT), + landscapeUrl: LANDSCAPE_URL(PICK_COMPAT), + portraitUrl: null, + }); + sqlite.close(); + process.env.DATABASE_PATH = env.dbPath; + + const days = await buildManifestDays(); + const day = findDay(days, PICK_COMPAT); + + // native 字段展开且值为 native URL(防把 legacy URL 塞进 native 字段) + expect("wallpaperVideoNative" in day).toBe(true); + expect(day.wallpaperVideoNative).toBe(NATIVE_URL(PICK_COMPAT)); + expect(day.wallpaperVideoNative?.endsWith("_native.mp4")).toBe(true); + // legacy 字段语义不变:landscape 照常展开 + expect(day.wallpaperVideoLandscape).toBe(LANDSCAPE_URL(PICK_COMPAT)); + }); + + it("JSON 序列化后字段缺省语义保持(推送到 VPS 的 manifest.json 键不存在,非空串非 null)", async () => { + const env = createTestEnv(); + const sqlite = openDb(env.dbPath); + seedDailyPick(sqlite, { + pickDate: PICK_NATIVE_ONLY, + nativeUrl: NATIVE_URL(PICK_NATIVE_ONLY), + landscapeUrl: null, + portraitUrl: null, + }); + seedDailyPick(sqlite, { + pickDate: PICK_LEGACY_ONLY, + nativeUrl: null, + landscapeUrl: LANDSCAPE_URL(PICK_LEGACY_ONLY), + portraitUrl: PORTRAIT_URL(PICK_LEGACY_ONLY), + }); + sqlite.close(); + process.env.DATABASE_PATH = env.dbPath; + + const manifest = { days: await buildManifestDays() }; + const json = JSON.parse(JSON.stringify(manifest)) as { days: ManifestDayShape[] }; + + const nativeDay = findDay(json.days, PICK_NATIVE_ONLY); + expect( + Object.prototype.hasOwnProperty.call(nativeDay, "wallpaperVideoNative"), + "native-only 日 JSON 必须含 wallpaperVideoNative 键", + ).toBe(true); + expect(Object.prototype.hasOwnProperty.call(nativeDay, "wallpaperVideoLandscape")).toBe(false); + + const legacyDay = findDay(json.days, PICK_LEGACY_ONLY); + expect( + Object.prototype.hasOwnProperty.call(legacyDay, "wallpaperVideoNative"), + "legacy-only 日 JSON 不得含 wallpaperVideoNative 键(undefined 不序列化)", + ).toBe(false); + }); +}); diff --git a/apps/backend/src/__tests__/wallpaper-video-native-overlay-realprocess.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-native-overlay-realprocess.acceptance.test.ts new file mode 100644 index 0000000..fe15e9e --- /dev/null +++ b/apps/backend/src/__tests__/wallpaper-video-native-overlay-realprocess.acceptance.test.ts @@ -0,0 +1,158 @@ +/** + * 验收测试(红队,real-process,capability-gate):renderTextOverlay 画布参数化真渲染 + * + * 设计文档(state.md §设计文档 D3 / §验收场景)对应谓词: + * - 场景 9.P1 [real-process]:以非历史默认画布(4:3 档 1248×928)触发 `npx remotion render` + * → 产物尺寸 == 参数化 W×H(kill 尺寸写死 No-op:若实现仍写死 1280×704/736×1600, + * 产物不会是 1248×928 → 红) + * + * 设计契约(D3 逐字):Root.tsx 两个 comp 增加 calculateMetadata,从新增 props + * canvasWidth/canvasHeight 返回动态 width/height(Remotion v4 支持); + * WallpaperOverlay.tsx 布局分支统一按 props 比例判定(≥0.9 两栏横版 / <0.9 竖版全屏)。 + * + * 环境前提(惯例沿 wallpaper-video-overlay-realprocess.acceptance.test.ts): + * {config.videoWorkspacePath}/wallpaper-overlay 工程存在——CI 必然缺失 → capability-gate + * skip + warn,本机工作区就绪即自动真跑(真实 npx remotion render,无 GPU 依赖; + * 首跑 bundling 冷启动可达 600s 级)。 + * 输入:4:3 档画布 1248×928(D1 参考输出,非历史默认 1280×704/736×1600)× 2s 母版。 + * 红队铁律:不读蓝队实现代码;renderTextOverlay 按契约函数名黑盒 import;不宽容跳过。 + */ +import { spawnSync } from "node:child_process"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { config } from "../lib/config"; + +const ARTIFACT_DIR = "/tmp/autopilot-artifacts"; + +function writeArtifact(id: string, content: string): void { + fs.mkdirSync(ARTIFACT_DIR, { recursive: true }); + fs.writeFileSync(path.join(ARTIFACT_DIR, `${id}.out`), content); +} + +/** §设计文档 D3 逐字:videoWorkspacePath 下 wallpaper-overlay 工程 */ +const OVERLAY_WORKSPACE = path.join(config.videoWorkspacePath, "wallpaper-overlay"); + +// [capability-gate 惯例]:overlay 工程是本机 runtime 工作区产物(不入库)—— +// 缺失时 skip + warn,本机存在即自动真跑(同 wallpaper-video-overlay-realprocess 决策留痕)。 +const OVERLAY_WS_AVAILABLE = fs.existsSync(OVERLAY_WORKSPACE); +if (!OVERLAY_WS_AVAILABLE) { + console.warn( + "[wallpaper-video-native-overlay-realprocess] Remotion 文字层工程不在本机——real-process 用例 skip(工作区就绪时自动真跑)", + ); +} +const dOverlay = OVERLAY_WS_AVAILABLE ? describe : describe.skip; + +let tmpRoot = ""; +let inputVideo = ""; +let renderTextOverlay: ( + videoPath: string, + meta: { + pickDate: string; + title: string; + narrative: string; + captureDateline?: string; + canvasWidth?: number; + canvasHeight?: number; + }, +) => Promise<{ overlaidPath: string }>; + +interface ProbeResult { + width: number | null; + height: number | null; + formatName: string; +} + +function probeVideo(file: string): ProbeResult { + const r = spawnSync( + "ffprobe", + ["-v", "error", "-show_format", "-show_streams", "-of", "json", file], + { encoding: "utf-8", timeout: 30000, maxBuffer: 16 * 1024 * 1024 }, + ); + if (r.status !== 0 || !r.stdout) { + throw new Error(`ffprobe 失败(${file}): ${r.stderr}`); + } + const j = JSON.parse(r.stdout) as { + format?: { format_name?: string }; + streams?: Array<{ codec_type?: string; width?: number; height?: number }>; + }; + const v = (j.streams ?? []).find((s) => s.codec_type === "video"); + return { + width: v?.width ?? null, + height: v?.height ?? null, + formatName: j.format?.format_name ?? "", + }; +} + +beforeAll(async () => { + for (const bin of ["ffmpeg", "ffprobe"]) { + const p = spawnSync(bin, ["-version"], { encoding: "utf-8", timeout: 15000 }); + if (p.status !== 0) { + throw new Error(`${bin} 不可用——本验收要求真实环境`); + } + } + + tmpRoot = fs.mkdtempSync(path.join(os.homedir(), ".relight-test-wvnosreal-")); + + // 4:3 档画布母版 1248×928(D1 参考输出;非历史默认 1280×704 / 736×1600)× 2s + inputVideo = path.join(tmpRoot, "input-1248x928.mp4"); + const r = spawnSync( + "ffmpeg", + [ + "-y", + "-f", + "lavfi", + "-i", + "testsrc2=size=1248x928:rate=24:duration=2", + "-c:v", + "libx264", + "-pix_fmt", + "yuv420p", + "-an", + inputVideo, + ], + { encoding: "utf-8", timeout: 60000 }, + ); + if (r.status !== 0 || !fs.existsSync(inputVideo)) { + throw new Error(`fixture ffmpeg 造样片失败: ${r.stderr}`); + } + + const mod = (await import("../lib/wallpaper/video")) as unknown as Record; + expect( + typeof mod.renderTextOverlay, + "契约函数 renderTextOverlay 未由 lib/wallpaper/video 导出", + ).toBe("function"); + renderTextOverlay = mod.renderTextOverlay as typeof renderTextOverlay; +}, 60000); + +afterAll(() => { + if (tmpRoot) fs.rmSync(tmpRoot, { recursive: true, force: true }); +}); + +dOverlay("场景 9.P1 [real-process]:4:3 档画布(1248×928)真渲染 → 产物尺寸 == 参数化 W×H", () => { + it("renderTextOverlay(canvasWidth=1248, canvasHeight=928) → 产物 1248×928 mp4(尺寸写死即红)", async () => { + const res = await renderTextOverlay(inputVideo, { + pickDate: "2026-09-20", + title: "金色黄昏", + narrative: "五年前的今天,你在海边捕捉到了这张温暖的照片。夕阳将天空染成金橙色。", + captureDateline: "拍摄于 2021-05-05 18:30 · 5 年前", + canvasWidth: 1248, + canvasHeight: 928, + }); + + expect(res.overlaidPath, "必须产出 overlaidPath").toBeTruthy(); + expect(fs.existsSync(res.overlaidPath), `产物不存在: ${res.overlaidPath}`).toBe(true); + + const meta = probeVideo(res.overlaidPath); + writeArtifact("s9p1", JSON.stringify({ overlaidPath: res.overlaidPath, ...meta }, null, 2)); + + // 谓词字面量:产物尺寸 == 参数化 W×H + expect(meta.width, `产物宽必须 1248(参数化画布),实际 ${meta.width}`).toBe(1248); + expect(meta.height, `产物高必须 928(参数化画布),实际 ${meta.height}`).toBe(928); + // 历史默认尺寸写死的直杀断言 + expect(`${meta.width}x${meta.height}`).not.toBe("1280x704"); + expect(`${meta.width}x${meta.height}`).not.toBe("736x1600"); + expect(meta.formatName).toContain("mp4"); + }, 900000); +}); diff --git a/apps/backend/src/__tests__/wallpaper-video-native-overlay-spawn.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-native-overlay-spawn.acceptance.test.ts new file mode 100644 index 0000000..7368922 --- /dev/null +++ b/apps/backend/src/__tests__/wallpaper-video-native-overlay-spawn.acceptance.test.ts @@ -0,0 +1,334 @@ +import { spawnSync } from "node:child_process"; +/** + * 验收测试(红队):单腿原生壁纸视频 — renderTextOverlay 画布参数化(mock spawn 面) + * + * 设计文档(state.md §设计文档 D2/D3 / §验收场景)对应谓词: + * - 场景 9.P2 [det-machine]:spawn 命令行显式携带画布宽高参数(kill 尺寸写死 No-op)。 + * 契约 7:Overlay props = {videoPath, pickDate, title, narrative, captureDateline, + * canvasWidth, canvasHeight}(既有字段不动,纯超集扩展);D3:Root.tsx 两个 comp 增加 + * calculateMetadata,从新增 props canvasWidth/canvasHeight 返回动态 width/height。 + * 断言:renderTextOverlay 调起 remotion render 时,其命令行(argv 直含,或 argv 引用的 + * props 载荷文件内容)必须同时含 canvasWidth/canvasHeight 键名与本次画布数值。 + * - D2:comp 选择按 canvasWidth/canvasHeight 比例(≥0.9 横版两栏 comp,<0.9 竖版全屏 + * comp)——横竖两种画布各渲染一次,载荷必须随画布变化(数值不同),证明参数化通路贯通。 + * // CONTRACT_AMBIGUOUS: comp id 命名未在契约中声明——不锁定 comp 名,以「载荷随画布 + * // 变化 + 画布键值显式出现」代码化 9.P2;comp 选择真伪由 realprocess 文件的产物尺寸 + * // 断言(场景 9.P1)兜底。 + * + * 惯例沿 wallpaper-video-overlay.acceptance.test.ts:mock node:child_process.spawn 断言调用 + * 形态;产物落盘用「argv 命中输出路径 → copy 输入视频」模拟。无 GPU、无真实渲染——无条件可跑。 + * 红队铁律:不读蓝队实现代码;renderTextOverlay 按契约函数名黑盒 import 执行;不 skip。 + */ +import { EventEmitter } from "node:events"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import Database from "better-sqlite3"; +import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { setupTestSchema } from "./helpers/test-schema"; + +// ============================================================================ +// hoisted holder + spawn mock +// ============================================================================ + +const holder = vi.hoisted(() => ({ + dbPath: ":memory:", + storageRoot: "/tmp/relight-none", + videoWorkspacePath: "/tmp/relight-none/video-workspace", + inputVideo: "/tmp/relight-none/input.mp4", +})); + +const mockSpawn = vi.hoisted(() => vi.fn()); + +vi.mock("node:child_process", async (importOriginal) => { + const actual = await importOriginal(); + return { ...actual, spawn: mockSpawn }; +}); + +interface SpawnCall { + cmd: string; + args: string[]; + cwd: string | undefined; +} + +const spawnCalls: SpawnCall[] = []; + +/** 构造最小假 child 进程:stdout/stderr 可监听,nextTick 后 emit close(0) */ +function fakeChild(): EventEmitter { + const child = new EventEmitter() as EventEmitter & { + stdout: EventEmitter; + stderr: EventEmitter; + kill: () => void; + pid: number; + }; + child.stdout = new EventEmitter(); + child.stderr = new EventEmitter(); + child.kill = () => false; + child.pid = 42424; + process.nextTick(() => { + child.stdout.emit("data", Buffer.from("mock-stdout")); + child.stderr.emit("data", Buffer.from("mock-stderr")); + child.emit("close", 0); + }); + return child; +} + +beforeEach(() => { + mockSpawn.mockReset(); + mockSpawn.mockImplementation((cmd: string, args: string[]) => { + spawnCalls.push({ cmd, args: [...args], cwd: process.cwd() }); + // 产物落盘:argv 中最后一个 .mp4 输出路径(≠输入视频)← copy 输入视频 + const outs = args.filter( + (a) => typeof a === "string" && a.endsWith(".mp4") && a !== holder.inputVideo, + ); + const out = outs[outs.length - 1]; + if (out) { + fs.mkdirSync(path.dirname(out), { recursive: true }); + fs.copyFileSync(holder.inputVideo, out); + } + return fakeChild(); + }); + spawnCalls.length = 0; +}); + +// ============================================================================ +// config / db mock(import 安全 + workspace 脚手架) +// ============================================================================ + +const TEST_COS = vi.hoisted(() => ({ + bucket: "little-bee-assets-1324334992", + region: "ap-shanghai", + prefix: "relight", +})); + +vi.mock("../lib/config", () => ({ + config: { + get databasePath() { + return process.env.DATABASE_PATH ?? holder.dbPath; + }, + get storageRoot() { + return holder.storageRoot; + }, + wallpaperVideoEnabled: true, + wallpaperVideoSeconds: 4, + wallpaperVideoLoopSeconds: 8, + wallpaperVideoSpawnTimeoutMs: 5400000, + honeydoCliPath: "/usr/bin/false", + wallpaperVideoPrompt: "画面中的景物以极缓慢的速度轻微摇曳", + get videoWorkspacePath() { + return holder.videoWorkspacePath; + }, + repoRoot: "/tmp/relight-none", + port: 3000, + redisUrl: "redis://localhost:6379", + bullmqPrefix: "bull-wvnos-test", + dailySelectionConcurrency: 1, + dailyAutoHealDays: 0, + dailySelectEnabled: false, + ai: { baseUrl: "", apiKey: "", visionModel: "", model: "", promptVersion: "v2" }, + video: { enabled: true, frameCount: 6, ffmpegPath: "ffmpeg", ffprobePath: "ffprobe" }, + daily: { cronTime: "0 0 * * *", maxCandidates: 20, timezone: "Asia/Shanghai" }, + cos: { + secretId: "test-id", + secretKey: "test-key", + bucket: TEST_COS.bucket, + region: TEST_COS.region, + prefix: TEST_COS.prefix, + }, + galleryPublicUrl: "https://gallery.stringzhao.life", + gallery: { vpsHost: "127.0.0.1", vpsUser: "test", vpsKey: "/tmp/test-key", vpsPath: "/tmp/g" }, + }, +})); + +vi.mock("../db", async () => { + const actualSchema = await import("../db/schema"); + const Database = (await import("better-sqlite3")).default; + const { drizzle } = await import("drizzle-orm/better-sqlite3"); + const sqlite = new Database(holder.dbPath); + sqlite.pragma("journal_mode = WAL"); + sqlite.pragma("foreign_keys = ON"); + return { db: drizzle(sqlite, { schema: actualSchema }), schema: actualSchema }; +}); + +// ============================================================================ +// 载荷收集:argv 直含 + argv 引用的 .json 载荷文件内容(--props 文件形态兼容) +// ============================================================================ + +function collectPayload(call: SpawnCall): string { + let payload = call.args.join("\n"); + for (const token of call.args) { + if (typeof token === "string" && token.endsWith(".json") && fs.existsSync(token)) { + payload += `\n${fs.readFileSync(token, "utf8")}`; + } + } + return payload; +} + +// ============================================================================ +// fixture + 被测函数导入 +// ============================================================================ + +let tmpRoot = ""; +let renderTextOverlay: ( + videoPath: string, + meta: { + pickDate: string; + title: string; + narrative: string; + captureDateline?: string; + canvasWidth?: number; + canvasHeight?: number; + }, +) => Promise<{ overlaidPath: string }>; + +beforeAll(async () => { + const ff = spawnSync("ffmpeg", ["-version"], { encoding: "utf-8", timeout: 10000 }); + if (ff.status !== 0) { + throw new Error("ffmpeg 不可用——本验收要求真实 ffmpeg 环境"); + } + + tmpRoot = fs.mkdtempSync(path.join(os.homedir(), ".relight-test-wvnos-")); + const dbPath = path.join(tmpRoot, "test.db"); + holder.dbPath = dbPath; + holder.storageRoot = path.join(tmpRoot, "storage"); + holder.videoWorkspacePath = path.join(tmpRoot, "video-workspace"); + // renderTextOverlay 前置检查 remotion bin 存在性(设计约定「不自动安装」)—— + // 本测试 mock 了 child_process.spawn 只为捕获载荷,须先造假 bin 让 access() 通过 + const overlayBinDir = path.join(holder.videoWorkspacePath, "node_modules", ".bin"); + fs.mkdirSync(overlayBinDir, { recursive: true }); + const overlayBin = path.join(overlayBinDir, "remotion"); + fs.writeFileSync(overlayBin, "#!/bin/sh\n"); + fs.chmodSync(overlayBin, 0o755); + process.env.DATABASE_PATH = dbPath; + process.env.STORAGE_ROOT = holder.storageRoot; + + const sqlite = new Database(dbPath); + sqlite.pragma("journal_mode = WAL"); + sqlite.pragma("foreign_keys = ON"); + setupTestSchema(sqlite); + sqlite.close(); + + // 输入母版:1s 1312×864 mp4(尺寸不影响本文件断言——spawn 形态为主) + holder.inputVideo = path.join(tmpRoot, "input-1312x864.mp4"); + const r = spawnSync( + "ffmpeg", + [ + "-y", + "-f", + "lavfi", + "-i", + "testsrc2=size=1312x864:rate=24:duration=1", + "-c:v", + "libx264", + "-pix_fmt", + "yuv420p", + "-an", + holder.inputVideo, + ], + { encoding: "utf-8", timeout: 30000 }, + ); + if (r.status !== 0 || !fs.existsSync(holder.inputVideo)) { + throw new Error(`fixture ffmpeg 造样片失败: ${r.stderr}`); + } + + // wallpaper-overlay 工程脚手架(fs access 前置校验用;渲染被 mock) + const ws = holder.videoWorkspacePath; + for (const rel of [ + path.join("wallpaper-overlay", "package.json"), + path.join("wallpaper-overlay", "src", "index.ts"), + path.join("wallpaper-overlay", "public", "fonts", "NotoSerifSC-Regular.otf"), + path.join("wallpaper-overlay", "public", "fonts", "Fraunces-Regular.otf"), + ]) { + const p = path.join(ws, rel); + fs.mkdirSync(path.dirname(p), { recursive: true }); + fs.writeFileSync(p, "// mock workspace scaffold\n"); + } + + const mod = (await import("../lib/wallpaper/video")) as unknown as Record; + expect( + typeof mod.renderTextOverlay, + "契约函数 renderTextOverlay 未由 lib/wallpaper/video 导出", + ).toBe("function"); + renderTextOverlay = mod.renderTextOverlay as typeof renderTextOverlay; +}, 60000); + +afterAll(() => { + if (tmpRoot) fs.rmSync(tmpRoot, { recursive: true, force: true }); +}); + +// ============================================================================ +// 场景 9.P2:spawn 命令行显式携带画布宽高参数 +// ============================================================================ + +describe("场景 9.P2 [det-machine]:renderTextOverlay spawn 显式携带画布宽高(契约 7 props 超集)", () => { + it("横版画布 1312×864 → 载荷含 canvasWidth/canvasHeight 键名与数值 1312/864", async () => { + const res = await renderTextOverlay(holder.inputVideo, { + pickDate: "2026-09-20", + title: "金色黄昏", + narrative: "五年前的今天,你在海边捕捉到了这张温暖的照片。", + captureDateline: "拍摄于 2021-05-05 18:30 · 5 年前", + canvasWidth: 1312, + canvasHeight: 864, + }); + expect(res.overlaidPath, "渲染必须产出 overlaidPath").toBeTruthy(); + expect(fs.existsSync(res.overlaidPath), "渲染产物必须落盘").toBe(true); + expect(spawnCalls.length, "必须发生 remotion render spawn").toBeGreaterThan(0); + + const payloads = spawnCalls.map(collectPayload).join("\n---call---\n"); + expect(payloads, `载荷必须显式含 canvasWidth 键:\n${payloads.slice(0, 2000)}`).toContain( + "canvasWidth", + ); + expect(payloads).toContain("canvasHeight"); + expect(payloads, "载荷必须携带画布宽 1312").toContain("1312"); + expect(payloads, "载荷必须携带画布高 864").toContain("864"); + }); + + it("竖版画布 800×1440(9:16 原生档,<0.9 → 竖版 comp)→ 载荷含 canvasWidth/canvasHeight 数值 800/1440", async () => { + const res = await renderTextOverlay(holder.inputVideo, { + pickDate: "2026-09-21", + title: "夏日正午", + narrative: "那天午后,阳光正好。", + canvasWidth: 800, + canvasHeight: 1440, + }); + expect(res.overlaidPath).toBeTruthy(); + expect(fs.existsSync(res.overlaidPath)).toBe(true); + + const payloads = spawnCalls.map(collectPayload).join("\n---call---\n"); + expect(payloads).toContain("canvasWidth"); + expect(payloads).toContain("canvasHeight"); + expect(payloads, "载荷必须携带画布宽 800").toContain("800"); + expect(payloads, "载荷必须携带画布高 1440").toContain("1440"); + }); + + it("画布参数随输入变化(1312×864 vs 800×1440 载荷不同)——kill 画布写死 No-op", async () => { + /** 提取载荷中 canvasWidth 键后紧跟的数值(JSON / argv 形态兼容) */ + function canvasWidthIn(payload: string): number | null { + const m = payload.match(/canvasWidth["'\s:=]+(\d+)/); + return m ? Number(m[1]) : null; + } + + await renderTextOverlay(holder.inputVideo, { + pickDate: "2026-09-20", + title: "t", + narrative: "n", + canvasWidth: 1312, + canvasHeight: 864, + }); + const firstPayload = spawnCalls.map(collectPayload).join("\n"); + expect(canvasWidthIn(firstPayload), "第一次渲染载荷 canvasWidth 必须 == 1312").toBe(1312); + + spawnCalls.length = 0; + await renderTextOverlay(holder.inputVideo, { + pickDate: "2026-09-21", + title: "t", + narrative: "n", + canvasWidth: 800, + canvasHeight: 1440, + }); + const secondPayload = spawnCalls.map(collectPayload).join("\n"); + + // 两次渲染的载荷必须真实携带各自画布数值(若实现写死 1280×704/736×1600,此断言红) + expect(canvasWidthIn(secondPayload), "第二次渲染载荷 canvasWidth 必须 == 800").toBe(800); + }); +}); diff --git a/apps/backend/src/__tests__/wallpaper-video-native-realprocess.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-native-realprocess.acceptance.test.ts new file mode 100644 index 0000000..7842046 --- /dev/null +++ b/apps/backend/src/__tests__/wallpaper-video-native-realprocess.acceptance.test.ts @@ -0,0 +1,292 @@ +/** + * 验收测试(红队,real-process,env 门控):单腿原生壁纸视频 — 全链路真跑 + COS 分发 + * + * 设计文档(state.md §验证方案 / §验收场景): + * 「真实生成(40-90min GPU)不进 CI 化 QA:1s 探针 + 自然日出片由用户按 SOP 验收」—— + * 本文件即 SOP 化的真跑验收,缺省 skip,操作者显式 env 接入后真跑(既有 env 门控模式, + * 参 WVQA_PORTRAIT_MP4 惯例;skip 在报告可见,非宽容跳过——det-machine 断言在其它 + * 验收文件中无条件可跑)。 + * + * 对应谓词: + * - 场景 1.P1 [real-process]:触发 + * `pnpm --filter @relight/backend run wallpaper-video:rerun -- --pickDate=D` + * → exit==0 且当日视频产物总数恰为 1(生成产物计数,排除 loop/overlay/转码中间产物) + * | env: WVQA_NATIVE_RERUN=1 + WVQA_NATIVE_RERUN_PICKDATE=D(进程 env 须已含 + * DAILY_WALLPAPER_VIDEO=true——rerun SOP 既有要求) + * - 场景 1.P2 [det-machine]:生成画布跟随原图比例(32 倍数 + abs(h - w*srcH/srcW) ≤ 32) + * | observe: ffprobe 产物 + DB 原图 dims + * - 场景 1.P3 [det-machine]:width*height ≤ 1177600 && ≥ 1177600/2 | observe: ffprobe + * - 场景 3.P3 [real-process]:兼容日护栏通过后 wallpaper_video_landscape_url != "" + * 且 COS HEAD 200 | env: WVQA_LANDSCAPE_COS_URL + * - 场景 6.P3 [real-process]:历史 COS 对象 HEAD 200 content-length>0 + * | env: WVQA_LEGACY_COS_URL + * - 场景 8.P3 [real-process]:原生视频 COS HEAD 200 video/mp4 | env: WVQA_NATIVE_COS_URL + * + * 红队铁律:门控 = 显式 env 接入(不是 try/catch 吞错、不是断言前 if 分支);接入后全部 + * 硬断言,失败即红。 + */ +import { spawnSync } from "node:child_process"; +import fs from "node:fs"; +import path from "node:path"; +import { describe, expect, it } from "vitest"; +import { config } from "../lib/config"; + +const ARTIFACT_DIR = "/tmp/autopilot-artifacts"; + +function writeArtifact(id: string, content: string): void { + fs.mkdirSync(ARTIFACT_DIR, { recursive: true }); + fs.writeFileSync(path.join(ARTIFACT_DIR, `${id}.out`), content); +} + +// ============================================================================ +// 门控(显式 env 接入,缺省 skip 可见) +// ============================================================================ + +const RERUN_ENABLED = process.env.WVQA_NATIVE_RERUN === "1"; +const RERUN_PICKDATE = process.env.WVQA_NATIVE_RERUN_PICKDATE ?? ""; +const dRerun = RERUN_ENABLED && RERUN_PICKDATE ? describe : describe.skip; +if (RERUN_ENABLED && !RERUN_PICKDATE) { + throw new Error( + "WVQA_NATIVE_RERUN=1 时必须同时提供 WVQA_NATIVE_RERUN_PICKDATE=(禁无目标真跑)", + ); +} +if (!RERUN_ENABLED) { + console.warn( + "[wallpaper-video-native-realprocess] 全链路真跑未接入(WVQA_NATIVE_RERUN!=1)——3 组真跑用例 skip(SOP:DAILY_WALLPAPER_VIDEO=true 下接入)", + ); +} + +// ============================================================================ +// ffprobe 辅助 +// ============================================================================ + +interface ProbeResult { + formatName: string; + width: number | null; + height: number | null; +} + +function probe(file: string): ProbeResult { + const r = spawnSync( + "ffprobe", + ["-v", "error", "-show_format", "-show_streams", "-of", "json", file], + { encoding: "utf-8", timeout: 30000, maxBuffer: 16 * 1024 * 1024 }, + ); + if (r.status !== 0 || !r.stdout) { + throw new Error(`ffprobe 失败(${file}): ${r.stderr}`); + } + const j = JSON.parse(r.stdout) as { + format?: { format_name?: string }; + streams?: Array<{ codec_type?: string; width?: number; height?: number }>; + }; + const v = (j.streams ?? []).find((s) => s.codec_type === "video"); + return { + formatName: j.format?.format_name ?? "", + width: v?.width ?? null, + height: v?.height ?? null, + }; +} + +/** HEAD 请求(原生 fetch;断言状态/类型/长度) */ +async function head( + url: string, +): Promise<{ status: number; contentType: string; contentLength: number }> { + const res = await fetch(url, { method: "HEAD" }); + return { + status: res.status, + contentType: res.headers.get("content-type") ?? "", + contentLength: Number(res.headers.get("content-length") ?? "0"), + }; +} + +// ============================================================================ +// 场景 1.P1 / 1.P2 / 1.P3:rerun 全链路真跑 +// ============================================================================ + +dRerun(`场景 1:rerun 全链路真跑(pickDate=${RERUN_PICKDATE},40-90min GPU,SOP 接入)`, () => { + it( + "场景 1.P1 [real-process]:rerun exit==0 且当日生成产物总数恰为 1", + async () => { + // 谓词逐字命令:pnpm --filter @relight/backend run wallpaper-video:rerun -- --pickDate=D + const repoRoot = path.resolve(__dirname, "../../.."); + const startedAt = new Date(Date.now() - 5000); // 5s 容差(文件 mtime 粒度) + const r = spawnSync( + "pnpm", + [ + "--filter", + "@relight/backend", + "run", + "wallpaper-video:rerun", + "--", + `--pickDate=${RERUN_PICKDATE}`, + ], + { + cwd: repoRoot, + encoding: "utf-8", + timeout: 2.5 * 3600 * 1000, + maxBuffer: 64 * 1024 * 1024, + env: { ...process.env, DAILY_WALLPAPER_VIDEO: "true" }, + }, + ); + writeArtifact( + "s1p1", + JSON.stringify( + { + exitCode: r.status, + signal: r.signal, + stdoutTail: (r.stdout ?? "").slice(-2000), + stderrTail: (r.stderr ?? "").slice(-2000), + }, + null, + 2, + ), + ); + // 谓词字面量:exit == 0 + expect(r.status, `rerun 必须退出码 0(stderr tail: ${(r.stderr ?? "").slice(-2000)})`).toBe( + 0, + ); + + // 当日生成产物计数:run 期间新建的 .mp4/.mov,排除 loop/overlay/转码/首帧中间产物与 + // legacy 命名——单腿原生 = 恰 1 个「生成」产物(native 母版)。 + // 生成/转码产物落 {STORAGE_ROOT}/wallpaper-videos/(jobs/wallpaper-video.ts wallpaperVideoDir), + // 非 videoWorkspacePath(那是 Remotion overlay 工程与中间件目录)。 + const ws = path.join(config.storageRoot, "wallpaper-videos"); + const fresh: string[] = []; + const scan = (dir: string): void => { + for (const e of fs.readdirSync(dir, { withFileTypes: true })) { + const p = path.join(dir, e.name); + if (e.isDirectory()) { + scan(p); + continue; + } + if (!/\.(mp4|mov)$/i.test(e.name)) continue; + if ( + /(loop|overlay|_landscape\.mov|_portrait\.mp4|first-frame|last-frame|preprocess)/i.test( + e.name, + ) + ) { + continue; + } + if (fs.statSync(p).mtimeMs > startedAt.getTime()) fresh.push(p); + } + }; + expect(fs.existsSync(ws), `videoWorkspacePath 不存在: ${ws}`).toBe(true); + scan(ws); + writeArtifact("s1p1-products", JSON.stringify(fresh, null, 2)); + expect( + fresh.length, + `当日生成产物总数必须恰为 1(单腿),实际 ${fresh.length}: ${JSON.stringify(fresh)}`, + ).toBe(1); + }, + 2.6 * 3600 * 1000, + ); + + it("场景 1.P2/P3 [det-machine]:生成画布跟随原图比例(32 倍数 + |h - w*srcH/srcW| ≤ 32)且像素预算窗口", async () => { + // 原图 dims:DB 当日 pick → photos 行(真实环境 DATABASE_PATH,只读) + const Database = (await import("better-sqlite3")).default; + const sqlite = new Database(config.databasePath, { readonly: true }); + const row = sqlite + .prepare( + `SELECT p.file_path AS file_path, p.width AS w, p.height AS h + FROM daily_picks dp JOIN photos p ON p.id = dp.photo_id + WHERE dp.pick_date = ?`, + ) + .get(RERUN_PICKDATE) as { file_path: string; w: number; h: number } | undefined; + sqlite.close(); + expect(row, `DB 缺 ${RERUN_PICKDATE} 当日 pick/photo 行`).toBeTruthy(); + const src = row as { file_path: string; w: number; h: number }; + + // 产物定位:同上轮扫描约定(native 生成产物,落 STORAGE_ROOT/wallpaper-videos/) + const ws = path.join(config.storageRoot, "wallpaper-videos"); + const startedAt = Date.now() - 3 * 3600 * 1000; // 3h 窗口(同轮真跑产物) + const fresh: string[] = []; + const scan = (dir: string): void => { + for (const e of fs.readdirSync(dir, { withFileTypes: true })) { + const p = path.join(dir, e.name); + if (e.isDirectory()) { + scan(p); + continue; + } + if (!/\.(mp4|mov)$/i.test(e.name)) continue; + if ( + /(loop|overlay|_landscape\.mov|_portrait\.mp4|first-frame|last-frame|preprocess)/i.test( + e.name, + ) + ) { + continue; + } + if (fs.statSync(p).mtimeMs > startedAt) fresh.push(p); + } + }; + scan(ws); + fresh.sort((a, b) => fs.statSync(b).mtimeMs - fs.statSync(a).mtimeMs); + const product = fresh[0]; + expect(product, `找不到近期生成产物( scanned ${fresh.length} 个)`).toBeTruthy(); + + const meta = probe(product as string); + const w = meta.width as number; + const h = meta.height as number; + // 谓词字面量:32 倍数 + expect(w % 32, `产物宽 ${w} 必须是 32 倍数`).toBe(0); + expect(h % 32, `产物高 ${h} 必须是 32 倍数`).toBe(0); + // 谓词字面量:abs(h - w*srcH/srcW) <= 32(画布跟随原图比例) + const expectedH = w * (src.h / src.w); + expect( + Math.abs(h - expectedH), + `画布比例必须跟随原图:|${h} - ${w}*${src.h}/${src.w}| = ${Math.abs(h - expectedH).toFixed(1)} > 32`, + ).toBeLessThanOrEqual(32); + // 谓词字面量:width*height <= 1177600 && >= 1177600/2 + const pixels = w * h; + expect(pixels).toBeLessThanOrEqual(1_177_600); + expect(pixels).toBeGreaterThanOrEqual(1_177_600 / 2); + writeArtifact("s1p2", JSON.stringify({ product, w, h, srcW: src.w, srcH: src.h, pixels })); + writeArtifact("s1p3", JSON.stringify({ product, pixels, budget: 1_177_600 })); + }, 120000); +}); + +// ============================================================================ +// COS 分发真跑(场景 3.P3 / 6.P3 / 8.P3)——env 提供 URL 接入(it.skipIf 收集期门控) +// ============================================================================ + +const LANDSCAPE_COS_URL = process.env.WVQA_LANDSCAPE_COS_URL; +const LEGACY_COS_URL = process.env.WVQA_LEGACY_COS_URL; +const NATIVE_COS_URL = process.env.WVQA_NATIVE_COS_URL; + +if (!LANDSCAPE_COS_URL || !LEGACY_COS_URL || !NATIVE_COS_URL) { + console.warn( + "[wallpaper-video-native-realprocess] COS HEAD 未全部接入(WVQA_LANDSCAPE_COS_URL / WVQA_LEGACY_COS_URL / WVQA_NATIVE_COS_URL)——对应组 skip(自然日出片后接入)", + ); +} + +describe("场景 COS HEAD 真跑(env 接入式门控)", () => { + it.skipIf(!LANDSCAPE_COS_URL)( + "场景 3.P3 [real-process]:兼容日 landscape .mov COS HEAD 200", + async () => { + const r = await head(LANDSCAPE_COS_URL as string); + writeArtifact("s3p3", JSON.stringify({ url: LANDSCAPE_COS_URL, ...r })); + expect(r.status).toBe(200); + expect(r.contentLength).toBeGreaterThan(0); + }, + ); + + it.skipIf(!LEGACY_COS_URL)( + "场景 6.P3 [real-process]:历史 legacy COS 对象 HEAD 200 content-length>0", + async () => { + const r = await head(LEGACY_COS_URL as string); + writeArtifact("s6p3", JSON.stringify({ url: LEGACY_COS_URL, ...r })); + expect(r.status).toBe(200); + expect(r.contentLength).toBeGreaterThan(0); + }, + ); + + it.skipIf(!NATIVE_COS_URL)( + "场景 8.P3 [real-process]:原生视频 COS HEAD 200 video/mp4", + async () => { + const r = await head(NATIVE_COS_URL as string); + writeArtifact("s8p3", JSON.stringify({ url: NATIVE_COS_URL, ...r })); + expect(r.status).toBe(200); + expect(r.contentType).toContain("video/mp4"); + expect(r.contentLength).toBeGreaterThan(0); + }, + ); +}); diff --git a/apps/backend/src/__tests__/wallpaper-video-native-route.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-native-route.acceptance.test.ts new file mode 100644 index 0000000..d058d17 --- /dev/null +++ b/apps/backend/src/__tests__/wallpaper-video-native-route.acceptance.test.ts @@ -0,0 +1,299 @@ +/** + * 验收测试(红队):单腿原生壁纸视频 — 路由契约(native 不进 mac 路由 / 静态回退存活) + * + * 设计文档(state.md §契约规约 5 / §验收场景)对应谓词: + * - 场景 1.P5 [real-process→route 级代码化]:非兼容日(native 列非空但无 .mov) + * → GET /api/daily/D/wallpaper-video → **404**(mac 回退静态语义;native 分发走画廊 + * manifest 字段,不经此路由) + * - 场景 4.P2 [real-process→route 级代码化]:比例不兼容日 GET wallpaper-video → 404 + * - 场景 4.P3 [real-process→route 级代码化]:静态壁纸路由 + * GET /api/daily/D/wallpaper?width=1920&height=1080 → 200 image/jpeg 非空 + * (mac 静态回退路径存活) + * - 场景 6.P1 [real-process→route 级代码化]:仅 legacy 两列的历史日 → 路由 302 到 + * legacy COS key(历史兼容,D4) + * - 场景 7.P1 [real-process→route 级代码化]:兼容日(native 与 landscape 并存) + * → GET wallpaper-video 302 到 **landscape** key(防实现误把路由改成 native 优先 + * ——mac 只允许消费 16:9;读取优先级 native > portrait > landscape 只在画廊/manifest 层,契约 4/5) + * + * 契约 5 逐字:GET /api/daily/:pickDate/wallpaper-video 语义不变(302 → landscape 列 = + * 16:9 版;空/缺失 → 404 JSON);mac App 零改动。 + * + * 惯例沿 wallpaper-video-api.acceptance.test.ts + daily-wallpaper-route.acceptance.test.ts: + * chainable mock db + createApp 黑盒请求 + mock composer(缓存路径推导)。 + * 红队铁律:不读蓝队实现代码;不 skip、硬断言。 + */ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; + +/** + * 可链式 Mock(daily-wallpaper-route.acceptance.test.ts 同款,含 values/set) + */ +function chainableMock(result: unknown[] = []) { + const fn = (..._args: unknown[]) => chainableMock(result); + return new Proxy(fn, { + get(_target, prop) { + if (prop === "then") { + return (resolve: (v: unknown) => unknown) => resolve(result); + } + if (prop === Symbol.toPrimitive || prop === "toString" || prop === "valueOf") { + return () => "[]"; + } + if (typeof prop === "string" && /^\d+$/.test(prop)) { + return result[Number(prop)]; + } + if (prop === "values") { + return (...args: unknown[]) => chainableMock(args[0] ? [args[0]] : result); + } + if (prop === "set") { + return (...args: unknown[]) => chainableMock(args[0] ? [args[0]] : result); + } + return chainableMock(result); + }, + }); +} + +const mockDb = vi.hoisted(() => ({ + select: vi.fn(), + insert: vi.fn(), + update: vi.fn(), +})); + +vi.mock("../db", () => ({ + db: mockDb, + schema: chainableMock([]), +})); + +vi.mock("../jobs/queues", () => ({ + scanQueue: { add: vi.fn(async () => ({ id: "mock-job-id" })) }, + analyzeQueue: { add: vi.fn(async () => ({ id: "mock-job-id" })) }, + dailyQueue: { add: vi.fn(async () => ({ id: "mock-job-id" })) }, + dailyPushQueue: { add: vi.fn(async () => ({ id: "mock-job-id" })) }, + dailyVideoQueue: { add: vi.fn(async () => ({ id: "mock-job-id" })) }, + wallpaperVideoQueue: { add: vi.fn(async () => ({ id: "mock-job-id" })) }, +})); + +// 合成器 mock:路由用 composedCachePath 推导缓存路径(daily-wallpaper-route 同款)—— +// 场景 4.P3 用缓存命中路径(预置真实 JPEG),缓存命中不应触发实时合成 +const mockComposeWallpaper = vi.hoisted(() => vi.fn()); +const mockComposeAndSave = vi.hoisted(() => vi.fn()); + +vi.mock("../lib/wallpaper/composer", () => ({ + composeWallpaper: mockComposeWallpaper, + composeAndSave: mockComposeAndSave, + composedCachePath: (pickDate: string, width: number, height: number) => + path.join( + process.env.STORAGE_ROOT ?? os.tmpdir(), + "daily-composed", + `${pickDate}_${width}x${height}.jpg`, + ), +})); + +import { createApp } from "../app"; + +// ---- 契约字面量(§契约规约 2 逐字 key → 回执 URL)---- + +const PICK_NATIVE_ONLY = "2026-09-20"; // 非兼容日:仅 native(场景 1.P5 / 4.P2) +const PICK_COMPAT = "2026-09-19"; // 兼容日:native + landscape(场景 7.P1) +const PICK_LEGACY = "2026-09-10"; // 历史 legacy-only(场景 6.P1) +const PICK_STATIC = "2026-09-21"; // 场景 4.P3 静态回退日 + +const BUCKET = "little-bee-assets-1324334992"; +const REGION = "ap-shanghai"; +const COS = (key: string) => `https://${BUCKET}.cos.${REGION}.myqcloud.com/${key}`; +const nativeUrl = (d: string) => COS(`relight/wallpaper-videos/${d}_native.mp4`); +const landscapeUrl = (d: string) => COS(`relight/wallpaper-videos/${d}_landscape.mov`); +const portraitUrl = (d: string) => COS(`relight/wallpaper-videos/${d}_portrait.mp4`); + +function makePick(pickDate: string, video: Partial> = {}) { + return { + id: `pick-${pickDate}`, + photoId: `photo-${pickDate}`, + pickDate, + title: "金色黄昏", + narrative: "五年前的今天,你在海边捕捉到了这张温暖的照片。", + score: 8.5, + composedImagePath: `daily-composed/${pickDate}.jpg`, + members: [], + createdAt: "2026-09-12T06:00:00.000Z", + wallpaperVideoNativeUrl: null as string | null, + wallpaperVideoLandscapeUrl: null as string | null, + wallpaperVideoPortraitUrl: null as string | null, + ...video, + }; +} + +function app() { + return createApp(); +} + +// ---- 场景 4.P3 静态缓存 fixture ---- + +let tmpRoot = ""; +let staticCachePath = ""; + +beforeAll(async () => { + tmpRoot = fs.mkdtempSync(path.join(os.homedir(), ".relight-test-wvnroute-")); + process.env.STORAGE_ROOT = tmpRoot; + + const sharp = (await import("sharp")).default; + const cacheDir = path.join(tmpRoot, "daily-composed"); + fs.mkdirSync(cacheDir, { recursive: true }); + staticCachePath = path.join(cacheDir, `${PICK_STATIC}_1920x1080.jpg`); + await sharp({ create: { width: 1920, height: 1080, channels: 3, background: "#2288cc" } }) + .jpeg({ quality: 85 }) + .toFile(staticCachePath); + expect(fs.statSync(staticCachePath).size, "fixture 缓存 JPEG 必须非空").toBeGreaterThan(0); +}); + +afterAll(() => { + // biome-ignore lint/performance/noDelete: process.env 必须用 delete 取消设置 + delete process.env.STORAGE_ROOT; + if (tmpRoot) fs.rmSync(tmpRoot, { recursive: true, force: true }); +}); + +beforeEach(() => { + vi.clearAllMocks(); + mockDb.select.mockReturnValue(chainableMock([])); + mockDb.insert.mockReturnValue(chainableMock([])); + mockDb.update.mockReturnValue(chainableMock([])); +}); + +// ============================================================================ +// GET /api/daily/:pickDate/wallpaper-video —— native 时代路由语义(契约 5:语义不变) +// ============================================================================ + +describe("场景 1.P5 / 4.P2:非兼容日(仅 native、无 .mov)→ wallpaper-video 路由 404", () => { + it("native 列非空 + landscape 列空 → 404 且 body JSON 含 error(mac 回退静态;native 不经此路由)", async () => { + mockDb.select.mockReturnValueOnce( + chainableMock([ + makePick(PICK_NATIVE_ONLY, { wallpaperVideoNativeUrl: nativeUrl(PICK_NATIVE_ONLY) }), + ]), + ); + + const res = await app().request(`/api/daily/${PICK_NATIVE_ONLY}/wallpaper-video`, { + method: "GET", + }); + const body = (await res.json().catch(() => null)) as { error?: unknown } | null; + + // 谓词字面量:404(native 存在不得改变此判定——防实现误把路由改成 native 优先) + expect(res.status, "native-only 日必须 404(契约 5:路由只消费 landscape 16:9 列)").toBe(404); + expect(body).not.toBeNull(); + expect(body).toHaveProperty("error"); + expect(typeof body?.error).toBe("string"); + expect((body?.error as string).length).toBeGreaterThan(0); + }); +}); + +describe("场景 7.P1:兼容日(native 与 landscape 并存)→ 302 到 landscape key(防 native 优先误改)", () => { + it("native+landscape 并存 → 302 且 Location == landscape COS URL(逐字,非 native URL)", async () => { + mockDb.select.mockReturnValueOnce( + chainableMock([ + makePick(PICK_COMPAT, { + wallpaperVideoNativeUrl: nativeUrl(PICK_COMPAT), + wallpaperVideoLandscapeUrl: landscapeUrl(PICK_COMPAT), + }), + ]), + ); + + const res = await app().request(`/api/daily/${PICK_COMPAT}/wallpaper-video`, { + method: "GET", + }); + const location = res.headers.get("Location") ?? ""; + + expect(res.status).toBe(302); + expect(location, "Location 必须逐字 == legacy landscape key URL").toBe( + landscapeUrl(PICK_COMPAT), + ); + expect(location.endsWith("_landscape.mov")).toBe(true); + expect(location.endsWith("_native.mp4"), "路由绝不允许 302 到 native(mac 只吃 16:9)").toBe( + false, + ); + }); + + it("契约 5 语义不变:GET /api/daily/today 的 wallpaperVideoUrl 仍取 landscape(_landscape.mov)", async () => { + mockDb.select + .mockReturnValueOnce( + chainableMock([ + makePick(PICK_COMPAT, { + wallpaperVideoNativeUrl: nativeUrl(PICK_COMPAT), + wallpaperVideoLandscapeUrl: landscapeUrl(PICK_COMPAT), + }), + ]), + ) + .mockReturnValueOnce( + chainableMock([ + { + id: `photo-${PICK_COMPAT}`, + storageSourceId: "source-001", + filePath: "/photos/sunset.jpg", + fileHash: "abc123", + width: 4000, + height: 3000, + fileSize: 5242880, + thumbnailPath: "/thumbnails/sunset.jpg", + takenAt: "2019-05-05T18:30:00.000Z", + createdAt: "2026-01-01T00:00:00.000Z", + }, + ]), + ); + + const res = await app().request("/api/daily/today", { method: "GET" }); + const body = (await res.json()) as { success: boolean; data: Record }; + + expect(res.status).toBe(200); + expect(body.success).toBe(true); + expect("wallpaperVideoUrl" in body.data).toBe(true); + expect((body.data.wallpaperVideoUrl as string).endsWith("_landscape.mov")).toBe(true); + }); +}); + +describe("场景 6.P1:历史 legacy-only 日 → 路由 302 到 legacy COS key", () => { + it("landscape+portrait 非空、native NULL → 302 且 Location == legacy landscape URL(逐字)", async () => { + mockDb.select.mockReturnValueOnce( + chainableMock([ + makePick(PICK_LEGACY, { + wallpaperVideoLandscapeUrl: landscapeUrl(PICK_LEGACY), + wallpaperVideoPortraitUrl: portraitUrl(PICK_LEGACY), + }), + ]), + ); + + const res = await app().request(`/api/daily/${PICK_LEGACY}/wallpaper-video`, { + method: "GET", + }); + const location = res.headers.get("Location") ?? ""; + + expect(res.status).toBe(302); + expect(location).toBe(landscapeUrl(PICK_LEGACY)); + expect(location).toContain("myqcloud.com"); + }); +}); + +// ============================================================================ +// 场景 4.P3:静态壁纸路由存活(mac 静态回退路径) +// ============================================================================ + +describe("场景 4.P3:非兼容日静态壁纸路由 → 200 image/jpeg 非空(mac 静态回退语义)", () => { + it("GET /api/daily/D/wallpaper?width=1920&height=1080 → 200 + image/jpeg + body 非空", async () => { + mockDb.select.mockReturnValueOnce( + chainableMock([ + makePick(PICK_STATIC, { composedImagePath: `daily-composed/${PICK_STATIC}.jpg` }), + ]), + ); + // 路由第二级查询:photos 表(pick.photoId 缺失会 404「照片不存在」) + mockDb.select.mockReturnValueOnce(chainableMock([{ id: `photo-${PICK_STATIC}` }])); + + const res = await app().request(`/api/daily/${PICK_STATIC}/wallpaper?width=1920&height=1080`, { + method: "GET", + }); + const buf = Buffer.from(await res.arrayBuffer()); + + expect(res.status, `静态壁纸路由必须 200(mac 回退依赖),实际 ${res.status}`).toBe(200); + expect(res.headers.get("content-type") ?? "").toContain("image/jpeg"); + expect(buf.length, "响应体必须非空").toBeGreaterThan(0); + // 缓存命中路径:不得触发实时合成(fixture 缓存文件已就位) + expect(mockComposeAndSave).not.toHaveBeenCalled(); + }); +}); diff --git a/apps/backend/src/__tests__/wallpaper-video-native-transcode.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-native-transcode.acceptance.test.ts new file mode 100644 index 0000000..05479a7 --- /dev/null +++ b/apps/backend/src/__tests__/wallpaper-video-native-transcode.acceptance.test.ts @@ -0,0 +1,439 @@ +/** + * 验收测试(红队):单腿原生壁纸视频 — 转码 invariant(真实小样本 ffmpeg/ffprobe) + * + * 设计文档(state.md §设计文档 D2 / §验收场景)对应谓词: + * - 场景 3.P1 [real-process→转码级代码化]:兼容窗口内转码段产出 + * width==1920 && height==1080 && format contains mov + * (输入 = 原生画布 1312×864(3:2,ratio≈1.5185 ∈ [1.5,1.9]);真实 ffprobe 断言) + * - 场景 3.P2 [det-machine]:转码 filter 链无造成非等比形变——「绝不拉伸」。 + * driver 设计文档为 fs-grep 转码命令日志;本文件以更强的像素级黑盒断言代码化: + * 源图中心放正方形标记 → 转码产物首帧中标记必须仍为正方形(若实现跳过 crop 前置、 + * 直接把 1312×864 拉伸到 1920×1080,正方形会变 439×375 长方形 → 红)。 + * crop 前置 + scale=1920:1080 等比后置时正方形保持(缩放统一 ×1.4634)。 + * - 场景 3.P4 [det-machine]:裁切面积占比 ≤ 0.15(T_crop=0.15,防大幅裁切伪装微裁)。 + * 行灰度梯度源:产物首帧中心列像素值反演存活的源行带 → cropFrac = 1 - 带高/864。 + * 3:2(1312×864)最小 16:9 居中裁切 = 1312×738 → cropFrac = 14.58% ≤ 15%; + * 并断言居中(D2「ffmpeg 居中裁到 16:9」):带顶 ≈ 63±10、带底距底 ≈ 63±10。 + * - 转码A 对照(D2:沿用 transcodeForGallery 无 -vf 尺寸透传 → probe 断言 w×h == 画布): + * 1312×864 输入 → 产物 1312×864 + mp4/h264/aac 立体声/+faststart(v2 invariant 沿承)。 + * // CONTRACT_AMBIGUOUS: transcodeForGallery 的「probe 断言 == 画布」预期值来源 + * //(签名是否新增画布参数)契约未逐字固定——本文件按既有参数面 (src, dst) 两参调用, + * // 画布尺寸输入下产物 invariant 必须成立;护栏注入面(错尺寸拒绝)落 job 级验收文件。 + * + * 惯例沿 wallpaper-video-transcode.acceptance.test.ts:真实 ffmpeg/ffprobe(缺失即真红, + * 项目硬依赖)+ mock config/db 仅保证模块 import 安全。 + * 红队铁律:transcodeForGallery / transcodeForAerialNative 按契约函数名黑盒 import 执行;不 skip。 + */ +import { spawnSync } from "node:child_process"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import Database from "better-sqlite3"; +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { setupTestSchema } from "./helpers/test-schema"; + +// ============================================================================ +// hoisted holder + mock(config 全量 stub / db 真实 drizzle——仅保证模块 import 安全) +// ============================================================================ + +const holder = vi.hoisted(() => ({ + dbPath: ":memory:", + storageRoot: "/tmp/relight-none", +})); + +const TEST_COS = vi.hoisted(() => ({ + bucket: "little-bee-assets-1324334992", + region: "ap-shanghai", + prefix: "relight", +})); + +vi.mock("../lib/config", () => ({ + config: { + get databasePath() { + return process.env.DATABASE_PATH ?? holder.dbPath; + }, + get storageRoot() { + return holder.storageRoot; + }, + wallpaperVideoEnabled: true, + wallpaperVideoSeconds: 4, + wallpaperVideoLoopSeconds: 8, + wallpaperVideoSpawnTimeoutMs: 5400000, + honeydoCliPath: "/usr/bin/false", + wallpaperVideoPrompt: + "画面中的景物以极缓慢的速度轻微摇曳,光影柔和流动,随后一切缓缓回到初始位置,如呼吸般自然", + videoWorkspacePath: "/tmp/relight-none/video-workspace", + repoRoot: "/tmp/relight-none", + port: 3000, + redisUrl: "redis://localhost:6379", + bullmqPrefix: "bull-wvnt-test", + dailySelectionConcurrency: 1, + dailyAutoHealDays: 0, + dailySelectEnabled: false, + ai: { baseUrl: "", apiKey: "", visionModel: "", model: "", promptVersion: "v2" }, + video: { enabled: true, frameCount: 6, ffmpegPath: "ffmpeg", ffprobePath: "ffprobe" }, + daily: { cronTime: "0 0 * * *", maxCandidates: 20, timezone: "Asia/Shanghai" }, + cos: { + secretId: "test-id", + secretKey: "test-key", + bucket: TEST_COS.bucket, + region: TEST_COS.region, + prefix: TEST_COS.prefix, + }, + galleryPublicUrl: "https://gallery.stringzhao.life", + gallery: { vpsHost: "127.0.0.1", vpsUser: "test", vpsKey: "/tmp/test-key", vpsPath: "/tmp/g" }, + }, +})); + +vi.mock("../db", async () => { + const actualSchema = await import("../db/schema"); + const Database = (await import("better-sqlite3")).default; + const { drizzle } = await import("drizzle-orm/better-sqlite3"); + const sqlite = new Database(holder.dbPath); + sqlite.pragma("journal_mode = WAL"); + sqlite.pragma("foreign_keys = ON"); + return { db: drizzle(sqlite, { schema: actualSchema }), schema: actualSchema }; +}); + +// ============================================================================ +// ffprobe / 像素提取辅助 +// ============================================================================ + +interface ProbeResult { + formatName: string; + videoCodec: string | null; + codecTag: string | null; + width: number | null; + height: number | null; + audioStreams: number; + audioCodec: string | null; + audioChannels: number | null; +} + +function probe(file: string): ProbeResult { + const r = spawnSync( + "ffprobe", + ["-v", "error", "-show_format", "-show_streams", "-of", "json", file], + { encoding: "utf-8", timeout: 30000, maxBuffer: 16 * 1024 * 1024 }, + ); + if (r.status !== 0 || !r.stdout) { + throw new Error(`ffprobe 失败(${file}): ${r.stderr}`); + } + const j = JSON.parse(r.stdout) as { + format?: { format_name?: string }; + streams?: Array<{ + codec_type?: string; + codec_name?: string; + codec_tag_string?: string; + width?: number; + height?: number; + channels?: number; + }>; + }; + const streams = j.streams ?? []; + const v = streams.find((s) => s.codec_type === "video"); + const audios = streams.filter((s) => s.codec_type === "audio"); + return { + formatName: j.format?.format_name ?? "", + videoCodec: v?.codec_name ?? null, + codecTag: v?.codec_tag_string ?? null, + width: v?.width ?? null, + height: v?.height ?? null, + audioStreams: audios.length, + audioCodec: audios[0]?.codec_name ?? null, + audioChannels: audios[0]?.channels ?? null, + }; +} + +function moovBeforeMdat(file: string): boolean { + const buf = fs.readFileSync(file); + const moov = buf.indexOf("moov"); + const mdat = buf.indexOf("mdat"); + return moov >= 0 && mdat >= 0 && moov < mdat; +} + +/** 提取首帧 rgb24 原始像素(W×H×3) */ +function firstFrameRaw(file: string, width: number, height: number): Buffer { + const r = spawnSync( + "ffmpeg", + ["-v", "error", "-i", file, "-frames:v", "1", "-f", "rawvideo", "-pix_fmt", "rgb24", "-"], + { encoding: "buffer", timeout: 60000, maxBuffer: 64 * 1024 * 1024 }, + ); + if (r.status !== 0 || !r.stdout || r.stdout.length < width * height * 3) { + throw new Error(`首帧提取失败(${file}): ${r.stderr?.toString().slice(0, 500)}`); + } + return Buffer.from(r.stdout); +} + +function pixelAt( + buf: Buffer, + width: number, + x: number, + y: number, +): { r: number; g: number; b: number } { + const idx = (y * width + x) * 3; + return { r: buf[idx] as number, g: buf[idx + 1] as number, b: buf[idx + 2] as number }; +} + +// ============================================================================ +// fixture 生成 +// ============================================================================ + +let tmpRoot = ""; +let canvasMaster = ""; // 1312×864 带立体声音轨(原生画布母版形态) +let markerSrc = ""; // 1312×864 中心 300×300 红方块(形变检测) +let gradientSrc = ""; // 1312×864 行灰度梯度(裁切率检测) +let transcodeForGallery: (src: string, dst: string) => Promise; +let transcodeForAerialNative: (src: string, dst: string) => Promise; + +function makeSrcWithStereoAudio(outPath: string, size: string): void { + const r = spawnSync( + "ffmpeg", + [ + "-y", + "-f", + "lavfi", + "-i", + `testsrc2=size=${size}:rate=24:duration=2`, + "-f", + "lavfi", + "-i", + "sine=frequency=440:sample_rate=44100:duration=2", + "-c:v", + "libx264", + "-pix_fmt", + "yuv420p", + "-c:a", + "aac", + "-b:a", + "128k", + "-ac", + "2", + "-shortest", + outPath, + ], + { encoding: "utf-8", timeout: 60000 }, + ); + if (r.status !== 0 || !fs.existsSync(outPath)) { + throw new Error(`fixture ffmpeg 造样片失败(${size}): ${r.stderr}`); + } +} + +/** 行灰度梯度 png:第 y 行灰度 = round(y*255/(H-1))(裁切率反演用) */ +async function makeRowGradientPng(outPath: string, width: number, height: number): Promise { + const sharp = (await import("sharp")).default; + const rows: Buffer[] = []; + for (let y = 0; y < height; y++) { + const g = Math.round((y * 255) / (height - 1)); + rows.push(Buffer.alloc(width * 3, g)); + } + await sharp(Buffer.concat(rows), { raw: { width, height, channels: 3 } }) + .png() + .toFile(outPath); +} + +beforeAll(async () => { + for (const bin of ["ffmpeg", "ffprobe"]) { + const p = spawnSync(bin, ["-version"], { encoding: "utf-8", timeout: 15000 }); + if (p.status !== 0) { + throw new Error(`${bin} 不可用——转码验收要求真实环境`); + } + } + + tmpRoot = fs.mkdtempSync(path.join(os.homedir(), ".relight-test-wvntr-")); + const dbPath = path.join(tmpRoot, "test.db"); + holder.dbPath = dbPath; + holder.storageRoot = path.join(tmpRoot, "storage"); + process.env.DATABASE_PATH = dbPath; + process.env.STORAGE_ROOT = holder.storageRoot; + + const sqlite = new Database(dbPath); + sqlite.pragma("journal_mode = WAL"); + sqlite.pragma("foreign_keys = ON"); + setupTestSchema(sqlite); + sqlite.close(); + + // 原生画布母版:1312×864(3:2 hero → computeNativeCanvas D1 参考输出)带立体声音轨 + canvasMaster = path.join(tmpRoot, "canvas-1312x864.mp4"); + makeSrcWithStereoAudio(canvasMaster, "1312x864"); + + // 形变检测源:1312×864 蓝底 + 中心 300×300 红方块 + markerSrc = path.join(tmpRoot, "marker-1312x864.png"); + { + const sharp = (await import("sharp")).default; + const square = await sharp({ + create: { width: 300, height: 300, channels: 3, background: "#cc2222" }, + }) + .png() + .toBuffer(); + await sharp({ + create: { width: 1312, height: 864, channels: 3, background: "#2288cc" }, + }) + .composite([ + { input: square, left: Math.round((1312 - 300) / 2), top: Math.round((864 - 300) / 2) }, + ]) + .png() + .toFile(markerSrc); + } + + // 裁切率检测源:1312×864 行灰度梯度 + gradientSrc = path.join(tmpRoot, "gradient-1312x864.png"); + await makeRowGradientPng(gradientSrc, 1312, 864); + + const mod = (await import("../lib/wallpaper/video")) as unknown as Record; + expect( + typeof mod.transcodeForGallery, + "契约函数 transcodeForGallery 未由 lib/wallpaper/video 导出", + ).toBe("function"); + expect( + typeof mod.transcodeForAerialNative, + "契约函数 transcodeForAerialNative 未由 lib/wallpaper/video 导出", + ).toBe("function"); + transcodeForGallery = mod.transcodeForGallery as typeof transcodeForGallery; + transcodeForAerialNative = mod.transcodeForAerialNative as typeof transcodeForAerialNative; +}, 60000); + +afterAll(() => { + if (tmpRoot) fs.rmSync(tmpRoot, { recursive: true, force: true }); +}); + +// ============================================================================ +// 场景 3.P1:兼容窗口画布输入 → Aerial 转码段产出 1920×1080 mov +// ============================================================================ + +describe("场景 3.P1:1312×864(3:2 原生画布,窗口内)→ transcodeForAerialNative 产出 1920×1080 mov", () => { + it("产物 width==1920 && height==1080 && format contains mov ∧ hvc1 ∧ 音轨保留 ∧ faststart", async () => { + const dst = path.join(tmpRoot, "2026-09-20_landscape.mov"); + // 音轨保留语义 = pass-through:输入必须自带音轨(生产输入 mmh3turbo 产物自带音轨) + await transcodeForAerialNative(canvasMaster, dst); + + expect(fs.existsSync(dst), `Aerial 转码产物不存在: ${dst}`).toBe(true); + expect(fs.statSync(dst).size).toBeGreaterThan(0); + + const p = probe(dst); + // 谓词字面量:width==1920 && height==1080 && format contains mov + expect(p.width, `宽度必须 1920,实际 ${p.width}`).toBe(1920); + expect(p.height, `高度必须 1080,实际 ${p.height}`).toBe(1080); + expect(p.formatName, `容器必须 mov,实际 ${p.formatName}`).toContain("mov"); + expect(p.codecTag, `tag 必须 hvc1,实际 ${p.codecTag}`).toBe("hvc1"); + expect(p.audioStreams, "必须保留音轨").toBe(1); + expect(p.audioCodec).toBe("aac"); + expect(moovBeforeMdat(dst), "mov 必须 +faststart(moov 在 mdat 之前)").toBe(true); + }, 600000); +}); + +// ============================================================================ +// 场景 3.P2:绝不拉伸(像素级正方形标记不变形) +// ============================================================================ + +describe("场景 3.P2:转码 filter 链无非等比形变(1312×864 → 1920×1080 正方形标记保持)", () => { + it("中心红方块转码后 bounding box 接近正方形(直接拉伸突变体会呈 439×375 → 红)", async () => { + const dst = path.join(tmpRoot, "distortion-check.mov"); + await transcodeForAerialNative(markerSrc, dst); + + const W = 1920; + const H = 1080; + const raw = firstFrameRaw(dst, W, H); + + // 红像素 bounding box(阈值放宽容忍 h264 色度压缩) + let minX = W; + let minY = H; + let maxX = -1; + let maxY = -1; + for (let y = 0; y < H; y++) { + for (let x = 0; x < W; x++) { + const p = pixelAt(raw, W, x, y); + if (p.r > 150 && p.g < 90 && p.b < 90) { + if (x < minX) minX = x; + if (x > maxX) maxX = x; + if (y < minY) minY = y; + if (y > maxY) maxY = y; + } + } + } + expect(maxX, "产物首帧必须能找到红色标记(标记丢失=裁切错位)").toBeGreaterThanOrEqual(0); + const bw = maxX - minX + 1; + const bh = maxY - minY + 1; + + // 统一缩放(crop 前置 + 等比 scale):300×300 → ≈439×439; + // 跳过 crop 直接拉伸:→ 439×375(宽高差 ≈15%)。容差 6% 分离两种实现。 + const maxSide = Math.max(bw, bh); + const asym = Math.abs(bw - bh) / maxSide; + expect( + asym, + `标记必须保持正方形(绝不拉伸):bounding box ${bw}x${bh},不对称率 ${(asym * 100).toFixed(1)}% 超容差 6%`, + ).toBeLessThanOrEqual(0.06); + // 标记确实被等比放大(≈×1.4634 → 439 上下),排除「原样 copy 无转码」假阳性 + expect(bw).toBeGreaterThan(380); + expect(bh).toBeGreaterThan(380); + }, 600000); +}); + +// ============================================================================ +// 场景 3.P4:裁切面积占比 ≤ 0.15 且居中(行灰度梯度反演) +// ============================================================================ + +describe("场景 3.P4:3:2 → 16:9 裁切面积占比 ≤ 0.15 且为居中裁切", () => { + it("梯度源反演存活行带:cropFrac ≤ 0.15、带顶 ≈63±6、带底距底 ≈63±6", async () => { + const dst = path.join(tmpRoot, "crop-ratio-check.mov"); + await transcodeForAerialNative(gradientSrc, dst); + + const W = 1920; + const H = 1080; + const raw = firstFrameRaw(dst, W, H); + const SRC_H = 864; + const x = Math.floor(W / 2); + + // 中心列每行灰度 → 反演源行号(梯度 y → gray=round(y*255/863);反演 y=gray*863/255) + const mappedRows: number[] = []; + for (let y = 0; y < H; y++) { + const g = pixelAt(raw, W, x, y).g; + mappedRows.push(Math.round((g * (SRC_H - 1)) / 255)); + } + // 最小 16:9 居中裁切保留源行 [63, 800](738 行)。h264 边缘模糊容差 ±3。 + const yTop = Math.min(...mappedRows); + const yBottom = Math.max(...mappedRows); + const bandRows = yBottom - yTop + 1; + const cropFrac = 1 - bandRows / SRC_H; + + expect( + cropFrac, + `裁切面积占比 ${(cropFrac * 100).toFixed(2)}% 必须 ≤ 15%(存活源行带 ${yTop}..${yBottom},${bandRows}/${SRC_H} 行)`, + ).toBeLessThanOrEqual(0.15); + + // D2「居中裁到 16:9」:带顶 ≈ (864-738)/2 = 63,带底距底 ≈ 63 + // 容差 ±10:min/max 反演受压缩噪声偏差 ~±7 行,防假红 + expect(yTop, `裁切带顶部应 ≈63(居中),实际 ${yTop}`).toBeGreaterThanOrEqual(53); + expect(yTop).toBeLessThanOrEqual(73); + expect( + SRC_H - 1 - yBottom, + `裁切带底部距底应 ≈63(居中),实际 ${SRC_H - 1 - yBottom}`, + ).toBeGreaterThanOrEqual(53); + expect(SRC_H - 1 - yBottom).toBeLessThanOrEqual(73); + }, 600000); +}); + +// ============================================================================ +// 转码A(画廊原生腿):无 -vf 尺寸透传 → 画布尺寸 invariant + v2 容器/编码 invariant +// ============================================================================ + +describe("转码A:transcodeForGallery 1312×864 原生画布输入 → 产物 == 画布尺寸(probe 断言 == 画布的放行面)", () => { + it("产物 1312×864 ∧ mp4 ∧ h264 ∧ aac 立体声 ∧ faststart(场景 5.P3 native 腿放行面的真实产物形态)", async () => { + const dst = path.join(tmpRoot, "2026-09-20_native.mp4"); + await transcodeForGallery(canvasMaster, dst); + + expect(fs.existsSync(dst)).toBe(true); + expect(fs.statSync(dst).size).toBeGreaterThan(0); + + const p = probe(dst); + // 画布尺寸透传 invariant(无 -vf 尺寸操作):产物 == 源 == 画布 + expect(p.width, `宽度必须 1312(画布透传),实际 ${p.width}`).toBe(1312); + expect(p.height, `高度必须 864(画布透传),实际 ${p.height}`).toBe(864); + expect(p.formatName).toContain("mp4"); + expect(p.videoCodec).toBe("h264"); + expect(p.audioStreams).toBe(1); + expect(p.audioCodec).toBe("aac"); + expect(p.audioChannels).toBe(2); + expect(moovBeforeMdat(dst), "mp4 必须 +faststart").toBe(true); + }, 120000); +}); diff --git a/apps/backend/src/__tests__/wallpaper-video-oriented-dims.test.ts b/apps/backend/src/__tests__/wallpaper-video-oriented-dims.test.ts new file mode 100644 index 0000000..02965ac --- /dev/null +++ b/apps/backend/src/__tests__/wallpaper-video-oriented-dims.test.ts @@ -0,0 +1,61 @@ +/** + * 单元测试:readOrientedDimensions 的 EXIF orientation 宽高互换。 + * + * 背景(QA 审查 Important 缺口):iPhone 竖拍(orientation 5-8)是照片库主场景, + * 画布朝向判定(computeNativeCanvas 的 landscape 分支)与预裁剪都吃这个函数的输出—— + * 若互换逻辑回归会静默产出横版画布。此前全部 job/acceptance 测试 mock 该函数,零直测。 + * + * 真实 sharp + 真实 JPEG orientation 标签(像素不旋转、仅元数据),无任何 mock。 + */ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import sharp from "sharp"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; + +let tmpRoot = ""; + +beforeAll(() => { + tmpRoot = fs.mkdtempSync(path.join(os.homedir(), ".relight-test-orient-")); +}); + +afterAll(() => { + if (tmpRoot) fs.rmSync(tmpRoot, { recursive: true, force: true }); +}); + +describe("readOrientedDimensions:EXIF orientation 5-8 宽高互换(iPhone 竖拍主场景)", () => { + it("orientation=6(90° CW 旋转)→ 300×200 像素报为 200×300", async () => { + const p = path.join(tmpRoot, "orient6.jpg"); + await sharp({ create: { width: 300, height: 200, channels: 3, background: "#2288cc" } }) + .withMetadata({ orientation: 6 }) + .jpeg() + .toFile(p); + const meta = await sharp(p).metadata(); + expect(meta.orientation, "fixture 必须带 orientation=6").toBe(6); + + const { readOrientedDimensions } = await import("../lib/wallpaper/video"); + expect(await readOrientedDimensions(p)).toEqual({ width: 200, height: 300 }); + }); + + it("orientation=8(270°)→ 同样互换", async () => { + const p = path.join(tmpRoot, "orient8.jpg"); + await sharp({ create: { width: 400, height: 100, channels: 3, background: "#22cc88" } }) + .withMetadata({ orientation: 8 }) + .jpeg() + .toFile(p); + + const { readOrientedDimensions } = await import("../lib/wallpaper/video"); + expect(await readOrientedDimensions(p)).toEqual({ width: 100, height: 400 }); + }); + + it("orientation=1(无旋转,横拍)→ 原样返回", async () => { + const p = path.join(tmpRoot, "orient1.jpg"); + await sharp({ create: { width: 640, height: 480, channels: 3, background: "#cc8822" } }) + .withMetadata({ orientation: 1 }) + .jpeg() + .toFile(p); + + const { readOrientedDimensions } = await import("../lib/wallpaper/video"); + expect(await readOrientedDimensions(p)).toEqual({ width: 640, height: 480 }); + }); +}); diff --git a/apps/backend/src/__tests__/wallpaper-video-overlay-realprocess.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-overlay-realprocess.acceptance.test.ts index 71694bc..2f81fcb 100644 --- a/apps/backend/src/__tests__/wallpaper-video-overlay-realprocess.acceptance.test.ts +++ b/apps/backend/src/__tests__/wallpaper-video-overlay-realprocess.acceptance.test.ts @@ -11,11 +11,11 @@ * * 验收点(round 2 编排器):真实渲染冒烟放独立 real-process 文件 + 超小输入(2s 样片)。 * mock 面(错误枚举 / 调用形态)见 wallpaper-video-overlay.acceptance.test.ts。 - * 输入用生产画布 1280×704(边界值【v2】横版画布,honeydo 720p 母版形态)× 2s—— - * 超小体现在时长(24fps × 2s = 48 帧);分辨率必须用生产画布: - * 实测实现按 1280×704 固定画布渲染(喂 256p 样片产出仍 1280×704,同分辨率 invariant - * 对非生产画布输入不成立——已作为观察项报告 QA;生产链路母版恒为 1280×704, - * invariant 在生产包络内成立)。 + * 输入用 1280×704(honeydo 720p 母版形态)× 2s——超小体现在时长(24fps × 2s = 48 帧)。 + * 【20260928 单腿原生参数化】comp 尺寸经 props.canvasWidth/canvasHeight + Root.tsx + * calculateMetadata 动态化:传画布 1280×704 → 产出 1280×704(同分辨率 invariant 回归 + * 「输入==画布==产出」);另以非默认 4:3 档画布(1248×928,computeNativeCanvas(4,3) 参考档) + * 真实渲染一例,断言产出 == 参数化 W×H(kill 尺寸写死 No-op,场景 9.P1 形态)。 * * 环境前提(§后端设计 §1【v2】——文字层工程是 v2 交付物的一部分): * {config.videoWorkspacePath}/wallpaper-overlay 工程必须存在;缺失即真红(禁宽容跳过)。 @@ -51,7 +51,14 @@ let tmpRoot = ""; let inputVideo = ""; let renderTextOverlay: ( videoPath: string, - meta: { pickDate: string; title: string; narrative: string }, + meta: { + pickDate: string; + title: string; + narrative: string; + takenAt?: string | null; + canvasWidth: number; + canvasHeight: number; + }, ) => Promise<{ overlaidPath: string }>; interface ProbeResult { @@ -184,6 +191,9 @@ dOverlay("【v2】renderTextOverlay 产物 invariant(真实 Remotion 渲染, pickDate: "2026-09-12", title: "金色黄昏", narrative: "五年前的今天,你在海边捕捉到了这张温暖的照片。夕阳染成金橙色,海浪轻抚沙滩。", + // 20260928:生成画布透传(== 输入分辨率;calculateMetadata 按此定 comp 尺寸) + canvasWidth: 1280, + canvasHeight: 704, }); // 返回契约 + 产物存在 @@ -198,13 +208,14 @@ dOverlay("【v2】renderTextOverlay 产物 invariant(真实 Remotion 渲染, const head = fs.readFileSync(res.overlaidPath).subarray(0, 64).toString("latin1"); expect(head, "成品必须是 mp4 封装(文件头含 ftyp)").toContain("ftyp"); - // 契约(2026-09-13 v3 修订):横版画布升 1920×1080(Aerial 原生 16:9), - // 输入 1280×704(20:11) 在两栏画布内按 contain 重排版 → 成品 1920×1080; - // 帧率仍与输入同源。 + // 契约(20260928 单腿原生参数化):comp 尺寸 == props 画布(1280×704 == 输入分辨率)—— + // 同分辨率 invariant 回归「输入==画布==产出」;帧率仍与输入同源。 const inProbe = probeVideo(inputVideo); const outProbe = probeVideo(res.overlaidPath); - expect(outProbe.width, "横版成品宽度必须 1920(两栏画布)").toBe(1920); - expect(outProbe.height, "横版成品高度必须 1080").toBe(1080); + expect(outProbe.width, "成品宽度必须 == props 画布 1280(calculateMetadata 参数化)").toBe( + 1280, + ); + expect(outProbe.height, "成品高度必须 == props 画布 704").toBe(704); expect( outProbe.rFrameRate, `成品帧率 ${outProbe.rFrameRate} ≠ 输入帧率 ${inProbe.rFrameRate}`, @@ -214,11 +225,28 @@ dOverlay("【v2】renderTextOverlay 产物 invariant(真实 Remotion 渲染, const inFrame = firstFrameRaw(inputVideo); const outFrame = firstFrameRaw(res.overlaidPath); expect(outFrame.length, "成品首帧 raw 尺寸与画布不一致(分辨率/像素格式漂移)").toBe( - 1920 * 1080 * 3, + 1280 * 704 * 3, ); expect( inFrame.equals(outFrame), "成品首帧与输入首帧逐像素相同——文字层不存在(首帧像素差异必须 >0)", ).toBe(false); }, 900000); + + it("场景 9.P1 形态:非默认 4:3 档画布(1248×928)真实渲染 → 产物尺寸 == 参数化 W×H(kill 尺寸写死)", async () => { + const res = await renderTextOverlay(inputVideo, { + pickDate: "2026-09-12", + title: "金色黄昏", + narrative: "五年前的今天,你在海边捕捉到了这张温暖的照片。", + // computeNativeCanvas(4,3) 参考档:非 16:9、非历史默认档 + canvasWidth: 1248, + canvasHeight: 928, + }); + expect(fs.existsSync(res.overlaidPath), `叠字成品不存在: ${res.overlaidPath}`).toBe(true); + const outProbe = probeVideo(res.overlaidPath); + expect(outProbe.width, "产物宽度必须 == 参数化画布 1248").toBe(1248); + expect(outProbe.height, "产物高度必须 == 参数化画布 928").toBe(928); + const head = fs.readFileSync(res.overlaidPath).subarray(0, 64).toString("latin1"); + expect(head).toContain("ftyp"); + }, 900000); }); diff --git a/apps/backend/src/__tests__/wallpaper-video-overlay.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-overlay.acceptance.test.ts index 322913d..ad15eca 100644 --- a/apps/backend/src/__tests__/wallpaper-video-overlay.acceptance.test.ts +++ b/apps/backend/src/__tests__/wallpaper-video-overlay.acceptance.test.ts @@ -3,13 +3,14 @@ import { spawnSync } from "node:child_process"; * 验收测试(红队):动态视频壁纸 — renderTextOverlay / 双转码 spawn 调用契约【v2 增量】(mock 面) * * 设计文档(state.md)对应契约(§契约规约 计算/spawn 契约【v2】逐字): - * - renderTextOverlay(videoPath, meta: {pickDate,title,narrative}) → {overlaidPath} + * - renderTextOverlay(videoPath, meta: {pickDate,title,narrative,takenAt?,canvasWidth, + * canvasHeight}) → {overlaidPath}(20260928 props 纯超集扩展 + comp 按画布比例选) * - 错误枚举:OverlayRenderError(remotion 非零退出 / 产物缺失 / 超时 900s(v2.1)) * - §后端设计 §1【v2】:videoWorkspacePath 下新增 wallpaper-overlay/ 工程, * 直接 `npx remotion render`(无 AI、确定性渲染) * - transcodeForGallery【v2】:保留音轨 `-c:v libx264 -crf 18 -c:a aac -b:a 128k * -movflags +faststart`(画廊静音自动播放 + 点击开声),超时 120s - * - transcodeForAerial:`-vf scale=1920:1080:flags=lanczos -c:v hevc_videotoolbox + * - transcodeForAerialNative(20260928 单腿条件产出):`-vf crop(居中裁16:9)+scale=1920:1080:flags=lanczos+setsar=1 -c:v hevc_videotoolbox * -b:v 8M -tag:v hvc1 -an -movflags +faststart`(hevc_videotoolbox 失败 fallback * `-c:v libx265 -crf 22 -tag:v hvc1`),超时 600s(Aerial 转码未改,v2.1 只调 renderTextOverlay 900s) * @@ -209,10 +210,17 @@ function flagValue(args: string[], flag: string): string | null { let tmpRoot = ""; let renderTextOverlay: ( videoPath: string, - meta: { pickDate: string; title: string; narrative: string }, + meta: { + pickDate: string; + title: string; + narrative: string; + takenAt?: string | null; + canvasWidth: number; + canvasHeight: number; + }, ) => Promise<{ overlaidPath: string }>; let transcodeForGallery: (src: string, dst: string) => Promise; -let transcodeForAerial: (src: string, dst: string) => Promise; +let transcodeForAerialNative: (src: string, dst: string) => Promise; let overlaySrc = ""; let gallerySrc = ""; let aerialSrc = ""; @@ -299,10 +307,12 @@ beforeAll(async () => { "契约函数 renderTextOverlay 未由 lib/wallpaper/video 导出(§契约规约【v2】)", ).toBe("function"); expect(typeof mod.transcodeForGallery, "契约函数 transcodeForGallery 未导出").toBe("function"); - expect(typeof mod.transcodeForAerial, "契约函数 transcodeForAerial 未导出").toBe("function"); + expect(typeof mod.transcodeForAerialNative, "契约函数 transcodeForAerialNative 未导出").toBe( + "function", + ); renderTextOverlay = mod.renderTextOverlay as typeof renderTextOverlay; transcodeForGallery = mod.transcodeForGallery as typeof transcodeForGallery; - transcodeForAerial = mod.transcodeForAerial as typeof transcodeForAerial; + transcodeForAerialNative = mod.transcodeForAerialNative as typeof transcodeForAerialNative; }, 60000); beforeEach(() => { @@ -335,6 +345,9 @@ describe("【v2】renderTextOverlay spawn 调用契约(mock spawn;§后端 pickDate: "2026-09-12", title: "金色黄昏", narrative: "五年前的今天,你在海边捕捉到了这张温暖的照片。", + // 20260928:生成画布透传(comp 选择 + calculateMetadata 尺寸依据) + canvasWidth: 1440, + canvasHeight: 800, }); // 返回契约:{overlaidPath}(产物必须真实存在于磁盘) @@ -372,6 +385,8 @@ describe("【v2】renderTextOverlay spawn 调用契约(mock spawn;§后端 pickDate: "2026-09-12", title: "金色黄昏", narrative: "narrative", + canvasWidth: 1440, + canvasHeight: 800, }); } catch (e) { caught = e; @@ -389,6 +404,8 @@ describe("【v2】renderTextOverlay spawn 调用契约(mock spawn;§后端 pickDate: "2026-09-12", title: "金色黄昏", narrative: "narrative", + canvasWidth: 1440, + canvasHeight: 800, }); } catch (e) { caught = e; @@ -424,11 +441,11 @@ describe("【v2】transcodeForGallery spawn argv 契约(保留音轨:libx264 }, 30000); }); -describe("transcodeForAerial spawn argv 契约(2026-09-13 起:scale 1920×1080/hvc1/带音轨 aac/faststart)", () => { - it("argv 含 -vf scale=1920:1080 ∧ -tag:v hvc1 ∧ -c:a aac ∧ -movflags +faststart(无 -an);-c:v ∈ {hevc_videotoolbox, libx265}", async () => { +describe("transcodeForAerialNative spawn argv 契约(20260928:crop 前置 16:9 + scale 1920×1080/hvc1/带音轨 aac/faststart)", () => { + it("argv 含 -vf crop(16:9)+scale=1920:1080 ∧ -tag:v hvc1 ∧ -c:a aac ∧ -movflags +faststart(无 -an);-c:v ∈ {hevc_videotoolbox, libx265}", async () => { const dst = path.join(tmpRoot, "argv-aerial.mov"); registeredOutputs.set(dst, aerialSrc); - await transcodeForAerial(aerialSrc, dst); + await transcodeForAerialNative(aerialSrc, dst); const call = spawnCalls.find((c) => c.args.includes(dst)); expect( @@ -437,10 +454,18 @@ describe("transcodeForAerial spawn argv 契约(2026-09-13 起:scale 1920×10 ).toBeTruthy(); const args = call?.args ?? []; expect(args).toContain(aerialSrc); - // 契约逐字:-vf scale=1920:1080:flags=lanczos + // 契约逐字:-vf crop 前置(居中裁恰 16:9,绝不拉伸)+ scale=1920:1080 后置 + setsar=1 const vf = flagValue(args, "-vf"); expect(vf, "-vf 必须存在").not.toBeNull(); - expect(vf ?? "", "-vf 必须含 scale=1920:1080").toContain("scale=1920:1080"); + const vfStr = vf ?? ""; + expect(vfStr, "-vf 必须含 scale=1920:1080").toContain("scale=1920:1080"); + expect(vfStr, "-vf 必须含居中 16:9 crop 前置").toContain( + "crop=w='min(iw,ih*16/9)':h='min(ih,iw*9/16)'", + ); + expect(vfStr, "-vf 必须 setsar=1(方像素)").toContain("setsar=1"); + expect(vfStr.indexOf("crop="), "crop 必须先于 scale(场景 3.P2)").toBeLessThan( + vfStr.indexOf("scale="), + ); // 契约逐字:-tag:v hvc1 expect(flagValue(args, "-tag:v"), "-tag:v 必须为 hvc1").toBe("hvc1"); // 契约逐字(2026-09-13 验收反转):不再 -an,保留音轨(aac 128k) diff --git a/apps/backend/src/__tests__/wallpaper-video-overlay.test.ts b/apps/backend/src/__tests__/wallpaper-video-overlay.test.ts index 3cde057..99565e5 100644 --- a/apps/backend/src/__tests__/wallpaper-video-overlay.test.ts +++ b/apps/backend/src/__tests__/wallpaper-video-overlay.test.ts @@ -1,9 +1,13 @@ /** * 单测:lib/wallpaper/video.ts — renderTextOverlay + assertVideoSpawnPrerequisites - * (v2 增量任务 14:Remotion 文字层) + * (v2 增量任务 14:Remotion 文字层;20260928 单腿原生:props 扩展 + comp 按画布比例选) * - * 契约(state.md ## 契约规约 计算/spawn 契约): - * renderTextOverlay(videoPath, meta: {pickDate,title,narrative,takenAt?}) → {overlaidPath} + * 契约(state.md ## 契约规约 计算/spawn 契约;20260928 契约 7 props 纯超集扩展): + * renderTextOverlay(videoPath, meta: {pickDate,title,narrative,takenAt?,canvasWidth,canvasHeight}) + * → {overlaidPath} + * comp 选择按 meta.canvasWidth/canvasHeight 比例(≥0.9 → landscape 两栏 / <0.9 → portrait + * 全屏),不再按输入视频探测尺寸;props 透传 canvasWidth/canvasHeight(Root.tsx + * calculateMetadata 动态定 comp 尺寸) * spawn `npx remotion render`(cwd=wallpaper-overlay 工程、npx 绝对路径、 * AbortController 600s 超时、stdout tail 留证) * 错误枚举 OverlayRenderError:工程缺失 / Remotion 运行时缺失(不自动安装)/ @@ -179,6 +183,9 @@ const META = { title: "巷口的猫", narrative: "午后的光落在墙沿。", takenAt: "2016-07-18T14:35:53.000Z", + // 20260928:生成画布(704×1216 竖版样片对应的画布档;比例 0.579 <0.9 → portrait comp) + canvasWidth: 704, + canvasHeight: 1216, }; describe("renderTextOverlay(mock spawn)", () => { @@ -197,7 +204,8 @@ describe("renderTextOverlay(mock spawn)", () => { // 竖版输入 → portrait composition expect(args[3]).toBe("wallpaper-overlay-portrait"); expect(args[4]).toMatch(/input-portrait-overlay\.mp4$/); - // props 契约:{videoPath, pickDate, title, narrative, captureDateline} + // props 契约(20260928 契约 7 纯超集扩展):{videoPath, pickDate, title, narrative, + // captureDateline, canvasWidth, canvasHeight} const propsIdx = args.indexOf("--props"); const props = JSON.parse(args[propsIdx + 1] ?? "{}") as Record; expect(props).toEqual({ @@ -206,6 +214,8 @@ describe("renderTextOverlay(mock spawn)", () => { title: META.title, narrative: META.narrative, captureDateline: buildCaptureDateline(META.takenAt), + canvasWidth: 704, + canvasHeight: 1216, }); // frames = ceil(duration × fps)(0.5s × 24fps = 12 帧 → 0-11) const framesIdx = args.indexOf("--frames"); @@ -224,12 +234,31 @@ describe("renderTextOverlay(mock spawn)", () => { expect(res.overlaidPath).toMatch(/input-portrait-overlay\.mp4$/); }); - it("横版输入 → landscape composition", async () => { - await renderTextOverlay(`${holder.tmpDir}/input-landscape.mp4`, META); + it("画布比例 ≥0.9 → landscape composition(20260928:按 canvas props 而非输入视频探测)", async () => { + await renderTextOverlay(`${holder.tmpDir}/input-landscape.mp4`, { + ...META, + canvasWidth: 1440, + canvasHeight: 800, + }); const args = holder.spawnCalls[0]?.args ?? []; expect(args[3]).toBe("wallpaper-overlay-landscape"); }); + it("comp 选择只看画布比例:竖版画布 + 横版视频输入 → 仍 portrait comp(kill 输入探测 No-op)", async () => { + await renderTextOverlay(`${holder.tmpDir}/input-landscape.mp4`, { + ...META, + canvasWidth: 800, + canvasHeight: 1440, + }); + const args = holder.spawnCalls[0]?.args ?? []; + expect(args[3]).toBe("wallpaper-overlay-portrait"); + // props 携带的是画布(非输入视频)尺寸 + const propsIdx = args.indexOf("--props"); + const props = JSON.parse(args[propsIdx + 1] ?? "{}") as Record; + expect(props.canvasWidth).toBe(800); + expect(props.canvasHeight).toBe(1440); + }); + it("takenAt 缺失 → props.captureDateline 为 null(footer 不渲染契约)", async () => { await renderTextOverlay(`${holder.tmpDir}/input-portrait.mp4`, { ...META, diff --git a/apps/backend/src/__tests__/wallpaper-video-portrait-canvas.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-portrait-canvas.acceptance.test.ts index e7701a4..16e3636 100644 --- a/apps/backend/src/__tests__/wallpaper-video-portrait-canvas.acceptance.test.ts +++ b/apps/backend/src/__tests__/wallpaper-video-portrait-canvas.acceptance.test.ts @@ -1,27 +1,25 @@ /** - * 验收测试(红队):竖版成片画布比例与手机展示比例对齐(场景 2) + * 验收测试(红队):壁纸视频产物画布契约【20260928 单腿原生改版——native 版】 * - * 设计文档(state.md)对应谓词: - * - 2.P1 [det-machine]:竖版产物宽高比落在 [0.44, 0.48] 且与静态竖版壁纸宽高比偏差 ≤ 5% - * assert: 0.44 <= (w/h)_video <= 0.48 && abs((w/h)_video - (w/h)_static)/(w/h)_static <= 0.05 - * driver: node-script 本地 ffprobe 尺寸探测(无网络) - * - 2.P2 [det-machine]:竖版产物保持手机全屏清晰度 - * assert: width >= 640 && height >= 1280 - * - 2.P3 [real-process]:竖版产物为浏览器可直接播放、首帧前可解码、保留音轨的 mp4 + * 设计文档(state.md ## 验收场景)对应谓词(QA 真机绑定求值): + * - 场景 1.P2 [det-machine]:生成画布跟随原图比例(32 倍数 + + * abs(h - w*srcH/srcW) <= 32)| driver: 本地 ffprobe 尺寸探测 + * - 场景 1.P3 [det-machine]:width*height <= 1177600 && >= 1177600/2 + * - 场景 2.P3 形态(沿用):产物为浏览器可直接播放、首帧前可解码、保留音轨的 mp4 * assert: video_codec=="h264" && has_audio_stream==true && moov_before_mdat==true * && http_status in {200,206} - * driver: ffprobe 流信息 + 一次真实 HTTP Range 请求 + * - 历史竖版 2.P1(比例对齐 1290×2796 静态壁纸)随固定竖版画布一并退役——原生画布 + * 比例跟随原图,不再有统一竖版档 * * QA 真机绑定项(信息隔离下只声明类别,求值时以 env 绑定,绑定缺失留 artifact + 可见 skip, * 绑定存在但资源缺失/断言失败一律真红): - * - WVQA_PORTRAIT_MP4 重跑后的竖版产物落盘路径 - * - WVQA_LANDSCAPE_MOV 重跑后的横版产物落盘路径(旁证,可选) - * - WVQA_STATIC_PORTRAIT_JPG 静态竖版壁纸路径(比例锚点;缺省用设计锚 1290×2796) - * - WVQA_PORTRAIT_URL 竖版产物公网地址(2.P3 HTTP Range;缺省回退 DB 回执列,见姊妹文件) - * - WVQA_DB_PATH SQLite 路径(用于从 daily_picks.wallpaper_video_portrait_url 派生公网地址) + * - WVQA_NATIVE_MP4 重跑后的 native 产物落盘路径 + * - WVQA_SOURCE_W/H hero 原图尺寸(1.P2 比例跟随断言用;未绑定则该谓词跳过并留痕) + * - WVQA_NATIVE_URL native 产物公网地址(HTTP 面;缺省回退 DB 回执列派生) + * - WVQA_DB_PATH SQLite 路径(从 daily_picks.wallpaper_video_native_url 派生公网地址) * - * 假绿防护(harness 自检,始终真跑):用 ffmpeg 合成正/负对照验证测量函数的判别力—— - * faststart 与非 faststart 产物 moov 顺序必须被正确区分(kill moov 解析 no-op)。 + * 假绿防护(harness 自检,始终真跑):ffmpeg 合成正/负对照验证测量函数判别力—— + * faststart 与非 faststart 产物 moov 顺序必须被正确区分;预算数学方向正确(1920×1080 超预算)。 */ import { execFileSync, spawnSync } from "node:child_process"; import fs from "node:fs"; @@ -31,11 +29,15 @@ import Database from "better-sqlite3"; import { describe, expect, it } from "vitest"; const ARTIFACT_DIR = "/tmp/autopilot-artifacts"; -const PORTRAIT_MP4 = process.env.WVQA_PORTRAIT_MP4 ?? ""; -const STATIC_JPG = process.env.WVQA_STATIC_PORTRAIT_JPG ?? ""; -const PORTRAIT_URL = process.env.WVQA_PORTRAIT_URL ?? ""; +const NATIVE_MP4 = process.env.WVQA_NATIVE_MP4 ?? ""; +const SOURCE_W = Number(process.env.WVQA_SOURCE_W ?? ""); +const SOURCE_H = Number(process.env.WVQA_SOURCE_H ?? ""); +const NATIVE_URL = process.env.WVQA_NATIVE_URL ?? ""; const DB_PATH = process.env.WVQA_DB_PATH ?? process.env.DATABASE_PATH ?? ""; +/** 像素预算(## 契约规约 实现绑定表逐字:pixel_budget = 1177600,下限语义 ≥ budget/2) */ +const PIXEL_BUDGET = 1_177_600; + function writeArtifact(id: string, content: string): void { fs.mkdirSync(ARTIFACT_DIR, { recursive: true }); fs.writeFileSync(path.join(ARTIFACT_DIR, `${id}.out`), content); @@ -114,81 +116,70 @@ function topLevelBoxOffsets(file: string): { moov: number; mdat: number } { } } -function ratioDeviation(a: number, b: number): number { - return Math.abs(a - b) / b; -} - // ---- QA 绑定评估:绑定缺失 → 留 artifact + 可见 skip(绑定存在即真红铁律生效)---- -const PORTRAIT_BOUND = PORTRAIT_MP4.trim().length > 0; -if (!PORTRAIT_BOUND) { +const NATIVE_BOUND = NATIVE_MP4.trim().length > 0; +if (!NATIVE_BOUND) { writeArtifact( - "场景2-跳过原因", - "WVQA_PORTRAIT_MP4 未绑定——重跑产物尚未产出(场景 2 谓词需先真跑一次壁纸视频重跑)。" + - "求值时绑定 env WVQA_PORTRAIT_MP4(可加 WVQA_STATIC_PORTRAIT_JPG / WVQA_PORTRAIT_URL / WVQA_DB_PATH)后重跑本套件;不得静默置 PASS。", + "场景1native-跳过原因", + "WVQA_NATIVE_MP4 未绑定——重跑产物尚未产出(native 谓词需先真跑一次壁纸视频重跑)。" + + "求值时绑定 env WVQA_NATIVE_MP4(可加 WVQA_SOURCE_W / WVQA_SOURCE_H / WVQA_NATIVE_URL / WVQA_DB_PATH)后重跑本套件;不得静默置 PASS。", ); console.warn( - "[portrait-canvas] WVQA_PORTRAIT_MP4 未绑定——场景 2 谓词 skip(留 artifact 场景2-跳过原因)", + "[native-canvas] WVQA_NATIVE_MP4 未绑定——native 产物谓词 skip(留 artifact 场景1native-跳过原因)", ); } -const dProduct = PORTRAIT_BOUND ? describe : describe.skip; +const dProduct = NATIVE_BOUND ? describe : describe.skip; -dProduct("场景 2.P1/2.P2:竖版产物画布比例与清晰度(本地 ffprobe)", () => { - const product = path.resolve(PORTRAIT_MP4); +dProduct("场景 1.P2/1.P3:native 产物画布跟随原图比例 + 像素预算(本地 ffprobe)", () => { + const product = path.resolve(NATIVE_MP4); it("产物文件存在(绑定路径缺失即真红)", () => { - expect(fs.existsSync(product), `竖版产物不存在: ${product}`).toBe(true); + expect(fs.existsSync(product), `native 产物不存在: ${product}`).toBe(true); }); - it("2.P1 宽高比 ∈ [0.44, 0.48] 且与静态竖版壁纸比例偏差 ≤ 5%", () => { + it("1.P3 像素预算:width*height ∈ [1177600/2, 1177600]", () => { const info = ffprobeStreams(product); - const ar = info.width / info.height; - // 静态锚点:QA 绑定文件优先,缺省用设计锚 1290×2796(§改动 A 逐字) - let staticAr = 1290 / 2796; - let staticSource = "设计锚 1290×2796"; - if (STATIC_JPG.trim()) { - const staticInfo = ffprobeStreams(path.resolve(STATIC_JPG)); - staticAr = staticInfo.width / staticInfo.height; - staticSource = `WVQA_STATIC_PORTRAIT_JPG ${staticInfo.width}×${staticInfo.height}`; - } - const deviation = ratioDeviation(ar, staticAr); + const pixels = info.width * info.height; writeArtifact( - "场景2.P1", - JSON.stringify( - { - video: `${info.width}x${info.height}`, - ar, - staticAr, - staticSource, - deviation, - thresholds: { arRange: [0.44, 0.48], maxDeviation: 0.05 }, - }, - null, - 2, - ), + "场景1.P3", + JSON.stringify({ video: `${info.width}x${info.height}`, pixels, budget: PIXEL_BUDGET }), ); - expect(ar).toBeGreaterThanOrEqual(0.44); - expect(ar).toBeLessThanOrEqual(0.48); - expect(deviation).toBeLessThanOrEqual(0.05); + expect(pixels).toBeLessThanOrEqual(PIXEL_BUDGET); + expect(pixels).toBeGreaterThanOrEqual(PIXEL_BUDGET / 2); }); - it("2.P2 width >= 640 && height >= 1280", () => { + it("1.P2 画布跟随原图比例:w/h 均 32 倍数 ∧ abs(h - w*srcH/srcW) <= 32", () => { + expect( + Number.isFinite(SOURCE_W) && Number.isFinite(SOURCE_H) && SOURCE_W > 0 && SOURCE_H > 0, + "WVQA_SOURCE_W / WVQA_SOURCE_H 未绑定——1.P2 比例跟随面无法求值(留 skip 痕迹,不静默 PASS)", + ).toBe(true); const info = ffprobeStreams(product); - writeArtifact("场景2.P2", JSON.stringify({ width: info.width, height: info.height })); - expect(info.width).toBeGreaterThanOrEqual(640); - expect(info.height).toBeGreaterThanOrEqual(1280); + writeArtifact( + "场景1.P2", + JSON.stringify({ + video: `${info.width}x${info.height}`, + source: `${SOURCE_W}x${SOURCE_H}`, + mod32: [info.width % 32, info.height % 32], + expectedH: (info.width * SOURCE_H) / SOURCE_W, + }), + ); + expect(info.width % 32).toBe(0); + expect(info.height % 32).toBe(0); + const expectedH = (info.width * SOURCE_H) / SOURCE_W; + expect(Math.abs(info.height - expectedH)).toBeLessThanOrEqual(32); }); }); -// ---- 2.P3:编码/音轨/moov(本地 ffprobe)+ 真实 HTTP Range(公网地址)---- +// ---- 2.P3(沿用):编码/音轨/moov(本地 ffprobe)+ 真实 HTTP Range(公网地址)---- -function derivePortraitUrlFromDb(): string { +function deriveNativeUrlFromDb(): string { if (!DB_PATH.trim() || !fs.existsSync(DB_PATH)) return ""; const db = new Database(DB_PATH, { readonly: true, fileMustExist: true }); try { const row = db .prepare( - "SELECT wallpaper_video_portrait_url AS u FROM daily_picks ORDER BY pick_date DESC LIMIT 1", + "SELECT wallpaper_video_native_url AS u FROM daily_picks WHERE wallpaper_video_native_url IS NOT NULL ORDER BY pick_date DESC LIMIT 1", ) .get() as { u: string | null } | undefined; return row?.u ?? ""; @@ -197,8 +188,8 @@ function derivePortraitUrlFromDb(): string { } } -dProduct("场景 2.P3:竖版产物可播放性(h264 + 音轨 + moov 前置 + 真实 HTTP)", () => { - const product = path.resolve(PORTRAIT_MP4); +dProduct("场景 2.P3(沿用):native 产物可播放性(h264 + 音轨 + moov 前置 + 真实 HTTP)", () => { + const product = path.resolve(NATIVE_MP4); it("video_codec == h264 且保留音轨(本地 ffprobe 流信息)", () => { const info = ffprobeStreams(product); @@ -219,10 +210,10 @@ dProduct("场景 2.P3:竖版产物可播放性(h264 + 音轨 + moov 前置 + }); it("真实 HTTP Range 请求 → status ∈ {200, 206}", async () => { - const url = PORTRAIT_URL.trim() || derivePortraitUrlFromDb(); + const url = NATIVE_URL.trim() || deriveNativeUrlFromDb(); expect( url, - "WVQA_PORTRAIT_URL 未绑定且 DB 回执列无法派生公网地址——2.P3 HTTP 面无法求值(真红)", + "WVQA_NATIVE_URL 未绑定且 DB native 回执列无法派生公网地址——HTTP 面无法求值(真红)", ).not.toBe(""); const res = await fetch(url, { headers: { Range: "bytes=0-1023" } }); let snippet = ""; @@ -250,10 +241,10 @@ dProduct("场景 2.P3:竖版产物可播放性(h264 + 音轨 + moov 前置 + // harness 自检(始终真跑):测量函数判别力 —— kill no-op mutation // ============================================================================ -describe("harness 自检:ffprobe/moov 解析与比例数学的判别力", () => { +describe("harness 自检:ffprobe/moov 解析与预算数学的判别力", () => { let tmpRoot = ""; - const genVideo = (out: string, faststart: boolean): void => { + const genVideo = (out: string, faststart: boolean, size = "184x400"): void => { const args = [ "-y", "-loglevel", @@ -261,13 +252,13 @@ describe("harness 自检:ffprobe/moov 解析与比例数学的判别力", () = "-f", "lavfi", "-i", - "gradients=size=184x400:c0=0x224466:c1=0x886633", + `gradients=size=${size}:c0=0x224466:c1=0x886633`, "-loop", "1", "-t", "1", "-vf", - "scale=184:400", + `scale=${size.replace("x", ":")}`, "-frames:v", "24", "-c:v", @@ -302,10 +293,10 @@ describe("harness 自检:ffprobe/moov 解析与比例数学的判别力", () = expect(slowOffsets.moov).toBeGreaterThan(slowOffsets.mdat); }); - it("比例数学:0.46 通过、旧画布 0.579 被拒(偏差函数方向正确)", () => { - const staticAr = 1290 / 2796; - expect(ratioDeviation(736 / 1600, staticAr)).toBeLessThanOrEqual(0.05); - expect(ratioDeviation(704 / 1216, staticAr)).toBeGreaterThan(0.05); + it("预算数学方向:1056×1056(1:1 档)在预算带内;1920×1080 超预算(判别不恒真)", () => { + expect(1056 * 1056).toBeLessThanOrEqual(PIXEL_BUDGET); + expect(1056 * 1056).toBeGreaterThanOrEqual(PIXEL_BUDGET / 2); + expect(1920 * 1080).toBeGreaterThan(PIXEL_BUDGET); }); it("音轨探测:合成无声产物 hasAudio==false(探测器不做恒真)", () => { diff --git a/apps/backend/src/__tests__/wallpaper-video-preprocess.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-preprocess.acceptance.test.ts index 896b623..47b2de4 100644 --- a/apps/backend/src/__tests__/wallpaper-video-preprocess.acceptance.test.ts +++ b/apps/backend/src/__tests__/wallpaper-video-preprocess.acceptance.test.ts @@ -1,34 +1,22 @@ /** - * 验收测试(红队):动态视频壁纸 — preprocessHeroFrame 人脸构图裁剪【v2 增量】(像素级验证) + * 验收测试(红队):动态视频壁纸 — preprocessHeroFrame 预裁剪【20260928 单腿原生改版】(像素级验证) * - * 设计文档(state.md)对应契约: - * - §总体架构(v2)步骤 1:dailyPicks 取 hero 原图;人脸构图裁剪(faces bbox → 1/4 占比, - * 回退中心) - * - §后端设计 §3:preprocessHeroFrame(photoPath, width, height) → Promise: - * sharp cover 裁剪+resize 到目标画布比例,产 tmp png(防引擎拉伸变形); - * 【v2】升级人脸构图裁剪(faces bbox → ≥1/4 占比,缺失回退中心) - * - 边界值(2026-09-25 画布改版):横版画布 1280×704、竖版画布 736×1600(预裁剪按此比例 - * cover-crop,人脸占比 ≥1/4,faces bbox 缺失回退中心) - * - 验收点(round 2 编排器):有 faces bbox → crop 窗口含 bbox 中心且脸高 ≥ 画布 1/4 - * (对 preprocessHeroFrame 输出做像素级验证:以 bbox 相对位置断言); - * 无 bbox → 回退中心(现行为不变)。mock db 查询。 + * 设计文档(state.md ## 设计文档 D2 / ## 契约规约): + * - preprocessHeroFrame(photoPath, width, height) → Promise:sharp cover 微裁+resize + * 到目标画布比例,产 tmp png(防引擎 LANCZOS 拉伸变形——锚定帧直接拉伸到画布, + * 预裁比例必须精确等于画布比例) + * - 【20260928】**去除 v2 人脸窗口裁剪**:原生比例下构图已忠实,预裁仅做比例对齐与预算缩放, + * 统一中心 cover 微裁(人脸 bbox 仅用于 prompt 分层默认,job 层职责) + * - 画布由 computeNativeCanvas 按原图比例算出(本文件用 9:16 原生参考档 800×1440, + * design D1 参考输出逐字),驱动 preprocessHeroFrame 黑盒执行 * * 像素级验证方案(不读实现,纯几何 + 像素探针): - * 输入 2000×1000。bbox = [1300,300,400,400](中心 (1500,500))。marker:左半红 - * [1300,1500)×[300,700)、右半蓝 [1500,1700)×[300,700)。 - * cover-crop 到 736×1600 的窗口宽 = 1000×(736/1600) = 460(高全含)。 - * 红蓝两半同时可见 ⇔ 窗口左缘 L ∈ (1040,1500] ⇔ bbox 中心 1500 ∈ [L, L+460](数学蕴含)。 - * 脸高(marker 纵向跨度)在输出中 = 400×(1600/1000) = 640 ≥ 1600/4 = 400 - * (人脸构图窗口下限 = 画布高与全幅 cover 窗口高的较小值 = 1000,不超分放大)。 - * 回退中心:无 faces 行 → 现行为 sharp cover 中心裁剪(窗口 ≈ [770,1230]): - * 中心绿 marker [900,1100] 可见、左右边缘 marker([0,150] / [1850,2000])不可见。 + * 输入 2000×1000(比例 2.0)。cover 到 800×1440 的窗口宽 = 1000×(800/1440) ≈ 556 + * (高全含),中心窗 x ∈ [722,1278]: + * - 中心构图照片:中心绿 marker [900,1100) 必含;左缘红 [0,150) / 右缘蓝 [1850,2000) 必不含。 + * - 偏右脸照片(红蓝 marker [1300,1700)):中心裁剪**必不含**——若实现仍按人脸重构图 + * (窗口右移对齐脸心),红蓝 marker 即现形 → 本断言 kill 人脸窗口残留(20260928 直接命中)。 * - * db mock:真实 drizzle over 临时 sqlite(setupTestSchema 含 faces 表)——实现经 ../db - * 查 photos/faces 的任何 drizzle 查询形态都能命中,红队不读查询代码。 - * 【v2.1 仲裁留痕】契约裁定:人脸查询在 job 层(getLargestFaceBbox,lib 函数无 DB 依赖), - * preprocessHeroFrame 经 opts.faceBbox 接收坐标(state.md §契约规约 v2.1 签名声明)。 - * 本文件 bbox 用例改为按声明接口显式传 opts 驱动;db→getLargestFaceBbox→opts 的接线 - * 由 wallpaper-video-job.acceptance.test.ts 的 faceBbox 透传断言覆盖(mock 面)。 * 红队铁律:不读蓝队实现代码;不 skip、硬断言(sharp/依赖缺失即真红)。 */ import fs from "node:fs"; @@ -188,40 +176,20 @@ async function scanPixels(pngPath: string): Promise Promise; +let preprocessHeroFrame: (photoPath: string, width: number, height: number) => Promise; -/** 画布(边界值:竖版 736×1600,2026-09-25 画布改版) */ -const CANVAS_W = 736; -const CANVAS_H = 1600; -/** 脸高占比下限(契约逐字:人脸占比 ≥1/4 → 脸高 ≥ 画布高 1/4) */ -const FACE_HEIGHT_MIN = Math.ceil(CANVAS_H / 4); +/** 画布(design D1 参考输出逐字:9:16 原生档 → 800×1440) */ +const CANVAS_W = 800; +const CANVAS_H = 1440; -/** 种子:storage_sources + photos(file_path 指向 fixture 绝对路径)+ 可选 faces bbox 行 */ -function seedPhoto( - photoId: string, - photoPath: string, - bbox: [number, number, number, number] | null, -): void { +/** 种子:storage_sources + photos(file_path 指向 fixture 绝对路径) */ +function seedPhoto(photoId: string, photoPath: string): void { sqlite .prepare( `INSERT INTO photos (id, storage_source_id, file_path, file_hash, width, height, file_size, created_at) VALUES (?, 'src-pp', ?, ?, 2000, 1000, 1024, '2026-01-01T00:00:00.000Z')`, ) .run(photoId, photoPath, `hash-${photoId}`); - if (bbox) { - const [bx, by, bw, bh] = bbox; - sqlite - .prepare( - `INSERT INTO faces (id, photo_id, person_id, bbox_x, bbox_y, bbox_w, bbox_h, detection_score, embedding, detected_at, attributes) - VALUES ('face-1', ?, NULL, ?, ?, ?, ?, 0.99, '[]', '2026-09-12T06:00:00.000Z', NULL)`, - ) - .run(photoId, bx, by, bw, bh); - } } beforeAll(async () => { @@ -243,22 +211,22 @@ beforeAll(async () => { ) .run(path.join(tmpRoot, "storage")); - // fixture ①:bbox 构图照片(bbox = [1300,300,400,400];左半红右半蓝 marker) - const bboxPhoto = path.join(tmpRoot, "hero-bbox.png"); - await makePhoto(bboxPhoto, (raw) => { - setRect(raw, 1300, 300, 1500, 700, [220, 40, 40]); // 左半红 - setRect(raw, 1500, 300, 1700, 700, [40, 40, 220]); // 右半蓝 + // fixture ①:偏右脸照片(红蓝 marker [1300,1700)×[300,700)——中心窗 [722,1278] 必不含) + const facePhoto = path.join(tmpRoot, "hero-face-right.png"); + await makePhoto(facePhoto, (raw) => { + setRect(raw, 1300, 300, 1500, 700, [220, 40, 40]); // 脸左半红 + setRect(raw, 1500, 300, 1700, 700, [40, 40, 220]); // 脸右半蓝 }); - seedPhoto("photo-bbox", bboxPhoto, [1300, 300, 400, 400]); + seedPhoto("photo-face", facePhoto); - // fixture ②:无 faces 中心构图照片(中心绿 + 左右边缘 marker) + // fixture ②:中心构图照片(中心绿 + 左右边缘 marker) const centerPhoto = path.join(tmpRoot, "hero-center.png"); await makePhoto(centerPhoto, (raw) => { setRect(raw, 900, 300, 1100, 700, [40, 220, 40]); // 中心绿(中心裁剪必含) setRect(raw, 0, 400, 150, 600, [220, 40, 40]); // 左缘红(中心裁剪必不含) setRect(raw, 1850, 400, 2000, 600, [40, 40, 220]); // 右缘蓝(中心裁剪必不含) }); - seedPhoto("photo-center", centerPhoto, null); + seedPhoto("photo-center", centerPhoto); const mod = (await import("../lib/wallpaper/video")) as unknown as Record; expect( @@ -281,14 +249,10 @@ afterAll(() => { // 契约断言 // ============================================================================ -describe("【v2】preprocessHeroFrame 人脸构图裁剪:有 faces bbox → crop 窗口含 bbox 中心 ∧ 脸高 ≥ 画布 1/4", () => { - it("bbox [1300,300,400,400] → 输出 736×1600 png;红蓝两半同时可见(⇔ bbox 中心在裁剪窗内);脸高 ≥ 400px", async () => { - const photoPath = path.join(tmpRoot, "hero-bbox.png"); - // 【v2.1】契约声明接口:bbox 经 opts 传入(对象形态 {x,y,w,h},与 faces 表列语义同源; - // db→getLargestFaceBbox→opts 接线由 job 验收覆盖) - const out = await preprocessHeroFrame(photoPath, CANVAS_W, CANVAS_H, { - faceBbox: { x: 1300, y: 300, w: 400, h: 400 }, - }); +describe("【20260928】preprocessHeroFrame 统一中心 cover 微裁(人脸窗口已去除)", () => { + it("偏右脸照片 → 仍中心裁剪:脸 marker(红/蓝)不现形(kill 人脸窗口残留)∧ 输出 800×1440 png", async () => { + const photoPath = path.join(tmpRoot, "hero-face-right.png"); + const out = await preprocessHeroFrame(photoPath, CANVAS_W, CANVAS_H); // 契约:产 tmp png(存在 ∧ PNG 文件头) expect(typeof out).toBe("string"); @@ -298,33 +262,18 @@ describe("【v2】preprocessHeroFrame 人脸构图裁剪:有 faces bbox → cr expect(head.toString("latin1", 1, 4)).toBe("PNG"); expect(out).not.toBe(photoPath); - // 契约:裁剪+resize 到目标画布比例(736×1600) + // 契约:裁剪+resize 到目标画布(800×1440,design D1 参考档) const scan = await scanPixels(out); expect(scan.width, `输出宽度必须 ${CANVAS_W}`).toBe(CANVAS_W); expect(scan.height, `输出高度必须 ${CANVAS_H}`).toBe(CANVAS_H); - // 像素级蕴含(见文件头数学注释):红半 [1300,1500) 与蓝半 [1500,1700) 同时可见 - // ⇔ 裁剪窗左缘 L ∈ (1040,1500] ⇔ bbox 中心 x=1500 ∈ [L, L+460] - expect( - scan.redCount, - "红半 marker 不可见——bbox 左半未进裁剪窗(人脸构图未生效或偏移越界)", - ).toBeGreaterThan(0); - expect( - scan.blueCount, - "蓝半 marker 不可见——bbox 右半未进裁剪窗(bbox 中心 x=1500 不在窗口内)", - ).toBeGreaterThan(0); - - // 契约逐字:脸高 ≥ 画布 1/4(marker 纵向跨度 = 脸高在输出中的像素高度) - const faceHeight = scan.markerMaxY - scan.markerMinY + 1; - expect( - faceHeight, - `脸高 ${faceHeight}px < 画布 1/4(${FACE_HEIGHT_MIN}px,${CANVAS_H}/4)`, - ).toBeGreaterThanOrEqual(FACE_HEIGHT_MIN); + // 20260928 直接命中:中心窗 x ∈ [722,1278] 不含脸 marker [1300,1700)—— + // 若实现仍按人脸重构图(窗口右移对齐脸心 1500),红/蓝 marker 即现形 → 此处即红 + expect(scan.redCount, "脸左半红 marker 现形——人脸窗口裁剪残留(20260928 已废除)").toBe(0); + expect(scan.blueCount, "脸右半蓝 marker 现形——人脸窗口裁剪残留(20260928 已废除)").toBe(0); }, 30000); -}); -describe("【v2】preprocessHeroFrame 回退中心:无 faces bbox → 现行为不变(中心 cover 裁剪)", () => { - it("无 faces 行 → 中心绿 marker 可见、左右边缘 marker 均不可见 ∧ 输出 736×1600 png", async () => { + it("中心构图照片 → 中心绿 marker 可见、左右边缘 marker 均不可见 ∧ 输出 800×1440 png", async () => { const photoPath = path.join(tmpRoot, "hero-center.png"); const out = await preprocessHeroFrame(photoPath, CANVAS_W, CANVAS_H); @@ -338,13 +287,10 @@ describe("【v2】preprocessHeroFrame 回退中心:无 faces bbox → 现行 expect(scan.width).toBe(CANVAS_W); expect(scan.height).toBe(CANVAS_H); - // 2000×1000 cover 到 736×1600 的中心窗 ≈ 源 x ∈ [770,1230]: + // 2000×1000 cover 到 800×1440 的中心窗 ≈ 源 x ∈ [722,1278]: // 中心绿 [900,1100) 必含;左缘红 [0,150) / 右缘蓝 [1850,2000) 必不含 - expect( - scan.greenCount, - "中心 marker 不可见——回退行为偏离中心裁剪(现行为不变契约被破坏)", - ).toBeGreaterThan(0); - expect(scan.redCount, "左缘 marker 可见——裁剪窗偏左,回退行为不是中心裁剪").toBe(0); - expect(scan.blueCount, "右缘 marker 可见——裁剪窗偏右,回退行为不是中心裁剪").toBe(0); + expect(scan.greenCount, "中心 marker 不可见——预裁剪偏离中心 cover 裁剪").toBeGreaterThan(0); + expect(scan.redCount, "左缘 marker 可见——裁剪窗偏左,不是中心裁剪").toBe(0); + expect(scan.blueCount, "右缘 marker 可见——裁剪窗偏右,不是中心裁剪").toBe(0); }, 30000); }); diff --git a/apps/backend/src/__tests__/wallpaper-video-spawn.test.ts b/apps/backend/src/__tests__/wallpaper-video-spawn.test.ts index 53671b9..6033320 100644 --- a/apps/backend/src/__tests__/wallpaper-video-spawn.test.ts +++ b/apps/backend/src/__tests__/wallpaper-video-spawn.test.ts @@ -1,11 +1,12 @@ /** * 单测:lib/wallpaper/video.ts — spawnHoneydoVideo + preprocessHeroFrame(任务 2) * - * 契约(state.md ## 契约规约 计算/spawn 契约): + * 契约(state.md ## 契约规约 计算/spawn 契约;20260928 单腿原生比例): * spawnHoneydoVideo({cliPath, prompt, firstFrame, lastFrame, outPath, seconds, res, timeoutMs}) * → {outPath: string, duration: number, stdout: string} * 错误枚举 HoneydoSpawnError(非零退出 / JSON 解析失败 / 产物文件不存在 / 超时 abort, * message 含 stdout tail ≤2000 字符与超时毫秒数) + * preprocessHeroFrame(photoPath, width, height)(20260928 三参:去人脸窗口,统一中心 cover 微裁) * * 测试策略(照 video-claude-runner 惯例):spawn stub 用真实 fake shell 脚本, * 不 mock child_process——更黑盒、更接近真实 spawn 路径。 @@ -200,20 +201,20 @@ describe("preprocessHeroFrame", () => { expect(meta.format).toBe("png"); }); - it("竖版:300×400 源图 cover 裁剪到 704×1216", async () => { + it("竖版:300×400 源图 cover 裁剪到 928×1248(20260928 原生画布档)", async () => { const src = path.join(dir, "portrait-src.png"); await sharp({ create: { width: 300, height: 400, channels: 3, background: "#cc2288" }, }) .png() .toFile(src); - const out = await preprocessHeroFrame(src, 704, 1216); + const out = await preprocessHeroFrame(src, 928, 1248); const meta = await sharp(out).metadata(); - expect(meta.width).toBe(704); - expect(meta.height).toBe(1216); + expect(meta.width).toBe(928); + expect(meta.height).toBe(1248); }); - // ---- v2 增量任务 12:人脸构图裁剪 ---- + // ---- 20260928 单腿原生:人脸窗口已去除(统一中心 cover 微裁) ---- /** 源图底色 bg,在 (fx,fy) 画 faceW×faceH 红色方块(模拟人脸),返回路径 */ async function makeSource( @@ -256,62 +257,37 @@ describe("preprocessHeroFrame", () => { return p.r > 150 && p.g < 90 && p.b < 90; } - it("人脸构图:小脸靠左上 → 窗口缩放至脸高≈画布高 1/4 且中心对齐人脸", async () => { - // 4000×3000 源,脸 300×300 @(200,200):推导窗口 winH=1200(=4×脸高,脸高映射恰 704/4=176) - // 窗口 2182×1200 中心对齐脸心 (350,350) → clamp 到 (0,0) - const src = await makeSource("face-small-tl.png", 4000, 3000, "#2288cc", 200, 200, 300, 300); - const out = await preprocessHeroFrame(src, 1280, 704, { - faceBbox: { x: 200, y: 200, w: 300, h: 300 }, - }); + it("20260928 去人脸窗口:偏脸源图统一中心 cover 微裁(不向脸重构图)", async () => { + // 2000×1000 源,脸 300×300 @(1500,400)(偏右)。中心 cover 窗口宽 = 1000×(1280/704) = 1818, + // 画布中心 = 图中心 (1000,500) → 窗口 (91..1909);脸 (1500..1800) 落窗口右半 + const src = await makeSource( + "native-center-crop.png", + 2000, + 1000, + "#2288cc", + 1500, + 400, + 300, + 300, + ); + const out = await preprocessHeroFrame(src, 1280, 704); const meta = await sharp(out).metadata(); expect(meta.width).toBe(1280); expect(meta.height).toBe(704); - // 脸方块落在窗口 (0,0,2182,1200) 内 (200..500,200..500) → 画布缩放 0.5866 → (117..293) - expect(isRed(await pixelAt(out, 200, 200))).toBe(true); - expect(isRed(await pixelAt(out, 640, 352))).toBe(false); - }); - - it("人脸构图:脸在整图已 ≥1/4 → 全幅窗口按脸心取位(不放大)", async () => { - // 2000×1000 源,脸 300×300 @(1500,400):脸高占全幅 30% → 窗口=全幅 cover(winH=1000) - // winW=1818,脸心 1650 超右界 → 窗口右对齐;脸在画布右半 - const src = await makeSource("face-big-right.png", 2000, 1000, "#2288cc", 1500, 400, 300, 300); - const out = await preprocessHeroFrame(src, 1280, 704, { - faceBbox: { x: 1500, y: 400, w: 300, h: 300 }, - }); - // 脸方块画布区域约 (928..1139, 282..493) - expect(isRed(await pixelAt(out, 1030, 387))).toBe(true); - expect(isRed(await pixelAt(out, 300, 387))).toBe(false); + // 脸方块画布区域 ≈ (1500-91)×0.704=993 .. (1800-91)×0.704=1203,纵向 282..493 + expect(isRed(await pixelAt(out, 1100, 387))).toBe(true); + // 画布左缘区域为底色(无人脸拉近/窗口偏移的证据) + expect(isRed(await pixelAt(out, 100, 387))).toBe(false); }); - it("人脸构图:竖版画布 704×1216 同样生效(脸高 ≥1216/4=304)", async () => { - // 3000×4000 源,脸 400×400 @(200,200):winH=min(max(min(4032? no: fullH=min(4000,3000/0.579)=4000? → capped=min(4000,1600)=1600; minWin=min(1216,4000)=1216 → winH=1600 - // 窗口 1600×1600? winW=1600*0.579=927 → 中心 (400,400) → left/top=0 - const src = await makeSource("face-portrait.png", 3000, 4000, "#2288cc", 200, 200, 400, 400); - const out = await preprocessHeroFrame(src, 704, 1216, { - faceBbox: { x: 200, y: 200, w: 400, h: 400 }, - }); + it("20260928 签名收窄:preprocessHeroFrame 仅三参(faceBbox 构图选项已删除)", async () => { + const src = await makeSource("native-signature.png", 4000, 3000, "#2288cc", 200, 200, 300, 300); + // 不传任何构图选项——cover 中心裁剪 + const out = await preprocessHeroFrame(src, 1248, 928); const meta = await sharp(out).metadata(); - expect(meta.width).toBe(704); - expect(meta.height).toBe(1216); - // 脸方块在窗口 (0,0,927,1600) 内 (200..600,200..600) → 缩放 1216/1600=0.76 → (152..456,152..456) - expect(isRed(await pixelAt(out, 300, 300))).toBe(true); - expect(isRed(await pixelAt(out, 352, 900))).toBe(false); - }); - - it("faceBbox 缺省/退化 → 回退中心构图(现状行为)", async () => { - const src = await makeSource("face-degenerate.png", 4000, 3000, "#2288cc", 200, 200, 300, 300); - // 缺省 - const out1 = await preprocessHeroFrame(src, 1280, 704); - const meta1 = await sharp(out1).metadata(); - expect(meta1.width).toBe(1280); - expect(meta1.height).toBe(704); - // 退化 bbox(w=0)→ 同样走中心构图(脸不被拉近中心:画布中心为底色) - const out2 = await preprocessHeroFrame(src, 1280, 704, { - faceBbox: { x: 200, y: 200, w: 0, h: 300 }, - }); - const meta2 = await sharp(out2).metadata(); - expect(meta2.width).toBe(1280); - expect(meta2.height).toBe(704); - expect(isRed(await pixelAt(out2, 640, 352))).toBe(false); + expect(meta.width).toBe(1248); + expect(meta.height).toBe(928); + // 4000×3000 → 1248×928(比例同为 4:3)→ 无裁切纯缩放:左上角仍底色(脸在 (200,200) 缩到 (62,62) 附近) + expect(isRed(await pixelAt(out, 63, 63))).toBe(true); }); }); diff --git a/apps/backend/src/__tests__/wallpaper-video-transcode.acceptance.test.ts b/apps/backend/src/__tests__/wallpaper-video-transcode.acceptance.test.ts index b8c9b5a..ad2f462 100644 --- a/apps/backend/src/__tests__/wallpaper-video-transcode.acceptance.test.ts +++ b/apps/backend/src/__tests__/wallpaper-video-transcode.acceptance.test.ts @@ -4,7 +4,8 @@ * 设计文档(state.md)对应契约(§契约规约 计算/spawn 契约【v2】逐字): * - transcodeForGallery 产物 invariant【v2】:容器 mp4 ∧ H.264 ∧ 有音频流(aac 立体声) * ∧ 分辨率 == 源(736×1600,2026-09-25 画布改版)∧ faststart(画廊静音自动播放 + 点击开声) - * - transcodeForAerial 产物 invariant(2026-09-13 验收反转):容器 mov ∧ HEVC(tag hvc1) + * - transcodeForAerialNative 产物 invariant(2026-09-13 验收反转;20260928 单腿条件产出): + * 容器 mov ∧ HEVC(tag hvc1) * ∧ 1920×1080 ∧ **保留音轨**(aac 立体声)∧ moov 在前(faststart) * - §总体架构(v2)步骤 5:横版 HEVC(hvc1) .mov 1920×1080 带音轨 +faststart(Aerial 注入 * 无声播放不受影响,下载/外放有环境音);竖版 H.264 mp4 带音轨 +faststart(画廊用) @@ -15,7 +16,7 @@ * 2s 短样片(画廊源 736×1600 竖版带立体声音轨;Aerial 源 1280×704 横版带音轨—— * 带音轨是为了证明音轨真实保留而非静默丢弃)。 * - * 红队铁律:不读蓝队实现代码;transcodeForGallery / transcodeForAerial 按契约函数名黑盒 + * 红队铁律:不读蓝队实现代码;transcodeForGallery / transcodeForAerialNative 按契约函数名黑盒 * import 执行;不 skip、硬断言——ffmpeg/ffprobe 不可用一律真红。 */ import { spawnSync } from "node:child_process"; @@ -155,7 +156,7 @@ let tmpRoot = ""; let gallerySrc = ""; let aerialSrc = ""; let transcodeForGallery: (src: string, dst: string) => Promise; -let transcodeForAerial: (src: string, dst: string) => Promise; +let transcodeForAerialNative: (src: string, dst: string) => Promise; function makeSrcWithStereoAudio(outPath: string, size: string): void { const r = spawnSync( @@ -233,11 +234,11 @@ beforeAll(async () => { "契约函数 transcodeForGallery 未由 lib/wallpaper/video 导出", ).toBe("function"); expect( - typeof mod.transcodeForAerial, - "契约函数 transcodeForAerial 未由 lib/wallpaper/video 导出", + typeof mod.transcodeForAerialNative, + "契约函数 transcodeForAerialNative 未由 lib/wallpaper/video 导出", ).toBe("function"); transcodeForGallery = mod.transcodeForGallery as typeof transcodeForGallery; - transcodeForAerial = mod.transcodeForAerial as typeof transcodeForAerial; + transcodeForAerialNative = mod.transcodeForAerialNative as typeof transcodeForAerialNative; }, 60000); afterAll(() => { @@ -276,10 +277,10 @@ describe("【v2】transcodeForGallery 产物 invariant:mp4 ∧ H.264 ∧ aac }, 120000); }); -describe("transcodeForAerial 产物 invariant(2026-09-13 起:mov ∧ hvc1 ∧ 1920×1080 ∧ 带音轨 ∧ faststart)", () => { +describe("transcodeForAerialNative 产物 invariant(mov ∧ hvc1 ∧ 1920×1080 ∧ 带音轨 ∧ faststart;crop 前置绝不拉伸)", () => { it("1280×704 带音轨源 → 产物容器 mov、tag hvc1、1920×1080、1 条 aac 立体声音轨、moov 在前", async () => { const dst = path.join(tmpRoot, "2026-09-12_landscape.mov"); - await transcodeForAerial(aerialSrc, dst); + await transcodeForAerialNative(aerialSrc, dst); expect(fs.existsSync(dst), `Aerial 转码产物不存在: ${dst}`).toBe(true); expect(fs.statSync(dst).size).toBeGreaterThan(0); diff --git a/apps/backend/src/__tests__/wallpaper-video-transcode.test.ts b/apps/backend/src/__tests__/wallpaper-video-transcode.test.ts index 9ab8da5..afd84e6 100644 --- a/apps/backend/src/__tests__/wallpaper-video-transcode.test.ts +++ b/apps/backend/src/__tests__/wallpaper-video-transcode.test.ts @@ -1,18 +1,25 @@ /** - * 单测:lib/wallpaper/video.ts — transcodeForAerial / transcodeForGallery / - * assertVideoSpawnPrerequisites / clampWallpaperVideoSeconds(任务 2 + v2 增量任务 12/14/15) + * 单测:lib/wallpaper/video.ts — transcodeForAerialNative / transcodeForGallery / + * assertVideoDimensions / assertVideoSpawnPrerequisites / clampWallpaperVideoSeconds + * (任务 2 + v2 增量任务 12/14/15;20260928 单腿原生:条件 Aerial crop 链 + 尺寸护栏) * - * 契约(state.md ## 契约规约): - * transcodeForAerial 产物 invariant:容器 mov ∧ HEVC(tag hvc1) ∧ 1920×1080 ∧ 无音频 ∧ faststart + * 契约(state.md ## 契约规约 / ## 设计文档 D2): + * transcodeForAerialNative 产物 invariant:容器 mov ∧ HEVC(tag hvc1) ∧ 1920×1080 ∧ + * 带音轨 aac 128k ∧ faststart;滤镜链 crop 前置(居中裁恰 16:9)+ scale=1920:1080 后置 + * (crop 已保证入 scale 前比例恰 16:9,绝不拉伸)+ setsar=1 * transcodeForGallery 产物 invariant【v2】:容器 mp4 ∧ H.264(libx264 crf18)∧ - * 有音频流(aac 128k)∧ faststart(画廊静音自动播放 + 点击开声) + * 有音频流(aac 128k)∧ faststart(画廊静音自动播放 + 点击开声)∧ 无 -vf 尺寸透传 + * assertVideoDimensions(20260928 尺寸护栏):ffprobe 断言产物尺寸,不匹配即 throw + * (message 显式含「尺寸断言失败」语义——场景 5.P2 禁静默失败) + * isAerialCompatCanvas:画布长短轴比 ∈ [1.5, 1.9](闭区间;窗外 mac 静态回退) * hevc_videotoolbox 失败 fallback libx265 -crf 22 -tag:v hvc1 * assertVideoSpawnPrerequisites【v2】增查:wallpaper-overlay 工程 / Remotion 运行时 / * overlay 字体(缺失报错,不自动安装) * clampWallpaperVideoSeconds:非有限 → 默认 4(v2 recipes 纪律) * * 测试策略:mock ../lib/config 注入 fake ffmpeg shell 脚本(记录 argv 后 touch 产物/ - * 退出非零),黑盒断言参数拼装与 fallback 行为。 + * 退出非零),黑盒断言参数拼装与 fallback 行为;assertVideoDimensions 用真实 ffmpeg 造 + * 定尺寸小样片 + 真实 ffprobe(护栏本身必须对真实媒体文件判别)。 */ import { execFileSync } from "node:child_process"; import { chmodSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; @@ -49,10 +56,13 @@ vi.mock("../lib/config", () => ({ }, })); +import { config } from "../lib/config"; import { + assertVideoDimensions, assertVideoSpawnPrerequisites, clampWallpaperVideoSeconds, - transcodeForAerial, + isAerialCompatCanvas, + transcodeForAerialNative, transcodeForGallery, } from "../lib/wallpaper/video"; @@ -120,17 +130,24 @@ afterAll(() => { rmSync(holder.tmpDir, { recursive: true, force: true }); }); -describe("transcodeForAerial / transcodeForGallery", () => { - it("transcodeForAerial:hevc_videotoolbox + scale=1920:1080 + hvc1 + 带音轨 aac 128k +faststart", async () => { +describe("transcodeForAerialNative / transcodeForGallery(20260928)", () => { + it("transcodeForAerialNative:crop 前置(居中裁 16:9)+ scale=1920:1080 后置 setsar=1 + hvc1 + 带音轨 aac +faststart", async () => { const dst = `${holder.tmpDir}/out.mov`; - await transcodeForAerial("/tmp/src.mp4", dst); + await transcodeForAerialNative("/tmp/src.mp4", dst); const args = readArgs(1); expect(args).toContain("-i"); - expect(args).toContain("scale=1920:1080:flags=lanczos"); + // 滤镜链逐字(D2 转码 B):crop 前置 + 等比 scale 后置 + setsar=1(绝不拉伸) + const vfIdx = args.indexOf("-vf"); + const vf = args[vfIdx + 1] ?? ""; + expect(vf).toContain("crop=w='min(iw,ih*16/9)':h='min(ih,iw*9/16)'"); + expect(vf).toContain("scale=1920:1080:flags=lanczos"); + expect(vf).toContain("setsar=1"); + // crop 必须先于 scale(场景 3.P2:crop 前置保证入 scale 前比例恰 16:9) + expect(vf.indexOf("crop=")).toBeLessThan(vf.indexOf("scale=")); expect(args).toContain("hevc_videotoolbox"); expect(args).toContain("hvc1"); - // 2026-09-13 验收要求:以后生成的视频带音轨(横版不再 -an) + // 2026-09-13 验收要求:以后生成的视频带音轨 expect(args).not.toContain("-an"); expect(args).toContain("-c:a"); expect(args[args.indexOf("-c:a") + 1]).toBe("aac"); @@ -139,14 +156,14 @@ describe("transcodeForAerial / transcodeForGallery", () => { expect(args).toContain("+faststart"); }); - it("transcodeForAerial:hevc_videotoolbox 失败 → fallback libx265 -crf 22 -tag:v hvc1", async () => { + it("transcodeForAerialNative:hevc_videotoolbox 失败 → fallback libx265 -crf 22 -tag:v hvc1", async () => { // fail-once ffmpeg:第 1 次(hevc_videotoolbox)失败,第 2 次(libx265)成功 const { config } = await import("../lib/config"); const saved = (config.video as { ffmpegPath: string }).ffmpegPath; (config.video as { ffmpegPath: string }).ffmpegPath = holder.ffmpegFailOncePath; try { const dst = `${holder.tmpDir}/out-fallback.mov`; - await transcodeForAerial("/tmp/src.mp4", dst); + await transcodeForAerialNative("/tmp/src.mp4", dst); const args1 = readArgs(1); expect(args1).toContain("hevc_videotoolbox"); @@ -155,12 +172,15 @@ describe("transcodeForAerial / transcodeForGallery", () => { expect(args2).toContain("-crf"); expect(args2[args2.indexOf("-crf") + 1]).toBe("22"); expect(args2).toContain("hvc1"); + // fallback 链同样带 crop 前置(窗口内任何硬件路径都不得拉伸) + const vf2 = args2[args2.indexOf("-vf") + 1] ?? ""; + expect(vf2).toContain("crop="); } finally { (config.video as { ffmpegPath: string }).ffmpegPath = saved; } }); - it("transcodeForGallery【v2】:libx264 crf18 + aac 128k + faststart(保留音轨)", async () => { + it("transcodeForGallery【v2】:libx264 crf18 + aac 128k + faststart(保留音轨);无 -vf 尺寸透传(原生比例直通)", async () => { const dst = `${holder.tmpDir}/out.mp4`; await transcodeForGallery("/tmp/src.mp4", dst); @@ -178,6 +198,80 @@ describe("transcodeForAerial / transcodeForGallery", () => { expect(args).not.toContain("-an"); // v2:不再流拷贝(-c:v copy → 重编码) expect(args[args.indexOf("-c:v") + 1]).not.toBe("copy"); + // 20260928:无 -vf(分辨率 == 源画布,画廊消费原生比例) + expect(args).not.toContain("-vf"); + }); +}); + +describe("isAerialCompatCanvas(20260928 方向性比例窗口 [1.5, 1.9] 闭区间)", () => { + it("窗口内(含边界,仅横版画布)→ true", () => { + expect(isAerialCompatCanvas({ width: 1500, height: 1000 })).toBe(true); // 1.5 恰边界 + expect(isAerialCompatCanvas({ width: 1900, height: 1000 })).toBe(true); // 1.9 恰边界 + expect(isAerialCompatCanvas({ width: 1440, height: 800 })).toBe(true); // 1.8(16:9 档) + expect(isAerialCompatCanvas({ width: 1312, height: 864 })).toBe(true); // 1.5185(3:2 档) + }); + + it("窗外 → false(竖版画布 w/h<1 恒窗外;超宽全景/1:1 也不产 .mov,mac 静态回退)", () => { + expect(isAerialCompatCanvas({ width: 1496, height: 1000 })).toBe(false); // 1.496 < 1.5 + expect(isAerialCompatCanvas({ width: 1912, height: 1000 })).toBe(false); // 1.912 > 1.9 + expect(isAerialCompatCanvas({ width: 800, height: 1440 })).toBe(false); // 竖版 0.5556(竖裁横砍 69% 画面) + expect(isAerialCompatCanvas({ width: 1664, height: 704 })).toBe(false); // 2.3636(超宽兜底档) + expect(isAerialCompatCanvas({ width: 1056, height: 1056 })).toBe(false); // 1:1 + }); +}); + +describe("assertVideoDimensions(20260928 尺寸护栏,真实 ffmpeg/ffprobe 判别)", () => { + // 本组用真实 ffprobe(文件级 config mock 注入的是 fake 路径)——护栏必须对真实媒体判别 + const videoCfg = config.video as { ffprobePath: string }; + const savedFfprobe = videoCfg.ffprobePath; + videoCfg.ffprobePath = "ffprobe"; + afterAll(() => { + videoCfg.ffprobePath = savedFfprobe; + }); + + /** ffmpeg 造定尺寸 0.3s 小样片(真实媒体,护栏必须对真实文件判别) */ + function makeTinyMp4(outPath: string, size: string): void { + execFileSync( + "ffmpeg", + [ + "-y", + "-loglevel", + "error", + "-f", + "lavfi", + "-i", + `testsrc2=size=${size}:rate=12:duration=0.3`, + "-c:v", + "libx264", + "-pix_fmt", + "yuv420p", + outPath, + ], + { timeout: 30000 }, + ); + } + + it("尺寸匹配 → 放行(1920×1080 期望 vs 1920×1080 产物)", async () => { + const src = `${holder.tmpDir}/guard-ok.mp4`; + makeTinyMp4(src, "1920x1080"); + await expect(assertVideoDimensions(src, 1920, 1080)).resolves.toBeUndefined(); + }); + + it("尺寸不匹配 → throw 且 message 显式含「尺寸断言失败」与期望/实际值(禁静默失败)", async () => { + const src = `${holder.tmpDir}/guard-bad.mp4`; + makeTinyMp4(src, "1918x1080"); + const err = (await assertVideoDimensions(src, 1920, 1080).catch((e) => e)) as Error; + expect(err).toBeInstanceOf(Error); + expect(err.message).toContain("尺寸断言失败"); + expect(err.message).toContain("1920×1080"); + expect(err.message).toContain("1918×1080"); + }); + + it("文件不存在/非媒体 → throw(ffprobe 探测异常分支同样显式报错)", async () => { + const err = (await assertVideoDimensions(`${holder.tmpDir}/no-such.mp4`, 1920, 1080).catch( + (e) => e, + )) as Error; + expect(err.message).toContain("尺寸断言失败"); }); }); diff --git a/apps/backend/src/cli/wallpaper-video.ts b/apps/backend/src/cli/wallpaper-video.ts index af00bca..ba0a187 100644 --- a/apps/backend/src/cli/wallpaper-video.ts +++ b/apps/backend/src/cli/wallpaper-video.ts @@ -10,7 +10,8 @@ * 契约: * - 参数 --pickDate 必填,isValidYmd 校验(YYYY-MM-DD 格式 + 实际日期合法性) * - 退出码:0 成功/跳过,1 失败(参数错误 / 主流程 throw) - * - stdout 逐行进度 + 末行 JSON {pickDate, landscape: , portrait: } + * - stdout 逐行进度 + 末行 JSON {pickDate, native: , landscape: } + * (20260928 单腿原生:native=原生 mp4 回执;landscape=16:9 兼容 .mov 回执(仅兼容日非空)) */ import { runWallpaperVideo } from "../jobs/wallpaper-video"; @@ -38,9 +39,9 @@ export function parsePickDateArg(argv: string[]): string | undefined { return undefined; } -/** 末行 JSON(契约:{pickDate, landscape, portrait}) */ -export function formatResultLine(pickDate: string, landscape: string, portrait: string): string { - return JSON.stringify({ pickDate, landscape, portrait }); +/** 末行 JSON(契约:{pickDate, native, landscape}) */ +export function formatResultLine(pickDate: string, native: string, landscape: string): string { + return JSON.stringify({ pickDate, native, landscape }); } async function main(): Promise { @@ -58,9 +59,9 @@ async function main(): Promise { } console.log(`[wallpaper-video:rerun] 开始 pickDate=${pickDate}`); - const { landscape, portrait } = await runWallpaperVideo(pickDate, console.log); + const { native, landscape } = await runWallpaperVideo(pickDate, console.log); // 末行机读 JSON(红队/QA 解析用) - console.log(formatResultLine(pickDate, landscape, portrait)); + console.log(formatResultLine(pickDate, native, landscape)); process.exit(0); } diff --git a/apps/backend/src/db/schema.ts b/apps/backend/src/db/schema.ts index 44dc8ca..d53ae81 100644 --- a/apps/backend/src/db/schema.ts +++ b/apps/backend/src/db/schema.ts @@ -200,11 +200,14 @@ export const dailyPicks = sqliteTable("daily_picks", { .$type<{ photoId: string; caption: string }[]>() .notNull() .default(sql`'[]'`), - /** 横版壁纸视频 COS URL(Aerial 用,.mov;null = 未生成/生成失败/开关关闭)。 + /** 横版壁纸视频 COS URL(20260928 语义收窄为「16:9 适配版」.mov;null = 未生成/开关关闭/ + * 画布比例不在 [1.5,1.9] 兼容窗口——mac App 拿空列走静态壁纸回退)。 * 值 = COS 上传回执 URL(非约定 key 拼接,死链教训);回执空串不写列。 */ wallpaperVideoLandscapeUrl: text("wallpaper_video_landscape_url"), - /** 竖版壁纸视频 COS URL(画廊壁纸卡用,.mp4;null = 未生成/生成失败/开关关闭)。 */ + /** 竖版壁纸视频 COS URL(历史 legacy 两腿产物;20260928 起不再新写,存量行保留)。 */ wallpaperVideoPortraitUrl: text("wallpaper_video_portrait_url"), + /** 单腿原生比例壁纸视频 COS URL(20260928 新增;画廊消费优先级 native > portrait > landscape)。 */ + wallpaperVideoNativeUrl: text("wallpaper_video_native_url"), /** hero 照片的微动视频运动描述(narrate AI 生成,2026-09-13;null → 壁纸视频按有无人脸分层默认 prompt) */ motionPrompt: text("motion_prompt"), createdAt: text("created_at").notNull(), diff --git a/apps/backend/src/jobs/__tests__/daily-push.portrait.acceptance.test.ts b/apps/backend/src/jobs/__tests__/daily-push.portrait.acceptance.test.ts index b863625..9760a15 100644 --- a/apps/backend/src/jobs/__tests__/daily-push.portrait.acceptance.test.ts +++ b/apps/backend/src/jobs/__tests__/daily-push.portrait.acceptance.test.ts @@ -92,6 +92,7 @@ function createTestDb() { title TEXT NOT NULL, narrative TEXT NOT NULL, score REAL NOT NULL DEFAULT 0, wallpaper_video_landscape_url TEXT, wallpaper_video_portrait_url TEXT, + wallpaper_video_native_url TEXT, motion_prompt TEXT, composed_image_path TEXT, created_at TEXT NOT NULL, members TEXT DEFAULT '[]' ); diff --git a/apps/backend/src/jobs/__tests__/daily-select-integration.acceptance.test.ts b/apps/backend/src/jobs/__tests__/daily-select-integration.acceptance.test.ts index cdc9f5f..b6eff05 100644 --- a/apps/backend/src/jobs/__tests__/daily-select-integration.acceptance.test.ts +++ b/apps/backend/src/jobs/__tests__/daily-select-integration.acceptance.test.ts @@ -133,6 +133,7 @@ function createTestDb() { score REAL NOT NULL DEFAULT 0, composed_image_path TEXT, wallpaper_video_landscape_url TEXT, wallpaper_video_portrait_url TEXT, + wallpaper_video_native_url TEXT, motion_prompt TEXT, members TEXT NOT NULL DEFAULT '[]', created_at TEXT NOT NULL ); diff --git a/apps/backend/src/jobs/__tests__/daily-selection-entries.acceptance.test.ts b/apps/backend/src/jobs/__tests__/daily-selection-entries.acceptance.test.ts index 9a2b371..b3d1c3f 100644 --- a/apps/backend/src/jobs/__tests__/daily-selection-entries.acceptance.test.ts +++ b/apps/backend/src/jobs/__tests__/daily-selection-entries.acceptance.test.ts @@ -180,6 +180,7 @@ function createTestDb() { created_at TEXT NOT NULL, wallpaper_video_landscape_url TEXT, wallpaper_video_portrait_url TEXT, + wallpaper_video_native_url TEXT, motion_prompt TEXT, members TEXT DEFAULT '[]' ); diff --git a/apps/backend/src/jobs/__tests__/daily-selection-multi-photo.acceptance.test.ts b/apps/backend/src/jobs/__tests__/daily-selection-multi-photo.acceptance.test.ts index 627f446..6835c02 100644 --- a/apps/backend/src/jobs/__tests__/daily-selection-multi-photo.acceptance.test.ts +++ b/apps/backend/src/jobs/__tests__/daily-selection-multi-photo.acceptance.test.ts @@ -194,6 +194,7 @@ function createTestDb() { created_at TEXT NOT NULL, wallpaper_video_landscape_url TEXT, wallpaper_video_portrait_url TEXT, + wallpaper_video_native_url TEXT, motion_prompt TEXT, members TEXT DEFAULT '[]' ); diff --git a/apps/backend/src/jobs/__tests__/daily-selection.portrait.acceptance.test.ts b/apps/backend/src/jobs/__tests__/daily-selection.portrait.acceptance.test.ts index 7f932e4..decb054 100644 --- a/apps/backend/src/jobs/__tests__/daily-selection.portrait.acceptance.test.ts +++ b/apps/backend/src/jobs/__tests__/daily-selection.portrait.acceptance.test.ts @@ -161,6 +161,7 @@ function createTestDb() { title TEXT NOT NULL, narrative TEXT NOT NULL, score REAL NOT NULL DEFAULT 0, wallpaper_video_landscape_url TEXT, wallpaper_video_portrait_url TEXT, + wallpaper_video_native_url TEXT, motion_prompt TEXT, composed_image_path TEXT, created_at TEXT NOT NULL, members TEXT DEFAULT '[]' ); diff --git a/apps/backend/src/jobs/wallpaper-video.ts b/apps/backend/src/jobs/wallpaper-video.ts index 59ff7c6..93fbcb4 100644 --- a/apps/backend/src/jobs/wallpaper-video.ts +++ b/apps/backend/src/jobs/wallpaper-video.ts @@ -2,24 +2,29 @@ import { mkdir, rm } from "node:fs/promises"; import { access } from "node:fs/promises"; import path from "node:path"; /** - * wallpaper-video Worker:每日精选完成后对 hero 照片做微动化,产出横竖两条壁纸视频 - * (动态视频壁纸,state.md ## 后端设计 4 / ## 契约规约;v2 增量任务 15 双轨转码改造)。 + * wallpaper-video Worker:每日精选完成后对 hero 照片做微动化,产出**单腿原生比例**壁纸视频 + * (动态视频壁纸,state.md ## 后端设计 4 / ## 契约规约;20260928 单腿原生比例改版)。 * * 触发:daily-selection 阶段 4 链式 enqueue(wallpaperVideoQueue.add,one-off,无 repeatable)。 - * 流程(runWallpaperVideo,v2): + * 流程(runWallpaperVideo,20260928): * 1. 开关关(config.wallpaperVideoEnabled)→ log skip 返回 * 2. 取 dailyPicks + hero photo(无记录 / hero 是视频 / composedImagePath null → skip) - * 3. faces 表取 hero 最大人脸 bbox(面积最大;无脸 → null 中心构图回退) - * 4. 串行两侧,每侧(v2 串接): - * preprocess(人脸构图裁剪)→ spawn honeydo 生成(seconds 默认 4,双锚定) - * → buildLoop(palindrome 拼接至 loopSeconds 默认 8) - * → renderTextOverlay(Remotion 文字层合成,杂志排版) - * → 双轨转码(横 Aerial:hvc1 .mov 1920×1080 无音轨,输入=Remotion 成品; - * 竖 Gallery:libx264 crf18 + aac 128k mp4 带音轨) - * → COS 上传 ×1/侧(回执 URL,失败返回空串;回执非空串才写 DB 列) + * 3. faces 表取 hero 最大人脸 bbox(仅用于 prompt 分层默认;预裁剪已去除人脸窗口) + * + sharp 读 hero EXIF 旋转后尺寸 → computeNativeCanvas 算单腿原生画布 + * 4. 单腿串接(produceNativeSide): + * preprocess(cover 微裁至画布)→ spawn honeydo 生成(seconds 默认 4,双锚定, + * -r 720p + --width/--height 画布显式覆盖)→ buildLoop(palindrome 拼接至 loopSeconds 默认 8) + * → renderTextOverlay(Remotion 文字层,props 携画布宽高,comp 按画布比例选) + * → 转码 A(画廊原生):transcodeForGallery 无尺寸透传(分辨率==画布)→ 尺寸护栏断言 + * → 上传 COS `relight/wallpaper-videos/{pickDate}_native.mp4` → 写 wallpaper_video_native_url + * → 转码 B(条件 Aerial):画布比例 ∈ [1.5,1.9] 才产出——transcodeForAerialNative + * (居中裁 16:9 → 1920×1080 hvc1 .mov)→ 尺寸护栏断言 → 上传现有 `_landscape.mov` key + * → 写 wallpaper_video_landscape_url(语义收窄为「16:9 适配版」) + * 窗口外:.mov 与 landscape 列都不产出 → mac App 拿空列走既有静态回退(零改动) * 5. syncDayToGallery(复用,重建 manifest 推 VPS) * - * 容错:单侧生成/上传失败不阻塞另一侧与主流程——失败当日回退静态壁纸(既有链路不动)。 + * 失败语义(契约 8):任一环节 throw → 当日 wallpaper_video_native_url / wallpaper_video_landscape_url + * 均空(护栏在任一回执列写入之前执行)→ 画廊静态卡 + mac 静态壁纸;job 不向调度层抛(旁路容错)。 * 注意:本 Worker 串行(concurrency 1),单条 spawn 90min 级;queue defaultJobOptions * attempts: 1(失败不重试,重试会连环占串行 Worker 且重复烧 GPU)。 */ @@ -28,23 +33,23 @@ import { desc, eq, sql } from "drizzle-orm"; import { db, schema } from "../db"; import { config } from "../lib/config"; import { uploadFile } from "../lib/cos/upload"; +import { wallpaperVideoLandscapeCosKey, wallpaperVideoNativeCosKey } from "../lib/gallery/manifest"; +// 画布 SSOT(20260928 单腿原生):computeNativeCanvas 从 native-canvas.ts 引入 +import { computeNativeCanvas } from "../lib/wallpaper/native-canvas"; import { - wallpaperVideoLandscapeCosKey, - wallpaperVideoPortraitCosKey, -} from "../lib/gallery/manifest"; -import { + DimensionAssertError, type FaceBbox, - WALLPAPER_VIDEO_LANDSCAPE_CANVAS, - WALLPAPER_VIDEO_LANDSCAPE_RES, - WALLPAPER_VIDEO_PORTRAIT_CANVAS, // 竖版画布 736×1600(portrait 档经 --width/--height 逐轴覆盖) - WALLPAPER_VIDEO_PORTRAIT_RES, + WALLPAPER_VIDEO_RES, + assertVideoDimensions, assertVideoSpawnPrerequisites, buildLoop, clampWallpaperVideoSeconds, + isAerialCompatCanvas, preprocessHeroFrame, + readOrientedDimensions, renderTextOverlay, spawnHoneydoVideo, - transcodeForAerial, + transcodeForAerialNative, transcodeForGallery, } from "../lib/wallpaper/video"; @@ -54,7 +59,7 @@ function wallpaperVideoDir(): string { } /** - * faces 表取该照片面积最大的人脸 bbox(人脸构图裁剪用;无脸 → null 中心构图回退)。 + * faces 表取该照片面积最大的人脸 bbox(仅 prompt 分层默认用;无脸 → null → 风景默认 prompt)。 * bbox 为 EXIF 旋转后原图像素坐标(detect-faces 主流程 rotate 后检测,同空间)。 */ export async function getLargestFaceBbox(photoId: string): Promise { @@ -73,113 +78,138 @@ export async function getLargestFaceBbox(photoId: string): Promise Promise; log: (m: string) => void; -}): Promise { - const { - pickDate, - pickId, - photoPath, - faceBbox, - meta, - prompt, - canvas, - res, - ext, - cosKey, - contentType, - dbColumn, - transcode, - log, - } = opts; - const side = ext === "mov" ? "landscape" : "portrait"; +}): Promise<{ nativeUrl: string; landscapeUrl: string }> { + const { pickDate, pickId, photoPath, meta, canvas, prompt, log } = opts; + const aerial = isAerialCompatCanvas(canvas); - // 1. sharp 预裁剪(v2 人脸构图:faces bbox → 脸占画布 ≥1/4;无 bbox → 中心构图回退) - const framePath = await preprocessHeroFrame(photoPath, canvas.width, canvas.height, { faceBbox }); + // 1. sharp 预裁剪(cover 微裁至画布;原生比例下构图已忠实,无人脸窗口) + const framePath = await preprocessHeroFrame(photoPath, canvas.width, canvas.height); // 中间产物(raw 生成 / loop 拼接 / overlay 合成)——转码完成后逐级清理 - const rawPath = path.join(wallpaperVideoDir(), `${pickDate}-${side}-raw.mp4`); + const rawPath = path.join(wallpaperVideoDir(), `${pickDate}-native-raw.mp4`); + const nativeDstPath = path.join(wallpaperVideoDir(), `${pickDate}-native.mp4`); + const aerialDstPath = path.join(wallpaperVideoDir(), `${pickDate}-landscape.mov`); let loopPath: string | null = null; let overlaidPath: string | null = null; try { - // 2. spawn honeydo(双锚定伪循环:--first-frame 与 --last-frame 同图;seconds 默认 4) + // 2. spawn honeydo(双锚定伪循环:--first-frame 与 --last-frame 同图;seconds 默认 4; + // 契约 6:-r 720p 保留保 stdout 回执语义,画布两轴经 --width/--height 显式覆盖) const seconds = clampWallpaperVideoSeconds(config.wallpaperVideoSeconds); - log(`[wallpaper-video] ${pickDate} ${res} 生成开始(seconds=${seconds})`); + log( + `[wallpaper-video] ${pickDate} native ${canvas.width}×${canvas.height} 生成开始(seconds=${seconds},aerial 兼容窗口=${aerial})`, + ); await spawnHoneydoVideo({ cliPath: config.honeydoCliPath, - prompt: opts.prompt, + prompt, firstFrame: framePath, lastFrame: framePath, outPath: rawPath, seconds, - res, - // C2:两条腿均显式传画布(竖版 736×1600 覆盖 portrait 档、横版 1280×704 与 720p 档等价) + res: WALLPAPER_VIDEO_RES, width: canvas.width, height: canvas.height, timeoutMs: config.wallpaperVideoSpawnTimeoutMs, }); - log(`[wallpaper-video] ${pickDate} ${res} 生成完成 → ${rawPath}`); + log(`[wallpaper-video] ${pickDate} native 生成完成 → ${rawPath}`); // 3. buildLoop:palindrome 拼接至 loopSeconds(默认 8s;4s 单条 ×2) const loop = await buildLoop(rawPath, config.wallpaperVideoLoopSeconds); loopPath = loop.loopPath; log( - `[wallpaper-video] ${pickDate} ${res} palindrome 拼接完成(segments=${loop.segments})→ ${loopPath}`, + `[wallpaper-video] ${pickDate} native palindrome 拼接完成(segments=${loop.segments})→ ${loopPath}`, ); - // 4. renderTextOverlay:Remotion 文字层合成(杂志排版:日期/标题/footer 拍摄时刻) + // 4. renderTextOverlay:Remotion 文字层合成(props 携画布宽高;comp 按画布比例选) const overlay = await renderTextOverlay(loopPath, { pickDate, title: meta.title, narrative: meta.narrative, takenAt: meta.takenAt, + canvasWidth: canvas.width, + canvasHeight: canvas.height, }); overlaidPath = overlay.overlaidPath; - log(`[wallpaper-video] ${pickDate} ${res} 文字层合成完成 → ${overlaidPath}`); + log(`[wallpaper-video] ${pickDate} native 文字层合成完成 → ${overlaidPath}`); + + // 5. 转码 A(画廊原生):H.264 mp4 无尺寸透传(分辨率==画布)+ 尺寸护栏 + await transcodeForGallery(overlaidPath, nativeDstPath); + await assertVideoDimensions(nativeDstPath, canvas.width, canvas.height); + log( + `[wallpaper-video] ${pickDate} native 转码完成(${canvas.width}×${canvas.height})→ ${nativeDstPath}`, + ); + + // 6. 转码 B(条件 Aerial):画布比例 ∈ [1.5,1.9] 才产出 16:9 微裁 .mov + 尺寸护栏; + // 窗口外不产出 → landscape 列保持空 → mac 当日走静态回退(WallpaperCoordinator 天然兼容) + if (aerial) { + await transcodeForAerialNative(overlaidPath, aerialDstPath); + await assertVideoDimensions(aerialDstPath, 1920, 1080); + log(`[wallpaper-video] ${pickDate} aerial 16:9 微裁转码完成(1920×1080)→ ${aerialDstPath}`); + } - // 5. 转码(横 Aerial:HEVC .mov 1920×1080 无音轨;竖 Gallery:H.264 mp4 带音轨) - // v2:双轨输入 = Remotion 成品 - const dstPath = path.join(wallpaperVideoDir(), `${pickDate}-${side}.${ext}`); - await transcode(overlaidPath, dstPath); - log(`[wallpaper-video] ${pickDate} ${res} 转码完成 → ${dstPath}`); + // 中间产物清理(两条转码均成功后) await rm(rawPath, { force: true }).catch(() => {}); await rm(loopPath, { force: true }).catch(() => {}); await rm(overlaidPath, { force: true }).catch(() => {}); - // 6. COS 上传(回执 URL;失败/凭据缺失返回空串,不 throw) + // 7. COS 上传(回执 URL;失败/凭据缺失返回空串,不 throw) + let nativeUrl = ""; try { - await access(dstPath); + await access(nativeDstPath); + nativeUrl = await uploadFile( + nativeDstPath, + wallpaperVideoNativeCosKey(pickDate), + "video/mp4", + ); + if (!nativeUrl) { + log(`[wallpaper-video] ${pickDate} native COS 上传返回空串(失败/凭据缺失),不写 DB 列`); + } } catch { - log(`[wallpaper-video] ${pickDate} ${res} 产物缺失,跳过上传: ${dstPath}`); - return ""; + log(`[wallpaper-video] ${pickDate} native 产物缺失,跳过上传: ${nativeDstPath}`); } - const url = await uploadFile(dstPath, cosKey, contentType); - if (!url) { - log(`[wallpaper-video] ${pickDate} ${res} COS 上传返回空串(失败/凭据缺失),不写 DB 列`); - return ""; + + let landscapeUrl = ""; + if (aerial) { + try { + await access(aerialDstPath); + landscapeUrl = await uploadFile( + aerialDstPath, + wallpaperVideoLandscapeCosKey(pickDate), + "video/quicktime", + ); + if (!landscapeUrl) { + log(`[wallpaper-video] ${pickDate} aerial COS 上传返回空串(失败/凭据缺失),不写 DB 列`); + } + } catch { + log(`[wallpaper-video] ${pickDate} aerial 产物缺失,跳过上传: ${aerialDstPath}`); + } } - // 7. 回执非空串才写 DB 列(单行 UPDATE,无多表事务) - await db - .update(schema.dailyPicks) - .set({ [dbColumn]: url }) - .where(eq(schema.dailyPicks.id, pickId)); - log(`[wallpaper-video] ${pickDate} ${res} 上传成功 → ${url}`); - return url; + // 8. 回执非空串才写 DB 列(单行 UPDATE,合并两列) + const cols: Record = {}; + if (nativeUrl) cols.wallpaperVideoNativeUrl = nativeUrl; + if (landscapeUrl) cols.wallpaperVideoLandscapeUrl = landscapeUrl; + if (Object.keys(cols).length > 0) { + await db.update(schema.dailyPicks).set(cols).where(eq(schema.dailyPicks.id, pickId)); + } + log( + `[wallpaper-video] ${pickDate} 完成 native=${nativeUrl || "(空)"} aerial=${landscapeUrl || "(空)"}`, + ); + return { nativeUrl, landscapeUrl }; } finally { await rm(framePath, { force: true }).catch(() => {}); // 失败路径清理已产出的中间产物(成功路径已在上方清理) @@ -191,16 +221,16 @@ async function produceOneSide(opts: { /** * 壁纸视频主流程(Worker 与 rerun CLI 共用)。 * - * @returns 两条 COS URL(失败/跳过为空串——调用方/CLI 末行 JSON 契约 ) + * @returns native 与 16:9 兼容两条 COS URL(失败/跳过为空串——调用方/CLI 末行 JSON 契约) */ export async function runWallpaperVideo( pickDate: string, log: (m: string) => void = console.log, -): Promise<{ landscape: string; portrait: string }> { +): Promise<{ native: string; landscape: string }> { // 1. 开关关 → skip(当日回退静态,零 honeydo 调用) if (!config.wallpaperVideoEnabled) { log("[wallpaper-video] skip:功能开关关闭(DAILY_WALLPAPER_VIDEO=false),维持静态壁纸链路"); - return { landscape: "", portrait: "" }; + return { native: "", landscape: "" }; } // 2. 取当日 hero(无记录 / hero 是视频 / 无合成壁纸 → skip) @@ -212,7 +242,7 @@ export async function runWallpaperVideo( const pick = pickRows[0]; if (!pick) { log(`[wallpaper-video] skip:dailyPicks 无 ${pickDate} 记录`); - return { landscape: "", portrait: "" }; + return { native: "", landscape: "" }; } const photoRows = await db @@ -223,31 +253,39 @@ export async function runWallpaperVideo( const hero = photoRows[0]; if (!hero) { log(`[wallpaper-video] skip:hero photoId=${pick.photoId} 不存在`); - return { landscape: "", portrait: "" }; + return { native: "", landscape: "" }; } if ((hero.mediaType ?? "image") === "video") { log("[wallpaper-video] skip:hero 是视频,当日不出壁纸视频(Mac 走旧 dynamic HEIC 链路)"); - return { landscape: "", portrait: "" }; + return { native: "", landscape: "" }; } if (!pick.composedImagePath) { log("[wallpaper-video] skip:composedImagePath 为空(无静态壁纸,不生成视频)"); - return { landscape: "", portrait: "" }; + return { native: "", landscape: "" }; } // 前置校验(honeydo bin / ffmpeg / 原图 / Remotion 文字层工程四组存在) await assertVideoSpawnPrerequisites(hero.filePath); - // 3. faces 表取 hero 最大人脸 bbox(两侧共用;无脸 → 中心构图回退) + // 3. hero EXIF 旋转后尺寸 → 单腿原生画布(SSOT computeNativeCanvas); + // faces 表取 hero 最大人脸 bbox(仅 prompt 分层默认;预裁剪已无人脸窗口) + const dims = await readOrientedDimensions(hero.filePath); + const canvas = computeNativeCanvas(dims.width, dims.height); + log( + `[wallpaper-video] ${pickDate} 原生画布 ${canvas.width}×${canvas.height}(原图 ${dims.width}×${dims.height})`, + ); + const faceBbox = await getLargestFaceBbox(hero.id); log( - `[wallpaper-video] ${pickDate} 人脸构图 bbox=${ - faceBbox ? `${faceBbox.w}×${faceBbox.h}@(${faceBbox.x},${faceBbox.y})` : "无(中心构图)" - }`, + `[wallpaper-video] ${pickDate} 人脸 bbox=${ + faceBbox ? `${faceBbox.w}×${faceBbox.h}@(${faceBbox.x},${faceBbox.y})` : "无" + }(仅 prompt 分层用)`, ); await mkdir(wallpaperVideoDir(), { recursive: true }); - // 4. 串行两侧「生成→buildLoop→renderTextOverlay→转码」;单侧失败旁路 log,不阻塞另一侧 + // 4. 单腿「生成→buildLoop→renderTextOverlay→双转码→上传→写列」; + // 任一环节 throw → 旁路 log,当日回退静态(契约 8 两列均空,不影响精选主流程) const meta = { title: pick.title, narrative: pick.narrative, takenAt: hero.takenAt ?? null }; // prompt 解析链(2026-09-13 契约修订):AI narrate 的 motionPrompt 优先, // 缺失时按有无人脸分层默认(人物收敛 / 风景放开),不再全场景共用一句。 @@ -259,60 +297,37 @@ export async function runWallpaperVideo( pick.motionPrompt?.trim() ? "AI motionPrompt" : faceBbox ? "默认人物" : "默认风景" }`, ); + let native = ""; let landscape = ""; - let portrait = ""; - try { - landscape = await produceOneSide({ - pickDate, - pickId: pick.id, - photoPath: hero.filePath, - faceBbox, - meta, - prompt, - canvas: WALLPAPER_VIDEO_LANDSCAPE_CANVAS, - res: WALLPAPER_VIDEO_LANDSCAPE_RES, - ext: "mov", - cosKey: wallpaperVideoLandscapeCosKey(pickDate), - contentType: "video/quicktime", - dbColumn: "wallpaperVideoLandscapeUrl", - transcode: transcodeForAerial, - log, - }); - } catch (err) { - log( - `[wallpaper-video] ${pickDate} 横版失败(当日回退静态): ${ - err instanceof Error ? err.message : String(err) - }`, - ); - } try { - portrait = await produceOneSide({ + const produced = await produceNativeSide({ pickDate, pickId: pick.id, photoPath: hero.filePath, - faceBbox, meta, prompt, - canvas: WALLPAPER_VIDEO_PORTRAIT_CANVAS, - res: WALLPAPER_VIDEO_PORTRAIT_RES, - ext: "mp4", - cosKey: wallpaperVideoPortraitCosKey(pickDate), - contentType: "video/mp4", - dbColumn: "wallpaperVideoPortraitUrl", - transcode: transcodeForGallery, + canvas, log, }); + native = produced.nativeUrl; + landscape = produced.landscapeUrl; } catch (err) { log( - `[wallpaper-video] ${pickDate} 竖版失败(当日回退静态): ${ + `[wallpaper-video] ${pickDate} native 腿失败(当日回退静态): ${ err instanceof Error ? err.message : String(err) }`, ); + if (err instanceof DimensionAssertError) { + // 设计 D2/验收 5.P1:尺寸护栏失败必须传播(worker 标记 job 失败 / rerun CLI exit≠0); + // 两列未写 → 当日自然回退静态(契约 8 fail-safe 语义不变) + throw err; + } + // 其余失败(spawn/上传/DB 等)→ 既有旁路容错:吞掉返回空列(契约 8,不影响精选主流程) } // 5. 画廊同步(复用 syncDayToGallery:重建 manifest + 推 VPS;幂等重传静态图无害)。 // 旁路容错:失败 log 不 throw(画廊是旁路)。 - if (landscape || portrait) { + if (native || landscape) { try { const { syncDayToGallery } = await import("../lib/gallery/sync"); await syncDayToGallery( @@ -331,9 +346,9 @@ export async function runWallpaperVideo( } log( - `[wallpaper-video] ${pickDate} 完成 landscape=${landscape || "(空)"} portrait=${portrait || "(空)"}`, + `[wallpaper-video] ${pickDate} 完成 native=${native || "(空)"} landscape=${landscape || "(空)"}`, ); - return { landscape, portrait }; + return { native, landscape }; } /** diff --git a/apps/backend/src/lib/gallery/manifest.ts b/apps/backend/src/lib/gallery/manifest.ts index 60cfe59..101e9e8 100644 --- a/apps/backend/src/lib/gallery/manifest.ts +++ b/apps/backend/src/lib/gallery/manifest.ts @@ -59,13 +59,15 @@ export interface ManifestDay { /** 竖版手机壁纸 COS URL(1290×2796;composedImagePath 为 null 时留空串) */ wallpaperPortrait: string; /** - * 横版壁纸视频 COS URL(Aerial 用,.mov)。 + * 横版壁纸视频 COS URL(20260928 语义收窄:仅 16:9 兼容日微裁 .mov;历史两腿日为横版 legacy)。 * 条件展开:仅当 DB 回执列非空时字段存在(null/空串 → JSON 中字段缺省,不输出空串—— * 冻结场景 4.P2 要求开关关闭时字段不存在)。 */ wallpaperVideoLandscape?: string; - /** 竖版壁纸视频 COS URL(画廊壁纸卡用,.mp4;条件展开同上)。 */ + /** 竖版壁纸视频 COS URL(历史 legacy,不再新写;存量行照常展开,画廊 legacy 回退用)。 */ wallpaperVideoPortrait?: string; + /** 单腿原生比例壁纸视频 COS URL(20260928 新增;画廊视频变体优先级 native > portrait > landscape)。 */ + wallpaperVideoNative?: string; photos: ManifestPhoto[]; } @@ -107,11 +109,16 @@ export function wallpaperVideoLandscapeCosKey(pickDate: string): string { return `${config.cos.prefix}/wallpaper-videos/${pickDate}_landscape.mov`; } -/** 竖版壁纸视频 key:`relight/wallpaper-videos/{pickDate}_portrait.mp4` */ +/** 竖版壁纸视频 key:`relight/wallpaper-videos/{pickDate}_portrait.mp4`(历史 legacy,不再新写) */ export function wallpaperVideoPortraitCosKey(pickDate: string): string { return `${config.cos.prefix}/wallpaper-videos/${pickDate}_portrait.mp4`; } +/** 单腿原生比例壁纸视频 key:`relight/wallpaper-videos/{pickDate}_native.mp4`(20260928 新增) */ +export function wallpaperVideoNativeCosKey(pickDate: string): string { + return `${config.cos.prefix}/wallpaper-videos/${pickDate}_native.mp4`; +} + /** 单张缩略图 key:`relight/photos/{photoId}-thumb.jpg` */ export function photoThumbCosKey(photoId: string): string { return `${config.cos.prefix}/photos/${photoId}-thumb.jpg`; @@ -167,6 +174,7 @@ interface PickRow { composedImagePath: string | null; wallpaperVideoLandscapeUrl: string | null; wallpaperVideoPortraitUrl: string | null; + wallpaperVideoNativeUrl: string | null; } interface EntryRow { dailyPickId: string; @@ -223,7 +231,8 @@ export async function buildManifest(): Promise { .prepare( `SELECT id, pick_date AS pickDate, title, narrative, composed_image_path AS composedImagePath, wallpaper_video_landscape_url AS wallpaperVideoLandscapeUrl, - wallpaper_video_portrait_url AS wallpaperVideoPortraitUrl + wallpaper_video_portrait_url AS wallpaperVideoPortraitUrl, + wallpaper_video_native_url AS wallpaperVideoNativeUrl FROM daily_picks ORDER BY pick_date ASC`, ) @@ -348,6 +357,10 @@ export async function buildManifest(): Promise { if (p.wallpaperVideoPortraitUrl) { day.wallpaperVideoPortrait = p.wallpaperVideoPortraitUrl; } + // native(20260928):条件展开语义与既有两字段一致——列空/空串 → 字段缺省 + if (p.wallpaperVideoNativeUrl) { + day.wallpaperVideoNative = p.wallpaperVideoNativeUrl; + } return day; }); diff --git a/apps/backend/src/lib/wallpaper/native-canvas.ts b/apps/backend/src/lib/wallpaper/native-canvas.ts new file mode 100644 index 0000000..af23a6f --- /dev/null +++ b/apps/backend/src/lib/wallpaper/native-canvas.ts @@ -0,0 +1,82 @@ +/** + * 壁纸视频生成画布 SSOT(20260928 单腿原生比例,state.md ## 设计文档 D1 / ## 实现绑定表) + * + * computeNativeCanvas(origW, origH) → {width, height}: + * 生成画布跟随 hero 原图比例(32 取整 + 像素预算封顶),竖图横图统一处理: + * 归一化长短轴比 R 恒 ≥1,朝向(landscape/portrait)由原图独立决定。 + * + * 引擎事实(已验证):mmh3turbo `--width/--height` 逐轴覆盖档位(须 32 倍数); + * 锚定帧 LANCZOS 直接拉伸到画布(不裁剪)→ 调用方预裁比例必须精确等于画布比例; + * fps 恒 24;输出画布与输入图尺寸无关。 + * + * 算法(design D1 伪代码 1:1 落地): + * R = clamp(max(w,h)/min(w,h), 1.0, 2.40) + * 主循环: L(长轴候选)从 1600 按 32 递减 + * s = round32(L / R) + * 若 L*s ≤ BUDGET 且 s ≥ MIN_SHORT → 按朝向返回 + * 兜底(极端比例,主循环恒不满足): + * L = round32(min(MIN_SHORT*R, BUDGET/MIN_SHORT))(回退防溢出预算), s = MIN_SHORT + * + * 参考输出(四舍五入 round32):1:1→1056×1056;3:2→1312×864;16:9→1440×800; + * 9:16→800×1440;4:3→1248×928;3:4→928×1248;2.35:1→1664×704;比例偏差全部 ≤1.3%。 + */ + +/** 像素预算 = 736×1600(现行竖版像素上限,注意力 token 等价预算) */ +export const NATIVE_CANVAS_PIXEL_BUDGET = 1_177_600; +/** 引擎质量纪律下限(短轴 ≥700 纪律取 32 倍数档 704) */ +export const NATIVE_CANVAS_MIN_SHORT = 704; +/** 长短轴比 clamp 上界(超过按 2.40 处理,防止全景图产出极端窄条) */ +export const NATIVE_CANVAS_MAX_RATIO = 2.4; +/** 长轴候选起点(预算内最大常见档) */ +const MAX_LONG = 1600; +const STEP = 32; + +/** 四舍五入到 32 的倍数(mmh3turbo 画布约束) */ +function round32(n: number): number { + return Math.round(n / STEP) * STEP; +} + +export interface NativeCanvas { + width: number; + height: number; +} + +/** + * 由 hero 原图尺寸计算单腿原生生成画布(width×height,均 32 倍数)。 + * + * @param origW hero 原图宽(EXIF 旋转后像素) + * @param origH hero 原图高(EXIF 旋转后像素) + * @throws Error 原图尺寸非法(非正数/非有限) + */ +export function computeNativeCanvas(origW: number, origH: number): NativeCanvas { + if (!Number.isFinite(origW) || !Number.isFinite(origH) || origW <= 0 || origH <= 0) { + throw new Error(`computeNativeCanvas 原图尺寸非法: ${origW}×${origH}`); + } + // 归一化长短轴比:恒 ≥1(竖图横图统一处理),朝向与 R 解耦 + const r = Math.min( + NATIVE_CANVAS_MAX_RATIO, + Math.max(1, Math.max(origW, origH) / Math.min(origW, origH)), + ); + const landscape = origW >= origH; + + // 主循环:长轴候选从 1600 按 32 递减,找首个「像素 ≤ 预算 且 短轴 ≥ 下限」的组合 + for (let long = MAX_LONG; long >= NATIVE_CANVAS_MIN_SHORT; long -= STEP) { + const short = round32(long / r); + if (long * short <= NATIVE_CANVAS_PIXEL_BUDGET && short >= NATIVE_CANVAS_MIN_SHORT) { + return landscape ? { width: long, height: short } : { width: short, height: long }; + } + } + + // 兜底(极端比例:round32(L/R) 在全域 < MIN_SHORT):短轴钉在 MIN_SHORT, + // 长轴 = round32(min(MIN_SHORT*R, BUDGET/MIN_SHORT));round32 向上取整可能溢出预算, + // 再按 32 递减兜回(保证像素 ≤ BUDGET) + let long = round32( + Math.min(NATIVE_CANVAS_MIN_SHORT * r, NATIVE_CANVAS_PIXEL_BUDGET / NATIVE_CANVAS_MIN_SHORT), + ); + while (long * NATIVE_CANVAS_MIN_SHORT > NATIVE_CANVAS_PIXEL_BUDGET && long > STEP) { + long -= STEP; + } + return landscape + ? { width: long, height: NATIVE_CANVAS_MIN_SHORT } + : { width: NATIVE_CANVAS_MIN_SHORT, height: long }; +} diff --git a/apps/backend/src/lib/wallpaper/video.ts b/apps/backend/src/lib/wallpaper/video.ts index 568f639..283f94e 100644 --- a/apps/backend/src/lib/wallpaper/video.ts +++ b/apps/backend/src/lib/wallpaper/video.ts @@ -1,12 +1,12 @@ /** - * 壁纸视频生成模块(动态视频壁纸,state.md ## 后端设计 3 / ## 契约规约 计算/spawn 契约;v2 增量任务 12-15) + * 壁纸视频生成模块(动态视频壁纸,state.md ## 后端设计 3 / ## 契约规约 计算/spawn 契约; + * 20260928 单腿原生比例改版:固定横竖两腿 → 单腿原生画布,画布 SSOT 在 lib/wallpaper/native-canvas.ts) * - * preprocessHeroFrame(photoPath, width, height, opts) → tmp png 绝对路径 - * sharp cover 裁剪+resize 到目标画布比例(横 1280×704 / 竖 736×1600,32 倍数约束)。 - * v2 人脸构图裁剪:opts.faceBbox(faces 表最大 bbox)→「脸占画布高度 ≥1/4」推 crop - * 窗口(中心对齐人脸);缺失/退化 → 回退现状中心构图。 + * preprocessHeroFrame(photoPath, width, height) → tmp png 绝对路径 + * sharp cover 微裁+resize 到目标画布(32 倍数约束,画布由 computeNativeCanvas 按原图比例算出)。 + * 原生比例下构图已忠实,预裁仅做比例对齐与预算缩放(20260928 去除 v2 人脸窗口裁剪)。 * honeydo first-frame 引擎直接 LANCZOS 拉伸到画布——调用方必须先按画布比例 cover-crop, - * 否则非 16:9 源图会被拉变形。 + * 否则源图会被拉变形(预裁比例 == 画布比例时拉伸无感)。 * * spawnHoneydoVideo({cliPath, prompt, firstFrame, lastFrame, outPath, seconds, res, timeoutMs}) * → {outPath, duration, stdout} @@ -19,15 +19,21 @@ * palindrome 拼接(split/reverse/concat,forward+reverse 交替;音轨 areverse 同构) * 至 ≥targetSeconds ∧ 偶数段;错误枚举 LoopBuildError(ffmpeg 失败 / 拼接后时长 < targetSeconds-1)。 * - * renderTextOverlay(videoPath, meta, opts) → {overlaidPath}(v2 增量任务 14) + * renderTextOverlay(videoPath, meta, opts) → {overlaidPath}(v2 增量任务 14;20260928 props 扩展) + * meta 增 canvasWidth/canvasHeight(生成画布,透传 Remotion props + calculateMetadata 动态尺寸); + * comp 选择按画布比例(≥0.9 横版两栏 / <0.9 竖版全屏),不再按输入视频探测尺寸。 * spawn `npx remotion render`(cwd=videoWorkspacePath/wallpaper-overlay 工程、npx 绝对路径、 * AbortController 900s 超时(v2.1)、stdout tail 留证);错误枚举 OverlayRenderError * (工程/Remotion 运行时缺失不自动安装 / 非零退出 / 产物缺失 / 超时)。 * - * transcodeForAerial(src, dst):HEVC(hvc1) .mov 1920×1080 无音轨 +faststart(Aerial 用, - * v2 输入=Remotion 成品)。hevc_videotoolbox 失败 fallback libx265。超时 600s。 + * transcodeForAerialNative(src, dst)(20260928 单腿条件 Aerial):画布比例 ∈ [1.5,1.9] 才产出—— + * ffmpeg 居中裁到 16:9 → scale=1920:1080:flags=lanczos,setsar=1(crop 前置保证入 scale 前 + * 比例恰 16:9,绝不拉伸)→ HEVC(hvc1) .mov 带音轨 aac 128k +faststart。 + * hevc_videotoolbox 失败 fallback libx265。超时 600s。 * transcodeForGallery(src, dst)(v2):H.264 mp4 重编码保留音轨(libx264 crf18 + aac 128k - * +faststart,画廊静音自动播放+点击开声)。超时 120s。 + * +faststart,无 -vf 尺寸透传——分辨率 == 源画布),画廊消费原生比例。超时 120s。 + * assertVideoDimensions(filePath, expectedWidth, expectedHeight)(20260928 尺寸护栏): + * ffprobe 断言转码产物尺寸,不匹配即 throw(job 失败 → 当日回退静态,fail-safe,禁静默失败)。 * * assertVideoSpawnPrerequisites(photoPath):honeydo bin、ffmpeg、原图路径 + * wallpaper-overlay 工程/Remotion 运行时/字体 存在校验(缺失报错,不自动安装)。 @@ -42,19 +48,27 @@ import { convertHeicToJpeg, isHeicBuffer } from "../heic"; import { buildCaptureDateline } from "./capture"; // ============================================================================ -// 画布与档位常量(honeydo 32 倍数约束:720p=1280×704;竖版经 --width/--height -// 逐轴覆盖 portrait 档 → 736×1600,比例 0.46 与静态竖版壁纸 1290×2796 对齐, -// 画廊手机端 cover 裁切从 ~20% 降到 <0.5%) +// 档位与兼容窗口常量(20260928 单腿原生:画布 SSOT 在 native-canvas.ts; +// -r 档位保留 720p 保 stdout 回执语义,画布由 --width/--height 显式覆盖) // ============================================================================ -/** 横版生成画布(honeydo res 档 720p;--width/--height 显式传同值) */ -export const WALLPAPER_VIDEO_LANDSCAPE_CANVAS = { width: 1280, height: 704 } as const; -/** 竖版生成画布(portrait 档 + --width/--height 逐轴覆盖;736=23×32、1600=50×32) */ -export const WALLPAPER_VIDEO_PORTRAIT_CANVAS = { width: 736, height: 1600 } as const; -/** honeydo res 档位:横版 */ -export const WALLPAPER_VIDEO_LANDSCAPE_RES = "720p"; -/** honeydo res 档位:竖版 */ -export const WALLPAPER_VIDEO_PORTRAIT_RES = "portrait"; +/** honeydo res 档位(契约:-r 保留保 stdout 回执语义;画布两轴经 --width/--height 显式覆盖) */ +export const WALLPAPER_VIDEO_RES = "720p"; + +/** Aerial 兼容窗口(画布长短轴比 ∈ [1.5, 1.9] 才产出 16:9 微裁 .mov;窗外 mac 当日走静态回退) */ +export const AERIAL_COMPAT_RATIO_MIN = 1.5; +export const AERIAL_COMPAT_RATIO_MAX = 1.9; + +/** + * 画布是否落在 Aerial 兼容窗口:**方向性比例** canvasWidth/canvasHeight ∈ [1.5, 1.9] + * (与 D2 overlay comp 选择的 ratio 定义同源——竖版画布 w/h < 1 恒窗外,绝不产 16:9 微裁 + * .mov:竖裁横会砍掉 ~69% 画面,T_crop 纪律只对横版画布成立)。 + */ +export function isAerialCompatCanvas(canvas: { width: number; height: number }): boolean { + if (canvas.height <= 0) return false; + const ratio = canvas.width / canvas.height; + return ratio >= AERIAL_COMPAT_RATIO_MIN && ratio <= AERIAL_COMPAT_RATIO_MAX; +} // ============================================================================ // HoneydoSpawnError @@ -77,10 +91,11 @@ function stdoutTail(stdout: string): string { } // ============================================================================ -// preprocessHeroFrame(v2:人脸构图裁剪) +// preprocessHeroFrame(20260928:比例对齐 cover 微裁,去除人脸窗口) // ============================================================================ -/** 人脸 bbox(EXIF 旋转后原图像素坐标——detect-faces 主流程 rotate 后检测,同空间) */ +/** 人脸 bbox(EXIF 旋转后原图像素坐标——detect-faces 主流程 rotate 后检测,同空间)。 + * 20260928 起仅供 prompt 分层默认(有无人脸)使用,不再参与预裁剪构图。 */ export interface FaceBbox { x: number; y: number; @@ -88,60 +103,13 @@ export interface FaceBbox { h: number; } -export interface PreprocessHeroFrameOptions { - /** hero 照片 faces 表最大 bbox(面积最大);null/缺省 → 回退现状中心构图(契约) */ - faceBbox?: FaceBbox | null; -} - -/** - * 「脸占画布 ≥1/4」的裁剪窗口推导(recipes 纪律:人脸占比 ≥1/4)。 - * - * 口径:脸 bbox 高度映射到画布后 ≥ 画布高度的 1/4(横竖版同为「脸高占画幅高 ≥1/4」)。 - * 缩放关系 face_canvas_h = fh × canvasH / winH ⟹ 约束 ⟺ 窗口高 winH ≤ 4 × fh。 - * 窗口高 = clamp(4×fh, 下限=画布高(防输入图超分放大,画质优先), 上限=全幅 cover 窗口高 - * (脸在整图已 ≥1/4 时不再放大));窗口宽 = 窗口高 × 画布比例;中心对齐人脸中心,clamp 图内。 - * - * @returns 裁剪窗口 {left, top, width, height};无法按人脸构图时返回 null(中心构图回退) - */ -function computeFaceCropWindow( - imgW: number, - imgH: number, - canvasW: number, - canvasH: number, - face: FaceBbox, -): { left: number; top: number; width: number; height: number } | null { - // bbox 合法性:先 clamp 进图内,退化(宽/高 ≤0)则放弃人脸构图 - const fx = Math.min(Math.max(face.x, 0), imgW - 1); - const fy = Math.min(Math.max(face.y, 0), imgH - 1); - const fw = Math.min(face.w, imgW - fx); - const fh = Math.min(face.h, imgH - fy); - if (!(fw > 0 && fh > 0)) return null; - - const ratio = canvasW / canvasH; - // 全幅 cover 窗口高(保持画布比例的最大窗口) - const fullH = Math.min(imgH, imgW / ratio); - // 下限:窗口高 ≥ 画布高 → 缩放系数 ≤1,不超分放大(v2 画质优先) - const minWinH = Math.min(canvasH, fullH); - const winH = Math.min(Math.max(Math.min(fullH, 4 * fh), minWinH), imgH); - const winW = Math.min(Math.round(winH * ratio), imgW); - const winHr = Math.round(winH); - - // 窗口中心对齐人脸中心,clamp 到图内 - const cx = fx + fw / 2; - const cy = fy + fh / 2; - const left = Math.min(Math.max(Math.round(cx - winW / 2), 0), imgW - winW); - const top = Math.min(Math.max(Math.round(cy - winHr / 2), 0), imgH - winHr); - return { left, top, width: winW, height: winHr }; -} - /** - * hero 原图预裁剪:sharp cover 裁剪+resize 到目标画布(width×height),产 tmp png。 + * hero 原图预裁剪:sharp cover 微裁+resize 到目标画布(width×height),产 tmp png。 * - * HEIC 源先经 convertHeicToJpeg 解码(复用 lib/heic,与 composer 同源); - * .rotate() 按 EXIF 方向摆正后再裁剪(faces bbox 与 detect-faces 同为 rotate 后空间)。 - * - * v2 人脸构图裁剪:opts.faceBbox 非空 → 按「脸占画布 ≥1/4」推 crop 窗口(中心对齐人脸); - * 缺失/退化 → 回退现状中心构图(契约)。 + * 20260928 单腿原生比例:画布由 computeNativeCanvas 按原图比例算出,预裁仅做 + * 「比例对齐 + 预算缩放」(cover 微裁量 ≤ 32 取整容差级),**去除 v2 人脸窗口裁剪**—— + * 原生比例下构图已忠实,不再为脸重构图。HEIC 源先经 convertHeicToJpeg 解码(复用 + * lib/heic,与 composer 同源);.rotate() 按 EXIF 方向摆正后再裁剪。 * * @returns tmp png 绝对路径(调用方负责清理) */ @@ -149,36 +117,16 @@ export async function preprocessHeroFrame( photoPath: string, width: number, height: number, - opts: PreprocessHeroFrameOptions = {}, ): Promise { let buf = await import("node:fs/promises").then((m) => m.readFile(photoPath)); if (isHeicBuffer(buf)) { - buf = await convertHeicToJpeg(buf, { quality: 90 }); // 不预缩:faces bbox 是旋转后原图像素坐标,预缩会错配(v2.1 修复) + buf = await convertHeicToJpeg(buf, { quality: 90 }); } const outPath = path.join( tmpdir(), `wallpaper-video-frame-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}.png`, ); - if (opts.faceBbox) { - // 人脸构图:先 rotate 摆正拿真实尺寸,再按窗口 extract → resize(两步避免模糊语义) - const rotated = await sharp(buf).rotate().toBuffer(); - const meta = await sharp(rotated).metadata(); - const imgW = meta.width ?? 0; - const imgH = meta.height ?? 0; - const win = - imgW > 0 && imgH > 0 ? computeFaceCropWindow(imgW, imgH, width, height, opts.faceBbox) : null; - if (win) { - await sharp(rotated) - .extract(win) - .resize(width, height, { fit: "cover" }) - .png() - .toFile(outPath); - return outPath; - } - // bbox 退化 → 落入中心构图回退 - } - await sharp(buf) .rotate() .resize(width, height, { fit: "cover", position: "centre" }) @@ -191,6 +139,27 @@ export async function preprocessHeroFrame( // spawnHoneydoVideo // ============================================================================ +/** + * 读 hero 原图 **EXIF 旋转后**有效尺寸(与 preprocessHeroFrame 的 .rotate() 同空间)—— + * computeNativeCanvas 的朝向判定必须基于旋转后尺寸,否则带 EXIF 旋转的竖拍照片会算错朝向。 + * HEIC 源先经 convertHeicToJpeg 解码(与 preprocessHeroFrame 同源)。 + * orientation 5-8(90°/270° 旋转)时宽高互换。 + */ +export async function readOrientedDimensions( + photoPath: string, +): Promise<{ width: number; height: number }> { + let buf = await import("node:fs/promises").then((m) => m.readFile(photoPath)); + if (isHeicBuffer(buf)) { + buf = await convertHeicToJpeg(buf, { quality: 90 }); + } + const meta = await sharp(buf).metadata(); + const w = meta.width ?? 0; + const h = meta.height ?? 0; + const o = meta.orientation ?? 1; + const swapped = o >= 5 && o <= 8; + return { width: swapped ? h : w, height: swapped ? w : h }; +} + export interface HoneydoVideoOptions { /** honeydo CLI 绝对路径(config.honeydoCliPath) */ cliPath: string; @@ -404,19 +373,24 @@ function spawnFfmpeg(args: string[], timeoutMs: number): Promise { } /** - * Aerial 用转码:HEVC(hvc1) .mov 1920×1080 **带音轨**(aac 128k,2026-09-13 验收要求 - * 以后生成的视频带音轨;Aerial 注入无声播放不受影响,下载/外放场景有环境音)+faststart。 + * Aerial 用条件转码(20260928 单腿原生):HEVC(hvc1) .mov 1920×1080 **带音轨** + * (aac 128k,2026-09-13 验收要求以后生成的视频带音轨)+faststart。 + * + * 滤镜链 = **crop 前置 + scale 后置**:先把源居中裁到恰 16:9(`crop=min(iw,ih*16/9):min(ih,iw*9/16)` + * 横竖超宽窄高统一处理),再 `scale=1920:1080:flags=lanczos,setsar=1` 等比放大—— + * crop 已保证入 scale 前比例恰 16:9,故 scale 无非等比形变(**绝不拉伸**)。 + * 调用方须先以 isAerialCompatCanvas 把关(画布比例 ∈ [1.5,1.9],最坏裁切占比 ≤0.15)。 * * hevc_videotoolbox 失败(非本机硬件编码器/旧系统)→ fallback libx265 -crf 22。 * 超时 600s。 */ -export async function transcodeForAerial(src: string, dst: string): Promise { +export async function transcodeForAerialNative(src: string, dst: string): Promise { const common = [ "-y", "-i", src, "-vf", - "scale=1920:1080:flags=lanczos", + "crop=w='min(iw,ih*16/9)':h='min(ih,iw*9/16)',scale=1920:1080:flags=lanczos,setsar=1", "-c:a", "aac", "-b:a", @@ -437,6 +411,39 @@ export async function transcodeForAerial(src: string, dst: string): Promise { + let probe: ProbeResult; + try { + probe = await probeVideoFile(filePath); + } catch (e) { + throw new DimensionAssertError( + `转码产物尺寸断言失败(ffprobe 探测异常): ${filePath} 期望 ${expectedWidth}×${expectedHeight},` + + `探测错误: ${e instanceof Error ? e.message : String(e)}`, + ); + } + if (probe.width !== expectedWidth || probe.height !== expectedHeight) { + throw new DimensionAssertError( + `转码产物尺寸断言失败: ${filePath} 期望 ${expectedWidth}×${expectedHeight},` + + `实际 ${probe.width}×${probe.height}(禁静默失败——错尺寸产物不得入库/上传)`, + ); + } +} + /** * 画廊用转码(v2):H.264 mp4 重编码,**保留音轨**(libx264 crf18 + aac 128k +faststart)。 * 输入 = Remotion 文字层成品(同分辨率);画廊静音自动播放 + 点击开声。超时 120s。 @@ -653,6 +660,10 @@ export interface TextOverlayMeta { narrative: string; /** hero 照片拍摄时刻 ISO(footer「拍摄于 …· N 年前」同源 web/静态壁纸);null/缺省 → footer 不渲染 */ takenAt?: string | null; + /** 生成画布宽(20260928 单腿原生:透传 Remotion props + calculateMetadata 动态 comp 尺寸) */ + canvasWidth: number; + /** 生成画布高(同上) */ + canvasHeight: number; } /** @@ -703,7 +714,8 @@ function resolveNpxPath(): string { * stdout/stderr tail 留证)。 * * 输入视频拷入工程 public/(staticFile 服务范围),渲染完成即清理; - * composition 按输入分辨率选(宽>高 → landscape,否则 portrait); + * composition 按生成画布比例选(canvasWidth/canvasHeight ≥0.9 → landscape 两栏, + * 否则 portrait 全屏;画布经 props 透传,Root.tsx calculateMetadata 动态定 comp 尺寸); * --frames=0-N 约束渲染时长 = 输入时长(ffprobe 探测,帧率同源)。 * * 错误枚举 OverlayRenderError:工程/Remotion 运行时缺失(不自动安装)/ @@ -751,8 +763,10 @@ export async function renderTextOverlay( `renderTextOverlay ffprobe 探测失败: ${e instanceof Error ? e.message : String(e)} (${videoPath})`, ); } - const compId = - probe.width > probe.height ? "wallpaper-overlay-landscape" : "wallpaper-overlay-portrait"; + // comp 选择(20260928 单腿原生):按生成画布比例判定(≥0.9 横版两栏 / <0.9 竖版全屏), + // 不再按输入视频探测尺寸——输入视频本就是该画布的 loop 产物(比例一致) + const canvasRatio = meta.canvasHeight > 0 ? meta.canvasWidth / meta.canvasHeight : 0; + const compId = canvasRatio >= 0.9 ? "wallpaper-overlay-landscape" : "wallpaper-overlay-portrait"; const frames = Math.max(1, Math.ceil(probe.duration * probe.fps)); // 输入视频拷入工程 public/(渲染完成即清理) @@ -765,12 +779,16 @@ export async function renderTextOverlay( path.dirname(absVideoPath), `${path.basename(absVideoPath).replace(/\.mp4$/i, "")}-overlay.mp4`, ); + // props 契约(20260928 契约 7 纯超集扩展):{videoPath, pickDate, title, narrative, + // captureDateline, canvasWidth, canvasHeight} const props = { videoPath: OVERLAY_PUBLIC_INPUT, pickDate: meta.pickDate, title: meta.title, narrative: meta.narrative, captureDateline: buildCaptureDateline(meta.takenAt), + canvasWidth: meta.canvasWidth, + canvasHeight: meta.canvasHeight, }; const args = [ "remotion", diff --git a/apps/backend/src/routes/__tests__/daily-select-acceptance.test.ts b/apps/backend/src/routes/__tests__/daily-select-acceptance.test.ts index 9c74510..ea7e6ae 100644 --- a/apps/backend/src/routes/__tests__/daily-select-acceptance.test.ts +++ b/apps/backend/src/routes/__tests__/daily-select-acceptance.test.ts @@ -115,6 +115,7 @@ function createTestDb() { members TEXT NOT NULL DEFAULT '[]', wallpaper_video_landscape_url TEXT, wallpaper_video_portrait_url TEXT, + wallpaper_video_native_url TEXT, motion_prompt TEXT, created_at TEXT NOT NULL ); diff --git a/apps/backend/src/routes/__tests__/daily-select-sync.acceptance.test.ts b/apps/backend/src/routes/__tests__/daily-select-sync.acceptance.test.ts index ae27160..a8889fa 100644 --- a/apps/backend/src/routes/__tests__/daily-select-sync.acceptance.test.ts +++ b/apps/backend/src/routes/__tests__/daily-select-sync.acceptance.test.ts @@ -117,6 +117,7 @@ function createTestDb() { members TEXT NOT NULL DEFAULT '[]', wallpaper_video_landscape_url TEXT, wallpaper_video_portrait_url TEXT, + wallpaper_video_native_url TEXT, motion_prompt TEXT, created_at TEXT NOT NULL ); diff --git a/apps/backend/src/routes/__tests__/daily.wallpaper.portrait.acceptance.test.ts b/apps/backend/src/routes/__tests__/daily.wallpaper.portrait.acceptance.test.ts index d402267..31f33fa 100644 --- a/apps/backend/src/routes/__tests__/daily.wallpaper.portrait.acceptance.test.ts +++ b/apps/backend/src/routes/__tests__/daily.wallpaper.portrait.acceptance.test.ts @@ -139,6 +139,7 @@ function createTestDb() { title TEXT NOT NULL, narrative TEXT NOT NULL, score REAL NOT NULL DEFAULT 0, wallpaper_video_landscape_url TEXT, wallpaper_video_portrait_url TEXT, + wallpaper_video_native_url TEXT, motion_prompt TEXT, composed_image_path TEXT, created_at TEXT NOT NULL, members TEXT DEFAULT '[]' ); diff --git a/apps/gallery/__tests__/gallery-wallpaper-video-native.e2e.acceptance.test.ts b/apps/gallery/__tests__/gallery-wallpaper-video-native.e2e.acceptance.test.ts new file mode 100644 index 0000000..4f5cd96 --- /dev/null +++ b/apps/gallery/__tests__/gallery-wallpaper-video-native.e2e.acceptance.test.ts @@ -0,0 +1,485 @@ +/** + * 验收测试(红队 E2E):画廊单腿原生壁纸视频 — native 优先级 + 任意比例渲染 + 视觉残留 + * + * 设计文档(state.md §契约规约 4 / §验收场景)对应谓词: + * - 场景 8.P1 [det-machine]:Playwright 画廊视频卡 videoWidth/videoHeight 比例 == + * 画布比例 ±0.01 且 readyState≥2(fixture 原生视频 1312×864 = 3:2 原生画布档; + * 若 app.js 仍取 legacy 源或回退静态,比例/存在性断言红) + * - 场景 8.P2 [visual-residue]:截图无拉伸变形/无错位黑边(二值清单) + * // VISUAL_RESIDUE: 留 QA 真机判定——产出卡片截图 + 二值清单 artifact, + * // 自动化仅做代理断言(video 元素可见且占位非零),人判以清单为准 + * - 场景 6.P4 [det-machine]:legacy-only 日(portrait 非空、native 缺省)→ 画廊视频 + * 变体与下载 URL 取 portrait key(契约 4 优先级 native > portrait > landscape 的 + * legacy 回退面);isDynamic 判定(视频变体仍在播)与 forceStatic 降级重建路径 + * (拦截 portrait.mp4 → 404 → 静态 img 重建)不回归 + * + * 契约 4 逐字:画廊视频变体与下载按钮 URL = native > portrait > landscape + * (apps/gallery/app.js 仅改 URL 取值处,布局/动作栏逻辑不动)。 + * + * 红队铁律:不读 app.js/app.css/index.html 内容(蓝队并行产出)。 + * driver = 本地 fixture manifest + 本地真实 mp4(native 1312×864 由 ffmpeg 现造—— + * ffmpeg 为项目硬依赖,缺失即真红;legacy 用 fixtures 既有 736×1600 样本)。 + * fixture 惯例沿 gallery-wallpaper-video.e2e.acceptance.test.ts(隔离临时目录 + python3 + * http.server --bind 127.0.0.1)。 + */ +import { type ChildProcess, spawn, spawnSync } from "node:child_process"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import { expect, test } from "@playwright/test"; +const { describe, beforeAll, afterAll, beforeEach } = test; +const it = test; +import type { Page } from "@playwright/test"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +const SRC_GALLERY_DIR = path.resolve(__dirname, "../"); +/** legacy 样本:fixtures 既有 736×1600 竖版壁纸视频(可播放,Chromium 可解码) */ +const LEGACY_PORTRAIT_SAMPLE = path.join(__dirname, "fixtures", "wallpaper-portrait-736x1600.mp4"); + +const PORT_NATIVE = 8794; // native-only 日(1312×864 原生画布档) +const PORT_LEGACY = 8795; // legacy-only 日(portrait 736×1600) +const BASE_NATIVE = `http://127.0.0.1:${PORT_NATIVE}`; +const BASE_LEGACY = `http://127.0.0.1:${PORT_LEGACY}`; +const ARTIFACT_DIR = "/tmp/autopilot-artifacts"; + +/** 原生画布档(3:2 hero → computeNativeCanvas D1 参考输出) */ +const NATIVE_W = 1312; +const NATIVE_H = 864; + +// ---- fixture 占位 JPEG(既有同款 base64,零运行时依赖)---- +const TINY_JPEG_B64 = + "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAP//////////////////////////////////////////////////////////////////////////////////////2wBDAf//////////////////////////////////////////////////////////////////////////////////////wAARCAABAAEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6M0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwAAAwEBAgEBAQEBAQAAAAEAAgIDAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmGmaq6OkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3iigoAKKKKACiiigA/9k="; +const WP_LANDSCAPE_JPEG_B64 = + "/9j/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//2wBDAQ4ODhMREyYVFSZPNS01T09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0//wAARCAAEAAgDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAP/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AJAA/9k="; +const WP_PORTRAIT_JPEG_B64 = + "/9j/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//2wBDAQ4ODhMREyYVFSZPNS01T09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0//wAARCAAIAAQDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAX/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AJAAP//Z"; + +function writeJpeg(filePath: string, b64: string): void { + fs.mkdirSync(path.dirname(filePath), { recursive: true }); + fs.writeFileSync(filePath, Buffer.from(b64, "base64")); +} + +interface FixtureDirs { + dir: string; + manifest: { days: Array>; videos: Array> }; +} + +/** + * 构建隔离 fixture 目录。 + * variant="native":当日 manifest 仅含 wallpaperVideoNative(1312×864 真实 mp4,ffmpeg 现造)。 + * variant="legacy":当日 manifest 仅含 wallpaperVideoPortrait(fixtures 既有 736×1600 样本)。 + */ +function buildFixture(port: number, variant: "native" | "legacy"): FixtureDirs { + const dir = path.join(os.tmpdir(), `relight-gallery-wvn-e2e-${port}`); + fs.rmSync(dir, { recursive: true, force: true }); + fs.mkdirSync(dir, { recursive: true }); + for (const f of ["index.html", "app.css", "app.js"]) { + fs.copyFileSync(path.join(SRC_GALLERY_DIR, f), path.join(dir, f)); + } + const fontsDir = path.join(SRC_GALLERY_DIR, "fonts"); + if (fs.existsSync(fontsDir)) { + fs.cpSync(fontsDir, path.join(dir, "fonts"), { recursive: true }); + } + + const todayStr = new Date().toISOString().slice(0, 10); + const yesterdayStr = new Date(Date.now() - 24 * 3600 * 1000).toISOString().slice(0, 10); + + const wpLandscape = `wallpapers/${todayStr}_v2-contain-default.jpg`; + const wpPortrait = `wallpapers/${todayStr}_v2-contain-1290x2796.jpg`; + writeJpeg(path.join(dir, wpLandscape), WP_LANDSCAPE_JPEG_B64); + writeJpeg(path.join(dir, wpPortrait), WP_PORTRAIT_JPEG_B64); + + fs.mkdirSync(path.join(dir, "videos"), { recursive: true }); + let videoField: Record; + if (variant === "native") { + // 场景 8.P1 fixture:1312×864 原生画布档真实 mp4(10s,规避采样窗口内播完) + const nativeMp4 = `videos/${todayStr}_native.mp4`; + const r = spawnSync( + "ffmpeg", + [ + "-y", + "-f", + "lavfi", + "-i", + `testsrc2=size=${NATIVE_W}x${NATIVE_H}:rate=24:duration=10`, + "-f", + "lavfi", + "-i", + "sine=frequency=440:sample_rate=44100:duration=10", + "-c:v", + "libx264", + "-pix_fmt", + "yuv420p", + "-c:a", + "aac", + "-shortest", + path.join(dir, nativeMp4), + ], + { encoding: "utf-8", timeout: 60000 }, + ); + if (r.status !== 0 || !fs.existsSync(path.join(dir, nativeMp4))) { + throw new Error(`ffmpeg 造 native fixture 失败(红队铁律:fixture 失败即真红): ${r.stderr}`); + } + videoField = { wallpaperVideoNative: nativeMp4 }; + } else { + const portraitMp4 = `videos/${todayStr}_portrait.mp4`; + fs.copyFileSync(LEGACY_PORTRAIT_SAMPLE, path.join(dir, portraitMp4)); + videoField = { wallpaperVideoPortrait: portraitMp4 }; + } + + const mkPhoto = (id: string, rank: number) => { + writeJpeg(path.join(dir, "photos", `${id}-thumb.jpg`), TINY_JPEG_B64); + writeJpeg(path.join(dir, "photos", `${id}-mid.jpg`), TINY_JPEG_B64); + return { + photoId: id, + rank, + title: `第 ${rank} 张`, + narrative: `第 ${rank} 张的叙事文案,长度足够通过非空断言。`, + thumbnail: `photos/${id}-thumb.jpg`, + original: `photos/${id}-mid.jpg`, + takenAt: "2024-07-15T10:00:00.000Z", + width: 4032, + height: 3024, + }; + }; + + const todayDay: Record = { + pickDate: todayStr, + title: "今日精选", + narrative: "今日的整体叙事。", + wallpaperLandscape: wpLandscape, + wallpaperPortrait: wpPortrait, + photos: [mkPhoto(`photo-today-01-${port}`, 1), mkPhoto(`photo-today-02-${port}`, 2)], + // §契约规约:条件展开——仅非空时注入当日视频变体字段(native-only / legacy-only 两态) + ...videoField, + }; + const yesterdayDay: Record = { + pickDate: yesterdayStr, + title: "昨日精选", + narrative: "昨日的整体叙事。", + wallpaperLandscape: null, + wallpaperPortrait: null, + photos: [mkPhoto(`photo-yesterday-01-${port}`, 1)], + }; + + const manifest = { + generatedAt: new Date().toISOString(), + days: [todayDay, yesterdayDay], + videos: [] as Array>, + }; + fs.writeFileSync(path.join(dir, "manifest.json"), JSON.stringify(manifest, null, 2)); + return { dir, manifest }; +} + +// ---- 静态服务(--bind 127.0.0.1,规避 dual-stack 坑,既有惯例)---- + +const servers: ChildProcess[] = []; +const fixtureRoots: string[] = []; +let nativeFixtureDir = ""; +let legacyFixtureDir = ""; + +async function startStaticServer(port: number, dir: string): Promise { + const proc = spawn( + "python3", + ["-m", "http.server", String(port), "--bind", "127.0.0.1", "--directory", dir], + { stdio: ["ignore", "pipe", "pipe"] }, + ); + servers.push(proc); + fixtureRoots.push(dir); + const deadline = Date.now() + 8000; + while (Date.now() < deadline) { + try { + const res = await fetch(`http://127.0.0.1:${port}/manifest.json`); + if (res.ok) return; + } catch { + // not ready yet + } + await new Promise((r) => setTimeout(r, 200)); + } + throw new Error(`静态服务 ${port} 未就绪`); +} + +beforeAll(async () => { + fs.mkdirSync(ARTIFACT_DIR, { recursive: true }); + const fxNative = buildFixture(PORT_NATIVE, "native"); + const fxLegacy = buildFixture(PORT_LEGACY, "legacy"); + nativeFixtureDir = fxNative.dir; + legacyFixtureDir = fxLegacy.dir; + await startStaticServer(PORT_NATIVE, nativeFixtureDir); + await startStaticServer(PORT_LEGACY, legacyFixtureDir); +}, 60000); + +afterAll(async () => { + for (const s of servers) s.kill("SIGTERM"); + for (const dir of fixtureRoots) { + try { + fs.rmSync(dir, { recursive: true, force: true }); + } catch { + // ignore + } + } +}); + +beforeEach(async ({ page }) => { + await page.setViewportSize({ width: 390, height: 844 }); + // fetch 直链日志(下载来源断言用;不依赖 app.js 内部) + await page.addInitScript(() => { + const w = window as unknown as { __fetchLog: string[] }; + w.__fetchLog = []; + const origFetch = window.fetch.bind(window); + window.fetch = async (...args: Parameters) => { + const url = typeof args[0] === "string" ? args[0] : (args[0] as Request).url; + w.__fetchLog.push(url); + return origFetch(...args); + }; + }); +}); + +async function writeArtifact(id: string, content: string): Promise { + await fs.promises.writeFile(path.join(ARTIFACT_DIR, `${id}.out`), content); +} + +const WALLPAPER_UNIT = '[data-stream-unit][data-role="wallpaper-card"]'; + +async function scrollWallpaperCardIntoView(page: Page): Promise { + await page.evaluate(() => { + document + .querySelector('[data-stream-unit][data-role="wallpaper-card"]') + ?.scrollIntoView({ behavior: "instant", block: "center" }); + }); +} + +// ============================================================================ +// 场景 8.P1:画廊渲染任意比例原生视频(videoWidth/videoHeight 比例 == 画布比例 ±0.01) +// ============================================================================ + +describe("[场景8.P1] 画廊视频卡渲染原生比例视频(1312×864,3:2 原生画布档)", () => { + it("壁纸卡