Chat 访谈 - 新增 persona 系统(default / warm_listener / curious_guide)与 background_voice 语气层 - 回复长度由 compute_reply_plan 统一决策(brief / standard / expanded),融合信息密度启发式 - 输入净稿(input_normalize):编排层可选 rules/llm 归一用户口语后再喂模型与记忆检索 - 记忆证据注入:按用户话检索 memory evidence 并注入 prompt Memoir 回忆录 - 口述归一(oral_normalize):segment 原文保留,story 管线取派生净稿作叙事输入 - segment 入队批次门闸:累计字数 + 最长等待秒数,减少零碎提交 - fidelity_check / prompts / narrative_agent 微调 - Alembic 0005:清理跨章节 story 外键 Infra - Dockerfile 加入 ffmpeg - pyproject.toml 新增依赖并同步 uv.lock - .env.example / .env.production 补全新配置项 Tests - 新增 test_background_voice、test_chat_input_normalize、test_experience_regressions - 扩展 test_interview_prompts、test_interview_reply_length、test_story_route_oral_invariant Made-with: Cursor
166 lines
5.3 KiB
Python
166 lines
5.3 KiB
Python
"""BackgroundTaskRunner:字数门闸、超时、flush(纯函数 + 异步 mock)。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import asyncio
|
||
from unittest.mock import AsyncMock, patch
|
||
|
||
import pytest
|
||
|
||
from app.features.memoir import background_runner as br
|
||
|
||
|
||
def test_batch_ready_for_submit_min_chars_zero() -> None:
|
||
assert br._batch_ready_for_submit(
|
||
min_chars=0,
|
||
max_wait_seconds=60.0,
|
||
total_text_chars=0,
|
||
elapsed_seconds=0.0,
|
||
)
|
||
|
||
|
||
def test_batch_ready_for_submit_chars_met() -> None:
|
||
assert br._batch_ready_for_submit(
|
||
min_chars=50,
|
||
max_wait_seconds=60.0,
|
||
total_text_chars=50,
|
||
elapsed_seconds=1.0,
|
||
)
|
||
|
||
|
||
def test_batch_ready_for_submit_not_ready() -> None:
|
||
assert not br._batch_ready_for_submit(
|
||
min_chars=50,
|
||
max_wait_seconds=60.0,
|
||
total_text_chars=10,
|
||
elapsed_seconds=5.0,
|
||
)
|
||
|
||
|
||
def test_batch_ready_for_submit_max_wait_elapsed() -> None:
|
||
assert br._batch_ready_for_submit(
|
||
min_chars=50,
|
||
max_wait_seconds=60.0,
|
||
total_text_chars=10,
|
||
elapsed_seconds=60.0,
|
||
)
|
||
|
||
|
||
def test_next_retry_sleep_seconds() -> None:
|
||
assert br._next_retry_sleep_seconds(5.0, 60.0, 1.0) == 5.0
|
||
assert br._next_retry_sleep_seconds(5.0, 60.0, 58.0) == 2.0
|
||
assert br._next_retry_sleep_seconds(5.0, 60.0, 60.0) == 0.0
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_flush_pending_submits_without_gate(
|
||
monkeypatch: pytest.MonkeyPatch,
|
||
) -> None:
|
||
monkeypatch.setattr(br.settings, "memoir_segment_batch_min_chars", 9999)
|
||
monkeypatch.setattr(br.settings, "memoir_segment_batch_max_wait_seconds", 9999.0)
|
||
|
||
submitted: list[tuple[str, list[str]]] = []
|
||
|
||
async def fake_submit(uid: str, ids: list[str]) -> str:
|
||
submitted.append((uid, ids))
|
||
return "tid"
|
||
|
||
runner = br.BackgroundTaskRunner(debounce_seconds=30)
|
||
uid = "u1"
|
||
runner._batch[uid] = br._MemoirBatchState(
|
||
segment_ids=["s1", "s2"],
|
||
total_text_chars=3,
|
||
first_queued_monotonic=0.0,
|
||
)
|
||
|
||
with patch.object(runner, "_submit_task", new=AsyncMock(side_effect=fake_submit)):
|
||
await runner.flush_pending(uid)
|
||
|
||
assert submitted == [("u1", ["s1", "s2"])]
|
||
assert uid not in runner._batch
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_queue_message_min_chars_zero_submits_after_debounce(
|
||
monkeypatch: pytest.MonkeyPatch,
|
||
) -> None:
|
||
monkeypatch.setattr(br.settings, "memoir_segment_batch_min_chars", 0)
|
||
monkeypatch.setattr(br.settings, "memoir_segment_batch_max_wait_seconds", 60.0)
|
||
|
||
submitted: list[tuple[str, list[str]]] = []
|
||
|
||
async def fake_submit(uid: str, ids: list[str]) -> str:
|
||
submitted.append((uid, ids))
|
||
return "tid"
|
||
|
||
runner = br.BackgroundTaskRunner(debounce_seconds=0)
|
||
with patch.object(runner, "_submit_task", new=AsyncMock(side_effect=fake_submit)):
|
||
await runner.queue_message("u1", "seg-a", text_char_count=0)
|
||
await asyncio.sleep(0.05)
|
||
|
||
assert submitted and submitted[0][1] == ["seg-a"]
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_queue_message_not_ready_then_max_wait_submits(
|
||
monkeypatch: pytest.MonkeyPatch,
|
||
) -> None:
|
||
monkeypatch.setattr(br.settings, "memoir_segment_batch_min_chars", 100)
|
||
monkeypatch.setattr(br.settings, "memoir_segment_batch_max_wait_seconds", 0.12)
|
||
|
||
submitted: list[tuple[str, list[str]]] = []
|
||
|
||
async def fake_submit(uid: str, ids: list[str]) -> str:
|
||
submitted.append((uid, ids))
|
||
return "tid"
|
||
|
||
# debounce 须 >0,否则 retry sleep 为 0 会误走「立即提交」分支
|
||
runner = br.BackgroundTaskRunner(debounce_seconds=0.02)
|
||
with patch.object(runner, "_submit_task", new=AsyncMock(side_effect=fake_submit)):
|
||
await runner.queue_message("u1", "seg-a", text_char_count=5)
|
||
await asyncio.sleep(0.2)
|
||
|
||
assert submitted and submitted[0][1] == ["seg-a"]
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_queue_message_not_ready_before_debounce_no_submit(
|
||
monkeypatch: pytest.MonkeyPatch,
|
||
) -> None:
|
||
monkeypatch.setattr(br.settings, "memoir_segment_batch_min_chars", 100)
|
||
monkeypatch.setattr(br.settings, "memoir_segment_batch_max_wait_seconds", 60.0)
|
||
|
||
submitted: list[tuple[str, list[str]]] = []
|
||
|
||
async def fake_submit(uid: str, ids: list[str]) -> str:
|
||
submitted.append((uid, ids))
|
||
return "tid"
|
||
|
||
runner = br.BackgroundTaskRunner(debounce_seconds=0.5)
|
||
with patch.object(runner, "_submit_task", new=AsyncMock(side_effect=fake_submit)):
|
||
await runner.queue_message("u1", "seg-a", text_char_count=5)
|
||
await asyncio.sleep(0.05)
|
||
|
||
assert submitted == []
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_queue_message_chars_met_submits_after_debounce(
|
||
monkeypatch: pytest.MonkeyPatch,
|
||
) -> None:
|
||
monkeypatch.setattr(br.settings, "memoir_segment_batch_min_chars", 10)
|
||
monkeypatch.setattr(br.settings, "memoir_segment_batch_max_wait_seconds", 60.0)
|
||
|
||
submitted: list[tuple[str, list[str]]] = []
|
||
|
||
async def fake_submit(uid: str, ids: list[str]) -> str:
|
||
submitted.append((uid, ids))
|
||
return "tid"
|
||
|
||
runner = br.BackgroundTaskRunner(debounce_seconds=0)
|
||
with patch.object(runner, "_submit_task", new=AsyncMock(side_effect=fake_submit)):
|
||
await runner.queue_message("u1", "seg-long", text_char_count=50)
|
||
await asyncio.sleep(0.05)
|
||
|
||
assert submitted and submitted[0][1] == ["seg-long"]
|