refactor: remove analytics module, update tool postprocessor tests
This commit is contained in:
@@ -37,6 +37,28 @@ def test_postprocess_calendar_create_partial() -> None:
|
||||
assert processed.ui_hints["status"] == "warning"
|
||||
|
||||
|
||||
def test_postprocess_calendar_share_has_ui_hints() -> None:
|
||||
output = _make_tool_output(
|
||||
command="calendar",
|
||||
subcommand="share",
|
||||
status=ToolStatus.SUCCESS,
|
||||
data={
|
||||
"status": "success",
|
||||
"success": 2,
|
||||
"failed": 0,
|
||||
"results": [
|
||||
{"phone": "+8613800138001", "status": "success"},
|
||||
{"phone": "+8613800138002", "status": "success"},
|
||||
],
|
||||
},
|
||||
)
|
||||
processed = postprocess_tool_output(output)
|
||||
assert processed.ui_hints is not None
|
||||
assert processed.ui_hints["intent"] == "status"
|
||||
assert processed.ui_hints["status"] == "success"
|
||||
assert processed.ui_hints["title"] == "日程分享结果"
|
||||
|
||||
|
||||
def test_postprocess_contacts_read_has_ui_hints() -> None:
|
||||
output = _make_tool_output(command="contacts", subcommand="read", status=ToolStatus.SUCCESS, data={"friends_count": 3, "friends": []})
|
||||
processed = postprocess_tool_output(output)
|
||||
|
||||
@@ -9,8 +9,10 @@ from core.config.settings import config
|
||||
|
||||
from v1.auth.gateway import SupabaseAuthGateway
|
||||
from v1.auth.schemas import (
|
||||
AuthUser,
|
||||
OtpSendRequest,
|
||||
PhoneSessionCreateRequest,
|
||||
SessionResponse,
|
||||
SessionRefreshRequest,
|
||||
)
|
||||
|
||||
@@ -108,6 +110,39 @@ class TestSupabaseAuthGateway:
|
||||
|
||||
assert exc_info.value.status_code == 401
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_refresh_session_uses_dev_flow_in_dev_environment(
|
||||
self,
|
||||
gateway: tuple[SupabaseAuthGateway, MagicMock, MagicMock],
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
sut, mock_client, _ = gateway
|
||||
monkeypatch.setattr(config.runtime, "environment", "dev")
|
||||
|
||||
expected = SessionResponse(
|
||||
access_token="dev-access",
|
||||
refresh_token="dev-refresh",
|
||||
expires_in=3600,
|
||||
token_type="bearer",
|
||||
user=AuthUser(id="user-dev", phone="+8613812345678"),
|
||||
)
|
||||
|
||||
async def _fake_refresh_dev_phone_session(*, request: SessionRefreshRequest) -> SessionResponse:
|
||||
assert request.refresh_token == "dev-refresh"
|
||||
return expected
|
||||
|
||||
monkeypatch.setattr(
|
||||
"v1.auth.gateway.refresh_dev_phone_session",
|
||||
_fake_refresh_dev_phone_session,
|
||||
)
|
||||
|
||||
response = await sut.refresh_session(
|
||||
SessionRefreshRequest(refresh_token="dev-refresh")
|
||||
)
|
||||
|
||||
assert response == expected
|
||||
assert mock_client.auth.refresh_session.call_count == 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_user_by_phone_uses_in_memory_cache(
|
||||
self,
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
from pydantic import SecretStr
|
||||
|
||||
from core.config.settings import config
|
||||
from core.http.errors import ApiProblemError
|
||||
from v1.auth.dev_phone_session import create_dev_phone_session, refresh_dev_phone_session
|
||||
from v1.auth.schemas import PhoneSessionCreateRequest, SessionRefreshRequest
|
||||
|
||||
|
||||
_TEST_JWT_SECRET = "test-secret-key-with-32-bytes-minimum!!"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dev_session_refresh_round_trip(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(config.runtime, "environment", "dev")
|
||||
monkeypatch.setattr(config.supabase, "jwt_secret", SecretStr(_TEST_JWT_SECRET))
|
||||
monkeypatch.setattr(config.supabase, "jwt_issuer", "http://localhost:8001/auth/v1")
|
||||
async def _fake_find_or_create_user_by_phone(_phone: str) -> UUID:
|
||||
return UUID("00000000-0000-0000-0000-000000000123")
|
||||
|
||||
monkeypatch.setattr(
|
||||
"v1.auth.dev_phone_session._find_or_create_user_by_phone",
|
||||
_fake_find_or_create_user_by_phone,
|
||||
)
|
||||
|
||||
created = await create_dev_phone_session(
|
||||
request=PhoneSessionCreateRequest(phone="+8613812345678", token="123456")
|
||||
)
|
||||
refreshed = await refresh_dev_phone_session(
|
||||
request=SessionRefreshRequest(refresh_token=created.refresh_token)
|
||||
)
|
||||
|
||||
assert refreshed.user.id == "00000000-0000-0000-0000-000000000123"
|
||||
assert refreshed.user.phone == "+8613812345678"
|
||||
assert refreshed.access_token
|
||||
assert refreshed.refresh_token
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dev_session_refresh_rejects_invalid_token(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(config.runtime, "environment", "dev")
|
||||
monkeypatch.setattr(config.supabase, "jwt_secret", SecretStr(_TEST_JWT_SECRET))
|
||||
monkeypatch.setattr(config.supabase, "jwt_issuer", "http://localhost:8001/auth/v1")
|
||||
|
||||
with pytest.raises(ApiProblemError) as exc_info:
|
||||
await refresh_dev_phone_session(
|
||||
request=SessionRefreshRequest(refresh_token="invalid-token")
|
||||
)
|
||||
|
||||
assert exc_info.value.status_code == 401
|
||||
assert exc_info.value.code == "AUTH_REFRESH_TOKEN_INVALID"
|
||||
Reference in New Issue
Block a user