# -*- coding: utf-8 -*-
"""
test_routes_submit.py — 问题记录提交路由测试

覆盖 routes/submit.py：submit_record（加 @login_required 后，P1 收尾三项）
RECORDS_DIR 与 rebuild_search_index 在 conftest app fixture 中已隔离/mock
"""


class TestSubmit:
    """POST /api/submit"""

    def test_requires_login(self, client):
        """未登录返回 401（加鉴权后）"""
        r = client.post("/api/submit", json={})
        assert r.status_code == 401
        assert r.get_json()["error"]["code"] == 1004

    def test_empty_project_name(self, auth_client):
        """空 project_name 返回 400"""
        r = auth_client.post("/api/submit", json={"phenomenon": "x", "recorder": "y"})
        assert r.status_code == 400
        assert r.get_json()["error"]["code"] == 1001

    def test_empty_phenomenon(self, auth_client):
        """空 phenomenon 返回 400"""
        r = auth_client.post("/api/submit", json={"project_name": "x", "recorder": "y"})
        assert r.status_code == 400
        assert r.get_json()["error"]["code"] == 1001

    def test_empty_recorder(self, auth_client):
        """空 recorder 返回 400"""
        r = auth_client.post("/api/submit", json={"project_name": "x", "phenomenon": "y"})
        assert r.status_code == 400
        assert r.get_json()["error"]["code"] == 1001

    def test_submit_success(self, auth_client):
        """正常提交返回 200 + record_id"""
        r = auth_client.post("/api/submit", json={
            "project_name": "测试项目",
            "phenomenon": "mqtt 连接失败",
            "recorder": "测试人",
            "troubleshoot_steps": "检查容器",
            "root_cause": "容器未启动",
            "solution": "启动容器",
        })
        assert r.status_code == 200
        data = r.get_json()
        assert data["success"] is True
        assert "record_id" in data
        assert data["record_id"].startswith("RC-")
