Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3b0a1aa743 | |||
| 76e6c6aef1 | |||
| 12fd78191d |
@@ -458,8 +458,6 @@ jobs:
|
||||
name: Frontend Lint
|
||||
runs-on: ci-l2
|
||||
timeout-minutes: 10
|
||||
needs: check-frontend-only
|
||||
if: needs.check-frontend-only.outputs.skip_frontend != 'true'
|
||||
steps:
|
||||
- name: Checkout code
|
||||
shell: sh
|
||||
@@ -578,12 +576,7 @@ jobs:
|
||||
name: PR Build ${{ matrix.service_display }} Image
|
||||
runs-on: runtime-builder
|
||||
timeout-minutes: ${{ matrix.timeout }}
|
||||
needs: check-frontend-only
|
||||
if: |
|
||||
github.event_name == 'pull_request' && (
|
||||
(matrix.service == 'web' && needs.check-frontend-only.outputs.skip_frontend != 'true') ||
|
||||
(matrix.service != 'web' && needs.check-frontend-only.outputs.skip_backend != 'true')
|
||||
)
|
||||
if: github.event_name == 'pull_request'
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -1199,11 +1192,6 @@ jobs:
|
||||
runs-on: runtime-builder
|
||||
timeout-minutes: ${{ matrix.timeout }}
|
||||
needs:
|
||||
- validate-code-quality
|
||||
- validate-type-check
|
||||
- unit-tests
|
||||
- frontend-lint
|
||||
- frontend-unit-test
|
||||
if: startsWith(github.ref, 'refs/tags/v') || (github.event_name == 'push' && github.ref_name == 'main')
|
||||
strategy:
|
||||
fail-fast: false
|
||||
@@ -1277,7 +1265,7 @@ jobs:
|
||||
docker buildx inspect --bootstrap
|
||||
|
||||
- name: Build and push production ${{ matrix.service_display }} image (with retry)
|
||||
shell: bash
|
||||
shell: sh
|
||||
run: |
|
||||
set -eu
|
||||
REGISTRY="xiaoxia-registry.cn-hangzhou.cr.aliyuncs.com/xiaoxiakeji"
|
||||
@@ -1730,30 +1718,6 @@ jobs:
|
||||
echo " frontend-lint: $RESULT_FRONTEND_LINT"
|
||||
echo " frontend-unit-test: $RESULT_FRONTEND_UNIT"
|
||||
echo " build-pr: $RESULT_BUILD_PR"
|
||||
|
||||
# 查询 AI Code Review 状态(跨workflow,读commit status)
|
||||
AI_REVIEW_STATUS="pending"
|
||||
AI_REVIEW_DESC=""
|
||||
STATUS_JSON=$(curl -sH "Authorization: token $GITHUB_TOKEN" \
|
||||
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/commits/${PR_HEAD_SHA}/status" 2>/dev/null || true)
|
||||
if [ -n "$STATUS_JSON" ]; then
|
||||
AI_STATUS=$(echo "$STATUS_JSON" | python3 -c "
|
||||
import json,sys
|
||||
try:
|
||||
data=json.load(sys.stdin)
|
||||
for s in data.get('statuses',[]):
|
||||
if 'AI Code Review' in s.get('context',''):
|
||||
print(s['state']+'|'+s.get('description',''))
|
||||
break
|
||||
except: pass
|
||||
" 2>/dev/null)
|
||||
if [ -n "$AI_STATUS" ]; then
|
||||
AI_REVIEW_STATUS="${AI_STATUS%%|*}"
|
||||
AI_REVIEW_DESC="${AI_STATUS#*|}"
|
||||
fi
|
||||
fi
|
||||
echo " ai-code-review: $AI_REVIEW_STATUS ($AI_REVIEW_DESC)"
|
||||
|
||||
echo ""
|
||||
|
||||
# 判断PR类型
|
||||
@@ -1769,7 +1733,6 @@ jobs:
|
||||
"validate-migration:$RESULT_MIGRATION"
|
||||
"frontend-lint:$RESULT_FRONTEND_LINT"
|
||||
"build-pr:$RESULT_BUILD_PR"
|
||||
"ai-code-review:$AI_REVIEW_STATUS"
|
||||
)
|
||||
|
||||
# 后端检查
|
||||
@@ -1804,11 +1767,6 @@ jobs:
|
||||
for item in "${REQUIRED_GENERAL[@]}"; do
|
||||
name="${item%%:*}"
|
||||
result="${item##*:}"
|
||||
# AI Code Review pending时不阻塞(可能还在跑),等它跑完自然会重跑Gate
|
||||
if [ "$name" = "ai-code-review" ] && [ "$result" = "pending" ]; then
|
||||
echo " ⏳ $name: pending(审查中,暂不阻塞)"
|
||||
continue
|
||||
fi
|
||||
check_job "$name" "$result"
|
||||
done
|
||||
|
||||
@@ -1847,7 +1805,7 @@ jobs:
|
||||
echo "❌ CI Gate: FAILED"
|
||||
echo "失败项: ${FAILED_ITEMS[*]}"
|
||||
echo "gate_result=failure" >> $GITHUB_OUTPUT
|
||||
exit 1
|
||||
exit 0
|
||||
fi
|
||||
|
||||
- name: Report CI trace
|
||||
|
||||
@@ -87,7 +87,7 @@ jobs:
|
||||
runs-on: ci-l2
|
||||
timeout-minutes: 10
|
||||
outputs:
|
||||
report: ${{ steps.report.outputs.report }}
|
||||
report: ${{ steps.smoke.outputs.report }}
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
@@ -201,7 +201,7 @@ jobs:
|
||||
runs-on: ci-l2
|
||||
timeout-minutes: 15
|
||||
outputs:
|
||||
report: ${{ steps.e2e.outputs.report }}
|
||||
report: ${{ steps.smoke.outputs.report }}
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
|
||||
Regular → Executable
+1
-6
@@ -8,11 +8,6 @@ on:
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
|
||||
concurrency:
|
||||
group: pr-automation-${{ gitea.event.pull_request.number }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
auto-approve:
|
||||
name: Auto Approve on CI Green
|
||||
@@ -61,7 +56,7 @@ jobs:
|
||||
name: Auto Merge on CI Green + Approved
|
||||
runs-on: ci-check
|
||||
if: github.event_name == 'pull_request' && !github.event.pull_request.draft && github.event.pull_request.base.ref == 'develop'
|
||||
timeout-minutes: 3 # 短作业模式:检查一次,不满足就退出,由pr-auto-scan每5分钟定时兜底
|
||||
timeout-minutes: 45 # 长等待模式:等CI全绿后自动合并,不遗漏任何PR
|
||||
steps:
|
||||
- name: Checkout code
|
||||
shell: sh
|
||||
|
||||
@@ -120,7 +120,7 @@ jobs:
|
||||
PREVIEW_SSH_KEY: ${{ secrets.PREVIEW_SSH_KEY }}
|
||||
run: |
|
||||
set -eux
|
||||
preview_host="${PREVIEW_SSH_HOST:-47.98.113.167}"
|
||||
preview_host="${PREVIEW_SSH_HOST:-172.30.18.197}"
|
||||
preview_user="${PREVIEW_SSH_USER:-deploy}"
|
||||
preview_port="${PREVIEW_SSH_PORT:-22222}"
|
||||
preview_dir="/var/www/preview/pr-${PR_NUMBER}"
|
||||
|
||||
@@ -101,7 +101,7 @@ jobs:
|
||||
# Install dependencies with retry
|
||||
for i in 1 2 3; do
|
||||
npm ci --no-audit --no-fund && break
|
||||
echo "npm install failed, retry $i/3..."
|
||||
echo "npm ci failed, retry $i/3..."
|
||||
[ $i -eq 3 ] && exit 1
|
||||
rm -rf node_modules
|
||||
sleep 5
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { useRef, useCallback, useEffect } from "react"
|
||||
import { createVoiceClone, toVoiceClone } from "@/api/voice-clone"
|
||||
import { uploadAsset, ensureDefaultLibrary } from "@/api/assets"
|
||||
import { getOrCreateDefaultProject } from "@/api/projects"
|
||||
import { uploadAsset } from "@/api/assets"
|
||||
import type { VoiceClone } from "@/api/voice-clone"
|
||||
|
||||
interface UseCloneSubmitOptions {
|
||||
@@ -62,14 +61,8 @@ export function useCloneSubmit({
|
||||
})
|
||||
}
|
||||
|
||||
// 获取默认项目和素材库(后端 /upload 接口必填)
|
||||
const project = await getOrCreateDefaultProject()
|
||||
const library = await ensureDefaultLibrary({ project_id: project.id, kind: "voice" })
|
||||
|
||||
const formData = new FormData()
|
||||
formData.append("file", fileToUpload)
|
||||
formData.append("project_id", project.id)
|
||||
formData.append("library_id", library.id)
|
||||
const uploadResult = await uploadAsset(formData)
|
||||
|
||||
// 阶段 2:克隆
|
||||
|
||||
Executable → Regular
+130
-21
@@ -1,11 +1,15 @@
|
||||
/**
|
||||
* 绿幕抠像配置面板
|
||||
* 5 种颜色预设 + 自定义颜色 + 相似度/边缘平滑/溢色抑制
|
||||
*/
|
||||
import React, { useCallback } from "react"
|
||||
import { Drawer, Switch } from "antd"
|
||||
import type { ChromaKeyConfig, ChromaKeyColorPreset } from "../types"
|
||||
import { DEFAULT_CHROMA_KEY_CONFIG, CHROMA_KEY_PRESET_COLORS } from "../types"
|
||||
import { GreenScreenPresets } from "./green-screen/GreenScreenPresets"
|
||||
import { GreenScreenCustomColor } from "./green-screen/GreenScreenCustomColor"
|
||||
import { GreenScreenSliders } from "./green-screen/GreenScreenSliders"
|
||||
import { GreenScreenPreview } from "./green-screen/GreenScreenPreview"
|
||||
import {
|
||||
DEFAULT_CHROMA_KEY_CONFIG,
|
||||
CHROMA_KEY_PRESET_LABELS,
|
||||
CHROMA_KEY_PRESET_COLORS,
|
||||
} from "../types"
|
||||
|
||||
interface GreenScreenPanelProps {
|
||||
open: boolean
|
||||
@@ -14,6 +18,9 @@ interface GreenScreenPanelProps {
|
||||
onChange: (config: ChromaKeyConfig) => void
|
||||
}
|
||||
|
||||
/** 预设列表 */
|
||||
const PRESET_LIST: ChromaKeyColorPreset[] = ["green", "blue", "red", "pure_green", "soft_green"]
|
||||
|
||||
const GreenScreenPanel: React.FC<GreenScreenPanelProps> = ({ open, onClose, config, onChange }) => {
|
||||
const update = useCallback(
|
||||
(partial: Partial<ChromaKeyConfig>) => {
|
||||
@@ -26,6 +33,7 @@ const GreenScreenPanel: React.FC<GreenScreenPanelProps> = ({ open, onClose, conf
|
||||
onChange({ ...DEFAULT_CHROMA_KEY_CONFIG, enabled: config.enabled })
|
||||
}, [config.enabled, onChange])
|
||||
|
||||
/** 选择颜色预设时同步更新 color 字段 */
|
||||
const handlePresetSelect = useCallback(
|
||||
(preset: ChromaKeyColorPreset) => {
|
||||
update({
|
||||
@@ -36,9 +44,10 @@ const GreenScreenPanel: React.FC<GreenScreenPanelProps> = ({ open, onClose, conf
|
||||
[update],
|
||||
)
|
||||
|
||||
/** 自定义颜色变化时清除预设标记 */
|
||||
const handleColorChange = useCallback(
|
||||
(color: string) => {
|
||||
update({ color })
|
||||
(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
update({ color: e.target.value })
|
||||
},
|
||||
[update],
|
||||
)
|
||||
@@ -52,6 +61,7 @@ const GreenScreenPanel: React.FC<GreenScreenPanelProps> = ({ open, onClose, conf
|
||||
onClose={onClose}
|
||||
className="green-screen-panel-drawer"
|
||||
>
|
||||
{/* 顶部开关 */}
|
||||
<div className="green-header">
|
||||
<span className="green-header-label">启用绿幕抠像</span>
|
||||
<Switch
|
||||
@@ -61,24 +71,123 @@ const GreenScreenPanel: React.FC<GreenScreenPanelProps> = ({ open, onClose, conf
|
||||
/>
|
||||
</div>
|
||||
|
||||
<GreenScreenPresets
|
||||
selectedPreset={config.color_preset}
|
||||
onPresetSelect={handlePresetSelect}
|
||||
/>
|
||||
{/* 颜色预设 */}
|
||||
<div className="green-section">
|
||||
<div className="green-section-title">颜色预设</div>
|
||||
<div className="green-presets">
|
||||
{PRESET_LIST.map((p) => (
|
||||
<button
|
||||
key={p}
|
||||
className={`green-preset-btn${config.color_preset === p ? " active" : ""}`}
|
||||
onClick={() => handlePresetSelect(p)}
|
||||
>
|
||||
<span
|
||||
className="green-preset-dot"
|
||||
style={{ background: CHROMA_KEY_PRESET_COLORS[p] }}
|
||||
/>
|
||||
<span className="green-preset-label">{CHROMA_KEY_PRESET_LABELS[p]}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<GreenScreenCustomColor color={config.color} onColorChange={handleColorChange} />
|
||||
{/* 自定义颜色 */}
|
||||
<div className="green-section">
|
||||
<div className="green-section-title">自定义颜色</div>
|
||||
<div className="green-color-row">
|
||||
<input
|
||||
type="color"
|
||||
className="green-color-picker"
|
||||
value={config.color}
|
||||
onChange={handleColorChange}
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
className="green-color-hex"
|
||||
value={config.color}
|
||||
onChange={handleColorChange}
|
||||
placeholder="#00FF00"
|
||||
/>
|
||||
<div className="green-color-swatch" style={{ background: config.color }} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<GreenScreenSliders
|
||||
similarity={config.similarity}
|
||||
blend={config.blend}
|
||||
spill={config.spill}
|
||||
onSimilarityChange={(v) => update({ similarity: v })}
|
||||
onBlendChange={(v) => update({ blend: v })}
|
||||
onSpillChange={(v) => update({ spill: v })}
|
||||
/>
|
||||
{/* 参数调节 */}
|
||||
<div className="green-section">
|
||||
<div className="green-section-title">参数调节</div>
|
||||
|
||||
<GreenScreenPreview color={config.color} blend={config.blend} />
|
||||
{/* 相似度 */}
|
||||
<div className="green-slider-row">
|
||||
<div className="green-slider-header">
|
||||
<span className="green-slider-label">相似度</span>
|
||||
<span className="green-slider-value">{config.similarity}%</span>
|
||||
</div>
|
||||
<input
|
||||
type="range"
|
||||
className="green-slider"
|
||||
min={0}
|
||||
max={100}
|
||||
value={config.similarity}
|
||||
onChange={(e) => update({ similarity: Number(e.target.value) })}
|
||||
/>
|
||||
<div className="green-slider-desc">越大容忍的色差范围越广</div>
|
||||
</div>
|
||||
|
||||
{/* 边缘平滑 */}
|
||||
<div className="green-slider-row">
|
||||
<div className="green-slider-header">
|
||||
<span className="green-slider-label">边缘平滑</span>
|
||||
<span className="green-slider-value">{config.blend}%</span>
|
||||
</div>
|
||||
<input
|
||||
type="range"
|
||||
className="green-slider"
|
||||
min={0}
|
||||
max={100}
|
||||
value={config.blend}
|
||||
onChange={(e) => update({ blend: Number(e.target.value) })}
|
||||
/>
|
||||
<div className="green-slider-desc">越大边缘越柔和自然</div>
|
||||
</div>
|
||||
|
||||
{/* 溢色抑制 */}
|
||||
<div className="green-slider-row">
|
||||
<div className="green-slider-header">
|
||||
<span className="green-slider-label">溢色抑制</span>
|
||||
<span className="green-slider-value">{config.spill}%</span>
|
||||
</div>
|
||||
<input
|
||||
type="range"
|
||||
className="green-slider"
|
||||
min={0}
|
||||
max={100}
|
||||
value={config.spill}
|
||||
onChange={(e) => update({ spill: Number(e.target.value) })}
|
||||
/>
|
||||
<div className="green-slider-desc">去除边缘颜色溢出</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 预览 */}
|
||||
<div className="green-section">
|
||||
<div className="green-section-title">效果预览</div>
|
||||
<div className="green-preview-box">
|
||||
<div className="green-preview-bg" style={{ background: config.color, opacity: 0.3 }} />
|
||||
<div className="green-preview-subject">
|
||||
<div className="green-preview-circle" />
|
||||
<div className="green-preview-text">主体</div>
|
||||
</div>
|
||||
<div
|
||||
className="green-preview-edge"
|
||||
style={{
|
||||
borderColor: config.color,
|
||||
filter: `blur(${config.blend / 10}px)`,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 底部重置 */}
|
||||
<div className="green-footer">
|
||||
<button className="green-reset-btn" onClick={handleReset}>
|
||||
重置参数
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
import React from "react"
|
||||
|
||||
interface GreenScreenCustomColorProps {
|
||||
color: string
|
||||
onColorChange: (color: string) => void
|
||||
}
|
||||
|
||||
export const GreenScreenCustomColor: React.FC<GreenScreenCustomColorProps> = ({
|
||||
color,
|
||||
onColorChange,
|
||||
}) => {
|
||||
const handleColorInput = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
onColorChange(e.target.value)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="green-section">
|
||||
<div className="green-section-title">自定义颜色</div>
|
||||
<div className="green-color-row">
|
||||
<input
|
||||
type="color"
|
||||
className="green-color-picker"
|
||||
value={color}
|
||||
onChange={handleColorInput}
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
className="green-color-hex"
|
||||
value={color}
|
||||
onChange={handleColorInput}
|
||||
placeholder="#00FF00"
|
||||
/>
|
||||
<div className="green-color-swatch" style={{ background: color }} />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
import React from "react"
|
||||
import type { ChromaKeyColorPreset } from "../../types"
|
||||
import { CHROMA_KEY_PRESET_LABELS, CHROMA_KEY_PRESET_COLORS } from "../../types"
|
||||
|
||||
interface GreenScreenPresetsProps {
|
||||
selectedPreset: ChromaKeyColorPreset | null
|
||||
onPresetSelect: (preset: ChromaKeyColorPreset) => void
|
||||
}
|
||||
|
||||
const PRESET_LIST: ChromaKeyColorPreset[] = ["green", "blue", "red", "pure_green", "soft_green"]
|
||||
|
||||
export const GreenScreenPresets: React.FC<GreenScreenPresetsProps> = ({
|
||||
selectedPreset,
|
||||
onPresetSelect,
|
||||
}) => {
|
||||
return (
|
||||
<div className="green-section">
|
||||
<div className="green-section-title">颜色预设</div>
|
||||
<div className="green-presets">
|
||||
{PRESET_LIST.map((p) => (
|
||||
<button
|
||||
key={p}
|
||||
className={`green-preset-btn${selectedPreset === p ? " active" : ""}`}
|
||||
onClick={() => onPresetSelect(p)}
|
||||
>
|
||||
<span
|
||||
className="green-preset-dot"
|
||||
style={{ background: CHROMA_KEY_PRESET_COLORS[p] }}
|
||||
/>
|
||||
<span className="green-preset-label">{CHROMA_KEY_PRESET_LABELS[p]}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
import React from "react"
|
||||
|
||||
interface GreenScreenPreviewProps {
|
||||
color: string
|
||||
blend: number
|
||||
}
|
||||
|
||||
export const GreenScreenPreview: React.FC<GreenScreenPreviewProps> = ({ color, blend }) => {
|
||||
return (
|
||||
<div className="green-section">
|
||||
<div className="green-section-title">效果预览</div>
|
||||
<div className="green-preview-box">
|
||||
<div className="green-preview-bg" style={{ background: color, opacity: 0.3 }} />
|
||||
<div className="green-preview-subject">
|
||||
<div className="green-preview-circle" />
|
||||
<div className="green-preview-text">主体</div>
|
||||
</div>
|
||||
<div
|
||||
className="green-preview-edge"
|
||||
style={{
|
||||
borderColor: color,
|
||||
filter: `blur(${blend / 10}px)`,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
import React from "react"
|
||||
|
||||
interface SliderConfig {
|
||||
label: string
|
||||
value: number
|
||||
description: string
|
||||
onChange: (value: number) => void
|
||||
}
|
||||
|
||||
interface GreenScreenSlidersProps {
|
||||
similarity: number
|
||||
blend: number
|
||||
spill: number
|
||||
onSimilarityChange: (value: number) => void
|
||||
onBlendChange: (value: number) => void
|
||||
onSpillChange: (value: number) => void
|
||||
}
|
||||
|
||||
const SliderRow: React.FC<SliderConfig> = ({ label, value, description, onChange }) => (
|
||||
<div className="green-slider-row">
|
||||
<div className="green-slider-header">
|
||||
<span className="green-slider-label">{label}</span>
|
||||
<span className="green-slider-value">{value}%</span>
|
||||
</div>
|
||||
<input
|
||||
type="range"
|
||||
className="green-slider"
|
||||
min={0}
|
||||
max={100}
|
||||
value={value}
|
||||
onChange={(e) => onChange(Number(e.target.value))}
|
||||
/>
|
||||
<div className="green-slider-desc">{description}</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
export const GreenScreenSliders: React.FC<GreenScreenSlidersProps> = ({
|
||||
similarity,
|
||||
blend,
|
||||
spill,
|
||||
onSimilarityChange,
|
||||
onBlendChange,
|
||||
onSpillChange,
|
||||
}) => {
|
||||
return (
|
||||
<div className="green-section">
|
||||
<div className="green-section-title">参数调节</div>
|
||||
<SliderRow
|
||||
label="相似度"
|
||||
value={similarity}
|
||||
description="越大容忍的色差范围越广"
|
||||
onChange={onSimilarityChange}
|
||||
/>
|
||||
<SliderRow
|
||||
label="边缘平滑"
|
||||
value={blend}
|
||||
description="越大边缘越柔和自然"
|
||||
onChange={onBlendChange}
|
||||
/>
|
||||
<SliderRow
|
||||
label="溢色抑制"
|
||||
value={spill}
|
||||
description="去除边缘颜色溢出"
|
||||
onChange={onSpillChange}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
import { useState, useRef, useCallback, useEffect, useMemo, useLayoutEffect } from "react"
|
||||
import type { ClipType } from "../../types"
|
||||
import { DEFAULT_ADD_DURATION, ADD_PICKER_WIDTH, TRACK_GAP } from "../../constants/timeline"
|
||||
|
||||
interface UseAddPickerOptions {
|
||||
currentMode: string
|
||||
onAddClip: (type: ClipType, duration: number) => void
|
||||
}
|
||||
|
||||
export function useAddPicker({ currentMode, onAddClip }: UseAddPickerOptions) {
|
||||
const [showAddPicker, setShowAddPicker] = useState(false)
|
||||
const pickerRef = useRef<HTMLDivElement>(null)
|
||||
const addCardRef = useRef<HTMLDivElement>(null)
|
||||
const [pickerPos, setPickerPos] = useState<{ top: number; right: number }>({ top: 0, right: 0 })
|
||||
|
||||
const availableTypes: ClipType[] = useMemo(
|
||||
() =>
|
||||
currentMode === "voice_over" ? ["voice"] : currentMode === "pip" ? ["pip"] : ["voice", "pip"],
|
||||
[currentMode],
|
||||
)
|
||||
|
||||
const defaultAddType: ClipType = useMemo(() => {
|
||||
if (currentMode === "voice_over") return "voice"
|
||||
if (currentMode === "pip") return "pip"
|
||||
return "voice"
|
||||
}, [currentMode])
|
||||
|
||||
const [addType, setAddType] = useState<ClipType>(defaultAddType)
|
||||
const [addDuration, setAddDuration] = useState<number>(DEFAULT_ADD_DURATION)
|
||||
|
||||
useEffect(() => {
|
||||
if (!availableTypes.includes(addType)) {
|
||||
setAddType(defaultAddType)
|
||||
}
|
||||
}, [currentMode, addType, availableTypes, defaultAddType])
|
||||
|
||||
const updatePickerPosition = useCallback(() => {
|
||||
if (!addCardRef.current) return
|
||||
const rect = addCardRef.current.getBoundingClientRect()
|
||||
const vw = window.innerWidth
|
||||
const roughHeight = 180
|
||||
let top = rect.top - TRACK_GAP - roughHeight
|
||||
if (top < 8) top = 8
|
||||
let right = vw - rect.right
|
||||
if (rect.right - ADD_PICKER_WIDTH < 8) {
|
||||
right = vw - ADD_PICKER_WIDTH - 8
|
||||
}
|
||||
setPickerPos({ top, right })
|
||||
}, [])
|
||||
|
||||
const handleTogglePicker = useCallback(() => {
|
||||
if (!showAddPicker) {
|
||||
setAddType(defaultAddType)
|
||||
updatePickerPosition()
|
||||
}
|
||||
setShowAddPicker((v) => !v)
|
||||
}, [showAddPicker, defaultAddType, updatePickerPosition])
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (!showAddPicker || !pickerRef.current || !addCardRef.current) return
|
||||
const pickerEl = pickerRef.current
|
||||
const addRect = addCardRef.current.getBoundingClientRect()
|
||||
const pickerH = pickerEl.offsetHeight
|
||||
const vh = window.innerHeight
|
||||
const vw = window.innerWidth
|
||||
|
||||
let top = addRect.top - TRACK_GAP - pickerH
|
||||
if (top < 8) {
|
||||
top = addRect.bottom + TRACK_GAP
|
||||
if (top + pickerH > vh - 8) {
|
||||
top = vh - 8 - pickerH
|
||||
if (top < 8) top = 8
|
||||
}
|
||||
}
|
||||
|
||||
let right = vw - addRect.right
|
||||
const pickerRect = pickerEl.getBoundingClientRect()
|
||||
if (pickerRect.left < 8) {
|
||||
right = vw - ADD_PICKER_WIDTH - 8
|
||||
}
|
||||
|
||||
setPickerPos({ top, right })
|
||||
}, [showAddPicker])
|
||||
|
||||
useEffect(() => {
|
||||
const handleClickOutside = (e: MouseEvent) => {
|
||||
if (pickerRef.current && !pickerRef.current.contains(e.target as Node)) {
|
||||
setShowAddPicker(false)
|
||||
}
|
||||
}
|
||||
if (showAddPicker) {
|
||||
document.addEventListener("mousedown", handleClickOutside)
|
||||
}
|
||||
return () => document.removeEventListener("mousedown", handleClickOutside)
|
||||
}, [showAddPicker])
|
||||
|
||||
const handleConfirmAdd = useCallback(() => {
|
||||
onAddClip(addType, addDuration)
|
||||
setShowAddPicker(false)
|
||||
}, [onAddClip, addType, addDuration])
|
||||
|
||||
return {
|
||||
showAddPicker,
|
||||
setShowAddPicker,
|
||||
pickerRef,
|
||||
addCardRef,
|
||||
pickerPos,
|
||||
availableTypes,
|
||||
addType,
|
||||
addDuration,
|
||||
setAddType,
|
||||
setAddDuration,
|
||||
handleTogglePicker,
|
||||
handleConfirmAdd,
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
import { useState, useRef, useCallback, useEffect } from "react"
|
||||
|
||||
interface ContextMenuState {
|
||||
x: number
|
||||
y: number
|
||||
clipId: string
|
||||
}
|
||||
|
||||
interface UseContextMenuOptions {
|
||||
onClipSplit?: (clipId: string, splitRatio: number) => void
|
||||
onClipResetTrim?: (clipId: string) => void
|
||||
onClipRemove?: (clipId: string) => void
|
||||
}
|
||||
|
||||
export function useContextMenu({
|
||||
onClipSplit,
|
||||
onClipResetTrim,
|
||||
onClipRemove,
|
||||
}: UseContextMenuOptions) {
|
||||
const [contextMenu, setContextMenu] = useState<ContextMenuState | null>(null)
|
||||
const contextMenuRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const handleContextMenu = useCallback((e: React.MouseEvent, clipId: string) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
setContextMenu({ x: e.clientX, y: e.clientY, clipId })
|
||||
}, [])
|
||||
|
||||
const handleContextSplit = useCallback(() => {
|
||||
if (!contextMenu) return
|
||||
onClipSplit?.(contextMenu.clipId, 0.5)
|
||||
setContextMenu(null)
|
||||
}, [contextMenu, onClipSplit])
|
||||
|
||||
const handleContextResetTrim = useCallback(() => {
|
||||
if (!contextMenu) return
|
||||
onClipResetTrim?.(contextMenu.clipId)
|
||||
setContextMenu(null)
|
||||
}, [contextMenu, onClipResetTrim])
|
||||
|
||||
const handleContextDelete = useCallback(() => {
|
||||
if (!contextMenu) return
|
||||
onClipRemove?.(contextMenu.clipId)
|
||||
setContextMenu(null)
|
||||
}, [contextMenu, onClipRemove])
|
||||
|
||||
useEffect(() => {
|
||||
const handleClickOutside = (e: MouseEvent) => {
|
||||
if (contextMenuRef.current && !contextMenuRef.current.contains(e.target as Node)) {
|
||||
setContextMenu(null)
|
||||
}
|
||||
}
|
||||
if (contextMenu) {
|
||||
document.addEventListener("mousedown", handleClickOutside)
|
||||
}
|
||||
return () => document.removeEventListener("mousedown", handleClickOutside)
|
||||
}, [contextMenu])
|
||||
|
||||
return {
|
||||
contextMenu,
|
||||
setContextMenu,
|
||||
contextMenuRef,
|
||||
handleContextMenu,
|
||||
handleContextSplit,
|
||||
handleContextResetTrim,
|
||||
handleContextDelete,
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
import { useCallback } from "react"
|
||||
import { message } from "antd"
|
||||
import { useQueryClient, useMutation } from "@tanstack/react-query"
|
||||
import { createClipsFromAssets } from "@/api/template-editor"
|
||||
|
||||
const QUERY_KEY = "editPlanClips"
|
||||
|
||||
export function useClipImport(planId: string | undefined) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const invalidate = () => {
|
||||
queryClient.invalidateQueries({ queryKey: [QUERY_KEY, planId] })
|
||||
}
|
||||
|
||||
const importFromAssetsMutation = useMutation({
|
||||
mutationFn: (assetIds: string[]) => createClipsFromAssets(planId!, assetIds),
|
||||
onSuccess: (res) => {
|
||||
invalidate()
|
||||
message.success(`已导入 ${res.created_count} 个素材片段`)
|
||||
},
|
||||
onError: () => {
|
||||
message.error("导入素材失败")
|
||||
},
|
||||
})
|
||||
|
||||
const importFromAssets = useCallback(
|
||||
(assetIds: string[]) => {
|
||||
if (!planId || assetIds.length === 0) return
|
||||
importFromAssetsMutation.mutate(assetIds)
|
||||
},
|
||||
[planId, importFromAssetsMutation],
|
||||
)
|
||||
|
||||
return {
|
||||
importFromAssets,
|
||||
isImporting: importFromAssetsMutation.isPending,
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
import { useCallback } from "react"
|
||||
import { message } from "antd"
|
||||
import { useQueryClient, useMutation } from "@tanstack/react-query"
|
||||
import type { ClipReorderItem } from "@/api/template-editor"
|
||||
import { reorderEditPlanClips } from "@/api/template-editor"
|
||||
|
||||
const QUERY_KEY = "editPlanClips"
|
||||
|
||||
export function useClipReorder(planId: string | undefined) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const invalidate = () => {
|
||||
queryClient.invalidateQueries({ queryKey: [QUERY_KEY, planId] })
|
||||
}
|
||||
|
||||
const reorderMutation = useMutation({
|
||||
mutationFn: (items: ClipReorderItem[]) => reorderEditPlanClips(planId!, items),
|
||||
onSuccess: () => {
|
||||
invalidate()
|
||||
},
|
||||
onError: () => {
|
||||
message.error("排序失败")
|
||||
invalidate()
|
||||
},
|
||||
})
|
||||
|
||||
const reorderClips = useCallback(
|
||||
(items: ClipReorderItem[]) => {
|
||||
if (!planId || items.length === 0) return
|
||||
reorderMutation.mutate(items)
|
||||
},
|
||||
[planId, reorderMutation],
|
||||
)
|
||||
|
||||
return {
|
||||
reorderClips,
|
||||
isReordering: reorderMutation.isPending,
|
||||
}
|
||||
}
|
||||
Executable → Regular
+56
-7
@@ -1,15 +1,19 @@
|
||||
import { useCallback } from "react"
|
||||
import { message } from "antd"
|
||||
import { useQueryClient, useMutation } from "@tanstack/react-query"
|
||||
import type { CreateEditPlanClipRequest, UpdateEditPlanClipRequest } from "@/api/template-editor"
|
||||
import type {
|
||||
CreateEditPlanClipRequest,
|
||||
UpdateEditPlanClipRequest,
|
||||
ClipReorderItem,
|
||||
} from "@/api/template-editor"
|
||||
import {
|
||||
createEditPlanClip,
|
||||
updateEditPlanClip,
|
||||
deleteEditPlanClip,
|
||||
reorderEditPlanClips,
|
||||
batchDeleteEditPlanClips,
|
||||
createClipsFromAssets,
|
||||
} from "@/api/template-editor"
|
||||
import { useClipReorder } from "./useClipReorder"
|
||||
import { useClipImport } from "./useClipImport"
|
||||
|
||||
const QUERY_KEY = "editPlanClips"
|
||||
|
||||
@@ -20,6 +24,10 @@ interface UseEditPlanClipMutationsOptions {
|
||||
clipsLength: number
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑计划片段 CRUD Hook
|
||||
* 封装创建、更新、删除、批量删除、重排序、素材导入等操作
|
||||
*/
|
||||
export function useEditPlanClipMutations({
|
||||
planId,
|
||||
selectedClipId,
|
||||
@@ -32,6 +40,7 @@ export function useEditPlanClipMutations({
|
||||
queryClient.invalidateQueries({ queryKey: [QUERY_KEY, planId] })
|
||||
}
|
||||
|
||||
/* ── 创建片段 ── */
|
||||
const createMutation = useMutation({
|
||||
mutationFn: (data: CreateEditPlanClipRequest) => createEditPlanClip(planId!, data),
|
||||
onSuccess: () => {
|
||||
@@ -52,6 +61,7 @@ export function useEditPlanClipMutations({
|
||||
[planId, clipsLength, createMutation],
|
||||
)
|
||||
|
||||
/* ── 更新片段 ── */
|
||||
const updateMutation = useMutation({
|
||||
mutationFn: ({ clipId, data }: { clipId: string; data: UpdateEditPlanClipRequest }) =>
|
||||
updateEditPlanClip(planId!, clipId, data),
|
||||
@@ -71,6 +81,7 @@ export function useEditPlanClipMutations({
|
||||
[planId, updateMutation],
|
||||
)
|
||||
|
||||
/* ── 删除片段 ── */
|
||||
const deleteMutation = useMutation({
|
||||
mutationFn: (clipId: string) => deleteEditPlanClip(planId!, clipId),
|
||||
onSuccess: () => {
|
||||
@@ -93,6 +104,7 @@ export function useEditPlanClipMutations({
|
||||
[planId, selectedClipId, setSelectedClipId, deleteMutation],
|
||||
)
|
||||
|
||||
/* ── 批量删除 ── */
|
||||
const batchDeleteMutation = useMutation({
|
||||
mutationFn: (clipIds: string[]) => batchDeleteEditPlanClips(planId!, clipIds),
|
||||
onSuccess: (res) => {
|
||||
@@ -115,8 +127,45 @@ export function useEditPlanClipMutations({
|
||||
[planId, selectedClipId, setSelectedClipId, batchDeleteMutation],
|
||||
)
|
||||
|
||||
const { reorderClips, isReordering } = useClipReorder(planId)
|
||||
const { importFromAssets, isImporting } = useClipImport(planId)
|
||||
/* ── 重排序 ── */
|
||||
const reorderMutation = useMutation({
|
||||
mutationFn: (items: ClipReorderItem[]) => reorderEditPlanClips(planId!, items),
|
||||
onSuccess: () => {
|
||||
invalidate()
|
||||
},
|
||||
onError: () => {
|
||||
message.error("排序失败")
|
||||
invalidate()
|
||||
},
|
||||
})
|
||||
|
||||
const reorderClips = useCallback(
|
||||
(items: ClipReorderItem[]) => {
|
||||
if (!planId || items.length === 0) return
|
||||
reorderMutation.mutate(items)
|
||||
},
|
||||
[planId, reorderMutation],
|
||||
)
|
||||
|
||||
/* ── 从素材批量导入 ── */
|
||||
const importFromAssetsMutation = useMutation({
|
||||
mutationFn: (assetIds: string[]) => createClipsFromAssets(planId!, assetIds),
|
||||
onSuccess: (res) => {
|
||||
invalidate()
|
||||
message.success(`已导入 ${res.created_count} 个素材片段`)
|
||||
},
|
||||
onError: () => {
|
||||
message.error("导入素材失败")
|
||||
},
|
||||
})
|
||||
|
||||
const importFromAssets = useCallback(
|
||||
(assetIds: string[]) => {
|
||||
if (!planId || assetIds.length === 0) return
|
||||
importFromAssetsMutation.mutate(assetIds)
|
||||
},
|
||||
[planId, importFromAssetsMutation],
|
||||
)
|
||||
|
||||
return {
|
||||
addClip,
|
||||
@@ -128,7 +177,7 @@ export function useEditPlanClipMutations({
|
||||
isCreating: createMutation.isPending,
|
||||
isUpdating: updateMutation.isPending,
|
||||
isDeleting: deleteMutation.isPending,
|
||||
isReordering,
|
||||
isImporting,
|
||||
isReordering: reorderMutation.isPending,
|
||||
isImporting: importFromAssetsMutation.isPending,
|
||||
}
|
||||
}
|
||||
|
||||
Executable → Regular
+149
-24
@@ -1,7 +1,12 @@
|
||||
import { useState } from "react"
|
||||
import { useState, useRef, useCallback, useEffect, useMemo, useLayoutEffect } from "react"
|
||||
import type { ClipData, ClipType } from "../types"
|
||||
import { useContextMenu } from "./timeline-menus/useContextMenu"
|
||||
import { useAddPicker } from "./timeline-menus/useAddPicker"
|
||||
import { DEFAULT_ADD_DURATION, ADD_PICKER_WIDTH, TRACK_GAP } from "../constants/timeline"
|
||||
|
||||
interface ContextMenuState {
|
||||
x: number
|
||||
y: number
|
||||
clipId: string
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间线菜单 Hook
|
||||
@@ -15,30 +20,150 @@ export const useTimelineMenus = (
|
||||
onClipResetTrim?: (clipId: string) => void,
|
||||
onClipRemove?: (clipId: string) => void,
|
||||
) => {
|
||||
/* ── 右键菜单 ── */
|
||||
const [contextMenu, setContextMenu] = useState<ContextMenuState | null>(null)
|
||||
const contextMenuRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
/* ── 添加片段面板 ── */
|
||||
const [showAddPicker, setShowAddPicker] = useState(false)
|
||||
const pickerRef = useRef<HTMLDivElement>(null)
|
||||
const addCardRef = useRef<HTMLDivElement>(null)
|
||||
const [pickerPos, setPickerPos] = useState<{ top: number; right: number }>({ top: 0, right: 0 })
|
||||
|
||||
/* ── 悬停的片段 ID(显示裁剪手柄) ── */
|
||||
const [hoveredClipId, setHoveredClipId] = useState<string | null>(null)
|
||||
|
||||
const {
|
||||
contextMenu,
|
||||
contextMenuRef,
|
||||
handleContextMenu,
|
||||
handleContextSplit,
|
||||
handleContextResetTrim,
|
||||
handleContextDelete,
|
||||
} = useContextMenu({ onClipSplit, onClipResetTrim, onClipRemove })
|
||||
/* ── 根据模式决定可选类型 ── */
|
||||
const availableTypes: ClipType[] = useMemo(
|
||||
() =>
|
||||
currentMode === "voice_over" ? ["voice"] : currentMode === "pip" ? ["pip"] : ["voice", "pip"],
|
||||
[currentMode],
|
||||
)
|
||||
|
||||
const {
|
||||
showAddPicker,
|
||||
pickerRef,
|
||||
addCardRef,
|
||||
pickerPos,
|
||||
availableTypes,
|
||||
addType,
|
||||
addDuration,
|
||||
setAddType,
|
||||
setAddDuration,
|
||||
handleTogglePicker,
|
||||
handleConfirmAdd,
|
||||
} = useAddPicker({ currentMode, onAddClip })
|
||||
/* ── 默认添加类型:跟随模式 ── */
|
||||
const defaultAddType: ClipType = useMemo(() => {
|
||||
if (currentMode === "voice_over") return "voice"
|
||||
if (currentMode === "pip") return "pip"
|
||||
return "voice"
|
||||
}, [currentMode])
|
||||
|
||||
/* ── "+" 卡片:类型+时长选择状态 ── */
|
||||
const [addType, setAddType] = useState<ClipType>(defaultAddType)
|
||||
const [addDuration, setAddDuration] = useState<number>(DEFAULT_ADD_DURATION)
|
||||
|
||||
/* ── 模式切换时自动同步默认添加类型 ── */
|
||||
useEffect(() => {
|
||||
if (!availableTypes.includes(addType)) {
|
||||
setAddType(defaultAddType)
|
||||
}
|
||||
}, [currentMode, addType, availableTypes, defaultAddType])
|
||||
|
||||
/* ── 计算 picker 初始位置 ── */
|
||||
const updatePickerPosition = useCallback(() => {
|
||||
if (!addCardRef.current) return
|
||||
const rect = addCardRef.current.getBoundingClientRect()
|
||||
const vw = window.innerWidth
|
||||
const roughHeight = 180
|
||||
let top = rect.top - TRACK_GAP - roughHeight
|
||||
if (top < 8) top = 8
|
||||
let right = vw - rect.right
|
||||
if (rect.right - ADD_PICKER_WIDTH < 8) {
|
||||
right = vw - ADD_PICKER_WIDTH - 8
|
||||
}
|
||||
setPickerPos({ top, right })
|
||||
}, [])
|
||||
|
||||
const handleTogglePicker = useCallback(() => {
|
||||
if (!showAddPicker) {
|
||||
setAddType(defaultAddType)
|
||||
updatePickerPosition()
|
||||
}
|
||||
setShowAddPicker((v) => !v)
|
||||
}, [showAddPicker, defaultAddType, updatePickerPosition])
|
||||
|
||||
/* ── 渲染后精确边界校正 ── */
|
||||
useLayoutEffect(() => {
|
||||
if (!showAddPicker || !pickerRef.current || !addCardRef.current) return
|
||||
const pickerEl = pickerRef.current
|
||||
const addRect = addCardRef.current.getBoundingClientRect()
|
||||
const pickerH = pickerEl.offsetHeight
|
||||
const vh = window.innerHeight
|
||||
const vw = window.innerWidth
|
||||
|
||||
let top = addRect.top - TRACK_GAP - pickerH
|
||||
if (top < 8) {
|
||||
top = addRect.bottom + TRACK_GAP
|
||||
if (top + pickerH > vh - 8) {
|
||||
top = vh - 8 - pickerH
|
||||
if (top < 8) top = 8
|
||||
}
|
||||
}
|
||||
|
||||
let right = vw - addRect.right
|
||||
const pickerRect = pickerEl.getBoundingClientRect()
|
||||
if (pickerRect.left < 8) {
|
||||
right = vw - ADD_PICKER_WIDTH - 8
|
||||
}
|
||||
|
||||
setPickerPos({ top, right })
|
||||
}, [showAddPicker])
|
||||
|
||||
/* ── 点击外部关闭添加面板 ── */
|
||||
useEffect(() => {
|
||||
const handleClickOutside = (e: MouseEvent) => {
|
||||
if (pickerRef.current && !pickerRef.current.contains(e.target as Node)) {
|
||||
setShowAddPicker(false)
|
||||
}
|
||||
}
|
||||
if (showAddPicker) {
|
||||
document.addEventListener("mousedown", handleClickOutside)
|
||||
}
|
||||
return () => document.removeEventListener("mousedown", handleClickOutside)
|
||||
}, [showAddPicker])
|
||||
|
||||
/* ── 点击外部关闭右键菜单 ── */
|
||||
useEffect(() => {
|
||||
const handleClickOutside = (e: MouseEvent) => {
|
||||
if (contextMenuRef.current && !contextMenuRef.current.contains(e.target as Node)) {
|
||||
setContextMenu(null)
|
||||
}
|
||||
}
|
||||
if (contextMenu) {
|
||||
document.addEventListener("mousedown", handleClickOutside)
|
||||
}
|
||||
return () => document.removeEventListener("mousedown", handleClickOutside)
|
||||
}, [contextMenu])
|
||||
|
||||
/* ── 确认添加片段 ── */
|
||||
const handleConfirmAdd = useCallback(() => {
|
||||
onAddClip(addType, addDuration)
|
||||
setShowAddPicker(false)
|
||||
}, [onAddClip, addType, addDuration])
|
||||
|
||||
/* ── 右键菜单操作 ── */
|
||||
const handleContextMenu = useCallback((e: React.MouseEvent, clipId: string) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
setContextMenu({ x: e.clientX, y: e.clientY, clipId })
|
||||
}, [])
|
||||
|
||||
const handleContextSplit = useCallback(() => {
|
||||
if (!contextMenu) return
|
||||
onClipSplit?.(contextMenu.clipId, 0.5)
|
||||
setContextMenu(null)
|
||||
}, [contextMenu, onClipSplit])
|
||||
|
||||
const handleContextResetTrim = useCallback(() => {
|
||||
if (!contextMenu) return
|
||||
onClipResetTrim?.(contextMenu.clipId)
|
||||
setContextMenu(null)
|
||||
}, [contextMenu, onClipResetTrim])
|
||||
|
||||
const handleContextDelete = useCallback(() => {
|
||||
if (!contextMenu) return
|
||||
onClipRemove?.(contextMenu.clipId)
|
||||
setContextMenu(null)
|
||||
}, [contextMenu, onClipRemove])
|
||||
|
||||
return {
|
||||
// 右键菜单
|
||||
|
||||
Executable → Regular
+102
-11
@@ -1,21 +1,37 @@
|
||||
/**
|
||||
* 智能剪辑右侧生成结果面板
|
||||
*/
|
||||
import React from "react"
|
||||
import { Typography } from "antd"
|
||||
import { PlayCircleOutlined, CloseCircleOutlined } from "@ant-design/icons"
|
||||
import {
|
||||
PlayCircleOutlined,
|
||||
CloseCircleOutlined,
|
||||
DownloadOutlined,
|
||||
ShareAltOutlined,
|
||||
} from "@ant-design/icons"
|
||||
import type { GeneratedVideo } from "@/api/template-editor"
|
||||
import { ProgressIndicator } from "./result-panel/ProgressIndicator"
|
||||
import { ResultVideoCard } from "./result-panel/ResultVideoCard"
|
||||
import { formatDuration } from "@/api/voice-clone"
|
||||
|
||||
const { Text } = Typography
|
||||
|
||||
interface GenerateResultPanelProps {
|
||||
/** 是否已生成完成 */
|
||||
generated: boolean
|
||||
/** 是否正在生成中 */
|
||||
generating: boolean
|
||||
/** 生成进度(0-100) */
|
||||
progress: number
|
||||
/** 生成错误信息 */
|
||||
generateError: string | null
|
||||
/** 生成的视频列表 */
|
||||
generatedVideos: GeneratedVideo[]
|
||||
/** 点击视频卡片预览回调 */
|
||||
onVideoPreview: (video: GeneratedVideo) => void
|
||||
/** 下载回调 */
|
||||
onDownload: () => void
|
||||
/** 分享回调 */
|
||||
onShare: () => void
|
||||
/** 前往成片库回调 */
|
||||
onGoToLibrary: () => void
|
||||
}
|
||||
|
||||
@@ -39,8 +55,45 @@ const GenerateResultPanel: React.FC<GenerateResultPanelProps> = ({
|
||||
)}
|
||||
</div>
|
||||
|
||||
{generating && <ProgressIndicator progress={progress} />}
|
||||
{/* 生成中进度 */}
|
||||
{generating && (
|
||||
<div className="xx-result-progress">
|
||||
<div className="xx-progress-circle">
|
||||
<svg viewBox="0 0 80 80">
|
||||
<circle
|
||||
cx="40"
|
||||
cy="40"
|
||||
r="36"
|
||||
fill="none"
|
||||
stroke="var(--border-color)"
|
||||
strokeWidth="6"
|
||||
/>
|
||||
<circle
|
||||
cx="40"
|
||||
cy="40"
|
||||
r="36"
|
||||
fill="none"
|
||||
stroke="var(--primary-color)"
|
||||
strokeWidth="6"
|
||||
strokeDasharray={`${Math.round(progress) * 2.26} 226`}
|
||||
strokeLinecap="round"
|
||||
transform="rotate(-90 40 40)"
|
||||
/>
|
||||
</svg>
|
||||
<span className="xx-progress-percent">{Math.round(progress)}%</span>
|
||||
</div>
|
||||
<div className="xx-progress-text">
|
||||
<Text strong style={{ fontSize: 14, display: "block", marginBottom: 4 }}>
|
||||
正在生成视频
|
||||
</Text>
|
||||
<Text style={{ fontSize: 12, color: "var(--text-secondary)" }}>
|
||||
AI 正在处理素材,请稍候…
|
||||
</Text>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 生成失败 */}
|
||||
{generateError && !generating && (
|
||||
<div className="xx-result-empty">
|
||||
<CloseCircleOutlined style={{ fontSize: 40, color: "#ff4d4f", marginBottom: 12 }} />
|
||||
@@ -53,6 +106,7 @@ const GenerateResultPanel: React.FC<GenerateResultPanelProps> = ({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 空状态 */}
|
||||
{!generated && !generating && !generateError && (
|
||||
<div className="xx-result-empty">
|
||||
<PlayCircleOutlined
|
||||
@@ -67,17 +121,54 @@ const GenerateResultPanel: React.FC<GenerateResultPanelProps> = ({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 生成结果卡片列表 */}
|
||||
{generated && generatedVideos.length > 0 && (
|
||||
<div className="xx-video-grid">
|
||||
{generatedVideos.map((video, idx) => (
|
||||
<ResultVideoCard
|
||||
<div
|
||||
key={video.id || idx}
|
||||
video={video}
|
||||
index={idx}
|
||||
onPreview={onVideoPreview}
|
||||
onDownload={onDownload}
|
||||
onShare={onShare}
|
||||
/>
|
||||
className="xx-video-card"
|
||||
onClick={() => onVideoPreview(video)}
|
||||
>
|
||||
<div className="xx-video-thumb">
|
||||
{video.thumbnail_url ? (
|
||||
<img src={video.thumbnail_url} alt="" />
|
||||
) : (
|
||||
<div className="xx-video-thumb-placeholder">
|
||||
<PlayCircleOutlined style={{ fontSize: 32, opacity: 0.5 }} />
|
||||
</div>
|
||||
)}
|
||||
<div className="xx-video-play-overlay">
|
||||
<PlayCircleOutlined style={{ fontSize: 36, color: "#fff" }} />
|
||||
</div>
|
||||
{video.duration && (
|
||||
<span className="xx-video-duration">{formatDuration(video.duration)}</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="xx-video-info">
|
||||
<div className="xx-video-title">视频 {idx + 1}</div>
|
||||
<div className="xx-video-actions">
|
||||
<button
|
||||
className="xx-video-action-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onDownload()
|
||||
}}
|
||||
>
|
||||
<DownloadOutlined />
|
||||
</button>
|
||||
<button
|
||||
className="xx-video-action-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onShare()
|
||||
}}
|
||||
>
|
||||
<ShareAltOutlined />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
Executable → Regular
+101
-16
@@ -1,9 +1,9 @@
|
||||
/**
|
||||
* Step 6 封面设置组件
|
||||
*/
|
||||
import React from "react"
|
||||
import type { CoverConfig } from "../../editing-planner/types"
|
||||
import { useStep6Cover } from "../hooks/useStep6Cover"
|
||||
import { CoverModeSelector } from "./cover-settings/CoverModeSelector"
|
||||
import { FrameCoverPicker } from "./cover-settings/FrameCoverPicker"
|
||||
import { UploadCoverPicker } from "./cover-settings/UploadCoverPicker"
|
||||
|
||||
interface Step6CoverSettingsProps {
|
||||
coverSettings: CoverConfig
|
||||
@@ -28,6 +28,7 @@ const Step6CoverSettings: React.FC<Step6CoverSettingsProps> = (props) => {
|
||||
<div className="xx-form-section">
|
||||
<h3>🖼️ 选择封面</h3>
|
||||
|
||||
{/* 启用开关 */}
|
||||
<div className="xx-cover-header">
|
||||
<span className="xx-cover-header-label">启用自定义封面</span>
|
||||
<label className="xx-switch">
|
||||
@@ -42,14 +43,22 @@ const Step6CoverSettings: React.FC<Step6CoverSettingsProps> = (props) => {
|
||||
|
||||
{coverSettings.enabled && (
|
||||
<>
|
||||
{/* 模式选择 */}
|
||||
<div className="xx-section-title">封面来源</div>
|
||||
<CoverModeSelector
|
||||
mode={coverSettings.mode}
|
||||
onModeChange={setMode}
|
||||
modeLabels={COVER_MODE_LABELS}
|
||||
modeIcons={COVER_MODE_ICONS}
|
||||
/>
|
||||
<div className="xx-cover-mode-tabs">
|
||||
{(["auto", "frame", "upload"] as const).map((m) => (
|
||||
<button
|
||||
key={m}
|
||||
className={`xx-cover-mode-tab${coverSettings.mode === m ? " active" : ""}`}
|
||||
onClick={() => setMode(m)}
|
||||
>
|
||||
<span className="xx-cover-mode-icon">{COVER_MODE_ICONS[m]}</span>
|
||||
<span className="xx-cover-mode-label">{COVER_MODE_LABELS[m]}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* 智能封面 */}
|
||||
{coverSettings.mode === "auto" && (
|
||||
<div className="xx-cover-auto">
|
||||
<div className="xx-cover-auto-desc">
|
||||
@@ -62,19 +71,95 @@ const Step6CoverSettings: React.FC<Step6CoverSettingsProps> = (props) => {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 抽帧选封面 */}
|
||||
{coverSettings.mode === "frame" && (
|
||||
<FrameCoverPicker
|
||||
frameTime={coverSettings.frame_time}
|
||||
totalDuration={totalDuration}
|
||||
formatTime={formatTime}
|
||||
onFrameTimeChange={setFrameTime}
|
||||
/>
|
||||
<div className="xx-cover-frame">
|
||||
<div className="xx-cover-frame-preview">
|
||||
<div className="xx-cover-frame-placeholder">
|
||||
<span className="xx-cover-frame-icon">🎞️</span>
|
||||
<span className="xx-cover-frame-time">
|
||||
{formatTime(coverSettings.frame_time)}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="xx-cover-frame-slider">
|
||||
<div className="xx-cover-frame-slider-header">
|
||||
<span>拖动选择封面帧</span>
|
||||
<span className="xx-cover-frame-value">
|
||||
{formatTime(coverSettings.frame_time)}
|
||||
</span>
|
||||
</div>
|
||||
<input
|
||||
type="range"
|
||||
min={0}
|
||||
max={Math.max(totalDuration, 1)}
|
||||
step={0.1}
|
||||
value={coverSettings.frame_time}
|
||||
onChange={(e) => setFrameTime(Number(e.target.value))}
|
||||
className="xx-cover-range"
|
||||
/>
|
||||
<div className="xx-cover-frame-range">
|
||||
<span>00:00</span>
|
||||
<span>{formatTime(totalDuration)}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="xx-cover-frame-quick">
|
||||
<span className="xx-cover-quick-label">快捷选帧:</span>
|
||||
{[0, 0.25, 0.5, 0.75].map((ratio) => {
|
||||
const t = totalDuration * ratio
|
||||
return (
|
||||
<button
|
||||
key={ratio}
|
||||
className="xx-cover-quick-btn"
|
||||
onClick={() => setFrameTime(t)}
|
||||
>
|
||||
{formatTime(t)}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 上传封面 */}
|
||||
{coverSettings.mode === "upload" && (
|
||||
<UploadCoverPicker uploadUrl={coverSettings.upload_url} onUpload={handleUpload} />
|
||||
<div className="xx-cover-upload">
|
||||
<div
|
||||
className="xx-cover-upload-area"
|
||||
onClick={() => {
|
||||
const input = document.getElementById("cover-upload-input")
|
||||
input?.click()
|
||||
}}
|
||||
>
|
||||
{coverSettings.upload_url ? (
|
||||
<div className="xx-cover-upload-preview">
|
||||
<img src={coverSettings.upload_url} alt="封面预览" />
|
||||
<div className="xx-cover-upload-overlay">点击更换</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="xx-cover-upload-placeholder">
|
||||
<span style={{ fontSize: 32 }}>📤</span>
|
||||
<span className="xx-cover-upload-text">点击上传封面图片</span>
|
||||
<span className="xx-cover-upload-hint">支持 JPG / PNG,建议 16:9 比例</span>
|
||||
</div>
|
||||
)}
|
||||
<input
|
||||
id="cover-upload-input"
|
||||
type="file"
|
||||
accept="image/*"
|
||||
style={{ display: "none" }}
|
||||
onChange={(e) => {
|
||||
const file = e.target.files?.[0]
|
||||
if (file) {
|
||||
handleUpload(file)
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 封面预览 */}
|
||||
<div className="xx-section-title">封面预览</div>
|
||||
<div className="xx-cover-preview-box">
|
||||
{coverSettings.upload_url ? (
|
||||
|
||||
Executable → Regular
+130
-24
@@ -2,14 +2,19 @@
|
||||
* Step 7 确认生成组件
|
||||
*/
|
||||
import React from "react"
|
||||
import {
|
||||
LoadingOutlined,
|
||||
CheckCircleFilled,
|
||||
CloseCircleOutlined,
|
||||
MinusOutlined,
|
||||
PlusOutlined,
|
||||
} from "@ant-design/icons"
|
||||
import type { EditingTemplate } from "@/api/editing-planner"
|
||||
import type { GeneratedVideo } from "@/api/template-editor"
|
||||
import type { CoverConfig } from "../../editing-planner/types"
|
||||
import type { VoiceClone } from "@/api/voice-clone"
|
||||
import type { PresetVoiceItem } from "@/api/voices"
|
||||
import { useStep7Generate } from "../hooks/useStep7Generate"
|
||||
import SummaryCard from "./step7-confirm/SummaryCard"
|
||||
import GenerationStatus from "./step7-confirm/GenerationStatus"
|
||||
|
||||
interface Step7ConfirmGenerateProps {
|
||||
templates: EditingTemplate[]
|
||||
@@ -59,28 +64,129 @@ const Step7ConfirmGenerate: React.FC<Step7ConfirmGenerateProps> = (props) => {
|
||||
return (
|
||||
<div className="xx-form-section">
|
||||
<h3>✨ 确认生成</h3>
|
||||
<SummaryCard
|
||||
templateName={templateName}
|
||||
materialSummary={materialSummary}
|
||||
title={title}
|
||||
voiceName={voiceName}
|
||||
coverSummary={coverSummary}
|
||||
generateCount={generateCount}
|
||||
generating={generating}
|
||||
onDecrement={handleDecrement}
|
||||
onIncrement={handleIncrement}
|
||||
/>
|
||||
<GenerationStatus
|
||||
generating={generating}
|
||||
generated={generated}
|
||||
generateError={generateError}
|
||||
progress={progress}
|
||||
generatedVideos={generatedVideos}
|
||||
getGenerationPhase={getGenerationPhase}
|
||||
onScrollToPreview={handleScrollToPreview}
|
||||
onRetry={onRetry}
|
||||
onDismissError={onDismissError}
|
||||
/>
|
||||
<div className="xx-summary-card">
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">模板</span>
|
||||
<span className="xx-summary-value">{templateName}</span>
|
||||
</div>
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">素材</span>
|
||||
<span className="xx-summary-value">{materialSummary}</span>
|
||||
</div>
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">标题</span>
|
||||
<span className="xx-summary-value">{title || "未选择"}</span>
|
||||
</div>
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">配音</span>
|
||||
<span className="xx-summary-value">{voiceName}</span>
|
||||
</div>
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">封面</span>
|
||||
<span className="xx-summary-value">{coverSummary}</span>
|
||||
</div>
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">生成数量</span>
|
||||
<span className="xx-summary-value">
|
||||
<div className="xx-count-stepper">
|
||||
<button
|
||||
className="xx-count-stepper-btn"
|
||||
disabled={generateCount <= 1 || generating}
|
||||
onClick={handleDecrement}
|
||||
>
|
||||
<MinusOutlined />
|
||||
</button>
|
||||
<span className="xx-count-stepper-value">{generateCount}</span>
|
||||
<button
|
||||
className="xx-count-stepper-btn"
|
||||
disabled={generateCount >= 10 || generating}
|
||||
onClick={handleIncrement}
|
||||
>
|
||||
<PlusOutlined />
|
||||
</button>
|
||||
<span className="xx-count-stepper-hint">条视频</span>
|
||||
</div>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 生成进度 / 结果反馈 */}
|
||||
{(generating || generated || generateError) && (
|
||||
<div style={{ marginTop: 16 }}>
|
||||
{generating && (
|
||||
<div className="xx-gen-progress-card">
|
||||
<div className="xx-gen-progress-header">
|
||||
<div className="xx-gen-progress-icon">
|
||||
<LoadingOutlined />
|
||||
</div>
|
||||
<div className="xx-gen-progress-info">
|
||||
<div className="xx-gen-progress-phase">
|
||||
{getGenerationPhase(progress).icon} {getGenerationPhase(progress).label}
|
||||
</div>
|
||||
<div className="xx-gen-progress-sub">预计还需 1-2 分钟,请稍候…</div>
|
||||
</div>
|
||||
<div className="xx-gen-progress-percent">{Math.round(progress)}%</div>
|
||||
</div>
|
||||
<div className="xx-gen-progress-bar">
|
||||
<div
|
||||
className="xx-gen-progress-bar-fill"
|
||||
style={{ width: `${Math.min(Math.round(progress), 100)}%` }}
|
||||
/>
|
||||
</div>
|
||||
<div className="xx-gen-progress-tip">
|
||||
💡 生成过程中可以切换到其他页面操作,完成后会自动通知
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{generated && !generating && (
|
||||
<div className="xx-gen-success-card">
|
||||
<div className="xx-gen-success-icon">
|
||||
<CheckCircleFilled style={{ fontSize: 32, color: "#52c41a" }} />
|
||||
</div>
|
||||
<div className="xx-gen-success-info">
|
||||
<div className="xx-gen-success-title">视频生成完成!</div>
|
||||
<div className="xx-gen-success-sub">
|
||||
共生成 {generatedVideos.length} 条视频,可在右侧预览或前往成片库查看
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-btn xx-btn-primary xx-btn-sm"
|
||||
onClick={handleScrollToPreview}
|
||||
>
|
||||
查看结果
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
{generateError && !generating && (
|
||||
<div className="xx-gen-error-card">
|
||||
<div className="xx-gen-error-icon">
|
||||
<CloseCircleOutlined style={{ fontSize: 28, color: "#ef4444" }} />
|
||||
</div>
|
||||
<div className="xx-gen-error-info">
|
||||
<div className="xx-gen-error-title">生成失败</div>
|
||||
<div className="xx-gen-error-msg">
|
||||
{typeof generateError === "string"
|
||||
? generateError
|
||||
: JSON.stringify(generateError)}
|
||||
</div>
|
||||
</div>
|
||||
<div style={{ display: "flex", gap: 8 }}>
|
||||
<button type="button" className="xx-btn xx-btn-primary xx-btn-sm" onClick={onRetry}>
|
||||
🔄 重试
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-btn xx-btn-ghost xx-btn-sm"
|
||||
onClick={onDismissError}
|
||||
>
|
||||
知道了
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
import React from "react"
|
||||
import type { CoverMode } from "../../../editing-planner/types"
|
||||
|
||||
interface CoverModeSelectorProps {
|
||||
mode: CoverMode
|
||||
onModeChange: (mode: CoverMode) => void
|
||||
modeLabels: Record<CoverMode, string>
|
||||
modeIcons: Record<CoverMode, string>
|
||||
}
|
||||
|
||||
export const CoverModeSelector: React.FC<CoverModeSelectorProps> = ({
|
||||
mode,
|
||||
onModeChange,
|
||||
modeLabels,
|
||||
modeIcons,
|
||||
}) => {
|
||||
const modes: CoverMode[] = ["auto", "frame", "upload"]
|
||||
return (
|
||||
<div className="xx-cover-mode-tabs">
|
||||
{modes.map((m) => (
|
||||
<button
|
||||
key={m}
|
||||
className={`xx-cover-mode-tab${mode === m ? " active" : ""}`}
|
||||
onClick={() => onModeChange(m)}
|
||||
>
|
||||
<span className="xx-cover-mode-icon">{modeIcons[m]}</span>
|
||||
<span className="xx-cover-mode-label">{modeLabels[m]}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
import React from "react"
|
||||
|
||||
interface FrameCoverPickerProps {
|
||||
frameTime: number
|
||||
totalDuration: number
|
||||
formatTime: (seconds: number) => string
|
||||
onFrameTimeChange: (time: number) => void
|
||||
}
|
||||
|
||||
export const FrameCoverPicker: React.FC<FrameCoverPickerProps> = ({
|
||||
frameTime,
|
||||
totalDuration,
|
||||
formatTime,
|
||||
onFrameTimeChange,
|
||||
}) => {
|
||||
const quickRatios = [0, 0.25, 0.5, 0.75]
|
||||
|
||||
return (
|
||||
<div className="xx-cover-frame">
|
||||
<div className="xx-cover-frame-preview">
|
||||
<div className="xx-cover-frame-placeholder">
|
||||
<span className="xx-cover-frame-icon">🎞️</span>
|
||||
<span className="xx-cover-frame-time">{formatTime(frameTime)}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="xx-cover-frame-slider">
|
||||
<div className="xx-cover-frame-slider-header">
|
||||
<span>拖动选择封面帧</span>
|
||||
<span className="xx-cover-frame-value">{formatTime(frameTime)}</span>
|
||||
</div>
|
||||
<input
|
||||
type="range"
|
||||
min={0}
|
||||
max={Math.max(totalDuration, 1)}
|
||||
step={0.1}
|
||||
value={frameTime}
|
||||
onChange={(e) => onFrameTimeChange(Number(e.target.value))}
|
||||
className="xx-cover-range"
|
||||
/>
|
||||
<div className="xx-cover-frame-range">
|
||||
<span>00:00</span>
|
||||
<span>{formatTime(totalDuration)}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="xx-cover-frame-quick">
|
||||
<span className="xx-cover-quick-label">快捷选帧:</span>
|
||||
{quickRatios.map((ratio) => {
|
||||
const t = totalDuration * ratio
|
||||
return (
|
||||
<button key={ratio} className="xx-cover-quick-btn" onClick={() => onFrameTimeChange(t)}>
|
||||
{formatTime(t)}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
import React from "react"
|
||||
|
||||
interface UploadCoverPickerProps {
|
||||
uploadUrl: string
|
||||
onUpload: (file: File) => void
|
||||
}
|
||||
|
||||
export const UploadCoverPicker: React.FC<UploadCoverPickerProps> = ({ uploadUrl, onUpload }) => {
|
||||
const handleClick = () => {
|
||||
const input = document.getElementById("cover-upload-input")
|
||||
input?.click()
|
||||
}
|
||||
|
||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0]
|
||||
if (file) {
|
||||
onUpload(file)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="xx-cover-upload">
|
||||
<div className="xx-cover-upload-area" onClick={handleClick}>
|
||||
{uploadUrl ? (
|
||||
<div className="xx-cover-upload-preview">
|
||||
<img src={uploadUrl} alt="封面预览" />
|
||||
<div className="xx-cover-upload-overlay">点击更换</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="xx-cover-upload-placeholder">
|
||||
<span style={{ fontSize: 32 }}>📤</span>
|
||||
<span className="xx-cover-upload-text">点击上传封面图片</span>
|
||||
<span className="xx-cover-upload-hint">支持 JPG / PNG,建议 16:9 比例</span>
|
||||
</div>
|
||||
)}
|
||||
<input
|
||||
id="cover-upload-input"
|
||||
type="file"
|
||||
accept="image/*"
|
||||
style={{ display: "none" }}
|
||||
onChange={handleFileChange}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
import React from "react"
|
||||
import { Typography } from "antd"
|
||||
|
||||
const { Text } = Typography
|
||||
|
||||
interface ProgressIndicatorProps {
|
||||
progress: number
|
||||
}
|
||||
|
||||
export const ProgressIndicator: React.FC<ProgressIndicatorProps> = ({ progress }) => {
|
||||
return (
|
||||
<div className="xx-result-progress">
|
||||
<div className="xx-progress-circle">
|
||||
<svg viewBox="0 0 80 80">
|
||||
<circle cx="40" cy="40" r="36" fill="none" stroke="var(--border-color)" strokeWidth="6" />
|
||||
<circle
|
||||
cx="40"
|
||||
cy="40"
|
||||
r="36"
|
||||
fill="none"
|
||||
stroke="var(--primary-color)"
|
||||
strokeWidth="6"
|
||||
strokeDasharray={`${Math.round(progress) * 2.26} 226`}
|
||||
strokeLinecap="round"
|
||||
transform="rotate(-90 40 40)"
|
||||
/>
|
||||
</svg>
|
||||
<span className="xx-progress-percent">{Math.round(progress)}%</span>
|
||||
</div>
|
||||
<div className="xx-progress-text">
|
||||
<Text strong style={{ fontSize: 14, display: "block", marginBottom: 4 }}>
|
||||
正在生成视频
|
||||
</Text>
|
||||
<Text style={{ fontSize: 12, color: "var(--text-secondary)" }}>
|
||||
AI 正在处理素材,请稍候…
|
||||
</Text>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
import React from "react"
|
||||
import { PlayCircleOutlined, DownloadOutlined, ShareAltOutlined } from "@ant-design/icons"
|
||||
import type { GeneratedVideo } from "@/api/template-editor"
|
||||
import { formatDuration } from "@/api/voice-clone"
|
||||
|
||||
interface ResultVideoCardProps {
|
||||
video: GeneratedVideo
|
||||
index: number
|
||||
onPreview: (video: GeneratedVideo) => void
|
||||
onDownload: () => void
|
||||
onShare: () => void
|
||||
}
|
||||
|
||||
export const ResultVideoCard: React.FC<ResultVideoCardProps> = ({
|
||||
video,
|
||||
index,
|
||||
onPreview,
|
||||
onDownload,
|
||||
onShare,
|
||||
}) => {
|
||||
return (
|
||||
<div className="xx-video-card" onClick={() => onPreview(video)}>
|
||||
<div className="xx-video-thumb">
|
||||
{video.thumbnail_url ? (
|
||||
<img src={video.thumbnail_url} alt="" />
|
||||
) : (
|
||||
<div className="xx-video-thumb-placeholder">
|
||||
<PlayCircleOutlined style={{ fontSize: 32, opacity: 0.5 }} />
|
||||
</div>
|
||||
)}
|
||||
<div className="xx-video-play-overlay">
|
||||
<PlayCircleOutlined style={{ fontSize: 36, color: "#fff" }} />
|
||||
</div>
|
||||
{video.duration && (
|
||||
<span className="xx-video-duration">{formatDuration(video.duration)}</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="xx-video-info">
|
||||
<div className="xx-video-title">视频 {index + 1}</div>
|
||||
<div className="xx-video-actions">
|
||||
<button
|
||||
className="xx-video-action-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onDownload()
|
||||
}}
|
||||
>
|
||||
<DownloadOutlined />
|
||||
</button>
|
||||
<button
|
||||
className="xx-video-action-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onShare()
|
||||
}}
|
||||
>
|
||||
<ShareAltOutlined />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
import React from "react"
|
||||
import { LoadingOutlined, CheckCircleFilled, CloseCircleOutlined } from "@ant-design/icons"
|
||||
import type { GeneratedVideo } from "@/api/template-editor"
|
||||
|
||||
interface GenerationStatusProps {
|
||||
generating: boolean
|
||||
generated: boolean
|
||||
generateError: string | null
|
||||
progress: number
|
||||
generatedVideos: GeneratedVideo[]
|
||||
getGenerationPhase: (progress: number) => { icon: string; label: string }
|
||||
onScrollToPreview: () => void
|
||||
onRetry: () => void
|
||||
onDismissError: () => void
|
||||
}
|
||||
|
||||
const GenerationStatus: React.FC<GenerationStatusProps> = ({
|
||||
generating,
|
||||
generated,
|
||||
generateError,
|
||||
progress,
|
||||
generatedVideos,
|
||||
getGenerationPhase,
|
||||
onScrollToPreview,
|
||||
onRetry,
|
||||
onDismissError,
|
||||
}) => {
|
||||
if (!generating && !generated && !generateError) return null
|
||||
|
||||
return (
|
||||
<div style={{ marginTop: 16 }}>
|
||||
{generating && (
|
||||
<div className="xx-gen-progress-card">
|
||||
<div className="xx-gen-progress-header">
|
||||
<div className="xx-gen-progress-icon">
|
||||
<LoadingOutlined />
|
||||
</div>
|
||||
<div className="xx-gen-progress-info">
|
||||
<div className="xx-gen-progress-phase">
|
||||
{getGenerationPhase(progress).icon} {getGenerationPhase(progress).label}
|
||||
</div>
|
||||
<div className="xx-gen-progress-sub">预计还需 1-2 分钟,请稍候…</div>
|
||||
</div>
|
||||
<div className="xx-gen-progress-percent">{Math.round(progress)}%</div>
|
||||
</div>
|
||||
<div className="xx-gen-progress-bar">
|
||||
<div
|
||||
className="xx-gen-progress-bar-fill"
|
||||
style={{ width: `${Math.min(Math.round(progress), 100)}%` }}
|
||||
/>
|
||||
</div>
|
||||
<div className="xx-gen-progress-tip">
|
||||
💡 生成过程中可以切换到其他页面操作,完成后会自动通知
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{generated && !generating && (
|
||||
<div className="xx-gen-success-card">
|
||||
<div className="xx-gen-success-icon">
|
||||
<CheckCircleFilled style={{ fontSize: 32, color: "#52c41a" }} />
|
||||
</div>
|
||||
<div className="xx-gen-success-info">
|
||||
<div className="xx-gen-success-title">视频生成完成!</div>
|
||||
<div className="xx-gen-success-sub">
|
||||
共生成 {generatedVideos.length} 条视频,可在右侧预览或前往成片库查看
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-btn xx-btn-primary xx-btn-sm"
|
||||
onClick={onScrollToPreview}
|
||||
>
|
||||
查看结果
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
{generateError && !generating && (
|
||||
<div className="xx-gen-error-card">
|
||||
<div className="xx-gen-error-icon">
|
||||
<CloseCircleOutlined style={{ fontSize: 28, color: "#ef4444" }} />
|
||||
</div>
|
||||
<div className="xx-gen-error-info">
|
||||
<div className="xx-gen-error-title">生成失败</div>
|
||||
<div className="xx-gen-error-msg">
|
||||
{typeof generateError === "string" ? generateError : JSON.stringify(generateError)}
|
||||
</div>
|
||||
</div>
|
||||
<div style={{ display: "flex", gap: 8 }}>
|
||||
<button type="button" className="xx-btn xx-btn-primary xx-btn-sm" onClick={onRetry}>
|
||||
🔄 重试
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-btn xx-btn-ghost xx-btn-sm"
|
||||
onClick={onDismissError}
|
||||
>
|
||||
知道了
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default GenerationStatus
|
||||
@@ -1,76 +0,0 @@
|
||||
import React from "react"
|
||||
import { MinusOutlined, PlusOutlined } from "@ant-design/icons"
|
||||
|
||||
interface SummaryCardProps {
|
||||
templateName: string
|
||||
materialSummary: string
|
||||
title: string
|
||||
voiceName: string
|
||||
coverSummary: string
|
||||
generateCount: number
|
||||
generating: boolean
|
||||
onDecrement: () => void
|
||||
onIncrement: () => void
|
||||
}
|
||||
|
||||
const SummaryCard: React.FC<SummaryCardProps> = ({
|
||||
templateName,
|
||||
materialSummary,
|
||||
title,
|
||||
voiceName,
|
||||
coverSummary,
|
||||
generateCount,
|
||||
generating,
|
||||
onDecrement,
|
||||
onIncrement,
|
||||
}) => {
|
||||
return (
|
||||
<div className="xx-summary-card">
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">模板</span>
|
||||
<span className="xx-summary-value">{templateName}</span>
|
||||
</div>
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">素材</span>
|
||||
<span className="xx-summary-value">{materialSummary}</span>
|
||||
</div>
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">标题</span>
|
||||
<span className="xx-summary-value">{title || "未选择"}</span>
|
||||
</div>
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">配音</span>
|
||||
<span className="xx-summary-value">{voiceName}</span>
|
||||
</div>
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">封面</span>
|
||||
<span className="xx-summary-value">{coverSummary}</span>
|
||||
</div>
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">生成数量</span>
|
||||
<span className="xx-summary-value">
|
||||
<div className="xx-count-stepper">
|
||||
<button
|
||||
className="xx-count-stepper-btn"
|
||||
disabled={generateCount <= 1 || generating}
|
||||
onClick={onDecrement}
|
||||
>
|
||||
<MinusOutlined />
|
||||
</button>
|
||||
<span className="xx-count-stepper-value">{generateCount}</span>
|
||||
<button
|
||||
className="xx-count-stepper-btn"
|
||||
disabled={generateCount >= 10 || generating}
|
||||
onClick={onIncrement}
|
||||
>
|
||||
<PlusOutlined />
|
||||
</button>
|
||||
<span className="xx-count-stepper-hint">条视频</span>
|
||||
</div>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default SummaryCard
|
||||
@@ -1,70 +0,0 @@
|
||||
import { useCallback } from "react"
|
||||
import type { VoiceClone } from "@/api/voice-clone"
|
||||
|
||||
interface UseVoiceModeSelectionOptions {
|
||||
selectedVoice: string
|
||||
onSelectedVoiceChange: (voiceId: string) => void
|
||||
voiceMode: "preset" | "custom" | "clone"
|
||||
onVoiceModeChange: (mode: "preset" | "custom" | "clone") => void
|
||||
selectedClonedVoice: string
|
||||
onSelectedClonedVoiceChange: (voiceId: string) => void
|
||||
addClone: (voice: VoiceClone) => void
|
||||
cloneModalOpen: boolean
|
||||
onCloneModalOpenChange: (open: boolean) => void
|
||||
}
|
||||
|
||||
export function useVoiceModeSelection({
|
||||
onSelectedVoiceChange,
|
||||
onVoiceModeChange,
|
||||
onSelectedClonedVoiceChange,
|
||||
addClone,
|
||||
onCloneModalOpenChange,
|
||||
}: UseVoiceModeSelectionOptions) {
|
||||
const handleSelectRecommendedVoice = useCallback(
|
||||
(voiceId: string) => {
|
||||
onVoiceModeChange("preset")
|
||||
onSelectedVoiceChange(voiceId)
|
||||
},
|
||||
[onVoiceModeChange, onSelectedVoiceChange],
|
||||
)
|
||||
|
||||
const handleSelectPresetVoice = useCallback(
|
||||
(voiceId: string) => {
|
||||
onVoiceModeChange("preset")
|
||||
onSelectedVoiceChange(voiceId)
|
||||
},
|
||||
[onVoiceModeChange, onSelectedVoiceChange],
|
||||
)
|
||||
|
||||
const handleSelectCloneVoice = useCallback(() => {
|
||||
onVoiceModeChange("clone")
|
||||
}, [onVoiceModeChange])
|
||||
|
||||
const handleSelectClonedVoice = useCallback(
|
||||
(voiceId: string) => {
|
||||
onSelectedClonedVoiceChange(voiceId)
|
||||
},
|
||||
[onSelectedClonedVoiceChange],
|
||||
)
|
||||
|
||||
const handleOpenCloneModal = useCallback(() => {
|
||||
onCloneModalOpenChange(true)
|
||||
}, [onCloneModalOpenChange])
|
||||
|
||||
const handleCloneSuccess = useCallback(
|
||||
(voice: VoiceClone) => {
|
||||
addClone(voice)
|
||||
onCloneModalOpenChange(false)
|
||||
},
|
||||
[addClone, onCloneModalOpenChange],
|
||||
)
|
||||
|
||||
return {
|
||||
handleSelectRecommendedVoice,
|
||||
handleSelectPresetVoice,
|
||||
handleSelectCloneVoice,
|
||||
handleSelectClonedVoice,
|
||||
handleOpenCloneModal,
|
||||
handleCloneSuccess,
|
||||
}
|
||||
}
|
||||
Executable → Regular
+43
-24
@@ -2,15 +2,15 @@
|
||||
* Step 5 配音选择 Hook
|
||||
* 封装 AI 推荐、预设音色试听、TTS 自定义合成、存为素材等逻辑
|
||||
*/
|
||||
import { message } from "antd"
|
||||
import { useCallback } from "react"
|
||||
import type { VoiceClone } from "@/api/voice-clone"
|
||||
import { message } from "antd"
|
||||
import { VOICE_GENDER_ICON, CLONE_STATUS_CONFIG } from "../constants"
|
||||
import { formatDuration } from "../utils/formatDuration"
|
||||
import { useVoiceAudio } from "./step5-voice/useVoiceAudio"
|
||||
import { useVoiceRecommend } from "./step5-voice/useVoiceRecommend"
|
||||
import { useTtsSynthesis } from "./step5-voice/useTtsSynthesis"
|
||||
import { useSaveToLibrary } from "./step5-voice/useSaveToLibrary"
|
||||
import { useVoiceModeSelection } from "./step5-voice/useVoiceModeSelection"
|
||||
|
||||
interface UseStep5VoiceProps {
|
||||
selectedVoice: string
|
||||
@@ -81,29 +81,48 @@ export function useStep5Voice({
|
||||
handleAddTagInModal,
|
||||
} = useSaveToLibrary(completedTtsJobId, resetTtsState)
|
||||
|
||||
const {
|
||||
handleSelectRecommendedVoice,
|
||||
handleSelectPresetVoice,
|
||||
handleSelectCloneVoice,
|
||||
handleSelectClonedVoice,
|
||||
handleOpenCloneModal,
|
||||
handleCloneSuccess: rawCloneSuccess,
|
||||
} = useVoiceModeSelection({
|
||||
selectedVoice,
|
||||
onSelectedVoiceChange,
|
||||
voiceMode,
|
||||
onVoiceModeChange,
|
||||
selectedClonedVoice,
|
||||
onSelectedClonedVoiceChange,
|
||||
addClone,
|
||||
cloneModalOpen,
|
||||
onCloneModalOpenChange,
|
||||
})
|
||||
/* ── 推荐音色选择 ── */
|
||||
const handleSelectRecommendedVoice = useCallback(
|
||||
(voiceId: string) => {
|
||||
onVoiceModeChange("preset")
|
||||
onSelectedVoiceChange(voiceId)
|
||||
},
|
||||
[onVoiceModeChange, onSelectedVoiceChange],
|
||||
)
|
||||
|
||||
const handleCloneSuccess = (voice: VoiceClone) => {
|
||||
rawCloneSuccess(voice)
|
||||
message.success("音色克隆成功!")
|
||||
}
|
||||
/* ── 克隆成功回调 ── */
|
||||
const handleCloneSuccess = useCallback(
|
||||
(voice: VoiceClone) => {
|
||||
addClone(voice)
|
||||
onCloneModalOpenChange(false)
|
||||
message.success("音色克隆成功!")
|
||||
},
|
||||
[addClone, onCloneModalOpenChange],
|
||||
)
|
||||
|
||||
/* ── 预设音色选择操作 ── */
|
||||
const handleSelectPresetVoice = useCallback(
|
||||
(voiceId: string) => {
|
||||
onVoiceModeChange("preset")
|
||||
onSelectedVoiceChange(voiceId)
|
||||
},
|
||||
[onVoiceModeChange, onSelectedVoiceChange],
|
||||
)
|
||||
|
||||
const handleSelectCloneVoice = useCallback(() => {
|
||||
onVoiceModeChange("clone")
|
||||
}, [onVoiceModeChange])
|
||||
|
||||
const handleSelectClonedVoice = useCallback(
|
||||
(voiceId: string) => {
|
||||
onSelectedClonedVoiceChange(voiceId)
|
||||
},
|
||||
[onSelectedClonedVoiceChange],
|
||||
)
|
||||
|
||||
const handleOpenCloneModal = useCallback(() => {
|
||||
onCloneModalOpenChange(true)
|
||||
}, [onCloneModalOpenChange])
|
||||
|
||||
return {
|
||||
// 数据
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
import { useQueryClient } from "@tanstack/react-query"
|
||||
import { message } from "antd"
|
||||
import { deleteProduct } from "@/api/products"
|
||||
|
||||
interface UseBatchDeleteOptions {
|
||||
selectedIds: Set<string>
|
||||
clearSelection: () => void
|
||||
}
|
||||
|
||||
export function useBatchDelete({ selectedIds, clearSelection }: UseBatchDeleteOptions) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const handleBatchDelete = async () => {
|
||||
const ids = Array.from(selectedIds)
|
||||
let successCount = 0
|
||||
for (const id of ids) {
|
||||
try {
|
||||
await deleteProduct(id)
|
||||
successCount++
|
||||
} catch {
|
||||
// 忽略单个失败
|
||||
}
|
||||
}
|
||||
queryClient.invalidateQueries({ queryKey: ["products"] })
|
||||
clearSelection()
|
||||
message.success(`已批量删除 ${successCount}/${ids.length} 个视频`)
|
||||
}
|
||||
|
||||
return { handleBatchDelete }
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
import { useState, useCallback } from "react"
|
||||
import { message } from "antd"
|
||||
import { batchDownload, getBatchDownloadStatus } from "@/api/products"
|
||||
|
||||
interface UseBatchDownloadOptions {
|
||||
selectedIds: Set<string>
|
||||
clearSelection: () => void
|
||||
}
|
||||
|
||||
export function useBatchDownload({ selectedIds, clearSelection }: UseBatchDownloadOptions) {
|
||||
const [batchDownloading, setBatchDownloading] = useState(false)
|
||||
|
||||
const handleBatchDownload = useCallback(async () => {
|
||||
const ids = Array.from(selectedIds)
|
||||
if (ids.length === 0) return
|
||||
setBatchDownloading(true)
|
||||
try {
|
||||
const { job_id } = await batchDownload(ids)
|
||||
message.info(`批量下载任务已创建,正在打包 ${ids.length} 个视频...`)
|
||||
|
||||
let attempts = 0
|
||||
const maxAttempts = 60
|
||||
const poll = async (): Promise<void> => {
|
||||
if (attempts >= maxAttempts) {
|
||||
message.warning("打包超时,请稍后在消息中心查看")
|
||||
return
|
||||
}
|
||||
attempts++
|
||||
await new Promise((r) => setTimeout(r, 2000))
|
||||
const status = await getBatchDownloadStatus(job_id)
|
||||
if (status.status === "completed" && status.download_url) {
|
||||
const a = document.createElement("a")
|
||||
a.href = status.download_url
|
||||
a.download = ""
|
||||
a.click()
|
||||
message.success(`已打包下载 ${ids.length} 个视频`)
|
||||
clearSelection()
|
||||
} else if (status.status === "failed") {
|
||||
message.error("批量下载失败,请重试")
|
||||
} else {
|
||||
await poll()
|
||||
}
|
||||
}
|
||||
await poll()
|
||||
} catch {
|
||||
message.error("发起批量下载失败")
|
||||
} finally {
|
||||
setBatchDownloading(false)
|
||||
}
|
||||
}, [selectedIds, clearSelection])
|
||||
|
||||
return { batchDownloading, handleBatchDownload }
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
||||
import { message } from "antd"
|
||||
import { deleteProduct, updateReviewStatus, type ReviewStatus } from "@/api/products"
|
||||
|
||||
export function useProductMutations() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const deleteMutation = useMutation({
|
||||
mutationFn: deleteProduct,
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["products"] })
|
||||
message.success("已删除")
|
||||
},
|
||||
onError: () => {
|
||||
message.error("删除失败")
|
||||
},
|
||||
})
|
||||
|
||||
const reviewMutation = useMutation({
|
||||
mutationFn: ({ id, status }: { id: string; status: ReviewStatus }) =>
|
||||
updateReviewStatus(id, status),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["products"] })
|
||||
message.success("复核状态已更新")
|
||||
},
|
||||
onError: () => {
|
||||
message.error("更新复核状态失败")
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
deleteMutation,
|
||||
reviewMutation,
|
||||
isDeleting: deleteMutation.isPending,
|
||||
isUpdatingReview: reviewMutation.isPending,
|
||||
}
|
||||
}
|
||||
Executable → Regular
+104
-12
@@ -1,11 +1,17 @@
|
||||
import { useState } from "react"
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
||||
import { useNavigate } from "react-router-dom"
|
||||
import { message } from "antd"
|
||||
import { getProductDownloadUrl } from "@/api/products"
|
||||
import {
|
||||
deleteProduct,
|
||||
getProductDownloadUrl,
|
||||
updateReviewStatus,
|
||||
batchDownload,
|
||||
getBatchDownloadStatus,
|
||||
type ReviewStatus,
|
||||
} from "@/api/products"
|
||||
import type { ProductItem } from "../types"
|
||||
import { getNextReviewStatus } from "../utils"
|
||||
import { useBatchDownload } from "./product-actions/useBatchDownload"
|
||||
import { useBatchDelete } from "./product-actions/useBatchDelete"
|
||||
import { useProductMutations } from "./product-actions/useProductMutations"
|
||||
|
||||
interface UseProductActionsOptions {
|
||||
selectedIds: Set<string>
|
||||
@@ -20,17 +26,35 @@ export const useProductActions = ({
|
||||
products,
|
||||
setPlayingProduct,
|
||||
}: UseProductActionsOptions) => {
|
||||
const queryClient = useQueryClient()
|
||||
const navigate = useNavigate()
|
||||
|
||||
const { deleteMutation, reviewMutation, isDeleting, isUpdatingReview } = useProductMutations()
|
||||
|
||||
const { batchDownloading, handleBatchDownload } = useBatchDownload({
|
||||
selectedIds,
|
||||
clearSelection,
|
||||
/* ── 删除 mutation ── */
|
||||
const deleteMutation = useMutation({
|
||||
mutationFn: deleteProduct,
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["products"] })
|
||||
message.success("已删除")
|
||||
},
|
||||
onError: () => {
|
||||
message.error("删除失败")
|
||||
},
|
||||
})
|
||||
|
||||
const { handleBatchDelete } = useBatchDelete({ selectedIds, clearSelection })
|
||||
/* ── 复核状态 mutation ── */
|
||||
const reviewMutation = useMutation({
|
||||
mutationFn: ({ id, status }: { id: string; status: ReviewStatus }) =>
|
||||
updateReviewStatus(id, status),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["products"] })
|
||||
message.success("复核状态已更新")
|
||||
},
|
||||
onError: () => {
|
||||
message.error("更新复核状态失败")
|
||||
},
|
||||
})
|
||||
|
||||
/* 下载 — 调用真实 API 获取下载链接 */
|
||||
const handleDownload = async (product: ProductItem) => {
|
||||
if (product.status !== "completed") return
|
||||
try {
|
||||
@@ -45,6 +69,7 @@ export const useProductActions = ({
|
||||
}
|
||||
}
|
||||
|
||||
/* 分享 */
|
||||
const handleShare = (product: ProductItem) => {
|
||||
if (product.status !== "completed") return
|
||||
const link = `${window.location.origin}/share/${product.id}`
|
||||
@@ -54,42 +79,109 @@ export const useProductActions = ({
|
||||
)
|
||||
}
|
||||
|
||||
/* 查看详情 — 跳转到产品详情页 */
|
||||
const handleViewDetail = (product: ProductItem) => {
|
||||
setPlayingProduct(null)
|
||||
navigate(`/app/products/${product.id}`)
|
||||
}
|
||||
|
||||
/* 删除 */
|
||||
const handleDelete = (id: string) => {
|
||||
deleteMutation.mutate(id)
|
||||
}
|
||||
|
||||
/* 发布 — TODO: 后端发布 API 待实现 */
|
||||
const handlePublish = (_product: ProductItem) => {
|
||||
message.info("发布功能待后端 API 补齐")
|
||||
}
|
||||
|
||||
/* 切换复核状态 */
|
||||
const handleReviewStatusChange = (id: string) => {
|
||||
const current = products.find((p) => p.id === id)?.reviewStatus
|
||||
const nextStatus = getNextReviewStatus(current)
|
||||
reviewMutation.mutate({ id, status: nextStatus })
|
||||
}
|
||||
|
||||
/* 批量下载 */
|
||||
const [batchDownloading, setBatchDownloading] = useState(false)
|
||||
|
||||
const handleBatchDownload = async () => {
|
||||
const ids = Array.from(selectedIds)
|
||||
if (ids.length === 0) return
|
||||
setBatchDownloading(true)
|
||||
try {
|
||||
const { job_id } = await batchDownload(ids)
|
||||
message.info(`批量下载任务已创建,正在打包 ${ids.length} 个视频...`)
|
||||
|
||||
let attempts = 0
|
||||
const maxAttempts = 60
|
||||
const poll = async (): Promise<void> => {
|
||||
if (attempts >= maxAttempts) {
|
||||
message.warning("打包超时,请稍后在消息中心查看")
|
||||
return
|
||||
}
|
||||
attempts++
|
||||
await new Promise((r) => setTimeout(r, 2000))
|
||||
const status = await getBatchDownloadStatus(job_id)
|
||||
if (status.status === "completed" && status.download_url) {
|
||||
const a = document.createElement("a")
|
||||
a.href = status.download_url
|
||||
a.download = ""
|
||||
a.click()
|
||||
message.success(`已打包下载 ${ids.length} 个视频`)
|
||||
clearSelection()
|
||||
} else if (status.status === "failed") {
|
||||
message.error("批量下载失败,请重试")
|
||||
} else {
|
||||
await poll()
|
||||
}
|
||||
}
|
||||
await poll()
|
||||
} catch {
|
||||
message.error("发起批量下载失败")
|
||||
} finally {
|
||||
setBatchDownloading(false)
|
||||
}
|
||||
}
|
||||
|
||||
/* 批量删除 */
|
||||
const handleBatchDelete = async () => {
|
||||
const ids = Array.from(selectedIds)
|
||||
let successCount = 0
|
||||
for (const id of ids) {
|
||||
try {
|
||||
await deleteProduct(id)
|
||||
successCount++
|
||||
} catch {
|
||||
// 忽略单个失败
|
||||
}
|
||||
}
|
||||
queryClient.invalidateQueries({ queryKey: ["products"] })
|
||||
clearSelection()
|
||||
message.success(`已批量删除 ${successCount}/${ids.length} 个视频`)
|
||||
}
|
||||
|
||||
/* 批量发布 */
|
||||
const handleBatchPublish = () => {
|
||||
message.info("批量发布功能待后端 API 补齐")
|
||||
clearSelection()
|
||||
}
|
||||
|
||||
return {
|
||||
// 单个操作
|
||||
handleDownload,
|
||||
handleShare,
|
||||
handleViewDetail,
|
||||
handleDelete,
|
||||
handlePublish,
|
||||
handleReviewStatusChange,
|
||||
// 批量操作
|
||||
handleBatchDownload,
|
||||
handleBatchDelete,
|
||||
handleBatchPublish,
|
||||
batchDownloading,
|
||||
isDeleting,
|
||||
isUpdatingReview,
|
||||
// mutation 状态
|
||||
isDeleting: deleteMutation.isPending,
|
||||
isUpdatingReview: reviewMutation.isPending,
|
||||
}
|
||||
}
|
||||
|
||||
Executable → Regular
+72
-16
@@ -1,8 +1,7 @@
|
||||
import React from "react"
|
||||
import React, { useState, useRef, useCallback, useMemo } from "react"
|
||||
import { CheckOutlined } from "@ant-design/icons"
|
||||
import { Tag } from "@/components/ui"
|
||||
import type { TagItem } from "@/api/tags"
|
||||
import { useTagInput } from "./tag-selector/useTagInput"
|
||||
import { type TagItem } from "@/api/tags"
|
||||
|
||||
export interface TagSelectorProps {
|
||||
/** 已选标签 ID 列表 */
|
||||
@@ -25,22 +24,77 @@ const TagSelector: React.FC<TagSelectorProps> = ({
|
||||
onCreateTag,
|
||||
placeholder = "输入标签后回车添加",
|
||||
}) => {
|
||||
const {
|
||||
inputVal,
|
||||
setInputVal,
|
||||
showSuggestions,
|
||||
setShowSuggestions,
|
||||
inputRef,
|
||||
suggestions,
|
||||
addTagId,
|
||||
removeTagId,
|
||||
handleKeyDown,
|
||||
focus,
|
||||
} = useTagInput({ value, onChange, tags, onCreateTag })
|
||||
const [inputVal, setInputVal] = useState("")
|
||||
const [showSuggestions, setShowSuggestions] = useState(false)
|
||||
const inputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
/** 按名称查找已有标签(大小写不敏感) */
|
||||
const findTagByName = useCallback(
|
||||
(name: string) => tags.find((t) => t.name.toLowerCase() === name.toLowerCase()),
|
||||
[tags],
|
||||
)
|
||||
|
||||
/** 去重添加标签(按 ID) */
|
||||
const addTagId = useCallback(
|
||||
(tagId: string) => {
|
||||
if (value.includes(tagId)) return
|
||||
onChange([...value, tagId])
|
||||
setInputVal("")
|
||||
setShowSuggestions(false)
|
||||
},
|
||||
[value, onChange],
|
||||
)
|
||||
|
||||
/** 输入自定义标签名:若已存在则直接选,否则创建新标签 */
|
||||
const addTagByName = useCallback(
|
||||
async (name: string) => {
|
||||
const trimmed = name.trim()
|
||||
if (!trimmed) return
|
||||
const existing = findTagByName(trimmed)
|
||||
if (existing) {
|
||||
addTagId(existing.id)
|
||||
} else {
|
||||
try {
|
||||
const created = await onCreateTag(trimmed)
|
||||
addTagId(created.id)
|
||||
} catch {
|
||||
/* 创建失败静默忽略 */
|
||||
}
|
||||
}
|
||||
},
|
||||
[findTagByName, addTagId, onCreateTag],
|
||||
)
|
||||
|
||||
const removeTagId = useCallback(
|
||||
(tagId: string) => {
|
||||
onChange(value.filter((t) => t !== tagId))
|
||||
},
|
||||
[value, onChange],
|
||||
)
|
||||
|
||||
/** 输入补全建议(排除已选) */
|
||||
const suggestions = useMemo(() => {
|
||||
if (!inputVal.trim()) return []
|
||||
const lower = inputVal.toLowerCase()
|
||||
return tags.filter((t) => t.name.toLowerCase().includes(lower) && !value.includes(t.id))
|
||||
}, [inputVal, tags, value])
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault()
|
||||
if (suggestions.length > 0) {
|
||||
addTagId(suggestions[0].id)
|
||||
} else {
|
||||
addTagByName(inputVal)
|
||||
}
|
||||
} else if (e.key === "Backspace" && !inputVal && value.length > 0) {
|
||||
removeTagId(value[value.length - 1])
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="vmat-tag-selector-wrapper">
|
||||
<div className="vmat-tag-selector" onClick={focus}>
|
||||
<div className="vmat-tag-selector" onClick={() => inputRef.current?.focus()}>
|
||||
{value.map((tagId) => (
|
||||
<Tag key={tagId} variant="info" closable onClose={() => removeTagId(tagId)}>
|
||||
{tagMap.get(tagId)?.name ?? tagId}
|
||||
@@ -61,6 +115,7 @@ const TagSelector: React.FC<TagSelectorProps> = ({
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 自动补全下拉 */}
|
||||
{showSuggestions && suggestions.length > 0 && (
|
||||
<div className="vmat-tag-suggestions">
|
||||
{suggestions.slice(0, 6).map((tag) => (
|
||||
@@ -79,6 +134,7 @@ const TagSelector: React.FC<TagSelectorProps> = ({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 已有标签快捷选择 */}
|
||||
{tags.length > 0 && (
|
||||
<div className="vmat-tag-selector-presets">
|
||||
{tags.map((tag) => {
|
||||
|
||||
Executable → Regular
+57
-9
@@ -1,4 +1,4 @@
|
||||
import React from "react"
|
||||
import React, { useRef } from "react"
|
||||
import {
|
||||
PlayCircleOutlined,
|
||||
PauseCircleOutlined,
|
||||
@@ -6,8 +6,11 @@ import {
|
||||
DeleteOutlined,
|
||||
CheckOutlined,
|
||||
} from "@ant-design/icons"
|
||||
import { Tooltip } from "antd"
|
||||
import { Tag } from "@/components/ui"
|
||||
import { type TagItem } from "@/api/tags"
|
||||
import { type VoiceMaterial } from "../types"
|
||||
import { MAX_ROW_TAGS, TAG_VARIANTS } from "../constants"
|
||||
import {
|
||||
genderClass,
|
||||
genderIcon,
|
||||
@@ -15,8 +18,6 @@ import {
|
||||
formatDuration,
|
||||
formatFileSize,
|
||||
} from "../utils/format"
|
||||
import { useRowProgress } from "./voice-material-row/useRowProgress"
|
||||
import TagDisplay from "./voice-material-row/TagDisplay"
|
||||
|
||||
export interface VoiceRowProps {
|
||||
material: VoiceMaterial
|
||||
@@ -47,10 +48,26 @@ const VoiceMaterialRow: React.FC<VoiceRowProps> = ({
|
||||
onDelete,
|
||||
onToggleSelect,
|
||||
}) => {
|
||||
const { progressRef, handleMouseDown } = useRowProgress({
|
||||
duration: material.duration,
|
||||
onSeek,
|
||||
})
|
||||
const progressRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const handleProgressMouseDown = (e: React.MouseEvent<HTMLDivElement>) => {
|
||||
if (!progressRef.current) return
|
||||
e.preventDefault()
|
||||
const doSeek = (ev: MouseEvent) => {
|
||||
if (!progressRef.current) return
|
||||
const rect = progressRef.current.getBoundingClientRect()
|
||||
const percent = Math.max(0, Math.min(1, (ev.clientX - rect.left) / rect.width))
|
||||
onSeek(percent * material.duration)
|
||||
}
|
||||
doSeek(e.nativeEvent)
|
||||
const handleMove = (ev: MouseEvent) => doSeek(ev)
|
||||
const handleUp = () => {
|
||||
document.removeEventListener("mousemove", handleMove)
|
||||
document.removeEventListener("mouseup", handleUp)
|
||||
}
|
||||
document.addEventListener("mousemove", handleMove)
|
||||
document.addEventListener("mouseup", handleUp)
|
||||
}
|
||||
|
||||
const progress = material.duration > 0 ? (currentTime / material.duration) * 100 : 0
|
||||
|
||||
@@ -58,6 +75,7 @@ const VoiceMaterialRow: React.FC<VoiceRowProps> = ({
|
||||
<div
|
||||
className={`vmat-row ${genderClass(material.gender)}${isPlaying ? " playing" : ""}${isSelected ? " selected" : ""}${batchMode ? " batch-mode" : ""}`}
|
||||
>
|
||||
{/* 批量选择 checkbox */}
|
||||
{(batchMode || isSelected) && (
|
||||
<div
|
||||
className={`vmat-row-checkbox vmat-checkbox${isSelected ? " checked" : ""}`}
|
||||
@@ -70,6 +88,7 @@ const VoiceMaterialRow: React.FC<VoiceRowProps> = ({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 播放按钮 */}
|
||||
<button
|
||||
type="button"
|
||||
className="vmat-row-play"
|
||||
@@ -82,31 +101,60 @@ const VoiceMaterialRow: React.FC<VoiceRowProps> = ({
|
||||
{isPlaying ? <PauseCircleOutlined /> : <PlayCircleOutlined />}
|
||||
</button>
|
||||
|
||||
{/* 名称 + 描述 */}
|
||||
<div className="vmat-row-info">
|
||||
<h4 className="vmat-row-name">{material.name}</h4>
|
||||
{material.description && <p className="vmat-row-desc">{material.description}</p>}
|
||||
</div>
|
||||
|
||||
{/* 性别 */}
|
||||
<span className={`vmat-row-gender ${genderClass(material.gender)}`}>
|
||||
{genderIcon(material.gender)}
|
||||
{genderLabel(material.gender)}
|
||||
</span>
|
||||
|
||||
{/* 标签 */}
|
||||
<div className="vmat-row-tags">
|
||||
<TagDisplay tagIds={material.tagIds} tagMap={tagMap} onAddTag={() => onEdit()} />
|
||||
{material.tagIds.length === 0 ? (
|
||||
<span className="vmat-tag-empty" onClick={() => onEdit()}>
|
||||
添加标签
|
||||
</span>
|
||||
) : (
|
||||
<>
|
||||
{material.tagIds.slice(0, MAX_ROW_TAGS).map((tagId, i) => (
|
||||
<Tag key={tagId} variant={TAG_VARIANTS[i % TAG_VARIANTS.length]}>
|
||||
{tagMap.get(tagId)?.name ?? tagId}
|
||||
</Tag>
|
||||
))}
|
||||
{material.tagIds.length > MAX_ROW_TAGS && (
|
||||
<Tooltip
|
||||
title={material.tagIds
|
||||
.slice(MAX_ROW_TAGS)
|
||||
.map((id) => tagMap.get(id)?.name ?? id)
|
||||
.join("、")}
|
||||
>
|
||||
<Tag className="vmat-tag-overflow">+{material.tagIds.length - MAX_ROW_TAGS}</Tag>
|
||||
</Tooltip>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div ref={progressRef} className="vmat-row-progress" onMouseDown={handleMouseDown}>
|
||||
{/* 进度条(可拖拽) */}
|
||||
<div ref={progressRef} className="vmat-row-progress" onMouseDown={handleProgressMouseDown}>
|
||||
<div className="vmat-row-progress-bar" style={{ width: `${progress}%` }} />
|
||||
{isPlaying && <div className="vmat-progress-thumb" style={{ left: `${progress}%` }} />}
|
||||
</div>
|
||||
|
||||
{/* 时长 */}
|
||||
<span className="vmat-row-time">
|
||||
{isPlaying ? formatDuration(currentTime) : formatDuration(material.duration)}
|
||||
</span>
|
||||
|
||||
{/* 文件大小 */}
|
||||
<span className="vmat-row-size">{formatFileSize(material.fileSize)}</span>
|
||||
|
||||
{/* 操作 */}
|
||||
<div className="vmat-row-actions">
|
||||
<button
|
||||
type="button"
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
import { useState, useRef, useCallback, useMemo } from "react"
|
||||
import type { TagItem } from "@/api/tags"
|
||||
|
||||
interface UseTagInputOptions {
|
||||
value: string[]
|
||||
onChange: (tagIds: string[]) => void
|
||||
tags: TagItem[]
|
||||
onCreateTag: (name: string) => Promise<TagItem>
|
||||
}
|
||||
|
||||
export function useTagInput({ value, onChange, tags, onCreateTag }: UseTagInputOptions) {
|
||||
const [inputVal, setInputVal] = useState("")
|
||||
const [showSuggestions, setShowSuggestions] = useState(false)
|
||||
const inputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
const findTagByName = useCallback(
|
||||
(name: string) => tags.find((t) => t.name.toLowerCase() === name.toLowerCase()),
|
||||
[tags],
|
||||
)
|
||||
|
||||
const addTagId = useCallback(
|
||||
(tagId: string) => {
|
||||
if (value.includes(tagId)) return
|
||||
onChange([...value, tagId])
|
||||
setInputVal("")
|
||||
setShowSuggestions(false)
|
||||
},
|
||||
[value, onChange],
|
||||
)
|
||||
|
||||
const addTagByName = useCallback(
|
||||
async (name: string) => {
|
||||
const trimmed = name.trim()
|
||||
if (!trimmed) return
|
||||
const existing = findTagByName(trimmed)
|
||||
if (existing) {
|
||||
addTagId(existing.id)
|
||||
} else {
|
||||
try {
|
||||
const created = await onCreateTag(trimmed)
|
||||
addTagId(created.id)
|
||||
} catch {
|
||||
/* 创建失败静默忽略 */
|
||||
}
|
||||
}
|
||||
},
|
||||
[findTagByName, addTagId, onCreateTag],
|
||||
)
|
||||
|
||||
const removeTagId = useCallback(
|
||||
(tagId: string) => {
|
||||
onChange(value.filter((t) => t !== tagId))
|
||||
},
|
||||
[value, onChange],
|
||||
)
|
||||
|
||||
const suggestions = useMemo(() => {
|
||||
if (!inputVal.trim()) return []
|
||||
const lower = inputVal.toLowerCase()
|
||||
return tags.filter((t) => t.name.toLowerCase().includes(lower) && !value.includes(t.id))
|
||||
}, [inputVal, tags, value])
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault()
|
||||
if (suggestions.length > 0) {
|
||||
addTagId(suggestions[0].id)
|
||||
} else {
|
||||
addTagByName(inputVal)
|
||||
}
|
||||
} else if (e.key === "Backspace" && !inputVal && value.length > 0) {
|
||||
removeTagId(value[value.length - 1])
|
||||
}
|
||||
}
|
||||
|
||||
const focus = () => inputRef.current?.focus()
|
||||
|
||||
return {
|
||||
inputVal,
|
||||
setInputVal,
|
||||
showSuggestions,
|
||||
setShowSuggestions,
|
||||
inputRef,
|
||||
suggestions,
|
||||
addTagId,
|
||||
removeTagId,
|
||||
handleKeyDown,
|
||||
focus,
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
import React from "react"
|
||||
import { Tooltip } from "antd"
|
||||
import { Tag } from "@/components/ui"
|
||||
import { type TagItem } from "@/api/tags"
|
||||
import { MAX_ROW_TAGS, TAG_VARIANTS } from "../../constants"
|
||||
|
||||
interface TagDisplayProps {
|
||||
tagIds: string[]
|
||||
tagMap: Map<string, TagItem>
|
||||
onAddTag?: () => void
|
||||
}
|
||||
|
||||
const TagDisplay: React.FC<TagDisplayProps> = ({ tagIds, tagMap, onAddTag }) => {
|
||||
if (tagIds.length === 0) {
|
||||
return (
|
||||
<span className="vmat-tag-empty" onClick={onAddTag}>
|
||||
添加标签
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
const visible = tagIds.slice(0, MAX_ROW_TAGS)
|
||||
const overflow = tagIds.slice(MAX_ROW_TAGS)
|
||||
|
||||
return (
|
||||
<>
|
||||
{visible.map((tagId, i) => (
|
||||
<Tag key={tagId} variant={TAG_VARIANTS[i % TAG_VARIANTS.length]}>
|
||||
{tagMap.get(tagId)?.name ?? tagId}
|
||||
</Tag>
|
||||
))}
|
||||
{overflow.length > 0 && (
|
||||
<Tooltip title={overflow.map((id) => tagMap.get(id)?.name ?? id).join("、")}>
|
||||
<Tag className="vmat-tag-overflow">+{overflow.length}</Tag>
|
||||
</Tooltip>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default TagDisplay
|
||||
@@ -1,38 +0,0 @@
|
||||
import { useRef, useCallback } from "react"
|
||||
|
||||
interface UseRowProgressOptions {
|
||||
duration: number
|
||||
onSeek: (time: number) => void
|
||||
}
|
||||
|
||||
export function useRowProgress({ duration, onSeek }: UseRowProgressOptions) {
|
||||
const progressRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const handleMouseDown = useCallback(
|
||||
(e: React.MouseEvent<HTMLDivElement>) => {
|
||||
if (!progressRef.current) return
|
||||
e.preventDefault()
|
||||
|
||||
const doSeek = (ev: MouseEvent) => {
|
||||
if (!progressRef.current) return
|
||||
const rect = progressRef.current.getBoundingClientRect()
|
||||
const percent = Math.max(0, Math.min(1, (ev.clientX - rect.left) / rect.width))
|
||||
onSeek(percent * duration)
|
||||
}
|
||||
|
||||
doSeek(e.nativeEvent)
|
||||
|
||||
const handleMove = (ev: MouseEvent) => doSeek(ev)
|
||||
const handleUp = () => {
|
||||
document.removeEventListener("mousemove", handleMove)
|
||||
document.removeEventListener("mouseup", handleUp)
|
||||
}
|
||||
|
||||
document.addEventListener("mousemove", handleMove)
|
||||
document.addEventListener("mouseup", handleUp)
|
||||
},
|
||||
[duration, onSeek],
|
||||
)
|
||||
|
||||
return { progressRef, handleMouseDown }
|
||||
}
|
||||
@@ -1,9 +1,16 @@
|
||||
import React from "react"
|
||||
import { DeleteOutlined, ReloadOutlined, CloseCircleOutlined } from "@ant-design/icons"
|
||||
import {
|
||||
SoundOutlined,
|
||||
PlayCircleOutlined,
|
||||
PauseCircleOutlined,
|
||||
DeleteOutlined,
|
||||
ReloadOutlined,
|
||||
CloseCircleOutlined,
|
||||
UserOutlined,
|
||||
} from "@ant-design/icons"
|
||||
import { Tooltip } from "antd"
|
||||
import { type ClonedVoiceDisplay } from "@/pages/voices/types"
|
||||
import CardHeader from "./clone-voice-card/CardHeader"
|
||||
import CardFooter from "./clone-voice-card/CardFooter"
|
||||
import { CLONE_STATUS_CONFIG } from "@/pages/voices/constants"
|
||||
|
||||
export interface CloneVoiceCardProps {
|
||||
voice: ClonedVoiceDisplay
|
||||
@@ -21,18 +28,26 @@ export interface CloneVoiceCardProps {
|
||||
const CloneVoiceCard: React.FC<CloneVoiceCardProps> = ({
|
||||
voice,
|
||||
isPlaying,
|
||||
currentTime,
|
||||
onPlay,
|
||||
onPause,
|
||||
onUse,
|
||||
onDelete,
|
||||
onRetry,
|
||||
onShowDetail,
|
||||
...footerProps
|
||||
}) => {
|
||||
const statusCfg = CLONE_STATUS_CONFIG[voice.status]
|
||||
const isFailed = voice.status === "failed"
|
||||
const isProcessing = voice.status === "processing"
|
||||
const genderText =
|
||||
voice.gender === "male" ? "男声" : voice.gender === "female" ? "女声" : voice.gender
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`xx-clone-card${isPlaying ? " playing" : ""}${isFailed ? " failed" : ""}`}
|
||||
onClick={isFailed ? undefined : onShowDetail}
|
||||
>
|
||||
{/* 右上角操作按钮 */}
|
||||
<div className="xx-clone-card-actions">
|
||||
<Tooltip title="删除">
|
||||
<button
|
||||
@@ -62,8 +77,38 @@ const CloneVoiceCard: React.FC<CloneVoiceCardProps> = ({
|
||||
)}
|
||||
</div>
|
||||
|
||||
<CardHeader voice={voice} />
|
||||
{/* 头部:头像 + 名称 + 状态 */}
|
||||
<div className="xx-clone-card-header">
|
||||
<div className={`xx-clone-avatar${isProcessing ? " xx-clone-avatar--processing" : ""}`}>
|
||||
<SoundOutlined />
|
||||
</div>
|
||||
<div className="xx-clone-header-info">
|
||||
<h4 className="xx-clone-name" title={voice.name}>
|
||||
{voice.name}
|
||||
</h4>
|
||||
<span className={`xx-clone-status ${statusCfg.className}`}>
|
||||
<span className="xx-clone-status-dot" />
|
||||
{statusCfg.label}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 描述 */}
|
||||
{voice.description && <p className="xx-clone-desc">{voice.description}</p>}
|
||||
|
||||
{/* 元信息 */}
|
||||
<div className="xx-clone-meta">
|
||||
{(voice.gender || voice.language) && (
|
||||
<span className="xx-clone-meta-item">
|
||||
<UserOutlined />
|
||||
{genderText}
|
||||
{voice.language ? ` · ${voice.language}` : ""}
|
||||
</span>
|
||||
)}
|
||||
<span className="xx-clone-meta-item">{voice.createdAt}</span>
|
||||
</div>
|
||||
|
||||
{/* 错误信息 */}
|
||||
{isFailed && voice.errorMessage && (
|
||||
<div className="xx-clone-error">
|
||||
<CloseCircleOutlined />
|
||||
@@ -71,7 +116,63 @@ const CloneVoiceCard: React.FC<CloneVoiceCardProps> = ({
|
||||
</div>
|
||||
)}
|
||||
|
||||
<CardFooter voice={voice} isPlaying={isPlaying} onRetry={onRetry} {...footerProps} />
|
||||
{/* 底部操作区 */}
|
||||
<div className="xx-clone-footer">
|
||||
{voice.status === "ready" && (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-clone-play-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
isPlaying ? onPause() : onPlay()
|
||||
}}
|
||||
title={isPlaying ? "暂停" : "试听"}
|
||||
>
|
||||
{isPlaying ? <PauseCircleOutlined /> : <PlayCircleOutlined />}
|
||||
</button>
|
||||
<div className="xx-clone-progress">
|
||||
<div
|
||||
className="xx-clone-progress-bar"
|
||||
style={{
|
||||
width: isPlaying
|
||||
? `${Math.min((currentTime / Math.max(voice.duration, 1)) * 100, 100)}%`
|
||||
: "0%",
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-clone-use-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onUse()
|
||||
}}
|
||||
>
|
||||
使用
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
{isProcessing && (
|
||||
<div className="xx-clone-processing-hint">
|
||||
<ReloadOutlined spin />
|
||||
克隆处理中,请稍候...
|
||||
</div>
|
||||
)}
|
||||
{isFailed && (
|
||||
<button
|
||||
type="button"
|
||||
className="xx-clone-retry-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onRetry()
|
||||
}}
|
||||
>
|
||||
<ReloadOutlined />
|
||||
重试克隆
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
import React from "react"
|
||||
import { PlayCircleOutlined, PauseCircleOutlined, ReloadOutlined } from "@ant-design/icons"
|
||||
import { type ClonedVoiceDisplay } from "@/pages/voices/types"
|
||||
|
||||
interface CardFooterProps {
|
||||
voice: ClonedVoiceDisplay
|
||||
isPlaying: boolean
|
||||
currentTime: number
|
||||
onPlay: () => void
|
||||
onPause: () => void
|
||||
onUse: () => void
|
||||
onRetry: () => void
|
||||
}
|
||||
|
||||
const CardFooter: React.FC<CardFooterProps> = ({
|
||||
voice,
|
||||
isPlaying,
|
||||
currentTime,
|
||||
onPlay,
|
||||
onPause,
|
||||
onUse,
|
||||
onRetry,
|
||||
}) => {
|
||||
const isFailed = voice.status === "failed"
|
||||
const isProcessing = voice.status === "processing"
|
||||
|
||||
return (
|
||||
<div className="xx-clone-footer">
|
||||
{voice.status === "ready" && (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-clone-play-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
isPlaying ? onPause() : onPlay()
|
||||
}}
|
||||
title={isPlaying ? "暂停" : "试听"}
|
||||
>
|
||||
{isPlaying ? <PauseCircleOutlined /> : <PlayCircleOutlined />}
|
||||
</button>
|
||||
<div className="xx-clone-progress">
|
||||
<div
|
||||
className="xx-clone-progress-bar"
|
||||
style={{
|
||||
width: isPlaying
|
||||
? `${Math.min((currentTime / Math.max(voice.duration, 1)) * 100, 100)}%`
|
||||
: "0%",
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-clone-use-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onUse()
|
||||
}}
|
||||
>
|
||||
使用
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
{isProcessing && (
|
||||
<div className="xx-clone-processing-hint">
|
||||
<ReloadOutlined spin />
|
||||
克隆处理中,请稍候...
|
||||
</div>
|
||||
)}
|
||||
{isFailed && (
|
||||
<button
|
||||
type="button"
|
||||
className="xx-clone-retry-btn"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onRetry()
|
||||
}}
|
||||
>
|
||||
<ReloadOutlined />
|
||||
重试克隆
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default CardFooter
|
||||
@@ -1,51 +0,0 @@
|
||||
import React from "react"
|
||||
import { SoundOutlined, UserOutlined } from "@ant-design/icons"
|
||||
import { type ClonedVoiceDisplay } from "@/pages/voices/types"
|
||||
import { CLONE_STATUS_CONFIG } from "@/pages/voices/constants"
|
||||
|
||||
interface CardHeaderProps {
|
||||
voice: ClonedVoiceDisplay
|
||||
}
|
||||
|
||||
const genderTextOf = (gender: string) =>
|
||||
gender === "male" ? "男声" : gender === "female" ? "女声" : gender
|
||||
|
||||
const CardHeader: React.FC<CardHeaderProps> = ({ voice }) => {
|
||||
const statusCfg = CLONE_STATUS_CONFIG[voice.status]
|
||||
const isProcessing = voice.status === "processing"
|
||||
const genderText = genderTextOf(voice.gender ?? "")
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="xx-clone-card-header">
|
||||
<div className={`xx-clone-avatar${isProcessing ? " xx-clone-avatar--processing" : ""}`}>
|
||||
<SoundOutlined />
|
||||
</div>
|
||||
<div className="xx-clone-header-info">
|
||||
<h4 className="xx-clone-name" title={voice.name}>
|
||||
{voice.name}
|
||||
</h4>
|
||||
<span className={`xx-clone-status ${statusCfg.className}`}>
|
||||
<span className="xx-clone-status-dot" />
|
||||
{statusCfg.label}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{voice.description && <p className="xx-clone-desc">{voice.description}</p>}
|
||||
|
||||
<div className="xx-clone-meta">
|
||||
{(voice.gender || voice.language) && (
|
||||
<span className="xx-clone-meta-item">
|
||||
<UserOutlined />
|
||||
{genderText}
|
||||
{voice.language ? ` · ${voice.language}` : ""}
|
||||
</span>
|
||||
)}
|
||||
<span className="xx-clone-meta-item">{voice.createdAt}</span>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default CardHeader
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional
|
||||
|
||||
# ── 单轨时间计算 ──────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -259,6 +261,7 @@ def db_to_linear(db: float) -> float:
|
||||
Returns:
|
||||
线性音量值
|
||||
"""
|
||||
import math
|
||||
|
||||
return 10 ** (db / 20.0)
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from packages.domain.pip_config import (
|
||||
ANIMATION_FADE,
|
||||
@@ -348,7 +349,7 @@ def build_pip_filters(
|
||||
input_args: list[str] = []
|
||||
current_label = base_label
|
||||
|
||||
for i, (layer, path) in enumerate(zip(layers, source_paths, strict=False)):
|
||||
for i, (layer, path) in enumerate(zip(layers, source_paths)):
|
||||
# 计算实际大小
|
||||
pip_w, pip_h = compute_pip_size(layer, output_width, output_height)
|
||||
|
||||
@@ -482,4 +483,4 @@ def count_visible_layers(layers: list[PiPLayerConfig]) -> int:
|
||||
|
||||
def sort_layers_by_z_index(layers: list[PiPLayerConfig]) -> list[PiPLayerConfig]:
|
||||
"""按 z_index 从小到大排序图层(z_index 小的先画,在底层)."""
|
||||
return sorted(layers, key=lambda layer: layer.z_index)
|
||||
return sorted(layers, key=lambda l: l.z_index)
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from packages.domain.speed_config import (
|
||||
DEFAULT_SPEED,
|
||||
MAX_SPEED,
|
||||
|
||||
@@ -4,14 +4,12 @@
|
||||
所有符号均从该模块重新导出,请新代码直接 import packages.shared.url_security。
|
||||
"""
|
||||
|
||||
from packages.domain.url_security import ( # noqa: F401
|
||||
ALLOWED_VIDEO_MIME_TYPES,
|
||||
)
|
||||
from packages.shared.url_security import ( # noqa: F401
|
||||
ALLOWED_AUDIO_MIME_TYPES,
|
||||
ALLOWED_IMAGE_MIME_TYPES,
|
||||
ALLOWED_PORTS,
|
||||
ALLOWED_SCHEMES,
|
||||
ALLOWED_VIDEO_MIME_TYPES,
|
||||
DEFAULT_MAX_DOWNLOAD_SIZE,
|
||||
MAX_URL_LENGTH,
|
||||
TRUSTED_DOMAINS,
|
||||
|
||||
@@ -8,10 +8,9 @@ from packages.adapters.sqlalchemy_impl import (
|
||||
from packages.adapters.sqlalchemy_impl.schema_guard import assert_auto_create_schema_allowed
|
||||
|
||||
settings = get_settings()
|
||||
_db_url = settings.effective_database_url
|
||||
ensure_database_exists(_db_url)
|
||||
ensure_database_exists(settings.database_url)
|
||||
engine, SessionLocal = build_session_factory(
|
||||
_db_url,
|
||||
settings.database_url,
|
||||
pool_size=settings.database_pool_size,
|
||||
max_overflow=settings.database_max_overflow,
|
||||
pool_timeout=settings.database_pool_timeout,
|
||||
|
||||
@@ -48,20 +48,12 @@ def build_session_factory(
|
||||
return engine, session_factory
|
||||
|
||||
|
||||
def _is_sqlite(database_url: str) -> bool:
|
||||
"""检测是否为 SQLite 数据库 URL."""
|
||||
return database_url.startswith("sqlite")
|
||||
|
||||
|
||||
def _build_admin_url(database_url: str) -> URL:
|
||||
url = make_url(database_url)
|
||||
return url.set(database="postgres")
|
||||
|
||||
|
||||
def ensure_database_exists(database_url: str) -> None:
|
||||
"""确保数据库存在(仅 PostgreSQL 需要,SQLite 自动创建)."""
|
||||
if _is_sqlite(database_url):
|
||||
return
|
||||
target_url = make_url(database_url)
|
||||
admin_engine = create_engine(_build_admin_url(database_url), isolation_level="AUTOCOMMIT")
|
||||
try:
|
||||
@@ -78,14 +70,6 @@ def ensure_database_exists(database_url: str) -> None:
|
||||
|
||||
|
||||
def initialize_database(engine) -> None:
|
||||
"""初始化数据库 schema。
|
||||
|
||||
PostgreSQL 使用 advisory lock 防止并发初始化冲突;
|
||||
SQLite 直接 create_all(单文件,无并发风险)。
|
||||
"""
|
||||
if _is_sqlite(str(engine.url)):
|
||||
Base.metadata.create_all(bind=engine)
|
||||
return
|
||||
with engine.connect() as connection:
|
||||
connection.execute(text("SELECT pg_advisory_lock(:lock_id)"), {"lock_id": SCHEMA_INIT_LOCK_ID})
|
||||
try:
|
||||
|
||||
@@ -34,9 +34,6 @@ class SharedSettings(BaseSettings):
|
||||
database_pool_timeout: int = 30
|
||||
database_pool_recycle: int = 3600
|
||||
|
||||
# 测试用:使用 SQLite 内存数据库(CI 环境无需 PostgreSQL)
|
||||
use_in_memory_db: bool = False
|
||||
|
||||
# ── Redis ────────────────────────────────────────────────────────────
|
||||
redis_url: str = "redis://localhost:6379/0"
|
||||
|
||||
@@ -69,16 +66,6 @@ class SharedSettings(BaseSettings):
|
||||
doubao_timeout: int = 30
|
||||
doubao_max_retries: int = 2
|
||||
|
||||
@property
|
||||
def effective_database_url(self) -> str:
|
||||
"""返回实际使用的数据库 URL。
|
||||
|
||||
当 USE_IN_MEMORY_DB=True 时返回 SQLite 内存 URL,否则返回 database_url。
|
||||
"""
|
||||
if self.use_in_memory_db:
|
||||
return "sqlite:///./test.db"
|
||||
return self.database_url
|
||||
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=".env",
|
||||
env_file_encoding="utf-8",
|
||||
|
||||
@@ -22,11 +22,17 @@ import urllib.error
|
||||
import urllib.request
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from packages.domain.url_security import ALLOWED_AUDIO_MIME_TYPES as _allowed_audio_base
|
||||
from packages.domain.url_security import ALLOWED_IMAGE_MIME_TYPES as _allowed_image_base
|
||||
from packages.domain.url_security import (
|
||||
ALLOWED_AUDIO_MIME_TYPES,
|
||||
ALLOWED_IMAGE_MIME_TYPES,
|
||||
)
|
||||
from packages.domain.url_security import ALLOWED_PORTS as _allowed_ports_base
|
||||
from packages.domain.url_security import ALLOWED_SCHEMES as _allowed_schemes_base
|
||||
from packages.domain.url_security import MAX_URL_LENGTH as _max_url_length_base
|
||||
from packages.domain.url_security import (
|
||||
ALLOWED_VIDEO_MIME_TYPES,
|
||||
MAGIC_NUMBERS,
|
||||
MAX_URL_LENGTH,
|
||||
)
|
||||
from packages.domain.url_security import UrlSecurityError as _UrlSecurityError_base
|
||||
from packages.domain.url_security import check_internal_hostname as _check_internal_hostname_base
|
||||
from packages.domain.url_security import check_ssrf_ip as _check_ssrf_ip_base
|
||||
@@ -40,21 +46,8 @@ logger = logging.getLogger(__name__)
|
||||
# ── 兼容导出(保持原有变量名供外部引用) ──────────────────────────────────
|
||||
ALLOWED_SCHEMES = set(_allowed_schemes_base)
|
||||
ALLOWED_PORTS = set(_allowed_ports_base)
|
||||
ALLOWED_AUDIO_MIME_TYPES = set(_allowed_audio_base)
|
||||
ALLOWED_IMAGE_MIME_TYPES = set(_allowed_image_base)
|
||||
MAX_URL_LENGTH = _max_url_length_base
|
||||
UrlSecurityError = _UrlSecurityError_base
|
||||
|
||||
# 私有别名(供测试和内部引用)
|
||||
_check_internal_hostnames = _check_internal_hostname_base
|
||||
_check_ssrf_ip = _check_ssrf_ip_base
|
||||
|
||||
|
||||
def _is_trusted_domain(hostname: str) -> bool:
|
||||
"""便捷包装:使用模块级 TRUSTED_DOMAINS 做可信域名检查."""
|
||||
return _is_trusted_domain_base(hostname, TRUSTED_DOMAINS)
|
||||
|
||||
|
||||
# 可信域名白名单(从环境变量读取)
|
||||
TRUSTED_DOMAINS: set[str] = set()
|
||||
_env_trusted = os.environ.get("URL_SECURITY_TRUSTED_DOMAINS", "")
|
||||
@@ -146,11 +139,7 @@ def validate_url_safety(url: str, *, purpose: str = "download") -> str:
|
||||
|
||||
parsed = urlparse(url)
|
||||
hostname = parsed.hostname
|
||||
if hostname and _is_ip_address_base(hostname):
|
||||
# IP 直接访问:通过本地别名调用以便 mock
|
||||
if ALLOW_DIRECT_IP:
|
||||
_check_ssrf_ip(hostname)
|
||||
elif hostname:
|
||||
if hostname and not _is_ip_address_base(hostname):
|
||||
try:
|
||||
_check_ssrf_domain(hostname)
|
||||
except UrlSecurityError:
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
[pytest]
|
||||
pythonpath = . apps/api apps/worker packages
|
||||
testpaths = tests
|
||||
# importlib 模式避免同名测试文件的模块名冲突
|
||||
addopts = --import-mode=importlib
|
||||
|
||||
# ===== 覆盖率配置 =====
|
||||
# 覆盖率统计范围(供 --cov 使用时的默认源)
|
||||
|
||||
Executable → Regular
+67
-78
@@ -1,13 +1,12 @@
|
||||
#!/usr/bin/env bash
|
||||
# 自动合并:CI全绿+已审批后自动squash merge PR到develop
|
||||
# 短作业模式:只检查一次,不满足条件就退出,由pr-auto-scan定时兜底
|
||||
# 环境变量:GITHUB_TOKEN, MERGE_TOKEN, PR_NUMBER, PR_HEAD_SHA, BASE_REF, GITHUB_API_URL, GITHUB_REPOSITORY
|
||||
set -eu
|
||||
|
||||
set -eu
|
||||
|
||||
echo "PR #${PR_NUMBER} - 检查CI状态+审批并自动合并到${BASE_REF}"
|
||||
echo
|
||||
echo "模式: 短作业(只检查一次,不满足则退出,由pr-auto-scan定时兜底)"
|
||||
echo
|
||||
|
||||
# 只合develop分支
|
||||
if [ "$BASE_REF" != "develop" ]; then
|
||||
@@ -23,7 +22,6 @@ TOTAL=$(echo "$FILES" | grep -cv '^$' || true)
|
||||
FRONTEND_COUNT=$(echo "$FILES" | grep -c '^apps/web/' || true)
|
||||
BACKEND_COUNT=$((TOTAL - FRONTEND_COUNT))
|
||||
echo "变更文件: ${TOTAL} 个 (前端: ${FRONTEND_COUNT}, 后端/公共: ${BACKEND_COUNT})"
|
||||
echo
|
||||
|
||||
# 使用统一的CI Gate门禁(单一检查点,自动处理前端/后端/全栈跳过逻辑)
|
||||
CONTEXTS=(
|
||||
@@ -32,20 +30,21 @@ CONTEXTS=(
|
||||
echo "检查CI Gate统一门禁"
|
||||
echo
|
||||
|
||||
# 等待60秒,给CI启动写status的时间
|
||||
echo "等待60秒让CI启动..."
|
||||
sleep 60
|
||||
# 初始等待30秒,给CI启动写status的时间
|
||||
echo "等待30秒让CI启动..."
|
||||
sleep 30
|
||||
|
||||
# 405计数器(单次运行内重试)
|
||||
# 405连续计数器
|
||||
MERGE_405_COUNT=0
|
||||
MAX_405_RETRIES=3
|
||||
MAX_405_RETRIES=10
|
||||
|
||||
check_and_merge() {
|
||||
# 轮询等待,最多30分钟(180次x10秒)
|
||||
for attempt in $(seq 1 90); do # 最多等45分钟(90次x30秒),确保等得到Worker构建完成
|
||||
ALL_SUCCESS=true
|
||||
ANY_FAILED=false
|
||||
ANY_PENDING=false
|
||||
|
||||
echo "--- 检查CI状态 ($(date '+%H:%M:%S')) ---"
|
||||
echo "--- 第${attempt}次检查 ($(date '+%H:%M:%S')) ---"
|
||||
|
||||
# 检查CI状态
|
||||
for ctx in "${CONTEXTS[@]}"; do
|
||||
@@ -62,87 +61,77 @@ check_and_merge() {
|
||||
fi
|
||||
done
|
||||
|
||||
# CI有失败 → 不合并,直接退出
|
||||
if [ "$ANY_FAILED" = "true" ]; then
|
||||
echo
|
||||
echo "❌ CI有失败项,不自动合并"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# CI未全绿(pending中)→ 退出,等下次触发
|
||||
if [ "$ALL_SUCCESS" != "true" ]; then
|
||||
echo
|
||||
echo "⏳ CI尚未全绿(仍有pending),退出等待下次触发"
|
||||
echo " (pr-auto-scan每5分钟扫描一次,CI通过后会自动合并)"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# CI全绿 → 合并
|
||||
echo
|
||||
echo "✅ CI全绿,执行自动合并"
|
||||
echo "等待30秒冷却,给Gitea内部状态同步时间..."
|
||||
sleep 30
|
||||
|
||||
# 幂等检查:PR是否还是open
|
||||
PR_STATE=$(curl -s -H "Authorization: token ${MERGE_TOKEN}" \
|
||||
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}" \
|
||||
| python3 -c "import sys,json; print(json.load(sys.stdin).get('state',''))")
|
||||
|
||||
if [ "$PR_STATE" != "open" ]; then
|
||||
echo "PR状态为 ${PR_STATE},无需合并"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# 执行squash merge
|
||||
HTTP_CODE=$(curl -s -o /tmp/merge_resp.json -w "%{http_code}" \
|
||||
-X POST \
|
||||
-H "Authorization: token ${MERGE_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"do":"squash","merge_title_field":"","merge_message_field":"","delete_branch_after_merge":true,"force_merge":false}' \
|
||||
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/merge")
|
||||
|
||||
echo "合并API HTTP状态: $HTTP_CODE"
|
||||
|
||||
if [ "$HTTP_CODE" = "200" ]; then
|
||||
echo "✅ 自动合并成功"
|
||||
exit 0
|
||||
elif [ "$HTTP_CODE" = "405" ]; then
|
||||
MERGE_405_COUNT=$((MERGE_405_COUNT + 1))
|
||||
echo "⚠️ 合并返回405(第${MERGE_405_COUNT}次),可能CI状态尚未同步或有未解决的门禁"
|
||||
cat /tmp/merge_resp.json 2>/dev/null || true
|
||||
if [ "$ALL_SUCCESS" = "true" ]; then
|
||||
echo
|
||||
if [ "$MERGE_405_COUNT" -ge "$MAX_405_RETRIES" ]; then
|
||||
echo "⚠️ 连续${MAX_405_RETRIES}次合并返回405,放弃本次自动合并"
|
||||
echo " (pr-auto-scan会继续尝试,需人工确认是否有冲突或门禁问题)"
|
||||
echo "CI全绿,执行自动合并"
|
||||
echo "等待60秒冷却,给Gitea内部状态同步时间..."
|
||||
sleep 60
|
||||
|
||||
# 幂等检查:PR是否还是open
|
||||
PR_STATE=$(curl -s -H "Authorization: token ${MERGE_TOKEN}" \
|
||||
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}" \
|
||||
| python3 -c "import sys,json; print(json.load(sys.stdin).get('state',''))")
|
||||
|
||||
if [ "$PR_STATE" != "open" ]; then
|
||||
echo "PR状态为 ${PR_STATE},无需合并"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# 执行squash merge
|
||||
HTTP_CODE=$(curl -s -o /tmp/merge_resp.json -w "%{http_code}" \
|
||||
-X POST \
|
||||
-H "Authorization: token ${MERGE_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"do":"squash","merge_title_field":"","merge_message_field":"","delete_branch_after_merge":true,"force_merge":false}' \
|
||||
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/merge")
|
||||
|
||||
echo "合并API HTTP状态: $HTTP_CODE"
|
||||
|
||||
if [ "$HTTP_CODE" = "200" ]; then
|
||||
echo "自动合并成功"
|
||||
exit 0
|
||||
elif [ "$HTTP_CODE" = "405" ]; then
|
||||
MERGE_405_COUNT=$((MERGE_405_COUNT + 1))
|
||||
echo "⚠️ 合并返回405(第${MERGE_405_COUNT}次),可能CI状态尚未同步或有未解决的门禁,继续等待重试..."
|
||||
cat /tmp/merge_resp.json 2>/dev/null || true
|
||||
echo
|
||||
if [ "$MERGE_405_COUNT" -ge "$MAX_405_RETRIES" ]; then
|
||||
echo "⚠️ 连续${MAX_405_RETRIES}次合并返回405,放弃自动合并(需人工确认,非代码问题)"
|
||||
curl -s -X POST \
|
||||
-H "Authorization: token ${MERGE_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"body": "Auto merge skipped after multiple 405 errors: PR may have conflicts or unresolved checks. Please review manually. This is not a CI failure."}' \
|
||||
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" > /dev/null 2>&1 || true
|
||||
exit 0
|
||||
fi
|
||||
sleep 30
|
||||
continue
|
||||
else
|
||||
echo "自动合并失败 (HTTP $HTTP_CODE)"
|
||||
cat /tmp/merge_resp.json 2>/dev/null || true
|
||||
curl -s -X POST \
|
||||
-H "Authorization: token ${MERGE_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"body": "Auto merge skipped after multiple 405 errors: PR may have conflicts or unresolved checks. Please review manually. This is not a CI failure."}' \
|
||||
-d "{\"body\": \"Auto merge failed (HTTP ${HTTP_CODE}), please check manually.\"}" \
|
||||
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" > /dev/null 2>&1 || true
|
||||
exit 0
|
||||
exit 1
|
||||
fi
|
||||
echo "30秒后重试..."
|
||||
sleep 30
|
||||
return 1 # 重试
|
||||
else
|
||||
echo "❌ 自动合并失败 (HTTP $HTTP_CODE)"
|
||||
cat /tmp/merge_resp.json 2>/dev/null || true
|
||||
curl -s -X POST \
|
||||
-H "Authorization: token ${MERGE_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"body\": \"Auto merge failed (HTTP ${HTTP_CODE}), please check manually.\"}" \
|
||||
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" > /dev/null 2>&1 || true
|
||||
exit 1
|
||||
# 本轮不满足合并条件,重置405计数器
|
||||
MERGE_405_COUNT=0
|
||||
fi
|
||||
}
|
||||
|
||||
# 最多重试3次(用于405重试,非CI轮询)
|
||||
for i in 1 2 3; do
|
||||
if check_and_merge; then
|
||||
if [ "$ANY_FAILED" = "true" ]; then
|
||||
echo
|
||||
echo "CI有失败项,不自动合并"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
sleep 30
|
||||
done
|
||||
|
||||
echo
|
||||
echo "本次检查未满足合并条件,退出。pr-auto-scan每5分钟会继续扫描。"
|
||||
echo "快速检查超时(3分钟),CI尚未全绿或无审批,退出等待下次触发"
|
||||
exit 0
|
||||
|
||||
@@ -1,531 +0,0 @@
|
||||
"""ass_subtitle_builder 单元测试 - wave169
|
||||
|
||||
覆盖:
|
||||
- hex_to_ass_color 颜色转换
|
||||
- position_to_ass_alignment 位置对齐映射
|
||||
- build_ass_style Style行构建
|
||||
- escape_ass_text 文本转义
|
||||
- format_ass_time 时间格式化
|
||||
- build_ass_content 完整ASS内容生成
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from packages.domain.ass_subtitle_builder import (
|
||||
TITLE_MARGIN_BOTTOM,
|
||||
TITLE_MARGIN_SIDE,
|
||||
TITLE_MARGIN_TOP,
|
||||
build_ass_content,
|
||||
build_ass_style,
|
||||
escape_ass_text,
|
||||
format_ass_time,
|
||||
hex_to_ass_color,
|
||||
position_to_ass_alignment,
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# hex_to_ass_color
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestHexToAssColor:
|
||||
def test_red(self):
|
||||
# #FF0000 → &H0000FF (BBGGRR)
|
||||
assert hex_to_ass_color("#FF0000") == "&H0000FF"
|
||||
|
||||
def test_blue(self):
|
||||
# #0000FF → &HFF0000
|
||||
assert hex_to_ass_color("#0000FF") == "&HFF0000"
|
||||
|
||||
def test_green(self):
|
||||
# #00FF00 → &H00FF00
|
||||
assert hex_to_ass_color("#00FF00") == "&H00FF00"
|
||||
|
||||
def test_white(self):
|
||||
assert hex_to_ass_color("#FFFFFF") == "&HFFFFFF"
|
||||
|
||||
def test_black(self):
|
||||
assert hex_to_ass_color("#000000") == "&H000000"
|
||||
|
||||
def test_without_hash_prefix(self):
|
||||
assert hex_to_ass_color("FF0000") == "&H0000FF"
|
||||
|
||||
def test_mixed_case(self):
|
||||
assert hex_to_ass_color("#aBcDeF") == "&HEFCDAB"
|
||||
|
||||
def test_invalid_length_short(self):
|
||||
assert hex_to_ass_color("#FFF") == "&H000000"
|
||||
|
||||
def test_invalid_length_long(self):
|
||||
assert hex_to_ass_color("#FF0000FF") == "&H000000"
|
||||
|
||||
def test_empty_string(self):
|
||||
assert hex_to_ass_color("") == "&H000000"
|
||||
|
||||
def test_uppercase_output(self):
|
||||
result = hex_to_ass_color("#abcdef")
|
||||
assert result == result.upper()
|
||||
|
||||
|
||||
# ============================================================
|
||||
# position_to_ass_alignment
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestPositionToAssAlignment:
|
||||
def test_top(self):
|
||||
assert position_to_ass_alignment("top") == 8
|
||||
|
||||
def test_center(self):
|
||||
assert position_to_ass_alignment("center") == 5
|
||||
|
||||
def test_bottom(self):
|
||||
assert position_to_ass_alignment("bottom") == 2
|
||||
|
||||
def test_unknown_defaults_top(self):
|
||||
assert position_to_ass_alignment("unknown") == 8
|
||||
|
||||
def test_empty_defaults_top(self):
|
||||
assert position_to_ass_alignment("") == 8
|
||||
|
||||
|
||||
# ============================================================
|
||||
# build_ass_style
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestBuildAssStyle:
|
||||
def test_minimal_style(self):
|
||||
result = build_ass_style("Default")
|
||||
assert result.startswith("Style: Default,")
|
||||
|
||||
def test_contains_font_name(self):
|
||||
result = build_ass_style("S1", font_name="Arial")
|
||||
assert "Arial" in result
|
||||
|
||||
def test_contains_font_size(self):
|
||||
result = build_ass_style("S1", font_size=36)
|
||||
# Style行格式:Name, Fontname, Fontsize, ...
|
||||
parts = result.split(",")
|
||||
assert parts[2] == "36"
|
||||
|
||||
def test_bold_true(self):
|
||||
result = build_ass_style("S1", bold=True)
|
||||
parts = result.split(",")
|
||||
# Bold 是第7个字段(索引7)
|
||||
assert parts[7] == "-1"
|
||||
|
||||
def test_bold_false(self):
|
||||
result = build_ass_style("S1", bold=False)
|
||||
parts = result.split(",")
|
||||
assert parts[7] == "0"
|
||||
|
||||
def test_italic_true(self):
|
||||
result = build_ass_style("S1", italic=True)
|
||||
parts = result.split(",")
|
||||
# Italic 是第8个字段(索引8)
|
||||
assert parts[8] == "-1"
|
||||
|
||||
def test_italic_false(self):
|
||||
result = build_ass_style("S1", italic=False)
|
||||
parts = result.split(",")
|
||||
assert parts[8] == "0"
|
||||
|
||||
def test_alignment(self):
|
||||
result = build_ass_style("S1", alignment=5)
|
||||
parts = result.split(",")
|
||||
# Alignment 是第18个字段(索引18)
|
||||
assert parts[18] == "5"
|
||||
|
||||
def test_outline_width(self):
|
||||
result = build_ass_style("S1", outline_width=3.0)
|
||||
parts = result.split(",")
|
||||
# Outline 是第16个字段(索引16)
|
||||
assert parts[16] == "3.0"
|
||||
|
||||
def test_margins(self):
|
||||
result = build_ass_style("S1", margin_l=10, margin_r=20, margin_v=30)
|
||||
parts = result.split(",")
|
||||
assert parts[19] == "10" # MarginL
|
||||
assert parts[20] == "20" # MarginR
|
||||
assert parts[21] == "30" # MarginV
|
||||
|
||||
def test_shadow_with_blur(self):
|
||||
result = build_ass_style("S1", shadow_blur=2.0, shadow_offset=(3, 5))
|
||||
parts = result.split(",")
|
||||
# Shadow 深度 = shadow_offset[1] when blur > 0
|
||||
assert parts[17] == "5"
|
||||
|
||||
def test_shadow_without_blur(self):
|
||||
result = build_ass_style("S1", shadow_blur=0.0, shadow_offset=(3, 5))
|
||||
parts = result.split(",")
|
||||
assert parts[17] == "0"
|
||||
|
||||
def test_primary_color(self):
|
||||
result = build_ass_style("S1", primary_color="&H00FFFFFF")
|
||||
parts = result.split(",")
|
||||
assert parts[3] == "&H00FFFFFF"
|
||||
|
||||
def test_outline_color(self):
|
||||
result = build_ass_style("S1", outline_color="&H000000FF")
|
||||
parts = result.split(",")
|
||||
assert parts[5] == "&H000000FF"
|
||||
|
||||
def test_22_fields(self):
|
||||
# ASS Style 行应有23个字段(Style: 前缀 + 22个逗号分隔字段)
|
||||
result = build_ass_style("Default")
|
||||
parts = result.split(",")
|
||||
assert len(parts) >= 22 # 至少22个字段
|
||||
|
||||
|
||||
# ============================================================
|
||||
# escape_ass_text
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestEscapeAssText:
|
||||
def test_plain_text_unchanged(self):
|
||||
assert escape_ass_text("Hello World") == "Hello World"
|
||||
|
||||
def test_newline_converted(self):
|
||||
assert escape_ass_text("line1\nline2") == "line1\\Nline2"
|
||||
|
||||
def test_crlf_converted(self):
|
||||
assert escape_ass_text("line1\r\nline2") == "line1\\Nline2"
|
||||
|
||||
def test_carriage_return_converted(self):
|
||||
assert escape_ass_text("line1\rline2") == "line1\\Nline2"
|
||||
|
||||
def test_curly_braces_escaped(self):
|
||||
assert escape_ass_text("{text}") == "(text)"
|
||||
|
||||
def test_opening_brace_escaped(self):
|
||||
assert escape_ass_text("{hello") == "(hello"
|
||||
|
||||
def test_closing_brace_escaped(self):
|
||||
assert escape_ass_text("hello}") == "hello)"
|
||||
|
||||
def test_multiple_braces(self):
|
||||
assert escape_ass_text("{a}{b}") == "(a)(b)"
|
||||
|
||||
def test_mixed_newlines_and_braces(self):
|
||||
result = escape_ass_text("line1\n{tag}line2")
|
||||
assert result == "line1\\N(tag)line2"
|
||||
|
||||
def test_empty_string(self):
|
||||
assert escape_ass_text("") == ""
|
||||
|
||||
def test_chinese_text(self):
|
||||
assert escape_ass_text("你好世界") == "你好世界"
|
||||
|
||||
def test_backslash_n_in_input(self):
|
||||
# 文本里本身有 \n 字符串(不是换行符)
|
||||
result = escape_ass_text("\\n")
|
||||
assert result == "\\n" # 不变,因为不是实际换行符
|
||||
|
||||
|
||||
# ============================================================
|
||||
# format_ass_time
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestFormatAssTime:
|
||||
def test_zero(self):
|
||||
assert format_ass_time(0) == "0:00:00.00"
|
||||
|
||||
def test_seconds_only(self):
|
||||
assert format_ass_time(5.5) == "0:00:05.50"
|
||||
|
||||
def test_minutes(self):
|
||||
assert format_ass_time(90.0) == "0:01:30.00"
|
||||
|
||||
def test_hours(self):
|
||||
assert format_ass_time(3661.5) == "1:01:01.50"
|
||||
|
||||
def test_multi_hours(self):
|
||||
assert format_ass_time(7384.25) == "2:03:04.25"
|
||||
|
||||
def test_two_decimal_places(self):
|
||||
result = format_ass_time(1.234)
|
||||
# 两位小数
|
||||
assert result.endswith(".23") or result.endswith(".24")
|
||||
|
||||
def test_minutes_two_digits(self):
|
||||
result = format_ass_time(65.0)
|
||||
parts = result.split(":")
|
||||
assert len(parts[1]) == 2
|
||||
assert parts[1] == "01"
|
||||
|
||||
def test_seconds_two_digits_before_decimal(self):
|
||||
result = format_ass_time(5.0)
|
||||
parts = result.split(":")
|
||||
sec_part = parts[2]
|
||||
assert sec_part.startswith("05")
|
||||
|
||||
def test_float_input(self):
|
||||
assert format_ass_time(123.45) == "0:02:03.45"
|
||||
|
||||
def test_exactly_one_hour(self):
|
||||
assert format_ass_time(3600.0) == "1:00:00.00"
|
||||
|
||||
|
||||
# ============================================================
|
||||
# build_ass_content
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestBuildAssContent:
|
||||
def test_no_subtitles_returns_empty(self):
|
||||
result = build_ass_content(video_width=1920, video_height=1080, video_duration=10.0)
|
||||
assert result == ""
|
||||
|
||||
def test_title_only(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
title_text="Test Title",
|
||||
)
|
||||
assert result != ""
|
||||
assert "[Script Info]" in result
|
||||
assert "PlayResX: 1920" in result
|
||||
assert "PlayResY: 1080" in result
|
||||
assert "[V4+ Styles]" in result
|
||||
assert "[Events]" in result
|
||||
assert "TitleStyle" in result
|
||||
assert "Test Title" in result
|
||||
|
||||
def test_subtitle_only(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
subtitle_text="Hello Subtitle",
|
||||
)
|
||||
assert result != ""
|
||||
assert "SubtitleStyle" in result
|
||||
assert "Hello Subtitle" in result
|
||||
|
||||
def test_both_title_and_subtitle(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
title_text="Title",
|
||||
subtitle_text="Subtitle",
|
||||
)
|
||||
assert "TitleStyle" in result
|
||||
assert "SubtitleStyle" in result
|
||||
assert "Title" in result
|
||||
assert "Subtitle" in result
|
||||
|
||||
def test_whitespace_title_returns_empty(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
title_text=" ",
|
||||
)
|
||||
assert result == ""
|
||||
|
||||
def test_whitespace_subtitle_returns_empty(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
subtitle_text=" \n ",
|
||||
)
|
||||
assert result == ""
|
||||
|
||||
def test_title_disabled(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
title_text="Title",
|
||||
title_config={"enabled": False},
|
||||
)
|
||||
assert result == ""
|
||||
|
||||
def test_subtitle_disabled(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
subtitle_text="Sub",
|
||||
subtitle_config={"enabled": False},
|
||||
)
|
||||
assert result == ""
|
||||
|
||||
def test_title_color(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
title_text="T",
|
||||
title_config={"color": "#FF0000"},
|
||||
)
|
||||
# 红色 → &H0000FF
|
||||
assert "&H0000FF" in result
|
||||
|
||||
def test_title_position_top(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
title_text="T",
|
||||
title_config={"position": "top"},
|
||||
)
|
||||
# top alignment = 8
|
||||
assert "TitleStyle" in result
|
||||
|
||||
def test_title_position_bottom(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
title_text="T",
|
||||
title_config={"position": "bottom"},
|
||||
)
|
||||
# bottom=2, 检查Style行里有2
|
||||
assert "TitleStyle" in result
|
||||
|
||||
def test_subtitle_position_bottom(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
subtitle_text="S",
|
||||
subtitle_config={"position": "bottom"},
|
||||
)
|
||||
assert "SubtitleStyle" in result
|
||||
|
||||
def test_title_font_size(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
title_text="T",
|
||||
title_config={"size": 72},
|
||||
)
|
||||
# 在TitleStyle行里查找字体大小
|
||||
for line in result.split("\n"):
|
||||
if line.startswith("Style: TitleStyle"):
|
||||
parts = line.split(",")
|
||||
assert parts[2] == "72"
|
||||
break
|
||||
|
||||
def test_title_bold(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
title_text="T",
|
||||
title_config={"bold": True},
|
||||
)
|
||||
for line in result.split("\n"):
|
||||
if line.startswith("Style: TitleStyle"):
|
||||
parts = line.split(",")
|
||||
assert parts[7] == "-1"
|
||||
break
|
||||
|
||||
def test_title_stroke_enabled(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
title_text="T",
|
||||
title_config={"stroke": {"enabled": True, "width": 3, "color": "#000000"}},
|
||||
)
|
||||
for line in result.split("\n"):
|
||||
if line.startswith("Style: TitleStyle"):
|
||||
parts = line.split(",")
|
||||
assert parts[16] == "3.0"
|
||||
break
|
||||
|
||||
def test_title_stroke_disabled(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
title_text="T",
|
||||
title_config={"stroke": {"enabled": False, "width": 3}},
|
||||
)
|
||||
for line in result.split("\n"):
|
||||
if line.startswith("Style: TitleStyle"):
|
||||
parts = line.split(",")
|
||||
assert parts[16] == "0.0"
|
||||
break
|
||||
|
||||
def test_title_shadow_enabled(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=5.0,
|
||||
title_text="T",
|
||||
title_config={"shadow": {"enabled": True, "blur": 2, "offset_x": 2, "offset_y": 4}},
|
||||
)
|
||||
for line in result.split("\n"):
|
||||
if line.startswith("Style: TitleStyle"):
|
||||
parts = line.split(",")
|
||||
assert parts[17] == "4" # Shadow = offset_y
|
||||
break
|
||||
|
||||
def test_dialogue_has_correct_timing(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=65.5,
|
||||
title_text="T",
|
||||
)
|
||||
# 结束时间应该是 0:01:05.50
|
||||
assert "0:01:05.50" in result
|
||||
|
||||
def test_dialogue_starts_at_zero(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=10.0,
|
||||
subtitle_text="S",
|
||||
)
|
||||
assert "0:00:00.00" in result
|
||||
|
||||
def test_contains_script_info_header(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=10.0,
|
||||
title_text="T",
|
||||
)
|
||||
assert "[Script Info]" in result
|
||||
assert "ScriptType: v4.00+" in result
|
||||
assert "ScaledBorderAndShadow: yes" in result
|
||||
|
||||
def test_escaped_text_in_dialogue(self):
|
||||
result = build_ass_content(
|
||||
video_width=1920,
|
||||
video_height=1080,
|
||||
video_duration=10.0,
|
||||
title_text="line1\nline2",
|
||||
)
|
||||
# 换行符应被转义为 \N
|
||||
assert "\\N" in result
|
||||
assert "line1" in result
|
||||
assert "line2" in result
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 常量验证
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestConstants:
|
||||
def test_margin_values(self):
|
||||
assert TITLE_MARGIN_TOP > 0
|
||||
assert TITLE_MARGIN_BOTTOM > 0
|
||||
assert TITLE_MARGIN_SIDE > 0
|
||||
|
||||
def test_margins_are_integers(self):
|
||||
assert isinstance(TITLE_MARGIN_TOP, int)
|
||||
assert isinstance(TITLE_MARGIN_BOTTOM, int)
|
||||
assert isinstance(TITLE_MARGIN_SIDE, int)
|
||||
@@ -1,530 +0,0 @@
|
||||
"""asset_scoring 单元测试 - wave162
|
||||
|
||||
覆盖:
|
||||
- 分辨率评分 score_resolution
|
||||
- 时长评分 score_duration
|
||||
- 码率评分 score_bitrate
|
||||
- 加权总分 calculate_total_score
|
||||
- 单个素材评分 score_asset_detail
|
||||
- 时长分桶 _bucket_by_duration
|
||||
- 多样性选择 diverse_selection
|
||||
- 候选过滤 filter_candidates
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
import pytest
|
||||
|
||||
from packages.domain.asset_scoring import (
|
||||
AssetScoreDetail,
|
||||
SmartSelectResult,
|
||||
_bucket_by_duration,
|
||||
calculate_total_score,
|
||||
diverse_selection,
|
||||
filter_candidates,
|
||||
score_asset_detail,
|
||||
score_bitrate,
|
||||
score_duration,
|
||||
score_resolution,
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# score_resolution
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestScoreResolution:
|
||||
def test_none_width_returns_mid(self):
|
||||
assert score_resolution(None, 1080) == 0.5
|
||||
|
||||
def test_none_height_returns_mid(self):
|
||||
assert score_resolution(1920, None) == 0.5
|
||||
|
||||
def test_zero_dimension_returns_mid(self):
|
||||
assert score_resolution(0, 1080) == 0.5
|
||||
assert score_resolution(1920, 0) == 0.5
|
||||
assert score_resolution(-1, 1080) == 0.5
|
||||
|
||||
def test_exact_target_returns_1(self):
|
||||
assert score_resolution(1920, 1080) == 1.0
|
||||
|
||||
def test_higher_than_target_returns_1(self):
|
||||
assert score_resolution(3840, 2160) == 1.0 # 4K
|
||||
assert score_resolution(2560, 1440) == 1.0 # 2K
|
||||
|
||||
def test_lower_than_target_linear_decay(self):
|
||||
# 720p = 1280*720 / 1920*1080 = 0.444 ratio
|
||||
# score = 0.3 + 0.7 * 0.444 = 0.611
|
||||
score = score_resolution(1280, 720)
|
||||
assert 0.55 < score < 0.7
|
||||
|
||||
def test_very_low_has_floor(self):
|
||||
# 最低不低于 0.1
|
||||
score = score_resolution(100, 100)
|
||||
assert score >= 0.1
|
||||
|
||||
def test_480p_still_reasonable(self):
|
||||
score = score_resolution(640, 480)
|
||||
assert 0.3 < score < 0.5
|
||||
|
||||
def test_custom_target(self):
|
||||
score = score_resolution(1280, 720, target_width=1280, target_height=720)
|
||||
assert score == 1.0
|
||||
|
||||
def test_between_0_and_1(self):
|
||||
for w, h in [(1920, 1080), (1280, 720), (640, 480), (3840, 2160)]:
|
||||
s = score_resolution(w, h)
|
||||
assert 0.0 <= s <= 1.0
|
||||
|
||||
|
||||
# ============================================================
|
||||
# score_duration
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestScoreDuration:
|
||||
def test_none_returns_mid(self):
|
||||
assert score_duration(None) == 0.5
|
||||
|
||||
def test_zero_or_negative_returns_mid(self):
|
||||
assert score_duration(0) == 0.5
|
||||
assert score_duration(-1) == 0.5
|
||||
|
||||
def test_optimal_range_returns_1(self):
|
||||
assert score_duration(3.0) == 1.0
|
||||
assert score_duration(10.0) == 1.0
|
||||
assert score_duration(30.0) == 1.0
|
||||
assert score_duration(15.0) == 1.0
|
||||
|
||||
def test_short_duration_linear_decay(self):
|
||||
# 1.5s: ratio = 1.5/3 = 0.5, score = 0.3 + 0.7*0.5 = 0.65
|
||||
score = score_duration(1.5)
|
||||
assert score == pytest.approx(0.65)
|
||||
|
||||
def test_very_short_above_floor(self):
|
||||
score = score_duration(0.1)
|
||||
assert 0.3 <= score < 0.5
|
||||
|
||||
def test_long_duration_penalty(self):
|
||||
# 40s: excess=10, penalty=10/10*0.1=0.1, score=0.9
|
||||
score = score_duration(40.0)
|
||||
assert score == pytest.approx(0.9)
|
||||
|
||||
def test_very_long_minimum_floor(self):
|
||||
# 超过很多,最低 0.2
|
||||
score = score_duration(1000.0)
|
||||
assert score >= 0.2
|
||||
assert score < 0.5
|
||||
|
||||
def test_just_below_optimal(self):
|
||||
score = score_duration(2.9)
|
||||
assert 0.9 < score < 1.0
|
||||
|
||||
def test_just_above_optimal(self):
|
||||
score = score_duration(30.1)
|
||||
assert 0.9 < score < 1.0
|
||||
|
||||
|
||||
# ============================================================
|
||||
# score_bitrate
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestScoreBitrate:
|
||||
def test_no_file_size_returns_mid(self):
|
||||
assert score_bitrate(0, 10.0) == 0.5
|
||||
|
||||
def test_no_duration_returns_mid(self):
|
||||
assert score_bitrate(1000000, None) == 0.5
|
||||
assert score_bitrate(1000000, 0) == 0.5
|
||||
assert score_bitrate(1000000, -1) == 0.5
|
||||
|
||||
def test_optimal_range_returns_1(self):
|
||||
# 5 Mbps for 10s = 5*10^6 * 10 / 8 = 6,250,000 bytes
|
||||
size_5mbps_10s = int(5_000_000 * 10 / 8)
|
||||
assert score_bitrate(size_5mbps_10s, 10.0) == 1.0
|
||||
|
||||
def test_low_bitrate_decay(self):
|
||||
# 500 Kbps for 10s
|
||||
size_500kbps = int(500_000 * 10 / 8)
|
||||
score = score_bitrate(size_500kbps, 10.0)
|
||||
assert 0.3 < score < 0.7
|
||||
|
||||
def test_high_bitrate_moderate_penalty(self):
|
||||
# 16 Mbps (2x optimal high), excess=1.0, penalty=min(0.5, 1.0*0.2)=0.2
|
||||
# score = 0.8
|
||||
size_16mbps = int(16_000_000 * 10 / 8)
|
||||
score = score_bitrate(size_16mbps, 10.0)
|
||||
assert 0.7 < score < 0.9
|
||||
|
||||
def test_very_high_bitrate_floor(self):
|
||||
# 极高码率,最低 0.5
|
||||
huge_size = 10**9 # 1GB for 1s = 8Gbps
|
||||
score = score_bitrate(huge_size, 1.0)
|
||||
assert score >= 0.5
|
||||
|
||||
def test_between_0_and_1(self):
|
||||
for size, dur in [(1000, 1), (1000000, 10), (100000000, 5)]:
|
||||
s = score_bitrate(size, dur)
|
||||
assert 0.0 <= s <= 1.0
|
||||
|
||||
|
||||
# ============================================================
|
||||
# calculate_total_score
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestCalculateTotalScore:
|
||||
def test_all_perfect_equals_1(self):
|
||||
assert calculate_total_score(1.0, 1.0, 1.0, 1.0) == 1.0
|
||||
|
||||
def test_all_zero_equals_0(self):
|
||||
assert calculate_total_score(0.0, 0.0, 0.0, 0.0) == 0.0
|
||||
|
||||
def test_weighted_sum(self):
|
||||
# 0.5*0.5 + 0.2*0.5 + 0.2*0.5 + 0.1*0.5 = 0.25+0.1+0.1+0.05 = 0.5
|
||||
assert calculate_total_score(0.5, 0.5, 0.5, 0.5) == pytest.approx(0.5)
|
||||
|
||||
def test_quality_has_highest_weight(self):
|
||||
# 只提高质量分,对比只提高其他
|
||||
q_high = calculate_total_score(1.0, 0.0, 0.0, 0.0)
|
||||
r_high = calculate_total_score(0.0, 1.0, 0.0, 0.0)
|
||||
assert q_high > r_high # 0.5 > 0.2
|
||||
|
||||
def test_bitrate_has_lowest_weight(self):
|
||||
b_high = calculate_total_score(0.0, 0.0, 0.0, 1.0)
|
||||
q_high = calculate_total_score(1.0, 0.0, 0.0, 0.0)
|
||||
assert b_high < q_high # 0.1 < 0.5
|
||||
|
||||
def test_rounded_to_4_decimals(self):
|
||||
result = calculate_total_score(0.3333, 0.3333, 0.3333, 0.3333)
|
||||
assert round(result, 4) == result
|
||||
|
||||
|
||||
# ============================================================
|
||||
# score_asset_detail
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestScoreAssetDetail:
|
||||
def test_returns_detail_object(self):
|
||||
detail = score_asset_detail(
|
||||
asset_id="a1",
|
||||
quality=80.0,
|
||||
width=1920,
|
||||
height=1080,
|
||||
duration=10.0,
|
||||
file_size=5_000_000,
|
||||
)
|
||||
assert isinstance(detail, AssetScoreDetail)
|
||||
assert detail.asset_id == "a1"
|
||||
assert 0.0 <= detail.total_score <= 1.0
|
||||
|
||||
def test_perfect_asset_high_score(self):
|
||||
detail = score_asset_detail(
|
||||
asset_id="perfect",
|
||||
quality=100.0,
|
||||
width=1920,
|
||||
height=1080,
|
||||
duration=10.0,
|
||||
file_size=6_250_000, # 5Mbps for 10s
|
||||
)
|
||||
assert detail.total_score > 0.9
|
||||
|
||||
def test_quality_none_defaults_mid(self):
|
||||
detail = score_asset_detail(
|
||||
asset_id="a1",
|
||||
quality=None,
|
||||
width=1920,
|
||||
height=1080,
|
||||
duration=10.0,
|
||||
file_size=5_000_000,
|
||||
)
|
||||
assert detail.quality_score == 0.5
|
||||
|
||||
def test_quality_normalized(self):
|
||||
detail = score_asset_detail(
|
||||
asset_id="a1",
|
||||
quality=50.0,
|
||||
width=1920,
|
||||
height=1080,
|
||||
duration=10.0,
|
||||
file_size=5_000_000,
|
||||
)
|
||||
assert detail.quality_score == pytest.approx(0.5)
|
||||
|
||||
def test_custom_target_resolution(self):
|
||||
detail = score_asset_detail(
|
||||
asset_id="a1",
|
||||
quality=100.0,
|
||||
width=1280,
|
||||
height=720,
|
||||
duration=10.0,
|
||||
file_size=5_000_000,
|
||||
target_width=1280,
|
||||
target_height=720,
|
||||
)
|
||||
assert detail.resolution_score == 1.0
|
||||
|
||||
def test_total_score_matches_components(self):
|
||||
detail = score_asset_detail(
|
||||
asset_id="a1",
|
||||
quality=80.0,
|
||||
width=1920,
|
||||
height=1080,
|
||||
duration=10.0,
|
||||
file_size=5_000_000,
|
||||
)
|
||||
expected = calculate_total_score(
|
||||
detail.quality_score,
|
||||
detail.resolution_score,
|
||||
detail.duration_score,
|
||||
detail.bitrate_score,
|
||||
)
|
||||
assert detail.total_score == pytest.approx(expected, abs=0.001)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# _bucket_by_duration
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestBucketByDuration:
|
||||
def test_none_is_unknown(self):
|
||||
item = AssetScoreDetail("a", 0.5, 0.5, 0.5, 0.5, 0.5, None)
|
||||
assert _bucket_by_duration(item) == "unknown"
|
||||
|
||||
def test_short(self):
|
||||
item = AssetScoreDetail("a", 0.5, 0.5, 0.5, 0.5, 0.5, 3.0)
|
||||
assert _bucket_by_duration(item) == "short"
|
||||
|
||||
def test_short_boundary(self):
|
||||
item = AssetScoreDetail("a", 0.5, 0.5, 0.5, 0.5, 0.5, 4.9)
|
||||
assert _bucket_by_duration(item) == "short"
|
||||
|
||||
def test_medium(self):
|
||||
item = AssetScoreDetail("a", 0.5, 0.5, 0.5, 0.5, 0.5, 10.0)
|
||||
assert _bucket_by_duration(item) == "medium"
|
||||
|
||||
def test_medium_boundary(self):
|
||||
item = AssetScoreDetail("a", 0.5, 0.5, 0.5, 0.5, 0.5, 5.0)
|
||||
assert _bucket_by_duration(item) == "medium"
|
||||
|
||||
def test_medium_upper_boundary(self):
|
||||
item = AssetScoreDetail("a", 0.5, 0.5, 0.5, 0.5, 0.5, 14.9)
|
||||
assert _bucket_by_duration(item) == "medium"
|
||||
|
||||
def test_long(self):
|
||||
item = AssetScoreDetail("a", 0.5, 0.5, 0.5, 0.5, 0.5, 20.0)
|
||||
assert _bucket_by_duration(item) == "long"
|
||||
|
||||
def test_long_boundary(self):
|
||||
item = AssetScoreDetail("a", 0.5, 0.5, 0.5, 0.5, 0.5, 15.0)
|
||||
assert _bucket_by_duration(item) == "long"
|
||||
|
||||
|
||||
# ============================================================
|
||||
# diverse_selection
|
||||
# ============================================================
|
||||
|
||||
|
||||
def _make_detail(asset_id: str, score: float, duration: float) -> AssetScoreDetail:
|
||||
return AssetScoreDetail(
|
||||
asset_id=asset_id,
|
||||
total_score=score,
|
||||
quality_score=score,
|
||||
resolution_score=score,
|
||||
duration_score=score,
|
||||
bitrate_score=score,
|
||||
duration=duration,
|
||||
)
|
||||
|
||||
|
||||
class TestDiverseSelection:
|
||||
def test_empty_input_returns_empty(self):
|
||||
assert diverse_selection([], 5) == []
|
||||
|
||||
def test_zero_count_returns_empty(self):
|
||||
items = [_make_detail("a1", 0.9, 10.0)]
|
||||
assert diverse_selection(items, 0) == []
|
||||
|
||||
def test_negative_count_returns_empty(self):
|
||||
items = [_make_detail("a1", 0.9, 10.0)]
|
||||
assert diverse_selection(items, -1) == []
|
||||
|
||||
def test_fewer_items_than_count(self):
|
||||
items = [_make_detail("a1", 0.9, 10.0), _make_detail("a2", 0.8, 3.0)]
|
||||
result = diverse_selection(items, 10)
|
||||
assert len(result) == 2
|
||||
|
||||
def test_picks_top_from_each_bucket(self):
|
||||
# 3个桶各有3个素材,选3个
|
||||
items = [
|
||||
_make_detail("s1", 0.95, 2.0),
|
||||
_make_detail("m1", 0.9, 10.0),
|
||||
_make_detail("l1", 0.85, 20.0),
|
||||
_make_detail("s2", 0.8, 3.0),
|
||||
_make_detail("m2", 0.75, 8.0),
|
||||
_make_detail("l2", 0.7, 25.0),
|
||||
]
|
||||
result = diverse_selection(items, 3)
|
||||
assert len(result) == 3
|
||||
ids = [d.asset_id for d in result]
|
||||
assert "s1" in ids
|
||||
assert "m1" in ids
|
||||
assert "l1" in ids
|
||||
|
||||
def test_base_quota_when_count_large(self):
|
||||
# count=6, base_quota=max(1, 6//3)=2
|
||||
items = [
|
||||
_make_detail("s1", 1.0, 2.0),
|
||||
_make_detail("s2", 0.9, 3.0),
|
||||
_make_detail("s3", 0.8, 4.0),
|
||||
_make_detail("m1", 0.95, 10.0),
|
||||
_make_detail("m2", 0.85, 12.0),
|
||||
_make_detail("l1", 0.92, 20.0),
|
||||
_make_detail("l2", 0.82, 30.0),
|
||||
]
|
||||
result = diverse_selection(items, 6)
|
||||
assert len(result) == 6
|
||||
ids = [d.asset_id for d in result]
|
||||
# 每桶至少2个
|
||||
short_count = sum(1 for d in result if d.duration and d.duration < 5)
|
||||
assert short_count >= 2
|
||||
|
||||
def test_remaining_filled_by_global_score(self):
|
||||
# 只有2个桶有内容,count=5,配额用完后剩余从全局取
|
||||
items = [
|
||||
_make_detail("s1", 1.0, 2.0),
|
||||
_make_detail("s2", 0.9, 3.0),
|
||||
_make_detail("m1", 0.95, 10.0),
|
||||
_make_detail("m2", 0.8, 12.0),
|
||||
_make_detail("s3", 0.7, 4.0),
|
||||
_make_detail("s4", 0.6, 1.0),
|
||||
_make_detail("m3", 0.5, 8.0),
|
||||
]
|
||||
result = diverse_selection(items, 5)
|
||||
assert len(result) == 5
|
||||
# 最高分的都应该在
|
||||
ids = [d.asset_id for d in result]
|
||||
assert "s1" in ids
|
||||
assert "m1" in ids
|
||||
|
||||
def test_single_bucket(self):
|
||||
items = [
|
||||
_make_detail("s1", 1.0, 2.0),
|
||||
_make_detail("s2", 0.9, 3.0),
|
||||
_make_detail("s3", 0.8, 4.0),
|
||||
]
|
||||
result = diverse_selection(items, 2)
|
||||
assert len(result) == 2
|
||||
assert result[0].asset_id == "s1"
|
||||
assert result[1].asset_id == "s2"
|
||||
|
||||
def test_unknown_duration_fallback(self):
|
||||
# 已知素材不够时用未知时长的补充
|
||||
items = [
|
||||
_make_detail("s1", 1.0, 2.0),
|
||||
_make_detail("u1", 0.95, None),
|
||||
_make_detail("u2", 0.9, None),
|
||||
]
|
||||
result = diverse_selection(items, 3)
|
||||
assert len(result) == 3
|
||||
ids = [d.asset_id for d in result]
|
||||
assert "s1" in ids
|
||||
assert "u1" in ids
|
||||
|
||||
def test_no_duplicates(self):
|
||||
items = [
|
||||
_make_detail("s1", 1.0, 2.0),
|
||||
_make_detail("m1", 0.9, 10.0),
|
||||
]
|
||||
result = diverse_selection(items, 5)
|
||||
ids = [d.asset_id for d in result]
|
||||
assert len(ids) == len(set(ids))
|
||||
|
||||
|
||||
# ============================================================
|
||||
# filter_candidates
|
||||
# ============================================================
|
||||
|
||||
|
||||
@dataclass
|
||||
class FakeAsset:
|
||||
status: str = "ready"
|
||||
mime_type: str = "video/mp4"
|
||||
quality_score: float | None = 50.0
|
||||
|
||||
|
||||
class TestFilterCandidates:
|
||||
def test_ready_video_passes(self):
|
||||
assets = [FakeAsset()]
|
||||
candidates, filtered = filter_candidates(assets)
|
||||
assert len(candidates) == 1
|
||||
assert filtered == 0
|
||||
|
||||
def test_non_ready_filtered(self):
|
||||
assets = [FakeAsset(status="uploading"), FakeAsset(status="processing")]
|
||||
candidates, filtered = filter_candidates(assets)
|
||||
assert len(candidates) == 0
|
||||
assert filtered == 0 # 被状态过滤的不计入质量门槛
|
||||
|
||||
def test_non_video_filtered(self):
|
||||
assets = [FakeAsset(mime_type="image/jpeg"), FakeAsset(mime_type="audio/mp3")]
|
||||
candidates, filtered = filter_candidates(assets)
|
||||
assert len(candidates) == 0
|
||||
|
||||
def test_low_quality_filtered(self):
|
||||
assets = [FakeAsset(quality_score=10.0), FakeAsset(quality_score=80.0)]
|
||||
candidates, filtered = filter_candidates(assets, min_quality_score=30.0)
|
||||
assert len(candidates) == 1
|
||||
assert filtered == 1
|
||||
|
||||
def test_quality_none_passes(self):
|
||||
assets = [FakeAsset(quality_score=None)]
|
||||
candidates, filtered = filter_candidates(assets)
|
||||
assert len(candidates) == 1
|
||||
assert filtered == 0
|
||||
|
||||
def test_exactly_min_quality_passes(self):
|
||||
assets = [FakeAsset(quality_score=30.0)]
|
||||
candidates, filtered = filter_candidates(assets, min_quality_score=30.0)
|
||||
assert len(candidates) == 1
|
||||
|
||||
def test_custom_min_quality(self):
|
||||
assets = [
|
||||
FakeAsset(quality_score=40.0),
|
||||
FakeAsset(quality_score=60.0),
|
||||
FakeAsset(quality_score=80.0),
|
||||
]
|
||||
candidates, filtered = filter_candidates(assets, min_quality_score=50.0)
|
||||
assert len(candidates) == 2
|
||||
assert filtered == 1
|
||||
|
||||
def test_empty_input(self):
|
||||
candidates, filtered = filter_candidates([])
|
||||
assert candidates == []
|
||||
assert filtered == 0
|
||||
|
||||
def test_mime_type_none(self):
|
||||
# None 的 mime_type 也应该被过滤掉(不是video开头)
|
||||
asset = FakeAsset(mime_type="")
|
||||
candidates, _ = filter_candidates([asset])
|
||||
assert len(candidates) == 0
|
||||
|
||||
def test_with_enum_status(self):
|
||||
from enum import Enum
|
||||
|
||||
class StatusEnum(Enum):
|
||||
READY = "ready"
|
||||
UPLOADING = "uploading"
|
||||
|
||||
@dataclass
|
||||
class EnumAsset:
|
||||
status: StatusEnum = StatusEnum.READY
|
||||
mime_type: str = "video/mp4"
|
||||
quality_score: float = 50.0
|
||||
|
||||
assets = [EnumAsset()]
|
||||
candidates, filtered = filter_candidates(assets)
|
||||
assert len(candidates) == 1
|
||||
@@ -1,470 +0,0 @@
|
||||
"""ChromaKeyConfig 绿幕抠像配置单测.
|
||||
|
||||
纯逻辑模块,覆盖:数据类、from_dict解析、from_preset预设、has_effect、
|
||||
validate校验、normalize_color颜色归一化、colorkey滤镜构建、
|
||||
chromakey滤镜构建、apply_chroma_key_if_needed便捷函数、get_preset_names。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.chroma_key_config import (
|
||||
CHROMA_KEY_PRESETS,
|
||||
MAX_BLEND,
|
||||
MAX_SIMILARITY,
|
||||
MAX_SPILL_SUPPRESS,
|
||||
MIN_BLEND,
|
||||
MIN_SIMILARITY,
|
||||
MIN_SPILL_SUPPRESS,
|
||||
VALID_PRESETS,
|
||||
ChromaKeyConfig,
|
||||
apply_chroma_key_if_needed,
|
||||
build_chromakey_filter,
|
||||
build_colorkey_filter,
|
||||
get_preset_names,
|
||||
normalize_color,
|
||||
)
|
||||
|
||||
|
||||
class TestChromaKeyConfigDefaults:
|
||||
def test_default_disabled(self):
|
||||
config = ChromaKeyConfig()
|
||||
assert config.enabled is False
|
||||
assert config.key_color == "#00FF00"
|
||||
assert config.similarity == 0.3
|
||||
assert config.blend == 0.1
|
||||
assert config.spill_suppress == 0.0
|
||||
|
||||
def test_default_has_no_effect(self):
|
||||
config = ChromaKeyConfig()
|
||||
assert config.has_effect() is False
|
||||
|
||||
def test_default_validate_passes(self):
|
||||
config = ChromaKeyConfig()
|
||||
ok, msg = config.validate()
|
||||
assert ok is True
|
||||
assert msg == ""
|
||||
|
||||
|
||||
class TestFromDict:
|
||||
def test_none_returns_disabled(self):
|
||||
config = ChromaKeyConfig.from_dict(None)
|
||||
assert config.enabled is False
|
||||
|
||||
def test_empty_dict_returns_disabled(self):
|
||||
config = ChromaKeyConfig.from_dict({})
|
||||
assert config.enabled is False
|
||||
|
||||
def test_enabled_false_returns_disabled(self):
|
||||
config = ChromaKeyConfig.from_dict({"enabled": False})
|
||||
assert config.enabled is False
|
||||
|
||||
def test_basic_enabled(self):
|
||||
config = ChromaKeyConfig.from_dict({"enabled": True, "key_color": "#FF0000"})
|
||||
assert config.enabled is True
|
||||
assert config.key_color == "#FF0000"
|
||||
|
||||
def test_default_values_when_enabled(self):
|
||||
config = ChromaKeyConfig.from_dict({"enabled": True})
|
||||
assert config.enabled is True
|
||||
assert config.key_color == "#00FF00"
|
||||
assert config.similarity == 0.3
|
||||
assert config.blend == 0.1
|
||||
assert config.spill_suppress == 0.0
|
||||
|
||||
def test_custom_values(self):
|
||||
config = ChromaKeyConfig.from_dict(
|
||||
{
|
||||
"enabled": True,
|
||||
"key_color": "#0000FF",
|
||||
"similarity": 0.5,
|
||||
"blend": 0.2,
|
||||
"spill_suppress": 0.3,
|
||||
}
|
||||
)
|
||||
assert config.key_color == "#0000FF"
|
||||
assert config.similarity == 0.5
|
||||
assert config.blend == 0.2
|
||||
assert config.spill_suppress == 0.3
|
||||
|
||||
def test_similarity_clamped_below_min(self):
|
||||
config = ChromaKeyConfig.from_dict({"enabled": True, "similarity": 0.0})
|
||||
assert config.similarity == MIN_SIMILARITY
|
||||
|
||||
def test_similarity_clamped_above_max(self):
|
||||
config = ChromaKeyConfig.from_dict({"enabled": True, "similarity": 2.0})
|
||||
assert config.similarity == MAX_SIMILARITY
|
||||
|
||||
def test_blend_clamped_below_min(self):
|
||||
config = ChromaKeyConfig.from_dict({"enabled": True, "blend": -1.0})
|
||||
assert config.blend == MIN_BLEND
|
||||
|
||||
def test_blend_clamped_above_max(self):
|
||||
config = ChromaKeyConfig.from_dict({"enabled": True, "blend": 2.0})
|
||||
assert config.blend == MAX_BLEND
|
||||
|
||||
def test_spill_suppress_clamped_below_min(self):
|
||||
config = ChromaKeyConfig.from_dict({"enabled": True, "spill_suppress": -0.5})
|
||||
assert config.spill_suppress == MIN_SPILL_SUPPRESS
|
||||
|
||||
def test_spill_suppress_clamped_above_max(self):
|
||||
config = ChromaKeyConfig.from_dict({"enabled": True, "spill_suppress": 2.0})
|
||||
assert config.spill_suppress == MAX_SPILL_SUPPRESS
|
||||
|
||||
def test_invalid_float_values_use_default(self):
|
||||
config = ChromaKeyConfig.from_dict(
|
||||
{
|
||||
"enabled": True,
|
||||
"similarity": "not_a_number",
|
||||
"blend": None,
|
||||
}
|
||||
)
|
||||
assert config.similarity == 0.3
|
||||
assert config.blend == 0.1
|
||||
|
||||
def test_key_color_stripped(self):
|
||||
config = ChromaKeyConfig.from_dict({"enabled": True, "key_color": " #FF0000 "})
|
||||
assert config.key_color == "#FF0000"
|
||||
|
||||
|
||||
class TestFromPreset:
|
||||
def test_green_screen_preset(self):
|
||||
config = ChromaKeyConfig.from_preset("green_screen")
|
||||
assert config is not None
|
||||
assert config.enabled is True
|
||||
assert config.key_color == "#00FF00"
|
||||
assert config.similarity == 0.3
|
||||
assert config.blend == 0.1
|
||||
assert config.spill_suppress == 0.5
|
||||
|
||||
def test_blue_screen_preset(self):
|
||||
config = ChromaKeyConfig.from_preset("blue_screen")
|
||||
assert config is not None
|
||||
assert config.key_color == "#0000FF"
|
||||
|
||||
def test_red_screen_preset(self):
|
||||
config = ChromaKeyConfig.from_preset("red_screen")
|
||||
assert config is not None
|
||||
assert config.key_color == "#FF0000"
|
||||
assert config.spill_suppress == 0.0
|
||||
|
||||
def test_invalid_preset_returns_none(self):
|
||||
config = ChromaKeyConfig.from_preset("nonexistent")
|
||||
assert config is None
|
||||
|
||||
def test_all_presets_are_valid(self):
|
||||
for name in CHROMA_KEY_PRESETS:
|
||||
config = ChromaKeyConfig.from_preset(name)
|
||||
assert config is not None
|
||||
assert config.enabled is True
|
||||
assert config.has_effect() is True
|
||||
ok, _ = config.validate()
|
||||
assert ok is True
|
||||
|
||||
|
||||
class TestHasEffect:
|
||||
def test_disabled_no_effect(self):
|
||||
config = ChromaKeyConfig(enabled=False)
|
||||
assert config.has_effect() is False
|
||||
|
||||
def test_enabled_but_zero_similarity(self):
|
||||
config = ChromaKeyConfig(enabled=True, similarity=0.0)
|
||||
assert config.has_effect() is False
|
||||
|
||||
def test_enabled_with_similarity_has_effect(self):
|
||||
config = ChromaKeyConfig(enabled=True, similarity=0.01)
|
||||
assert config.has_effect() is True
|
||||
|
||||
def test_full_config_has_effect(self):
|
||||
config = ChromaKeyConfig(
|
||||
enabled=True,
|
||||
key_color="#00FF00",
|
||||
similarity=0.3,
|
||||
blend=0.1,
|
||||
spill_suppress=0.5,
|
||||
)
|
||||
assert config.has_effect() is True
|
||||
|
||||
|
||||
class TestValidate:
|
||||
def test_disabled_valid(self):
|
||||
config = ChromaKeyConfig(enabled=False)
|
||||
ok, msg = config.validate()
|
||||
assert ok is True
|
||||
assert msg == ""
|
||||
|
||||
def test_valid_config(self):
|
||||
config = ChromaKeyConfig(
|
||||
enabled=True,
|
||||
key_color="#00FF00",
|
||||
similarity=0.3,
|
||||
blend=0.1,
|
||||
spill_suppress=0.5,
|
||||
)
|
||||
ok, msg = config.validate()
|
||||
assert ok is True
|
||||
assert msg == ""
|
||||
|
||||
def test_empty_key_color_invalid(self):
|
||||
config = ChromaKeyConfig(enabled=True, key_color="")
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "key_color" in msg
|
||||
|
||||
def test_similarity_below_min_invalid(self):
|
||||
config = ChromaKeyConfig(enabled=True, similarity=0.001)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "similarity" in msg
|
||||
|
||||
def test_similarity_above_max_invalid(self):
|
||||
config = ChromaKeyConfig(enabled=True, similarity=1.5)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "similarity" in msg
|
||||
|
||||
def test_blend_below_min_invalid(self):
|
||||
config = ChromaKeyConfig(enabled=True, blend=-0.1)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "blend" in msg
|
||||
|
||||
def test_blend_above_max_invalid(self):
|
||||
config = ChromaKeyConfig(enabled=True, blend=1.5)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "blend" in msg
|
||||
|
||||
def test_spill_suppress_below_min_invalid(self):
|
||||
config = ChromaKeyConfig(enabled=True, spill_suppress=-0.1)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "spill_suppress" in msg
|
||||
|
||||
def test_spill_suppress_above_max_invalid(self):
|
||||
config = ChromaKeyConfig(enabled=True, spill_suppress=1.5)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "spill_suppress" in msg
|
||||
|
||||
def test_boundary_values_valid(self):
|
||||
config = ChromaKeyConfig(
|
||||
enabled=True,
|
||||
key_color="#000",
|
||||
similarity=MIN_SIMILARITY,
|
||||
blend=MIN_BLEND,
|
||||
spill_suppress=MIN_SPILL_SUPPRESS,
|
||||
)
|
||||
ok, _ = config.validate()
|
||||
assert ok is True
|
||||
|
||||
config2 = ChromaKeyConfig(
|
||||
enabled=True,
|
||||
key_color="#fff",
|
||||
similarity=MAX_SIMILARITY,
|
||||
blend=MAX_BLEND,
|
||||
spill_suppress=MAX_SPILL_SUPPRESS,
|
||||
)
|
||||
ok2, _ = config2.validate()
|
||||
assert ok2 is True
|
||||
|
||||
|
||||
class TestNormalizeColor:
|
||||
def test_hex_with_hash(self):
|
||||
assert normalize_color("#FF0000") == "0xFF0000"
|
||||
|
||||
def test_hex_without_hash(self):
|
||||
assert normalize_color("FF0000") == "0xFF0000"
|
||||
|
||||
def test_hex_lowercase(self):
|
||||
assert normalize_color("#ff0000") == "0xFF0000"
|
||||
|
||||
def test_hex_mixed_case(self):
|
||||
assert normalize_color("#aBcDeF") == "0xABCDEF"
|
||||
|
||||
def test_hex_with_alpha(self):
|
||||
assert normalize_color("#FF000080") == "0xFF0000"
|
||||
|
||||
def test_0x_format_passthrough(self):
|
||||
assert normalize_color("0xFF0000") == "0XFF0000"
|
||||
|
||||
def test_0x_lowercase(self):
|
||||
assert normalize_color("0xff0000") == "0XFF0000"
|
||||
|
||||
def test_color_name_passthrough(self):
|
||||
assert normalize_color("green") == "green"
|
||||
|
||||
def test_color_name_blue(self):
|
||||
assert normalize_color("blue") == "blue"
|
||||
|
||||
def test_whitespace_stripped(self):
|
||||
assert normalize_color(" #FF0000 ") == "0xFF0000"
|
||||
|
||||
def test_black_color(self):
|
||||
assert normalize_color("#000000") == "0x000000"
|
||||
|
||||
def test_white_color(self):
|
||||
assert normalize_color("#FFFFFF") == "0xFFFFFF"
|
||||
|
||||
|
||||
class TestBuildColorkeyFilter:
|
||||
def test_disabled_returns_copy(self):
|
||||
config = ChromaKeyConfig(enabled=False)
|
||||
result = build_colorkey_filter(config, "[0:v]", "[out]")
|
||||
assert result == "[0:v]copy[out]"
|
||||
|
||||
def test_zero_similarity_returns_copy(self):
|
||||
config = ChromaKeyConfig(enabled=True, similarity=0.0)
|
||||
result = build_colorkey_filter(config, "[v0]", "[ck]")
|
||||
assert result == "[v0]copy[ck]"
|
||||
|
||||
def test_basic_colorkey(self):
|
||||
config = ChromaKeyConfig(enabled=True, key_color="#00FF00", similarity=0.3, blend=0.1)
|
||||
result = build_colorkey_filter(config, "[0:v]", "[ck]")
|
||||
assert "colorkey=color=0x00FF00:similarity=0.3:blend=0.1" in result
|
||||
assert result.startswith("[0:v]")
|
||||
assert result.endswith("[ck]")
|
||||
|
||||
def test_with_spill_suppress(self):
|
||||
config = ChromaKeyConfig(
|
||||
enabled=True,
|
||||
key_color="#00FF00",
|
||||
similarity=0.3,
|
||||
blend=0.1,
|
||||
spill_suppress=0.5,
|
||||
)
|
||||
result = build_colorkey_filter(config, "[0:v]", "[ck]")
|
||||
assert "colorkey=" in result
|
||||
assert "colorchannelmixer=" in result
|
||||
# Spill suppress reduces green gain
|
||||
assert "gg=" in result
|
||||
assert "rr=" in result
|
||||
assert "bb=" in result
|
||||
|
||||
def test_no_spill_suppress_no_colorchannelmixer(self):
|
||||
config = ChromaKeyConfig(
|
||||
enabled=True,
|
||||
key_color="#00FF00",
|
||||
similarity=0.3,
|
||||
blend=0.1,
|
||||
spill_suppress=0.0,
|
||||
)
|
||||
result = build_colorkey_filter(config, "[0:v]", "[ck]")
|
||||
assert "colorchannelmixer" not in result
|
||||
|
||||
def test_different_labels(self):
|
||||
config = ChromaKeyConfig(enabled=True, key_color="#FF0000", similarity=0.5)
|
||||
result = build_colorkey_filter(config, "[v_in]", "[v_out]")
|
||||
assert result.startswith("[v_in]")
|
||||
assert result.endswith("[v_out]")
|
||||
|
||||
def test_spill_suppress_gain_values(self):
|
||||
config = ChromaKeyConfig(
|
||||
enabled=True,
|
||||
key_color="#00FF00",
|
||||
similarity=0.3,
|
||||
blend=0.1,
|
||||
spill_suppress=1.0,
|
||||
)
|
||||
result = build_colorkey_filter(config, "[0:v]", "[ck]")
|
||||
# At spill=1.0: g_gain = max(0.3, 1.0 - 1.0*0.7) = max(0.3, 0.3) = 0.3
|
||||
assert "gg=0.3" in result
|
||||
|
||||
|
||||
class TestBuildChromakeyFilter:
|
||||
def test_disabled_returns_copy(self):
|
||||
config = ChromaKeyConfig(enabled=False)
|
||||
result = build_chromakey_filter(config, "[0:v]", "[out]")
|
||||
assert result == "[0:v]copy[out]"
|
||||
|
||||
def test_basic_chromakey(self):
|
||||
config = ChromaKeyConfig(enabled=True, key_color="#0000FF", similarity=0.4, blend=0.2)
|
||||
result = build_chromakey_filter(config, "[0:v]", "[ck]")
|
||||
assert "chromakey=color=0x0000FF:similarity=0.4:blend=0.2" in result
|
||||
assert result.startswith("[0:v]")
|
||||
assert result.endswith("[ck]")
|
||||
|
||||
def test_spill_suppress_not_included(self):
|
||||
"""chromakey滤镜不包含spill_suppress(只有colorkey有)."""
|
||||
config = ChromaKeyConfig(
|
||||
enabled=True,
|
||||
key_color="#00FF00",
|
||||
similarity=0.3,
|
||||
blend=0.1,
|
||||
spill_suppress=0.5,
|
||||
)
|
||||
result = build_chromakey_filter(config, "[0:v]", "[ck]")
|
||||
assert "colorchannelmixer" not in result
|
||||
assert "chromakey=" in result
|
||||
|
||||
|
||||
class TestApplyChromaKeyIfNeeded:
|
||||
def test_none_clip_config_returns_none(self):
|
||||
result = apply_chroma_key_if_needed(None, "[0:v]", "[ck]")
|
||||
assert result is None
|
||||
|
||||
def test_no_chroma_key_returns_none(self):
|
||||
result = apply_chroma_key_if_needed({"other": "data"}, "[0:v]", "[ck]")
|
||||
assert result is None
|
||||
|
||||
def test_chroma_key_disabled_returns_none(self):
|
||||
config = {"chroma_key": {"enabled": False}}
|
||||
result = apply_chroma_key_if_needed(config, "[0:v]", "[ck]")
|
||||
assert result is None
|
||||
|
||||
def test_chroma_key_enabled_returns_filter(self):
|
||||
config = {
|
||||
"chroma_key": {
|
||||
"enabled": True,
|
||||
"key_color": "#00FF00",
|
||||
"similarity": 0.3,
|
||||
"blend": 0.1,
|
||||
}
|
||||
}
|
||||
result = apply_chroma_key_if_needed(config, "[0:v]", "[ck]")
|
||||
assert result is not None
|
||||
assert "colorkey=" in result
|
||||
assert result.startswith("[0:v]")
|
||||
assert result.endswith("[ck]")
|
||||
|
||||
def test_invalid_config_returns_none(self):
|
||||
"""配置异常时应该返回None而不是抛异常."""
|
||||
config = {"chroma_key": "invalid_data"}
|
||||
result = apply_chroma_key_if_needed(config, "[0:v]", "[ck]")
|
||||
assert result is None
|
||||
|
||||
|
||||
class TestGetPresetNames:
|
||||
def test_returns_sorted_list(self):
|
||||
names = get_preset_names()
|
||||
assert isinstance(names, list)
|
||||
assert names == sorted(names)
|
||||
|
||||
def test_contains_known_presets(self):
|
||||
names = get_preset_names()
|
||||
assert "green_screen" in names
|
||||
assert "blue_screen" in names
|
||||
assert "red_screen" in names
|
||||
|
||||
def test_count_matches_presets_dict(self):
|
||||
names = get_preset_names()
|
||||
assert len(names) == len(CHROMA_KEY_PRESETS)
|
||||
|
||||
|
||||
class TestPresetsAndConstants:
|
||||
def test_valid_presets_equals_preset_keys(self):
|
||||
assert VALID_PRESETS == set(CHROMA_KEY_PRESETS.keys())
|
||||
|
||||
def test_each_preset_has_required_keys(self):
|
||||
for name, preset in CHROMA_KEY_PRESETS.items():
|
||||
assert "key_color" in preset, f"{name} missing key_color"
|
||||
assert "similarity" in preset, f"{name} missing similarity"
|
||||
assert "blend" in preset, f"{name} missing blend"
|
||||
assert "spill_suppress" in preset, f"{name} missing spill_suppress"
|
||||
|
||||
def test_min_less_than_max(self):
|
||||
assert MIN_SIMILARITY < MAX_SIMILARITY
|
||||
assert MIN_BLEND <= MAX_BLEND
|
||||
assert MIN_SPILL_SUPPRESS <= MAX_SPILL_SUPPRESS
|
||||
|
||||
def test_min_similarity_positive(self):
|
||||
assert MIN_SIMILARITY > 0
|
||||
@@ -1,184 +0,0 @@
|
||||
"""classification 单测.
|
||||
|
||||
domain 层素材分类模块纯逻辑,0 外部依赖。
|
||||
覆盖:4个枚举 + ClassificationJob 工厂/校验。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.classification import (
|
||||
AssetClassification,
|
||||
AssetLibraryKind,
|
||||
ClassificationJob,
|
||||
ClassificationJobStatus,
|
||||
IngestJobStatus,
|
||||
)
|
||||
|
||||
|
||||
class TestAssetLibraryKind:
|
||||
"""AssetLibraryKind 枚举测试."""
|
||||
|
||||
def test_three_values(self):
|
||||
"""视频/配音/图片三类."""
|
||||
assert len(AssetLibraryKind) == 3
|
||||
|
||||
def test_video(self):
|
||||
assert AssetLibraryKind.VIDEO == "video"
|
||||
|
||||
def test_voice(self):
|
||||
assert AssetLibraryKind.VOICE == "voice"
|
||||
|
||||
def test_image(self):
|
||||
assert AssetLibraryKind.IMAGE == "image"
|
||||
|
||||
def test_str_compatible(self):
|
||||
"""StrEnum 字符串兼容."""
|
||||
assert AssetLibraryKind.VIDEO == "video"
|
||||
|
||||
|
||||
class TestIngestJobStatus:
|
||||
"""IngestJobStatus 枚举测试."""
|
||||
|
||||
def test_four_statuses(self):
|
||||
assert len(IngestJobStatus) == 4
|
||||
|
||||
def test_pending(self):
|
||||
assert IngestJobStatus.PENDING == "pending"
|
||||
|
||||
def test_processing(self):
|
||||
assert IngestJobStatus.PROCESSING == "processing"
|
||||
|
||||
def test_completed(self):
|
||||
assert IngestJobStatus.COMPLETED == "completed"
|
||||
|
||||
def test_failed(self):
|
||||
assert IngestJobStatus.FAILED == "failed"
|
||||
|
||||
|
||||
class TestClassificationJobStatus:
|
||||
"""ClassificationJobStatus 枚举测试."""
|
||||
|
||||
def test_four_statuses(self):
|
||||
assert len(ClassificationJobStatus) == 4
|
||||
|
||||
def test_pending(self):
|
||||
assert ClassificationJobStatus.PENDING == "pending"
|
||||
|
||||
def test_processing(self):
|
||||
assert ClassificationJobStatus.PROCESSING == "processing"
|
||||
|
||||
def test_completed(self):
|
||||
assert ClassificationJobStatus.COMPLETED == "completed"
|
||||
|
||||
def test_failed(self):
|
||||
assert ClassificationJobStatus.FAILED == "failed"
|
||||
|
||||
def test_same_values_as_ingest(self):
|
||||
"""两种任务状态值相同."""
|
||||
assert set(ClassificationJobStatus) == set(IngestJobStatus)
|
||||
|
||||
|
||||
class TestAssetClassification:
|
||||
"""AssetClassification 枚举测试."""
|
||||
|
||||
def test_nine_categories(self):
|
||||
"""9个分类."""
|
||||
assert len(AssetClassification) == 9
|
||||
|
||||
def test_scenic(self):
|
||||
assert AssetClassification.SCENIC == "scenic"
|
||||
|
||||
def test_product(self):
|
||||
assert AssetClassification.PRODUCT == "product"
|
||||
|
||||
def test_person(self):
|
||||
assert AssetClassification.PERSON == "person"
|
||||
|
||||
def test_animal(self):
|
||||
assert AssetClassification.ANIMAL == "animal"
|
||||
|
||||
def test_food(self):
|
||||
assert AssetClassification.FOOD == "food"
|
||||
|
||||
def test_tech(self):
|
||||
assert AssetClassification.TECH == "tech"
|
||||
|
||||
def test_sport(self):
|
||||
assert AssetClassification.SPORT == "sport"
|
||||
|
||||
def test_music(self):
|
||||
assert AssetClassification.MUSIC == "music"
|
||||
|
||||
def test_other(self):
|
||||
assert AssetClassification.OTHER == "other"
|
||||
|
||||
def test_all_values_unique(self):
|
||||
"""所有分类值唯一."""
|
||||
values = [c.value for c in AssetClassification]
|
||||
assert len(values) == len(set(values))
|
||||
|
||||
|
||||
class TestClassificationJobCreate:
|
||||
"""ClassificationJob.create 测试."""
|
||||
|
||||
def test_create_valid(self):
|
||||
"""正常创建."""
|
||||
job = ClassificationJob.create(project_id="proj1", asset_id="asset1")
|
||||
assert job.project_id == "proj1"
|
||||
assert job.asset_id == "asset1"
|
||||
assert job.status == ClassificationJobStatus.PENDING
|
||||
assert job.classification == ""
|
||||
assert job.confidence == 0.0
|
||||
assert job.error_message == ""
|
||||
assert isinstance(job.id, str)
|
||||
assert len(job.id) > 0
|
||||
|
||||
def test_create_strips(self):
|
||||
"""project_id 和 asset_id 会 strip."""
|
||||
job = ClassificationJob.create(project_id=" proj1 ", asset_id=" asset1 ")
|
||||
assert job.project_id == "proj1"
|
||||
assert job.asset_id == "asset1"
|
||||
|
||||
def test_create_empty_project_id(self):
|
||||
"""空 project_id 无效."""
|
||||
try:
|
||||
ClassificationJob.create(project_id="", asset_id="a1")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "project_id" in str(e)
|
||||
|
||||
def test_create_whitespace_project_id(self):
|
||||
"""纯空白 project_id 无效."""
|
||||
try:
|
||||
ClassificationJob.create(project_id=" ", asset_id="a1")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "project_id" in str(e)
|
||||
|
||||
def test_create_empty_asset_id(self):
|
||||
"""空 asset_id 无效."""
|
||||
try:
|
||||
ClassificationJob.create(project_id="p1", asset_id="")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "asset_id" in str(e)
|
||||
|
||||
def test_create_whitespace_asset_id(self):
|
||||
"""纯空白 asset_id 无效."""
|
||||
try:
|
||||
ClassificationJob.create(project_id="p1", asset_id=" ")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "asset_id" in str(e)
|
||||
|
||||
def test_create_unique_id(self):
|
||||
"""不同 job id 不同."""
|
||||
j1 = ClassificationJob.create("p", "a")
|
||||
j2 = ClassificationJob.create("p", "a")
|
||||
assert j1.id != j2.id
|
||||
|
||||
def test_create_has_timestamps(self):
|
||||
"""有创建和更新时间."""
|
||||
job = ClassificationJob.create("p", "a")
|
||||
assert job.created_at is not None
|
||||
assert job.updated_at is not None
|
||||
@@ -1,474 +0,0 @@
|
||||
"""ColorGradeConfig 色彩调色配置单测.
|
||||
|
||||
纯逻辑模块,覆盖:数据类、resolve_params参数解析、has_effect效果判断、
|
||||
from_dict字典解析、validate校验、预设查询函数、clamp_param钳制。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.color_grade_config import (
|
||||
ALL_PARAM_KEYS,
|
||||
DEFAULT_PARAMS,
|
||||
PARAM_RANGES,
|
||||
PRESET_DISPLAY_NAMES,
|
||||
PRESET_PARAMS,
|
||||
VALID_PRESETS,
|
||||
ColorGradeConfig,
|
||||
clamp_param,
|
||||
get_preset_names,
|
||||
get_preset_params,
|
||||
)
|
||||
|
||||
|
||||
class TestColorGradeConfigDefaults:
|
||||
def test_default_disabled(self):
|
||||
config = ColorGradeConfig()
|
||||
assert config.enabled is False
|
||||
assert config.preset == ""
|
||||
assert config.brightness is None
|
||||
assert config.contrast is None
|
||||
assert config.saturation is None
|
||||
assert config.temperature is None
|
||||
assert config.hue is None
|
||||
|
||||
def test_default_resolve_returns_defaults(self):
|
||||
config = ColorGradeConfig()
|
||||
params = config.resolve_params()
|
||||
for key in ALL_PARAM_KEYS:
|
||||
assert params[key] == DEFAULT_PARAMS[key]
|
||||
|
||||
def test_default_has_no_effect(self):
|
||||
config = ColorGradeConfig()
|
||||
assert config.has_effect() is False
|
||||
|
||||
def test_default_validate_passes(self):
|
||||
config = ColorGradeConfig()
|
||||
ok, msg = config.validate()
|
||||
assert ok is True
|
||||
assert msg == ""
|
||||
|
||||
|
||||
class TestResolveParams:
|
||||
def test_disabled_still_resolves(self):
|
||||
"""禁用状态下仍能解析参数."""
|
||||
config = ColorGradeConfig(enabled=False, brightness=50.0)
|
||||
params = config.resolve_params()
|
||||
assert params["brightness"] == 50.0
|
||||
|
||||
def test_preset_fresh(self):
|
||||
config = ColorGradeConfig(enabled=True, preset="fresh")
|
||||
params = config.resolve_params()
|
||||
assert params["brightness"] == 8
|
||||
assert params["contrast"] == 10
|
||||
assert params["saturation"] == 120
|
||||
assert params["temperature"] == -8
|
||||
assert params["hue"] == 5
|
||||
|
||||
def test_preset_black_white(self):
|
||||
config = ColorGradeConfig(enabled=True, preset="black_white")
|
||||
params = config.resolve_params()
|
||||
assert params["saturation"] == 0
|
||||
assert params["contrast"] == 15
|
||||
|
||||
def test_preset_warm(self):
|
||||
config = ColorGradeConfig(enabled=True, preset="warm")
|
||||
params = config.resolve_params()
|
||||
assert params["temperature"] == 30
|
||||
|
||||
def test_preset_cool(self):
|
||||
config = ColorGradeConfig(enabled=True, preset="cool")
|
||||
params = config.resolve_params()
|
||||
assert params["temperature"] == -25
|
||||
|
||||
def test_invalid_preset_uses_defaults(self):
|
||||
config = ColorGradeConfig(enabled=True, preset="nonexistent")
|
||||
params = config.resolve_params()
|
||||
for key in ALL_PARAM_KEYS:
|
||||
assert params[key] == DEFAULT_PARAMS[key]
|
||||
|
||||
def test_custom_override_preset(self):
|
||||
config = ColorGradeConfig(
|
||||
enabled=True,
|
||||
preset="fresh",
|
||||
brightness=50.0,
|
||||
)
|
||||
params = config.resolve_params()
|
||||
# custom覆盖了预设
|
||||
assert params["brightness"] == 50.0
|
||||
# 其他参数仍用预设值
|
||||
assert params["contrast"] == 10
|
||||
assert params["saturation"] == 120
|
||||
|
||||
def test_multiple_custom_overrides(self):
|
||||
config = ColorGradeConfig(
|
||||
enabled=True,
|
||||
preset="vintage",
|
||||
brightness=20.0,
|
||||
saturation=150.0,
|
||||
hue=10.0,
|
||||
)
|
||||
params = config.resolve_params()
|
||||
assert params["brightness"] == 20.0
|
||||
assert params["saturation"] == 150.0
|
||||
assert params["hue"] == 10.0
|
||||
# 未覆盖的保留预设值
|
||||
assert params["contrast"] == 5
|
||||
assert params["temperature"] == 25
|
||||
|
||||
def test_custom_without_preset(self):
|
||||
config = ColorGradeConfig(
|
||||
enabled=True,
|
||||
preset="",
|
||||
brightness=30.0,
|
||||
contrast=-20.0,
|
||||
)
|
||||
params = config.resolve_params()
|
||||
assert params["brightness"] == 30.0
|
||||
assert params["contrast"] == -20.0
|
||||
# 未设置的用默认值
|
||||
assert params["saturation"] == 100.0
|
||||
assert params["temperature"] == 0.0
|
||||
assert params["hue"] == 0.0
|
||||
|
||||
def test_clamping_brightness_above_max(self):
|
||||
config = ColorGradeConfig(enabled=True, brightness=200.0)
|
||||
params = config.resolve_params()
|
||||
assert params["brightness"] == 100.0
|
||||
|
||||
def test_clamping_brightness_below_min(self):
|
||||
config = ColorGradeConfig(enabled=True, brightness=-200.0)
|
||||
params = config.resolve_params()
|
||||
assert params["brightness"] == -100.0
|
||||
|
||||
def test_clamping_saturation_below_zero(self):
|
||||
config = ColorGradeConfig(enabled=True, saturation=-10.0)
|
||||
params = config.resolve_params()
|
||||
assert params["saturation"] == 0.0
|
||||
|
||||
def test_clamping_saturation_above_max(self):
|
||||
config = ColorGradeConfig(enabled=True, saturation=300.0)
|
||||
params = config.resolve_params()
|
||||
assert params["saturation"] == 200.0
|
||||
|
||||
def test_clamping_hue_above_max(self):
|
||||
config = ColorGradeConfig(enabled=True, hue=200.0)
|
||||
params = config.resolve_params()
|
||||
assert params["hue"] == 180.0
|
||||
|
||||
def test_clamping_hue_below_min(self):
|
||||
config = ColorGradeConfig(enabled=True, hue=-200.0)
|
||||
params = config.resolve_params()
|
||||
assert params["hue"] == -180.0
|
||||
|
||||
def test_clamping_preset_plus_custom(self):
|
||||
"""预设值+自定义值超出范围时仍会钳制."""
|
||||
config = ColorGradeConfig(
|
||||
enabled=True,
|
||||
preset="fresh",
|
||||
saturation=250.0, # 超出200上限
|
||||
)
|
||||
params = config.resolve_params()
|
||||
assert params["saturation"] == 200.0
|
||||
|
||||
def test_returns_new_dict_each_time(self):
|
||||
config = ColorGradeConfig(enabled=True, brightness=10.0)
|
||||
p1 = config.resolve_params()
|
||||
p2 = config.resolve_params()
|
||||
assert p1 is not p2
|
||||
p1["brightness"] = 999
|
||||
assert p2["brightness"] == 10.0
|
||||
|
||||
|
||||
class TestHasEffect:
|
||||
def test_default_no_effect(self):
|
||||
config = ColorGradeConfig()
|
||||
assert config.has_effect() is False
|
||||
|
||||
def test_enabled_but_all_defaults(self):
|
||||
config = ColorGradeConfig(
|
||||
enabled=True,
|
||||
brightness=0.0,
|
||||
contrast=0.0,
|
||||
saturation=100.0,
|
||||
temperature=0.0,
|
||||
hue=0.0,
|
||||
)
|
||||
assert config.has_effect() is False
|
||||
|
||||
def test_brightness_change_has_effect(self):
|
||||
config = ColorGradeConfig(enabled=True, brightness=1.0)
|
||||
assert config.has_effect() is True
|
||||
|
||||
def test_contrast_change_has_effect(self):
|
||||
config = ColorGradeConfig(enabled=True, contrast=1.0)
|
||||
assert config.has_effect() is True
|
||||
|
||||
def test_saturation_change_has_effect(self):
|
||||
config = ColorGradeConfig(enabled=True, saturation=99.0)
|
||||
assert config.has_effect() is True
|
||||
|
||||
def test_temperature_change_has_effect(self):
|
||||
config = ColorGradeConfig(enabled=True, temperature=1.0)
|
||||
assert config.has_effect() is True
|
||||
|
||||
def test_hue_change_has_effect(self):
|
||||
config = ColorGradeConfig(enabled=True, hue=1.0)
|
||||
assert config.has_effect() is True
|
||||
|
||||
def test_all_presets_have_effect(self):
|
||||
for preset in VALID_PRESETS:
|
||||
config = ColorGradeConfig(enabled=True, preset=preset)
|
||||
assert config.has_effect() is True, f"preset {preset} should have effect"
|
||||
|
||||
def test_very_small_change_no_effect(self):
|
||||
"""小于0.001的浮点误差视为无效果."""
|
||||
config = ColorGradeConfig(enabled=True, brightness=0.0001)
|
||||
assert config.has_effect() is False
|
||||
|
||||
|
||||
class TestFromDict:
|
||||
def test_none_returns_disabled(self):
|
||||
config = ColorGradeConfig.from_dict(None)
|
||||
assert config.enabled is False
|
||||
|
||||
def test_empty_dict_returns_disabled(self):
|
||||
config = ColorGradeConfig.from_dict({})
|
||||
assert config.enabled is False
|
||||
|
||||
def test_enabled_false_returns_disabled(self):
|
||||
config = ColorGradeConfig.from_dict({"enabled": False})
|
||||
assert config.enabled is False
|
||||
|
||||
def test_basic_enabled(self):
|
||||
config = ColorGradeConfig.from_dict({"enabled": True})
|
||||
assert config.enabled is True
|
||||
assert config.preset == ""
|
||||
assert config.brightness is None
|
||||
|
||||
def test_with_preset(self):
|
||||
config = ColorGradeConfig.from_dict({"enabled": True, "preset": "warm"})
|
||||
assert config.enabled is True
|
||||
assert config.preset == "warm"
|
||||
|
||||
def test_invalid_preset_ignored(self):
|
||||
config = ColorGradeConfig.from_dict({"enabled": True, "preset": "invalid"})
|
||||
assert config.enabled is True
|
||||
assert config.preset == ""
|
||||
|
||||
def test_with_custom_params(self):
|
||||
config = ColorGradeConfig.from_dict(
|
||||
{
|
||||
"enabled": True,
|
||||
"brightness": 50,
|
||||
"contrast": -10.5,
|
||||
"saturation": 150.0,
|
||||
"temperature": 20,
|
||||
"hue": -15.5,
|
||||
}
|
||||
)
|
||||
assert config.brightness == 50.0
|
||||
assert config.contrast == -10.5
|
||||
assert config.saturation == 150.0
|
||||
assert config.temperature == 20.0
|
||||
assert config.hue == -15.5
|
||||
|
||||
def test_invalid_float_values_return_none(self):
|
||||
config = ColorGradeConfig.from_dict(
|
||||
{
|
||||
"enabled": True,
|
||||
"brightness": "not_a_number",
|
||||
"contrast": None,
|
||||
}
|
||||
)
|
||||
assert config.brightness is None
|
||||
assert config.contrast is None
|
||||
|
||||
def test_int_values_work(self):
|
||||
config = ColorGradeConfig.from_dict({"enabled": True, "brightness": 10})
|
||||
assert config.brightness == 10.0
|
||||
|
||||
def test_string_float_values_work(self):
|
||||
config = ColorGradeConfig.from_dict({"enabled": True, "brightness": "15.5"})
|
||||
assert config.brightness == 15.5
|
||||
|
||||
def test_partial_custom_params(self):
|
||||
config = ColorGradeConfig.from_dict(
|
||||
{
|
||||
"enabled": True,
|
||||
"preset": "cinema",
|
||||
"brightness": 5.0,
|
||||
}
|
||||
)
|
||||
assert config.preset == "cinema"
|
||||
assert config.brightness == 5.0
|
||||
assert config.contrast is None
|
||||
|
||||
|
||||
class TestValidate:
|
||||
def test_disabled_valid(self):
|
||||
config = ColorGradeConfig(enabled=False)
|
||||
ok, msg = config.validate()
|
||||
assert ok is True
|
||||
assert msg == ""
|
||||
|
||||
def test_valid_config(self):
|
||||
config = ColorGradeConfig(
|
||||
enabled=True,
|
||||
preset="fresh",
|
||||
brightness=50.0,
|
||||
)
|
||||
ok, msg = config.validate()
|
||||
assert ok is True
|
||||
|
||||
def test_invalid_preset(self):
|
||||
config = ColorGradeConfig(enabled=True, preset="invalid")
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "预设" in msg
|
||||
|
||||
def test_brightness_above_max_invalid(self):
|
||||
config = ColorGradeConfig(enabled=True, brightness=150.0)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "brightness" in msg
|
||||
|
||||
def test_brightness_below_min_invalid(self):
|
||||
config = ColorGradeConfig(enabled=True, brightness=-150.0)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "brightness" in msg
|
||||
|
||||
def test_saturation_below_zero_invalid(self):
|
||||
config = ColorGradeConfig(enabled=True, saturation=-10.0)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "saturation" in msg
|
||||
|
||||
def test_saturation_above_max_invalid(self):
|
||||
config = ColorGradeConfig(enabled=True, saturation=250.0)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "saturation" in msg
|
||||
|
||||
def test_hue_above_max_invalid(self):
|
||||
config = ColorGradeConfig(enabled=True, hue=200.0)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "hue" in msg
|
||||
|
||||
def test_hue_below_min_invalid(self):
|
||||
config = ColorGradeConfig(enabled=True, hue=-200.0)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "hue" in msg
|
||||
|
||||
def test_boundary_values_valid(self):
|
||||
config = ColorGradeConfig(
|
||||
enabled=True,
|
||||
brightness=-100.0,
|
||||
contrast=100.0,
|
||||
saturation=0.0,
|
||||
temperature=-100.0,
|
||||
hue=-180.0,
|
||||
)
|
||||
ok, _ = config.validate()
|
||||
assert ok is True
|
||||
|
||||
def test_none_values_valid(self):
|
||||
"""None值不参与校验(视为未设置)."""
|
||||
config = ColorGradeConfig(enabled=True)
|
||||
ok, _ = config.validate()
|
||||
assert ok is True
|
||||
|
||||
|
||||
class TestGetPresetNames:
|
||||
def test_returns_sorted_list(self):
|
||||
names = get_preset_names()
|
||||
assert isinstance(names, list)
|
||||
preset_keys = [n[0] for n in names]
|
||||
assert preset_keys == sorted(preset_keys)
|
||||
|
||||
def test_count_matches_valid_presets(self):
|
||||
names = get_preset_names()
|
||||
assert len(names) == len(VALID_PRESETS)
|
||||
|
||||
def test_each_entry_has_name_and_display(self):
|
||||
names = get_preset_names()
|
||||
for name, display in names:
|
||||
assert name in VALID_PRESETS
|
||||
assert display == PRESET_DISPLAY_NAMES[name]
|
||||
assert len(display) > 0
|
||||
|
||||
|
||||
class TestGetPresetParams:
|
||||
def test_existing_preset(self):
|
||||
params = get_preset_params("fresh")
|
||||
assert params is not None
|
||||
assert params["brightness"] == 8
|
||||
|
||||
def test_nonexistent_preset(self):
|
||||
params = get_preset_params("nonexistent")
|
||||
assert params is None
|
||||
|
||||
def test_all_presets_have_all_params(self):
|
||||
for preset in VALID_PRESETS:
|
||||
params = get_preset_params(preset)
|
||||
assert params is not None
|
||||
for key in ALL_PARAM_KEYS:
|
||||
assert key in params
|
||||
|
||||
def test_returns_dict_with_correct_values(self):
|
||||
"""返回的字典包含所有预期参数."""
|
||||
params = get_preset_params("warm")
|
||||
assert params["brightness"] == 5
|
||||
assert params["temperature"] == 30
|
||||
assert len(params) == 5
|
||||
|
||||
|
||||
class TestClampParam:
|
||||
def test_brightness_within_range(self):
|
||||
assert clamp_param("brightness", 50.0) == 50.0
|
||||
|
||||
def test_brightness_above_max(self):
|
||||
assert clamp_param("brightness", 200.0) == 100.0
|
||||
|
||||
def test_brightness_below_min(self):
|
||||
assert clamp_param("brightness", -200.0) == -100.0
|
||||
|
||||
def test_saturation_within_range(self):
|
||||
assert clamp_param("saturation", 100.0) == 100.0
|
||||
|
||||
def test_saturation_at_boundary(self):
|
||||
assert clamp_param("saturation", 0.0) == 0.0
|
||||
assert clamp_param("saturation", 200.0) == 200.0
|
||||
|
||||
def test_hue_within_range(self):
|
||||
assert clamp_param("hue", 90.0) == 90.0
|
||||
|
||||
def test_hue_above_max(self):
|
||||
assert clamp_param("hue", 200.0) == 180.0
|
||||
|
||||
def test_unknown_param_passthrough(self):
|
||||
assert clamp_param("unknown", 999.0) == 999.0
|
||||
|
||||
|
||||
class TestConstants:
|
||||
def test_all_presets_have_params(self):
|
||||
for preset in VALID_PRESETS:
|
||||
assert preset in PRESET_PARAMS
|
||||
|
||||
def test_all_presets_have_display_names(self):
|
||||
for preset in VALID_PRESETS:
|
||||
assert preset in PRESET_DISPLAY_NAMES
|
||||
|
||||
def test_param_ranges_has_all_keys(self):
|
||||
for key in ALL_PARAM_KEYS:
|
||||
assert key in PARAM_RANGES
|
||||
|
||||
def test_default_params_has_all_keys(self):
|
||||
for key in ALL_PARAM_KEYS:
|
||||
assert key in DEFAULT_PARAMS
|
||||
|
||||
def test_param_ranges_min_less_than_max(self):
|
||||
for key, (min_val, max_val) in PARAM_RANGES.items():
|
||||
assert min_val < max_val, f"{key}: min ({min_val}) should be < max ({max_val})"
|
||||
@@ -1,421 +0,0 @@
|
||||
"""domain层小模块批量单测.
|
||||
|
||||
覆盖:recipe / tag / editing_mode / voice_library / title_library / template / edit_template
|
||||
共 7 个模块,纯逻辑 0 外部依赖。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.edit_template import EditTemplate, EditTemplateStatus
|
||||
from packages.domain.editing_mode import EditingMode
|
||||
from packages.domain.recipe import Recipe, RecipeItem
|
||||
from packages.domain.tag import Tag
|
||||
from packages.domain.template import Template, TemplateCategory, TemplateSegment
|
||||
from packages.domain.title_library import TitleLibraryItem
|
||||
from packages.domain.voice_library import VoiceLibraryItem
|
||||
|
||||
|
||||
class TestEditingMode:
|
||||
"""EditingMode 枚举测试."""
|
||||
|
||||
def test_four_modes(self):
|
||||
"""四种剪辑模式."""
|
||||
assert len(EditingMode) == 4
|
||||
|
||||
def test_one_take(self):
|
||||
assert EditingMode.ONE_TAKE == "one_take"
|
||||
|
||||
def test_pip(self):
|
||||
assert EditingMode.PIP == "pip"
|
||||
|
||||
def test_voice_over(self):
|
||||
assert EditingMode.VOICE_OVER == "voice_over"
|
||||
|
||||
def test_voice_pip(self):
|
||||
assert EditingMode.VOICE_PIP == "voice_pip"
|
||||
|
||||
def test_all_values_unique(self):
|
||||
values = [m.value for m in EditingMode]
|
||||
assert len(values) == len(set(values))
|
||||
|
||||
|
||||
class TestTag:
|
||||
"""Tag 测试."""
|
||||
|
||||
def test_create_valid(self):
|
||||
"""正常创建."""
|
||||
tag = Tag.create(user_id="u1", name=" 搞笑 ")
|
||||
assert tag.user_id == "u1"
|
||||
assert tag.name == "搞笑"
|
||||
assert isinstance(tag.id, str)
|
||||
assert len(tag.id) > 0
|
||||
assert tag.created_at is not None
|
||||
|
||||
def test_create_empty_name(self):
|
||||
"""空名称无效."""
|
||||
try:
|
||||
Tag.create("u1", "")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "不能为空" in str(e)
|
||||
|
||||
def test_create_whitespace_name(self):
|
||||
"""纯空白名称无效."""
|
||||
try:
|
||||
Tag.create("u1", " ")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "不能为空" in str(e)
|
||||
|
||||
def test_create_unique_id(self):
|
||||
t1 = Tag.create("u", "t1")
|
||||
t2 = Tag.create("u", "t2")
|
||||
assert t1.id != t2.id
|
||||
|
||||
|
||||
class TestRecipeItem:
|
||||
"""RecipeItem 测试."""
|
||||
|
||||
def test_create_asset_item(self):
|
||||
"""素材配方项."""
|
||||
item = RecipeItem(
|
||||
id="item1",
|
||||
recipe_id="r1",
|
||||
item_type="asset",
|
||||
item_id="a1",
|
||||
position=0,
|
||||
)
|
||||
assert item.item_type == "asset"
|
||||
assert item.item_id == "a1"
|
||||
assert item.position == 0
|
||||
|
||||
def test_create_title_item(self):
|
||||
"""标题配方项."""
|
||||
item = RecipeItem(id="i2", recipe_id="r1", item_type="title", item_id="t1", position=1)
|
||||
assert item.item_type == "title"
|
||||
assert item.position == 1
|
||||
|
||||
def test_default_metadata(self):
|
||||
"""默认 metadata 为空 dict."""
|
||||
item = RecipeItem(id="i1", recipe_id="r1", item_type="voice", item_id="v1")
|
||||
assert item.metadata_ == {}
|
||||
|
||||
|
||||
class TestRecipe:
|
||||
"""Recipe 测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简配方."""
|
||||
recipe = Recipe(id="r1", user_id="u1", name="我的配方")
|
||||
assert recipe.name == "我的配方"
|
||||
assert recipe.description == ""
|
||||
assert recipe.template_id == ""
|
||||
assert recipe.generation_params == {}
|
||||
assert recipe.items == []
|
||||
assert recipe.is_active is True
|
||||
assert recipe.created_at is not None
|
||||
assert recipe.updated_at is not None
|
||||
|
||||
def test_create_with_items(self):
|
||||
"""带配方项."""
|
||||
items = [
|
||||
RecipeItem(id="i1", recipe_id="r1", item_type="asset", item_id="a1", position=0),
|
||||
RecipeItem(id="i2", recipe_id="r1", item_type="title", item_id="t1", position=1),
|
||||
]
|
||||
recipe = Recipe(id="r1", user_id="u1", name="配方", items=items)
|
||||
assert len(recipe.items) == 2
|
||||
assert recipe.items[0].item_type == "asset"
|
||||
assert recipe.items[1].position == 1
|
||||
|
||||
def test_default_items_empty_list(self):
|
||||
"""默认 items 为空列表."""
|
||||
r1 = Recipe(id="r1", user_id="u1", name="r1")
|
||||
r2 = Recipe(id="r2", user_id="u2", name="r2")
|
||||
r1.items.append("fake")
|
||||
assert r2.items == []
|
||||
|
||||
|
||||
class TestVoiceLibraryItem:
|
||||
"""VoiceLibraryItem 测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简创建."""
|
||||
item = VoiceLibraryItem(id="v1", user_id="u1", name="我的配音")
|
||||
assert item.name == "我的配音"
|
||||
assert item.text == ""
|
||||
assert item.voice_provider == ""
|
||||
assert item.voice_id == ""
|
||||
assert item.voice_name == ""
|
||||
assert item.audio_url == ""
|
||||
assert item.duration == 0
|
||||
assert item.file_size == 0
|
||||
assert item.status == "completed"
|
||||
assert item.project_id is None
|
||||
assert item.tags == []
|
||||
assert item.metadata_ == {}
|
||||
|
||||
def test_create_full(self):
|
||||
"""带全部字段."""
|
||||
item = VoiceLibraryItem(
|
||||
id="v1",
|
||||
user_id="u1",
|
||||
name="旁白",
|
||||
text="大家好",
|
||||
voice_provider="xf",
|
||||
voice_id="v1",
|
||||
voice_name="小云",
|
||||
audio_url="http://x/a.mp3",
|
||||
duration=10.5,
|
||||
file_size=102400,
|
||||
status="processing",
|
||||
project_id="p1",
|
||||
tags=["旁白", "正式"],
|
||||
)
|
||||
assert item.text == "大家好"
|
||||
assert item.duration == 10.5
|
||||
assert item.file_size == 102400
|
||||
assert item.project_id == "p1"
|
||||
assert item.tags == ["旁白", "正式"]
|
||||
|
||||
def test_tags_independent(self):
|
||||
"""不同实例的 tags 独立."""
|
||||
i1 = VoiceLibraryItem(id="v1", user_id="u", name="n1")
|
||||
i2 = VoiceLibraryItem(id="v2", user_id="u", name="n2")
|
||||
i1.tags.append("x")
|
||||
assert i2.tags == []
|
||||
|
||||
|
||||
class TestTitleLibraryItem:
|
||||
"""TitleLibraryItem 测试."""
|
||||
|
||||
def test_create_required(self):
|
||||
"""必填字段."""
|
||||
item = TitleLibraryItem(id="t1", user_id="u1", name="爆款标题", text="这也太牛了")
|
||||
assert item.name == "爆款标题"
|
||||
assert item.text == "这也太牛了"
|
||||
assert item.category == "default"
|
||||
assert item.description == ""
|
||||
assert item.tags == []
|
||||
assert item.usage_count == 0
|
||||
assert item.is_active is True
|
||||
assert item.metadata_ == {}
|
||||
|
||||
def test_create_full(self):
|
||||
"""带全部字段."""
|
||||
item = TitleLibraryItem(
|
||||
id="t1",
|
||||
user_id="u1",
|
||||
name="科技标题",
|
||||
text="震惊!",
|
||||
category="tech",
|
||||
description="科技类标题",
|
||||
tags=["科技", "爆款"],
|
||||
usage_count=100,
|
||||
is_active=False,
|
||||
)
|
||||
assert item.category == "tech"
|
||||
assert item.usage_count == 100
|
||||
assert item.is_active is False
|
||||
assert item.tags == ["科技", "爆款"]
|
||||
|
||||
def test_tags_independent(self):
|
||||
i1 = TitleLibraryItem(id="t1", user_id="u", name="n", text="t")
|
||||
i2 = TitleLibraryItem(id="t2", user_id="u", name="n", text="t")
|
||||
i1.tags.append("x")
|
||||
assert i2.tags == []
|
||||
|
||||
|
||||
class TestTemplateSegment:
|
||||
"""TemplateSegment 测试."""
|
||||
|
||||
def test_create(self):
|
||||
"""正常创建."""
|
||||
seg = TemplateSegment(
|
||||
id="s1",
|
||||
template_id="t1",
|
||||
segment_order=0,
|
||||
duration_min=2.0,
|
||||
duration_max=5.0,
|
||||
)
|
||||
assert seg.segment_order == 0
|
||||
assert seg.duration_min == 2.0
|
||||
assert seg.duration_max == 5.0
|
||||
assert seg.material_type is None
|
||||
|
||||
def test_with_material_type(self):
|
||||
"""带素材类型(voice_over模式)."""
|
||||
seg = TemplateSegment(
|
||||
id="s1",
|
||||
template_id="t1",
|
||||
segment_order=0,
|
||||
duration_min=3.0,
|
||||
duration_max=8.0,
|
||||
material_type="person",
|
||||
)
|
||||
assert seg.material_type == "person"
|
||||
|
||||
def test_has_timestamps(self):
|
||||
seg = TemplateSegment(id="s1", template_id="t1", segment_order=0, duration_min=1, duration_max=2)
|
||||
assert seg.created_at is not None
|
||||
assert seg.updated_at is not None
|
||||
|
||||
|
||||
class TestTemplate:
|
||||
"""Template 测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简模板."""
|
||||
tpl = Template(id="t1", user_id="u1", name="通用模板", mode="one_take")
|
||||
assert tpl.name == "通用模板"
|
||||
assert tpl.mode == "one_take"
|
||||
assert tpl.category == ""
|
||||
assert tpl.tags == []
|
||||
assert tpl.title_config == {}
|
||||
assert tpl.subtitle_config == {}
|
||||
assert tpl.bgm_config == {}
|
||||
assert tpl.estimated_duration == 0.0
|
||||
assert tpl.segments == []
|
||||
assert tpl.is_active is True
|
||||
|
||||
def test_create_with_segments(self):
|
||||
"""带片段."""
|
||||
segs = [
|
||||
TemplateSegment(id="s1", template_id="t1", segment_order=0, duration_min=2, duration_max=5),
|
||||
TemplateSegment(id="s2", template_id="t1", segment_order=1, duration_min=3, duration_max=7),
|
||||
]
|
||||
tpl = Template(id="t1", user_id="u1", name="模板", mode="pip", segments=segs)
|
||||
assert len(tpl.segments) == 2
|
||||
assert tpl.segments[0].segment_order == 0
|
||||
|
||||
def test_segments_independent(self):
|
||||
t1 = Template(id="t1", user_id="u", name="n1", mode="one_take")
|
||||
t2 = Template(id="t2", user_id="u", name="n2", mode="pip")
|
||||
t1.segments.append("fake")
|
||||
assert t2.segments == []
|
||||
|
||||
|
||||
class TestTemplateCategory:
|
||||
"""TemplateCategory 测试."""
|
||||
|
||||
def test_create(self):
|
||||
cat = TemplateCategory(id="c1", user_id="u1", name="科技")
|
||||
assert cat.name == "科技"
|
||||
assert cat.created_at is not None
|
||||
|
||||
|
||||
class TestEditTemplateStatus:
|
||||
"""EditTemplateStatus 枚举测试."""
|
||||
|
||||
def test_two_statuses(self):
|
||||
assert len(EditTemplateStatus) == 2
|
||||
|
||||
def test_active(self):
|
||||
assert EditTemplateStatus.ACTIVE == "active"
|
||||
|
||||
def test_inactive(self):
|
||||
assert EditTemplateStatus.INACTIVE == "inactive"
|
||||
|
||||
|
||||
class TestEditTemplate:
|
||||
"""EditTemplate 测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简创建."""
|
||||
tpl = EditTemplate.create(name=" 通用模板 ")
|
||||
assert tpl.name == "通用模板"
|
||||
assert tpl.description == ""
|
||||
assert tpl.template_type == "default"
|
||||
assert tpl.config == {}
|
||||
assert tpl.preview_url == ""
|
||||
assert tpl.sort_weight == 0
|
||||
assert tpl.status == EditTemplateStatus.ACTIVE
|
||||
assert isinstance(tpl.id, str)
|
||||
assert len(tpl.id) > 0
|
||||
|
||||
def test_create_full(self):
|
||||
"""带全部字段."""
|
||||
tpl = EditTemplate.create(
|
||||
name="口播模板",
|
||||
description="口播类模板",
|
||||
template_type="voice_over",
|
||||
config={"style": "formal"},
|
||||
preview_url="https://x/preview.mp4",
|
||||
sort_weight=100,
|
||||
status=EditTemplateStatus.INACTIVE,
|
||||
)
|
||||
assert tpl.name == "口播模板"
|
||||
assert tpl.description == "口播类模板"
|
||||
assert tpl.template_type == "voice_over"
|
||||
assert tpl.config == {"style": "formal"}
|
||||
assert tpl.preview_url == "https://x/preview.mp4"
|
||||
assert tpl.sort_weight == 100
|
||||
assert tpl.status == EditTemplateStatus.INACTIVE
|
||||
|
||||
def test_create_empty_name(self):
|
||||
"""空名称无效."""
|
||||
try:
|
||||
EditTemplate.create(name="")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "不能为空" in str(e)
|
||||
|
||||
def test_create_whitespace_name(self):
|
||||
"""空白名称无效."""
|
||||
try:
|
||||
EditTemplate.create(name=" ")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "不能为空" in str(e)
|
||||
|
||||
def test_empty_template_type_defaults(self):
|
||||
"""空 template_type 默认 default."""
|
||||
tpl = EditTemplate.create(name="t", template_type="")
|
||||
assert tpl.template_type == "default"
|
||||
|
||||
def test_whitespace_template_type_defaults(self):
|
||||
"""空白 template_type 默认 default."""
|
||||
tpl = EditTemplate.create(name="t", template_type=" ")
|
||||
assert tpl.template_type == "default"
|
||||
|
||||
def test_config_none_defaults_empty(self):
|
||||
"""config=None 默认为空 dict."""
|
||||
tpl = EditTemplate.create(name="t", config=None)
|
||||
assert tpl.config == {}
|
||||
|
||||
def test_activate(self):
|
||||
"""激活."""
|
||||
tpl = EditTemplate.create(name="t", status=EditTemplateStatus.INACTIVE)
|
||||
old = tpl.updated_at
|
||||
tpl.activate()
|
||||
assert tpl.is_active is True
|
||||
assert tpl.status == EditTemplateStatus.ACTIVE
|
||||
assert tpl.updated_at >= old
|
||||
|
||||
def test_deactivate(self):
|
||||
"""停用."""
|
||||
tpl = EditTemplate.create(name="t")
|
||||
old = tpl.updated_at
|
||||
tpl.deactivate()
|
||||
assert tpl.is_active is False
|
||||
assert tpl.status == EditTemplateStatus.INACTIVE
|
||||
assert tpl.updated_at >= old
|
||||
|
||||
def test_is_active_property(self):
|
||||
"""is_active 属性."""
|
||||
tpl = EditTemplate.create(name="t")
|
||||
assert tpl.is_active is True
|
||||
tpl.deactivate()
|
||||
assert tpl.is_active is False
|
||||
tpl.activate()
|
||||
assert tpl.is_active is True
|
||||
|
||||
def test_unique_id(self):
|
||||
t1 = EditTemplate.create(name="t1")
|
||||
t2 = EditTemplate.create(name="t2")
|
||||
assert t1.id != t2.id
|
||||
|
||||
def test_config_independent(self):
|
||||
t1 = EditTemplate.create(name="t1")
|
||||
t2 = EditTemplate.create(name="t2")
|
||||
t1.config["k"] = "v"
|
||||
assert "k" not in t2.config
|
||||
@@ -1,376 +0,0 @@
|
||||
"""duplication 单测.
|
||||
|
||||
domain 层查重记录纯逻辑模块,0 外部依赖。
|
||||
覆盖:DuplicateSegment 工厂/校验、DuplicationRecord 创建/状态流转/重试。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.duplication import DuplicateSegment, DuplicationRecord
|
||||
|
||||
|
||||
class TestDuplicateSegmentCreate:
|
||||
"""DuplicateSegment.create 工厂方法测试."""
|
||||
|
||||
def test_create_valid(self):
|
||||
"""正常创建."""
|
||||
seg = DuplicateSegment.create(
|
||||
source_start=1.0,
|
||||
source_end=5.0,
|
||||
matched_video_id="vid123",
|
||||
matched_video_name="测试视频",
|
||||
matched_start=10.0,
|
||||
matched_end=14.0,
|
||||
similarity=85.5,
|
||||
)
|
||||
assert seg.source_start == 1.0
|
||||
assert seg.source_end == 5.0
|
||||
assert seg.matched_video_id == "vid123"
|
||||
assert seg.matched_video_name == "测试视频"
|
||||
assert seg.matched_start == 10.0
|
||||
assert seg.matched_end == 14.0
|
||||
assert seg.similarity == 85.5
|
||||
assert isinstance(seg.id, str)
|
||||
assert len(seg.id) > 0
|
||||
|
||||
def test_create_generates_unique_id(self):
|
||||
"""每次创建生成不同的 id."""
|
||||
seg1 = DuplicateSegment.create(0, 1, "v", "n", 0, 1, 50.0)
|
||||
seg2 = DuplicateSegment.create(0, 1, "v", "n", 0, 1, 50.0)
|
||||
assert seg1.id != seg2.id
|
||||
|
||||
def test_create_negative_source_start(self):
|
||||
"""source_start 为负抛出 ValueError."""
|
||||
try:
|
||||
DuplicateSegment.create(-1, 5, "v", "n", 0, 1, 50.0)
|
||||
assert False, "应该抛出 ValueError"
|
||||
except ValueError as e:
|
||||
assert "source" in str(e).lower()
|
||||
|
||||
def test_create_source_end_equals_start(self):
|
||||
"""source_end 等于 source_start 无效."""
|
||||
try:
|
||||
DuplicateSegment.create(5, 5, "v", "n", 0, 1, 50.0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "source" in str(e).lower()
|
||||
|
||||
def test_create_source_end_less_than_start(self):
|
||||
"""source_end 小于 source_start 无效."""
|
||||
try:
|
||||
DuplicateSegment.create(5, 3, "v", "n", 0, 1, 50.0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "source" in str(e).lower()
|
||||
|
||||
def test_create_negative_matched_start(self):
|
||||
"""matched_start 为负无效."""
|
||||
try:
|
||||
DuplicateSegment.create(0, 5, "v", "n", -1, 1, 50.0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "matched" in str(e).lower()
|
||||
|
||||
def test_create_matched_end_invalid(self):
|
||||
"""matched_end <= matched_start 无效."""
|
||||
try:
|
||||
DuplicateSegment.create(0, 5, "v", "n", 5, 5, 50.0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "matched" in str(e).lower()
|
||||
|
||||
def test_create_similarity_zero(self):
|
||||
"""similarity = 0 是合法的."""
|
||||
seg = DuplicateSegment.create(0, 1, "v", "n", 0, 1, 0.0)
|
||||
assert seg.similarity == 0.0
|
||||
|
||||
def test_create_similarity_100(self):
|
||||
"""similarity = 100 是合法的."""
|
||||
seg = DuplicateSegment.create(0, 1, "v", "n", 0, 1, 100.0)
|
||||
assert seg.similarity == 100.0
|
||||
|
||||
def test_create_similarity_negative(self):
|
||||
"""similarity < 0 无效."""
|
||||
try:
|
||||
DuplicateSegment.create(0, 1, "v", "n", 0, 1, -1.0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "similarity" in str(e).lower()
|
||||
|
||||
def test_create_similarity_over_100(self):
|
||||
"""similarity > 100 无效."""
|
||||
try:
|
||||
DuplicateSegment.create(0, 1, "v", "n", 0, 1, 101.0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "similarity" in str(e).lower()
|
||||
|
||||
|
||||
class TestDuplicationRecordCreate:
|
||||
"""DuplicationRecord.create 工厂方法测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简创建."""
|
||||
rec = DuplicationRecord.create(
|
||||
user_id="user1",
|
||||
filename="test.mp4",
|
||||
file_size=1024,
|
||||
storage_key="oss://bucket/test.mp4",
|
||||
)
|
||||
assert rec.user_id == "user1"
|
||||
assert rec.filename == "test.mp4"
|
||||
assert rec.file_size == 1024
|
||||
assert rec.storage_key == "oss://bucket/test.mp4"
|
||||
assert rec.duration_seconds == 0.0
|
||||
assert rec.status == "pending"
|
||||
assert rec.duplicate_rate is None
|
||||
assert rec.duplicate_count == 0
|
||||
assert rec.segments == []
|
||||
assert rec.error_message == ""
|
||||
assert isinstance(rec.id, str)
|
||||
assert len(rec.id) > 0
|
||||
|
||||
def test_create_with_duration(self):
|
||||
"""带时长创建."""
|
||||
rec = DuplicationRecord.create(
|
||||
user_id="user1",
|
||||
filename="test.mp4",
|
||||
file_size=1024,
|
||||
storage_key="oss://key",
|
||||
duration_seconds=120.5,
|
||||
)
|
||||
assert rec.duration_seconds == 120.5
|
||||
|
||||
def test_create_strips_whitespace(self):
|
||||
"""user_id 和 filename 会 strip."""
|
||||
rec = DuplicationRecord.create(
|
||||
user_id=" user1 ",
|
||||
filename=" test.mp4 ",
|
||||
file_size=1024,
|
||||
storage_key="oss://key",
|
||||
)
|
||||
assert rec.user_id == "user1"
|
||||
assert rec.filename == "test.mp4"
|
||||
|
||||
def test_create_empty_user_id(self):
|
||||
"""空 user_id 无效."""
|
||||
try:
|
||||
DuplicationRecord.create("", "test.mp4", 1024, "oss://key")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "user_id" in str(e)
|
||||
|
||||
def test_create_whitespace_user_id(self):
|
||||
"""纯空白 user_id 无效."""
|
||||
try:
|
||||
DuplicationRecord.create(" ", "test.mp4", 1024, "oss://key")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "user_id" in str(e)
|
||||
|
||||
def test_create_empty_filename(self):
|
||||
"""空 filename 无效."""
|
||||
try:
|
||||
DuplicationRecord.create("user1", "", 1024, "oss://key")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "filename" in str(e)
|
||||
|
||||
def test_create_whitespace_filename(self):
|
||||
"""纯空白 filename 无效."""
|
||||
try:
|
||||
DuplicationRecord.create("user1", " ", 1024, "oss://key")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "filename" in str(e)
|
||||
|
||||
def test_create_zero_file_size(self):
|
||||
"""file_size = 0 无效."""
|
||||
try:
|
||||
DuplicationRecord.create("user1", "test.mp4", 0, "oss://key")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "file_size" in str(e)
|
||||
|
||||
def test_create_negative_file_size(self):
|
||||
"""file_size 为负无效."""
|
||||
try:
|
||||
DuplicationRecord.create("user1", "test.mp4", -1, "oss://key")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "file_size" in str(e)
|
||||
|
||||
def test_create_unique_id(self):
|
||||
"""不同记录 id 不同."""
|
||||
r1 = DuplicationRecord.create("u", "f", 1, "k")
|
||||
r2 = DuplicationRecord.create("u", "f", 1, "k")
|
||||
assert r1.id != r2.id
|
||||
|
||||
def test_create_has_timestamps(self):
|
||||
"""有创建和更新时间."""
|
||||
rec = DuplicationRecord.create("u", "f", 1, "k")
|
||||
assert rec.created_at is not None
|
||||
assert rec.updated_at is not None
|
||||
# 两者应该很接近(都是 now)
|
||||
delta = (rec.updated_at - rec.created_at).total_seconds()
|
||||
assert abs(delta) < 1.0
|
||||
|
||||
|
||||
class TestDuplicationRecordStatusFlow:
|
||||
"""状态流转测试."""
|
||||
|
||||
def _make_record(self):
|
||||
return DuplicationRecord.create("user1", "test.mp4", 1024, "oss://key")
|
||||
|
||||
def test_initial_status_pending(self):
|
||||
"""初始状态 pending."""
|
||||
rec = self._make_record()
|
||||
assert rec.status == "pending"
|
||||
|
||||
def test_mark_processing(self):
|
||||
"""标记为处理中."""
|
||||
rec = self._make_record()
|
||||
old_updated = rec.updated_at
|
||||
rec.mark_processing()
|
||||
assert rec.status == "processing"
|
||||
assert rec.updated_at >= old_updated
|
||||
|
||||
def test_mark_completed(self):
|
||||
"""标记为完成."""
|
||||
rec = self._make_record()
|
||||
seg = DuplicateSegment.create(0, 1, "v", "n", 0, 1, 80.0)
|
||||
rec.mark_completed(duplicate_rate=45.5, duplicate_count=3, segments=[seg])
|
||||
assert rec.status == "completed"
|
||||
assert rec.duplicate_rate == 45.5
|
||||
assert rec.duplicate_count == 3
|
||||
assert len(rec.segments) == 1
|
||||
assert rec.segments[0].similarity == 80.0
|
||||
|
||||
def test_mark_completed_zero_rate(self):
|
||||
"""重复率为 0 合法."""
|
||||
rec = self._make_record()
|
||||
rec.mark_completed(0.0, 0, [])
|
||||
assert rec.status == "completed"
|
||||
assert rec.duplicate_rate == 0.0
|
||||
assert rec.duplicate_count == 0
|
||||
assert rec.segments == []
|
||||
|
||||
def test_mark_completed_full_rate(self):
|
||||
"""重复率 100 合法."""
|
||||
rec = self._make_record()
|
||||
rec.mark_completed(100.0, 1, [])
|
||||
assert rec.duplicate_rate == 100.0
|
||||
|
||||
def test_mark_completed_negative_rate(self):
|
||||
"""重复率为负无效."""
|
||||
rec = self._make_record()
|
||||
try:
|
||||
rec.mark_completed(-1, 0, [])
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "duplicate_rate" in str(e)
|
||||
|
||||
def test_mark_completed_over_100(self):
|
||||
"""重复率超过 100 无效."""
|
||||
rec = self._make_record()
|
||||
try:
|
||||
rec.mark_completed(101, 0, [])
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "duplicate_rate" in str(e)
|
||||
|
||||
def test_mark_failed(self):
|
||||
"""标记为失败."""
|
||||
rec = self._make_record()
|
||||
rec.mark_failed("网络超时")
|
||||
assert rec.status == "failed"
|
||||
assert rec.error_message == "网络超时"
|
||||
|
||||
def test_mark_failed_empty_message(self):
|
||||
"""失败信息可以为空字符串."""
|
||||
rec = self._make_record()
|
||||
rec.mark_failed("")
|
||||
assert rec.status == "failed"
|
||||
assert rec.error_message == ""
|
||||
|
||||
def test_can_retry_failed(self):
|
||||
"""failed 状态可以重试."""
|
||||
rec = self._make_record()
|
||||
rec.mark_failed("error")
|
||||
assert rec.can_retry() is True
|
||||
|
||||
def test_cannot_retry_pending(self):
|
||||
"""pending 状态不可重试."""
|
||||
rec = self._make_record()
|
||||
assert rec.can_retry() is False
|
||||
|
||||
def test_cannot_retry_processing(self):
|
||||
"""processing 状态不可重试."""
|
||||
rec = self._make_record()
|
||||
rec.mark_processing()
|
||||
assert rec.can_retry() is False
|
||||
|
||||
def test_cannot_retry_completed(self):
|
||||
"""completed 状态不可重试."""
|
||||
rec = self._make_record()
|
||||
rec.mark_completed(50, 1, [])
|
||||
assert rec.can_retry() is False
|
||||
|
||||
def test_reset_for_retry(self):
|
||||
"""重置回 pending."""
|
||||
rec = self._make_record()
|
||||
rec.mark_failed("error")
|
||||
seg = DuplicateSegment.create(0, 1, "v", "n", 0, 1, 50.0)
|
||||
rec.segments = [seg]
|
||||
rec.video_fingerprint = {"hash": "abc"}
|
||||
rec.duplicate_rate = 50.0
|
||||
rec.duplicate_count = 5
|
||||
|
||||
rec.reset_for_retry()
|
||||
assert rec.status == "pending"
|
||||
assert rec.duplicate_rate is None
|
||||
assert rec.duplicate_count == 0
|
||||
assert rec.error_message == ""
|
||||
assert rec.segments == []
|
||||
assert rec.video_fingerprint is None
|
||||
|
||||
def test_reset_updates_timestamp(self):
|
||||
"""重置更新 updated_at."""
|
||||
rec = self._make_record()
|
||||
rec.mark_failed("error")
|
||||
old_updated = rec.updated_at
|
||||
rec.reset_for_retry()
|
||||
assert rec.updated_at >= old_updated
|
||||
|
||||
|
||||
class TestDuplicationRecordSegments:
|
||||
"""segments 列表相关测试."""
|
||||
|
||||
def _make_record(self):
|
||||
return DuplicationRecord.create("user1", "test.mp4", 1024, "oss://key")
|
||||
|
||||
def test_segments_default_empty(self):
|
||||
"""初始 segments 为空列表."""
|
||||
rec = self._make_record()
|
||||
assert rec.segments == []
|
||||
|
||||
def test_segments_independent_list(self):
|
||||
"""不同记录的 segments 是独立列表."""
|
||||
r1 = self._make_record()
|
||||
r2 = self._make_record()
|
||||
r1.segments.append("fake")
|
||||
assert len(r2.segments) == 0
|
||||
|
||||
def test_completed_with_multiple_segments(self):
|
||||
"""完成时带多个片段."""
|
||||
rec = self._make_record()
|
||||
segs = [
|
||||
DuplicateSegment.create(0, 1, "v1", "n1", 0, 1, 90.0),
|
||||
DuplicateSegment.create(2, 3, "v2", "n2", 5, 6, 70.0),
|
||||
DuplicateSegment.create(4, 5, "v3", "n3", 10, 11, 85.0),
|
||||
]
|
||||
rec.mark_completed(60.0, 3, segs)
|
||||
assert len(rec.segments) == 3
|
||||
assert rec.segments[0].similarity == 90.0
|
||||
assert rec.segments[1].matched_video_id == "v2"
|
||||
assert rec.segments[2].matched_video_name == "n3"
|
||||
@@ -1,296 +0,0 @@
|
||||
"""edit_plan 单测.
|
||||
|
||||
domain 层剪辑计划纯逻辑模块,0 外部依赖。
|
||||
覆盖:枚举常量、create工厂/校验、完整状态流转、重置。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.edit_plan import EditPlan, EditPlanStatus
|
||||
|
||||
|
||||
class TestEditPlanStatus:
|
||||
"""EditPlanStatus 枚举测试."""
|
||||
|
||||
def test_five_statuses(self):
|
||||
"""五种状态."""
|
||||
assert len(EditPlanStatus) == 5
|
||||
|
||||
def test_draft(self):
|
||||
assert EditPlanStatus.DRAFT == "draft"
|
||||
|
||||
def test_editing(self):
|
||||
assert EditPlanStatus.EDITING == "editing"
|
||||
|
||||
def test_rendering(self):
|
||||
assert EditPlanStatus.RENDERING == "rendering"
|
||||
|
||||
def test_completed(self):
|
||||
assert EditPlanStatus.COMPLETED == "completed"
|
||||
|
||||
def test_failed(self):
|
||||
assert EditPlanStatus.FAILED == "failed"
|
||||
|
||||
|
||||
class TestEditPlanCreate:
|
||||
"""EditPlan.create 工厂测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简创建."""
|
||||
plan = EditPlan.create(template_id="tpl1", name=" 我的计划 ")
|
||||
assert plan.template_id == "tpl1"
|
||||
assert plan.name == "我的计划"
|
||||
assert plan.status == EditPlanStatus.DRAFT
|
||||
assert plan.total_duration == 0.0
|
||||
assert plan.config == {}
|
||||
assert plan.project_id == ""
|
||||
assert plan.created_by_user_id == ""
|
||||
assert isinstance(plan.id, str)
|
||||
assert len(plan.id) > 0
|
||||
|
||||
def test_create_full(self):
|
||||
"""带全部字段."""
|
||||
plan = EditPlan.create(
|
||||
template_id="tpl1",
|
||||
name="口播视频计划",
|
||||
config={"bgm": "rock"},
|
||||
total_duration=120.5,
|
||||
source_edit_plan_id="src1",
|
||||
project_id="proj1",
|
||||
created_by_user_id="user1",
|
||||
)
|
||||
assert plan.name == "口播视频计划"
|
||||
assert plan.total_duration == 120.5
|
||||
assert plan.source_edit_plan_id == "src1"
|
||||
assert plan.project_id == "proj1"
|
||||
assert plan.created_by_user_id == "user1"
|
||||
assert plan.config == {"bgm": "rock"}
|
||||
|
||||
def test_create_empty_name(self):
|
||||
"""空名称无效."""
|
||||
try:
|
||||
EditPlan.create(template_id="t1", name="")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "名称" in str(e)
|
||||
|
||||
def test_create_whitespace_name(self):
|
||||
"""空白名称无效."""
|
||||
try:
|
||||
EditPlan.create(template_id="t1", name=" ")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "名称" in str(e)
|
||||
|
||||
def test_create_empty_template_id(self):
|
||||
"""空 template_id 无效."""
|
||||
try:
|
||||
EditPlan.create(template_id="", name="计划")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "template_id" in str(e)
|
||||
|
||||
def test_create_whitespace_template_id(self):
|
||||
"""空白 template_id 无效."""
|
||||
try:
|
||||
EditPlan.create(template_id=" ", name="计划")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "template_id" in str(e)
|
||||
|
||||
def test_config_none_defaults_empty(self):
|
||||
"""config=None 默认为空 dict."""
|
||||
plan = EditPlan.create(template_id="t1", name="p", config=None)
|
||||
assert plan.config == {}
|
||||
|
||||
def test_unique_id(self):
|
||||
"""不同计划 id 不同."""
|
||||
p1 = EditPlan.create("t", "p1")
|
||||
p2 = EditPlan.create("t", "p2")
|
||||
assert p1.id != p2.id
|
||||
|
||||
def test_has_timestamps(self):
|
||||
"""有创建和更新时间."""
|
||||
plan = EditPlan.create("t", "p")
|
||||
assert plan.created_at is not None
|
||||
assert plan.updated_at is not None
|
||||
|
||||
def test_strips_string_fields(self):
|
||||
"""字符串字段 strip."""
|
||||
plan = EditPlan.create(
|
||||
template_id=" t1 ",
|
||||
name=" n ",
|
||||
source_edit_plan_id=" s ",
|
||||
project_id=" p ",
|
||||
created_by_user_id=" u ",
|
||||
)
|
||||
assert plan.template_id == "t1"
|
||||
assert plan.name == "n"
|
||||
assert plan.source_edit_plan_id == "s"
|
||||
assert plan.project_id == "p"
|
||||
assert plan.created_by_user_id == "u"
|
||||
|
||||
|
||||
class TestEditPlanStatusFlow:
|
||||
"""状态流转测试."""
|
||||
|
||||
def _make_plan(self):
|
||||
return EditPlan.create(template_id="t1", name="测试计划")
|
||||
|
||||
def test_initial_status_draft(self):
|
||||
"""初始状态 draft."""
|
||||
plan = self._make_plan()
|
||||
assert plan.status == EditPlanStatus.DRAFT
|
||||
|
||||
def test_draft_to_editing(self):
|
||||
"""draft -> editing."""
|
||||
plan = self._make_plan()
|
||||
old = plan.updated_at
|
||||
plan.start_editing()
|
||||
assert plan.status == EditPlanStatus.EDITING
|
||||
assert plan.updated_at >= old
|
||||
|
||||
def test_editing_to_rendering(self):
|
||||
"""editing -> rendering."""
|
||||
plan = self._make_plan()
|
||||
plan.start_editing()
|
||||
plan.start_rendering()
|
||||
assert plan.status == EditPlanStatus.RENDERING
|
||||
|
||||
def test_rendering_to_completed(self):
|
||||
"""rendering -> completed."""
|
||||
plan = self._make_plan()
|
||||
plan.start_editing()
|
||||
plan.start_rendering()
|
||||
plan.mark_completed()
|
||||
assert plan.status == EditPlanStatus.COMPLETED
|
||||
|
||||
def test_rendering_to_failed(self):
|
||||
"""rendering -> failed."""
|
||||
plan = self._make_plan()
|
||||
plan.start_editing()
|
||||
plan.start_rendering()
|
||||
plan.mark_failed()
|
||||
assert plan.status == EditPlanStatus.FAILED
|
||||
|
||||
def test_failed_to_draft_reset(self):
|
||||
"""failed -> draft 重置."""
|
||||
plan = self._make_plan()
|
||||
plan.start_editing()
|
||||
plan.start_rendering()
|
||||
plan.mark_failed()
|
||||
plan.reset_to_draft()
|
||||
assert plan.status == EditPlanStatus.DRAFT
|
||||
|
||||
def test_cannot_editing_from_completed(self):
|
||||
"""completed 不能 start_editing."""
|
||||
plan = self._make_plan()
|
||||
plan.start_editing()
|
||||
plan.start_rendering()
|
||||
plan.mark_completed()
|
||||
try:
|
||||
plan.start_editing()
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "draft" in str(e).lower()
|
||||
|
||||
def test_cannot_rendering_from_draft(self):
|
||||
"""draft 不能直接 start_rendering."""
|
||||
plan = self._make_plan()
|
||||
try:
|
||||
plan.start_rendering()
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "editing" in str(e).lower()
|
||||
|
||||
def test_cannot_complete_from_editing(self):
|
||||
"""editing 不能直接完成."""
|
||||
plan = self._make_plan()
|
||||
plan.start_editing()
|
||||
try:
|
||||
plan.mark_completed()
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "rendering" in str(e).lower()
|
||||
|
||||
def test_cannot_fail_from_editing(self):
|
||||
"""editing 不能直接失败."""
|
||||
plan = self._make_plan()
|
||||
plan.start_editing()
|
||||
try:
|
||||
plan.mark_failed()
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "rendering" in str(e).lower()
|
||||
|
||||
def test_cannot_reset_from_draft(self):
|
||||
"""draft 不能重置."""
|
||||
plan = self._make_plan()
|
||||
try:
|
||||
plan.reset_to_draft()
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "failed" in str(e).lower()
|
||||
|
||||
def test_cannot_reset_from_completed(self):
|
||||
"""completed 不能重置."""
|
||||
plan = self._make_plan()
|
||||
plan.start_editing()
|
||||
plan.start_rendering()
|
||||
plan.mark_completed()
|
||||
try:
|
||||
plan.reset_to_draft()
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "failed" in str(e).lower()
|
||||
|
||||
def test_full_happy_path(self):
|
||||
"""完整成功路径."""
|
||||
plan = self._make_plan()
|
||||
assert plan.status == EditPlanStatus.DRAFT
|
||||
plan.start_editing()
|
||||
assert plan.status == EditPlanStatus.EDITING
|
||||
plan.start_rendering()
|
||||
assert plan.status == EditPlanStatus.RENDERING
|
||||
plan.mark_completed()
|
||||
assert plan.status == EditPlanStatus.COMPLETED
|
||||
|
||||
def test_failed_reset_retry(self):
|
||||
"""失败后重置重试完整路径."""
|
||||
plan = self._make_plan()
|
||||
plan.start_editing()
|
||||
plan.start_rendering()
|
||||
plan.mark_failed()
|
||||
assert plan.status == EditPlanStatus.FAILED
|
||||
plan.reset_to_draft()
|
||||
assert plan.status == EditPlanStatus.DRAFT
|
||||
# 重新走一遍
|
||||
plan.start_editing()
|
||||
plan.start_rendering()
|
||||
plan.mark_completed()
|
||||
assert plan.status == EditPlanStatus.COMPLETED
|
||||
|
||||
def test_each_transition_updates_timestamp(self):
|
||||
"""每次状态变更都更新 updated_at."""
|
||||
plan = self._make_plan()
|
||||
timestamps = [plan.updated_at]
|
||||
plan.start_editing()
|
||||
timestamps.append(plan.updated_at)
|
||||
plan.start_rendering()
|
||||
timestamps.append(plan.updated_at)
|
||||
plan.mark_completed()
|
||||
timestamps.append(plan.updated_at)
|
||||
# 单调递增
|
||||
for i in range(1, len(timestamps)):
|
||||
assert timestamps[i] >= timestamps[i - 1]
|
||||
|
||||
|
||||
class TestEditPlanConfig:
|
||||
"""config 独立性测试."""
|
||||
|
||||
def test_config_independent(self):
|
||||
"""不同计划的 config 独立."""
|
||||
p1 = EditPlan.create(template_id="t", name="p1")
|
||||
p2 = EditPlan.create(template_id="t", name="p2")
|
||||
p1.config["key"] = "value"
|
||||
assert "key" not in p2.config
|
||||
@@ -1,362 +0,0 @@
|
||||
"""edit_plan_clip 单测.
|
||||
|
||||
domain 层剪辑计划片段纯逻辑模块,0 外部依赖。
|
||||
覆盖:枚举常量、create工厂/校验、素材分配、状态流转、属性计算。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.edit_plan_clip import EditPlanClip, EditPlanClipStatus
|
||||
|
||||
|
||||
class TestEditPlanClipStatus:
|
||||
"""EditPlanClipStatus 枚举测试."""
|
||||
|
||||
def test_four_statuses(self):
|
||||
"""四种状态."""
|
||||
assert len(EditPlanClipStatus) == 4
|
||||
|
||||
def test_pending(self):
|
||||
"""pending 状态."""
|
||||
assert EditPlanClipStatus.PENDING == "pending"
|
||||
|
||||
def test_ready(self):
|
||||
"""ready 状态."""
|
||||
assert EditPlanClipStatus.READY == "ready"
|
||||
|
||||
def test_rendered(self):
|
||||
"""rendered 状态."""
|
||||
assert EditPlanClipStatus.RENDERED == "rendered"
|
||||
|
||||
def test_failed(self):
|
||||
"""failed 状态."""
|
||||
assert EditPlanClipStatus.FAILED == "failed"
|
||||
|
||||
def test_is_string(self):
|
||||
"""枚举值是字符串."""
|
||||
for status in EditPlanClipStatus:
|
||||
assert isinstance(status.value, str)
|
||||
assert len(status.value) > 0
|
||||
|
||||
def test_str_compatible(self):
|
||||
"""StrEnum 可与字符串比较."""
|
||||
assert EditPlanClipStatus.PENDING == "pending"
|
||||
assert EditPlanClipStatus.READY + "" == "ready"
|
||||
|
||||
|
||||
class TestEditPlanClipCreate:
|
||||
"""EditPlanClip.create 工厂方法测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简创建."""
|
||||
clip = EditPlanClip.create(plan_id="plan1", clip_type="video", order=0)
|
||||
assert clip.plan_id == "plan1"
|
||||
assert clip.clip_type == "video"
|
||||
assert clip.order == 0
|
||||
assert clip.status == EditPlanClipStatus.PENDING
|
||||
assert clip.template_clip_config_id == ""
|
||||
assert clip.asset_id == ""
|
||||
assert clip.text_content == ""
|
||||
assert clip.start_time == 0.0
|
||||
assert clip.duration == 0.0
|
||||
assert clip.transition_effect == "cut"
|
||||
assert clip.config == {}
|
||||
assert isinstance(clip.id, str)
|
||||
assert len(clip.id) > 0
|
||||
|
||||
def test_create_with_all_fields(self):
|
||||
"""带全部字段创建."""
|
||||
clip = EditPlanClip.create(
|
||||
plan_id="plan1",
|
||||
clip_type="video",
|
||||
order=2,
|
||||
template_clip_config_id="tpl1",
|
||||
asset_id="asset1",
|
||||
text_content=" 你好世界 ",
|
||||
start_time=10.5,
|
||||
duration=5.0,
|
||||
transition_effect="fade",
|
||||
config={"key": "value"},
|
||||
)
|
||||
assert clip.order == 2
|
||||
assert clip.template_clip_config_id == "tpl1"
|
||||
assert clip.asset_id == "asset1"
|
||||
assert clip.text_content == "你好世界" # strip了
|
||||
assert clip.start_time == 10.5
|
||||
assert clip.duration == 5.0
|
||||
assert clip.transition_effect == "fade"
|
||||
assert clip.config == {"key": "value"}
|
||||
|
||||
def test_create_strips_ids(self):
|
||||
"""plan_id 和 clip_type 会 strip."""
|
||||
clip = EditPlanClip.create(plan_id=" plan1 ", clip_type=" video ", order=0)
|
||||
assert clip.plan_id == "plan1"
|
||||
assert clip.clip_type == "video"
|
||||
|
||||
def test_create_empty_plan_id(self):
|
||||
"""空 plan_id 无效."""
|
||||
try:
|
||||
EditPlanClip.create(plan_id="", clip_type="video", order=0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "plan_id" in str(e)
|
||||
|
||||
def test_create_whitespace_plan_id(self):
|
||||
"""纯空白 plan_id 无效."""
|
||||
try:
|
||||
EditPlanClip.create(plan_id=" ", clip_type="video", order=0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "plan_id" in str(e)
|
||||
|
||||
def test_create_empty_clip_type(self):
|
||||
"""空 clip_type 无效."""
|
||||
try:
|
||||
EditPlanClip.create(plan_id="p1", clip_type="", order=0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "clip_type" in str(e)
|
||||
|
||||
def test_create_whitespace_clip_type(self):
|
||||
"""纯空白 clip_type 无效."""
|
||||
try:
|
||||
EditPlanClip.create(plan_id="p1", clip_type=" ", order=0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "clip_type" in str(e)
|
||||
|
||||
def test_create_negative_start_time(self):
|
||||
"""start_time 为负无效."""
|
||||
try:
|
||||
EditPlanClip.create(plan_id="p1", clip_type="v", order=0, start_time=-1.0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "start_time" in str(e)
|
||||
|
||||
def test_create_negative_duration(self):
|
||||
"""duration 为负无效."""
|
||||
try:
|
||||
EditPlanClip.create(plan_id="p1", clip_type="v", order=0, duration=-1.0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "duration" in str(e)
|
||||
|
||||
def test_create_zero_duration_valid(self):
|
||||
"""duration 为 0 合法."""
|
||||
clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, duration=0.0)
|
||||
assert clip.duration == 0.0
|
||||
|
||||
def test_create_empty_transition_defaults_to_cut(self):
|
||||
"""空 transition_effect 默认 cut."""
|
||||
clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, transition_effect="")
|
||||
assert clip.transition_effect == "cut"
|
||||
|
||||
def test_create_whitespace_transition_defaults_to_cut(self):
|
||||
"""空白 transition_effect 默认 cut."""
|
||||
clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, transition_effect=" ")
|
||||
assert clip.transition_effect == "cut"
|
||||
|
||||
def test_create_config_none_defaults_empty_dict(self):
|
||||
"""config=None 默认为空 dict."""
|
||||
clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, config=None)
|
||||
assert clip.config == {}
|
||||
|
||||
def test_create_unique_id(self):
|
||||
"""不同 clip id 不同."""
|
||||
c1 = EditPlanClip.create("p1", "v", 0)
|
||||
c2 = EditPlanClip.create("p1", "v", 0)
|
||||
assert c1.id != c2.id
|
||||
|
||||
def test_create_has_timestamps(self):
|
||||
"""有创建和更新时间."""
|
||||
clip = EditPlanClip.create("p1", "v", 0)
|
||||
assert clip.created_at is not None
|
||||
assert clip.updated_at is not None
|
||||
|
||||
def test_create_negative_order_valid(self):
|
||||
"""order 可以为负(表示排序位置)."""
|
||||
clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=-1)
|
||||
assert clip.order == -1
|
||||
|
||||
|
||||
class TestEditPlanClipAssignAsset:
|
||||
"""素材分配测试."""
|
||||
|
||||
def _make_clip(self):
|
||||
return EditPlanClip.create(plan_id="p1", clip_type="video", order=0)
|
||||
|
||||
def test_assign_asset(self):
|
||||
"""正常分配素材."""
|
||||
clip = self._make_clip()
|
||||
old_updated = clip.updated_at
|
||||
clip.assign_asset("asset123")
|
||||
assert clip.asset_id == "asset123"
|
||||
assert clip.updated_at >= old_updated
|
||||
|
||||
def test_assign_asset_strips(self):
|
||||
"""asset_id 会 strip."""
|
||||
clip = self._make_clip()
|
||||
clip.assign_asset(" asset123 ")
|
||||
assert clip.asset_id == "asset123"
|
||||
|
||||
def test_assign_asset_empty(self):
|
||||
"""空 asset_id 无效."""
|
||||
clip = self._make_clip()
|
||||
try:
|
||||
clip.assign_asset("")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "asset_id" in str(e)
|
||||
|
||||
def test_assign_asset_whitespace(self):
|
||||
"""纯空白 asset_id 无效."""
|
||||
clip = self._make_clip()
|
||||
try:
|
||||
clip.assign_asset(" ")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "asset_id" in str(e)
|
||||
|
||||
def test_has_asset_false_initially(self):
|
||||
"""初始无素材."""
|
||||
clip = self._make_clip()
|
||||
assert clip.has_asset is False
|
||||
|
||||
def test_has_asset_true_after_assign(self):
|
||||
"""分配后有素材."""
|
||||
clip = self._make_clip()
|
||||
clip.assign_asset("a1")
|
||||
assert clip.has_asset is True
|
||||
|
||||
|
||||
class TestEditPlanClipStatusFlow:
|
||||
"""状态流转测试."""
|
||||
|
||||
def _make_clip(self):
|
||||
return EditPlanClip.create(plan_id="p1", clip_type="video", order=0)
|
||||
|
||||
def test_initial_status_pending(self):
|
||||
"""初始状态 pending."""
|
||||
clip = self._make_clip()
|
||||
assert clip.status == EditPlanClipStatus.PENDING
|
||||
|
||||
def test_pending_to_ready(self):
|
||||
"""pending -> ready."""
|
||||
clip = self._make_clip()
|
||||
clip.mark_ready()
|
||||
assert clip.status == EditPlanClipStatus.READY
|
||||
|
||||
def test_ready_to_rendered(self):
|
||||
"""ready -> rendered."""
|
||||
clip = self._make_clip()
|
||||
clip.mark_ready()
|
||||
clip.mark_rendered()
|
||||
assert clip.status == EditPlanClipStatus.RENDERED
|
||||
|
||||
def test_ready_to_failed(self):
|
||||
"""ready -> failed."""
|
||||
clip = self._make_clip()
|
||||
clip.mark_ready()
|
||||
clip.mark_failed()
|
||||
assert clip.status == EditPlanClipStatus.FAILED
|
||||
|
||||
def test_cannot_ready_from_rendered(self):
|
||||
"""rendered 状态不能再 mark_ready."""
|
||||
clip = self._make_clip()
|
||||
clip.mark_ready()
|
||||
clip.mark_rendered()
|
||||
try:
|
||||
clip.mark_ready()
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "pending" in str(e).lower()
|
||||
|
||||
def test_cannot_ready_from_failed(self):
|
||||
"""failed 状态不能 mark_ready."""
|
||||
clip = self._make_clip()
|
||||
clip.mark_ready()
|
||||
clip.mark_failed()
|
||||
try:
|
||||
clip.mark_ready()
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "pending" in str(e).lower()
|
||||
|
||||
def test_cannot_render_from_pending(self):
|
||||
"""pending 不能直接 mark_rendered."""
|
||||
clip = self._make_clip()
|
||||
try:
|
||||
clip.mark_rendered()
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "ready" in str(e).lower()
|
||||
|
||||
def test_cannot_failed_from_pending(self):
|
||||
"""pending 不能直接 mark_failed."""
|
||||
clip = self._make_clip()
|
||||
try:
|
||||
clip.mark_failed()
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "ready" in str(e).lower()
|
||||
|
||||
def test_status_change_updates_timestamp(self):
|
||||
"""状态变更更新 updated_at."""
|
||||
clip = self._make_clip()
|
||||
old_updated = clip.updated_at
|
||||
clip.mark_ready()
|
||||
assert clip.updated_at >= old_updated
|
||||
|
||||
|
||||
class TestEditPlanClipProperties:
|
||||
"""属性计算测试."""
|
||||
|
||||
def test_end_time(self):
|
||||
"""end_time = start_time + duration."""
|
||||
clip = EditPlanClip.create(
|
||||
plan_id="p1",
|
||||
clip_type="v",
|
||||
order=0,
|
||||
start_time=10.0,
|
||||
duration=5.5,
|
||||
)
|
||||
assert clip.end_time == 15.5
|
||||
|
||||
def test_end_time_zero_duration(self):
|
||||
"""零时长 end_time = start_time."""
|
||||
clip = EditPlanClip.create(
|
||||
plan_id="p1",
|
||||
clip_type="v",
|
||||
order=0,
|
||||
start_time=10.0,
|
||||
duration=0.0,
|
||||
)
|
||||
assert clip.end_time == 10.0
|
||||
|
||||
def test_end_time_zero_start(self):
|
||||
"""零起点 end_time = duration."""
|
||||
clip = EditPlanClip.create(
|
||||
plan_id="p1",
|
||||
clip_type="v",
|
||||
order=0,
|
||||
start_time=0.0,
|
||||
duration=7.0,
|
||||
)
|
||||
assert clip.end_time == 7.0
|
||||
|
||||
def test_has_asset_empty_string(self):
|
||||
"""空字符串无素材."""
|
||||
clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, asset_id="")
|
||||
assert clip.has_asset is False
|
||||
|
||||
def test_has_asset_with_value(self):
|
||||
"""有值则有素材."""
|
||||
clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, asset_id="a1")
|
||||
assert clip.has_asset is True
|
||||
|
||||
def test_config_independent_between_clips(self):
|
||||
"""不同 clip 的 config 独立."""
|
||||
c1 = EditPlanClip.create(plan_id="p1", clip_type="v", order=0)
|
||||
c2 = EditPlanClip.create(plan_id="p1", clip_type="v", order=1)
|
||||
c1.config["key"] = "value"
|
||||
assert "key" not in c2.config
|
||||
@@ -1,520 +0,0 @@
|
||||
"""Domain entities 单元测试 - wave161
|
||||
|
||||
覆盖:AssetLibraryKind / IngestJobStatus / AssetStatus / ClassificationStatus 枚举
|
||||
User / Project / AssetLibrary / Asset / IngestJob 领域模型
|
||||
"""
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import pytest
|
||||
|
||||
from packages.domain.entities import (
|
||||
Asset,
|
||||
AssetLibrary,
|
||||
AssetLibraryKind,
|
||||
AssetStatus,
|
||||
ClassificationStatus,
|
||||
IngestJob,
|
||||
IngestJobStatus,
|
||||
Project,
|
||||
User,
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 枚举测试
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestAssetLibraryKind:
|
||||
def test_values(self):
|
||||
assert AssetLibraryKind.VIDEO == "video"
|
||||
assert AssetLibraryKind.VOICE == "voice"
|
||||
assert AssetLibraryKind.IMAGE == "image"
|
||||
|
||||
def test_is_str_enum(self):
|
||||
assert isinstance(AssetLibraryKind.VIDEO, str)
|
||||
assert AssetLibraryKind.VIDEO == "video"
|
||||
|
||||
def test_members_count(self):
|
||||
assert len(AssetLibraryKind) == 3
|
||||
|
||||
|
||||
class TestIngestJobStatus:
|
||||
def test_values(self):
|
||||
assert IngestJobStatus.PENDING == "pending"
|
||||
assert IngestJobStatus.PROCESSING == "processing"
|
||||
assert IngestJobStatus.COMPLETED == "completed"
|
||||
assert IngestJobStatus.FAILED == "failed"
|
||||
|
||||
def test_members_count(self):
|
||||
assert len(IngestJobStatus) == 4
|
||||
|
||||
|
||||
class TestAssetStatus:
|
||||
def test_values(self):
|
||||
assert AssetStatus.UPLOADING == "uploading"
|
||||
assert AssetStatus.READY == "ready"
|
||||
assert AssetStatus.PROCESSING == "processing"
|
||||
assert AssetStatus.ERROR == "error"
|
||||
assert AssetStatus.DELETED == "deleted"
|
||||
|
||||
def test_members_count(self):
|
||||
assert len(AssetStatus) == 5
|
||||
|
||||
|
||||
class TestClassificationStatus:
|
||||
def test_values(self):
|
||||
assert ClassificationStatus.PENDING == "pending"
|
||||
assert ClassificationStatus.PROCESSING == "processing"
|
||||
assert ClassificationStatus.COMPLETED == "completed"
|
||||
assert ClassificationStatus.FAILED == "failed"
|
||||
|
||||
def test_members_count(self):
|
||||
assert len(ClassificationStatus) == 4
|
||||
|
||||
|
||||
# ============================================================
|
||||
# User 测试
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestUser:
|
||||
def test_create_minimal(self):
|
||||
user = User(id="u1", email="test@example.com", display_name="Test")
|
||||
assert user.id == "u1"
|
||||
assert user.email == "test@example.com"
|
||||
assert user.display_name == "Test"
|
||||
|
||||
def test_default_values(self):
|
||||
user = User(id="u1", email="t@e.com", display_name="T")
|
||||
assert user.username == ""
|
||||
assert user.password_hash == ""
|
||||
assert user.email_verified is False
|
||||
assert user.subscription_plan == "free"
|
||||
assert user.subscription_status == "active"
|
||||
assert user.max_projects == 3
|
||||
assert user.max_storage_gb == 10
|
||||
assert user.used_storage_gb == 0.0
|
||||
assert user.is_admin is False
|
||||
assert user.wechat_openid is None
|
||||
assert user.wechat_unionid is None
|
||||
|
||||
def test_has_created_at(self):
|
||||
before = datetime.now(timezone.utc)
|
||||
user = User(id="u1", email="t@e.com", display_name="T")
|
||||
after = datetime.now(timezone.utc)
|
||||
assert before <= user.created_at <= after
|
||||
|
||||
def test_full_fields(self):
|
||||
user = User(
|
||||
id="u1",
|
||||
email="admin@example.com",
|
||||
display_name="Admin",
|
||||
username="admin",
|
||||
is_admin=True,
|
||||
subscription_plan="enterprise",
|
||||
max_projects=100,
|
||||
max_storage_gb=1000,
|
||||
)
|
||||
assert user.username == "admin"
|
||||
assert user.is_admin is True
|
||||
assert user.subscription_plan == "enterprise"
|
||||
assert user.max_projects == 100
|
||||
assert user.max_storage_gb == 1000
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Project 测试
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestProjectCreate:
|
||||
def test_create_minimal(self):
|
||||
p = Project.create(owner_user_id="u1", name="My Project")
|
||||
assert p.id
|
||||
assert len(p.id) == 32 # uuid4 hex
|
||||
assert p.owner_user_id == "u1"
|
||||
assert p.name == "My Project"
|
||||
assert p.description == ""
|
||||
assert p.shared_users == []
|
||||
|
||||
def test_create_with_description(self):
|
||||
p = Project.create(owner_user_id="u1", name="P", description=" desc ")
|
||||
assert p.description == "desc" # strip
|
||||
|
||||
def test_create_strips_name(self):
|
||||
p = Project.create(owner_user_id="u1", name=" My Project ")
|
||||
assert p.name == "My Project"
|
||||
|
||||
def test_create_empty_name_raises(self):
|
||||
with pytest.raises(ValueError, match="项目名称不能为空"):
|
||||
Project.create(owner_user_id="u1", name="")
|
||||
|
||||
def test_create_whitespace_name_raises(self):
|
||||
with pytest.raises(ValueError, match="项目名称不能为空"):
|
||||
Project.create(owner_user_id="u1", name=" ")
|
||||
|
||||
def test_create_unique_ids(self):
|
||||
p1 = Project.create(owner_user_id="u1", name="P1")
|
||||
p2 = Project.create(owner_user_id="u1", name="P2")
|
||||
assert p1.id != p2.id
|
||||
|
||||
def test_create_has_timestamp(self):
|
||||
before = datetime.now(timezone.utc)
|
||||
p = Project.create(owner_user_id="u1", name="P")
|
||||
after = datetime.now(timezone.utc)
|
||||
assert before <= p.created_at <= after
|
||||
|
||||
|
||||
class TestProjectAccess:
|
||||
def test_is_owner_true(self):
|
||||
p = Project.create(owner_user_id="u1", name="P")
|
||||
assert p.is_owner("u1") is True
|
||||
|
||||
def test_is_owner_false(self):
|
||||
p = Project.create(owner_user_id="u1", name="P")
|
||||
assert p.is_owner("u2") is False
|
||||
|
||||
def test_is_shared_with_true(self):
|
||||
p = Project.create(owner_user_id="u1", name="P")
|
||||
p.shared_users = ["u2", "u3"]
|
||||
assert p.is_shared_with("u2") is True
|
||||
|
||||
def test_is_shared_with_false(self):
|
||||
p = Project.create(owner_user_id="u1", name="P")
|
||||
p.shared_users = ["u2"]
|
||||
assert p.is_shared_with("u3") is False
|
||||
|
||||
def test_is_shared_with_empty(self):
|
||||
p = Project.create(owner_user_id="u1", name="P")
|
||||
assert p.is_shared_with("u2") is False
|
||||
|
||||
def test_can_access_owner(self):
|
||||
p = Project.create(owner_user_id="u1", name="P")
|
||||
assert p.can_access("u1") is True
|
||||
|
||||
def test_can_access_shared(self):
|
||||
p = Project.create(owner_user_id="u1", name="P")
|
||||
p.shared_users = ["u2"]
|
||||
assert p.can_access("u2") is True
|
||||
|
||||
def test_cannot_access_other(self):
|
||||
p = Project.create(owner_user_id="u1", name="P")
|
||||
assert p.can_access("u3") is False
|
||||
|
||||
|
||||
# ============================================================
|
||||
# AssetLibrary 测试
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestAssetLibraryCreate:
|
||||
def test_create_minimal(self):
|
||||
lib = AssetLibrary.create(project_id="p1", name="Videos", kind=AssetLibraryKind.VIDEO)
|
||||
assert lib.id
|
||||
assert len(lib.id) == 32
|
||||
assert lib.project_id == "p1"
|
||||
assert lib.name == "Videos"
|
||||
assert lib.kind == AssetLibraryKind.VIDEO
|
||||
assert lib.asset_count == 0
|
||||
assert lib.total_size == 0
|
||||
|
||||
def test_create_strips_name(self):
|
||||
lib = AssetLibrary.create(project_id="p1", name=" Voices ", kind=AssetLibraryKind.VOICE)
|
||||
assert lib.name == "Voices"
|
||||
|
||||
def test_create_empty_name_raises(self):
|
||||
with pytest.raises(ValueError, match="素材库名称不能为空"):
|
||||
AssetLibrary.create(project_id="p1", name="", kind=AssetLibraryKind.IMAGE)
|
||||
|
||||
def test_create_whitespace_name_raises(self):
|
||||
with pytest.raises(ValueError, match="素材库名称不能为空"):
|
||||
AssetLibrary.create(project_id="p1", name=" ", kind=AssetLibraryKind.VIDEO)
|
||||
|
||||
def test_create_unique_ids(self):
|
||||
lib1 = AssetLibrary.create("p1", "L1", AssetLibraryKind.VIDEO)
|
||||
lib2 = AssetLibrary.create("p1", "L2", AssetLibraryKind.IMAGE)
|
||||
assert lib1.id != lib2.id
|
||||
|
||||
def test_create_has_timestamps(self):
|
||||
before = datetime.now(timezone.utc)
|
||||
lib = AssetLibrary.create("p1", "L", AssetLibraryKind.VIDEO)
|
||||
after = datetime.now(timezone.utc)
|
||||
assert before <= lib.created_at <= after
|
||||
assert before <= lib.updated_at <= after
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Asset 测试
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestAssetCreate:
|
||||
def test_create_minimal(self):
|
||||
asset = Asset.create(
|
||||
project_id="p1",
|
||||
library_id="lib1",
|
||||
name="video.mp4",
|
||||
storage_key="uploads/v1.mp4",
|
||||
mime_type="video/mp4",
|
||||
)
|
||||
assert asset.id
|
||||
assert len(asset.id) == 32
|
||||
assert asset.project_id == "p1"
|
||||
assert asset.library_id == "lib1"
|
||||
assert asset.name == "video.mp4"
|
||||
assert asset.storage_key == "uploads/v1.mp4"
|
||||
assert asset.mime_type == "video/mp4"
|
||||
assert asset.file_size == 0
|
||||
assert asset.status == AssetStatus.UPLOADING
|
||||
assert asset.classification_status == ClassificationStatus.PENDING
|
||||
assert asset.tag_ids == []
|
||||
assert asset.metadata == {}
|
||||
|
||||
def test_create_full(self):
|
||||
asset = Asset.create(
|
||||
project_id="p1",
|
||||
library_id="lib1",
|
||||
name=" clip.mov ",
|
||||
storage_key=" s3://bucket/clip.mov ",
|
||||
mime_type=" video/quicktime ",
|
||||
metadata={"resolution": "1080p"},
|
||||
file_size=1024000,
|
||||
thumbnail_url="https://img/thumb.jpg",
|
||||
duration=30.5,
|
||||
width=1920,
|
||||
height=1080,
|
||||
fps=29.97,
|
||||
codec="h264",
|
||||
status=AssetStatus.READY,
|
||||
classification_status=ClassificationStatus.COMPLETED,
|
||||
quality_score=0.95,
|
||||
uploaded_by_user_id=" u1 ",
|
||||
file_hash=" abc123 ",
|
||||
)
|
||||
assert asset.name == "clip.mov" # stripped
|
||||
assert asset.storage_key == "s3://bucket/clip.mov"
|
||||
assert asset.mime_type == "video/quicktime"
|
||||
assert asset.file_size == 1024000
|
||||
assert asset.status == AssetStatus.READY
|
||||
assert asset.classification_status == ClassificationStatus.COMPLETED
|
||||
assert asset.quality_score == 0.95
|
||||
assert asset.uploaded_by_user_id == "u1"
|
||||
assert asset.file_hash == "abc123"
|
||||
assert asset.metadata == {"resolution": "1080p"}
|
||||
|
||||
def test_create_empty_name_raises(self):
|
||||
with pytest.raises(ValueError, match="素材名称不能为空"):
|
||||
Asset.create(
|
||||
project_id="p1",
|
||||
library_id="lib1",
|
||||
name="",
|
||||
storage_key="k",
|
||||
mime_type="video/mp4",
|
||||
)
|
||||
|
||||
def test_create_whitespace_name_raises(self):
|
||||
with pytest.raises(ValueError, match="素材名称不能为空"):
|
||||
Asset.create(
|
||||
project_id="p1",
|
||||
library_id="lib1",
|
||||
name=" ",
|
||||
storage_key="k",
|
||||
mime_type="video/mp4",
|
||||
)
|
||||
|
||||
def test_create_empty_storage_key_raises(self):
|
||||
with pytest.raises(ValueError, match="storage_key 不能为空"):
|
||||
Asset.create(
|
||||
project_id="p1",
|
||||
library_id="lib1",
|
||||
name="v.mp4",
|
||||
storage_key="",
|
||||
mime_type="video/mp4",
|
||||
)
|
||||
|
||||
def test_create_whitespace_storage_key_raises(self):
|
||||
with pytest.raises(ValueError, match="storage_key 不能为空"):
|
||||
Asset.create(
|
||||
project_id="p1",
|
||||
library_id="lib1",
|
||||
name="v.mp4",
|
||||
storage_key=" ",
|
||||
mime_type="video/mp4",
|
||||
)
|
||||
|
||||
def test_create_empty_mime_type_raises(self):
|
||||
with pytest.raises(ValueError, match="mime_type 不能为空"):
|
||||
Asset.create(
|
||||
project_id="p1",
|
||||
library_id="lib1",
|
||||
name="v.mp4",
|
||||
storage_key="k",
|
||||
mime_type="",
|
||||
)
|
||||
|
||||
def test_create_metadata_none_defaults_empty_dict(self):
|
||||
asset = Asset.create(
|
||||
project_id="p1",
|
||||
library_id="lib1",
|
||||
name="v.mp4",
|
||||
storage_key="k",
|
||||
mime_type="video/mp4",
|
||||
metadata=None,
|
||||
)
|
||||
assert asset.metadata == {}
|
||||
|
||||
def test_create_unique_ids(self):
|
||||
a1 = Asset.create("p1", "lib1", "a.mp4", "k1", "video/mp4")
|
||||
a2 = Asset.create("p1", "lib1", "b.mp4", "k2", "video/mp4")
|
||||
assert a1.id != a2.id
|
||||
|
||||
def test_create_has_timestamps(self):
|
||||
before = datetime.now(timezone.utc)
|
||||
a = Asset.create("p1", "lib1", "v.mp4", "k", "video/mp4")
|
||||
after = datetime.now(timezone.utc)
|
||||
assert before <= a.created_at <= after
|
||||
assert before <= a.updated_at <= after
|
||||
|
||||
|
||||
class TestAssetTags:
|
||||
def test_add_tag(self):
|
||||
a = Asset.create("p1", "lib1", "v.mp4", "k", "video/mp4")
|
||||
a.add_tag("tag1")
|
||||
assert "tag1" in a.tag_ids
|
||||
assert len(a.tag_ids) == 1
|
||||
|
||||
def test_add_tag_strips(self):
|
||||
a = Asset.create("p1", "lib1", "v.mp4", "k", "video/mp4")
|
||||
a.add_tag(" tag1 ")
|
||||
assert a.tag_ids == ["tag1"]
|
||||
|
||||
def test_add_tag_deduplicates(self):
|
||||
a = Asset.create("p1", "lib1", "v.mp4", "k", "video/mp4")
|
||||
a.add_tag("tag1")
|
||||
a.add_tag("tag1")
|
||||
assert a.tag_ids == ["tag1"]
|
||||
|
||||
def test_add_empty_tag_raises(self):
|
||||
a = Asset.create("p1", "lib1", "v.mp4", "k", "video/mp4")
|
||||
with pytest.raises(ValueError, match="标签 ID 不能为空"):
|
||||
a.add_tag("")
|
||||
|
||||
def test_add_whitespace_tag_raises(self):
|
||||
a = Asset.create("p1", "lib1", "v.mp4", "k", "video/mp4")
|
||||
with pytest.raises(ValueError, match="标签 ID 不能为空"):
|
||||
a.add_tag(" ")
|
||||
|
||||
def test_add_tag_updates_updated_at(self):
|
||||
a = Asset.create("p1", "lib1", "v.mp4", "k", "video/mp4")
|
||||
old_updated = a.updated_at
|
||||
# 确保时间差
|
||||
import time
|
||||
|
||||
time.sleep(0.001)
|
||||
a.add_tag("tag1")
|
||||
assert a.updated_at >= old_updated
|
||||
|
||||
def test_add_duplicate_tag_no_updated_at_change(self):
|
||||
a = Asset.create("p1", "lib1", "v.mp4", "k", "video/mp4")
|
||||
a.add_tag("tag1")
|
||||
old_updated = a.updated_at
|
||||
a.add_tag("tag1") # 重复
|
||||
assert a.updated_at == old_updated
|
||||
|
||||
def test_remove_tag(self):
|
||||
a = Asset.create("p1", "lib1", "v.mp4", "k", "video/mp4")
|
||||
a.add_tag("tag1")
|
||||
a.add_tag("tag2")
|
||||
a.remove_tag("tag1")
|
||||
assert a.tag_ids == ["tag2"]
|
||||
|
||||
def test_remove_tag_strips(self):
|
||||
a = Asset.create("p1", "lib1", "v.mp4", "k", "video/mp4")
|
||||
a.add_tag("tag1")
|
||||
a.remove_tag(" tag1 ")
|
||||
assert a.tag_ids == []
|
||||
|
||||
def test_remove_nonexistent_tag_idempotent(self):
|
||||
a = Asset.create("p1", "lib1", "v.mp4", "k", "video/mp4")
|
||||
a.add_tag("tag1")
|
||||
old_updated = a.updated_at
|
||||
a.remove_tag("nonexistent") # 不报错
|
||||
assert a.tag_ids == ["tag1"]
|
||||
assert a.updated_at == old_updated # 没修改就不更新时间
|
||||
|
||||
def test_remove_tag_updates_updated_at(self):
|
||||
a = Asset.create("p1", "lib1", "v.mp4", "k", "video/mp4")
|
||||
a.add_tag("tag1")
|
||||
old_updated = a.updated_at
|
||||
import time
|
||||
|
||||
time.sleep(0.001)
|
||||
a.remove_tag("tag1")
|
||||
assert a.updated_at >= old_updated
|
||||
|
||||
|
||||
# ============================================================
|
||||
# IngestJob 测试
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestIngestJobCreate:
|
||||
def test_create_minimal(self):
|
||||
job = IngestJob.create(project_id="p1", library_id="lib1", storage_key="uploads/v1.mp4")
|
||||
assert job.id
|
||||
assert len(job.id) == 32
|
||||
assert job.project_id == "p1"
|
||||
assert job.library_id == "lib1"
|
||||
assert job.storage_key == "uploads/v1.mp4"
|
||||
assert job.status == IngestJobStatus.PENDING
|
||||
assert job.error_message == ""
|
||||
assert job.result_asset_id == ""
|
||||
assert job.file_hash == ""
|
||||
|
||||
def test_create_with_hash(self):
|
||||
job = IngestJob.create(
|
||||
project_id="p1",
|
||||
library_id="lib1",
|
||||
storage_key="k",
|
||||
file_hash=" abc123 ",
|
||||
)
|
||||
assert job.file_hash == "abc123" # stripped
|
||||
|
||||
def test_create_strips_fields(self):
|
||||
job = IngestJob.create(
|
||||
project_id=" p1 ",
|
||||
library_id=" lib1 ",
|
||||
storage_key=" key1 ",
|
||||
)
|
||||
assert job.project_id == "p1"
|
||||
assert job.library_id == "lib1"
|
||||
assert job.storage_key == "key1"
|
||||
|
||||
def test_create_empty_project_id_raises(self):
|
||||
with pytest.raises(ValueError, match="project_id 不能为空"):
|
||||
IngestJob.create(project_id="", library_id="lib1", storage_key="k")
|
||||
|
||||
def test_create_whitespace_project_id_raises(self):
|
||||
with pytest.raises(ValueError, match="project_id 不能为空"):
|
||||
IngestJob.create(project_id=" ", library_id="lib1", storage_key="k")
|
||||
|
||||
def test_create_empty_library_id_raises(self):
|
||||
with pytest.raises(ValueError, match="library_id 不能为空"):
|
||||
IngestJob.create(project_id="p1", library_id="", storage_key="k")
|
||||
|
||||
def test_create_empty_storage_key_raises(self):
|
||||
with pytest.raises(ValueError, match="storage_key 不能为空"):
|
||||
IngestJob.create(project_id="p1", library_id="lib1", storage_key="")
|
||||
|
||||
def test_create_unique_ids(self):
|
||||
j1 = IngestJob.create("p1", "lib1", "k1")
|
||||
j2 = IngestJob.create("p1", "lib1", "k2")
|
||||
assert j1.id != j2.id
|
||||
|
||||
def test_create_has_timestamps(self):
|
||||
before = datetime.now(timezone.utc)
|
||||
job = IngestJob.create("p1", "lib1", "k")
|
||||
after = datetime.now(timezone.utc)
|
||||
assert before <= job.created_at <= after
|
||||
assert before <= job.updated_at <= after
|
||||
@@ -1,350 +0,0 @@
|
||||
"""filter_presets 单元测试 - wave164
|
||||
|
||||
覆盖:
|
||||
- FilterPreset 数据类(frozen/默认值/字段)
|
||||
- FILTER_PRESET_LIBRARY 预设库(数量/分类/ID唯一性)
|
||||
- get_filter_preset 按ID获取
|
||||
- list_filter_presets 筛选列表(分类/关键词)
|
||||
- build_ffmpeg_filter 滤镜生成(强度/参数插值/边界/空值)
|
||||
"""
|
||||
|
||||
import dataclasses
|
||||
|
||||
import pytest
|
||||
|
||||
from packages.domain.filter_presets import (
|
||||
FILTER_PRESET_LIBRARY,
|
||||
FilterPreset,
|
||||
build_ffmpeg_filter,
|
||||
get_filter_preset,
|
||||
list_filter_presets,
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# FilterPreset 数据类
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestFilterPresetDataclass:
|
||||
def test_minimal_creation(self):
|
||||
p = FilterPreset(id="test", name="Test", category="basic")
|
||||
assert p.id == "test"
|
||||
assert p.name == "Test"
|
||||
assert p.category == "basic"
|
||||
|
||||
def test_default_values(self):
|
||||
p = FilterPreset(id="t", name="T", category="basic")
|
||||
assert p.description == ""
|
||||
assert p.tags == []
|
||||
assert p.brightness == 0.0
|
||||
assert p.contrast == 1.0
|
||||
assert p.saturation == 1.0
|
||||
assert p.gamma == 1.0
|
||||
assert p.gamma_r == 1.0
|
||||
assert p.gamma_g == 1.0
|
||||
assert p.gamma_b == 1.0
|
||||
assert p.hue == 0.0
|
||||
assert p.lut_url == ""
|
||||
|
||||
def test_full_creation(self):
|
||||
p = FilterPreset(
|
||||
id="full",
|
||||
name="Full",
|
||||
category="cinematic",
|
||||
description="test desc",
|
||||
tags=["tag1", "tag2"],
|
||||
brightness=0.5,
|
||||
contrast=1.5,
|
||||
saturation=2.0,
|
||||
gamma=1.2,
|
||||
gamma_r=1.3,
|
||||
gamma_g=0.9,
|
||||
gamma_b=0.8,
|
||||
hue=30.0,
|
||||
lut_url="http://lut.png",
|
||||
)
|
||||
assert p.description == "test desc"
|
||||
assert p.tags == ["tag1", "tag2"]
|
||||
assert p.brightness == 0.5
|
||||
assert p.contrast == 1.5
|
||||
assert p.gamma == 1.2
|
||||
assert p.hue == 30.0
|
||||
assert p.lut_url == "http://lut.png"
|
||||
|
||||
def test_frozen_immutable(self):
|
||||
p = FilterPreset(id="t", name="T", category="basic")
|
||||
with pytest.raises((AttributeError, dataclasses.FrozenInstanceError)):
|
||||
p.brightness = 0.5 # frozen=True,不能修改
|
||||
|
||||
def test_tags_default_new_list(self):
|
||||
# 每个实例有独立的列表
|
||||
p1 = FilterPreset(id="t1", name="T1", category="basic")
|
||||
p2 = FilterPreset(id="t2", name="T2", category="basic")
|
||||
assert p1.tags is not p2.tags
|
||||
|
||||
|
||||
# ============================================================
|
||||
# FILTER_PRESET_LIBRARY 预设库
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestFilterPresetLibrary:
|
||||
def test_library_not_empty(self):
|
||||
assert len(FILTER_PRESET_LIBRARY) > 0
|
||||
|
||||
def test_all_ids_unique(self):
|
||||
ids = [p.id for p in FILTER_PRESET_LIBRARY]
|
||||
assert len(ids) == len(set(ids))
|
||||
|
||||
def test_all_have_required_fields(self):
|
||||
for p in FILTER_PRESET_LIBRARY:
|
||||
assert p.id, f"预设缺少id: {p}"
|
||||
assert p.name, f"预设缺少name: {p.id}"
|
||||
assert p.category, f"预设缺少category: {p.id}"
|
||||
|
||||
def test_categories_are_valid(self):
|
||||
valid_categories = {"basic", "cinematic", "vintage", "bw", "style"}
|
||||
for p in FILTER_PRESET_LIBRARY:
|
||||
assert p.category in valid_categories, f"无效分类: {p.id} -> {p.category}"
|
||||
|
||||
def test_basic_category_exists(self):
|
||||
basics = [p for p in FILTER_PRESET_LIBRARY if p.category == "basic"]
|
||||
assert len(basics) >= 3
|
||||
|
||||
def test_cinematic_category_exists(self):
|
||||
cinematic = [p for p in FILTER_PRESET_LIBRARY if p.category == "cinematic"]
|
||||
assert len(cinematic) >= 1
|
||||
|
||||
def test_vintage_category_exists(self):
|
||||
vintage = [p for p in FILTER_PRESET_LIBRARY if p.category == "vintage"]
|
||||
assert len(vintage) >= 1
|
||||
|
||||
def test_bw_category_exists(self):
|
||||
bw = [p for p in FILTER_PRESET_LIBRARY if p.category == "bw"]
|
||||
assert len(bw) >= 1
|
||||
# 所有黑白预设饱和度为0
|
||||
for p in bw:
|
||||
assert p.saturation == 0.0
|
||||
|
||||
def test_style_category_exists(self):
|
||||
style = [p for p in FILTER_PRESET_LIBRARY if p.category == "style"]
|
||||
assert len(style) >= 1
|
||||
|
||||
def test_none_filter_has_no_effect(self):
|
||||
none_preset = get_filter_preset("filter_none")
|
||||
assert none_preset is not None
|
||||
assert none_preset.brightness == 0.0
|
||||
assert none_preset.contrast == 1.0
|
||||
assert none_preset.saturation == 1.0
|
||||
assert none_preset.gamma == 1.0
|
||||
|
||||
def test_total_count(self):
|
||||
# 至少有15个预设
|
||||
assert len(FILTER_PRESET_LIBRARY) >= 15
|
||||
|
||||
|
||||
# ============================================================
|
||||
# get_filter_preset
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestGetFilterPreset:
|
||||
def test_existing_id(self):
|
||||
p = get_filter_preset("filter_brighten")
|
||||
assert p is not None
|
||||
assert p.id == "filter_brighten"
|
||||
assert p.name == "明亮"
|
||||
|
||||
def test_nonexistent_id(self):
|
||||
assert get_filter_preset("nonexistent") is None
|
||||
|
||||
def test_empty_id(self):
|
||||
assert get_filter_preset("") is None
|
||||
|
||||
def test_case_sensitive(self):
|
||||
# 大小写敏感
|
||||
assert get_filter_preset("Filter_Brighten") is None
|
||||
|
||||
def test_returns_preset_object(self):
|
||||
p = get_filter_preset("filter_cinematic")
|
||||
assert isinstance(p, FilterPreset)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# list_filter_presets
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestListFilterPresets:
|
||||
def test_no_filters_returns_all(self):
|
||||
result = list_filter_presets()
|
||||
assert len(result) == len(FILTER_PRESET_LIBRARY)
|
||||
|
||||
def test_filter_by_category_basic(self):
|
||||
result = list_filter_presets(category="basic")
|
||||
assert len(result) > 0
|
||||
for p in result:
|
||||
assert p.category == "basic"
|
||||
|
||||
def test_filter_by_category_cinematic(self):
|
||||
result = list_filter_presets(category="cinematic")
|
||||
for p in result:
|
||||
assert p.category == "cinematic"
|
||||
|
||||
def test_filter_by_category_bw(self):
|
||||
result = list_filter_presets(category="bw")
|
||||
for p in result:
|
||||
assert p.category == "bw"
|
||||
|
||||
def test_filter_by_category_vintage(self):
|
||||
result = list_filter_presets(category="vintage")
|
||||
for p in result:
|
||||
assert p.category == "vintage"
|
||||
|
||||
def test_filter_by_category_style(self):
|
||||
result = list_filter_presets(category="style")
|
||||
for p in result:
|
||||
assert p.category == "style"
|
||||
|
||||
def test_invalid_category_returns_empty(self):
|
||||
result = list_filter_presets(category="nonexistent")
|
||||
assert result == []
|
||||
|
||||
def test_keyword_search_name(self):
|
||||
result = list_filter_presets(keyword="电影")
|
||||
assert len(result) >= 1
|
||||
names = [p.name for p in result]
|
||||
assert any("电影" in n for n in names)
|
||||
|
||||
def test_keyword_search_tags(self):
|
||||
result = list_filter_presets(keyword="复古")
|
||||
assert len(result) >= 1
|
||||
# 至少有一个的标签或名称包含复古
|
||||
found = any(any("复古" in t for t in p.tags) or "复古" in p.name for p in result)
|
||||
assert found
|
||||
|
||||
def test_keyword_search_description(self):
|
||||
result = list_filter_presets(keyword="青橙")
|
||||
assert len(result) >= 1
|
||||
|
||||
def test_keyword_case_insensitive(self):
|
||||
# 中文不区分大小写,用英文标签/名称试
|
||||
result = list_filter_presets(keyword="FILTER")
|
||||
# 搜索 filter 应该能匹配到很多(id里有但搜索name/desc/tags)
|
||||
# 用确定的中文关键词更可靠
|
||||
assert isinstance(result, list)
|
||||
|
||||
def test_keyword_nonexistent_returns_empty(self):
|
||||
result = list_filter_presets(keyword="zzz不存在的关键词zzz")
|
||||
assert result == []
|
||||
|
||||
def test_category_and_keyword_combined(self):
|
||||
result = list_filter_presets(category="basic", keyword="亮")
|
||||
for p in result:
|
||||
assert p.category == "basic"
|
||||
assert "亮" in p.name or "亮" in p.description or any("亮" in t for t in p.tags)
|
||||
|
||||
def test_keyword_empty_returns_all(self):
|
||||
result = list_filter_presets(keyword="")
|
||||
assert len(result) == len(FILTER_PRESET_LIBRARY)
|
||||
|
||||
def test_returns_list_of_presets(self):
|
||||
result = list_filter_presets()
|
||||
for p in result:
|
||||
assert isinstance(p, FilterPreset)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# build_ffmpeg_filter
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestBuildFfmpegFilter:
|
||||
def test_valid_preset_full_intensity(self):
|
||||
result = build_ffmpeg_filter("filter_brighten", 100)
|
||||
assert result.startswith("eq=")
|
||||
assert "brightness=" in result
|
||||
assert "contrast=" in result
|
||||
|
||||
def test_invalid_preset_returns_empty(self):
|
||||
assert build_ffmpeg_filter("nonexistent", 100) == ""
|
||||
|
||||
def test_zero_intensity_returns_empty(self):
|
||||
assert build_ffmpeg_filter("filter_brighten", 0) == ""
|
||||
|
||||
def test_negative_intensity_returns_empty(self):
|
||||
assert build_ffmpeg_filter("filter_brighten", -10) == ""
|
||||
|
||||
def test_intensity_over_100_clamped(self):
|
||||
# >100 按100算
|
||||
r100 = build_ffmpeg_filter("filter_saturate", 100)
|
||||
r200 = build_ffmpeg_filter("filter_saturate", 200)
|
||||
assert r100 == r200
|
||||
|
||||
def test_half_intensity_half_effect(self):
|
||||
full = build_ffmpeg_filter("filter_brighten", 100)
|
||||
half = build_ffmpeg_filter("filter_brighten", 50)
|
||||
# 半强度的brightness应该小一些
|
||||
assert full != half
|
||||
# 半强度仍然有效果
|
||||
assert half.startswith("eq=")
|
||||
|
||||
def test_none_filter_returns_empty(self):
|
||||
# filter_none所有参数都是默认值,应该返回空
|
||||
result = build_ffmpeg_filter("filter_none", 100)
|
||||
assert result == ""
|
||||
|
||||
def test_format_has_eq_prefix(self):
|
||||
result = build_ffmpeg_filter("filter_saturate", 100)
|
||||
assert result.startswith("eq=")
|
||||
|
||||
def test_parameters_separated_by_colon(self):
|
||||
result = build_ffmpeg_filter("filter_cinematic", 100)
|
||||
parts = result[len("eq=") :].split(":")
|
||||
assert len(parts) >= 3 # 至少有3个参数
|
||||
|
||||
def test_brightness_value_format(self):
|
||||
result = build_ffmpeg_filter("filter_brighten", 100)
|
||||
# brightness参数应该存在且是数值
|
||||
assert "brightness=0." in result or "brightness=-0." in result
|
||||
|
||||
def test_saturation_value_format(self):
|
||||
result = build_ffmpeg_filter("filter_saturate", 100)
|
||||
assert "saturation=" in result
|
||||
|
||||
def test_contrast_value_format(self):
|
||||
result = build_ffmpeg_filter("filter_contrast", 100)
|
||||
assert "contrast=" in result
|
||||
|
||||
def test_gamma_parameters_present(self):
|
||||
result = build_ffmpeg_filter("filter_warm", 100)
|
||||
# 暖色预设应该有gamma_r/gamma_b变化
|
||||
assert "gamma_r=" in result or "gamma_b=" in result
|
||||
|
||||
def test_very_low_intensity_near_zero(self):
|
||||
# 强度1%,效果接近0,但因为有brightness等可能仍然在阈值以上
|
||||
result = build_ffmpeg_filter("filter_brighten", 1)
|
||||
# 至少应该有一个参数(brightness * 0.01 = 0.0012 > 0.001)
|
||||
assert isinstance(result, str)
|
||||
|
||||
def test_all_presets_generate_valid_output(self):
|
||||
# 每个预设在100强度下都能生成合法结果
|
||||
for preset in FILTER_PRESET_LIBRARY:
|
||||
result = build_ffmpeg_filter(preset.id, 100)
|
||||
assert isinstance(result, str)
|
||||
if result: # filter_none 可能为空
|
||||
assert result.startswith("eq=")
|
||||
|
||||
def test_intensity_50_between_0_and_100(self):
|
||||
# 验证50%强度确实在0和100之间插值
|
||||
r0 = build_ffmpeg_filter("filter_saturate", 0)
|
||||
r50 = build_ffmpeg_filter("filter_saturate", 50)
|
||||
r100 = build_ffmpeg_filter("filter_saturate", 100)
|
||||
assert r0 == ""
|
||||
assert r50 != r100
|
||||
assert r50 != ""
|
||||
|
||||
def test_hue_not_included_in_eq(self):
|
||||
# hue 不在 eq 滤镜参数中(当前实现没有hue)
|
||||
result = build_ffmpeg_filter("filter_cinematic", 100)
|
||||
assert "hue=" not in result
|
||||
@@ -1,170 +0,0 @@
|
||||
"""generated_video 单测.
|
||||
|
||||
domain 层生成视频实体纯逻辑模块,0 外部依赖。
|
||||
覆盖:create工厂/校验、默认值、数据完整性。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.generated_video import GeneratedVideo
|
||||
|
||||
|
||||
class TestGeneratedVideoCreate:
|
||||
"""GeneratedVideo.create 工厂测试."""
|
||||
|
||||
def test_create_required_fields(self):
|
||||
"""最简创建(仅必填字段)."""
|
||||
v = GeneratedVideo.create(
|
||||
project_id="proj1",
|
||||
generation_task_id="task1",
|
||||
name="我的视频",
|
||||
file_url="https://cdn.example.com/v.mp4",
|
||||
)
|
||||
assert v.project_id == "proj1"
|
||||
assert v.generation_task_id == "task1"
|
||||
assert v.name == "我的视频"
|
||||
assert v.file_url == "https://cdn.example.com/v.mp4"
|
||||
assert isinstance(v.id, str)
|
||||
assert len(v.id) > 0
|
||||
|
||||
def test_create_defaults(self):
|
||||
"""默认值正确."""
|
||||
v = GeneratedVideo.create(
|
||||
project_id="p1",
|
||||
generation_task_id="t1",
|
||||
name="v",
|
||||
file_url="http://x/v.mp4",
|
||||
)
|
||||
assert v.file_size == 0
|
||||
assert v.duration == 0.0
|
||||
assert v.width == 0
|
||||
assert v.height == 0
|
||||
assert v.fps == 0.0
|
||||
assert v.thumbnail_url is None
|
||||
assert v.status == "completed"
|
||||
assert v.review_status == "pending_review"
|
||||
assert v.generation_params == {}
|
||||
assert v.video_fingerprint is None
|
||||
assert v.is_duplicate is False
|
||||
assert v.duplicate_of is None
|
||||
|
||||
def test_create_with_all_fields(self):
|
||||
"""带全部字段创建."""
|
||||
v = GeneratedVideo.create(
|
||||
project_id="proj1",
|
||||
generation_task_id="task1",
|
||||
name=" 测试视频 ",
|
||||
file_url=" https://cdn.example.com/v.mp4 ",
|
||||
file_size=1024000,
|
||||
duration=120.5,
|
||||
width=1920,
|
||||
height=1080,
|
||||
fps=30.0,
|
||||
thumbnail_url="https://cdn.example.com/thumb.jpg",
|
||||
generation_params={"template": "tpl1", "bgm": "bgm1"},
|
||||
)
|
||||
assert v.name == "测试视频" # strip
|
||||
assert v.file_url == "https://cdn.example.com/v.mp4" # strip
|
||||
assert v.file_size == 1024000
|
||||
assert v.duration == 120.5
|
||||
assert v.width == 1920
|
||||
assert v.height == 1080
|
||||
assert v.fps == 30.0
|
||||
assert v.thumbnail_url == "https://cdn.example.com/thumb.jpg"
|
||||
assert v.generation_params == {"template": "tpl1", "bgm": "bgm1"}
|
||||
|
||||
def test_create_strips_fields(self):
|
||||
"""字符串字段会 strip."""
|
||||
v = GeneratedVideo.create(
|
||||
project_id=" p1 ",
|
||||
generation_task_id=" t1 ",
|
||||
name=" v ",
|
||||
file_url=" http://x/v ",
|
||||
)
|
||||
assert v.project_id == "p1"
|
||||
assert v.generation_task_id == "t1"
|
||||
assert v.name == "v"
|
||||
assert v.file_url == "http://x/v"
|
||||
|
||||
def test_create_empty_project_id(self):
|
||||
"""空 project_id 无效."""
|
||||
try:
|
||||
GeneratedVideo.create("", "t1", "v", "http://x/v")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "project_id" in str(e)
|
||||
|
||||
def test_create_whitespace_project_id(self):
|
||||
"""纯空白 project_id 无效."""
|
||||
try:
|
||||
GeneratedVideo.create(" ", "t1", "v", "http://x/v")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "project_id" in str(e)
|
||||
|
||||
def test_create_empty_task_id(self):
|
||||
"""空 generation_task_id 无效."""
|
||||
try:
|
||||
GeneratedVideo.create("p1", "", "v", "http://x/v")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "generation_task_id" in str(e)
|
||||
|
||||
def test_create_empty_name(self):
|
||||
"""空 name 无效."""
|
||||
try:
|
||||
GeneratedVideo.create("p1", "t1", "", "http://x/v")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "name" in str(e)
|
||||
|
||||
def test_create_empty_file_url(self):
|
||||
"""空 file_url 无效."""
|
||||
try:
|
||||
GeneratedVideo.create("p1", "t1", "v", "")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "file_url" in str(e)
|
||||
|
||||
def test_create_whitespace_file_url(self):
|
||||
"""纯空白 file_url 无效."""
|
||||
try:
|
||||
GeneratedVideo.create("p1", "t1", "v", " ")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "file_url" in str(e)
|
||||
|
||||
def test_create_generation_params_none_defaults_empty(self):
|
||||
"""generation_params=None 默认为空 dict."""
|
||||
v = GeneratedVideo.create("p1", "t1", "v", "http://x/v", generation_params=None)
|
||||
assert v.generation_params == {}
|
||||
|
||||
def test_create_unique_id(self):
|
||||
"""不同视频 id 不同."""
|
||||
v1 = GeneratedVideo.create("p", "t", "v", "http://x/v")
|
||||
v2 = GeneratedVideo.create("p", "t", "v", "http://x/v")
|
||||
assert v1.id != v2.id
|
||||
|
||||
def test_create_has_timestamps(self):
|
||||
"""有生成和创建时间."""
|
||||
v = GeneratedVideo.create("p", "t", "v", "http://x/v")
|
||||
assert v.generated_at is not None
|
||||
assert v.created_at is not None
|
||||
|
||||
def test_zero_dimensions_valid(self):
|
||||
"""宽高为 0 合法(未指定分辨率)."""
|
||||
v = GeneratedVideo.create("p", "t", "v", "http://x/v", width=0, height=0)
|
||||
assert v.width == 0
|
||||
assert v.height == 0
|
||||
|
||||
def test_zero_fps_valid(self):
|
||||
"""fps 为 0 合法."""
|
||||
v = GeneratedVideo.create("p", "t", "v", "http://x/v", fps=0.0)
|
||||
assert v.fps == 0.0
|
||||
|
||||
def test_generation_params_independent(self):
|
||||
"""不同实例的 generation_params 独立."""
|
||||
v1 = GeneratedVideo.create("p", "t", "v", "http://x/v")
|
||||
v2 = GeneratedVideo.create("p", "t", "v", "http://x/v")
|
||||
v1.generation_params["key"] = "value"
|
||||
assert "key" not in v2.generation_params
|
||||
@@ -1,208 +0,0 @@
|
||||
"""generation_task 单测.
|
||||
|
||||
domain 层生成任务实体纯逻辑模块,0 外部依赖。
|
||||
覆盖:枚举、create工厂/校验、列表拷贝。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.generation_task import GenerationTask, GenerationTaskStatus
|
||||
|
||||
|
||||
class TestGenerationTaskStatus:
|
||||
"""GenerationTaskStatus 枚举测试."""
|
||||
|
||||
def test_five_statuses(self):
|
||||
"""五种状态."""
|
||||
assert len(GenerationTaskStatus) == 5
|
||||
|
||||
def test_pending(self):
|
||||
assert GenerationTaskStatus.PENDING == "pending"
|
||||
|
||||
def test_running(self):
|
||||
assert GenerationTaskStatus.RUNNING == "running"
|
||||
|
||||
def test_completed(self):
|
||||
assert GenerationTaskStatus.COMPLETED == "completed"
|
||||
|
||||
def test_failed(self):
|
||||
assert GenerationTaskStatus.FAILED == "failed"
|
||||
|
||||
def test_cancelled(self):
|
||||
assert GenerationTaskStatus.CANCELLED == "cancelled"
|
||||
|
||||
|
||||
class TestGenerationTaskCreate:
|
||||
"""GenerationTask.create 工厂测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简创建(project_id + asset_library_id)."""
|
||||
task = GenerationTask.create(project_id="proj1", asset_library_id="lib1")
|
||||
assert task.project_id == "proj1"
|
||||
assert task.asset_library_id == "lib1"
|
||||
assert task.status == GenerationTaskStatus.PENDING
|
||||
assert task.progress == 0.0
|
||||
assert task.result_count == 0
|
||||
assert task.error_message == ""
|
||||
assert task.asset_ids == []
|
||||
assert task.title_ids == []
|
||||
assert task.voice_ids == []
|
||||
assert isinstance(task.id, str)
|
||||
assert len(task.id) > 0
|
||||
|
||||
def test_create_with_all_fields(self):
|
||||
"""带全部字段创建."""
|
||||
task = GenerationTask.create(
|
||||
project_id=" proj1 ",
|
||||
asset_library_id=" lib1 ",
|
||||
strategy_id=" strat1 ",
|
||||
voice_library_id=" vlib1 ",
|
||||
template_id=" tpl1 ",
|
||||
asset_ids=["a1", "a2", "a3"],
|
||||
title_ids=["t1", "t2"],
|
||||
voice_ids=["v1"],
|
||||
created_by_user_id=" user1 ",
|
||||
source_edit_plan_id=" plan1 ",
|
||||
asset_select_mode="random",
|
||||
batch_id="batch1",
|
||||
)
|
||||
assert task.project_id == "proj1"
|
||||
assert task.asset_library_id == "lib1"
|
||||
assert task.strategy_id == "strat1"
|
||||
assert task.voice_library_id == "vlib1"
|
||||
assert task.template_id == "tpl1"
|
||||
assert task.asset_ids == ["a1", "a2", "a3"]
|
||||
assert task.title_ids == ["t1", "t2"]
|
||||
assert task.voice_ids == ["v1"]
|
||||
assert task.created_by_user_id == "user1"
|
||||
assert task.source_edit_plan_id == "plan1"
|
||||
assert task.asset_select_mode == "random"
|
||||
assert task.batch_id == "batch1"
|
||||
|
||||
def test_create_with_template_instead_of_project(self):
|
||||
"""有 template_id 但 project_id 为空也可以."""
|
||||
task = GenerationTask.create(
|
||||
project_id="",
|
||||
asset_library_id="lib1",
|
||||
template_id="tpl1",
|
||||
)
|
||||
assert task.template_id == "tpl1"
|
||||
assert task.project_id == ""
|
||||
|
||||
def test_create_neither_project_nor_template(self):
|
||||
"""project_id 和 template_id 都为空,抛错."""
|
||||
try:
|
||||
GenerationTask.create(project_id="", asset_library_id="lib1")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "project_id" in str(e) and "template_id" in str(e)
|
||||
|
||||
def test_create_whitespace_project_and_template(self):
|
||||
"""都是空白也抛错."""
|
||||
try:
|
||||
GenerationTask.create(project_id=" ", asset_library_id="lib1", template_id=" ")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "project_id" in str(e) and "template_id" in str(e)
|
||||
|
||||
def test_create_no_asset_library_and_no_ids(self):
|
||||
"""asset_library_id 为空且没有素材列表,抛错."""
|
||||
try:
|
||||
GenerationTask.create(project_id="p1", asset_library_id="")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "asset_library_id" in str(e)
|
||||
|
||||
def test_create_whitespace_asset_library_and_no_ids(self):
|
||||
"""空白 asset_library 且无素材列表,抛错."""
|
||||
try:
|
||||
GenerationTask.create(project_id="p1", asset_library_id=" ")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "asset_library_id" in str(e)
|
||||
|
||||
def test_create_with_asset_ids_instead_of_library(self):
|
||||
"""用 asset_ids 替代 asset_library_id."""
|
||||
task = GenerationTask.create(
|
||||
project_id="p1",
|
||||
asset_library_id="",
|
||||
asset_ids=["a1", "a2"],
|
||||
)
|
||||
assert task.asset_library_id == ""
|
||||
assert task.asset_ids == ["a1", "a2"]
|
||||
|
||||
def test_create_with_title_ids_instead_of_library(self):
|
||||
"""用 title_ids 替代 asset_library_id."""
|
||||
task = GenerationTask.create(
|
||||
project_id="p1",
|
||||
asset_library_id="",
|
||||
title_ids=["t1"],
|
||||
)
|
||||
assert task.title_ids == ["t1"]
|
||||
|
||||
def test_create_with_voice_ids_instead_of_library(self):
|
||||
"""用 voice_ids 替代 asset_library_id."""
|
||||
task = GenerationTask.create(
|
||||
project_id="p1",
|
||||
asset_library_id="",
|
||||
voice_ids=["v1"],
|
||||
)
|
||||
assert task.voice_ids == ["v1"]
|
||||
|
||||
def test_create_asset_ids_copied(self):
|
||||
"""asset_ids 是拷贝不是引用."""
|
||||
original = ["a1", "a2"]
|
||||
task = GenerationTask.create(project_id="p1", asset_library_id="lib1", asset_ids=original)
|
||||
original.append("a3")
|
||||
assert task.asset_ids == ["a1", "a2"]
|
||||
|
||||
def test_create_title_ids_copied(self):
|
||||
"""title_ids 是拷贝不是引用."""
|
||||
original = ["t1"]
|
||||
task = GenerationTask.create(project_id="p1", asset_library_id="lib1", title_ids=original)
|
||||
original.append("t2")
|
||||
assert task.title_ids == ["t1"]
|
||||
|
||||
def test_create_voice_ids_copied(self):
|
||||
"""voice_ids 是拷贝不是引用."""
|
||||
original = ["v1"]
|
||||
task = GenerationTask.create(project_id="p1", asset_library_id="lib1", voice_ids=original)
|
||||
original.append("v2")
|
||||
assert task.voice_ids == ["v1"]
|
||||
|
||||
def test_create_none_lists_default_empty(self):
|
||||
"""None 列表默认为空."""
|
||||
task = GenerationTask.create(
|
||||
project_id="p1",
|
||||
asset_library_id="lib1",
|
||||
asset_ids=None,
|
||||
title_ids=None,
|
||||
voice_ids=None,
|
||||
)
|
||||
assert task.asset_ids == []
|
||||
assert task.title_ids == []
|
||||
assert task.voice_ids == []
|
||||
|
||||
def test_create_unique_id(self):
|
||||
"""不同任务 id 不同."""
|
||||
t1 = GenerationTask.create("p", "l")
|
||||
t2 = GenerationTask.create("p", "l")
|
||||
assert t1.id != t2.id
|
||||
|
||||
def test_create_has_created_at(self):
|
||||
"""有创建时间."""
|
||||
task = GenerationTask.create("p", "l")
|
||||
assert task.created_at is not None
|
||||
|
||||
def test_create_defaults_started_completed_none(self):
|
||||
"""started_at 和 completed_at 默认 None."""
|
||||
task = GenerationTask.create("p", "l")
|
||||
assert task.started_at is None
|
||||
assert task.completed_at is None
|
||||
|
||||
def test_empty_lists_independent(self):
|
||||
"""不同任务的空列表互不影响."""
|
||||
t1 = GenerationTask.create("p", "l")
|
||||
t2 = GenerationTask.create("p", "l")
|
||||
t1.asset_ids.append("x")
|
||||
assert t2.asset_ids == []
|
||||
@@ -1,523 +0,0 @@
|
||||
"""job 单测.
|
||||
|
||||
domain 层统一异步任务纯逻辑模块,0 外部依赖。
|
||||
覆盖:枚举、create工厂、状态机、进度更新、重试机制、序列化。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.job import TERMINAL_STATUSES, Job, JobStatus, JobType
|
||||
|
||||
|
||||
class TestJobType:
|
||||
"""JobType 枚举测试."""
|
||||
|
||||
def test_six_types(self):
|
||||
"""六种任务类型."""
|
||||
assert len(JobType) == 6
|
||||
|
||||
def test_video_compose(self):
|
||||
assert JobType.VIDEO_COMPOSE == "video_compose"
|
||||
|
||||
def test_render_edit_plan(self):
|
||||
assert JobType.RENDER_EDIT_PLAN == "render_edit_plan"
|
||||
|
||||
def test_asset_ingest(self):
|
||||
assert JobType.ASSET_INGEST == "asset_ingest"
|
||||
|
||||
def test_classification(self):
|
||||
assert JobType.CLASSIFICATION == "classification"
|
||||
|
||||
def test_voice_extraction(self):
|
||||
assert JobType.VOICE_EXTRACTION == "voice_extraction"
|
||||
|
||||
def test_generation(self):
|
||||
assert JobType.GENERATION == "generation"
|
||||
|
||||
|
||||
class TestJobStatus:
|
||||
"""JobStatus 枚举测试."""
|
||||
|
||||
def test_five_statuses(self):
|
||||
assert len(JobStatus) == 5
|
||||
|
||||
def test_pending(self):
|
||||
assert JobStatus.PENDING == "pending"
|
||||
|
||||
def test_running(self):
|
||||
assert JobStatus.RUNNING == "running"
|
||||
|
||||
def test_success(self):
|
||||
assert JobStatus.SUCCESS == "success"
|
||||
|
||||
def test_failed(self):
|
||||
assert JobStatus.FAILED == "failed"
|
||||
|
||||
def test_cancelled(self):
|
||||
assert JobStatus.CANCELLED == "cancelled"
|
||||
|
||||
|
||||
class TestTerminalStatuses:
|
||||
"""终态集合测试."""
|
||||
|
||||
def test_success_is_terminal(self):
|
||||
assert JobStatus.SUCCESS in TERMINAL_STATUSES
|
||||
|
||||
def test_failed_is_terminal(self):
|
||||
assert JobStatus.FAILED in TERMINAL_STATUSES
|
||||
|
||||
def test_cancelled_is_terminal(self):
|
||||
assert JobStatus.CANCELLED in TERMINAL_STATUSES
|
||||
|
||||
def test_pending_not_terminal(self):
|
||||
assert JobStatus.PENDING not in TERMINAL_STATUSES
|
||||
|
||||
def test_running_not_terminal(self):
|
||||
assert JobStatus.RUNNING not in TERMINAL_STATUSES
|
||||
|
||||
|
||||
class TestJobCreate:
|
||||
"""Job.create 工厂测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简创建."""
|
||||
job = Job.create(project_id="proj1", job_type=JobType.VIDEO_COMPOSE)
|
||||
assert job.project_id == "proj1"
|
||||
assert job.job_type == JobType.VIDEO_COMPOSE
|
||||
assert job.status == JobStatus.PENDING
|
||||
assert job.progress == 0.0
|
||||
assert job.current_stage == ""
|
||||
assert job.payload == {}
|
||||
assert job.result == {}
|
||||
assert job.error_message == ""
|
||||
assert job.retry_count == 0
|
||||
assert job.max_retries == 3
|
||||
assert job.source_id == ""
|
||||
assert job.created_by_user_id == ""
|
||||
assert job.started_at is None
|
||||
assert job.completed_at is None
|
||||
assert isinstance(job.id, str)
|
||||
assert len(job.id) > 0
|
||||
|
||||
def test_create_with_string_job_type(self):
|
||||
"""用字符串传 job_type."""
|
||||
job = Job.create(project_id="p1", job_type="video_compose")
|
||||
assert job.job_type == JobType.VIDEO_COMPOSE
|
||||
|
||||
def test_create_invalid_job_type_string(self):
|
||||
"""无效的 job_type 字符串."""
|
||||
try:
|
||||
Job.create(project_id="p1", job_type="invalid_type")
|
||||
raise AssertionError("unexpected success")
|
||||
except ValueError as e:
|
||||
assert "不支持的任务类型" in str(e)
|
||||
|
||||
def test_create_full(self):
|
||||
"""带全部字段."""
|
||||
job = Job.create(
|
||||
project_id=" proj1 ",
|
||||
job_type=JobType.CLASSIFICATION,
|
||||
payload={"asset_id": "a1"},
|
||||
source_id=" src1 ",
|
||||
created_by_user_id=" user1 ",
|
||||
max_retries=5,
|
||||
)
|
||||
assert job.project_id == "proj1" # strip
|
||||
assert job.job_type == JobType.CLASSIFICATION
|
||||
assert job.payload == {"asset_id": "a1"}
|
||||
assert job.source_id == "src1" # strip
|
||||
assert job.created_by_user_id == "user1" # strip
|
||||
assert job.max_retries == 5
|
||||
|
||||
def test_create_empty_project_id(self):
|
||||
"""空 project_id 无效."""
|
||||
try:
|
||||
Job.create(project_id="", job_type=JobType.VIDEO_COMPOSE)
|
||||
raise AssertionError("unexpected success")
|
||||
except ValueError as e:
|
||||
assert "project_id" in str(e)
|
||||
|
||||
def test_create_whitespace_project_id(self):
|
||||
"""空白 project_id 无效."""
|
||||
try:
|
||||
Job.create(project_id=" ", job_type=JobType.VIDEO_COMPOSE)
|
||||
raise AssertionError("unexpected success")
|
||||
except ValueError as e:
|
||||
assert "project_id" in str(e)
|
||||
|
||||
def test_create_payload_none_defaults_empty(self):
|
||||
"""payload=None 默认为空 dict."""
|
||||
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE, payload=None)
|
||||
assert job.payload == {}
|
||||
|
||||
def test_create_unique_id(self):
|
||||
"""不同任务 id 不同."""
|
||||
j1 = Job.create("p", JobType.VIDEO_COMPOSE)
|
||||
j2 = Job.create("p", JobType.VIDEO_COMPOSE)
|
||||
assert j1.id != j2.id
|
||||
|
||||
def test_create_has_timestamps(self):
|
||||
"""有创建和更新时间."""
|
||||
job = Job.create("p", JobType.VIDEO_COMPOSE)
|
||||
assert job.created_at is not None
|
||||
assert job.updated_at is not None
|
||||
|
||||
|
||||
class TestJobIsTerminal:
|
||||
"""is_terminal 属性测试."""
|
||||
|
||||
def _make_job(self):
|
||||
return Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
||||
|
||||
def test_pending_not_terminal(self):
|
||||
job = self._make_job()
|
||||
assert job.is_terminal is False
|
||||
|
||||
def test_running_not_terminal(self):
|
||||
job = self._make_job()
|
||||
job.mark_running()
|
||||
assert job.is_terminal is False
|
||||
|
||||
def test_success_is_terminal(self):
|
||||
job = self._make_job()
|
||||
job.mark_running()
|
||||
job.mark_success()
|
||||
assert job.is_terminal is True
|
||||
|
||||
def test_failed_is_terminal(self):
|
||||
job = self._make_job()
|
||||
job.mark_running()
|
||||
job.mark_failed("error")
|
||||
assert job.is_terminal is True
|
||||
|
||||
def test_cancelled_is_terminal(self):
|
||||
job = self._make_job()
|
||||
job.mark_cancelled()
|
||||
assert job.is_terminal is True
|
||||
|
||||
|
||||
class TestJobStatusTransitions:
|
||||
"""状态转换测试."""
|
||||
|
||||
def _make_job(self):
|
||||
return Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
||||
|
||||
def test_pending_to_running(self):
|
||||
"""pending → running."""
|
||||
job = self._make_job()
|
||||
job.transition_to(JobStatus.RUNNING)
|
||||
assert job.status == JobStatus.RUNNING
|
||||
|
||||
def test_pending_to_success(self):
|
||||
"""pending → success(瞬时任务)."""
|
||||
job = self._make_job()
|
||||
job.transition_to(JobStatus.SUCCESS)
|
||||
assert job.status == JobStatus.SUCCESS
|
||||
|
||||
def test_pending_to_cancelled(self):
|
||||
"""pending → cancelled."""
|
||||
job = self._make_job()
|
||||
job.transition_to(JobStatus.CANCELLED)
|
||||
assert job.status == JobStatus.CANCELLED
|
||||
|
||||
def test_running_to_success(self):
|
||||
"""running → success."""
|
||||
job = self._make_job()
|
||||
job.transition_to(JobStatus.RUNNING)
|
||||
job.transition_to(JobStatus.SUCCESS)
|
||||
assert job.status == JobStatus.SUCCESS
|
||||
|
||||
def test_running_to_failed(self):
|
||||
"""running → failed."""
|
||||
job = self._make_job()
|
||||
job.transition_to(JobStatus.RUNNING)
|
||||
job.transition_to(JobStatus.FAILED)
|
||||
assert job.status == JobStatus.FAILED
|
||||
|
||||
def test_running_to_cancelled(self):
|
||||
"""running → cancelled."""
|
||||
job = self._make_job()
|
||||
job.transition_to(JobStatus.RUNNING)
|
||||
job.transition_to(JobStatus.CANCELLED)
|
||||
assert job.status == JobStatus.CANCELLED
|
||||
|
||||
def test_failed_to_pending_retry(self):
|
||||
"""failed → pending(重试)."""
|
||||
job = self._make_job()
|
||||
job.transition_to(JobStatus.RUNNING)
|
||||
job.transition_to(JobStatus.FAILED)
|
||||
job.transition_to(JobStatus.PENDING)
|
||||
assert job.status == JobStatus.PENDING
|
||||
|
||||
def test_cannot_success_to_running(self):
|
||||
"""success 不能回 running."""
|
||||
job = self._make_job()
|
||||
job.mark_running()
|
||||
job.mark_success()
|
||||
try:
|
||||
job.transition_to(JobStatus.RUNNING)
|
||||
raise AssertionError("unexpected success")
|
||||
except ValueError as e:
|
||||
assert "非法状态转换" in str(e)
|
||||
|
||||
def test_cannot_pending_to_failed_directly(self):
|
||||
"""pending 不能直接到 failed(必须经过 running)."""
|
||||
job = self._make_job()
|
||||
try:
|
||||
job.transition_to(JobStatus.FAILED)
|
||||
raise AssertionError("unexpected success")
|
||||
except ValueError as e:
|
||||
assert "非法状态转换" in str(e)
|
||||
|
||||
def test_transition_with_string(self):
|
||||
"""用字符串传状态."""
|
||||
job = self._make_job()
|
||||
job.transition_to("running")
|
||||
assert job.status == JobStatus.RUNNING
|
||||
|
||||
def test_transition_invalid_string(self):
|
||||
"""无效状态字符串."""
|
||||
job = self._make_job()
|
||||
try:
|
||||
job.transition_to("invalid")
|
||||
raise AssertionError("unexpected success")
|
||||
except ValueError as e:
|
||||
assert "无效状态" in str(e)
|
||||
|
||||
def test_transition_sets_started_at(self):
|
||||
"""第一次到 running 设置 started_at."""
|
||||
job = self._make_job()
|
||||
assert job.started_at is None
|
||||
job.transition_to(JobStatus.RUNNING)
|
||||
assert job.started_at is not None
|
||||
|
||||
def test_transition_sets_completed_at_on_success(self):
|
||||
"""success 设置 completed_at."""
|
||||
job = self._make_job()
|
||||
job.mark_running()
|
||||
assert job.completed_at is None
|
||||
job.mark_success()
|
||||
assert job.completed_at is not None
|
||||
|
||||
def test_transition_sets_completed_at_on_failed(self):
|
||||
"""failed 设置 completed_at."""
|
||||
job = self._make_job()
|
||||
job.mark_running()
|
||||
job.mark_failed("err")
|
||||
assert job.completed_at is not None
|
||||
|
||||
|
||||
class TestJobMarkMethods:
|
||||
"""便捷标记方法测试."""
|
||||
|
||||
def _make_job(self):
|
||||
return Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
||||
|
||||
def test_mark_running(self):
|
||||
job = self._make_job()
|
||||
job.mark_running()
|
||||
assert job.status == JobStatus.RUNNING
|
||||
|
||||
def test_mark_running_with_stage(self):
|
||||
job = self._make_job()
|
||||
job.mark_running(stage="正在合成视频")
|
||||
assert job.status == JobStatus.RUNNING
|
||||
assert job.current_stage == "正在合成视频"
|
||||
|
||||
def test_mark_success(self):
|
||||
job = self._make_job()
|
||||
job.mark_running()
|
||||
job.mark_success()
|
||||
assert job.status == JobStatus.SUCCESS
|
||||
assert job.progress == 100.0
|
||||
assert job.current_stage == "完成"
|
||||
|
||||
def test_mark_success_with_result(self):
|
||||
job = self._make_job()
|
||||
job.mark_running()
|
||||
job.mark_success(result={"video_url": "http://x/v.mp4"})
|
||||
assert job.result == {"video_url": "http://x/v.mp4"}
|
||||
|
||||
def test_mark_failed(self):
|
||||
job = self._make_job()
|
||||
job.mark_running()
|
||||
job.mark_failed("网络超时")
|
||||
assert job.status == JobStatus.FAILED
|
||||
assert job.error_message == "网络超时"
|
||||
assert job.current_stage == "失败"
|
||||
|
||||
def test_mark_cancelled(self):
|
||||
job = self._make_job()
|
||||
job.mark_cancelled()
|
||||
assert job.status == JobStatus.CANCELLED
|
||||
assert job.current_stage == "已取消"
|
||||
|
||||
|
||||
class TestJobProgress:
|
||||
"""进度更新测试."""
|
||||
|
||||
def _make_job(self):
|
||||
return Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
||||
|
||||
def test_update_progress(self):
|
||||
"""正常更新进度."""
|
||||
job = self._make_job()
|
||||
job.update_progress(50.0)
|
||||
assert job.progress == 50.0
|
||||
|
||||
def test_update_progress_with_stage(self):
|
||||
"""更新进度同时更新阶段."""
|
||||
job = self._make_job()
|
||||
job.update_progress(30.0, stage="合成中")
|
||||
assert job.progress == 30.0
|
||||
assert job.current_stage == "合成中"
|
||||
|
||||
def test_update_progress_zero(self):
|
||||
"""0% 合法."""
|
||||
job = self._make_job()
|
||||
job.update_progress(0.0)
|
||||
assert job.progress == 0.0
|
||||
|
||||
def test_update_progress_100(self):
|
||||
"""100% 合法."""
|
||||
job = self._make_job()
|
||||
job.update_progress(100.0)
|
||||
assert job.progress == 100.0
|
||||
|
||||
def test_update_progress_negative(self):
|
||||
"""负进度无效."""
|
||||
job = self._make_job()
|
||||
try:
|
||||
job.update_progress(-1.0)
|
||||
raise AssertionError("unexpected success")
|
||||
except ValueError as e:
|
||||
assert "进度" in str(e)
|
||||
|
||||
def test_update_progress_over_100(self):
|
||||
"""超过100%无效."""
|
||||
job = self._make_job()
|
||||
try:
|
||||
job.update_progress(101.0)
|
||||
raise AssertionError("unexpected success")
|
||||
except ValueError as e:
|
||||
assert "进度" in str(e)
|
||||
|
||||
def test_update_progress_updates_timestamp(self):
|
||||
"""更新进度时更新 updated_at."""
|
||||
job = self._make_job()
|
||||
old = job.updated_at
|
||||
job.update_progress(50.0)
|
||||
assert job.updated_at >= old
|
||||
|
||||
|
||||
class TestJobRetry:
|
||||
"""重试机制测试."""
|
||||
|
||||
def _make_failed_job(self, max_retries=3):
|
||||
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE, max_retries=max_retries)
|
||||
job.mark_running()
|
||||
job.mark_failed("error")
|
||||
return job
|
||||
|
||||
def test_is_retryable_true(self):
|
||||
"""失败且未超过重试次数时可重试."""
|
||||
job = self._make_failed_job(max_retries=3)
|
||||
assert job.is_retryable is True
|
||||
|
||||
def test_is_retryable_false_not_failed(self):
|
||||
"""非失败状态不可重试."""
|
||||
job = Job.create("p", JobType.VIDEO_COMPOSE)
|
||||
assert job.is_retryable is False
|
||||
|
||||
def test_is_retryable_false_exceeded(self):
|
||||
"""超过重试次数不可重试."""
|
||||
job = self._make_failed_job(max_retries=0)
|
||||
assert job.is_retryable is False
|
||||
|
||||
def test_prepare_retry(self):
|
||||
"""准备重试."""
|
||||
job = self._make_failed_job(max_retries=3)
|
||||
job.prepare_retry()
|
||||
assert job.status == JobStatus.PENDING
|
||||
assert job.retry_count == 1
|
||||
assert job.progress == 0.0
|
||||
assert "第 1 次重试" in job.current_stage
|
||||
assert job.error_message == ""
|
||||
assert job.started_at is None
|
||||
assert job.completed_at is None
|
||||
assert job.celery_task_id == ""
|
||||
|
||||
def test_prepare_retry_multiple_times(self):
|
||||
"""多次重试."""
|
||||
job = self._make_failed_job(max_retries=3)
|
||||
job.prepare_retry()
|
||||
job.mark_running()
|
||||
job.mark_failed("err2")
|
||||
assert job.retry_count == 1
|
||||
job.prepare_retry()
|
||||
assert job.retry_count == 2
|
||||
assert "第 2 次重试" in job.current_stage
|
||||
|
||||
def test_prepare_retry_not_retryable(self):
|
||||
"""不可重试时报错."""
|
||||
job = self._make_failed_job(max_retries=0)
|
||||
try:
|
||||
job.prepare_retry()
|
||||
raise AssertionError("unexpected success")
|
||||
except ValueError as e:
|
||||
assert "不可重试" in str(e)
|
||||
|
||||
|
||||
class TestJobToDict:
|
||||
"""to_dict 序列化测试."""
|
||||
|
||||
def test_to_dict_keys(self):
|
||||
"""包含所有必要字段."""
|
||||
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
||||
d = job.to_dict()
|
||||
assert isinstance(d, dict)
|
||||
keys = [
|
||||
"id",
|
||||
"project_id",
|
||||
"job_type",
|
||||
"status",
|
||||
"progress",
|
||||
"payload",
|
||||
"result",
|
||||
"error_message",
|
||||
"retry_count",
|
||||
"max_retries",
|
||||
"source_id",
|
||||
"is_retryable",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
]
|
||||
for k in keys:
|
||||
assert k in d, f"缺少字段: {k}"
|
||||
|
||||
def test_to_dict_enums_as_strings(self):
|
||||
"""枚举值序列化为字符串."""
|
||||
job = Job.create(project_id="p1", job_type=JobType.CLASSIFICATION)
|
||||
d = job.to_dict()
|
||||
assert d["job_type"] == "classification"
|
||||
assert d["status"] == "pending"
|
||||
|
||||
def test_to_dict_none_timestamps(self):
|
||||
"""None 时间戳序列化为 None."""
|
||||
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
||||
d = job.to_dict()
|
||||
assert d["started_at"] is None
|
||||
assert d["completed_at"] is None
|
||||
|
||||
def test_to_dict_after_success(self):
|
||||
"""成功后的序列化."""
|
||||
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
||||
job.mark_running("test")
|
||||
job.mark_success({"url": "http://x"})
|
||||
d = job.to_dict()
|
||||
assert d["status"] == "success"
|
||||
assert d["progress"] == 100.0
|
||||
assert d["result"] == {"url": "http://x"}
|
||||
assert d["started_at"] is not None
|
||||
assert d["completed_at"] is not None
|
||||
assert d["is_retryable"] is False
|
||||
@@ -1,471 +0,0 @@
|
||||
"""PiP 画中画配置单测.
|
||||
|
||||
纯逻辑模块,覆盖:PiPLayerConfig校验、PiPConfig解析+属性、
|
||||
parse_size_value尺寸解析、calculate_pip_position位置计算。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.pip_config import (
|
||||
ANIMATION_FADE,
|
||||
ANIMATION_SCALE,
|
||||
ANIMATION_SLIDE_BOTTOM,
|
||||
ANIMATION_SLIDE_LEFT,
|
||||
ANIMATION_SLIDE_RIGHT,
|
||||
ANIMATION_SLIDE_TOP,
|
||||
POSITION_BOTTOM_LEFT,
|
||||
POSITION_BOTTOM_RIGHT,
|
||||
POSITION_CENTER,
|
||||
POSITION_TOP_LEFT,
|
||||
POSITION_TOP_RIGHT,
|
||||
PiPConfig,
|
||||
PiPLayerConfig,
|
||||
calculate_pip_position,
|
||||
parse_size_value,
|
||||
)
|
||||
|
||||
|
||||
class TestPiPLayerConfigDefaults:
|
||||
def test_default_source(self):
|
||||
layer = PiPLayerConfig()
|
||||
assert layer.source == ""
|
||||
assert layer.source_type == "asset_id"
|
||||
|
||||
def test_default_position(self):
|
||||
layer = PiPLayerConfig()
|
||||
assert layer.position == POSITION_BOTTOM_RIGHT
|
||||
assert layer.x == 0
|
||||
assert layer.y == 0
|
||||
assert layer.margin == 20
|
||||
|
||||
def test_default_size(self):
|
||||
layer = PiPLayerConfig()
|
||||
assert layer.width == "25%"
|
||||
assert layer.height == ""
|
||||
|
||||
def test_default_style(self):
|
||||
layer = PiPLayerConfig()
|
||||
assert layer.opacity == 1.0
|
||||
assert layer.corner_radius == 0
|
||||
assert layer.border_width == 0
|
||||
assert layer.border_color == "white"
|
||||
|
||||
def test_default_timing(self):
|
||||
layer = PiPLayerConfig()
|
||||
assert layer.start_time == 0.0
|
||||
assert layer.duration == 0.0
|
||||
|
||||
def test_default_animation(self):
|
||||
layer = PiPLayerConfig()
|
||||
assert layer.animation_in == ""
|
||||
assert layer.animation_out == ""
|
||||
assert layer.animation_duration == 0.5
|
||||
|
||||
def test_default_z_index(self):
|
||||
layer = PiPLayerConfig()
|
||||
assert layer.z_index == 1
|
||||
|
||||
|
||||
class TestPiPLayerConfigValidate:
|
||||
def test_valid_with_source(self):
|
||||
layer = PiPLayerConfig(source="asset_123")
|
||||
ok, msg = layer.validate()
|
||||
assert ok is True
|
||||
assert msg == ""
|
||||
|
||||
def test_empty_source_invalid(self):
|
||||
layer = PiPLayerConfig(source="")
|
||||
ok, msg = layer.validate()
|
||||
assert ok is False
|
||||
assert "source" in msg
|
||||
|
||||
def test_invalid_position(self):
|
||||
layer = PiPLayerConfig(source="a", position="invalid")
|
||||
ok, msg = layer.validate()
|
||||
assert ok is False
|
||||
assert "position" in msg
|
||||
|
||||
def test_custom_position_valid(self):
|
||||
layer = PiPLayerConfig(source="a", position="custom")
|
||||
ok, _ = layer.validate()
|
||||
assert ok is True
|
||||
|
||||
def test_all_9_positions_valid(self):
|
||||
positions = [
|
||||
"top_left",
|
||||
"top_center",
|
||||
"top_right",
|
||||
"center_left",
|
||||
"center",
|
||||
"center_right",
|
||||
"bottom_left",
|
||||
"bottom_center",
|
||||
"bottom_right",
|
||||
]
|
||||
for pos in positions:
|
||||
layer = PiPLayerConfig(source="a", position=pos)
|
||||
ok, _ = layer.validate()
|
||||
assert ok is True, f"position {pos} should be valid"
|
||||
|
||||
def test_opacity_below_zero_invalid(self):
|
||||
layer = PiPLayerConfig(source="a", opacity=-0.1)
|
||||
ok, msg = layer.validate()
|
||||
assert ok is False
|
||||
assert "opacity" in msg
|
||||
|
||||
def test_opacity_above_one_invalid(self):
|
||||
layer = PiPLayerConfig(source="a", opacity=1.5)
|
||||
ok, msg = layer.validate()
|
||||
assert ok is False
|
||||
assert "opacity" in msg
|
||||
|
||||
def test_opacity_zero_valid(self):
|
||||
layer = PiPLayerConfig(source="a", opacity=0.0)
|
||||
ok, _ = layer.validate()
|
||||
assert ok is True
|
||||
|
||||
def test_opacity_one_valid(self):
|
||||
layer = PiPLayerConfig(source="a", opacity=1.0)
|
||||
ok, _ = layer.validate()
|
||||
assert ok is True
|
||||
|
||||
def test_negative_corner_radius_invalid(self):
|
||||
layer = PiPLayerConfig(source="a", corner_radius=-1)
|
||||
ok, msg = layer.validate()
|
||||
assert ok is False
|
||||
assert "corner_radius" in msg
|
||||
|
||||
def test_negative_start_time_invalid(self):
|
||||
layer = PiPLayerConfig(source="a", start_time=-1.0)
|
||||
ok, msg = layer.validate()
|
||||
assert ok is False
|
||||
assert "start_time" in msg
|
||||
|
||||
def test_negative_duration_invalid(self):
|
||||
layer = PiPLayerConfig(source="a", duration=-1.0)
|
||||
ok, msg = layer.validate()
|
||||
assert ok is False
|
||||
assert "duration" in msg
|
||||
|
||||
def test_zero_duration_valid(self):
|
||||
layer = PiPLayerConfig(source="a", duration=0.0)
|
||||
ok, _ = layer.validate()
|
||||
assert ok is True
|
||||
|
||||
def test_invalid_animation_in(self):
|
||||
layer = PiPLayerConfig(source="a", animation_in="invalid")
|
||||
ok, msg = layer.validate()
|
||||
assert ok is False
|
||||
assert "入场动画" in msg
|
||||
|
||||
def test_invalid_animation_out(self):
|
||||
layer = PiPLayerConfig(source="a", animation_out="invalid")
|
||||
ok, msg = layer.validate()
|
||||
assert ok is False
|
||||
assert "出场动画" in msg
|
||||
|
||||
def test_empty_animation_valid(self):
|
||||
layer = PiPLayerConfig(source="a", animation_in="", animation_out="")
|
||||
ok, _ = layer.validate()
|
||||
assert ok is True
|
||||
|
||||
def test_all_valid_animations(self):
|
||||
anims = [
|
||||
ANIMATION_FADE,
|
||||
ANIMATION_SLIDE_LEFT,
|
||||
ANIMATION_SLIDE_RIGHT,
|
||||
ANIMATION_SLIDE_TOP,
|
||||
ANIMATION_SLIDE_BOTTOM,
|
||||
ANIMATION_SCALE,
|
||||
]
|
||||
for anim in anims:
|
||||
layer = PiPLayerConfig(source="a", animation_in=anim, animation_out=anim)
|
||||
ok, _ = layer.validate()
|
||||
assert ok is True, f"animation {anim} should be valid"
|
||||
|
||||
def test_negative_animation_duration_invalid(self):
|
||||
layer = PiPLayerConfig(source="a", animation_duration=-0.5)
|
||||
ok, msg = layer.validate()
|
||||
assert ok is False
|
||||
assert "animation_duration" in msg
|
||||
|
||||
|
||||
class TestPiPConfigDefaults:
|
||||
def test_default_disabled(self):
|
||||
config = PiPConfig()
|
||||
assert config.enabled is False
|
||||
assert config.layers == []
|
||||
|
||||
def test_default_layer_count(self):
|
||||
config = PiPConfig()
|
||||
assert config.layer_count == 0
|
||||
|
||||
def test_default_max_z_index(self):
|
||||
config = PiPConfig()
|
||||
assert config.max_z_index == 0
|
||||
|
||||
|
||||
class TestPiPConfigFromDict:
|
||||
def test_none_returns_disabled(self):
|
||||
config = PiPConfig.from_dict(None)
|
||||
assert config.enabled is False
|
||||
assert config.layer_count == 0
|
||||
|
||||
def test_empty_dict_returns_disabled(self):
|
||||
config = PiPConfig.from_dict({})
|
||||
assert config.enabled is False
|
||||
|
||||
def test_enabled_false_returns_disabled(self):
|
||||
config = PiPConfig.from_dict({"enabled": False})
|
||||
assert config.enabled is False
|
||||
|
||||
def test_enabled_no_layers_returns_disabled(self):
|
||||
config = PiPConfig.from_dict({"enabled": True, "layers": []})
|
||||
assert config.enabled is False
|
||||
assert config.layer_count == 0
|
||||
|
||||
def test_single_layer(self):
|
||||
config = PiPConfig.from_dict(
|
||||
{
|
||||
"enabled": True,
|
||||
"layers": [
|
||||
{"source": "asset_1", "position": "top_left"},
|
||||
],
|
||||
}
|
||||
)
|
||||
assert config.enabled is True
|
||||
assert config.layer_count == 1
|
||||
assert config.layers[0].source == "asset_1"
|
||||
assert config.layers[0].position == POSITION_TOP_LEFT
|
||||
|
||||
def test_multiple_layers_sorted_by_z_index(self):
|
||||
config = PiPConfig.from_dict(
|
||||
{
|
||||
"enabled": True,
|
||||
"layers": [
|
||||
{"source": "a", "z_index": 3},
|
||||
{"source": "b", "z_index": 1},
|
||||
{"source": "c", "z_index": 2},
|
||||
],
|
||||
}
|
||||
)
|
||||
assert config.layer_count == 3
|
||||
assert config.layers[0].source == "b" # z=1
|
||||
assert config.layers[1].source == "c" # z=2
|
||||
assert config.layers[2].source == "a" # z=3
|
||||
|
||||
def test_invalid_layer_skipped(self):
|
||||
config = PiPConfig.from_dict(
|
||||
{
|
||||
"enabled": True,
|
||||
"layers": [
|
||||
{"source": "valid_layer", "position": "center"},
|
||||
{"source": "", "position": "center"}, # 空source,无效
|
||||
],
|
||||
}
|
||||
)
|
||||
assert config.layer_count == 1
|
||||
assert config.layers[0].source == "valid_layer"
|
||||
|
||||
def test_all_invalid_layers_returns_disabled(self):
|
||||
config = PiPConfig.from_dict(
|
||||
{
|
||||
"enabled": True,
|
||||
"layers": [
|
||||
{"source": ""}, # 无效
|
||||
],
|
||||
}
|
||||
)
|
||||
assert config.enabled is False
|
||||
assert config.layer_count == 0
|
||||
|
||||
def test_layer_with_full_config(self):
|
||||
config = PiPConfig.from_dict(
|
||||
{
|
||||
"enabled": True,
|
||||
"layers": [
|
||||
{
|
||||
"source": "https://example.com/video.mp4",
|
||||
"source_type": "url",
|
||||
"position": "bottom_right",
|
||||
"width": "30%",
|
||||
"height": "auto",
|
||||
"opacity": 0.8,
|
||||
"corner_radius": 8,
|
||||
"border_width": 2,
|
||||
"border_color": "#00FF00",
|
||||
"start_time": 2.5,
|
||||
"duration": 10.0,
|
||||
"animation_in": "fade",
|
||||
"animation_out": "slide_right",
|
||||
"animation_duration": 0.8,
|
||||
"z_index": 5,
|
||||
"margin": 30,
|
||||
},
|
||||
],
|
||||
}
|
||||
)
|
||||
assert config.layer_count == 1
|
||||
layer = config.layers[0]
|
||||
assert layer.source == "https://example.com/video.mp4"
|
||||
assert layer.source_type == "url"
|
||||
assert layer.width == "30%"
|
||||
assert layer.opacity == 0.8
|
||||
assert layer.corner_radius == 8
|
||||
assert layer.border_width == 2
|
||||
assert layer.start_time == 2.5
|
||||
assert layer.duration == 10.0
|
||||
assert layer.animation_in == "fade"
|
||||
assert layer.animation_out == "slide_right"
|
||||
assert layer.animation_duration == 0.8
|
||||
assert layer.z_index == 5
|
||||
assert layer.margin == 30
|
||||
|
||||
def test_layer_parse_error_skipped(self):
|
||||
config = PiPConfig.from_dict(
|
||||
{
|
||||
"enabled": True,
|
||||
"layers": [
|
||||
{"source": "ok", "opacity": "not_a_number"}, # 会抛ValueError
|
||||
{"source": "valid"},
|
||||
],
|
||||
}
|
||||
)
|
||||
# opacity解析失败会被跳过
|
||||
assert config.layer_count >= 1
|
||||
# 至少valid那个还在
|
||||
sources = [layer.source for layer in config.layers]
|
||||
assert "valid" in sources
|
||||
|
||||
|
||||
class TestPiPConfigProperties:
|
||||
def test_layer_count(self):
|
||||
config = PiPConfig(
|
||||
enabled=True,
|
||||
layers=[
|
||||
PiPLayerConfig(source="a"),
|
||||
PiPLayerConfig(source="b"),
|
||||
PiPLayerConfig(source="c"),
|
||||
],
|
||||
)
|
||||
assert config.layer_count == 3
|
||||
|
||||
def test_max_z_index(self):
|
||||
config = PiPConfig(
|
||||
enabled=True,
|
||||
layers=[
|
||||
PiPLayerConfig(source="a", z_index=5),
|
||||
PiPLayerConfig(source="b", z_index=2),
|
||||
PiPLayerConfig(source="c", z_index=10),
|
||||
],
|
||||
)
|
||||
assert config.max_z_index == 10
|
||||
|
||||
def test_max_z_index_empty(self):
|
||||
config = PiPConfig(enabled=False, layers=[])
|
||||
assert config.max_z_index == 0
|
||||
|
||||
|
||||
class TestParseSizeValue:
|
||||
def test_int_passthrough(self):
|
||||
assert parse_size_value(100, 1000) == 100
|
||||
|
||||
def test_int_zero_clamped_to_1(self):
|
||||
assert parse_size_value(0, 1000) == 1
|
||||
|
||||
def test_int_negative_clamped_to_1(self):
|
||||
assert parse_size_value(-10, 1000) == 1
|
||||
|
||||
def test_percentage_string(self):
|
||||
assert parse_size_value("50%", 1000) == 500
|
||||
|
||||
def test_percentage_25(self):
|
||||
assert parse_size_value("25%", 1920) == 480
|
||||
|
||||
def test_percentage_small(self):
|
||||
assert parse_size_value("1%", 100) == 1
|
||||
|
||||
def test_percentage_zero_clamped(self):
|
||||
assert parse_size_value("0%", 1000) == 1
|
||||
|
||||
def test_invalid_percentage_uses_default(self):
|
||||
assert parse_size_value("abc%", 1000) == 250 # default 25% of 1000
|
||||
|
||||
def test_numeric_string(self):
|
||||
assert parse_size_value("200", 1000) == 200
|
||||
|
||||
def test_empty_string_uses_default(self):
|
||||
assert parse_size_value("", 1000) == 250
|
||||
|
||||
def test_custom_default_pct(self):
|
||||
assert parse_size_value("invalid", 1000, default_pct=0.5) == 500
|
||||
|
||||
def test_float_string(self):
|
||||
"""float字符串会走int()转换路径."""
|
||||
result = parse_size_value("150.5", 1000)
|
||||
assert result >= 1 # 至少不崩
|
||||
|
||||
|
||||
class TestCalculatePipPosition:
|
||||
def test_top_left(self):
|
||||
x, y = calculate_pip_position(POSITION_TOP_LEFT, 1920, 1080, 400, 300, margin=20)
|
||||
assert (x, y) == (20, 20)
|
||||
|
||||
def test_top_right(self):
|
||||
x, y = calculate_pip_position(POSITION_TOP_RIGHT, 1920, 1080, 400, 300, margin=20)
|
||||
assert (x, y) == (1920 - 400 - 20, 20)
|
||||
|
||||
def test_bottom_right(self):
|
||||
x, y = calculate_pip_position(POSITION_BOTTOM_RIGHT, 1920, 1080, 400, 300, margin=20)
|
||||
assert (x, y) == (1920 - 400 - 20, 1080 - 300 - 20)
|
||||
|
||||
def test_bottom_left(self):
|
||||
x, y = calculate_pip_position(POSITION_BOTTOM_LEFT, 1920, 1080, 400, 300, margin=20)
|
||||
assert (x, y) == (20, 1080 - 300 - 20)
|
||||
|
||||
def test_center(self):
|
||||
x, y = calculate_pip_position(POSITION_CENTER, 1920, 1080, 400, 300, margin=20)
|
||||
assert x == (1920 - 400) // 2
|
||||
assert y == (1080 - 300) // 2
|
||||
|
||||
def test_top_center(self):
|
||||
x, y = calculate_pip_position("top_center", 1920, 1080, 400, 300, margin=20)
|
||||
assert x == (1920 - 400) // 2
|
||||
assert y == 20
|
||||
|
||||
def test_bottom_center(self):
|
||||
x, y = calculate_pip_position("bottom_center", 1920, 1080, 400, 300, margin=30)
|
||||
assert x == (1920 - 400) // 2
|
||||
assert y == 1080 - 300 - 30
|
||||
|
||||
def test_center_left(self):
|
||||
x, y = calculate_pip_position("center_left", 1920, 1080, 400, 300, margin=20)
|
||||
assert x == 20
|
||||
assert y == (1080 - 300) // 2
|
||||
|
||||
def test_center_right(self):
|
||||
x, y = calculate_pip_position("center_right", 1920, 1080, 400, 300, margin=20)
|
||||
assert x == 1920 - 400 - 20
|
||||
assert y == (1080 - 300) // 2
|
||||
|
||||
def test_custom_position_pixel_values(self):
|
||||
x, y = calculate_pip_position("custom", 1920, 1080, 400, 300, custom_x=100, custom_y=200)
|
||||
assert (x, y) == (100, 200)
|
||||
|
||||
def test_custom_position_percentage(self):
|
||||
x, y = calculate_pip_position("custom", 1920, 1080, 400, 300, custom_x="10%", custom_y="20%")
|
||||
assert x == 192 # 10% of 1920
|
||||
assert y == 216 # 20% of 1080
|
||||
|
||||
def test_different_margin(self):
|
||||
x, y = calculate_pip_position(POSITION_TOP_LEFT, 1920, 1080, 400, 300, margin=50)
|
||||
assert (x, y) == (50, 50)
|
||||
|
||||
def test_invalid_position_defaults_to_bottom_right(self):
|
||||
x, y = calculate_pip_position("invalid_pos", 1920, 1080, 400, 300, margin=20)
|
||||
assert (x, y) == (1920 - 400 - 20, 1080 - 300 - 20)
|
||||
|
||||
def test_small_output_size(self):
|
||||
x, y = calculate_pip_position(POSITION_CENTER, 100, 100, 50, 50, margin=5)
|
||||
assert x == 25
|
||||
assert y == 25
|
||||
@@ -1,537 +0,0 @@
|
||||
"""plan_generator_utils 单元测试 - wave166
|
||||
|
||||
覆盖:
|
||||
- distribute_assets 素材分配(4种模式 + 边界)
|
||||
- map_clip_types_for_mode clip类型映射(4种模式)
|
||||
- generate_default_clips 默认片段生成(4种模式 + 边界)
|
||||
- create_clips_from_configs 从模板配置创建
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from packages.domain.edit_plan_clip import EditPlanClip
|
||||
from packages.domain.editing_mode import EditingMode
|
||||
from packages.domain.plan_generator_utils import (
|
||||
DEFAULT_CLIP_DURATION,
|
||||
create_clips_from_configs,
|
||||
distribute_assets,
|
||||
generate_default_clips,
|
||||
map_clip_types_for_mode,
|
||||
)
|
||||
from packages.domain.template_clip_config import ClipType, TemplateClipConfig
|
||||
|
||||
# ============================================================
|
||||
# 辅助函数
|
||||
# ============================================================
|
||||
|
||||
|
||||
def _make_main_clip(plan_id: str, order: int = 0) -> EditPlanClip:
|
||||
return EditPlanClip.create(
|
||||
plan_id=plan_id,
|
||||
clip_type=ClipType.MAIN.value,
|
||||
order=order,
|
||||
duration=5.0,
|
||||
)
|
||||
|
||||
|
||||
def _make_clips(plan_id: str, count: int, clip_type: str = "main") -> list[EditPlanClip]:
|
||||
return [
|
||||
EditPlanClip.create(
|
||||
plan_id=plan_id,
|
||||
clip_type=clip_type,
|
||||
order=i,
|
||||
duration=5.0,
|
||||
)
|
||||
for i in range(count)
|
||||
]
|
||||
|
||||
|
||||
# ============================================================
|
||||
# distribute_assets - ONE_TAKE
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestDistributeOneTake:
|
||||
def test_equal_count(self):
|
||||
clips = _make_clips("p1", 3)
|
||||
assets = ["a1", "a2", "a3"]
|
||||
distribute_assets(clips, assets, EditingMode.ONE_TAKE.value)
|
||||
assert clips[0].asset_id == "a1"
|
||||
assert clips[1].asset_id == "a2"
|
||||
assert clips[2].asset_id == "a3"
|
||||
|
||||
def test_more_clips_than_assets(self):
|
||||
clips = _make_clips("p1", 5)
|
||||
assets = ["a1", "a2"]
|
||||
distribute_assets(clips, assets, EditingMode.ONE_TAKE.value)
|
||||
assert clips[0].asset_id == "a1"
|
||||
assert clips[1].asset_id == "a2"
|
||||
assert clips[2].asset_id == "" # 没分配到
|
||||
|
||||
def test_more_assets_than_clips(self):
|
||||
clips = _make_clips("p1", 2)
|
||||
assets = ["a1", "a2", "a3"]
|
||||
distribute_assets(clips, assets, EditingMode.ONE_TAKE.value)
|
||||
assert clips[0].asset_id == "a1"
|
||||
assert clips[1].asset_id == "a2"
|
||||
|
||||
def test_empty_assets(self):
|
||||
clips = _make_clips("p1", 3)
|
||||
distribute_assets(clips, [], EditingMode.ONE_TAKE.value)
|
||||
for c in clips:
|
||||
assert c.asset_id == ""
|
||||
|
||||
def test_empty_clips(self):
|
||||
# 不报错即可
|
||||
distribute_assets([], ["a1", "a2"], EditingMode.ONE_TAKE.value)
|
||||
|
||||
def test_only_main_clips_get_assigned(self):
|
||||
# intro/outro 不应该被分配
|
||||
clips = []
|
||||
clips.append(EditPlanClip.create("p1", clip_type="intro", order=0, duration=3.0))
|
||||
clips.append(_make_main_clip("p1", order=1))
|
||||
clips.append(EditPlanClip.create("p1", clip_type="outro", order=2, duration=3.0))
|
||||
assets = ["a1"]
|
||||
distribute_assets(clips, assets, EditingMode.ONE_TAKE.value)
|
||||
assert clips[0].asset_id == "" # intro 无
|
||||
assert clips[1].asset_id == "a1" # main 有
|
||||
assert clips[2].asset_id == "" # outro 无
|
||||
|
||||
|
||||
# ============================================================
|
||||
# distribute_assets - PIP
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestDistributePip:
|
||||
def test_first_asset_to_main(self):
|
||||
clips = _make_clips("p1", 3)
|
||||
# 第一个main是背景,其余改为overlay
|
||||
map_clip_types_for_mode(clips, EditingMode.PIP.value)
|
||||
assets = ["a1", "a2", "a3"]
|
||||
distribute_assets(clips, assets, EditingMode.PIP.value)
|
||||
assert clips[0].asset_id == "a1" # main → 背景
|
||||
assert clips[1].asset_id == "a2" # overlay
|
||||
assert clips[2].asset_id == "a3" # overlay
|
||||
|
||||
def test_single_asset(self):
|
||||
clips = _make_clips("p1", 1)
|
||||
map_clip_types_for_mode(clips, EditingMode.PIP.value)
|
||||
assets = ["a1"]
|
||||
distribute_assets(clips, assets, EditingMode.PIP.value)
|
||||
assert clips[0].asset_id == "a1"
|
||||
|
||||
def test_only_main_clip_with_no_overlays(self):
|
||||
clips = _make_clips("p1", 1)
|
||||
map_clip_types_for_mode(clips, EditingMode.PIP.value)
|
||||
assets = ["a1", "a2", "a3"] # 多余素材
|
||||
distribute_assets(clips, assets, EditingMode.PIP.value)
|
||||
assert clips[0].asset_id == "a1"
|
||||
|
||||
|
||||
# ============================================================
|
||||
# distribute_assets - VOICE_OVER
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestDistributeVoiceOver:
|
||||
def test_assets_to_main_clips(self):
|
||||
clips = _make_clips("p1", 3)
|
||||
assets = ["a1", "a2", "a3"]
|
||||
distribute_assets(clips, assets, EditingMode.VOICE_OVER.value)
|
||||
assert clips[0].asset_id == "a1"
|
||||
assert clips[1].asset_id == "a2"
|
||||
assert clips[2].asset_id == "a3"
|
||||
|
||||
def test_more_clips_than_assets(self):
|
||||
clips = _make_clips("p1", 5)
|
||||
assets = ["a1", "a2"]
|
||||
distribute_assets(clips, assets, EditingMode.VOICE_OVER.value)
|
||||
assert clips[0].asset_id == "a1"
|
||||
assert clips[1].asset_id == "a2"
|
||||
assert clips[2].asset_id == ""
|
||||
|
||||
|
||||
# ============================================================
|
||||
# distribute_assets - VOICE_PIP
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestDistributeVoicePip:
|
||||
def test_three_assets_three_roles(self):
|
||||
clips = _make_clips("p1", 3)
|
||||
map_clip_types_for_mode(clips, EditingMode.VOICE_PIP.value)
|
||||
assets = ["a1", "a2", "a3"]
|
||||
distribute_assets(clips, assets, EditingMode.VOICE_PIP.value)
|
||||
assert clips[0].clip_type == "background"
|
||||
assert clips[0].asset_id == "a1"
|
||||
assert clips[1].clip_type == "corner_voice"
|
||||
assert clips[1].asset_id == "a2"
|
||||
assert clips[2].clip_type == "b_roll"
|
||||
assert clips[2].asset_id == "a3"
|
||||
|
||||
def test_single_asset(self):
|
||||
clips = _make_clips("p1", 1)
|
||||
map_clip_types_for_mode(clips, EditingMode.VOICE_PIP.value)
|
||||
assets = ["a1"]
|
||||
distribute_assets(clips, assets, EditingMode.VOICE_PIP.value)
|
||||
assert clips[0].clip_type == "background"
|
||||
assert clips[0].asset_id == "a1"
|
||||
|
||||
def test_two_assets(self):
|
||||
clips = _make_clips("p1", 2)
|
||||
map_clip_types_for_mode(clips, EditingMode.VOICE_PIP.value)
|
||||
assets = ["a1", "a2"]
|
||||
distribute_assets(clips, assets, EditingMode.VOICE_PIP.value)
|
||||
assert clips[0].asset_id == "a1"
|
||||
assert clips[1].asset_id == "a2"
|
||||
|
||||
|
||||
# ============================================================
|
||||
# distribute_assets - 边界情况
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestDistributeEdgeCases:
|
||||
def test_unknown_mode_falls_back_to_one_take(self):
|
||||
clips = _make_clips("p1", 2)
|
||||
assets = ["a1", "a2"]
|
||||
distribute_assets(clips, assets, "unknown_mode")
|
||||
assert clips[0].asset_id == "a1"
|
||||
assert clips[1].asset_id == "a2"
|
||||
|
||||
def test_none_clips_no_crash(self):
|
||||
# 空列表
|
||||
distribute_assets([], ["a1"], EditingMode.ONE_TAKE.value)
|
||||
|
||||
def test_none_assets_no_crash(self):
|
||||
clips = _make_clips("p1", 2)
|
||||
distribute_assets(clips, [], EditingMode.ONE_TAKE.value)
|
||||
for c in clips:
|
||||
assert c.asset_id == ""
|
||||
|
||||
|
||||
# ============================================================
|
||||
# map_clip_types_for_mode
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestMapClipTypesForMode:
|
||||
def test_one_take_unchanged(self):
|
||||
clips = _make_clips("p1", 3)
|
||||
original_types = [c.clip_type for c in clips]
|
||||
map_clip_types_for_mode(clips, EditingMode.ONE_TAKE.value)
|
||||
assert [c.clip_type for c in clips] == original_types
|
||||
|
||||
def test_voice_over_unchanged(self):
|
||||
clips = _make_clips("p1", 3)
|
||||
map_clip_types_for_mode(clips, EditingMode.VOICE_OVER.value)
|
||||
for c in clips:
|
||||
assert c.clip_type == ClipType.MAIN.value
|
||||
|
||||
def test_pip_first_stays_main_rest_overlay(self):
|
||||
clips = _make_clips("p1", 4)
|
||||
map_clip_types_for_mode(clips, EditingMode.PIP.value)
|
||||
assert clips[0].clip_type == ClipType.MAIN.value
|
||||
assert clips[1].clip_type == "overlay"
|
||||
assert clips[2].clip_type == "overlay"
|
||||
assert clips[3].clip_type == "overlay"
|
||||
|
||||
def test_pip_single_clip_stays_main(self):
|
||||
clips = _make_clips("p1", 1)
|
||||
map_clip_types_for_mode(clips, EditingMode.PIP.value)
|
||||
assert clips[0].clip_type == ClipType.MAIN.value
|
||||
|
||||
def test_voice_pip_mapping(self):
|
||||
clips = _make_clips("p1", 5)
|
||||
map_clip_types_for_mode(clips, EditingMode.VOICE_PIP.value)
|
||||
assert clips[0].clip_type == "background"
|
||||
assert clips[1].clip_type == "corner_voice"
|
||||
assert clips[2].clip_type == "b_roll"
|
||||
assert clips[3].clip_type == "b_roll"
|
||||
assert clips[4].clip_type == "b_roll"
|
||||
|
||||
def test_voice_pip_two_clips(self):
|
||||
clips = _make_clips("p1", 2)
|
||||
map_clip_types_for_mode(clips, EditingMode.VOICE_PIP.value)
|
||||
assert clips[0].clip_type == "background"
|
||||
assert clips[1].clip_type == "corner_voice"
|
||||
|
||||
def test_voice_pip_single_clip(self):
|
||||
clips = _make_clips("p1", 1)
|
||||
map_clip_types_for_mode(clips, EditingMode.VOICE_PIP.value)
|
||||
assert clips[0].clip_type == "background"
|
||||
|
||||
def test_non_main_clips_unchanged(self):
|
||||
clips = [
|
||||
EditPlanClip.create("p1", clip_type="intro", order=0, duration=3.0),
|
||||
_make_main_clip("p1", order=1),
|
||||
EditPlanClip.create("p1", clip_type="outro", order=2, duration=3.0),
|
||||
]
|
||||
map_clip_types_for_mode(clips, EditingMode.VOICE_PIP.value)
|
||||
assert clips[0].clip_type == "intro"
|
||||
assert clips[1].clip_type == "background" # main 被改了
|
||||
assert clips[2].clip_type == "outro"
|
||||
|
||||
def test_empty_clips_no_error(self):
|
||||
map_clip_types_for_mode([], EditingMode.PIP.value)
|
||||
|
||||
def test_no_main_clips_no_error(self):
|
||||
clips = [
|
||||
EditPlanClip.create("p1", clip_type="intro", order=0, duration=3.0),
|
||||
]
|
||||
map_clip_types_for_mode(clips, EditingMode.PIP.value)
|
||||
assert clips[0].clip_type == "intro"
|
||||
|
||||
|
||||
# ============================================================
|
||||
# generate_default_clips
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestGenerateDefaultClipsOneTake:
|
||||
def test_basic(self):
|
||||
clips = generate_default_clips("p1", EditingMode.ONE_TAKE.value, 3)
|
||||
assert len(clips) == 3
|
||||
for c in clips:
|
||||
assert c.clip_type == ClipType.MAIN.value
|
||||
assert c.plan_id == "p1"
|
||||
|
||||
def test_order_sequential(self):
|
||||
clips = generate_default_clips("p1", EditingMode.ONE_TAKE.value, 5)
|
||||
for i, c in enumerate(clips):
|
||||
assert c.order == i
|
||||
|
||||
def test_zero_assets_at_least_one(self):
|
||||
clips = generate_default_clips("p1", EditingMode.ONE_TAKE.value, 0)
|
||||
assert len(clips) == 1
|
||||
|
||||
def test_negative_assets_at_least_one(self):
|
||||
clips = generate_default_clips("p1", EditingMode.ONE_TAKE.value, -5)
|
||||
assert len(clips) == 1
|
||||
|
||||
def test_default_duration(self):
|
||||
clips = generate_default_clips("p1", EditingMode.ONE_TAKE.value, 1)
|
||||
assert clips[0].duration == DEFAULT_CLIP_DURATION
|
||||
|
||||
|
||||
class TestGenerateDefaultClipsPip:
|
||||
def test_one_asset(self):
|
||||
clips = generate_default_clips("p1", EditingMode.PIP.value, 1)
|
||||
assert len(clips) == 1
|
||||
assert clips[0].clip_type == ClipType.MAIN.value
|
||||
|
||||
def test_three_assets(self):
|
||||
clips = generate_default_clips("p1", EditingMode.PIP.value, 3)
|
||||
assert len(clips) == 3
|
||||
assert clips[0].clip_type == ClipType.MAIN.value
|
||||
assert clips[1].clip_type == "overlay"
|
||||
assert clips[2].clip_type == "overlay"
|
||||
|
||||
def test_zero_assets(self):
|
||||
clips = generate_default_clips("p1", EditingMode.PIP.value, 0)
|
||||
assert len(clips) >= 1
|
||||
assert clips[0].clip_type == ClipType.MAIN.value
|
||||
|
||||
|
||||
class TestGenerateDefaultClipsVoiceOver:
|
||||
def test_basic(self):
|
||||
clips = generate_default_clips("p1", EditingMode.VOICE_OVER.value, 3)
|
||||
assert len(clips) == 3
|
||||
for c in clips:
|
||||
assert c.clip_type == ClipType.MAIN.value
|
||||
|
||||
def test_has_b_roll_config(self):
|
||||
clips = generate_default_clips("p1", EditingMode.VOICE_OVER.value, 2)
|
||||
# VOICE_OVER 标记 role=b_roll
|
||||
assert clips[0].config.get("role") == "b_roll"
|
||||
|
||||
|
||||
class TestGenerateDefaultClipsVoicePip:
|
||||
def test_one_asset(self):
|
||||
clips = generate_default_clips("p1", EditingMode.VOICE_PIP.value, 1)
|
||||
assert len(clips) == 1
|
||||
assert clips[0].clip_type == "background"
|
||||
|
||||
def test_two_assets(self):
|
||||
clips = generate_default_clips("p1", EditingMode.VOICE_PIP.value, 2)
|
||||
assert len(clips) == 2
|
||||
assert clips[0].clip_type == "background"
|
||||
assert clips[1].clip_type == "corner_voice"
|
||||
|
||||
def test_five_assets(self):
|
||||
clips = generate_default_clips("p1", EditingMode.VOICE_PIP.value, 5)
|
||||
assert len(clips) == 5
|
||||
assert clips[0].clip_type == "background"
|
||||
assert clips[1].clip_type == "corner_voice"
|
||||
assert clips[2].clip_type == "b_roll"
|
||||
assert clips[3].clip_type == "b_roll"
|
||||
assert clips[4].clip_type == "b_roll"
|
||||
|
||||
def test_zero_assets(self):
|
||||
clips = generate_default_clips("p1", EditingMode.VOICE_PIP.value, 0)
|
||||
assert len(clips) >= 1
|
||||
assert clips[0].clip_type == "background"
|
||||
|
||||
|
||||
class TestGenerateDefaultClipsUnknownMode:
|
||||
def test_falls_back_to_one_take(self):
|
||||
clips = generate_default_clips("p1", "unknown_mode", 3)
|
||||
assert len(clips) == 3
|
||||
for c in clips:
|
||||
assert c.clip_type == ClipType.MAIN.value
|
||||
|
||||
|
||||
# ============================================================
|
||||
# create_clips_from_configs
|
||||
# ============================================================
|
||||
|
||||
|
||||
def _make_template_config(
|
||||
cfg_id: str,
|
||||
order: int,
|
||||
clip_type: ClipType = ClipType.MAIN,
|
||||
min_dur: float = 0,
|
||||
max_dur: float = 0,
|
||||
) -> TemplateClipConfig:
|
||||
return TemplateClipConfig(
|
||||
id=cfg_id,
|
||||
template_id="t1",
|
||||
clip_type=clip_type,
|
||||
order=order,
|
||||
min_duration=min_dur,
|
||||
max_duration=max_dur,
|
||||
transition_effect="cut",
|
||||
config={},
|
||||
)
|
||||
|
||||
|
||||
class TestCreateClipsFromConfigs:
|
||||
def test_empty_configs(self):
|
||||
result = create_clips_from_configs("p1", [])
|
||||
assert result == []
|
||||
|
||||
def test_single_config(self):
|
||||
configs = [_make_template_config("c1", 0, min_dur=3.0, max_dur=7.0)]
|
||||
result = create_clips_from_configs("p1", configs)
|
||||
assert len(result) == 1
|
||||
assert result[0].plan_id == "p1"
|
||||
assert result[0].template_clip_config_id == "c1"
|
||||
# 平均时长 = (3+7)/2 = 5.0
|
||||
assert result[0].duration == pytest.approx(5.0)
|
||||
|
||||
def test_duration_min_only(self):
|
||||
configs = [_make_template_config("c1", 0, min_dur=4.0)]
|
||||
result = create_clips_from_configs("p1", configs)
|
||||
assert result[0].duration == 4.0
|
||||
|
||||
def test_duration_max_only(self):
|
||||
configs = [_make_template_config("c1", 0, max_dur=6.0)]
|
||||
result = create_clips_from_configs("p1", configs)
|
||||
assert result[0].duration == 6.0
|
||||
|
||||
def test_duration_default_when_no_bounds(self):
|
||||
configs = [_make_template_config("c1", 0)]
|
||||
result = create_clips_from_configs("p1", configs)
|
||||
assert result[0].duration == DEFAULT_CLIP_DURATION
|
||||
|
||||
def test_sorted_by_order(self):
|
||||
configs = [
|
||||
_make_template_config("c_third", 2),
|
||||
_make_template_config("c_first", 0),
|
||||
_make_template_config("c_second", 1),
|
||||
]
|
||||
result = create_clips_from_configs("p1", configs)
|
||||
assert len(result) == 3
|
||||
assert result[0].template_clip_config_id == "c_first"
|
||||
assert result[1].template_clip_config_id == "c_second"
|
||||
assert result[2].template_clip_config_id == "c_third"
|
||||
assert result[0].order == 0
|
||||
assert result[1].order == 1
|
||||
assert result[2].order == 2
|
||||
|
||||
def test_clip_type_preserved(self):
|
||||
configs = [
|
||||
TemplateClipConfig(
|
||||
id="c_intro",
|
||||
template_id="t1",
|
||||
clip_type=ClipType.INTRO,
|
||||
order=0,
|
||||
min_duration=3.0,
|
||||
max_duration=3.0,
|
||||
transition_effect="cut",
|
||||
config={},
|
||||
),
|
||||
TemplateClipConfig(
|
||||
id="c_main",
|
||||
template_id="t1",
|
||||
clip_type=ClipType.MAIN,
|
||||
order=1,
|
||||
min_duration=5.0,
|
||||
max_duration=5.0,
|
||||
transition_effect="cut",
|
||||
config={},
|
||||
),
|
||||
]
|
||||
result = create_clips_from_configs("p1", configs)
|
||||
assert result[0].clip_type == ClipType.INTRO.value
|
||||
assert result[1].clip_type == ClipType.MAIN.value
|
||||
|
||||
def test_playback_speed_from_config(self):
|
||||
configs = [
|
||||
TemplateClipConfig(
|
||||
id="c1",
|
||||
template_id="t1",
|
||||
clip_type=ClipType.MAIN,
|
||||
order=0,
|
||||
min_duration=5.0,
|
||||
max_duration=5.0,
|
||||
transition_effect="cut",
|
||||
config={"playback_speed": 1.5},
|
||||
)
|
||||
]
|
||||
result = create_clips_from_configs("p1", configs)
|
||||
assert result[0].playback_speed == pytest.approx(1.5)
|
||||
|
||||
def test_speed_ratio_fallback(self):
|
||||
# 兼容 speed_ratio 字段名
|
||||
configs = [
|
||||
TemplateClipConfig(
|
||||
id="c1",
|
||||
template_id="t1",
|
||||
clip_type=ClipType.MAIN,
|
||||
order=0,
|
||||
min_duration=5.0,
|
||||
max_duration=5.0,
|
||||
transition_effect="cut",
|
||||
config={"speed_ratio": 0.8},
|
||||
)
|
||||
]
|
||||
result = create_clips_from_configs("p1", configs)
|
||||
assert result[0].playback_speed == pytest.approx(0.8)
|
||||
|
||||
def test_default_playback_speed(self):
|
||||
configs = [_make_template_config("c1", 0, min_dur=5.0, max_dur=5.0)]
|
||||
result = create_clips_from_configs("p1", configs)
|
||||
assert result[0].playback_speed == pytest.approx(1.0)
|
||||
|
||||
def test_transition_effect_preserved(self):
|
||||
configs = [
|
||||
TemplateClipConfig(
|
||||
id="c1",
|
||||
template_id="t1",
|
||||
clip_type=ClipType.MAIN,
|
||||
order=0,
|
||||
min_duration=5.0,
|
||||
max_duration=5.0,
|
||||
transition_effect="fade",
|
||||
config={},
|
||||
)
|
||||
]
|
||||
result = create_clips_from_configs("p1", configs)
|
||||
assert result[0].transition_effect == "fade"
|
||||
|
||||
def test_returns_edit_plan_clip_objects(self):
|
||||
configs = [_make_template_config("c1", 0, min_dur=3.0, max_dur=5.0)]
|
||||
result = create_clips_from_configs("p1", configs)
|
||||
assert isinstance(result[0], EditPlanClip)
|
||||
@@ -1,575 +0,0 @@
|
||||
"""quota 单测.
|
||||
|
||||
domain 层配额系统纯逻辑模块,0 外部依赖。
|
||||
覆盖:枚举常量、QuotaTier、QUOTA_TIERS常量、QuotaWarningLevel、
|
||||
QuotaCheckResult、QuotaRegistry注册/查询、QuotaChecker检查/告警级别。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
from packages.domain.quota import (
|
||||
QUOTA_TIERS,
|
||||
QuotaChecker,
|
||||
QuotaCheckResult,
|
||||
QuotaDimension,
|
||||
QuotaRegistry,
|
||||
QuotaTier,
|
||||
QuotaWarningLevel,
|
||||
get_warning_level,
|
||||
quota_checker,
|
||||
quota_registry,
|
||||
)
|
||||
|
||||
|
||||
class TestQuotaDimension:
|
||||
"""QuotaDimension 枚举测试."""
|
||||
|
||||
def test_member_count(self):
|
||||
"""内置维度数量."""
|
||||
assert len(QuotaDimension) == 11
|
||||
|
||||
def test_storage_gb(self):
|
||||
"""存储维度值."""
|
||||
assert QuotaDimension.STORAGE_GB.value == "storage_gb"
|
||||
|
||||
def test_videos_per_month(self):
|
||||
"""每月视频数维度."""
|
||||
assert QuotaDimension.VIDEOS_PER_MONTH.value == "videos_per_month"
|
||||
|
||||
def test_max_concurrent(self):
|
||||
"""并发数维度."""
|
||||
assert QuotaDimension.MAX_CONCURRENT.value == "max_concurrent"
|
||||
|
||||
def test_max_templates(self):
|
||||
"""模板数维度."""
|
||||
assert QuotaDimension.MAX_TEMPLATES.value == "max_templates"
|
||||
|
||||
def test_max_titles(self):
|
||||
"""标题库维度."""
|
||||
assert QuotaDimension.MAX_TITLES.value == "max_titles"
|
||||
|
||||
def test_max_voiceovers(self):
|
||||
"""配音库维度."""
|
||||
assert QuotaDimension.MAX_VOICEOVERS.value == "max_voiceovers"
|
||||
|
||||
def test_ai_voice_enabled(self):
|
||||
"""AI配音开关维度."""
|
||||
assert QuotaDimension.AI_VOICE_ENABLED.value == "ai_voice_enabled"
|
||||
|
||||
def test_ai_voice_credits(self):
|
||||
"""AI配音积分维度."""
|
||||
assert QuotaDimension.AI_VOICE_CREDITS.value == "ai_voice_credits"
|
||||
|
||||
def test_all_values_are_strings(self):
|
||||
"""所有枚举值都是字符串."""
|
||||
for dim in QuotaDimension:
|
||||
assert isinstance(dim.value, str)
|
||||
assert len(dim.value) > 0
|
||||
|
||||
|
||||
class TestQuotaTier:
|
||||
"""QuotaTier 数据类测试."""
|
||||
|
||||
def test_create_empty(self):
|
||||
"""创建空配额等级."""
|
||||
tier = QuotaTier(name="test")
|
||||
assert tier.name == "test"
|
||||
assert tier.limits == {}
|
||||
|
||||
def test_create_with_limits(self):
|
||||
"""创建带限制的配额等级."""
|
||||
tier = QuotaTier(name="pro", limits={"storage": 100, "videos": 50})
|
||||
assert tier.name == "pro"
|
||||
assert tier.get_limit("storage") == 100
|
||||
assert tier.get_limit("videos") == 50
|
||||
|
||||
def test_get_limit_undefined_returns_zero(self):
|
||||
"""未定义的维度返回 0."""
|
||||
tier = QuotaTier(name="test")
|
||||
assert tier.get_limit("nonexistent") == 0
|
||||
|
||||
def test_is_unlimited_inf(self):
|
||||
"""inf 视为不限量."""
|
||||
tier = QuotaTier(name="test", limits={"templates": float("inf")})
|
||||
assert tier.is_unlimited("templates") is True
|
||||
|
||||
def test_is_unlimited_finite(self):
|
||||
"""有限值不是不限量."""
|
||||
tier = QuotaTier(name="test", limits={"storage": 100})
|
||||
assert tier.is_unlimited("storage") is False
|
||||
|
||||
def test_is_unlimited_undefined(self):
|
||||
"""未定义的维度默认不限量."""
|
||||
tier = QuotaTier(name="test")
|
||||
# 未定义的 key 取默认值 inf,因此 is_unlimited 应该返回 True
|
||||
assert tier.is_unlimited("unknown") is True
|
||||
|
||||
|
||||
class TestQuotaTiers:
|
||||
"""QUOTA_TIERS 常量测试."""
|
||||
|
||||
def test_three_tiers_exist(self):
|
||||
"""三个套餐等级都存在."""
|
||||
assert "free" in QUOTA_TIERS
|
||||
assert "basic" in QUOTA_TIERS
|
||||
assert "premium" in QUOTA_TIERS
|
||||
|
||||
def test_free_storage(self):
|
||||
"""免费版 2GB 存储."""
|
||||
assert QUOTA_TIERS["free"].get_limit(QuotaDimension.STORAGE_GB) == 2
|
||||
|
||||
def test_free_videos_per_month(self):
|
||||
"""免费版 5个视频/月."""
|
||||
assert QUOTA_TIERS["free"].get_limit(QuotaDimension.VIDEOS_PER_MONTH) == 5
|
||||
|
||||
def test_free_ai_voice_disabled(self):
|
||||
"""免费版无AI配音."""
|
||||
assert QUOTA_TIERS["free"].get_limit(QuotaDimension.AI_VOICE_ENABLED) == 0
|
||||
|
||||
def test_basic_storage(self):
|
||||
"""基础版 20GB 存储."""
|
||||
assert QUOTA_TIERS["basic"].get_limit(QuotaDimension.STORAGE_GB) == 20
|
||||
|
||||
def test_basic_videos(self):
|
||||
"""基础版 30视频/月."""
|
||||
assert QUOTA_TIERS["basic"].get_limit(QuotaDimension.VIDEOS_PER_MONTH) == 30
|
||||
|
||||
def test_basic_ai_voice_enabled(self):
|
||||
"""基础版有AI配音."""
|
||||
assert QUOTA_TIERS["basic"].get_limit(QuotaDimension.AI_VOICE_ENABLED) == 1
|
||||
|
||||
def test_basic_ai_voice_credits(self):
|
||||
"""基础版 100 AI配音积分."""
|
||||
assert QUOTA_TIERS["basic"].get_limit(QuotaDimension.AI_VOICE_CREDITS) == 100
|
||||
|
||||
def test_premium_storage(self):
|
||||
"""高级版 100GB 存储."""
|
||||
assert QUOTA_TIERS["premium"].get_limit(QuotaDimension.STORAGE_GB) == 100
|
||||
|
||||
def test_premium_videos(self):
|
||||
"""高级版 100视频/月."""
|
||||
assert QUOTA_TIERS["premium"].get_limit(QuotaDimension.VIDEOS_PER_MONTH) == 100
|
||||
|
||||
def test_premium_templates_unlimited(self):
|
||||
"""高级版模板不限量."""
|
||||
assert QUOTA_TIERS["premium"].is_unlimited(QuotaDimension.MAX_TEMPLATES)
|
||||
|
||||
def test_premium_multi_platform_enabled(self):
|
||||
"""高级版多平台发布."""
|
||||
assert QUOTA_TIERS["premium"].get_limit(QuotaDimension.MULTI_PLATFORM_ENABLED) == 1
|
||||
|
||||
def test_storage_monotonic(self):
|
||||
"""存储量随套餐升级单调递增."""
|
||||
free = QUOTA_TIERS["free"].get_limit(QuotaDimension.STORAGE_GB)
|
||||
basic = QUOTA_TIERS["basic"].get_limit(QuotaDimension.STORAGE_GB)
|
||||
premium = QUOTA_TIERS["premium"].get_limit(QuotaDimension.STORAGE_GB)
|
||||
assert free < basic < premium
|
||||
|
||||
def test_videos_monotonic(self):
|
||||
"""视频数随套餐升级单调递增."""
|
||||
free = QUOTA_TIERS["free"].get_limit(QuotaDimension.VIDEOS_PER_MONTH)
|
||||
basic = QUOTA_TIERS["basic"].get_limit(QuotaDimension.VIDEOS_PER_MONTH)
|
||||
premium = QUOTA_TIERS["premium"].get_limit(QuotaDimension.VIDEOS_PER_MONTH)
|
||||
assert free < basic < premium
|
||||
|
||||
|
||||
class TestQuotaWarningLevel:
|
||||
"""告警级别常量测试."""
|
||||
|
||||
def test_levels_defined(self):
|
||||
"""四个级别都有定义."""
|
||||
assert QuotaWarningLevel.NORMAL == "normal"
|
||||
assert QuotaWarningLevel.WARNING == "warning"
|
||||
assert QuotaWarningLevel.CRITICAL == "critical"
|
||||
assert QuotaWarningLevel.EXCEEDED == "exceeded"
|
||||
|
||||
def test_four_distinct_levels(self):
|
||||
"""四个级别各不相同."""
|
||||
levels = {
|
||||
QuotaWarningLevel.NORMAL,
|
||||
QuotaWarningLevel.WARNING,
|
||||
QuotaWarningLevel.CRITICAL,
|
||||
QuotaWarningLevel.EXCEEDED,
|
||||
}
|
||||
assert len(levels) == 4
|
||||
|
||||
|
||||
class TestQuotaCheckResult:
|
||||
"""QuotaCheckResult 测试."""
|
||||
|
||||
def test_usage_percent_normal(self):
|
||||
"""正常使用百分比."""
|
||||
result = QuotaCheckResult(
|
||||
allowed=True,
|
||||
dimension="storage",
|
||||
limit=100,
|
||||
used=30,
|
||||
remaining=70,
|
||||
warning_level=QuotaWarningLevel.NORMAL,
|
||||
)
|
||||
assert result.usage_percent == 30.0
|
||||
|
||||
def test_usage_percent_zero_used(self):
|
||||
"""使用量为 0."""
|
||||
result = QuotaCheckResult(
|
||||
allowed=True,
|
||||
dimension="storage",
|
||||
limit=100,
|
||||
used=0,
|
||||
remaining=100,
|
||||
warning_level=QuotaWarningLevel.NORMAL,
|
||||
)
|
||||
assert result.usage_percent == 0.0
|
||||
|
||||
def test_usage_percent_exactly_100(self):
|
||||
"""刚好用完."""
|
||||
result = QuotaCheckResult(
|
||||
allowed=False,
|
||||
dimension="storage",
|
||||
limit=100,
|
||||
used=100,
|
||||
remaining=0,
|
||||
warning_level=QuotaWarningLevel.EXCEEDED,
|
||||
)
|
||||
assert result.usage_percent == 100.0
|
||||
|
||||
def test_usage_percent_over_limit_capped(self):
|
||||
"""超出限制时封顶 100%."""
|
||||
result = QuotaCheckResult(
|
||||
allowed=False,
|
||||
dimension="storage",
|
||||
limit=100,
|
||||
used=150,
|
||||
remaining=0,
|
||||
warning_level=QuotaWarningLevel.EXCEEDED,
|
||||
)
|
||||
assert result.usage_percent == 100.0
|
||||
|
||||
def test_usage_percent_unlimited(self):
|
||||
"""不限量时使用率为 0."""
|
||||
result = QuotaCheckResult(
|
||||
allowed=True,
|
||||
dimension="templates",
|
||||
limit=float("inf"),
|
||||
used=1000,
|
||||
remaining=float("inf"),
|
||||
warning_level=QuotaWarningLevel.NORMAL,
|
||||
)
|
||||
assert result.usage_percent == 0.0
|
||||
|
||||
def test_usage_percent_zero_limit_with_usage(self):
|
||||
"""限制为 0 但有使用量,返回 100%."""
|
||||
result = QuotaCheckResult(
|
||||
allowed=False,
|
||||
dimension="ai_voice",
|
||||
limit=0,
|
||||
used=1,
|
||||
remaining=0,
|
||||
warning_level=QuotaWarningLevel.EXCEEDED,
|
||||
)
|
||||
assert result.usage_percent == 100.0
|
||||
|
||||
def test_usage_percent_zero_limit_no_usage(self):
|
||||
"""限制为 0 且无使用量,返回 0%."""
|
||||
result = QuotaCheckResult(
|
||||
allowed=True,
|
||||
dimension="ai_voice",
|
||||
limit=0,
|
||||
used=0,
|
||||
remaining=0,
|
||||
warning_level=QuotaWarningLevel.NORMAL,
|
||||
)
|
||||
assert result.usage_percent == 0.0
|
||||
|
||||
|
||||
class TestGetWarningLevel:
|
||||
"""get_warning_level 便捷函数测试."""
|
||||
|
||||
def test_zero_usage(self):
|
||||
"""0% 使用 - normal."""
|
||||
assert get_warning_level(0, 100) == QuotaWarningLevel.NORMAL
|
||||
|
||||
def test_below_80_percent(self):
|
||||
"""低于80% - normal."""
|
||||
assert get_warning_level(50, 100) == QuotaWarningLevel.NORMAL
|
||||
assert get_warning_level(79, 100) == QuotaWarningLevel.NORMAL
|
||||
|
||||
def test_at_80_percent(self):
|
||||
"""刚好80% - warning."""
|
||||
assert get_warning_level(80, 100) == QuotaWarningLevel.WARNING
|
||||
|
||||
def test_between_80_and_95(self):
|
||||
"""80%-95%之间 - warning."""
|
||||
assert get_warning_level(90, 100) == QuotaWarningLevel.WARNING
|
||||
|
||||
def test_at_95_percent(self):
|
||||
"""刚好95% - critical."""
|
||||
assert get_warning_level(95, 100) == QuotaWarningLevel.CRITICAL
|
||||
|
||||
def test_between_95_and_100(self):
|
||||
"""95%-100%之间 - critical."""
|
||||
assert get_warning_level(99, 100) == QuotaWarningLevel.CRITICAL
|
||||
|
||||
def test_at_100_percent(self):
|
||||
"""刚好100% - exceeded."""
|
||||
assert get_warning_level(100, 100) == QuotaWarningLevel.EXCEEDED
|
||||
|
||||
def test_over_100_percent(self):
|
||||
"""超过100% - exceeded."""
|
||||
assert get_warning_level(150, 100) == QuotaWarningLevel.EXCEEDED
|
||||
|
||||
def test_zero_limit_with_usage(self):
|
||||
"""限制为0但有使用 - exceeded."""
|
||||
assert get_warning_level(1, 0) == QuotaWarningLevel.EXCEEDED
|
||||
|
||||
def test_zero_limit_no_usage(self):
|
||||
"""限制为0且无使用 - normal."""
|
||||
assert get_warning_level(0, 0) == QuotaWarningLevel.NORMAL
|
||||
|
||||
def test_unlimited(self):
|
||||
"""不限量 - 始终 normal."""
|
||||
assert get_warning_level(0, float("inf")) == QuotaWarningLevel.NORMAL
|
||||
assert get_warning_level(9999, float("inf")) == QuotaWarningLevel.NORMAL
|
||||
|
||||
def test_negative_usage(self):
|
||||
"""负使用量 - normal."""
|
||||
assert get_warning_level(-10, 100) == QuotaWarningLevel.NORMAL
|
||||
|
||||
|
||||
class TestQuotaRegistry:
|
||||
"""QuotaRegistry 测试."""
|
||||
|
||||
def test_initial_dimensions(self):
|
||||
"""初始化后内置维度都在."""
|
||||
reg = QuotaRegistry()
|
||||
dims = reg.list_dimensions()
|
||||
for dim in QuotaDimension:
|
||||
assert dim.value in dims
|
||||
|
||||
def test_initial_dimensions_count(self):
|
||||
"""初始维度数量等于枚举数量."""
|
||||
reg = QuotaRegistry()
|
||||
assert len(reg.list_dimensions()) == len(QuotaDimension)
|
||||
|
||||
def test_list_tiers(self):
|
||||
"""三个套餐等级."""
|
||||
reg = QuotaRegistry()
|
||||
tiers = reg.list_tiers()
|
||||
assert "free" in tiers
|
||||
assert "basic" in tiers
|
||||
assert "premium" in tiers
|
||||
assert len(tiers) == 3
|
||||
|
||||
def test_get_tier_existing(self):
|
||||
"""获取已有的套餐."""
|
||||
reg = QuotaRegistry()
|
||||
tier = reg.get_tier("free")
|
||||
assert tier is not None
|
||||
assert tier.name == "free"
|
||||
|
||||
def test_get_tier_nonexistent(self):
|
||||
"""获取不存在的套餐返回 None."""
|
||||
reg = QuotaRegistry()
|
||||
assert reg.get_tier("enterprise") is None
|
||||
|
||||
def test_get_limit_existing(self):
|
||||
"""获取已有限制."""
|
||||
reg = QuotaRegistry()
|
||||
assert reg.get_limit("free", QuotaDimension.STORAGE_GB) == 2
|
||||
|
||||
def test_get_limit_nonexistent_plan(self):
|
||||
"""不存在的套餐返回 0."""
|
||||
reg = QuotaRegistry()
|
||||
assert reg.get_limit("enterprise", QuotaDimension.STORAGE_GB) == 0
|
||||
|
||||
def test_get_limit_unknown_dimension(self):
|
||||
"""未知维度返回 0."""
|
||||
reg = QuotaRegistry()
|
||||
assert reg.get_limit("free", "unknown_dim") == 0
|
||||
|
||||
def test_register_dimension_new(self):
|
||||
"""注册新维度."""
|
||||
reg = QuotaRegistry()
|
||||
count_before = len(reg.list_dimensions())
|
||||
reg.register_dimension("custom_dim", "自定义维度")
|
||||
dims = reg.list_dimensions()
|
||||
assert "custom_dim" in dims
|
||||
assert len(dims) == count_before + 1
|
||||
|
||||
def test_register_dimension_with_default_limits(self):
|
||||
"""注册带默认限制的维度."""
|
||||
reg = QuotaRegistry()
|
||||
reg.register_dimension(
|
||||
"api_calls",
|
||||
"API调用次数",
|
||||
default_limits={"free": 100, "basic": 1000, "premium": 10000},
|
||||
)
|
||||
assert reg.get_limit("free", "api_calls") == 100
|
||||
assert reg.get_limit("basic", "api_calls") == 1000
|
||||
assert reg.get_limit("premium", "api_calls") == 10000
|
||||
|
||||
def test_register_dimension_default_zero(self):
|
||||
"""不带默认限制的维度,各套餐默认为 0."""
|
||||
reg = QuotaRegistry()
|
||||
reg.register_dimension("beta_feature", "测试功能")
|
||||
assert reg.get_limit("free", "beta_feature") == 0
|
||||
assert reg.get_limit("basic", "beta_feature") == 0
|
||||
assert reg.get_limit("premium", "beta_feature") == 0
|
||||
|
||||
def test_register_dimension_idempotent(self):
|
||||
"""重复注册幂等."""
|
||||
reg = QuotaRegistry()
|
||||
reg.register_dimension("dup", "重复测试", default_limits={"free": 10})
|
||||
count_before = len(reg.list_dimensions())
|
||||
# 第二次注册不同的限制,应该不生效
|
||||
reg.register_dimension("dup", "重复测试2", default_limits={"free": 999})
|
||||
count_after = len(reg.list_dimensions())
|
||||
assert count_before == count_after
|
||||
assert reg.get_limit("free", "dup") == 10 # 仍然是第一次的值
|
||||
|
||||
def test_list_dimensions_returns_copy(self):
|
||||
"""list_dimensions 返回副本,修改不影响内部."""
|
||||
reg = QuotaRegistry()
|
||||
dims = reg.list_dimensions()
|
||||
dims["hacked"] = "hack"
|
||||
assert "hacked" not in reg.list_dimensions()
|
||||
|
||||
def test_register_partial_default_limits(self):
|
||||
"""只给部分套餐设置默认限制."""
|
||||
reg = QuotaRegistry()
|
||||
reg.register_dimension(
|
||||
"partial",
|
||||
"部分套餐",
|
||||
default_limits={"premium": 100},
|
||||
)
|
||||
assert reg.get_limit("free", "partial") == 0
|
||||
assert reg.get_limit("basic", "partial") == 0
|
||||
assert reg.get_limit("premium", "partial") == 100
|
||||
|
||||
|
||||
class TestQuotaChecker:
|
||||
"""QuotaChecker 测试."""
|
||||
|
||||
def test_check_within_limit(self):
|
||||
"""在限制内,allowed=True."""
|
||||
checker = QuotaChecker()
|
||||
result = checker.check("free", QuotaDimension.STORAGE_GB, 1)
|
||||
assert result.allowed is True
|
||||
assert result.dimension == QuotaDimension.STORAGE_GB
|
||||
assert result.limit == 2
|
||||
assert result.used == 1
|
||||
assert result.remaining == 1
|
||||
assert result.warning_level == QuotaWarningLevel.NORMAL
|
||||
|
||||
def test_check_exceeded(self):
|
||||
"""超出限制,allowed=False."""
|
||||
checker = QuotaChecker()
|
||||
result = checker.check("free", QuotaDimension.STORAGE_GB, 3)
|
||||
assert result.allowed is False
|
||||
assert result.remaining == 0
|
||||
assert result.warning_level == QuotaWarningLevel.EXCEEDED
|
||||
|
||||
def test_check_exactly_at_limit(self):
|
||||
"""刚好等于限制视为超出(used < limit 才允许)."""
|
||||
checker = QuotaChecker()
|
||||
result = checker.check("free", QuotaDimension.STORAGE_GB, 2)
|
||||
assert result.allowed is False
|
||||
|
||||
def test_check_unlimited(self):
|
||||
"""不限量维度."""
|
||||
checker = QuotaChecker()
|
||||
result = checker.check("premium", QuotaDimension.MAX_TEMPLATES, 999)
|
||||
assert result.allowed is True
|
||||
assert math.isinf(result.remaining)
|
||||
assert result.warning_level == QuotaWarningLevel.NORMAL
|
||||
|
||||
def test_check_warning_level_boundaries(self):
|
||||
"""各告警级别的边界值."""
|
||||
checker = QuotaChecker()
|
||||
# 79% - normal
|
||||
assert checker.check("free", QuotaDimension.STORAGE_GB, 1.58).warning_level == QuotaWarningLevel.NORMAL
|
||||
# 80% - warning
|
||||
assert checker.check("free", QuotaDimension.STORAGE_GB, 1.6).warning_level == QuotaWarningLevel.WARNING
|
||||
# 95% - critical
|
||||
assert checker.check("free", QuotaDimension.STORAGE_GB, 1.9).warning_level == QuotaWarningLevel.CRITICAL
|
||||
# 100% - exceeded
|
||||
assert checker.check("free", QuotaDimension.STORAGE_GB, 2).warning_level == QuotaWarningLevel.EXCEEDED
|
||||
|
||||
def test_check_zero_usage(self):
|
||||
"""0使用量."""
|
||||
checker = QuotaChecker()
|
||||
result = checker.check("basic", QuotaDimension.VIDEOS_PER_MONTH, 0)
|
||||
assert result.allowed is True
|
||||
assert result.remaining == 30
|
||||
assert result.usage_percent == 0.0
|
||||
|
||||
def test_check_unknown_plan(self):
|
||||
"""未知套餐,限制为0."""
|
||||
checker = QuotaChecker()
|
||||
result = checker.check("enterprise", QuotaDimension.STORAGE_GB, 0)
|
||||
assert result.limit == 0
|
||||
# used=0, limit=0 → 0 < 0 is False → allowed=False
|
||||
assert result.allowed is False
|
||||
assert result.warning_level == QuotaWarningLevel.NORMAL
|
||||
|
||||
def test_check_unknown_dimension(self):
|
||||
"""未知维度,限制为0."""
|
||||
checker = QuotaChecker()
|
||||
result = checker.check("free", "unknown", 0)
|
||||
assert result.limit == 0
|
||||
|
||||
def test_check_multiple(self):
|
||||
"""批量检查多个维度."""
|
||||
checker = QuotaChecker()
|
||||
usage = {
|
||||
QuotaDimension.STORAGE_GB: 1,
|
||||
QuotaDimension.VIDEOS_PER_MONTH: 10,
|
||||
}
|
||||
results = checker.check_multiple("free", usage)
|
||||
assert len(results) == 2
|
||||
dims = {r.dimension for r in results}
|
||||
assert QuotaDimension.STORAGE_GB in dims
|
||||
assert QuotaDimension.VIDEOS_PER_MONTH in dims
|
||||
|
||||
def test_check_multiple_empty(self):
|
||||
"""空字典返回空列表."""
|
||||
checker = QuotaChecker()
|
||||
results = checker.check_multiple("free", {})
|
||||
assert results == []
|
||||
|
||||
def test_remaining_never_negative(self):
|
||||
"""剩余量不为负."""
|
||||
checker = QuotaChecker()
|
||||
result = checker.check("free", QuotaDimension.STORAGE_GB, 100)
|
||||
assert result.remaining >= 0
|
||||
|
||||
def test_custom_registry(self):
|
||||
"""使用自定义 registry."""
|
||||
reg = QuotaRegistry()
|
||||
reg.register_dimension("custom", "自定义", default_limits={"free": 42})
|
||||
checker = QuotaChecker(reg)
|
||||
result = checker.check("free", "custom", 10)
|
||||
assert result.limit == 42
|
||||
assert result.allowed is True
|
||||
|
||||
|
||||
class TestGlobalSingletons:
|
||||
"""全局单例测试."""
|
||||
|
||||
def test_quota_registry_exists(self):
|
||||
"""全局 registry 单例存在."""
|
||||
assert quota_registry is not None
|
||||
assert isinstance(quota_registry, QuotaRegistry)
|
||||
|
||||
def test_quota_checker_exists(self):
|
||||
"""全局 checker 单例存在."""
|
||||
assert quota_checker is not None
|
||||
assert isinstance(quota_checker, QuotaChecker)
|
||||
|
||||
def test_global_checker_works(self):
|
||||
"""全局 checker 能正常工作."""
|
||||
result = quota_checker.check("free", QuotaDimension.STORAGE_GB, 1)
|
||||
assert result.allowed is True
|
||||
assert result.limit == 2
|
||||
@@ -1,675 +0,0 @@
|
||||
"""sticker_config 单元测试 - wave167
|
||||
|
||||
覆盖:
|
||||
- ImageStickerConfig 数据类 / from_dict / has_time_range / end_time
|
||||
- TextStickerConfig 数据类 / from_dict / has_background / has_time_range
|
||||
- StickerOverlayResult 数据类
|
||||
- resolve_sticker_position 位置解析(9宫格/自定义坐标/单位转换/钳制)
|
||||
- parse_stickers_from_config 贴纸列表解析
|
||||
- get_sticker_categories 分类列表
|
||||
- 常量验证(POSITION_PRESETS / STICKER_CATEGORIES)
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from packages.domain.sticker_config import (
|
||||
POSITION_PRESETS,
|
||||
STICKER_CATEGORIES,
|
||||
ImageStickerConfig,
|
||||
StickerOverlayResult,
|
||||
TextStickerConfig,
|
||||
get_sticker_categories,
|
||||
parse_stickers_from_config,
|
||||
resolve_sticker_position,
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 常量验证
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestConstants:
|
||||
def test_nine_position_presets(self):
|
||||
assert len(POSITION_PRESETS) == 9
|
||||
|
||||
def test_positions_in_normalized_range(self):
|
||||
for name, (x, y) in POSITION_PRESETS.items():
|
||||
assert 0.0 <= x <= 1.0, f"{name} x out of range: {x}"
|
||||
assert 0.0 <= y <= 1.0, f"{name} y out of range: {y}"
|
||||
|
||||
def test_top_left_is_near_zero(self):
|
||||
x, y = POSITION_PRESETS["top_left"]
|
||||
assert x < 0.1
|
||||
assert y < 0.1
|
||||
|
||||
def test_bottom_right_is_near_one(self):
|
||||
x, y = POSITION_PRESETS["bottom_right"]
|
||||
assert x > 0.9
|
||||
assert y > 0.9
|
||||
|
||||
def test_center_is_middle(self):
|
||||
x, y = POSITION_PRESETS["center"]
|
||||
assert x == 0.5
|
||||
assert y == 0.5
|
||||
|
||||
def test_sticker_categories_not_empty(self):
|
||||
assert len(STICKER_CATEGORIES) >= 3
|
||||
|
||||
def test_sticker_categories_format(self):
|
||||
for cat in STICKER_CATEGORIES:
|
||||
assert len(cat) == 2
|
||||
assert isinstance(cat[0], str)
|
||||
assert isinstance(cat[1], str)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# ImageStickerConfig
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestImageStickerConfigDefaults:
|
||||
def test_default_values(self):
|
||||
cfg = ImageStickerConfig()
|
||||
assert cfg.enabled is False
|
||||
assert cfg.type == "image"
|
||||
assert cfg.position == "top_right"
|
||||
assert cfg.x is None
|
||||
assert cfg.y is None
|
||||
assert cfg.x_unit == "percent"
|
||||
assert cfg.y_unit == "percent"
|
||||
assert cfg.scale == 1.0
|
||||
assert cfg.width is None
|
||||
assert cfg.height is None
|
||||
assert cfg.opacity == 1.0
|
||||
assert cfg.start_time == 0.0
|
||||
assert cfg.duration == 0.0
|
||||
assert cfg.fade_in == 0.0
|
||||
assert cfg.fade_out == 0.0
|
||||
assert cfg.z_index == 10
|
||||
assert cfg.image_url == ""
|
||||
assert cfg.preset_id == ""
|
||||
|
||||
def test_has_time_range_false_when_zero(self):
|
||||
cfg = ImageStickerConfig()
|
||||
assert cfg.has_time_range is False
|
||||
|
||||
def test_has_time_range_true_when_set(self):
|
||||
cfg = ImageStickerConfig(duration=5.0)
|
||||
assert cfg.has_time_range is True
|
||||
|
||||
def test_end_time_calculation(self):
|
||||
cfg = ImageStickerConfig(start_time=2.0, duration=3.0)
|
||||
assert cfg.end_time == pytest.approx(5.0)
|
||||
|
||||
def test_end_time_zero_duration(self):
|
||||
cfg = ImageStickerConfig(start_time=5.0, duration=0.0)
|
||||
assert cfg.end_time == pytest.approx(5.0)
|
||||
|
||||
|
||||
class TestImageStickerFromDict:
|
||||
def test_none_returns_default(self):
|
||||
cfg = ImageStickerConfig.from_dict(None)
|
||||
assert cfg.enabled is False
|
||||
assert cfg.position == "top_right"
|
||||
|
||||
def test_empty_dict_returns_default(self):
|
||||
cfg = ImageStickerConfig.from_dict({})
|
||||
assert cfg.enabled is False
|
||||
|
||||
def test_not_dict_returns_default(self):
|
||||
cfg = ImageStickerConfig.from_dict("not_a_dict")
|
||||
assert cfg.enabled is False
|
||||
|
||||
def test_enabled_true(self):
|
||||
cfg = ImageStickerConfig.from_dict({"enabled": True})
|
||||
assert cfg.enabled is True
|
||||
|
||||
def test_position_custom(self):
|
||||
cfg = ImageStickerConfig.from_dict({"position": "bottom_left"})
|
||||
assert cfg.position == "bottom_left"
|
||||
|
||||
def test_custom_coordinates(self):
|
||||
cfg = ImageStickerConfig.from_dict({"x": 30.0, "y": 50.0})
|
||||
assert cfg.x == 30.0
|
||||
assert cfg.y == 50.0
|
||||
|
||||
def test_string_coordinates_converted(self):
|
||||
cfg = ImageStickerConfig.from_dict({"x": "25.5", "y": "75.0"})
|
||||
assert cfg.x == 25.5
|
||||
assert cfg.y == 75.0
|
||||
|
||||
def test_invalid_coordinates_become_none(self):
|
||||
cfg = ImageStickerConfig.from_dict({"x": "invalid", "y": "abc"})
|
||||
assert cfg.x is None
|
||||
assert cfg.y is None
|
||||
|
||||
def test_scale_clamped_min(self):
|
||||
cfg = ImageStickerConfig.from_dict({"scale": 0.001})
|
||||
assert cfg.scale == 0.01
|
||||
|
||||
def test_scale_normal(self):
|
||||
cfg = ImageStickerConfig.from_dict({"scale": 1.5})
|
||||
assert cfg.scale == 1.5
|
||||
|
||||
def test_opacity_clamped_high(self):
|
||||
cfg = ImageStickerConfig.from_dict({"opacity": 1.5})
|
||||
assert cfg.opacity == 1.0
|
||||
|
||||
def test_opacity_clamped_low(self):
|
||||
cfg = ImageStickerConfig.from_dict({"opacity": -0.5})
|
||||
assert cfg.opacity == 0.0
|
||||
|
||||
def test_opacity_normal(self):
|
||||
cfg = ImageStickerConfig.from_dict({"opacity": 0.5})
|
||||
assert cfg.opacity == 0.5
|
||||
|
||||
def test_start_time_clamped_non_negative(self):
|
||||
cfg = ImageStickerConfig.from_dict({"start_time": -5.0})
|
||||
assert cfg.start_time == 0.0
|
||||
|
||||
def test_duration_clamped_non_negative(self):
|
||||
cfg = ImageStickerConfig.from_dict({"duration": -3.0})
|
||||
assert cfg.duration == 0.0
|
||||
|
||||
def test_fade_in_non_negative(self):
|
||||
cfg = ImageStickerConfig.from_dict({"fade_in": -1.0})
|
||||
assert cfg.fade_in == 0.0
|
||||
|
||||
def test_fade_out_non_negative(self):
|
||||
cfg = ImageStickerConfig.from_dict({"fade_out": -1.0})
|
||||
assert cfg.fade_out == 0.0
|
||||
|
||||
def test_z_index_default_when_zero(self):
|
||||
cfg = ImageStickerConfig.from_dict({"z_index": 0})
|
||||
# 0 or None → 10
|
||||
assert cfg.z_index == 10
|
||||
|
||||
def test_z_index_custom(self):
|
||||
cfg = ImageStickerConfig.from_dict({"z_index": 5})
|
||||
assert cfg.z_index == 5
|
||||
|
||||
def test_width_height(self):
|
||||
cfg = ImageStickerConfig.from_dict({"width": 200, "height": 100})
|
||||
assert cfg.width == 200
|
||||
assert cfg.height == 100
|
||||
|
||||
def test_width_height_none(self):
|
||||
cfg = ImageStickerConfig.from_dict({})
|
||||
assert cfg.width is None
|
||||
assert cfg.height is None
|
||||
|
||||
def test_image_url_and_preset_id(self):
|
||||
cfg = ImageStickerConfig.from_dict({"image_url": "http://img.png", "preset_id": "sticker_001"})
|
||||
assert cfg.image_url == "http://img.png"
|
||||
assert cfg.preset_id == "sticker_001"
|
||||
|
||||
def test_units_custom(self):
|
||||
cfg = ImageStickerConfig.from_dict({"x_unit": "pixel", "y_unit": "pixel"})
|
||||
assert cfg.x_unit == "pixel"
|
||||
assert cfg.y_unit == "pixel"
|
||||
|
||||
def test_invalid_numeric_values_use_default(self):
|
||||
cfg = ImageStickerConfig.from_dict({"scale": "not_a_number"})
|
||||
assert cfg.scale == 1.0 # 回退到默认值
|
||||
|
||||
def test_invalid_int_values_use_default(self):
|
||||
cfg = ImageStickerConfig.from_dict({"z_index": "invalid"})
|
||||
assert cfg.z_index == 10
|
||||
|
||||
|
||||
# ============================================================
|
||||
# TextStickerConfig
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestTextStickerConfigDefaults:
|
||||
def test_default_values(self):
|
||||
cfg = TextStickerConfig()
|
||||
assert cfg.enabled is False
|
||||
assert cfg.type == "text"
|
||||
assert cfg.text == ""
|
||||
assert cfg.font_size == 36
|
||||
assert cfg.font_color == "#FFFFFF"
|
||||
assert cfg.font_family == "sans"
|
||||
assert cfg.stroke_color == "#000000"
|
||||
assert cfg.stroke_width == 2
|
||||
assert cfg.shadow_color == "#000000"
|
||||
assert cfg.shadow_x == 2
|
||||
assert cfg.shadow_y == 2
|
||||
assert cfg.shadow_alpha == 0.5
|
||||
assert cfg.position == "center"
|
||||
assert cfg.start_time == 0.0
|
||||
assert cfg.duration == 0.0
|
||||
assert cfg.z_index == 10
|
||||
assert cfg.bg_color == ""
|
||||
assert cfg.bg_padding == 8
|
||||
assert cfg.bg_alpha == 0.8
|
||||
assert cfg.bg_corner_radius == 8
|
||||
|
||||
def test_has_background_false_when_empty(self):
|
||||
cfg = TextStickerConfig()
|
||||
assert cfg.has_background is False
|
||||
|
||||
def test_has_background_true_when_set(self):
|
||||
cfg = TextStickerConfig(bg_color="#FF0000")
|
||||
assert cfg.has_background is True
|
||||
|
||||
def test_has_time_range_false(self):
|
||||
cfg = TextStickerConfig()
|
||||
assert cfg.has_time_range is False
|
||||
|
||||
def test_has_time_range_true(self):
|
||||
cfg = TextStickerConfig(duration=10.0)
|
||||
assert cfg.has_time_range is True
|
||||
|
||||
|
||||
class TestTextStickerFromDict:
|
||||
def test_none_returns_default(self):
|
||||
cfg = TextStickerConfig.from_dict(None)
|
||||
assert cfg.enabled is False
|
||||
assert cfg.text == ""
|
||||
|
||||
def test_empty_dict_returns_default(self):
|
||||
cfg = TextStickerConfig.from_dict({})
|
||||
assert cfg.text == ""
|
||||
|
||||
def test_not_dict_returns_default(self):
|
||||
cfg = TextStickerConfig.from_dict(123)
|
||||
assert cfg.text == ""
|
||||
|
||||
def test_text_content(self):
|
||||
cfg = TextStickerConfig.from_dict({"text": "Hello World"})
|
||||
assert cfg.text == "Hello World"
|
||||
|
||||
def test_font_size_minimum_1(self):
|
||||
cfg = TextStickerConfig.from_dict({"font_size": 0})
|
||||
assert cfg.font_size == 1
|
||||
|
||||
def test_font_size_normal(self):
|
||||
cfg = TextStickerConfig.from_dict({"font_size": 48})
|
||||
assert cfg.font_size == 48
|
||||
|
||||
def test_font_color(self):
|
||||
cfg = TextStickerConfig.from_dict({"font_color": "#00FF00"})
|
||||
assert cfg.font_color == "#00FF00"
|
||||
|
||||
def test_stroke_width_non_negative(self):
|
||||
cfg = TextStickerConfig.from_dict({"stroke_width": -2})
|
||||
assert cfg.stroke_width == 0
|
||||
|
||||
def test_stroke_width_normal(self):
|
||||
cfg = TextStickerConfig.from_dict({"stroke_width": 4})
|
||||
assert cfg.stroke_width == 4
|
||||
|
||||
def test_shadow_alpha_clamped(self):
|
||||
cfg = TextStickerConfig.from_dict({"shadow_alpha": 1.5})
|
||||
assert cfg.shadow_alpha == 1.0
|
||||
|
||||
def test_shadow_alpha_negative(self):
|
||||
cfg = TextStickerConfig.from_dict({"shadow_alpha": -0.5})
|
||||
assert cfg.shadow_alpha == 0.0
|
||||
|
||||
def test_shadow_offset(self):
|
||||
cfg = TextStickerConfig.from_dict({"shadow_x": 5, "shadow_y": 3})
|
||||
assert cfg.shadow_x == 5
|
||||
assert cfg.shadow_y == 3
|
||||
|
||||
def test_position_custom(self):
|
||||
cfg = TextStickerConfig.from_dict({"position": "top_right"})
|
||||
assert cfg.position == "top_right"
|
||||
|
||||
def test_custom_coordinates(self):
|
||||
cfg = TextStickerConfig.from_dict({"x": 10, "y": 20})
|
||||
assert cfg.x == 10.0
|
||||
assert cfg.y == 20.0
|
||||
|
||||
def test_start_time_non_negative(self):
|
||||
cfg = TextStickerConfig.from_dict({"start_time": -1.0})
|
||||
assert cfg.start_time == 0.0
|
||||
|
||||
def test_duration_non_negative(self):
|
||||
cfg = TextStickerConfig.from_dict({"duration": -2.0})
|
||||
assert cfg.duration == 0.0
|
||||
|
||||
def test_fade_in_out_non_negative(self):
|
||||
cfg = TextStickerConfig.from_dict({"fade_in": -1, "fade_out": -1})
|
||||
assert cfg.fade_in == 0.0
|
||||
assert cfg.fade_out == 0.0
|
||||
|
||||
def test_z_index_default(self):
|
||||
cfg = TextStickerConfig.from_dict({})
|
||||
assert cfg.z_index == 10
|
||||
|
||||
def test_bg_color(self):
|
||||
cfg = TextStickerConfig.from_dict({"bg_color": "#0000FF"})
|
||||
assert cfg.bg_color == "#0000FF"
|
||||
|
||||
def test_bg_padding_non_negative(self):
|
||||
cfg = TextStickerConfig.from_dict({"bg_padding": -5})
|
||||
assert cfg.bg_padding == 0
|
||||
|
||||
def test_bg_alpha_clamped(self):
|
||||
cfg = TextStickerConfig.from_dict({"bg_alpha": 2.0})
|
||||
assert cfg.bg_alpha == 1.0
|
||||
|
||||
def test_bg_corner_radius_non_negative(self):
|
||||
cfg = TextStickerConfig.from_dict({"bg_corner_radius": -3})
|
||||
assert cfg.bg_corner_radius == 0
|
||||
|
||||
def test_invalid_numeric_values_use_default(self):
|
||||
cfg = TextStickerConfig.from_dict({"font_size": "big"})
|
||||
assert cfg.font_size == 36 # 默认值
|
||||
|
||||
|
||||
# ============================================================
|
||||
# StickerOverlayResult
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestStickerOverlayResult:
|
||||
def test_minimal_creation(self):
|
||||
r = StickerOverlayResult(filter_str="overlay=10:20", output_label="[out]")
|
||||
assert r.filter_str == "overlay=10:20"
|
||||
assert r.output_label == "[out]"
|
||||
assert r.extra_inputs == []
|
||||
|
||||
def test_with_extra_inputs(self):
|
||||
r = StickerOverlayResult(
|
||||
filter_str="overlay",
|
||||
output_label="[out]",
|
||||
extra_inputs=["img1.png", "img2.png"],
|
||||
)
|
||||
assert len(r.extra_inputs) == 2
|
||||
assert r.extra_inputs == ["img1.png", "img2.png"]
|
||||
|
||||
def test_extra_inputs_independent_lists(self):
|
||||
r1 = StickerOverlayResult(filter_str="a", output_label="[o1]")
|
||||
r2 = StickerOverlayResult(filter_str="b", output_label="[o2]")
|
||||
r1.extra_inputs.append("test.png")
|
||||
assert r2.extra_inputs == [] # 独立
|
||||
|
||||
|
||||
# ============================================================
|
||||
# resolve_sticker_position
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestResolveStickerPosition:
|
||||
CANVAS_W = 1920
|
||||
CANVAS_H = 1080
|
||||
|
||||
def test_preset_top_left(self):
|
||||
x, y = resolve_sticker_position(
|
||||
"top_left",
|
||||
None,
|
||||
None,
|
||||
"percent",
|
||||
"percent",
|
||||
self.CANVAS_W,
|
||||
self.CANVAS_H,
|
||||
100,
|
||||
50,
|
||||
)
|
||||
# preset x=0.05, y=0.05 → 0.05*1920 - 50 = 96-50=46, 0.05*1080-25=54-25=29
|
||||
assert x == pytest.approx(0.05 * 1920 - 50)
|
||||
assert y == pytest.approx(0.05 * 1080 - 25)
|
||||
|
||||
def test_preset_center(self):
|
||||
x, y = resolve_sticker_position(
|
||||
"center",
|
||||
None,
|
||||
None,
|
||||
"percent",
|
||||
"percent",
|
||||
self.CANVAS_W,
|
||||
self.CANVAS_H,
|
||||
200,
|
||||
100,
|
||||
)
|
||||
# center 0.5,0.5 → 贴纸左上角居中
|
||||
assert x == pytest.approx(1920 / 2 - 100)
|
||||
assert y == pytest.approx(1080 / 2 - 50)
|
||||
|
||||
def test_preset_bottom_right(self):
|
||||
x, y = resolve_sticker_position(
|
||||
"bottom_right",
|
||||
None,
|
||||
None,
|
||||
"percent",
|
||||
"percent",
|
||||
self.CANVAS_W,
|
||||
self.CANVAS_H,
|
||||
100,
|
||||
50,
|
||||
)
|
||||
assert x == pytest.approx(0.95 * 1920 - 50)
|
||||
assert y == pytest.approx(0.95 * 1080 - 25)
|
||||
|
||||
def test_invalid_preset_defaults_center(self):
|
||||
x, y = resolve_sticker_position(
|
||||
"invalid",
|
||||
None,
|
||||
None,
|
||||
"percent",
|
||||
"percent",
|
||||
self.CANVAS_W,
|
||||
self.CANVAS_H,
|
||||
100,
|
||||
50,
|
||||
)
|
||||
# 默认居中
|
||||
assert x == pytest.approx(1920 / 2 - 50)
|
||||
assert y == pytest.approx(1080 / 2 - 25)
|
||||
|
||||
def test_custom_percent_coordinates(self):
|
||||
x, y = resolve_sticker_position(
|
||||
"top_left",
|
||||
30.0,
|
||||
70.0,
|
||||
"percent",
|
||||
"percent",
|
||||
self.CANVAS_W,
|
||||
self.CANVAS_H,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
# x=30% of 1920 = 576, y=70% of 1080 = 756
|
||||
assert x == pytest.approx(576.0)
|
||||
assert y == pytest.approx(756.0)
|
||||
|
||||
def test_custom_percent_clamped_0(self):
|
||||
x, y = resolve_sticker_position(
|
||||
"center",
|
||||
-10.0,
|
||||
-5.0,
|
||||
"percent",
|
||||
"percent",
|
||||
self.CANVAS_W,
|
||||
self.CANVAS_H,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
assert x == 0.0
|
||||
assert y == 0.0
|
||||
|
||||
def test_custom_percent_clamped_100(self):
|
||||
x, y = resolve_sticker_position(
|
||||
"center",
|
||||
150.0,
|
||||
120.0,
|
||||
"percent",
|
||||
"percent",
|
||||
self.CANVAS_W,
|
||||
self.CANVAS_H,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
assert x == pytest.approx(1920.0)
|
||||
assert y == pytest.approx(1080.0)
|
||||
|
||||
def test_custom_pixel_coordinates(self):
|
||||
x, y = resolve_sticker_position(
|
||||
"center",
|
||||
500,
|
||||
300,
|
||||
"pixel",
|
||||
"pixel",
|
||||
self.CANVAS_W,
|
||||
self.CANVAS_H,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
assert x == pytest.approx(500.0)
|
||||
assert y == pytest.approx(300.0)
|
||||
|
||||
def test_x_only_override(self):
|
||||
# 只有x覆盖,y用预设
|
||||
x, y = resolve_sticker_position(
|
||||
"top_left",
|
||||
50.0,
|
||||
None,
|
||||
"percent",
|
||||
"percent",
|
||||
self.CANVAS_W,
|
||||
self.CANVAS_H,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
assert x == pytest.approx(0.5 * 1920) # 50%
|
||||
assert y == pytest.approx(0.05 * 1080) # 预设 top_left
|
||||
|
||||
def test_y_only_override(self):
|
||||
x, y = resolve_sticker_position(
|
||||
"top_left",
|
||||
None,
|
||||
50.0,
|
||||
"percent",
|
||||
"percent",
|
||||
self.CANVAS_W,
|
||||
self.CANVAS_H,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
assert x == pytest.approx(0.05 * 1920)
|
||||
assert y == pytest.approx(0.5 * 1080)
|
||||
|
||||
def test_clamped_to_canvas_with_sticker_size(self):
|
||||
# 贴纸100x50,放在最右下角,不能超出画布
|
||||
x, y = resolve_sticker_position(
|
||||
"bottom_right",
|
||||
None,
|
||||
None,
|
||||
"percent",
|
||||
"percent",
|
||||
self.CANVAS_W,
|
||||
self.CANVAS_H,
|
||||
100,
|
||||
50,
|
||||
)
|
||||
assert x <= self.CANVAS_W - 100
|
||||
assert y <= self.CANVAS_H - 50
|
||||
|
||||
def test_clamped_left_top(self):
|
||||
# 很大的负偏移,被钳制在0
|
||||
x, y = resolve_sticker_position(
|
||||
"center",
|
||||
0.0,
|
||||
0.0,
|
||||
"percent",
|
||||
"percent",
|
||||
self.CANVAS_W,
|
||||
self.CANVAS_H,
|
||||
200,
|
||||
100,
|
||||
)
|
||||
# 0%位置 + 贴纸尺寸的一半偏移 = -100, -50 → 钳制到 0, 0
|
||||
assert x == 0
|
||||
assert y == 0
|
||||
|
||||
def test_zero_canvas_defaults_center(self):
|
||||
x, y = resolve_sticker_position(
|
||||
"top_left",
|
||||
50,
|
||||
50,
|
||||
"pixel",
|
||||
"pixel",
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
# canvas_w=0 → 回退到0.5 → 0*0.5=0
|
||||
assert x == 0.0
|
||||
assert y == 0.0
|
||||
|
||||
def test_sticker_size_zero(self):
|
||||
x, y = resolve_sticker_position(
|
||||
"center",
|
||||
None,
|
||||
None,
|
||||
"percent",
|
||||
"percent",
|
||||
self.CANVAS_W,
|
||||
self.CANVAS_H,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
assert x == pytest.approx(960.0)
|
||||
assert y == pytest.approx(540.0)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# parse_stickers_from_config
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestParseStickersFromConfig:
|
||||
def test_none_returns_empty(self):
|
||||
assert parse_stickers_from_config(None) == []
|
||||
|
||||
def test_empty_dict_returns_empty(self):
|
||||
assert parse_stickers_from_config({}) == []
|
||||
|
||||
def test_stickers_list(self):
|
||||
config = {"stickers": [{"type": "text", "text": "hi"}, {"type": "image", "image_url": "a.png"}]}
|
||||
result = parse_stickers_from_config(config)
|
||||
assert len(result) == 2
|
||||
assert result[0]["type"] == "text"
|
||||
|
||||
def test_stickers_empty_list(self):
|
||||
config = {"stickers": []}
|
||||
assert parse_stickers_from_config(config) == []
|
||||
|
||||
def test_stickers_not_list_returns_empty(self):
|
||||
config = {"stickers": "not_a_list"}
|
||||
assert parse_stickers_from_config(config) == []
|
||||
|
||||
def test_other_config_ignored(self):
|
||||
config = {"other_field": "value"}
|
||||
assert parse_stickers_from_config(config) == []
|
||||
|
||||
|
||||
# ============================================================
|
||||
# get_sticker_categories
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestGetStickerCategories:
|
||||
def test_returns_list(self):
|
||||
result = get_sticker_categories()
|
||||
assert isinstance(result, list)
|
||||
assert len(result) == len(STICKER_CATEGORIES)
|
||||
|
||||
def test_returns_copy(self):
|
||||
# 修改返回值不应影响常量
|
||||
result = get_sticker_categories()
|
||||
result.append(("new", "新分类"))
|
||||
assert len(STICKER_CATEGORIES) == len(STICKER_CATEGORIES)
|
||||
# 原常量不变
|
||||
assert ("new", "新分类") not in STICKER_CATEGORIES
|
||||
|
||||
def test_format(self):
|
||||
for cat in get_sticker_categories():
|
||||
assert len(cat) == 2
|
||||
assert isinstance(cat[0], str)
|
||||
assert isinstance(cat[1], str)
|
||||
@@ -1,514 +0,0 @@
|
||||
"""subtitle_style 字幕样式单测.
|
||||
|
||||
纯逻辑模块,覆盖:颜色转换、透明度转换、文本转义、时间格式化、
|
||||
文本换行、SubtitleStyle数据类+from_dict、SubtitleSegment片段。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.subtitle_style import (
|
||||
ALLOWED_SUBTITLE_EXTENSIONS,
|
||||
DEFAULT_COLOR,
|
||||
DEFAULT_FONT,
|
||||
DEFAULT_FONT_SIZE,
|
||||
DEFAULT_POSITION,
|
||||
DEFAULT_STROKE_COLOR,
|
||||
DEFAULT_STROKE_WIDTH,
|
||||
POSITION_ALIASES,
|
||||
POSITION_ALIGNMENT,
|
||||
SubtitleSegment,
|
||||
SubtitleStyle,
|
||||
escape_ass_text,
|
||||
format_ass_time,
|
||||
hex_to_ass_bgr,
|
||||
hex_to_ass_color,
|
||||
opacity_to_ass_alpha,
|
||||
wrap_text,
|
||||
)
|
||||
|
||||
|
||||
class TestHexToAssColor:
|
||||
def test_standard_red(self):
|
||||
assert hex_to_ass_color("#FF0000") == "&H000000FF"
|
||||
|
||||
def test_standard_green(self):
|
||||
assert hex_to_ass_color("#00FF00") == "&H0000FF00"
|
||||
|
||||
def test_standard_blue(self):
|
||||
assert hex_to_ass_color("#0000FF") == "&H00FF0000"
|
||||
|
||||
def test_white(self):
|
||||
assert hex_to_ass_color("#FFFFFF") == "&H00FFFFFF"
|
||||
|
||||
def test_black(self):
|
||||
assert hex_to_ass_color("#000000") == "&H00000000"
|
||||
|
||||
def test_no_hash_prefix(self):
|
||||
assert hex_to_ass_color("FF0000") == "&H000000FF"
|
||||
|
||||
def test_lowercase(self):
|
||||
assert hex_to_ass_color("#ff0000") == "&H000000FF"
|
||||
|
||||
def test_mixed_case(self):
|
||||
assert hex_to_ass_color("#aBcDeF") == "&H00EFCDAB"
|
||||
|
||||
def test_invalid_short_length(self):
|
||||
assert hex_to_ass_color("#FFF") == "&H00FFFFFF"
|
||||
|
||||
def test_empty_string(self):
|
||||
assert hex_to_ass_color("") == "&H00FFFFFF"
|
||||
|
||||
def test_alpha_is_00(self):
|
||||
"""默认不透明(alpha=00)."""
|
||||
result = hex_to_ass_color("#123456")
|
||||
assert result.startswith("&H00")
|
||||
|
||||
|
||||
class TestHexToAssBgr:
|
||||
def test_red(self):
|
||||
assert hex_to_ass_bgr("#FF0000") == "0000FF"
|
||||
|
||||
def test_green(self):
|
||||
assert hex_to_ass_bgr("#00FF00") == "00FF00"
|
||||
|
||||
def test_blue(self):
|
||||
assert hex_to_ass_bgr("#0000FF") == "FF0000"
|
||||
|
||||
def test_white(self):
|
||||
assert hex_to_ass_bgr("#FFFFFF") == "FFFFFF"
|
||||
|
||||
def test_no_hash(self):
|
||||
assert hex_to_ass_bgr("FF0000") == "0000FF"
|
||||
|
||||
def test_non_hex_chars_still_processed(self):
|
||||
"""函数只校验长度,不校验字符有效性."""
|
||||
assert hex_to_ass_bgr("#GGGGGG") == "GGGGGG"
|
||||
|
||||
def test_short_returns_white(self):
|
||||
assert hex_to_ass_bgr("#FFF") == "FFFFFF"
|
||||
|
||||
|
||||
class TestOpacityToAssAlpha:
|
||||
def test_fully_opaque(self):
|
||||
assert opacity_to_ass_alpha(1.0) == "00"
|
||||
|
||||
def test_fully_transparent(self):
|
||||
assert opacity_to_ass_alpha(0.0) == "FF"
|
||||
|
||||
def test_half(self):
|
||||
assert opacity_to_ass_alpha(0.5) == "80"
|
||||
|
||||
def test_quarter(self):
|
||||
assert opacity_to_ass_alpha(0.25) == "C0"
|
||||
|
||||
def test_below_zero_clamped(self):
|
||||
assert opacity_to_ass_alpha(-0.5) == "FF"
|
||||
|
||||
def test_above_one_clamped(self):
|
||||
assert opacity_to_ass_alpha(1.5) == "00"
|
||||
|
||||
def test_zero_point_one(self):
|
||||
assert opacity_to_ass_alpha(0.1) == "E6"
|
||||
|
||||
|
||||
class TestEscapeAssText:
|
||||
def test_plain_text(self):
|
||||
assert escape_ass_text("hello world") == "hello world"
|
||||
|
||||
def test_newline_converted(self):
|
||||
assert escape_ass_text("line1\nline2") == "line1\\Nline2"
|
||||
|
||||
def test_crlf_converted(self):
|
||||
assert escape_ass_text("line1\r\nline2") == "line1\\Nline2"
|
||||
|
||||
def test_carriage_return_converted(self):
|
||||
assert escape_ass_text("line1\rline2") == "line1\\Nline2"
|
||||
|
||||
def test_curly_braces_escaped(self):
|
||||
assert escape_ass_text("{text}") == "(text)"
|
||||
|
||||
def test_opening_brace(self):
|
||||
assert escape_ass_text("{start") == "(start"
|
||||
|
||||
def test_closing_brace(self):
|
||||
assert escape_ass_text("end}") == "end)"
|
||||
|
||||
def test_multiple_braces(self):
|
||||
assert escape_ass_text("{a}{b}") == "(a)(b)"
|
||||
|
||||
def test_mixed_newlines_and_braces(self):
|
||||
assert escape_ass_text("{line1}\n{line2}") == "(line1)\\N(line2)"
|
||||
|
||||
def test_empty_string(self):
|
||||
assert escape_ass_text("") == ""
|
||||
|
||||
def test_chinese_text(self):
|
||||
assert escape_ass_text("你好世界") == "你好世界"
|
||||
|
||||
|
||||
class TestFormatAssTime:
|
||||
def test_zero(self):
|
||||
assert format_ass_time(0.0) == "0:00:00.00"
|
||||
|
||||
def test_seconds_only(self):
|
||||
assert format_ass_time(5.5) == "0:00:05.50"
|
||||
|
||||
def test_one_minute(self):
|
||||
assert format_ass_time(60.0) == "0:01:00.00"
|
||||
|
||||
def test_minutes_and_seconds(self):
|
||||
assert format_ass_time(125.5) == "0:02:05.50"
|
||||
|
||||
def test_one_hour(self):
|
||||
assert format_ass_time(3600.0) == "1:00:00.00"
|
||||
|
||||
def test_multi_hours(self):
|
||||
assert format_ass_time(7384.5) == "2:03:04.50"
|
||||
|
||||
def test_negative_becomes_zero(self):
|
||||
assert format_ass_time(-1.0) == "0:00:00.00"
|
||||
|
||||
def test_two_decimal_places(self):
|
||||
assert format_ass_time(1.234) == "0:00:01.23"
|
||||
|
||||
def test_minutes_two_digits(self):
|
||||
assert format_ass_time(599.0) == "0:09:59.00"
|
||||
|
||||
def test_float_input(self):
|
||||
assert format_ass_time(120.5) == "0:02:00.50"
|
||||
|
||||
|
||||
class TestWrapText:
|
||||
def test_short_text_no_wrap(self):
|
||||
result = wrap_text("你好世界", 20)
|
||||
assert result == ["你好世界"]
|
||||
|
||||
def test_empty_text(self):
|
||||
result = wrap_text("", 20)
|
||||
assert result == [""]
|
||||
|
||||
def test_zero_max_chars(self):
|
||||
result = wrap_text("你好世界", 0)
|
||||
assert result == ["你好世界"]
|
||||
|
||||
def test_negative_max_chars(self):
|
||||
result = wrap_text("你好世界", -5)
|
||||
assert result == ["你好世界"]
|
||||
|
||||
def test_exact_length(self):
|
||||
text = "一二三四五六七八九十"
|
||||
result = wrap_text(text, 10)
|
||||
assert result == [text]
|
||||
|
||||
def test_long_text_wrap_at_max(self):
|
||||
text = "一二三四五六七八九十一二三四五六七八九十一二三四五"
|
||||
result = wrap_text(text, 10)
|
||||
assert len(result) == 3
|
||||
assert len(result[0]) == 10
|
||||
|
||||
def test_wrap_at_punctuation(self):
|
||||
"""标点在搜索范围内时,优先在标点处断开."""
|
||||
text = "一二三四五六七八,九十一二三四五六"
|
||||
result = wrap_text(text, 15)
|
||||
# 搜索范围是15到7(15//2+1=8),","在位置8(0-indexed),在范围内
|
||||
assert result[0].endswith(",")
|
||||
assert len(result[0]) == 9 # 包括标点
|
||||
|
||||
def test_punctuation_out_of_range_breaks_at_max(self):
|
||||
"""标点在搜索范围外时,在max_chars处断开."""
|
||||
text = "你好,世界!这是一个测试。"
|
||||
result = wrap_text(text, 10)
|
||||
# "!"在位置5(< 10//2 = 5,不在搜索范围6-10内)
|
||||
assert len(result[0]) == 10
|
||||
|
||||
def test_multiple_lines(self):
|
||||
text = "一" * 50
|
||||
result = wrap_text(text, 10)
|
||||
assert len(result) == 5
|
||||
for line in result:
|
||||
assert len(line) <= 10
|
||||
|
||||
def test_punctuation_preferred_in_range(self):
|
||||
"""搜索范围内有句号时,在句号处断开而不是中间截断."""
|
||||
# 总长度12("一二三四五六七八九十。" + "二三四五六。")
|
||||
text = "一二三四五六七八九十。二三四五六。"
|
||||
result = wrap_text(text, 15)
|
||||
# "。"在位置10,在搜索范围15到7(8-15)内
|
||||
assert result[0] == "一二三四五六七八九十。"
|
||||
assert len(result[0]) == 11
|
||||
|
||||
|
||||
class TestSubtitleStyleDefaults:
|
||||
def test_default_font(self):
|
||||
style = SubtitleStyle()
|
||||
assert style.font_name == DEFAULT_FONT
|
||||
|
||||
def test_default_size(self):
|
||||
style = SubtitleStyle()
|
||||
assert style.font_size == DEFAULT_FONT_SIZE
|
||||
|
||||
def test_default_color(self):
|
||||
style = SubtitleStyle()
|
||||
assert style.font_color == DEFAULT_COLOR
|
||||
|
||||
def test_default_bold_italic(self):
|
||||
style = SubtitleStyle()
|
||||
assert style.bold is False
|
||||
assert style.italic is False
|
||||
|
||||
def test_default_stroke(self):
|
||||
style = SubtitleStyle()
|
||||
assert style.stroke_enabled is True
|
||||
assert style.stroke_color == DEFAULT_STROKE_COLOR
|
||||
assert style.stroke_width == DEFAULT_STROKE_WIDTH
|
||||
|
||||
def test_default_shadow(self):
|
||||
style = SubtitleStyle()
|
||||
assert style.shadow_enabled is False
|
||||
assert style.shadow_offset_x == 2
|
||||
assert style.shadow_offset_y == 2
|
||||
|
||||
def test_default_background(self):
|
||||
style = SubtitleStyle()
|
||||
assert style.background_enabled is False
|
||||
assert style.background_opacity == 0.5
|
||||
|
||||
def test_default_position(self):
|
||||
style = SubtitleStyle()
|
||||
assert style.position == DEFAULT_POSITION
|
||||
|
||||
def test_default_margins(self):
|
||||
style = SubtitleStyle()
|
||||
assert style.margin_v == 60
|
||||
assert style.margin_l == 40
|
||||
assert style.margin_r == 40
|
||||
|
||||
def test_default_animation(self):
|
||||
style = SubtitleStyle()
|
||||
assert style.fade_in == 0.0
|
||||
assert style.fade_out == 0.0
|
||||
assert style.animation_type == "none"
|
||||
|
||||
|
||||
class TestSubtitleStyleProperties:
|
||||
def test_alignment_bottom_center(self):
|
||||
style = SubtitleStyle(position="bottom_center")
|
||||
assert style.alignment == 2
|
||||
|
||||
def test_alignment_top_center(self):
|
||||
style = SubtitleStyle(position="top_center")
|
||||
assert style.alignment == 8
|
||||
|
||||
def test_alignment_center(self):
|
||||
style = SubtitleStyle(position="center")
|
||||
assert style.alignment == 5
|
||||
|
||||
def test_ass_font_color(self):
|
||||
style = SubtitleStyle(font_color="#FF0000")
|
||||
assert style.ass_font_color == "&H000000FF"
|
||||
|
||||
def test_ass_stroke_color(self):
|
||||
style = SubtitleStyle(stroke_color="#000000")
|
||||
assert style.ass_stroke_color == "&H00000000"
|
||||
|
||||
def test_ass_shadow_color(self):
|
||||
style = SubtitleStyle(shadow_color="#FFFFFF")
|
||||
assert style.ass_shadow_color == "&H00FFFFFF"
|
||||
|
||||
def test_ass_background_color(self):
|
||||
style = SubtitleStyle(
|
||||
background_enabled=True,
|
||||
background_color="#000000",
|
||||
background_opacity=0.5,
|
||||
)
|
||||
# alpha=80 (50%透明), bgr=000000
|
||||
assert style.ass_background_color == "&H80000000"
|
||||
|
||||
def test_ass_background_color_full_opaque(self):
|
||||
style = SubtitleStyle(background_color="#FF0000", background_opacity=1.0)
|
||||
assert style.ass_background_color == "&H000000FF"
|
||||
|
||||
|
||||
class TestSubtitleStyleFromDict:
|
||||
def test_none_returns_default(self):
|
||||
style = SubtitleStyle.from_dict(None)
|
||||
assert style == SubtitleStyle()
|
||||
|
||||
def test_empty_dict_returns_default(self):
|
||||
style = SubtitleStyle.from_dict({})
|
||||
assert style == SubtitleStyle()
|
||||
|
||||
def test_not_dict_returns_default(self):
|
||||
style = SubtitleStyle.from_dict("not a dict")
|
||||
assert style == SubtitleStyle()
|
||||
|
||||
def test_custom_font(self):
|
||||
style = SubtitleStyle.from_dict({"font": "微软雅黑", "size": 32})
|
||||
assert style.font_name == "微软雅黑"
|
||||
assert style.font_size == 32
|
||||
|
||||
def test_custom_color(self):
|
||||
style = SubtitleStyle.from_dict({"color": "#FF0000"})
|
||||
assert style.font_color == "#FF0000"
|
||||
|
||||
def test_bold_italic(self):
|
||||
style = SubtitleStyle.from_dict({"bold": True, "italic": True})
|
||||
assert style.bold is True
|
||||
assert style.italic is True
|
||||
|
||||
def test_stroke_custom(self):
|
||||
style = SubtitleStyle.from_dict(
|
||||
{
|
||||
"stroke_enabled": False,
|
||||
"stroke_color": "#FF0000",
|
||||
"stroke_width": 3.0,
|
||||
}
|
||||
)
|
||||
assert style.stroke_enabled is False
|
||||
assert style.stroke_color == "#FF0000"
|
||||
assert style.stroke_width == 3.0
|
||||
|
||||
def test_shadow_custom(self):
|
||||
style = SubtitleStyle.from_dict(
|
||||
{
|
||||
"shadow_enabled": True,
|
||||
"shadow_color": "#0000FF",
|
||||
"shadow_offset_x": 4,
|
||||
"shadow_offset_y": 4,
|
||||
"shadow_blur": 2.0,
|
||||
}
|
||||
)
|
||||
assert style.shadow_enabled is True
|
||||
assert style.shadow_color == "#0000FF"
|
||||
assert style.shadow_offset_x == 4
|
||||
assert style.shadow_offset_y == 4
|
||||
assert style.shadow_blur == 2.0
|
||||
|
||||
def test_background_custom(self):
|
||||
style = SubtitleStyle.from_dict(
|
||||
{
|
||||
"background_enabled": True,
|
||||
"background_color": "#00FF00",
|
||||
"background_opacity": 0.8,
|
||||
"background_padding": 12,
|
||||
"background_radius": 8,
|
||||
}
|
||||
)
|
||||
assert style.background_enabled is True
|
||||
assert style.background_color == "#00FF00"
|
||||
assert style.background_opacity == 0.8
|
||||
assert style.background_padding == 12
|
||||
assert style.background_radius == 8
|
||||
|
||||
def test_background_opacity_clamped(self):
|
||||
style = SubtitleStyle.from_dict({"background_opacity": -0.5})
|
||||
assert style.background_opacity == 0.0
|
||||
style2 = SubtitleStyle.from_dict({"background_opacity": 2.0})
|
||||
assert style2.background_opacity == 1.0
|
||||
|
||||
def test_position_alias_top(self):
|
||||
style = SubtitleStyle.from_dict({"position": "top"})
|
||||
assert style.position == "top_center"
|
||||
|
||||
def test_position_alias_bottom(self):
|
||||
style = SubtitleStyle.from_dict({"position": "bottom"})
|
||||
assert style.position == "bottom_center"
|
||||
|
||||
def test_position_alias_left(self):
|
||||
style = SubtitleStyle.from_dict({"position": "left"})
|
||||
assert style.position == "middle_left"
|
||||
|
||||
def test_invalid_position_falls_back(self):
|
||||
style = SubtitleStyle.from_dict({"position": "invalid_pos"})
|
||||
assert style.position == DEFAULT_POSITION
|
||||
|
||||
def test_margins(self):
|
||||
style = SubtitleStyle.from_dict(
|
||||
{
|
||||
"margin_v": 80,
|
||||
"margin_l": 60,
|
||||
"margin_r": 60,
|
||||
}
|
||||
)
|
||||
assert style.margin_v == 80
|
||||
assert style.margin_l == 60
|
||||
assert style.margin_r == 60
|
||||
|
||||
def test_line_spacing(self):
|
||||
style = SubtitleStyle.from_dict({"line_spacing": 4})
|
||||
assert style.line_spacing == 4
|
||||
|
||||
def test_fade_in_out_clamped(self):
|
||||
style = SubtitleStyle.from_dict({"fade_in": -1.0, "fade_out": -0.5})
|
||||
assert style.fade_in == 0.0
|
||||
assert style.fade_out == 0.0
|
||||
|
||||
def test_invalid_int_uses_default(self):
|
||||
style = SubtitleStyle.from_dict({"size": "not_a_number"})
|
||||
assert style.font_size == DEFAULT_FONT_SIZE
|
||||
|
||||
def test_none_str_uses_default(self):
|
||||
style = SubtitleStyle.from_dict({"color": None})
|
||||
assert style.font_color == DEFAULT_COLOR
|
||||
|
||||
def test_animation_type(self):
|
||||
style = SubtitleStyle.from_dict({"animation_type": "fade"})
|
||||
assert style.animation_type == "fade"
|
||||
|
||||
|
||||
class TestSubtitleSegment:
|
||||
def test_basic_segment(self):
|
||||
seg = SubtitleSegment(start=1.0, end=3.5, text="你好世界")
|
||||
assert seg.start == 1.0
|
||||
assert seg.end == 3.5
|
||||
assert seg.text == "你好世界"
|
||||
assert seg.style_name == "Default"
|
||||
|
||||
def test_custom_style(self):
|
||||
seg = SubtitleSegment(start=0.0, end=2.0, text="test", style_name="Title")
|
||||
assert seg.style_name == "Title"
|
||||
|
||||
def test_duration(self):
|
||||
seg = SubtitleSegment(start=1.0, end=3.5, text="test")
|
||||
assert seg.duration == 2.5
|
||||
|
||||
def test_duration_zero_when_end_before_start(self):
|
||||
seg = SubtitleSegment(start=5.0, end=3.0, text="test")
|
||||
assert seg.duration == 0.0
|
||||
|
||||
def test_is_valid_true(self):
|
||||
seg = SubtitleSegment(start=1.0, end=3.0, text="hello")
|
||||
assert seg.is_valid is True
|
||||
|
||||
def test_is_valid_empty_text(self):
|
||||
seg = SubtitleSegment(start=1.0, end=3.0, text="")
|
||||
assert seg.is_valid is False
|
||||
|
||||
def test_is_valid_zero_duration(self):
|
||||
seg = SubtitleSegment(start=1.0, end=1.0, text="hello")
|
||||
assert seg.is_valid is False
|
||||
|
||||
def test_is_valid_negative_duration(self):
|
||||
seg = SubtitleSegment(start=5.0, end=3.0, text="hello")
|
||||
assert seg.is_valid is False
|
||||
|
||||
|
||||
class TestConstants:
|
||||
def test_position_alignment_has_9_positions(self):
|
||||
assert len(POSITION_ALIGNMENT) == 9
|
||||
|
||||
def test_position_aliases_resolve_to_valid(self):
|
||||
for _alias, full in POSITION_ALIASES.items():
|
||||
assert full in POSITION_ALIGNMENT
|
||||
|
||||
def test_allowed_extensions_contains_common(self):
|
||||
assert ".srt" in ALLOWED_SUBTITLE_EXTENSIONS
|
||||
assert ".ass" in ALLOWED_SUBTITLE_EXTENSIONS
|
||||
assert ".vtt" in ALLOWED_SUBTITLE_EXTENSIONS
|
||||
|
||||
def test_defaults_are_valid(self):
|
||||
assert isinstance(DEFAULT_FONT, str)
|
||||
assert isinstance(DEFAULT_FONT_SIZE, int)
|
||||
assert DEFAULT_FONT_SIZE > 0
|
||||
assert DEFAULT_COLOR.startswith("#")
|
||||
assert DEFAULT_POSITION in POSITION_ALIGNMENT
|
||||
@@ -1,347 +0,0 @@
|
||||
"""template_clip_config 单测.
|
||||
|
||||
domain 层模板片段配置纯逻辑模块,0 外部依赖。
|
||||
覆盖:ClipType枚举、TransitionEffect枚举、create工厂/校验、计算属性。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.template_clip_config import (
|
||||
ClipType,
|
||||
TemplateClipConfig,
|
||||
TransitionEffect,
|
||||
)
|
||||
|
||||
|
||||
class TestClipType:
|
||||
"""ClipType 枚举测试."""
|
||||
|
||||
def test_six_types(self):
|
||||
"""六种片段类型."""
|
||||
assert len(ClipType) == 6
|
||||
|
||||
def test_intro(self):
|
||||
assert ClipType.INTRO == "intro"
|
||||
|
||||
def test_main(self):
|
||||
assert ClipType.MAIN == "main"
|
||||
|
||||
def test_transition(self):
|
||||
assert ClipType.TRANSITION == "transition"
|
||||
|
||||
def test_outro(self):
|
||||
assert ClipType.OUTRO == "outro"
|
||||
|
||||
def test_title(self):
|
||||
assert ClipType.TITLE == "title"
|
||||
|
||||
def test_subtitle(self):
|
||||
assert ClipType.SUBTITLE == "subtitle"
|
||||
|
||||
def test_from_string(self):
|
||||
"""可从字符串构造."""
|
||||
assert ClipType("main") == ClipType.MAIN
|
||||
assert ClipType("intro") == ClipType.INTRO
|
||||
|
||||
|
||||
class TestTransitionEffect:
|
||||
"""TransitionEffect 枚举测试."""
|
||||
|
||||
def test_six_effects(self):
|
||||
"""六种转场效果."""
|
||||
assert len(TransitionEffect) == 6
|
||||
|
||||
def test_cut(self):
|
||||
assert TransitionEffect.CUT == "cut"
|
||||
|
||||
def test_fade(self):
|
||||
assert TransitionEffect.FADE == "fade"
|
||||
|
||||
def test_slide_left(self):
|
||||
assert TransitionEffect.SLIDE_LEFT == "slide_left"
|
||||
|
||||
def test_slide_right(self):
|
||||
assert TransitionEffect.SLIDE_RIGHT == "slide_right"
|
||||
|
||||
def test_dissolve(self):
|
||||
assert TransitionEffect.DISSOLVE == "dissolve"
|
||||
|
||||
def test_wipe(self):
|
||||
assert TransitionEffect.WIPE == "wipe"
|
||||
|
||||
def test_from_string(self):
|
||||
"""可从字符串构造."""
|
||||
assert TransitionEffect("fade") == TransitionEffect.FADE
|
||||
assert TransitionEffect("cut") == TransitionEffect.CUT
|
||||
|
||||
|
||||
class TestTemplateClipConfigCreate:
|
||||
"""TemplateClipConfig.create 工厂测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简创建."""
|
||||
clip = TemplateClipConfig.create(
|
||||
template_id="tpl1",
|
||||
clip_type=ClipType.MAIN,
|
||||
order=0,
|
||||
)
|
||||
assert clip.template_id == "tpl1"
|
||||
assert clip.clip_type == ClipType.MAIN
|
||||
assert clip.order == 0
|
||||
assert clip.min_duration == 0.0
|
||||
assert clip.max_duration == 0.0
|
||||
assert clip.text_template == ""
|
||||
assert clip.material_requirements == {}
|
||||
assert clip.transition_effect == TransitionEffect.CUT
|
||||
assert clip.config == {}
|
||||
assert isinstance(clip.id, str)
|
||||
assert len(clip.id) > 0
|
||||
|
||||
def test_create_with_string_clip_type(self):
|
||||
"""用字符串传 clip_type."""
|
||||
clip = TemplateClipConfig.create(
|
||||
template_id="t1",
|
||||
clip_type="intro",
|
||||
order=0,
|
||||
)
|
||||
assert clip.clip_type == ClipType.INTRO
|
||||
|
||||
def test_create_with_string_transition(self):
|
||||
"""用字符串传 transition_effect."""
|
||||
clip = TemplateClipConfig.create(
|
||||
template_id="t1",
|
||||
clip_type=ClipType.MAIN,
|
||||
order=0,
|
||||
transition_effect="fade",
|
||||
)
|
||||
assert clip.transition_effect == TransitionEffect.FADE
|
||||
|
||||
def test_create_full(self):
|
||||
"""带全部字段."""
|
||||
clip = TemplateClipConfig.create(
|
||||
template_id=" tpl1 ",
|
||||
clip_type=ClipType.TITLE,
|
||||
order=2,
|
||||
min_duration=2.0,
|
||||
max_duration=5.0,
|
||||
text_template=" 欢迎关注 ",
|
||||
material_requirements={"category": "scenic"},
|
||||
transition_effect=TransitionEffect.FADE,
|
||||
config={"font_size": 24},
|
||||
)
|
||||
assert clip.template_id == "tpl1" # strip
|
||||
assert clip.clip_type == ClipType.TITLE
|
||||
assert clip.order == 2
|
||||
assert clip.min_duration == 2.0
|
||||
assert clip.max_duration == 5.0
|
||||
assert clip.text_template == "欢迎关注" # strip
|
||||
assert clip.material_requirements == {"category": "scenic"}
|
||||
assert clip.transition_effect == TransitionEffect.FADE
|
||||
assert clip.config == {"font_size": 24}
|
||||
|
||||
def test_create_empty_template_id(self):
|
||||
"""空 template_id 无效."""
|
||||
try:
|
||||
TemplateClipConfig.create(template_id="", clip_type=ClipType.MAIN, order=0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "template_id" in str(e)
|
||||
|
||||
def test_create_whitespace_template_id(self):
|
||||
"""空白 template_id 无效."""
|
||||
try:
|
||||
TemplateClipConfig.create(template_id=" ", clip_type=ClipType.MAIN, order=0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "template_id" in str(e)
|
||||
|
||||
def test_create_negative_min_duration(self):
|
||||
"""min_duration 为负无效."""
|
||||
try:
|
||||
TemplateClipConfig.create(
|
||||
template_id="t1",
|
||||
clip_type=ClipType.MAIN,
|
||||
order=0,
|
||||
min_duration=-1.0,
|
||||
)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "min_duration" in str(e)
|
||||
|
||||
def test_create_negative_max_duration(self):
|
||||
"""max_duration 为负无效."""
|
||||
try:
|
||||
TemplateClipConfig.create(
|
||||
template_id="t1",
|
||||
clip_type=ClipType.MAIN,
|
||||
order=0,
|
||||
max_duration=-1.0,
|
||||
)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "max_duration" in str(e)
|
||||
|
||||
def test_create_min_greater_than_max(self):
|
||||
"""min > max 且 max > 0 时无效."""
|
||||
try:
|
||||
TemplateClipConfig.create(
|
||||
template_id="t1",
|
||||
clip_type=ClipType.MAIN,
|
||||
order=0,
|
||||
min_duration=5.0,
|
||||
max_duration=3.0,
|
||||
)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "min_duration" in str(e) and "max_duration" in str(e)
|
||||
|
||||
def test_create_min_equals_max_valid(self):
|
||||
"""min == max 合法."""
|
||||
clip = TemplateClipConfig.create(
|
||||
template_id="t1",
|
||||
clip_type=ClipType.MAIN,
|
||||
order=0,
|
||||
min_duration=3.0,
|
||||
max_duration=3.0,
|
||||
)
|
||||
assert clip.min_duration == 3.0
|
||||
assert clip.max_duration == 3.0
|
||||
|
||||
def test_create_zero_both_valid(self):
|
||||
"""都为 0 合法(未设置时长)."""
|
||||
clip = TemplateClipConfig.create(
|
||||
template_id="t1",
|
||||
clip_type=ClipType.MAIN,
|
||||
order=0,
|
||||
min_duration=0.0,
|
||||
max_duration=0.0,
|
||||
)
|
||||
assert clip.min_duration == 0.0
|
||||
assert clip.max_duration == 0.0
|
||||
|
||||
def test_create_invalid_clip_type_string(self):
|
||||
"""无效的 clip_type 字符串抛错."""
|
||||
try:
|
||||
TemplateClipConfig.create(template_id="t1", clip_type="invalid", order=0)
|
||||
assert False
|
||||
except ValueError:
|
||||
pass # 枚举构造失败会抛 ValueError
|
||||
|
||||
def test_create_material_req_none_defaults_empty(self):
|
||||
"""material_requirements=None 默认为空 dict."""
|
||||
clip = TemplateClipConfig.create(
|
||||
template_id="t1",
|
||||
clip_type=ClipType.MAIN,
|
||||
order=0,
|
||||
material_requirements=None,
|
||||
)
|
||||
assert clip.material_requirements == {}
|
||||
|
||||
def test_create_config_none_defaults_empty(self):
|
||||
"""config=None 默认为空 dict."""
|
||||
clip = TemplateClipConfig.create(
|
||||
template_id="t1",
|
||||
clip_type=ClipType.MAIN,
|
||||
order=0,
|
||||
config=None,
|
||||
)
|
||||
assert clip.config == {}
|
||||
|
||||
def test_create_unique_id(self):
|
||||
"""不同配置 id 不同."""
|
||||
c1 = TemplateClipConfig.create("t", ClipType.MAIN, 0)
|
||||
c2 = TemplateClipConfig.create("t", ClipType.MAIN, 0)
|
||||
assert c1.id != c2.id
|
||||
|
||||
def test_create_has_timestamps(self):
|
||||
"""有创建和更新时间."""
|
||||
clip = TemplateClipConfig.create("t", ClipType.MAIN, 0)
|
||||
assert clip.created_at is not None
|
||||
assert clip.updated_at is not None
|
||||
|
||||
|
||||
class TestTemplateClipConfigProperties:
|
||||
"""计算属性测试."""
|
||||
|
||||
def test_has_duration_range_false_zero(self):
|
||||
"""都为 0 时没有时长范围."""
|
||||
clip = TemplateClipConfig.create("t", ClipType.MAIN, 0)
|
||||
assert clip.has_duration_range is False
|
||||
|
||||
def test_has_duration_range_true_min_only(self):
|
||||
"""只有 min > 0 也算有范围."""
|
||||
clip = TemplateClipConfig.create(
|
||||
"t",
|
||||
ClipType.MAIN,
|
||||
0,
|
||||
min_duration=2.0,
|
||||
)
|
||||
assert clip.has_duration_range is True
|
||||
|
||||
def test_has_duration_range_true_max_only(self):
|
||||
"""只有 max > 0 也算有范围."""
|
||||
clip = TemplateClipConfig.create(
|
||||
"t",
|
||||
ClipType.MAIN,
|
||||
0,
|
||||
max_duration=5.0,
|
||||
)
|
||||
assert clip.has_duration_range is True
|
||||
|
||||
def test_has_duration_range_true_both(self):
|
||||
"""两者都 > 0 有范围."""
|
||||
clip = TemplateClipConfig.create(
|
||||
"t",
|
||||
ClipType.MAIN,
|
||||
0,
|
||||
min_duration=2.0,
|
||||
max_duration=5.0,
|
||||
)
|
||||
assert clip.has_duration_range is True
|
||||
|
||||
def test_default_duration_zero(self):
|
||||
"""都为 0 默认时长 0."""
|
||||
clip = TemplateClipConfig.create("t", ClipType.MAIN, 0)
|
||||
assert clip.default_duration == 0.0
|
||||
|
||||
def test_default_duration_middle(self):
|
||||
"""两者都有取中间值."""
|
||||
clip = TemplateClipConfig.create(
|
||||
"t",
|
||||
ClipType.MAIN,
|
||||
0,
|
||||
min_duration=2.0,
|
||||
max_duration=6.0,
|
||||
)
|
||||
assert clip.default_duration == 4.0
|
||||
|
||||
def test_default_duration_min_only(self):
|
||||
"""只有 min 用 min."""
|
||||
clip = TemplateClipConfig.create(
|
||||
"t",
|
||||
ClipType.MAIN,
|
||||
0,
|
||||
min_duration=3.0,
|
||||
)
|
||||
assert clip.default_duration == 3.0
|
||||
|
||||
def test_default_duration_max_only(self):
|
||||
"""只有 max 用 max."""
|
||||
clip = TemplateClipConfig.create(
|
||||
"t",
|
||||
ClipType.MAIN,
|
||||
0,
|
||||
max_duration=5.0,
|
||||
)
|
||||
assert clip.default_duration == 5.0
|
||||
|
||||
def test_default_duration_equal_min_max(self):
|
||||
"""min == max 时值相等."""
|
||||
clip = TemplateClipConfig.create(
|
||||
"t",
|
||||
ClipType.MAIN,
|
||||
0,
|
||||
min_duration=3.0,
|
||||
max_duration=3.0,
|
||||
)
|
||||
assert clip.default_duration == 3.0
|
||||
@@ -1,240 +0,0 @@
|
||||
"""transition_presets 单元测试 - wave168
|
||||
|
||||
覆盖:
|
||||
- TransitionPreset 数据类(frozen/默认值/字段)
|
||||
- TRANSITION_PRESET_LIBRARY 预设库(数量/分类/ID唯一性)
|
||||
- get_transition_preset 按ID获取
|
||||
- list_transition_presets 筛选列表(分类/关键词)
|
||||
- get_default_transition 默认转场
|
||||
"""
|
||||
|
||||
import dataclasses
|
||||
|
||||
import pytest
|
||||
|
||||
from packages.domain.transition_presets import (
|
||||
TRANSITION_PRESET_LIBRARY,
|
||||
TransitionPreset,
|
||||
get_default_transition,
|
||||
get_transition_preset,
|
||||
list_transition_presets,
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# TransitionPreset 数据类
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestTransitionPresetDataclass:
|
||||
def test_minimal_creation(self):
|
||||
p = TransitionPreset(id="test", name="Test", category="basic")
|
||||
assert p.id == "test"
|
||||
assert p.name == "Test"
|
||||
assert p.category == "basic"
|
||||
|
||||
def test_default_values(self):
|
||||
p = TransitionPreset(id="t", name="T", category="basic")
|
||||
assert p.description == ""
|
||||
assert p.tags == []
|
||||
assert p.transition == "fade"
|
||||
assert p.default_duration == 0.5
|
||||
assert p.min_duration == 0.1
|
||||
assert p.max_duration == 3.0
|
||||
assert p.has_custom_params is False
|
||||
|
||||
def test_full_creation(self):
|
||||
p = TransitionPreset(
|
||||
id="full",
|
||||
name="Full",
|
||||
category="fade",
|
||||
description="test desc",
|
||||
tags=["tag1", "tag2"],
|
||||
transition="fadeblack",
|
||||
default_duration=1.0,
|
||||
min_duration=0.2,
|
||||
max_duration=5.0,
|
||||
has_custom_params=True,
|
||||
)
|
||||
assert p.description == "test desc"
|
||||
assert p.tags == ["tag1", "tag2"]
|
||||
assert p.transition == "fadeblack"
|
||||
assert p.default_duration == 1.0
|
||||
assert p.min_duration == 0.2
|
||||
assert p.max_duration == 5.0
|
||||
assert p.has_custom_params is True
|
||||
|
||||
def test_frozen_immutable(self):
|
||||
p = TransitionPreset(id="t", name="T", category="basic")
|
||||
with pytest.raises(dataclasses.FrozenInstanceError):
|
||||
p.name = "Changed"
|
||||
|
||||
def test_tags_default_new_list(self):
|
||||
p1 = TransitionPreset(id="t1", name="T1", category="basic")
|
||||
p2 = TransitionPreset(id="t2", name="T2", category="basic")
|
||||
assert p1.tags is not p2.tags
|
||||
|
||||
|
||||
# ============================================================
|
||||
# TRANSITION_PRESET_LIBRARY 预设库
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestTransitionPresetLibrary:
|
||||
def test_library_not_empty(self):
|
||||
assert len(TRANSITION_PRESET_LIBRARY) > 0
|
||||
|
||||
def test_all_ids_unique(self):
|
||||
ids = [p.id for p in TRANSITION_PRESET_LIBRARY]
|
||||
assert len(ids) == len(set(ids))
|
||||
|
||||
def test_all_have_required_fields(self):
|
||||
for p in TRANSITION_PRESET_LIBRARY:
|
||||
assert p.id, f"缺少id: {p}"
|
||||
assert p.name, f"缺少name: {p.id}"
|
||||
assert p.category, f"缺少category: {p.id}"
|
||||
assert p.transition, f"缺少transition: {p.id}"
|
||||
|
||||
def test_valid_categories(self):
|
||||
valid = {"basic", "fade", "slide", "zoom", "warp", "special"}
|
||||
for p in TRANSITION_PRESET_LIBRARY:
|
||||
assert p.category in valid, f"无效分类: {p.id} -> {p.category}"
|
||||
|
||||
def test_basic_category_exists(self):
|
||||
basics = [p for p in TRANSITION_PRESET_LIBRARY if p.category == "basic"]
|
||||
assert len(basics) >= 1
|
||||
|
||||
def test_fade_category_exists(self):
|
||||
fades = [p for p in TRANSITION_PRESET_LIBRARY if p.category == "fade"]
|
||||
assert len(fades) >= 1
|
||||
|
||||
def test_transition_none_exists(self):
|
||||
none_p = get_transition_preset("transition_none")
|
||||
assert none_p is not None
|
||||
assert none_p.transition == "none"
|
||||
assert none_p.default_duration == 0.0
|
||||
|
||||
def test_duration_consistency(self):
|
||||
# min <= default <= max
|
||||
for p in TRANSITION_PRESET_LIBRARY:
|
||||
assert p.min_duration <= p.default_duration, f"{p.id}: min > default"
|
||||
assert p.default_duration <= p.max_duration, f"{p.id}: default > max"
|
||||
|
||||
def test_total_count(self):
|
||||
# 至少有10个转场预设
|
||||
assert len(TRANSITION_PRESET_LIBRARY) >= 10
|
||||
|
||||
|
||||
# ============================================================
|
||||
# get_transition_preset
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestGetTransitionPreset:
|
||||
def test_existing_id(self):
|
||||
p = get_transition_preset("transition_none")
|
||||
assert p is not None
|
||||
assert p.id == "transition_none"
|
||||
assert p.name == "无转场"
|
||||
|
||||
def test_nonexistent_id(self):
|
||||
assert get_transition_preset("nonexistent") is None
|
||||
|
||||
def test_empty_id(self):
|
||||
assert get_transition_preset("") is None
|
||||
|
||||
def test_case_sensitive(self):
|
||||
assert get_transition_preset("Transition_None") is None
|
||||
|
||||
def test_returns_preset_object(self):
|
||||
p = get_transition_preset("transition_none")
|
||||
assert isinstance(p, TransitionPreset)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# list_transition_presets
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestListTransitionPresets:
|
||||
def test_no_filters_returns_all(self):
|
||||
result = list_transition_presets()
|
||||
assert len(result) == len(TRANSITION_PRESET_LIBRARY)
|
||||
|
||||
def test_filter_by_category_basic(self):
|
||||
result = list_transition_presets(category="basic")
|
||||
assert len(result) > 0
|
||||
for p in result:
|
||||
assert p.category == "basic"
|
||||
|
||||
def test_filter_by_category_fade(self):
|
||||
result = list_transition_presets(category="fade")
|
||||
for p in result:
|
||||
assert p.category == "fade"
|
||||
|
||||
def test_filter_by_category_slide(self):
|
||||
result = list_transition_presets(category="slide")
|
||||
for p in result:
|
||||
assert p.category == "slide"
|
||||
|
||||
def test_invalid_category_returns_empty(self):
|
||||
result = list_transition_presets(category="nonexistent")
|
||||
assert result == []
|
||||
|
||||
def test_keyword_search_name(self):
|
||||
result = list_transition_presets(keyword="淡")
|
||||
assert len(result) >= 1
|
||||
names = [p.name for p in result]
|
||||
assert any("淡" in n for n in names)
|
||||
|
||||
def test_keyword_search_tags(self):
|
||||
result = list_transition_presets(keyword="硬切")
|
||||
assert len(result) >= 1
|
||||
found = any(any("硬切" in t for t in p.tags) or "硬切" in p.name for p in result)
|
||||
assert found
|
||||
|
||||
def test_keyword_search_description(self):
|
||||
# 搜索描述中的关键词
|
||||
result = list_transition_presets(keyword="过渡")
|
||||
assert isinstance(result, list)
|
||||
|
||||
def test_keyword_nonexistent_returns_empty(self):
|
||||
result = list_transition_presets(keyword="zzz不存在的关键词zzz")
|
||||
assert result == []
|
||||
|
||||
def test_category_and_keyword_combined(self):
|
||||
result = list_transition_presets(category="fade", keyword="淡")
|
||||
for p in result:
|
||||
assert p.category == "fade"
|
||||
assert "淡" in p.name or "淡" in p.description or any("淡" in t for t in p.tags)
|
||||
|
||||
def test_keyword_empty_returns_all(self):
|
||||
result = list_transition_presets(keyword="")
|
||||
assert len(result) == len(TRANSITION_PRESET_LIBRARY)
|
||||
|
||||
def test_returns_list_of_presets(self):
|
||||
result = list_transition_presets()
|
||||
for p in result:
|
||||
assert isinstance(p, TransitionPreset)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# get_default_transition
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestGetDefaultTransition:
|
||||
def test_returns_preset(self):
|
||||
p = get_default_transition()
|
||||
assert isinstance(p, TransitionPreset)
|
||||
|
||||
def test_default_is_none(self):
|
||||
p = get_default_transition()
|
||||
assert p.id == "transition_none"
|
||||
|
||||
def test_default_has_zero_duration(self):
|
||||
p = get_default_transition()
|
||||
assert p.default_duration == 0.0
|
||||
|
||||
def test_default_is_hard_cut(self):
|
||||
p = get_default_transition()
|
||||
assert p.transition == "none"
|
||||
@@ -1,612 +0,0 @@
|
||||
"""trim_config 单元测试 - wave165
|
||||
|
||||
覆盖:
|
||||
- TrimConfig.from_dict 构造
|
||||
- TrimConfig.validate_and_resolve 三选二推导 + 边界钳制
|
||||
- TrimConfig.is_valid / is_noop / trim_from_start 属性
|
||||
- TrimSegment.from_dict 构造
|
||||
- build_video_trim_filter 视频裁剪滤镜
|
||||
- build_audio_trim_filter 音频裁剪滤镜
|
||||
- resolve_segments 多段解析
|
||||
- parse_segments_from_config 配置解析
|
||||
- extract_trim_from_clip_config 提取工具
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from packages.domain.trim_config import (
|
||||
MIN_TRIM_DURATION,
|
||||
TrimConfig,
|
||||
TrimSegment,
|
||||
build_audio_trim_filter,
|
||||
build_video_trim_filter,
|
||||
extract_trim_from_clip_config,
|
||||
parse_segments_from_config,
|
||||
resolve_segments,
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# TrimConfig.from_dict
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestTrimConfigFromDict:
|
||||
def test_none_returns_none(self):
|
||||
assert TrimConfig.from_dict(None) is None
|
||||
|
||||
def test_empty_dict_returns_none(self):
|
||||
assert TrimConfig.from_dict({}) is None
|
||||
|
||||
def test_all_zero_returns_none(self):
|
||||
assert TrimConfig.from_dict({"start_time": 0, "end_time": 0, "duration": 0}) is None
|
||||
|
||||
def test_start_only_returns_config(self):
|
||||
result = TrimConfig.from_dict({"start_time": 5.0})
|
||||
assert result is not None
|
||||
assert result.start_time == 5.0
|
||||
assert result.end_time == 0
|
||||
assert result.duration == 0
|
||||
|
||||
def test_duration_only_returns_config(self):
|
||||
result = TrimConfig.from_dict({"duration": 10.0})
|
||||
assert result is not None
|
||||
assert result.duration == 10.0
|
||||
assert result.start_time == 0
|
||||
|
||||
def test_start_and_end(self):
|
||||
result = TrimConfig.from_dict({"start_time": 5.0, "end_time": 15.0})
|
||||
assert result is not None
|
||||
assert result.start_time == 5.0
|
||||
assert result.end_time == 15.0
|
||||
|
||||
def test_start_and_duration(self):
|
||||
result = TrimConfig.from_dict({"start_time": 2.0, "duration": 8.0})
|
||||
assert result is not None
|
||||
assert result.start_time == 2.0
|
||||
assert result.duration == 8.0
|
||||
|
||||
def test_end_and_duration(self):
|
||||
result = TrimConfig.from_dict({"end_time": 20.0, "duration": 5.0})
|
||||
assert result is not None
|
||||
assert result.end_time == 20.0
|
||||
assert result.duration == 5.0
|
||||
|
||||
def test_all_three(self):
|
||||
result = TrimConfig.from_dict({"start_time": 1, "end_time": 5, "duration": 4})
|
||||
assert result is not None
|
||||
assert result.start_time == 1.0
|
||||
|
||||
def test_string_values(self):
|
||||
result = TrimConfig.from_dict({"start_time": "3.5", "duration": "2.0"})
|
||||
assert result is not None
|
||||
assert result.start_time == 3.5
|
||||
assert result.duration == 2.0
|
||||
|
||||
def test_falsy_values_treated_as_zero(self):
|
||||
result = TrimConfig.from_dict({"start_time": None, "duration": None})
|
||||
assert result is None # 两个都是None等价于0
|
||||
|
||||
def test_zero_start_with_duration(self):
|
||||
result = TrimConfig.from_dict({"start_time": 0, "duration": 5.0})
|
||||
assert result is not None
|
||||
assert result.duration == 5.0
|
||||
|
||||
|
||||
# ============================================================
|
||||
# validate_and_resolve - 三选二推导
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestValidateAndResolveDerive:
|
||||
ASSET_DUR = 60.0 # 素材时长60秒
|
||||
|
||||
def test_start_plus_end(self):
|
||||
config = TrimConfig(start_time=10.0, end_time=25.0)
|
||||
result = config.validate_and_resolve(self.ASSET_DUR)
|
||||
assert result.start_time == 10.0
|
||||
assert result.end_time == 25.0
|
||||
assert result.duration == 15.0
|
||||
|
||||
def test_start_plus_duration(self):
|
||||
config = TrimConfig(start_time=5.0, duration=10.0)
|
||||
result = config.validate_and_resolve(self.ASSET_DUR)
|
||||
assert result.start_time == 5.0
|
||||
assert result.end_time == 15.0
|
||||
assert result.duration == 10.0
|
||||
|
||||
def test_end_plus_duration(self):
|
||||
config = TrimConfig(end_time=30.0, duration=10.0)
|
||||
result = config.validate_and_resolve(self.ASSET_DUR)
|
||||
assert result.start_time == 20.0
|
||||
assert result.end_time == 30.0
|
||||
assert result.duration == 10.0
|
||||
|
||||
def test_end_plus_duration_start_negative_clamped(self):
|
||||
# end=5, duration=10 → start=-5 → 钳制到0,duration=5
|
||||
config = TrimConfig(end_time=5.0, duration=10.0)
|
||||
result = config.validate_and_resolve(self.ASSET_DUR)
|
||||
assert result.start_time == 0.0
|
||||
assert result.end_time == 5.0
|
||||
assert result.duration == 5.0
|
||||
|
||||
def test_start_only_takes_to_end(self):
|
||||
config = TrimConfig(start_time=50.0)
|
||||
result = config.validate_and_resolve(self.ASSET_DUR)
|
||||
assert result.start_time == 50.0
|
||||
assert result.end_time == 60.0
|
||||
assert result.duration == 10.0
|
||||
|
||||
def test_end_only_starts_from_zero(self):
|
||||
config = TrimConfig(end_time=30.0)
|
||||
result = config.validate_and_resolve(self.ASSET_DUR)
|
||||
assert result.start_time == 0.0
|
||||
assert result.end_time == 30.0
|
||||
assert result.duration == 30.0
|
||||
|
||||
def test_duration_only_starts_from_zero(self):
|
||||
config = TrimConfig(duration=20.0)
|
||||
result = config.validate_and_resolve(self.ASSET_DUR)
|
||||
assert result.start_time == 0.0
|
||||
assert result.end_time == 20.0
|
||||
assert result.duration == 20.0
|
||||
|
||||
def test_all_three_uses_start_end(self):
|
||||
# start=5, end=20, duration=10 → 优先用 start+end 推导 → duration=15
|
||||
config = TrimConfig(start_time=5.0, end_time=20.0, duration=10.0)
|
||||
result = config.validate_and_resolve(self.ASSET_DUR)
|
||||
assert result.start_time == 5.0
|
||||
assert result.end_time == 20.0
|
||||
assert result.duration == 15.0 # start+end 优先
|
||||
|
||||
def test_start_equals_end_invalid(self):
|
||||
config = TrimConfig(start_time=10.0, end_time=10.0)
|
||||
result = config.validate_and_resolve(self.ASSET_DUR)
|
||||
assert result.duration == 0.0
|
||||
assert result.is_valid is False
|
||||
|
||||
|
||||
# ============================================================
|
||||
# validate_and_resolve - 边界钳制
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestValidateAndResolveClamp:
|
||||
ASSET_DUR = 60.0
|
||||
|
||||
def test_negative_start_clamped(self):
|
||||
config = TrimConfig(start_time=-5.0, duration=10.0)
|
||||
result = config.validate_and_resolve(self.ASSET_DUR)
|
||||
assert result.start_time == 0.0
|
||||
assert result.duration == 10.0
|
||||
|
||||
def test_end_exceeds_asset_duration(self):
|
||||
config = TrimConfig(start_time=50.0, duration=20.0)
|
||||
result = config.validate_and_resolve(self.ASSET_DUR)
|
||||
assert result.end_time == 60.0
|
||||
assert result.duration == 10.0
|
||||
|
||||
def test_start_exceeds_asset_duration(self):
|
||||
config = TrimConfig(start_time=70.0, duration=5.0)
|
||||
result = config.validate_and_resolve(self.ASSET_DUR)
|
||||
assert result.start_time < self.ASSET_DUR
|
||||
assert result.end_time == self.ASSET_DUR
|
||||
|
||||
def test_zero_asset_duration_returns_noop(self):
|
||||
config = TrimConfig(start_time=5.0, duration=10.0)
|
||||
result = config.validate_and_resolve(0.0)
|
||||
assert result.is_noop is True
|
||||
|
||||
def test_negative_asset_duration_returns_noop(self):
|
||||
config = TrimConfig(start_time=5.0, duration=10.0)
|
||||
result = config.validate_and_resolve(-1.0)
|
||||
assert result.is_noop is True
|
||||
|
||||
def test_no_params_returns_noop(self):
|
||||
config = TrimConfig()
|
||||
result = config.validate_and_resolve(self.ASSET_DUR)
|
||||
assert result.is_noop is True
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 属性 is_valid / is_noop / trim_from_start
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestTrimConfigProperties:
|
||||
def test_is_valid_true(self):
|
||||
config = TrimConfig(start_time=0, end_time=0, duration=5.0)
|
||||
assert config.is_valid is True
|
||||
|
||||
def test_is_valid_false_zero(self):
|
||||
config = TrimConfig(duration=0.0)
|
||||
assert config.is_valid is False
|
||||
|
||||
def test_is_valid_false_negative(self):
|
||||
config = TrimConfig(duration=-1.0)
|
||||
assert config.is_valid is False
|
||||
|
||||
def test_is_valid_exactly_minimum(self):
|
||||
config = TrimConfig(duration=MIN_TRIM_DURATION)
|
||||
assert config.is_valid is True
|
||||
|
||||
def test_is_valid_below_minimum(self):
|
||||
config = TrimConfig(duration=MIN_TRIM_DURATION / 2)
|
||||
assert config.is_valid is False
|
||||
|
||||
def test_is_noop_true(self):
|
||||
config = TrimConfig()
|
||||
assert config.is_noop is True
|
||||
|
||||
def test_is_noop_false_with_start(self):
|
||||
config = TrimConfig(start_time=5.0)
|
||||
assert config.is_noop is False
|
||||
|
||||
def test_is_noop_false_with_duration(self):
|
||||
config = TrimConfig(duration=10.0)
|
||||
assert config.is_noop is False
|
||||
|
||||
def test_trim_from_start_true(self):
|
||||
config = TrimConfig(start_time=0.0, duration=10.0)
|
||||
assert config.trim_from_start is True
|
||||
|
||||
def test_trim_from_start_false(self):
|
||||
config = TrimConfig(start_time=5.0, duration=10.0)
|
||||
assert config.trim_from_start is False
|
||||
|
||||
def test_trim_from_start_negative_treated_as_start(self):
|
||||
# trim_from_start 检查 start_time <= 0,负值也算从开头
|
||||
config = TrimConfig(start_time=-1.0)
|
||||
assert config.trim_from_start is True
|
||||
|
||||
|
||||
# ============================================================
|
||||
# TrimSegment
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestTrimSegment:
|
||||
def test_from_dict_minimal(self):
|
||||
seg = TrimSegment.from_dict({"start_time": 5.0, "duration": 10.0})
|
||||
assert seg.segment_id # 有默认值
|
||||
assert seg.trim.start_time == 5.0
|
||||
assert seg.trim.duration == 10.0
|
||||
assert seg.order == 0
|
||||
|
||||
def test_from_dict_with_segment_id(self):
|
||||
seg = TrimSegment.from_dict({"segment_id": "seg_abc", "start_time": 1.0, "duration": 2.0})
|
||||
assert seg.segment_id == "seg_abc"
|
||||
|
||||
def test_from_dict_with_order(self):
|
||||
seg = TrimSegment.from_dict({"order": 3, "start_time": 1.0, "duration": 2.0})
|
||||
assert seg.order == 3
|
||||
|
||||
def test_from_dict_default_order(self):
|
||||
seg = TrimSegment.from_dict({"start_time": 1.0}, default_order=5)
|
||||
assert seg.order == 5
|
||||
|
||||
def test_segment_dataclass(self):
|
||||
seg = TrimSegment(
|
||||
segment_id="s1",
|
||||
trim=TrimConfig(start_time=0, duration=5),
|
||||
order=1,
|
||||
)
|
||||
assert seg.segment_id == "s1"
|
||||
assert seg.order == 1
|
||||
assert seg.trim.duration == 5.0
|
||||
|
||||
|
||||
# ============================================================
|
||||
# build_video_trim_filter
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestBuildVideoTrimFilter:
|
||||
def test_noop_passthrough(self):
|
||||
trim = TrimConfig()
|
||||
result = build_video_trim_filter("[0:v]", trim, "[out]")
|
||||
assert result == "[0:v]setpts=PTS-STARTPTS[out]"
|
||||
|
||||
def test_with_start_and_duration(self):
|
||||
trim = TrimConfig(start_time=5.5, duration=10.0).validate_and_resolve(60.0)
|
||||
result = build_video_trim_filter("[0:v]", trim, "[v0]")
|
||||
assert "trim=" in result
|
||||
assert "start=5.500" in result
|
||||
assert "duration=10.000" in result
|
||||
assert "setpts=PTS-STARTPTS" in result
|
||||
assert result.startswith("[0:v]")
|
||||
assert result.endswith("[v0]")
|
||||
|
||||
def test_start_at_zero(self):
|
||||
trim = TrimConfig(duration=5.0).validate_and_resolve(60.0)
|
||||
result = build_video_trim_filter("[in]", trim, "[out]")
|
||||
assert "trim=" in result
|
||||
assert "start=" not in result # start=0 不写
|
||||
assert "duration=5.000" in result
|
||||
|
||||
def test_trim_only_start(self):
|
||||
# 只有 start,duration 由素材推导
|
||||
trim = TrimConfig(start_time=10.0).validate_and_resolve(30.0)
|
||||
result = build_video_trim_filter("[0:v]", trim, "[out]")
|
||||
assert "start=10.000" in result
|
||||
assert "duration=20.000" in result
|
||||
|
||||
def test_comma_separated_filters(self):
|
||||
trim = TrimConfig(start_time=1.0, duration=2.0).validate_and_resolve(10.0)
|
||||
result = build_video_trim_filter("[in]", trim, "[out]")
|
||||
assert "trim=" in result
|
||||
# trim 和 setpts 用逗号分隔
|
||||
assert "setpts=PTS-STARTPTS" in result
|
||||
|
||||
|
||||
# ============================================================
|
||||
# build_audio_trim_filter
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestBuildAudioTrimFilter:
|
||||
def test_noop_passthrough(self):
|
||||
trim = TrimConfig()
|
||||
result = build_audio_trim_filter("[0:a]", trim, "[out]")
|
||||
assert result == "[0:a]asetpts=PTS-STARTPTS[out]"
|
||||
|
||||
def test_with_start_and_duration(self):
|
||||
trim = TrimConfig(start_time=3.0, duration=7.0).validate_and_resolve(60.0)
|
||||
result = build_audio_trim_filter("[0:a]", trim, "[a0]")
|
||||
assert "atrim=" in result
|
||||
assert "start=3.000" in result
|
||||
assert "duration=7.000" in result
|
||||
assert "asetpts=PTS-STARTPTS" in result
|
||||
assert result.startswith("[0:a]")
|
||||
assert result.endswith("[a0]")
|
||||
|
||||
def test_start_at_zero(self):
|
||||
trim = TrimConfig(duration=5.0).validate_and_resolve(60.0)
|
||||
result = build_audio_trim_filter("[in]", trim, "[out]")
|
||||
assert "atrim=" in result
|
||||
assert "start=" not in result
|
||||
assert "duration=5.000" in result
|
||||
|
||||
def test_uses_atrim_not_trim(self):
|
||||
trim = TrimConfig(start_time=1.0, duration=2.0).validate_and_resolve(10.0)
|
||||
result = build_audio_trim_filter("[in]", trim, "[out]")
|
||||
assert "atrim=" in result
|
||||
# 不应该有单独的 trim=(即视频的 trim)
|
||||
# 注意:atrim= 包含 "trim=" 子串,所以检查完整的
|
||||
|
||||
|
||||
# ============================================================
|
||||
# resolve_segments
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestResolveSegments:
|
||||
def test_empty_list(self):
|
||||
result = resolve_segments([], 60.0)
|
||||
assert result == []
|
||||
|
||||
def test_single_segment(self):
|
||||
segs = [
|
||||
TrimSegment(
|
||||
segment_id="s1",
|
||||
trim=TrimConfig(start_time=0, duration=10.0),
|
||||
order=0,
|
||||
)
|
||||
]
|
||||
result = resolve_segments(segs, 60.0)
|
||||
assert len(result) == 1
|
||||
assert result[0].segment_id == "s1"
|
||||
assert result[0].trim.duration == 10.0
|
||||
|
||||
def test_invalid_segment_skipped(self):
|
||||
segs = [
|
||||
TrimSegment(
|
||||
segment_id="good",
|
||||
trim=TrimConfig(start_time=0, duration=10.0),
|
||||
order=0,
|
||||
),
|
||||
TrimSegment(
|
||||
segment_id="bad",
|
||||
trim=TrimConfig(start_time=10, end_time=10), # 0时长
|
||||
order=1,
|
||||
),
|
||||
]
|
||||
result = resolve_segments(segs, 60.0)
|
||||
assert len(result) == 1
|
||||
assert result[0].segment_id == "good"
|
||||
|
||||
def test_sorted_by_order(self):
|
||||
segs = [
|
||||
TrimSegment(
|
||||
segment_id="third",
|
||||
trim=TrimConfig(duration=1.0),
|
||||
order=2,
|
||||
),
|
||||
TrimSegment(
|
||||
segment_id="first",
|
||||
trim=TrimConfig(duration=1.0),
|
||||
order=0,
|
||||
),
|
||||
TrimSegment(
|
||||
segment_id="second",
|
||||
trim=TrimConfig(duration=1.0),
|
||||
order=1,
|
||||
),
|
||||
]
|
||||
result = resolve_segments(segs, 60.0)
|
||||
assert len(result) == 3
|
||||
assert [s.segment_id for s in result] == ["first", "second", "third"]
|
||||
|
||||
def test_negative_order_uses_index(self):
|
||||
segs = [
|
||||
TrimSegment(
|
||||
segment_id="s0",
|
||||
trim=TrimConfig(duration=1.0),
|
||||
order=-1,
|
||||
),
|
||||
TrimSegment(
|
||||
segment_id="s1",
|
||||
trim=TrimConfig(duration=1.0),
|
||||
order=-1,
|
||||
),
|
||||
]
|
||||
result = resolve_segments(segs, 60.0)
|
||||
assert len(result) == 2
|
||||
# order 为负时使用索引 i,所以 s0 order=0, s1 order=1
|
||||
assert result[0].segment_id == "s0"
|
||||
assert result[1].segment_id == "s1"
|
||||
|
||||
def test_respects_asset_duration(self):
|
||||
segs = [
|
||||
TrimSegment(
|
||||
segment_id="s1",
|
||||
trim=TrimConfig(start_time=50, duration=20.0), # 超出60s素材
|
||||
order=0,
|
||||
)
|
||||
]
|
||||
result = resolve_segments(segs, 60.0)
|
||||
assert len(result) == 1
|
||||
assert result[0].trim.end_time == 60.0
|
||||
assert result[0].trim.duration == 10.0
|
||||
|
||||
|
||||
# ============================================================
|
||||
# parse_segments_from_config
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestParseSegmentsFromConfig:
|
||||
def test_none_returns_empty(self):
|
||||
assert parse_segments_from_config(None) == []
|
||||
|
||||
def test_empty_dict_returns_empty(self):
|
||||
assert parse_segments_from_config({}) == []
|
||||
|
||||
def test_trim_segments_list(self):
|
||||
config = {
|
||||
"trim_segments": [
|
||||
{"segment_id": "s1", "start_time": 0, "duration": 5, "order": 0},
|
||||
{"segment_id": "s2", "start_time": 10, "duration": 5, "order": 1},
|
||||
]
|
||||
}
|
||||
result = parse_segments_from_config(config)
|
||||
assert len(result) == 2
|
||||
assert result[0].segment_id == "s1"
|
||||
assert result[1].segment_id == "s2"
|
||||
|
||||
def test_trim_segments_empty_list(self):
|
||||
config = {"trim_segments": []}
|
||||
result = parse_segments_from_config(config)
|
||||
assert result == []
|
||||
|
||||
def test_trim_segments_not_list_ignored(self):
|
||||
config = {"trim_segments": "not_a_list"}
|
||||
result = parse_segments_from_config(config)
|
||||
assert result == []
|
||||
|
||||
def test_single_trim_start(self):
|
||||
config = {"trim_start": 5.0}
|
||||
result = parse_segments_from_config(config)
|
||||
assert len(result) == 1
|
||||
assert result[0].segment_id == "main"
|
||||
assert result[0].trim.start_time == 5.0
|
||||
|
||||
def test_single_trim_end(self):
|
||||
config = {"trim_end": 30.0}
|
||||
result = parse_segments_from_config(config)
|
||||
assert len(result) == 1
|
||||
assert result[0].trim.end_time == 30.0
|
||||
|
||||
def test_single_trim_duration(self):
|
||||
config = {"trim_duration": 10.0}
|
||||
result = parse_segments_from_config(config)
|
||||
assert len(result) == 1
|
||||
assert result[0].trim.duration == 10.0
|
||||
|
||||
def test_single_full(self):
|
||||
config = {"trim_start": 2.0, "trim_end": 8.0, "trim_duration": 6.0}
|
||||
result = parse_segments_from_config(config)
|
||||
assert len(result) == 1
|
||||
assert result[0].trim.start_time == 2.0
|
||||
|
||||
def test_segments_priority_over_single(self):
|
||||
# 同时有 trim_segments 和单段字段,优先多段
|
||||
config = {
|
||||
"trim_segments": [
|
||||
{"segment_id": "s1", "start_time": 0, "duration": 5},
|
||||
],
|
||||
"trim_start": 10.0,
|
||||
}
|
||||
result = parse_segments_from_config(config)
|
||||
assert len(result) == 1
|
||||
assert result[0].segment_id == "s1"
|
||||
|
||||
def test_segment_dict_with_non_dict_entries_ignored(self):
|
||||
config = {
|
||||
"trim_segments": [
|
||||
{"segment_id": "s1", "start_time": 0, "duration": 5},
|
||||
"not_a_dict",
|
||||
None,
|
||||
]
|
||||
}
|
||||
result = parse_segments_from_config(config)
|
||||
assert len(result) == 1
|
||||
|
||||
|
||||
# ============================================================
|
||||
# extract_trim_from_clip_config
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestExtractTrimFromClipConfig:
|
||||
def test_none_returns_none(self):
|
||||
assert extract_trim_from_clip_config(None) is None
|
||||
|
||||
def test_empty_dict_returns_none(self):
|
||||
assert extract_trim_from_clip_config({}) is None
|
||||
|
||||
def test_trim_subdict(self):
|
||||
config = {"trim": {"start_time": 5.0, "duration": 10.0}}
|
||||
result = extract_trim_from_clip_config(config)
|
||||
assert result is not None
|
||||
assert result.start_time == 5.0
|
||||
assert result.duration == 10.0
|
||||
|
||||
def test_trim_subdict_empty(self):
|
||||
config = {"trim": {}}
|
||||
result = extract_trim_from_clip_config(config)
|
||||
assert result is None
|
||||
|
||||
def test_flat_trim_start(self):
|
||||
config = {"trim_start": 5.0, "trim_duration": 10.0}
|
||||
result = extract_trim_from_clip_config(config)
|
||||
assert result is not None
|
||||
assert result.start_time == 5.0
|
||||
assert result.duration == 10.0
|
||||
|
||||
def test_flat_trim_end_only(self):
|
||||
config = {"trim_end": 20.0}
|
||||
result = extract_trim_from_clip_config(config)
|
||||
assert result is not None
|
||||
assert result.end_time == 20.0
|
||||
|
||||
def test_trim_subdict_priority_over_flat(self):
|
||||
# trim子字典优先
|
||||
config = {
|
||||
"trim": {"start_time": 1.0, "duration": 2.0},
|
||||
"trim_start": 10.0,
|
||||
"trim_duration": 20.0,
|
||||
}
|
||||
result = extract_trim_from_clip_config(config)
|
||||
assert result is not None
|
||||
assert result.start_time == 1.0
|
||||
assert result.duration == 2.0
|
||||
|
||||
def test_no_trim_fields(self):
|
||||
config = {"other_field": "value"}
|
||||
assert extract_trim_from_clip_config(config) is None
|
||||
|
||||
def test_trim_not_dict_ignored(self):
|
||||
config = {"trim": "not_a_dict"}
|
||||
# trim 不是 dict,继续看扁平字段 → 没有 → None
|
||||
result = extract_trim_from_clip_config(config)
|
||||
assert result is None
|
||||
@@ -1,567 +0,0 @@
|
||||
"""url_security 单测.
|
||||
|
||||
domain 层 URL 安全校验纯逻辑模块,0 网络依赖。
|
||||
覆盖 SSRF 防护、主机名校验、IP 检查、魔数校验等。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from packages.domain.url_security import (
|
||||
ALLOWED_IMAGE_MIME_TYPES,
|
||||
ALLOWED_PORTS,
|
||||
ALLOWED_SCHEMES,
|
||||
ALLOWED_VIDEO_MIME_TYPES,
|
||||
MAGIC_NUMBERS,
|
||||
MAX_URL_LENGTH,
|
||||
UrlSecurityError,
|
||||
check_internal_hostname,
|
||||
check_ssrf_ip,
|
||||
is_ip_address,
|
||||
is_trusted_domain,
|
||||
is_url_basic_safe,
|
||||
validate_magic_number,
|
||||
validate_url_basic,
|
||||
)
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# 常量与异常类
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestConstants:
|
||||
"""常量测试."""
|
||||
|
||||
def test_allowed_schemes(self):
|
||||
"""允许的 scheme 包含 http 和 https."""
|
||||
assert "http" in ALLOWED_SCHEMES
|
||||
assert "https" in ALLOWED_SCHEMES
|
||||
|
||||
def test_allowed_ports(self):
|
||||
"""允许的端口:80, 443."""
|
||||
assert 80 in ALLOWED_PORTS
|
||||
assert 443 in ALLOWED_PORTS
|
||||
|
||||
def test_max_url_length(self):
|
||||
"""最大 URL 长度 2048."""
|
||||
assert MAX_URL_LENGTH == 2048
|
||||
|
||||
def test_magic_numbers_has_common_formats(self):
|
||||
"""魔数表包含常见格式."""
|
||||
assert "image/jpeg" in MAGIC_NUMBERS
|
||||
assert "image/png" in MAGIC_NUMBERS
|
||||
assert "image/gif" in MAGIC_NUMBERS
|
||||
assert "video/mp4" in MAGIC_NUMBERS
|
||||
assert "audio/mpeg" in MAGIC_NUMBERS
|
||||
|
||||
|
||||
class TestUrlSecurityError:
|
||||
"""异常类测试."""
|
||||
|
||||
def test_is_value_error(self):
|
||||
"""UrlSecurityError 继承 ValueError."""
|
||||
assert issubclass(UrlSecurityError, ValueError)
|
||||
|
||||
def test_raise_with_message(self):
|
||||
"""抛出时携带错误信息."""
|
||||
with pytest.raises(UrlSecurityError, match="test error"):
|
||||
raise UrlSecurityError("test error")
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# check_internal_hostname
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestCheckInternalHostname:
|
||||
"""内部主机名检查测试."""
|
||||
|
||||
def test_normal_domain_passes(self):
|
||||
"""普通外部域名通过."""
|
||||
check_internal_hostname("example.com")
|
||||
check_internal_hostname("www.google.com")
|
||||
|
||||
def test_localhost_blocked(self):
|
||||
"""localhost 被拦截."""
|
||||
with pytest.raises(UrlSecurityError, match="内部主机名"):
|
||||
check_internal_hostname("localhost")
|
||||
|
||||
def test_localhost_case_insensitive(self):
|
||||
"""大小写不敏感."""
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_internal_hostname("LOCALHOST")
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_internal_hostname("LocalHost")
|
||||
|
||||
def test_localhost_localdomain_blocked(self):
|
||||
"""localhost.localdomain 被拦截."""
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_internal_hostname("localhost.localdomain")
|
||||
|
||||
def test_metadata_blocked(self):
|
||||
"""metadata 被拦截."""
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_internal_hostname("metadata")
|
||||
|
||||
def test_metadata_google_internal_blocked(self):
|
||||
"""GCP 元数据服务被拦截."""
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_internal_hostname("metadata.google.internal")
|
||||
|
||||
def test_cloud_metadata_ip_blocked(self):
|
||||
"""云元数据 IP 169.254.169.254 被拦截."""
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_internal_hostname("169.254.169.254")
|
||||
|
||||
def test_local_suffix_blocked(self):
|
||||
""".local 后缀域名被拦截."""
|
||||
with pytest.raises(UrlSecurityError, match="内网域名"):
|
||||
check_internal_hostname("myhost.local")
|
||||
|
||||
def test_internal_suffix_blocked(self):
|
||||
""".internal 后缀被拦截."""
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_internal_hostname("svc.cluster.internal")
|
||||
|
||||
def test_localdomain_suffix_blocked(self):
|
||||
""".localdomain 后缀被拦截."""
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_internal_hostname("host.localdomain")
|
||||
|
||||
def test_com_domain_not_blocked(self):
|
||||
""".com 域名不被拦截."""
|
||||
check_internal_hostname("example.com")
|
||||
check_internal_hostname("sub.example.com")
|
||||
|
||||
def test_subdomain_of_public_domain_ok(self):
|
||||
"""公网域名的子域名正常."""
|
||||
check_internal_hostname("api.example.com")
|
||||
check_internal_hostname("cdn.assets.example.org")
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# is_trusted_domain
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestIsTrustedDomain:
|
||||
"""可信域名匹配测试."""
|
||||
|
||||
def test_empty_trusted_domains_allows_all(self):
|
||||
"""空集合允许所有域名."""
|
||||
assert is_trusted_domain("anything.com", set()) is True
|
||||
assert is_trusted_domain("anywhere.org", set()) is True
|
||||
|
||||
def test_exact_match(self):
|
||||
"""精确匹配."""
|
||||
trusted = {"example.com", "example.org"}
|
||||
assert is_trusted_domain("example.com", trusted) is True
|
||||
assert is_trusted_domain("example.org", trusted) is True
|
||||
|
||||
def test_subdomain_match(self):
|
||||
"""子域名匹配."""
|
||||
trusted = {"example.com"}
|
||||
assert is_trusted_domain("api.example.com", trusted) is True
|
||||
assert is_trusted_domain("cdn.assets.example.com", trusted) is True
|
||||
|
||||
def test_no_match(self):
|
||||
"""不匹配."""
|
||||
trusted = {"example.com"}
|
||||
assert is_trusted_domain("other.com", trusted) is False
|
||||
assert is_trusted_domain("example.net", trusted) is False
|
||||
|
||||
def test_case_insensitive(self):
|
||||
"""大小写不敏感."""
|
||||
trusted = {"Example.COM"}
|
||||
assert is_trusted_domain("example.com", trusted) is True
|
||||
assert is_trusted_domain("API.EXAMPLE.COM", trusted) is True
|
||||
|
||||
def test_partial_match_no(self):
|
||||
"""域名部分相同但不是子域名不匹配."""
|
||||
trusted = {"example.com"}
|
||||
# fakeexample.com 不是 example.com 的子域名
|
||||
assert is_trusted_domain("fakeexample.com", trusted) is False
|
||||
|
||||
def test_none_trusted_domains(self):
|
||||
"""trusted_domains 为 None 时由调用方处理,空 set 全允许."""
|
||||
# 传空集合时全允许
|
||||
assert is_trusted_domain("a.com", set()) is True
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# check_ssrf_ip
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestCheckSsrIp:
|
||||
"""IP SSRF 检查测试."""
|
||||
|
||||
def test_public_ip_passes(self):
|
||||
"""公网 IP 通过."""
|
||||
check_ssrf_ip("8.8.8.8")
|
||||
check_ssrf_ip("1.1.1.1")
|
||||
check_ssrf_ip("114.114.114.114")
|
||||
|
||||
def test_loopback_blocked(self):
|
||||
"""回环地址被拦截."""
|
||||
with pytest.raises(UrlSecurityError, match="回环"):
|
||||
check_ssrf_ip("127.0.0.1")
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_ssrf_ip("127.0.0.53")
|
||||
|
||||
def test_private_ip_blocked(self):
|
||||
"""私有内网 IP 被拦截."""
|
||||
with pytest.raises(UrlSecurityError, match="内网"):
|
||||
check_ssrf_ip("192.168.1.1")
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_ssrf_ip("10.0.0.1")
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_ssrf_ip("172.16.0.1")
|
||||
|
||||
def test_link_local_blocked(self):
|
||||
"""链路本地地址被拦截."""
|
||||
with pytest.raises(UrlSecurityError, match="链路本地"):
|
||||
check_ssrf_ip("169.254.169.254")
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_ssrf_ip("169.254.1.1")
|
||||
|
||||
def test_multicast_blocked(self):
|
||||
"""组播地址被拦截."""
|
||||
with pytest.raises(UrlSecurityError, match="组播"):
|
||||
check_ssrf_ip("224.0.0.1")
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_ssrf_ip("239.255.255.250")
|
||||
|
||||
def test_unspecified_blocked(self):
|
||||
"""未指定地址被拦截."""
|
||||
with pytest.raises(UrlSecurityError, match="未指定"):
|
||||
check_ssrf_ip("0.0.0.0")
|
||||
|
||||
def test_ipv6_loopback_blocked(self):
|
||||
"""IPv6 回环地址被拦截."""
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_ssrf_ip("::1")
|
||||
|
||||
def test_ipv6_private_blocked(self):
|
||||
"""IPv6 内网地址被拦截."""
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_ssrf_ip("fc00::1")
|
||||
with pytest.raises(UrlSecurityError):
|
||||
check_ssrf_ip("fe80::1")
|
||||
|
||||
def test_ipv6_public_passes(self):
|
||||
"""IPv6 公网地址通过."""
|
||||
check_ssrf_ip("2001:4860:4860::8888")
|
||||
|
||||
def test_invalid_ip_raises_value_error(self):
|
||||
"""非法 IP 抛出 ValueError(不是 UrlSecurityError)."""
|
||||
with pytest.raises(ValueError):
|
||||
check_ssrf_ip("not-an-ip")
|
||||
with pytest.raises(ValueError):
|
||||
check_ssrf_ip("999.999.999.999")
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# is_ip_address
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestIsIpAddress:
|
||||
"""IP 地址判断测试."""
|
||||
|
||||
def test_ipv4_true(self):
|
||||
"""IPv4 地址返回 True."""
|
||||
assert is_ip_address("127.0.0.1") is True
|
||||
assert is_ip_address("8.8.8.8") is True
|
||||
assert is_ip_address("0.0.0.0") is True
|
||||
|
||||
def test_ipv6_true(self):
|
||||
"""IPv6 地址返回 True."""
|
||||
assert is_ip_address("::1") is True
|
||||
assert is_ip_address("2001:db8::1") is True
|
||||
|
||||
def test_hostname_false(self):
|
||||
"""主机名返回 False."""
|
||||
assert is_ip_address("example.com") is False
|
||||
assert is_ip_address("localhost") is False
|
||||
assert is_ip_address("sub.domain.org") is False
|
||||
|
||||
def test_empty_string_false(self):
|
||||
"""空字符串返回 False."""
|
||||
assert is_ip_address("") is False
|
||||
|
||||
def test_invalid_ip_false(self):
|
||||
"""非法 IP 返回 False."""
|
||||
assert is_ip_address("999.999.999.999") is False
|
||||
assert is_ip_address("1234") is False
|
||||
assert is_ip_address("abc.def") is False
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# validate_url_basic
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestValidateUrlBasic:
|
||||
"""URL 基础校验测试."""
|
||||
|
||||
def test_normal_https_url_passes(self):
|
||||
"""正常 HTTPS URL 通过."""
|
||||
result = validate_url_basic("https://example.com/path")
|
||||
assert result == "https://example.com/path"
|
||||
|
||||
def test_normal_http_url_passes(self):
|
||||
"""正常 HTTP URL 通过."""
|
||||
result = validate_url_basic("http://example.com/path")
|
||||
assert result == "http://example.com/path"
|
||||
|
||||
def test_empty_url_rejected(self):
|
||||
"""空 URL 被拒."""
|
||||
with pytest.raises(UrlSecurityError, match="为空"):
|
||||
validate_url_basic("")
|
||||
|
||||
def test_none_url_not_passed_as_str(self):
|
||||
"""None 作为 URL(这里只测空字符串)."""
|
||||
# 空字符串被拒
|
||||
with pytest.raises(UrlSecurityError):
|
||||
validate_url_basic("")
|
||||
|
||||
def test_too_long_url_rejected(self):
|
||||
"""超长 URL 被拒."""
|
||||
long_url = "https://example.com/" + "a" * 3000
|
||||
with pytest.raises(UrlSecurityError, match="过长"):
|
||||
validate_url_basic(long_url)
|
||||
|
||||
def test_invalid_scheme_rejected(self):
|
||||
"""非法 scheme 被拒."""
|
||||
with pytest.raises(UrlSecurityError, match="scheme"):
|
||||
validate_url_basic("ftp://example.com/file")
|
||||
with pytest.raises(UrlSecurityError):
|
||||
validate_url_basic("file:///etc/passwd")
|
||||
with pytest.raises(UrlSecurityError):
|
||||
validate_url_basic("javascript:alert(1)")
|
||||
|
||||
def test_missing_hostname_rejected(self):
|
||||
"""缺少主机名被拒."""
|
||||
with pytest.raises(UrlSecurityError, match="主机名"):
|
||||
validate_url_basic("https:///path")
|
||||
|
||||
def test_localhost_rejected(self):
|
||||
"""localhost 被拒."""
|
||||
with pytest.raises(UrlSecurityError):
|
||||
validate_url_basic("https://localhost/api")
|
||||
|
||||
def test_internal_domain_rejected(self):
|
||||
"""内网域名被拒."""
|
||||
with pytest.raises(UrlSecurityError):
|
||||
validate_url_basic("http://server.local/api")
|
||||
|
||||
def test_non_standard_port_rejected(self):
|
||||
"""非标准端口被拒."""
|
||||
with pytest.raises(UrlSecurityError, match="端口"):
|
||||
validate_url_basic("https://example.com:8080/")
|
||||
with pytest.raises(UrlSecurityError):
|
||||
validate_url_basic("http://example.com:3000/")
|
||||
|
||||
def test_port_80_ok(self):
|
||||
"""80 端口允许."""
|
||||
validate_url_basic("http://example.com:80/path")
|
||||
|
||||
def test_port_443_ok(self):
|
||||
"""443 端口允许."""
|
||||
validate_url_basic("https://example.com:443/path")
|
||||
|
||||
def test_no_port_ok(self):
|
||||
"""无端口默认允许."""
|
||||
validate_url_basic("https://example.com/path")
|
||||
|
||||
def test_direct_ip_rejected_by_default(self):
|
||||
"""默认禁止直接 IP 访问."""
|
||||
with pytest.raises(UrlSecurityError, match="直接 IP"):
|
||||
validate_url_basic("https://8.8.8.8/path")
|
||||
|
||||
def test_direct_ip_allowed_when_enabled(self):
|
||||
"""allow_direct_ip=True 时允许公网 IP."""
|
||||
validate_url_basic("https://8.8.8.8/path", allow_direct_ip=True)
|
||||
|
||||
def test_direct_ip_private_still_blocked(self):
|
||||
"""即使 allow_direct_ip,内网 IP 仍被拒."""
|
||||
with pytest.raises(UrlSecurityError, match="内网"):
|
||||
validate_url_basic("https://192.168.1.1/", allow_direct_ip=True)
|
||||
|
||||
def test_direct_ip_loopback_still_blocked(self):
|
||||
"""回环 IP 即使开启 direct_ip 也被拒."""
|
||||
with pytest.raises(UrlSecurityError):
|
||||
validate_url_basic("https://127.0.0.1/", allow_direct_ip=True)
|
||||
|
||||
def test_trusted_domains_pass(self):
|
||||
"""可信域名列表内的域名通过."""
|
||||
trusted = {"example.com", "cdn.com"}
|
||||
validate_url_basic("https://api.example.com/path", trusted_domains=trusted)
|
||||
validate_url_basic("https://cdn.com/asset.jpg", trusted_domains=trusted)
|
||||
|
||||
def test_untrusted_domain_rejected(self):
|
||||
"""不在可信域名列表中的域名被拒."""
|
||||
trusted = {"example.com"}
|
||||
with pytest.raises(UrlSecurityError, match="白名单"):
|
||||
validate_url_basic("https://evil.com/malware", trusted_domains=trusted)
|
||||
|
||||
def test_trusted_domain_subdomain_pass(self):
|
||||
"""可信域名的子域名通过."""
|
||||
trusted = {"example.com"}
|
||||
validate_url_basic("https://sub.example.com/a", trusted_domains=trusted)
|
||||
validate_url_basic("https://a.b.example.com/b", trusted_domains=trusted)
|
||||
|
||||
def test_return_value_is_original_url(self):
|
||||
"""返回原始 URL 字符串."""
|
||||
url = "https://example.com/path?query=value#frag"
|
||||
assert validate_url_basic(url) == url
|
||||
|
||||
def test_metadata_ip_rejected(self):
|
||||
"""云元数据 IP 被内部主机名检查拦截."""
|
||||
with pytest.raises(UrlSecurityError):
|
||||
validate_url_basic("http://169.254.169.254/latest/meta-data/")
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# is_url_basic_safe
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestIsUrlBasicSafe:
|
||||
"""便捷函数 is_url_basic_safe 测试."""
|
||||
|
||||
def test_safe_url_returns_true(self):
|
||||
"""安全 URL 返回 True."""
|
||||
assert is_url_basic_safe("https://example.com/") is True
|
||||
assert is_url_basic_safe("http://example.org/path") is True
|
||||
|
||||
def test_unsafe_url_returns_false(self):
|
||||
"""不安全 URL 返回 False."""
|
||||
assert is_url_basic_safe("https://localhost/") is False
|
||||
assert is_url_basic_safe("ftp://example.com/") is False
|
||||
assert is_url_basic_safe("") is False
|
||||
|
||||
def test_trusted_domains_param(self):
|
||||
"""支持 trusted_domains 参数."""
|
||||
trusted = {"example.com"}
|
||||
assert is_url_basic_safe("https://other.com/", trusted_domains=trusted) is False
|
||||
assert is_url_basic_safe("https://example.com/", trusted_domains=trusted) is True
|
||||
|
||||
def test_allow_direct_ip_param(self):
|
||||
"""支持 allow_direct_ip 参数."""
|
||||
assert is_url_basic_safe("https://8.8.8.8/") is False
|
||||
assert is_url_basic_safe("https://8.8.8.8/", allow_direct_ip=True) is True
|
||||
|
||||
def test_no_exceptions_raised(self):
|
||||
"""不抛出异常,只返回 bool."""
|
||||
# 各种边界情况都不抛异常
|
||||
try:
|
||||
is_url_basic_safe("")
|
||||
is_url_basic_safe("not a url")
|
||||
is_url_basic_safe("http://" + "a" * 3000)
|
||||
except UrlSecurityError:
|
||||
pytest.fail("is_url_basic_safe should not raise UrlSecurityError")
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# validate_magic_number
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestValidateMagicNumber:
|
||||
"""魔数校验测试."""
|
||||
|
||||
def test_jpeg_valid(self):
|
||||
"""JPEG 文件通过."""
|
||||
# JPEG 文件头: FF D8 FF
|
||||
jpeg_header = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00"
|
||||
validate_magic_number(jpeg_header, {"image/jpeg"})
|
||||
|
||||
def test_png_valid(self):
|
||||
"""PNG 文件通过."""
|
||||
png_header = b"\x89PNG\r\n\x1a\n\x00\x00\x00"
|
||||
validate_magic_number(png_header, {"image/png"})
|
||||
|
||||
def test_gif_valid(self):
|
||||
"""GIF 文件通过(GIF89a 和 GIF87a)."""
|
||||
validate_magic_number(b"GIF89a...", {"image/gif"})
|
||||
validate_magic_number(b"GIF87a...", {"image/gif"})
|
||||
|
||||
def test_wav_valid(self):
|
||||
"""WAV 文件通过(RIFF + WAVE)."""
|
||||
wav_header = b"RIFF\x00\x00\x00\x00WAVEfmt "
|
||||
validate_magic_number(wav_header, {"audio/wav"})
|
||||
|
||||
def test_mp3_id3_valid(self):
|
||||
"""带 ID3 标签的 MP3 通过."""
|
||||
mp3_header = b"ID3\x03\x00\x00\x00\x00\x0f\x76"
|
||||
validate_magic_number(mp3_header, {"audio/mpeg"})
|
||||
|
||||
def test_mp3_sync_valid(self):
|
||||
"""不带 ID3 的 MP3(帧同步字)通过."""
|
||||
mp3_header = b"\xff\xfb\x90\x00" + b"\x00" * 32
|
||||
validate_magic_number(mp3_header, {"audio/mpeg"})
|
||||
|
||||
def test_ogg_valid(self):
|
||||
"""OGG 文件通过."""
|
||||
validate_magic_number(b"OggS\x00\x00...", {"audio/ogg"})
|
||||
|
||||
def test_flac_valid(self):
|
||||
"""FLAC 文件通过."""
|
||||
validate_magic_number(b"fLaC\x00\x00...", {"audio/flac"})
|
||||
|
||||
def test_webp_valid(self):
|
||||
"""WebP 文件通过(RIFF + WEBP)."""
|
||||
webp_header = b"RIFF\x00\x00\x00\x00WEBPVP8 "
|
||||
validate_magic_number(webp_header, {"image/webp"})
|
||||
|
||||
def test_bmp_valid(self):
|
||||
"""BMP 文件通过."""
|
||||
validate_magic_number(b"BM\x00\x00\x00\x00...", {"image/bmp"})
|
||||
|
||||
def test_mp4_valid(self):
|
||||
"""MP4 文件通过(ftyp 在偏移 4)."""
|
||||
mp4_header = b"\x00\x00\x00\x20ftypisom\x00\x00\x02\x00"
|
||||
validate_magic_number(mp4_header, {"video/mp4"})
|
||||
|
||||
def test_invalid_format_rejected(self):
|
||||
"""不匹配的格式被拒."""
|
||||
with pytest.raises(UrlSecurityError, match="魔数"):
|
||||
validate_magic_number(b"hello world", {"image/jpeg"})
|
||||
|
||||
def test_empty_bytes_rejected(self):
|
||||
"""空字节被拒."""
|
||||
with pytest.raises(UrlSecurityError, match="为空"):
|
||||
validate_magic_number(b"", {"image/jpeg"})
|
||||
|
||||
def test_too_short_bytes_rejected(self):
|
||||
"""字节太短不匹配魔数时被拒."""
|
||||
with pytest.raises(UrlSecurityError):
|
||||
validate_magic_number(b"\xff\xd8", {"image/jpeg"}) # 只2字节,不够JPEG魔数
|
||||
|
||||
def test_multiple_allowed_types(self):
|
||||
"""允许多种格式时任一匹配即通过."""
|
||||
jpeg_header = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00"
|
||||
validate_magic_number(jpeg_header, {"image/jpeg", "image/png", "image/gif"})
|
||||
|
||||
def test_wrong_type_rejected(self):
|
||||
"""用 PNG 魔数校验 JPEG 类型失败."""
|
||||
jpeg_header = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00"
|
||||
with pytest.raises(UrlSecurityError):
|
||||
validate_magic_number(jpeg_header, {"image/png"})
|
||||
|
||||
def test_unknown_mime_skipped(self):
|
||||
"""未知 MIME 类型(无对应魔数)不阻断."""
|
||||
# application/octet-stream 没有魔数定义,直接通过
|
||||
validate_magic_number(b"random bytes here", {"application/octet-stream"})
|
||||
|
||||
def test_allowed_image_mime_types_has_common(self):
|
||||
"""图片 MIME 白名单包含常见类型."""
|
||||
assert "image/jpeg" in ALLOWED_IMAGE_MIME_TYPES
|
||||
assert "image/png" in ALLOWED_IMAGE_MIME_TYPES
|
||||
|
||||
def test_allowed_video_mime_types_has_common(self):
|
||||
"""视频 MIME 白名单包含常见类型."""
|
||||
assert "video/mp4" in ALLOWED_VIDEO_MIME_TYPES
|
||||
@@ -5,7 +5,6 @@ domain 层纯逻辑模块,0 FFmpeg 依赖,快速轻量。
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
@@ -132,7 +131,7 @@ class TestClipFilterChain:
|
||||
def test_frozen(self):
|
||||
"""frozen dataclass 不可修改."""
|
||||
chain = _make_chain()
|
||||
with pytest.raises(dataclasses.FrozenInstanceError):
|
||||
with pytest.raises(Exception):
|
||||
chain.duration = 10.0 # type: ignore
|
||||
|
||||
|
||||
|
||||
@@ -1,602 +0,0 @@
|
||||
"""watermark_config 单元测试 - wave163
|
||||
|
||||
覆盖:
|
||||
- WatermarkConfig 数据类 / from_dict / validate / has_effect
|
||||
- calc_position 9宫格位置计算
|
||||
- calc_scroll_x 滚动计算
|
||||
- build_image_watermark_filter 图片水印滤镜
|
||||
- build_text_watermark_filter 文字水印滤镜
|
||||
- get_position_names / get_position_display_name
|
||||
- 常量验证
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from packages.domain.watermark_config import (
|
||||
DEFAULT_FONT_COLOR,
|
||||
DEFAULT_FONT_SIZE,
|
||||
DEFAULT_MARGIN_X,
|
||||
DEFAULT_MARGIN_Y,
|
||||
DEFAULT_MODE,
|
||||
DEFAULT_OPACITY,
|
||||
DEFAULT_POSITION,
|
||||
DEFAULT_SCALE,
|
||||
DEFAULT_SCROLL_SPEED,
|
||||
VALID_POSITIONS,
|
||||
WATERMARK_POSITIONS,
|
||||
WatermarkConfig,
|
||||
build_image_watermark_filter,
|
||||
build_text_watermark_filter,
|
||||
calc_position,
|
||||
calc_scroll_x,
|
||||
get_position_display_name,
|
||||
get_position_names,
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 常量
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestConstants:
|
||||
def test_nine_positions(self):
|
||||
assert len(VALID_POSITIONS) == 9
|
||||
|
||||
def test_watermark_positions_contains_all(self):
|
||||
assert set(WATERMARK_POSITIONS.keys()) == VALID_POSITIONS
|
||||
|
||||
def test_default_position_valid(self):
|
||||
assert DEFAULT_POSITION in VALID_POSITIONS
|
||||
|
||||
def test_default_values(self):
|
||||
assert DEFAULT_MODE in ("text", "image")
|
||||
assert 0 < DEFAULT_SCALE <= 1.0
|
||||
assert 0 <= DEFAULT_OPACITY <= 1.0
|
||||
assert DEFAULT_FONT_SIZE > 0
|
||||
assert DEFAULT_MARGIN_X >= 0
|
||||
assert DEFAULT_MARGIN_Y >= 0
|
||||
assert DEFAULT_SCROLL_SPEED > 0
|
||||
assert isinstance(DEFAULT_FONT_COLOR, str)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# WatermarkConfig 默认值
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestWatermarkConfigDefaults:
|
||||
def test_default_constructor(self):
|
||||
config = WatermarkConfig()
|
||||
assert config.mode == DEFAULT_MODE
|
||||
assert config.position == DEFAULT_POSITION
|
||||
assert config.scale == DEFAULT_SCALE
|
||||
assert config.opacity == DEFAULT_OPACITY
|
||||
assert config.font_size == DEFAULT_FONT_SIZE
|
||||
assert config.font_color == DEFAULT_FONT_COLOR
|
||||
assert config.margin_x == DEFAULT_MARGIN_X
|
||||
assert config.margin_y == DEFAULT_MARGIN_Y
|
||||
assert config.scroll is False
|
||||
assert config.scroll_speed == DEFAULT_SCROLL_SPEED
|
||||
|
||||
def test_text_mode_default(self):
|
||||
config = WatermarkConfig()
|
||||
assert config.mode == "text"
|
||||
assert config.text == ""
|
||||
assert config.font_path == ""
|
||||
|
||||
def test_image_mode_default(self):
|
||||
config = WatermarkConfig(mode="image")
|
||||
assert config.image_path == ""
|
||||
|
||||
|
||||
# ============================================================
|
||||
# from_dict
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestFromDict:
|
||||
def test_none_returns_none(self):
|
||||
assert WatermarkConfig.from_dict(None) is None
|
||||
|
||||
def test_empty_dict_returns_none(self):
|
||||
assert WatermarkConfig.from_dict({}) is None
|
||||
|
||||
def test_disabled_returns_none(self):
|
||||
assert WatermarkConfig.from_dict({"enabled": False}) is None
|
||||
|
||||
def test_image_mode_missing_path_returns_none(self):
|
||||
result = WatermarkConfig.from_dict({"enabled": True, "mode": "image"})
|
||||
assert result is None
|
||||
|
||||
def test_image_mode_with_image_field(self):
|
||||
result = WatermarkConfig.from_dict({"enabled": True, "mode": "image", "image": "/path/to/wm.png"})
|
||||
assert result is not None
|
||||
assert result.mode == "image"
|
||||
assert result.image_path == "/path/to/wm.png"
|
||||
|
||||
def test_image_mode_with_image_path_field(self):
|
||||
result = WatermarkConfig.from_dict({"enabled": True, "mode": "image", "image_path": "/path/wm.png"})
|
||||
assert result is not None
|
||||
assert result.image_path == "/path/wm.png"
|
||||
|
||||
def test_text_mode_missing_text_returns_none(self):
|
||||
result = WatermarkConfig.from_dict({"enabled": True, "mode": "text"})
|
||||
assert result is None
|
||||
|
||||
def test_text_mode_with_text(self):
|
||||
result = WatermarkConfig.from_dict({"enabled": True, "mode": "text", "text": "hello"})
|
||||
assert result is not None
|
||||
assert result.mode == "text"
|
||||
assert result.text == "hello"
|
||||
|
||||
def test_invalid_position_falls_back_default(self):
|
||||
result = WatermarkConfig.from_dict(
|
||||
{
|
||||
"enabled": True,
|
||||
"mode": "text",
|
||||
"text": "hello",
|
||||
"position": "invalid_pos",
|
||||
}
|
||||
)
|
||||
assert result is not None
|
||||
assert result.position == DEFAULT_POSITION
|
||||
|
||||
def test_custom_values_propagated(self):
|
||||
result = WatermarkConfig.from_dict(
|
||||
{
|
||||
"enabled": True,
|
||||
"mode": "text",
|
||||
"text": "测试",
|
||||
"position": "top_left",
|
||||
"opacity": 0.5,
|
||||
"font_size": 32,
|
||||
"font_color": "red",
|
||||
"margin_x": 30,
|
||||
"margin_y": 40,
|
||||
"scroll": True,
|
||||
"scroll_speed": 100,
|
||||
}
|
||||
)
|
||||
assert result is not None
|
||||
assert result.position == "top_left"
|
||||
assert result.opacity == 0.5
|
||||
assert result.font_size == 32
|
||||
assert result.font_color == "red"
|
||||
assert result.margin_x == 30
|
||||
assert result.margin_y == 40
|
||||
assert result.scroll is True
|
||||
assert result.scroll_speed == 100
|
||||
|
||||
def test_image_mode_custom_values(self):
|
||||
result = WatermarkConfig.from_dict(
|
||||
{
|
||||
"enabled": True,
|
||||
"mode": "image",
|
||||
"image_path": "/wm.png",
|
||||
"scale": 0.3,
|
||||
"opacity": 0.6,
|
||||
}
|
||||
)
|
||||
assert result is not None
|
||||
assert result.scale == 0.3
|
||||
assert result.opacity == 0.6
|
||||
|
||||
def test_default_mode_when_unspecified(self):
|
||||
# 只给enabled和text,mode默认text
|
||||
result = WatermarkConfig.from_dict({"enabled": True, "text": "hello"})
|
||||
assert result is not None
|
||||
assert result.mode == DEFAULT_MODE
|
||||
|
||||
def test_text_empty_string_returns_none(self):
|
||||
result = WatermarkConfig.from_dict({"enabled": True, "mode": "text", "text": ""})
|
||||
assert result is None
|
||||
|
||||
|
||||
# ============================================================
|
||||
# validate
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestValidate:
|
||||
def test_valid_text_config(self):
|
||||
config = WatermarkConfig(mode="text", text="hello")
|
||||
ok, msg = config.validate()
|
||||
assert ok is True
|
||||
assert msg == ""
|
||||
|
||||
def test_valid_image_config(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png")
|
||||
ok, msg = config.validate()
|
||||
assert ok is True
|
||||
assert msg == ""
|
||||
|
||||
def test_invalid_position(self):
|
||||
config = WatermarkConfig(mode="text", text="hi", position="nowhere")
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "位置" in msg
|
||||
|
||||
def test_opacity_too_high(self):
|
||||
config = WatermarkConfig(mode="text", text="hi", opacity=1.5)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "透明度" in msg
|
||||
|
||||
def test_opacity_negative(self):
|
||||
config = WatermarkConfig(mode="text", text="hi", opacity=-0.1)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "透明度" in msg
|
||||
|
||||
def test_opacity_boundary_zero(self):
|
||||
config = WatermarkConfig(mode="text", text="hi", opacity=0.0)
|
||||
ok, _ = config.validate()
|
||||
assert ok is True
|
||||
|
||||
def test_opacity_boundary_one(self):
|
||||
config = WatermarkConfig(mode="text", text="hi", opacity=1.0)
|
||||
ok, _ = config.validate()
|
||||
assert ok is True
|
||||
|
||||
def test_image_missing_path(self):
|
||||
config = WatermarkConfig(mode="image")
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "图片路径" in msg
|
||||
|
||||
def test_image_scale_too_small(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png", scale=0.001)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "缩放" in msg
|
||||
|
||||
def test_image_scale_too_large(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png", scale=1.5)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "缩放" in msg
|
||||
|
||||
def test_image_scale_boundary_low(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png", scale=0.01)
|
||||
ok, _ = config.validate()
|
||||
assert ok is True
|
||||
|
||||
def test_image_scale_boundary_high(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png", scale=1.0)
|
||||
ok, _ = config.validate()
|
||||
assert ok is True
|
||||
|
||||
def test_text_empty(self):
|
||||
config = WatermarkConfig(mode="text", text="")
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "文字内容" in msg
|
||||
|
||||
def test_font_size_zero(self):
|
||||
config = WatermarkConfig(mode="text", text="hi", font_size=0)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "字体大小" in msg
|
||||
|
||||
def test_font_size_negative(self):
|
||||
config = WatermarkConfig(mode="text", text="hi", font_size=-5)
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "字体大小" in msg
|
||||
|
||||
def test_unknown_mode(self):
|
||||
config = WatermarkConfig(mode="video", text="hi")
|
||||
ok, msg = config.validate()
|
||||
assert ok is False
|
||||
assert "模式" in msg
|
||||
|
||||
|
||||
# ============================================================
|
||||
# has_effect
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestHasEffect:
|
||||
def test_text_has_effect(self):
|
||||
config = WatermarkConfig(mode="text", text="hello", opacity=0.5, font_size=24)
|
||||
assert config.has_effect() is True
|
||||
|
||||
def test_text_empty_no_effect(self):
|
||||
config = WatermarkConfig(mode="text", text="")
|
||||
assert config.has_effect() is False
|
||||
|
||||
def test_text_zero_opacity_no_effect(self):
|
||||
config = WatermarkConfig(mode="text", text="hi", opacity=0.0)
|
||||
assert config.has_effect() is False
|
||||
|
||||
def test_text_zero_font_size_no_effect(self):
|
||||
config = WatermarkConfig(mode="text", text="hi", font_size=0)
|
||||
assert config.has_effect() is False
|
||||
|
||||
def test_image_has_effect(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png", opacity=0.5)
|
||||
assert config.has_effect() is True
|
||||
|
||||
def test_image_no_path_no_effect(self):
|
||||
config = WatermarkConfig(mode="image")
|
||||
assert config.has_effect() is False
|
||||
|
||||
def test_image_zero_opacity_no_effect(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png", opacity=0.0)
|
||||
assert config.has_effect() is False
|
||||
|
||||
def test_unknown_mode_no_effect(self):
|
||||
config = WatermarkConfig(mode="invalid")
|
||||
assert config.has_effect() is False
|
||||
|
||||
|
||||
# ============================================================
|
||||
# calc_position
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestCalcPosition:
|
||||
OUT_W = 1920
|
||||
OUT_H = 1080
|
||||
WM_W = 200
|
||||
WM_H = 50
|
||||
MX = 20
|
||||
MY = 20
|
||||
|
||||
def test_top_left(self):
|
||||
x, y = calc_position("top_left", self.OUT_W, self.OUT_H, self.WM_W, self.WM_H, self.MX, self.MY)
|
||||
assert x == 20
|
||||
assert y == 20
|
||||
|
||||
def test_top_center(self):
|
||||
x, y = calc_position("top_center", self.OUT_W, self.OUT_H, self.WM_W, self.WM_H, self.MX, self.MY)
|
||||
assert x == (1920 - 200) // 2
|
||||
assert y == 20
|
||||
|
||||
def test_top_right(self):
|
||||
x, y = calc_position("top_right", self.OUT_W, self.OUT_H, self.WM_W, self.WM_H, self.MX, self.MY)
|
||||
assert x == 1920 - 200 - 20
|
||||
assert y == 20
|
||||
|
||||
def test_center_left(self):
|
||||
x, y = calc_position("center_left", self.OUT_W, self.OUT_H, self.WM_W, self.WM_H, self.MX, self.MY)
|
||||
assert x == 20
|
||||
assert y == (1080 - 50) // 2
|
||||
|
||||
def test_center(self):
|
||||
x, y = calc_position("center", self.OUT_W, self.OUT_H, self.WM_W, self.WM_H, self.MX, self.MY)
|
||||
assert x == (1920 - 200) // 2
|
||||
assert y == (1080 - 50) // 2
|
||||
|
||||
def test_center_right(self):
|
||||
x, y = calc_position("center_right", self.OUT_W, self.OUT_H, self.WM_W, self.WM_H, self.MX, self.MY)
|
||||
assert x == 1920 - 200 - 20
|
||||
assert y == (1080 - 50) // 2
|
||||
|
||||
def test_bottom_left(self):
|
||||
x, y = calc_position("bottom_left", self.OUT_W, self.OUT_H, self.WM_W, self.WM_H, self.MX, self.MY)
|
||||
assert x == 20
|
||||
assert y == 1080 - 50 - 20
|
||||
|
||||
def test_bottom_center(self):
|
||||
x, y = calc_position("bottom_center", self.OUT_W, self.OUT_H, self.WM_W, self.WM_H, self.MX, self.MY)
|
||||
assert x == (1920 - 200) // 2
|
||||
assert y == 1080 - 50 - 20
|
||||
|
||||
def test_bottom_right(self):
|
||||
x, y = calc_position("bottom_right", self.OUT_W, self.OUT_H, self.WM_W, self.WM_H, self.MX, self.MY)
|
||||
assert x == 1920 - 200 - 20
|
||||
assert y == 1080 - 50 - 20
|
||||
|
||||
def test_invalid_position_defaults_bottom_right(self):
|
||||
x, y = calc_position("invalid", self.OUT_W, self.OUT_H, self.WM_W, self.WM_H, self.MX, self.MY)
|
||||
assert x == 1920 - 200 - 20
|
||||
assert y == 1080 - 50 - 20
|
||||
|
||||
def test_zero_margins(self):
|
||||
x, y = calc_position("top_left", self.OUT_W, self.OUT_H, self.WM_W, self.WM_H, 0, 0)
|
||||
assert x == 0
|
||||
assert y == 0
|
||||
|
||||
def test_all_nine_positions_are_unique(self):
|
||||
positions = set()
|
||||
for pos in VALID_POSITIONS:
|
||||
pos_xy = calc_position(pos, self.OUT_W, self.OUT_H, self.WM_W, self.WM_H, self.MX, self.MY)
|
||||
positions.add(pos_xy)
|
||||
assert len(positions) == 9
|
||||
|
||||
|
||||
# ============================================================
|
||||
# calc_scroll_x
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestCalcScrollX:
|
||||
def test_returns_string(self):
|
||||
result = calc_scroll_x("bottom_left", 1920, 200, 50)
|
||||
assert isinstance(result, str)
|
||||
|
||||
def test_contains_output_width(self):
|
||||
result = calc_scroll_x("bottom_left", 1920, 200, 50)
|
||||
assert "1920" in result
|
||||
|
||||
def test_contains_wm_width(self):
|
||||
result = calc_scroll_x("bottom_left", 1920, 200, 50)
|
||||
assert "200" in result
|
||||
|
||||
def test_contains_speed(self):
|
||||
result = calc_scroll_x("bottom_left", 1920, 200, 50)
|
||||
assert "50" in result
|
||||
|
||||
def test_contains_mod_keyword(self):
|
||||
result = calc_scroll_x("bottom_left", 1920, 200, 50)
|
||||
assert "mod" in result
|
||||
|
||||
|
||||
# ============================================================
|
||||
# build_image_watermark_filter
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestBuildImageWatermarkFilter:
|
||||
def test_returns_tuple(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png", position="top_left")
|
||||
result = build_image_watermark_filter("[in]", "/wm.png", 1920, 1080, "[out]", config)
|
||||
assert isinstance(result, tuple)
|
||||
assert len(result) == 2
|
||||
|
||||
def test_filter_contains_overlay(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png", position="bottom_right")
|
||||
filter_str, _ = build_image_watermark_filter("[in]", "/wm.png", 1920, 1080, "[out]", config)
|
||||
assert "overlay" in filter_str
|
||||
|
||||
def test_filter_contains_scale(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png", scale=0.3)
|
||||
filter_str, _ = build_image_watermark_filter("[in]", "/wm.png", 1920, 1080, "[out]", config)
|
||||
assert "scale=" in filter_str
|
||||
|
||||
def test_opacity_applied_when_below_one(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png", opacity=0.5)
|
||||
filter_str, _ = build_image_watermark_filter("[in]", "/wm.png", 1920, 1080, "[out]", config)
|
||||
assert "colorchannelmixer" in filter_str
|
||||
assert "aa=0.5" in filter_str
|
||||
|
||||
def test_opacity_one_no_mixer(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png", opacity=1.0)
|
||||
filter_str, _ = build_image_watermark_filter("[in]", "/wm.png", 1920, 1080, "[out]", config)
|
||||
assert "colorchannelmixer" not in filter_str
|
||||
|
||||
def test_input_args_contain_image_path(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/path/to/wm.png")
|
||||
_, input_args = build_image_watermark_filter("[in]", "/path/to/wm.png", 1920, 1080, "[out]", config)
|
||||
assert input_args == ["-i", "/path/to/wm.png"]
|
||||
|
||||
def test_output_label_present(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png")
|
||||
filter_str, _ = build_image_watermark_filter("[in]", "/wm.png", 1920, 1080, "[final]", config)
|
||||
assert "[final]" in filter_str
|
||||
|
||||
def test_scroll_mode_contains_t_variable(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png", scroll=True, scroll_speed=60)
|
||||
filter_str, _ = build_image_watermark_filter("[in]", "/wm.png", 1920, 1080, "[out]", config)
|
||||
assert "*t" in filter_str or "t*" in filter_str
|
||||
|
||||
def test_static_mode_no_t_variable_in_overlay_x(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png", scroll=False)
|
||||
filter_str, _ = build_image_watermark_filter("[in]", "/wm.png", 1920, 1080, "[out]", config)
|
||||
# 静态模式 x 是具体数值,不包含 t 变量
|
||||
overlay_part = filter_str.split("overlay=")[1]
|
||||
assert "t" not in overlay_part.split(":")[0] or "wm" in overlay_part.split(":")[0]
|
||||
|
||||
def test_semicolon_separated_filters(self):
|
||||
config = WatermarkConfig(mode="image", image_path="/wm.png")
|
||||
filter_str, _ = build_image_watermark_filter("[in]", "/wm.png", 1920, 1080, "[out]", config)
|
||||
assert ";" in filter_str
|
||||
|
||||
|
||||
# ============================================================
|
||||
# build_text_watermark_filter
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestBuildTextWatermarkFilter:
|
||||
def test_returns_string(self):
|
||||
config = WatermarkConfig(mode="text", text="hello")
|
||||
result = build_text_watermark_filter("[in]", "[out]", config, 1920, 1080)
|
||||
assert isinstance(result, str)
|
||||
|
||||
def test_contains_drawtext(self):
|
||||
config = WatermarkConfig(mode="text", text="hello")
|
||||
result = build_text_watermark_filter("[in]", "[out]", config, 1920, 1080)
|
||||
assert "drawtext=" in result
|
||||
|
||||
def test_contains_text(self):
|
||||
config = WatermarkConfig(mode="text", text="测试水印")
|
||||
result = build_text_watermark_filter("[in]", "[out]", config, 1920, 1080)
|
||||
assert "测试水印" in result
|
||||
|
||||
def test_fontsize_in_filter(self):
|
||||
config = WatermarkConfig(mode="text", text="hi", font_size=36)
|
||||
result = build_text_watermark_filter("[in]", "[out]", config, 1920, 1080)
|
||||
assert "fontsize=36" in result
|
||||
|
||||
def test_fontcolor_with_opacity(self):
|
||||
config = WatermarkConfig(mode="text", text="hi", font_color="red", opacity=0.5)
|
||||
result = build_text_watermark_filter("[in]", "[out]", config, 1920, 1080)
|
||||
assert "fontcolor=red@0.5" in result
|
||||
|
||||
def test_font_path_included(self):
|
||||
config = WatermarkConfig(mode="text", text="hi", font_path="/fonts/simsun.ttf")
|
||||
result = build_text_watermark_filter("[in]", "[out]", config, 1920, 1080)
|
||||
assert "fontfile=" in result
|
||||
assert "simsun.ttf" in result
|
||||
|
||||
def test_no_font_path_when_empty(self):
|
||||
config = WatermarkConfig(mode="text", text="hi", font_path="")
|
||||
result = build_text_watermark_filter("[in]", "[out]", config, 1920, 1080)
|
||||
assert "fontfile=" not in result
|
||||
|
||||
def test_text_colon_escaped(self):
|
||||
config = WatermarkConfig(mode="text", text="time: 00:00")
|
||||
result = build_text_watermark_filter("[in]", "[out]", config, 1920, 1080)
|
||||
# 冒号应该被转义
|
||||
assert "time\\:" in result or "time\\\\:" in result
|
||||
|
||||
def test_scroll_mode_contains_mod(self):
|
||||
config = WatermarkConfig(mode="text", text="scrolling", scroll=True, scroll_speed=50)
|
||||
result = build_text_watermark_filter("[in]", "[out]", config, 1920, 1080)
|
||||
assert "mod(" in result
|
||||
|
||||
def test_static_mode_has_numeric_x_y(self):
|
||||
config = WatermarkConfig(mode="text", text="hi", position="top_left")
|
||||
result = build_text_watermark_filter("[in]", "[out]", config, 1920, 1080)
|
||||
assert "x=" in result
|
||||
assert "y=" in result
|
||||
|
||||
def test_output_label_present(self):
|
||||
config = WatermarkConfig(mode="text", text="hi")
|
||||
result = build_text_watermark_filter("[in]", "[final_out]", config, 1920, 1080)
|
||||
assert "[final_out]" in result
|
||||
|
||||
def test_input_label_present(self):
|
||||
config = WatermarkConfig(mode="text", text="hi")
|
||||
result = build_text_watermark_filter("[video_in]", "[out]", config, 1920, 1080)
|
||||
assert result.startswith("[video_in]")
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 工具函数
|
||||
# ============================================================
|
||||
|
||||
|
||||
class TestGetPositionNames:
|
||||
def test_returns_nine_names(self):
|
||||
names = get_position_names()
|
||||
assert len(names) == 9
|
||||
|
||||
def test_order_is_correct(self):
|
||||
names = get_position_names()
|
||||
# 按从上到下、从左到右
|
||||
assert names[0] == "top_left"
|
||||
assert names[1] == "top_center"
|
||||
assert names[2] == "top_right"
|
||||
assert names[3] == "center_left"
|
||||
assert names[4] == "center"
|
||||
assert names[5] == "center_right"
|
||||
assert names[6] == "bottom_left"
|
||||
assert names[7] == "bottom_center"
|
||||
assert names[8] == "bottom_right"
|
||||
|
||||
def test_all_valid(self):
|
||||
names = get_position_names()
|
||||
assert set(names) == VALID_POSITIONS
|
||||
|
||||
|
||||
class TestGetPositionDisplayName:
|
||||
def test_known_position(self):
|
||||
assert get_position_display_name("top_left") == "左上"
|
||||
assert get_position_display_name("center") == "中心"
|
||||
assert get_position_display_name("bottom_right") == "右下"
|
||||
|
||||
def test_unknown_position_returns_original(self):
|
||||
assert get_position_display_name("invalid") == "invalid"
|
||||
@@ -468,8 +468,7 @@ class TestSubtitleStyle:
|
||||
assert style3.alignment == 5
|
||||
|
||||
def test_9grid_positions(self):
|
||||
from packages.domain.subtitle_style import POSITION_ALIGNMENT
|
||||
from video_processing.subtitle_render_engine import SubtitleStyle
|
||||
from video_processing.subtitle_render_engine import POSITION_ALIGNMENT, SubtitleStyle
|
||||
|
||||
for pos, align in POSITION_ALIGNMENT.items():
|
||||
style = SubtitleStyle.from_dict({"position": pos})
|
||||
|
||||
@@ -934,7 +934,7 @@ class TestSortLayersByZIndex:
|
||||
_make_layer(z_index=3, source="/tmp/c.mp4"),
|
||||
]
|
||||
sorted_layers = sort_layers_by_z_index(layers)
|
||||
assert [layer.z_index for layer in sorted_layers] == [1, 3, 5]
|
||||
assert [l.z_index for l in sorted_layers] == [1, 3, 5]
|
||||
|
||||
def test_same_z_index_stable(self):
|
||||
"""相同 z_index 保持相对顺序."""
|
||||
@@ -963,4 +963,4 @@ class TestSortLayersByZIndex:
|
||||
_make_layer(z_index=3, source="/tmp/3.mp4"),
|
||||
]
|
||||
sorted_layers = sort_layers_by_z_index(layers)
|
||||
assert [layer.z_index for layer in sorted_layers] == [-5, 0, 3]
|
||||
assert [l.z_index for l in sorted_layers] == [-5, 0, 3]
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
|
||||
import pytest
|
||||
|
||||
from packages.domain.transition_presets import (
|
||||
@@ -59,7 +57,7 @@ class TestTransitionPreset:
|
||||
def test_frozen_immutable(self):
|
||||
"""frozen dataclass 不可修改."""
|
||||
p = TransitionPreset(id="t", name="T", category="basic")
|
||||
with pytest.raises(dataclasses.FrozenInstanceError):
|
||||
with pytest.raises(Exception): # FrozenInstanceError
|
||||
p.name = "新名字"
|
||||
|
||||
def test_not_hashable_due_to_list(self):
|
||||
|
||||
Reference in New Issue
Block a user