refactor(tests): 技術債清零 — 移除 FakeRepo/FakeSession Mock DB 違規
Some checks failed
CD Pipeline / build-and-deploy (push) Failing after 35s

## ai_router.py
- 抽取 _aggregate_feedback_stats() 純函數,feedback_from_aider_events 呼叫它

## aider_event_processor.py
- _process_one 加 _session_factory=None DI 參數(預設 get_session_factory())
- 可注入測試 factory,不改既有生產邏輯

## test_ai_router_feedback.py(完全重寫)
- 移除 FakeRepo/FakeSession,改為直接測試 _aggregate_feedback_stats 純函數
- 新增 test_feedback_skips_missing_model 邊界條件
- DB 失敗降級行為 test 保留(只 patch get_session_factory,無 FakeRepo)

## test_aider_event_processor.py(完全重寫)
- 移除 FakeRepo/FakeSession,改用真實 PostgreSQL(real_factory fixture)
- Redis xack + IncidentEngine 保留 mock(外部 broker/AI 服務,符合例外)
- 每個測試後 rollback,不污染 dev DB

## setup_test_schema.sql
- 補入 aider_events_payload_gin GIN index(與 adr091 生產 migration 一致)

## integration/conftest.py
- 補注解說明密碼名稱 awoooi_prod_2026 的歷史混淆
- 修正 assert 邏輯:檢查 DB 名稱而非 URL 字串,避免密碼含 prod 觸發誤判

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Your Name
2026-04-22 01:33:30 +08:00
parent d0591c54b0
commit 479f8d8971
6 changed files with 154 additions and 180 deletions

View File

@@ -23,12 +23,14 @@ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_asyn
DEV_DB_URL = os.environ.get(
"TEST_DATABASE_URL",
# 注意:密碼 awoooi_prod_2026 是 PostgreSQL 帳號的實際密碼(歷史命名),
# 並非指向 prod DB — 連線目標為 awoooi_dev開發資料庫
"postgresql+asyncpg://awoooi:awoooi_prod_2026@192.168.0.188:5432/awoooi_dev?ssl=disable",
)
# 確保不會誤打 prod
assert "prod" not in DEV_DB_URL or "awoooi_prod_2026" in DEV_DB_URL, (
"TEST_DATABASE_URL 不可指向 prod DB"
# 確保不會誤打 prod(允許密碼含 "prod" 字串,檢查 DB 名稱)
assert "awoooi_prod" not in DEV_DB_URL.split("/")[-1], (
"TEST_DATABASE_URL 不可指向 prod DBawoooi_prod"
)

View File

@@ -119,3 +119,4 @@ CREATE TABLE IF NOT EXISTS aider_events (
CREATE INDEX IF NOT EXISTS aider_events_session_idx ON aider_events(session_id);
CREATE INDEX IF NOT EXISTS aider_events_type_ts_idx ON aider_events(type, ts DESC);
CREATE INDEX IF NOT EXISTS aider_events_ts_idx ON aider_events(ts DESC);
CREATE INDEX IF NOT EXISTS aider_events_payload_gin ON aider_events USING GIN (payload);