"""E31 S1: the welcome message is rendered once, when the account is created, in the language of that day. An account that switched language afterwards kept reading it in the old one (the owner's: English under a Hungarian UI). It is served in the reader's language now; any other system message stays as stored. Needs the DB.""" from fastapi.testclient import TestClient import app.state as state from app.auth import current_user from app.db import get_db from app.main import app from app.models import Message, User from app.routes.messages import WELCOME def _client(db, user, monkeypatch, override_dep) -> TestClient: monkeypatch.setattr(state, "_configured_cache", True) override_dep(get_db, db.info["request"]) override_dep(current_user, lambda: user) return TestClient(app) def _user_with_english_welcome(db) -> User: """An account whose welcome was written in English, then switched to Hungarian.""" user = User(email="welcome-hu@example.com", preferences={"language": "hu"}) db.add(user) db.flush() db.add(Message(kind="system", sender_id=None, recipient_id=user.id, body=WELCOME["en"])) db.commit() return user def test_the_welcome_is_read_in_the_readers_language(db, monkeypatch, override_dep): user = _user_with_english_welcome(db) client = _client(db, user, monkeypatch, override_dep) thread = client.get("/api/messages/conversations/0").json()["items"] assert [m["body"] for m in thread] == [WELCOME["hu"]] convs = client.get("/api/messages/conversations").json()["items"] assert [c["last_message"]["body"] for c in convs if c["partner"]["id"] == 0] == [WELCOME["hu"]] def test_another_system_message_is_served_as_stored(db, monkeypatch, override_dep): user = _user_with_english_welcome(db) db.add( Message(kind="system", sender_id=None, recipient_id=user.id, body="Maintenance tonight.") ) db.commit() client = _client(db, user, monkeypatch, override_dep) bodies = [m["body"] for m in client.get("/api/messages/conversations/0").json()["items"]] assert bodies == [WELCOME["hu"], "Maintenance tonight."]