"""画像版本生成器单元测试(`docs/00` §6.4.6 的四条规则)。 覆盖四个方向,都要有牙: 1. **同事务两条事件**:一次生成必须同时投 `MILVUS` 与 `NEO4J` 两条,共用同一 `event_uuid`。 2. **幂等**:`snapshot_hash` 未变 → **不升版本、不投事件**(否则每次调用都灌 outbox)。 3. **旧版本置 0**:清旧当前标记必须发生在插新版本**之前**(唯一键约束)。 4. **失败关闭**:客户主表没有该行 → 抛错,不凭空造画像。 5. **字段对齐**:写入快照必须覆盖读取侧白名单所需字段(否则端点会返回空画像)。 """ from __future__ import annotations import json from datetime import UTC, datetime, timedelta from typing import Any import pytest from app.core.errors import ValidationAgentError from app.core.profile_projection import project_profile from app.service.profile_generation_service import ( REQUIRED_SNAPSHOT_FIELDS, SYNC_TARGETS, ProfileGenerationService, build_snapshot, compute_hash, ) NOW = datetime(2026, 9, 11, 12, 0, tzinfo=UTC) # --------------------------------------------------------------------------- # 替身:只实现 ProfileRepository 用到的方法 # --------------------------------------------------------------------------- class FakeRepository: """记录调用顺序与写入内容;`executed` 用来断言"先清旧、后插新"。""" def __init__( self, *, profile: dict[str, Any] | None = None, assessment: dict[str, Any] | None = None, current: dict[str, Any] | None = None, next_version: int = 2, ) -> None: self._profile = profile self._assessment = assessment self._current = current self._next_version = next_version self.executed: list[str] = [] self.inserted_snapshot: dict[str, Any] | None = None self.sync_events: list[dict[str, Any]] = [] async def profile_row(self, _cid: int) -> dict[str, Any] | None: self.executed.append("profile_row") return self._profile async def latest_assessment(self, _cid: int) -> dict[str, Any] | None: self.executed.append("latest_assessment") return self._assessment async def current_snapshot(self, _cid: int) -> dict[str, Any] | None: self.executed.append("current_snapshot") return self._current async def next_version(self, _cid: int) -> int: self.executed.append("next_version") return self._next_version async def clear_current(self, _cid: int, *, now: datetime) -> None: self.executed.append("clear_current") async def insert_snapshot(self, **kwargs: Any) -> None: self.executed.append("insert_snapshot") self.inserted_snapshot = kwargs def add_sync_event(self, **kwargs: Any) -> None: self.executed.append("add_sync_event") self.sync_events.append(kwargs) def service(monkeypatch: pytest.MonkeyPatch, repo: FakeRepository) -> ProfileGenerationService: monkeypatch.setattr( "app.service.profile_generation_service.ProfileRepository", lambda _session: repo ) return ProfileGenerationService(session=object()) # type: ignore[arg-type] def profile_row(**overrides: Any) -> dict[str, Any]: row: dict[str, Any] = { "customer_id": 9102, "investor_type": "C3", "investment_horizon": "medium_term", "trading_frequency": "medium", "preferred_asset_class": ["bond_fund"], "risk_tags": ["balanced"], "behavior_score": 61, "total_asset": "860000.00", "last_active_at": NOW, } row.update(overrides) return row def assessment_row(valid_days: int = 200) -> dict[str, Any]: return { "id": 99102, "questionnaire_version": "v2026.1", "investor_type": "C3", "assessed_at": NOW - timedelta(days=165), "valid_until": (NOW + timedelta(days=valid_days)).replace(tzinfo=None), } # --------------------------------------------------------------------------- # 1. 快照构建 # --------------------------------------------------------------------------- def test_snapshot_covers_every_field_the_read_side_needs() -> None: """写侧必须覆盖读取侧白名单所需字段,否则端点会返回空画像。""" snapshot = build_snapshot(profile_row(), assessment_row(), now=NOW) missing = [f for f in REQUIRED_SNAPSHOT_FIELDS if f not in snapshot] assert not missing, f"快照缺少读取侧需要的字段:{missing}" def test_snapshot_is_projectable_by_the_read_path() -> None: """端到端对齐:生成出来的快照经 `project_profile` 能得到非空投影。""" snapshot = build_snapshot(profile_row(), assessment_row(), now=NOW) projected = project_profile(snapshot, now=NOW) assert projected["investor_type"] == "C3" assert projected["assessment_expired"] is False def test_expired_assessment_is_marked_expired_in_snapshot() -> None: """过期测评必须在快照里标成过期(与读取侧"按当前时间重算"同向)。""" snapshot = build_snapshot(profile_row(), assessment_row(valid_days=-45), now=NOW) assert snapshot["assessment_expired"] is True assert project_profile(snapshot, now=NOW)["assessment_expired"] is True def test_no_assessment_means_no_fabricated_validity() -> None: """没有测评行时**不写**有效期/过期标记,而不是伪造一个。""" snapshot = build_snapshot(profile_row(), None, now=NOW) assert "assessment_valid_until" not in snapshot assert "assessment_expired" not in snapshot def test_hash_is_stable_against_key_order() -> None: """哈希是幂等判据,不能被字典插入顺序影响。""" a = {"investor_type": "C3", "behavior_score": 61} b = {"behavior_score": 61, "investor_type": "C3"} assert compute_hash(a) == compute_hash(b) assert compute_hash({"x": 1}) != compute_hash({"x": 2}) # --------------------------------------------------------------------------- # 2. 首次生成:两条事件 + 顺序 # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_first_generation_writes_two_events_and_clears_old_current( monkeypatch: pytest.MonkeyPatch, ) -> None: repo = FakeRepository(profile=profile_row(), assessment=assessment_row(), current=None) result = await service(monkeypatch, repo).generate(9102, now=NOW) assert result.changed is True assert result.version == 2 assert result.sync_events == 2 # 两个目标存储各一条,且**共用同一个 event_uuid**(唯一键是 (event_uuid, target_store)) targets = [e["target_store"] for e in repo.sync_events] assert targets == list(SYNC_TARGETS) assert len({e["event_uuid"] for e in repo.sync_events}) == 1 # `aggregate_type='profile'` 由仓储层固定写入(不在 kwargs 里),此处断言服务传入的实体标识 assert len({e["aggregate_uuid"] for e in repo.sync_events}) == 1 assert all(e["aggregate_uuid"] == result.profile_uuid for e in repo.sync_events) assert all(e["operation"] == "UPSERT" for e in repo.sync_events) # 顺序:清旧当前标记必须在插新版本之前(否则撞唯一键) assert repo.executed.index("clear_current") < repo.executed.index("insert_snapshot") # 新版本标为当前 assert repo.inserted_snapshot is not None assert repo.inserted_snapshot["version"] == 2 @pytest.mark.asyncio async def test_each_version_gets_a_fresh_profile_uuid(monkeypatch: pytest.MonkeyPatch) -> None: """`profile_uuid` 有唯一键,**每个版本必须用新 uuid**(实测撞过 Duplicate entry)。""" old = {"profile_uuid": "old-uuid", "version": 1, "snapshot_hash": "different"} repo = FakeRepository(profile=profile_row(), assessment=assessment_row(), current=old) result = await service(monkeypatch, repo).generate(9102, now=NOW) assert result.profile_uuid != "old-uuid" assert repo.inserted_snapshot is not None assert repo.inserted_snapshot["profile_uuid"] == result.profile_uuid # --------------------------------------------------------------------------- # 3. 幂等 # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_unchanged_content_does_not_bump_version_or_emit_events( monkeypatch: pytest.MonkeyPatch, ) -> None: snapshot = build_snapshot(profile_row(), assessment_row(), now=NOW) current = { "profile_uuid": "u1", "version": 7, "snapshot_hash": compute_hash(snapshot), } repo = FakeRepository(profile=profile_row(), assessment=assessment_row(), current=current) result = await service(monkeypatch, repo).generate(9102, now=NOW) assert result.changed is False assert result.version == 7 # 不升版本 assert result.sync_events == 0 # 不投事件 assert "insert_snapshot" not in repo.executed assert repo.sync_events == [] @pytest.mark.asyncio async def test_changed_content_bumps_version_and_reemits( monkeypatch: pytest.MonkeyPatch, ) -> None: current = {"profile_uuid": "u1", "version": 3, "snapshot_hash": "stale-hash"} repo = FakeRepository( profile=profile_row(total_asset="999999.00"), assessment=assessment_row(), current=current, next_version=4, ) result = await service(monkeypatch, repo).generate(9102, now=NOW) assert result.changed is True assert result.version == 4 assert result.sync_events == 2 # --------------------------------------------------------------------------- # 4. 失败关闭 # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_missing_customer_fails_closed(monkeypatch: pytest.MonkeyPatch) -> None: """主表没有该客户 → 抛错,不凭空造画像。""" repo = FakeRepository(profile=None) with pytest.raises(ValidationAgentError, match="客户不存在"): await service(monkeypatch, repo).generate(99999, now=NOW) assert repo.inserted_snapshot is None assert repo.sync_events == [] # --------------------------------------------------------------------------- # 5. 事件载荷 # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_event_payload_carries_version_and_hash(monkeypatch: pytest.MonkeyPatch) -> None: repo = FakeRepository(profile=profile_row(), assessment=assessment_row(), current=None) result = await service(monkeypatch, repo).generate(9102, now=NOW) payload = repo.sync_events[0]["payload"] assert payload["version"] == result.version assert payload["snapshot_hash"] == result.snapshot_hash assert payload["customer_id"] == "9102" assert payload["snapshot"]["investor_type"] == "C3" # 载荷必须可 JSON 序列化(outbox 是 JSON 列) json.dumps(payload, ensure_ascii=False)