大模型速度增加提示
This commit is contained in:
1 parent
4e764350a4
commit
3ea33e1dee
52 files changed
+4673
-176
No files matched your search
@@ -0,0 +1,321 @@
|
||||
"""Tests for the LLM call metrics middleware + repository.
|
||||
|
||||
Covers:
|
||||
- Successful calls emit a row with duration, token counts and tokens_per_sec.
|
||||
- Failed calls record the exception class as ``error_type`` plus a trimmed
|
||||
``error_message``.
|
||||
- The middleware never blocks on a backend exception (best-effort writes).
|
||||
- The SQL store applies filters and pagination for the admin list endpoint.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import uuid
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from langchain_core.messages import AIMessage
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from deerflow.agents.middlewares.llm_metrics_middleware import (
|
||||
LlmMetricsMiddleware,
|
||||
_extract_usage,
|
||||
)
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.llm_metrics import SqlLlmMetricsStore
|
||||
from deerflow.persistence.llm_metrics.base import LlmMetricRecord, LlmMetricsQuery
|
||||
|
||||
|
||||
class _RecordingStore:
|
||||
"""Minimal in-memory store used to assert what the middleware persists."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.records: list[LlmMetricRecord] = []
|
||||
|
||||
async def record(self, metric: LlmMetricRecord) -> None:
|
||||
self.records.append(metric)
|
||||
|
||||
async def list(self, query: LlmMetricsQuery):
|
||||
return [r.__dict__ for r in self.records]
|
||||
|
||||
async def count(self, query: LlmMetricsQuery) -> int:
|
||||
return len(self.records)
|
||||
|
||||
async def iter_all(self, query: LlmMetricsQuery):
|
||||
for record in self.records:
|
||||
yield record.__dict__
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def sql_store(tmp_path):
|
||||
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'metrics.db'}")
|
||||
async with engine.begin() as conn:
|
||||
# Importing the package registers the LlmCallMetricRow with Base.metadata.
|
||||
import deerflow.persistence.llm_metrics.model # noqa: F401
|
||||
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
sf = async_sessionmaker(engine, expire_on_commit=False)
|
||||
store = SqlLlmMetricsStore(sf)
|
||||
try:
|
||||
yield store
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
def _make_request():
|
||||
return type(
|
||||
"FakeRequest",
|
||||
(),
|
||||
{
|
||||
"model": type("FakeModel", (), {"model_name": "gpt-test"}),
|
||||
"runtime": type(
|
||||
"FakeRuntime",
|
||||
(),
|
||||
{
|
||||
"config": {"configurable": {"thread_id": "t1"}},
|
||||
"context": {"agent_name": "lead_agent", "run_id": "r1"},
|
||||
},
|
||||
),
|
||||
"state": {"thread_id": "t1"},
|
||||
},
|
||||
)()
|
||||
|
||||
|
||||
def test_middleware_records_success_with_speed():
|
||||
import time as _time
|
||||
|
||||
store = _RecordingStore()
|
||||
mw = LlmMetricsMiddleware(store=store)
|
||||
response = AIMessage(
|
||||
content="hi",
|
||||
usage_metadata={"input_tokens": 5, "output_tokens": 20, "total_tokens": 25},
|
||||
)
|
||||
|
||||
def slow_handler(_req):
|
||||
# Force a measurable elapsed window so tokens_per_sec is populated.
|
||||
# 50ms is comfortably above Windows monotonic-clock granularity.
|
||||
_time.sleep(0.05)
|
||||
return response
|
||||
|
||||
result = mw.wrap_model_call(_make_request(), slow_handler)
|
||||
|
||||
assert result is response
|
||||
# awrap_model_call uses a fire-and-forget task; here the sync path runs
|
||||
# asyncio.run synchronously, so the record landed already.
|
||||
assert len(store.records) == 1
|
||||
record = store.records[0]
|
||||
assert record.status == "success"
|
||||
assert record.model_name == "gpt-test"
|
||||
assert record.thread_id == "t1"
|
||||
assert record.run_id == "r1"
|
||||
assert record.input_tokens == 5
|
||||
assert record.output_tokens == 20
|
||||
assert record.total_tokens == 25
|
||||
# tokens_per_sec may be very high in tests because the handler is
|
||||
# ~instant; we only assert that the field is populated.
|
||||
assert record.tokens_per_sec is not None
|
||||
assert record.duration_ms >= 0
|
||||
|
||||
|
||||
def test_middleware_records_error_with_classification():
|
||||
store = _RecordingStore()
|
||||
mw = LlmMetricsMiddleware(store=store)
|
||||
|
||||
class BoomError(RuntimeError):
|
||||
pass
|
||||
|
||||
def explode(_req):
|
||||
raise BoomError("provider exploded")
|
||||
|
||||
with pytest.raises(BoomError):
|
||||
mw.wrap_model_call(_make_request(), explode)
|
||||
|
||||
assert len(store.records) == 1
|
||||
record = store.records[0]
|
||||
assert record.status == "error"
|
||||
assert record.error_type == "BoomError"
|
||||
assert record.error_message == "provider exploded"
|
||||
assert record.tokens_per_sec is None
|
||||
|
||||
|
||||
def test_middleware_swallows_store_errors():
|
||||
"""A persistence outage must not break the agent runtime."""
|
||||
|
||||
class FaultyStore:
|
||||
async def record(self, metric):
|
||||
raise RuntimeError("disk gone")
|
||||
|
||||
async def list(self, q):
|
||||
return []
|
||||
|
||||
async def count(self, q):
|
||||
return 0
|
||||
|
||||
async def iter_all(self, q):
|
||||
if False:
|
||||
yield {}
|
||||
|
||||
mw = LlmMetricsMiddleware(store=FaultyStore())
|
||||
# The wrap returns normally even though _flush would raise; the call below
|
||||
# must not raise.
|
||||
response = AIMessage(
|
||||
content="ok",
|
||||
usage_metadata={"input_tokens": 1, "output_tokens": 1, "total_tokens": 2},
|
||||
)
|
||||
out = mw.wrap_model_call(_make_request(), lambda req: response)
|
||||
assert out is response
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sql_store_round_trip(sql_store):
|
||||
base = datetime.now(UTC)
|
||||
for i in range(3):
|
||||
await sql_store.record(
|
||||
LlmMetricRecord(
|
||||
id=uuid.uuid4().hex,
|
||||
created_at=base - timedelta(minutes=i),
|
||||
user_id="alice",
|
||||
model_name="gpt-test" if i % 2 == 0 else "gpt-other",
|
||||
duration_ms=100 * (i + 1),
|
||||
output_tokens=10 * (i + 1),
|
||||
tokens_per_sec=10.0 * (i + 1),
|
||||
status="success",
|
||||
)
|
||||
)
|
||||
rows = await sql_store.list(LlmMetricsQuery(limit=10))
|
||||
assert len(rows) == 3
|
||||
# Newest first.
|
||||
assert rows[0]["duration_ms"] == 100
|
||||
|
||||
filtered = await sql_store.list(LlmMetricsQuery(model_name="gpt-test", limit=10))
|
||||
assert all(r["model_name"] == "gpt-test" for r in filtered)
|
||||
assert len(filtered) == 2
|
||||
|
||||
assert await sql_store.count(LlmMetricsQuery(model_name="gpt-test")) == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sql_store_filters_errors_only(sql_store):
|
||||
base = datetime.now(UTC)
|
||||
await sql_store.record(
|
||||
LlmMetricRecord(id=uuid.uuid4().hex, created_at=base, status="success", duration_ms=10, output_tokens=1)
|
||||
)
|
||||
await sql_store.record(
|
||||
LlmMetricRecord(
|
||||
id=uuid.uuid4().hex,
|
||||
created_at=base,
|
||||
status="error",
|
||||
duration_ms=5,
|
||||
error_type="APITimeoutError",
|
||||
error_message="timed out",
|
||||
)
|
||||
)
|
||||
only_errors = await sql_store.list(LlmMetricsQuery(status="error", limit=10))
|
||||
assert len(only_errors) == 1
|
||||
assert only_errors[0]["error_type"] == "APITimeoutError"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sql_store_iter_all_paginates(sql_store):
|
||||
base = datetime.now(UTC)
|
||||
for i in range(1200):
|
||||
await sql_store.record(
|
||||
LlmMetricRecord(
|
||||
id=uuid.uuid4().hex,
|
||||
created_at=base - timedelta(seconds=i),
|
||||
duration_ms=1,
|
||||
status="success",
|
||||
)
|
||||
)
|
||||
seen = 0
|
||||
async for _ in sql_store.iter_all(LlmMetricsQuery()):
|
||||
seen += 1
|
||||
assert seen == 1200
|
||||
|
||||
|
||||
def test_extract_usage_prefers_usage_metadata():
|
||||
response = AIMessage(
|
||||
content="x",
|
||||
usage_metadata={"input_tokens": 3, "output_tokens": 7, "total_tokens": 10},
|
||||
)
|
||||
out = _extract_usage(response)
|
||||
assert out == {"input_tokens": 3, "output_tokens": 7, "total_tokens": 10}
|
||||
|
||||
|
||||
def test_extract_usage_falls_back_to_response_metadata_token_usage():
|
||||
"""DeepSeek / vLLM-style: only total in usage_metadata, breakdown elsewhere."""
|
||||
response = AIMessage(
|
||||
content="x",
|
||||
usage_metadata={"input_tokens": 0, "output_tokens": 0, "total_tokens": 11093},
|
||||
response_metadata={
|
||||
"token_usage": {
|
||||
"prompt_tokens": 9000,
|
||||
"completion_tokens": 2093,
|
||||
"total_tokens": 11093,
|
||||
},
|
||||
},
|
||||
)
|
||||
out = _extract_usage(response)
|
||||
assert out["input_tokens"] == 9000
|
||||
assert out["output_tokens"] == 2093
|
||||
assert out["total_tokens"] == 11093
|
||||
|
||||
|
||||
def test_extract_usage_unwraps_model_response():
|
||||
"""The handler hands us a ModelResponse dataclass, not a bare AIMessage."""
|
||||
from langchain.agents.middleware.types import ModelResponse
|
||||
|
||||
inner = AIMessage(
|
||||
content="hi",
|
||||
usage_metadata={"input_tokens": 7, "output_tokens": 9, "total_tokens": 16},
|
||||
)
|
||||
response = ModelResponse(result=[inner])
|
||||
out = _extract_usage(response)
|
||||
assert out == {"input_tokens": 7, "output_tokens": 9, "total_tokens": 16}
|
||||
|
||||
|
||||
def test_extract_usage_unwraps_extended_model_response():
|
||||
"""ExtendedModelResponse wraps a ModelResponse in a ``model_response`` field."""
|
||||
from langchain.agents.middleware.types import ExtendedModelResponse, ModelResponse
|
||||
|
||||
inner = AIMessage(
|
||||
content="hi",
|
||||
usage_metadata={"input_tokens": 1, "output_tokens": 2, "total_tokens": 3},
|
||||
)
|
||||
extended = ExtendedModelResponse(model_response=ModelResponse(result=[inner]))
|
||||
out = _extract_usage(extended)
|
||||
assert out["output_tokens"] == 2
|
||||
|
||||
|
||||
def test_extract_usage_derives_missing_field_when_two_present():
|
||||
response = AIMessage(
|
||||
content="x",
|
||||
usage_metadata={"input_tokens": 9000, "output_tokens": 0, "total_tokens": 11093},
|
||||
)
|
||||
out = _extract_usage(response)
|
||||
# output should be derived as total - input = 2093.
|
||||
assert out["output_tokens"] == 2093
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_middleware_records_success():
|
||||
store = _RecordingStore()
|
||||
mw = LlmMetricsMiddleware(store=store)
|
||||
|
||||
async def handler(_req):
|
||||
return AIMessage(
|
||||
content="hi",
|
||||
usage_metadata={"input_tokens": 1, "output_tokens": 4, "total_tokens": 5},
|
||||
)
|
||||
|
||||
response = await mw.awrap_model_call(_make_request(), handler)
|
||||
assert isinstance(response, AIMessage)
|
||||
|
||||
# awrap_model_call schedules the write as an asyncio.task; give the loop a
|
||||
# chance to drain.
|
||||
await asyncio.sleep(0)
|
||||
assert len(store.records) == 1
|
||||
assert store.records[0].status == "success"
|
||||
assert store.records[0].output_tokens == 4
|
||||
@@ -0,0 +1,108 @@
|
||||
"""Tests for the admin-configurable prompt-prefix toggle."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from langchain_core.messages import AIMessage, HumanMessage
|
||||
|
||||
from app.gateway.services import _prepend_prompt_prefix, _prompt_prefix_opted_in
|
||||
from deerflow.config import system_settings as ss
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def tmp_runtime_home(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path:
|
||||
"""Redirect ``runtime_home()`` to a per-test tmp dir."""
|
||||
|
||||
def fake_home() -> Path:
|
||||
return tmp_path
|
||||
|
||||
monkeypatch.setattr(ss, "runtime_home", fake_home)
|
||||
return tmp_path
|
||||
|
||||
|
||||
def test_load_returns_defaults_when_missing(tmp_runtime_home: Path) -> None:
|
||||
settings = ss.load_system_settings()
|
||||
assert settings.prompt_prefix.enabled is False
|
||||
assert settings.prompt_prefix.prompt_prefix == ""
|
||||
|
||||
|
||||
def test_save_then_load_roundtrips(tmp_runtime_home: Path) -> None:
|
||||
settings = ss.SystemSettings(
|
||||
prompt_prefix=ss.PromptPrefixSettings(
|
||||
enabled=True,
|
||||
switch_label="加入立场前缀",
|
||||
prompt_prefix="你需要站在中方角度",
|
||||
)
|
||||
)
|
||||
ss.save_system_settings(settings)
|
||||
|
||||
file_path = tmp_runtime_home / "system_settings.json"
|
||||
assert file_path.is_file()
|
||||
saved = json.loads(file_path.read_text(encoding="utf-8"))
|
||||
assert saved["prompt_prefix"]["prompt_prefix"] == "你需要站在中方角度"
|
||||
|
||||
reloaded = ss.load_system_settings()
|
||||
assert reloaded.prompt_prefix.enabled is True
|
||||
assert reloaded.prompt_prefix.switch_label == "加入立场前缀"
|
||||
assert reloaded.prompt_prefix.prompt_prefix == "你需要站在中方角度"
|
||||
|
||||
|
||||
def test_prepend_prefix_on_string_content() -> None:
|
||||
graph_input = {"messages": [HumanMessage(content="评价一下中美贸易")]}
|
||||
out = _prepend_prompt_prefix(graph_input, "你需要站在中方角度")
|
||||
msg = out["messages"][0]
|
||||
assert isinstance(msg, HumanMessage)
|
||||
assert msg.content == "你需要站在中方角度\n\n评价一下中美贸易"
|
||||
|
||||
|
||||
def test_prepend_prefix_on_block_content() -> None:
|
||||
graph_input = {
|
||||
"messages": [
|
||||
HumanMessage(content=[{"type": "text", "text": "评价一下中美贸易"}])
|
||||
]
|
||||
}
|
||||
out = _prepend_prompt_prefix(graph_input, "你需要站在中方角度")
|
||||
blocks = out["messages"][0].content
|
||||
assert isinstance(blocks, list)
|
||||
assert blocks[0] == {"type": "text", "text": "你需要站在中方角度"}
|
||||
assert blocks[1] == {"type": "text", "text": "评价一下中美贸易"}
|
||||
|
||||
|
||||
def test_prepend_prefix_only_touches_first_human() -> None:
|
||||
graph_input = {
|
||||
"messages": [
|
||||
AIMessage(content="hi"),
|
||||
HumanMessage(content="q1"),
|
||||
HumanMessage(content="q2"),
|
||||
]
|
||||
}
|
||||
out = _prepend_prompt_prefix(graph_input, "PREFIX")
|
||||
# first human is index 1; index 2 must remain untouched
|
||||
assert out["messages"][1].content == "PREFIX\n\nq1"
|
||||
assert out["messages"][2].content == "q2"
|
||||
|
||||
|
||||
def test_prepend_prefix_no_messages_is_noop() -> None:
|
||||
assert _prepend_prompt_prefix({"messages": []}, "X") == {"messages": []}
|
||||
assert _prepend_prompt_prefix({}, "X") == {}
|
||||
|
||||
|
||||
def test_opted_in_via_context() -> None:
|
||||
body = SimpleNamespace(context={"prompt_prefix_enabled": True}, metadata=None)
|
||||
assert _prompt_prefix_opted_in(body) is True
|
||||
|
||||
|
||||
def test_opted_in_via_metadata() -> None:
|
||||
body = SimpleNamespace(context=None, metadata={"prompt_prefix_enabled": True})
|
||||
assert _prompt_prefix_opted_in(body) is True
|
||||
|
||||
|
||||
def test_not_opted_in_by_default() -> None:
|
||||
body = SimpleNamespace(context=None, metadata=None)
|
||||
assert _prompt_prefix_opted_in(body) is False
|
||||
body2 = SimpleNamespace(context={"mode": "ultra"}, metadata={})
|
||||
assert _prompt_prefix_opted_in(body2) is False
|
||||
@@ -147,13 +147,23 @@ async def test_subscriber_can_read_runs_and_get_run(store):
|
||||
)
|
||||
assert run is not None
|
||||
|
||||
alice_runs = await service.list_runs("alice", "t1", limit=10)
|
||||
assert alice_runs[0]["can_edit"] is True
|
||||
|
||||
# Bob (subscriber) sees runs on the task.
|
||||
bob_runs = await service.list_runs("bob", "t1", limit=10)
|
||||
assert [r["id"] for r in bob_runs] == ["run-1"]
|
||||
assert bob_runs[0]["can_edit"] is False
|
||||
|
||||
# Bob can fetch the run by id.
|
||||
fetched = await service.get_run("bob", "run-1")
|
||||
assert fetched is not None and fetched["id"] == "run-1"
|
||||
assert fetched["can_edit"] is False
|
||||
|
||||
# Bob can read subscribed records, but cannot delete Alice's record.
|
||||
with pytest.raises(PermissionError):
|
||||
await service.delete_run("bob", "run-1")
|
||||
assert await store.get_run("run-1", "alice") is not None
|
||||
|
||||
# Carol (no subscription) cannot.
|
||||
assert await service.list_runs("carol", "t1", limit=10) == []
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
"""Tests for the username-based login domain handling.
|
||||
|
||||
Verifies that:
|
||||
- The default mail domain is ``cm.com`` (with env-var override still honoured).
|
||||
- ``_normalize_username_local_part`` distinguishes raw-email vs username input.
|
||||
- The login flow falls back to a legacy-domain lookup so the same username
|
||||
keeps mapping to the same account after the default domain changes.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from datetime import UTC, datetime
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
import pytest
|
||||
|
||||
from app.gateway.auth.models import User
|
||||
from app.gateway.auth.repositories.base import UserRepository
|
||||
from app.gateway.routers.auth import (
|
||||
_normalize_username_local_part,
|
||||
_username_to_email,
|
||||
)
|
||||
|
||||
|
||||
class FakeUserRepository(UserRepository):
|
||||
"""In-memory repo just enough to exercise the local-part lookup."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.rows: dict[str, User] = {}
|
||||
self.create_calls: list[User] = []
|
||||
|
||||
async def create_user(self, user: User) -> User:
|
||||
for existing in self.rows.values():
|
||||
if existing.email == user.email:
|
||||
raise ValueError(f"Email already registered: {user.email}")
|
||||
u = User(
|
||||
id=user.id or uuid4(),
|
||||
email=user.email,
|
||||
password_hash=user.password_hash,
|
||||
system_role=user.system_role,
|
||||
created_at=user.created_at or datetime.now(UTC),
|
||||
oauth_provider=user.oauth_provider,
|
||||
oauth_id=user.oauth_id,
|
||||
needs_setup=user.needs_setup,
|
||||
token_version=user.token_version,
|
||||
)
|
||||
self.rows[str(u.id)] = u
|
||||
self.create_calls.append(u)
|
||||
return u
|
||||
|
||||
async def get_user_by_id(self, user_id: str) -> User | None:
|
||||
return self.rows.get(user_id)
|
||||
|
||||
async def get_user_by_email(self, email: str) -> User | None:
|
||||
for user in self.rows.values():
|
||||
if user.email == email:
|
||||
return user
|
||||
return None
|
||||
|
||||
async def find_user_by_email_local_part(self, local_part: str) -> User | None:
|
||||
prefix = f"{(local_part or '').strip().lower()}@"
|
||||
candidates = [u for u in self.rows.values() if u.email.lower().startswith(prefix)]
|
||||
candidates.sort(key=lambda u: u.created_at)
|
||||
return candidates[0] if candidates else None
|
||||
|
||||
async def update_user(self, user: User) -> User:
|
||||
self.rows[str(user.id)] = user
|
||||
return user
|
||||
|
||||
async def count_users(self) -> int:
|
||||
return len(self.rows)
|
||||
|
||||
async def count_admin_users(self) -> int:
|
||||
return sum(1 for u in self.rows.values() if u.system_role == "admin")
|
||||
|
||||
async def get_user_by_oauth(self, provider: str, oauth_id: str) -> User | None:
|
||||
return None
|
||||
|
||||
|
||||
def _seed(repo: FakeUserRepository, email: str, *, role: str = "user") -> User:
|
||||
user = User(
|
||||
id=uuid4(),
|
||||
email=email,
|
||||
password_hash=None,
|
||||
system_role=role, # type: ignore[arg-type]
|
||||
created_at=datetime.now(UTC),
|
||||
needs_setup=False,
|
||||
)
|
||||
repo.rows[str(user.id)] = user
|
||||
return user
|
||||
|
||||
|
||||
def test_normalize_returns_none_for_explicit_email() -> None:
|
||||
assert _normalize_username_local_part("alice@old.com") is None
|
||||
|
||||
|
||||
def test_normalize_lowercases_and_keeps_safe_chars() -> None:
|
||||
assert _normalize_username_local_part("Alice_Smith.1") == "alice_smith.1"
|
||||
|
||||
|
||||
def test_normalize_strips_unsafe_chars() -> None:
|
||||
assert _normalize_username_local_part("hi there!") == "hi-there"
|
||||
|
||||
|
||||
def test_username_to_email_uses_cm_com_by_default(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.delenv("DEER_FLOW_USERNAME_LOGIN_DOMAIN", raising=False)
|
||||
assert _username_to_email("alice") == "alice@cm.com"
|
||||
|
||||
|
||||
def test_username_to_email_honours_env_override(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setenv("DEER_FLOW_USERNAME_LOGIN_DOMAIN", "example.org")
|
||||
assert _username_to_email("alice") == "alice@example.org"
|
||||
|
||||
|
||||
def test_username_to_email_passes_explicit_email_through() -> None:
|
||||
assert _username_to_email("bob@legacy.io") == "bob@legacy.io"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_local_part_lookup_finds_legacy_domain_user() -> None:
|
||||
"""Same username under legacy domain should match the new lookup."""
|
||||
repo = FakeUserRepository()
|
||||
_seed(repo, "alice@local.deerflow")
|
||||
legacy = await repo.find_user_by_email_local_part("alice")
|
||||
assert legacy is not None
|
||||
assert legacy.email == "alice@local.deerflow"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_local_part_lookup_returns_oldest_when_collision() -> None:
|
||||
"""When the same local part exists under multiple domains, prefer the
|
||||
oldest row so re-logins land on the original account."""
|
||||
repo = FakeUserRepository()
|
||||
older = _seed(repo, "alice@local.deerflow")
|
||||
older.created_at = datetime(2024, 1, 1, tzinfo=UTC)
|
||||
newer = _seed(repo, "alice@cm.com")
|
||||
newer.created_at = datetime(2026, 1, 1, tzinfo=UTC)
|
||||
match = await repo.find_user_by_email_local_part("alice")
|
||||
assert match is not None
|
||||
assert match.email == "alice@local.deerflow"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_local_part_lookup_misses_unknown_username() -> None:
|
||||
repo = FakeUserRepository()
|
||||
_seed(repo, "alice@cm.com")
|
||||
assert await repo.find_user_by_email_local_part("bob") is None
|
||||
Reference in new issue
Block a user