47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
from datetime import datetime
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.main import app, get_gateway
|
|
from app.models import LeaveDraftSuggestion, LeaveDraftSuggestionRequest, LeaveType
|
|
|
|
|
|
class FakeGateway:
|
|
async def suggest(self, request: LeaveDraftSuggestionRequest) -> LeaveDraftSuggestion:
|
|
assert request.text == "明天下午请事假四小时"
|
|
return LeaveDraftSuggestion(
|
|
type=LeaveType.PERSONAL,
|
|
startsAt=datetime.fromisoformat("2026-07-19T13:30:00+08:00"),
|
|
endsAt=datetime.fromisoformat("2026-07-19T17:30:00+08:00"),
|
|
reason="办理个人事务",
|
|
assumptions=["下午按 13:30 开始计算"],
|
|
)
|
|
|
|
|
|
def test_returns_structured_suggestion_without_executing_business_action() -> None:
|
|
app.dependency_overrides[get_gateway] = lambda: FakeGateway()
|
|
try:
|
|
response = TestClient(app).post(
|
|
"/v1/leave-drafts/suggest",
|
|
json={"text": "明天下午请事假四小时", "timezone": "Asia/Shanghai"},
|
|
)
|
|
finally:
|
|
app.dependency_overrides.clear()
|
|
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["suggestion"]["type"] == "PERSONAL"
|
|
assert body["requiresUserConfirmation"] is True
|
|
assert "applicantId" not in body["suggestion"]
|
|
|
|
|
|
def test_rejects_invalid_time_range_from_model() -> None:
|
|
try:
|
|
LeaveDraftSuggestion(
|
|
startsAt=datetime.fromisoformat("2026-07-19T17:30:00+08:00"),
|
|
endsAt=datetime.fromisoformat("2026-07-19T13:30:00+08:00"),
|
|
)
|
|
assert False, "validation should fail"
|
|
except ValueError:
|
|
pass
|