|
| 1 | +import json |
| 2 | +from io import BytesIO |
| 3 | +from urllib.error import HTTPError |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from WeekFlow.models.report import ProjectItem, WeeklyReport |
| 8 | +from WeekFlow.services.ai import AIConfigError, AIService |
| 9 | + |
| 10 | + |
| 11 | +def _configured_report(**kwargs) -> WeeklyReport: |
| 12 | + report = WeeklyReport(**kwargs) |
| 13 | + report.ai["config"]["api_key"] = "test-key" |
| 14 | + return report |
| 15 | + |
| 16 | + |
| 17 | +def test_ai_service_requires_configuration(): |
| 18 | + report = WeeklyReport() |
| 19 | + report.ai["config"]["api_key"] = "" |
| 20 | + |
| 21 | + service = AIService() |
| 22 | + |
| 23 | + try: |
| 24 | + service.test_connection(report) |
| 25 | + except AIConfigError as exc: |
| 26 | + assert "API Key" in str(exc) |
| 27 | + else: # pragma: no cover |
| 28 | + raise AssertionError("Expected AIConfigError when API Key is missing") |
| 29 | + |
| 30 | + |
| 31 | +def test_ai_service_summarizes_basic_info_from_full_report(monkeypatch): |
| 32 | + report = _configured_report( |
| 33 | + topic="社区活动筹备", |
| 34 | + achievements=["完成了志愿者分工表", "整理了活动物资清单"], |
| 35 | + projects=[ProjectItem(name="项目 A", summary="完善报名说明", next_step="继续确认现场安排")], |
| 36 | + ) |
| 37 | + service = AIService() |
| 38 | + seen_messages = {} |
| 39 | + |
| 40 | + def fake_chat(_config, messages): |
| 41 | + seen_messages["messages"] = messages |
| 42 | + return json.dumps({"one_line_summary": "集中整理了活动安排与资料信息。"}, ensure_ascii=False) |
| 43 | + |
| 44 | + monkeypatch.setattr(service, "_chat", fake_chat) |
| 45 | + |
| 46 | + polished = service.polish_current_section(report, section_key="basic_info") |
| 47 | + |
| 48 | + assert polished.one_line_summary == "集中整理了活动安排与资料信息。" |
| 49 | + prompt_text = seen_messages["messages"][1]["content"] |
| 50 | + assert "项目 A" in prompt_text |
| 51 | + assert "完成了志愿者分工表" in prompt_text |
| 52 | + |
| 53 | + |
| 54 | +def test_ai_service_polishes_weekly_results_with_stubbed_response(monkeypatch): |
| 55 | + report = _configured_report(achievements=["旧成果"]) |
| 56 | + service = AIService() |
| 57 | + |
| 58 | + def fake_chat(_config, _messages): |
| 59 | + return json.dumps({"achievements": ["新成果", "补充说明"]}, ensure_ascii=False) |
| 60 | + |
| 61 | + monkeypatch.setattr(service, "_chat", fake_chat) |
| 62 | + |
| 63 | + polished = service.polish_current_section(report, section_key="overview") |
| 64 | + |
| 65 | + assert polished.achievements == ["新成果", "补充说明"] |
| 66 | + |
| 67 | + |
| 68 | +def test_ai_service_polishes_current_project_with_stubbed_response(monkeypatch): |
| 69 | + report = _configured_report(projects=[ProjectItem(name="项目 A", summary="旧内容", next_step="旧计划")]) |
| 70 | + service = AIService() |
| 71 | + |
| 72 | + def fake_chat(_config, _messages): |
| 73 | + return json.dumps({"summary": "新内容", "next_step": "新计划"}, ensure_ascii=False) |
| 74 | + |
| 75 | + monkeypatch.setattr(service, "_chat", fake_chat) |
| 76 | + |
| 77 | + polished = service.polish_current_section(report, section_key="projects", project_index=0) |
| 78 | + |
| 79 | + assert polished.projects[0].name == "项目 A" |
| 80 | + assert polished.projects[0].summary == "新内容" |
| 81 | + assert polished.projects[0].next_step == "新计划" |
| 82 | + |
| 83 | + |
| 84 | +def test_ai_service_uses_volcengine_plan_defaults(): |
| 85 | + report = WeeklyReport() |
| 86 | + |
| 87 | + assert report.ai["provider"] == "volcengine_plan" |
| 88 | + assert report.ai["config"]["model"] == "doubao-seed-2-0-pro-260215" |
| 89 | + |
| 90 | + |
| 91 | +def test_ai_service_uses_ark_sdk_for_volcengine_ark(monkeypatch): |
| 92 | + report = _configured_report() |
| 93 | + report.ai["provider"] = "volcengine_ark" |
| 94 | + report.ai["config"]["base_url"] = "https://ark.cn-beijing.volces.com/api/v3" |
| 95 | + report.ai["config"]["model"] = "doubao-seed-2-0-lite-260215" |
| 96 | + service = AIService() |
| 97 | + called = {} |
| 98 | + |
| 99 | + def fake_ark(self, config, messages): |
| 100 | + called["provider"] = config.provider |
| 101 | + called["message_count"] = len(messages) |
| 102 | + return "连接成功" |
| 103 | + |
| 104 | + def fail_openai(self, config, messages): # pragma: no cover |
| 105 | + pytest.fail("volcengine_ark provider should use Ark SDK path") |
| 106 | + |
| 107 | + monkeypatch.setattr(AIService, "_chat_with_ark", fake_ark, raising=False) |
| 108 | + monkeypatch.setattr(AIService, "_chat_with_openai_compatible", fail_openai, raising=False) |
| 109 | + |
| 110 | + assert service.test_connection(report) == "连接成功" |
| 111 | + assert called["provider"] == "volcengine_ark" |
| 112 | + assert called["message_count"] == 2 |
| 113 | + |
| 114 | + |
| 115 | +def test_ai_service_maps_http_401_to_clear_configuration_hint(monkeypatch): |
| 116 | + report = _configured_report() |
| 117 | + service = AIService() |
| 118 | + |
| 119 | + def fake_urlopen(_req, timeout=60): |
| 120 | + raise HTTPError( |
| 121 | + url="https://operator.las.cn-beijing.volces.com/api/v1/chat/completions", |
| 122 | + code=401, |
| 123 | + msg="Unauthorized", |
| 124 | + hdrs=None, |
| 125 | + fp=BytesIO(b'{"path":"/api/v1/chat/completions","message":"Unauthorized"}'), |
| 126 | + ) |
| 127 | + |
| 128 | + monkeypatch.setattr("WeekFlow.services.ai.request.urlopen", fake_urlopen) |
| 129 | + |
| 130 | + try: |
| 131 | + service.test_connection(report) |
| 132 | + except AIConfigError as exc: |
| 133 | + message = str(exc) |
| 134 | + assert "HTTP 401" in message |
| 135 | + assert "火山引擎plan" in message |
| 136 | + assert "API Key" in message |
| 137 | + assert "Base URL" in message |
| 138 | + else: # pragma: no cover |
| 139 | + raise AssertionError("Expected AIConfigError for HTTP 401") |
0 commit comments