File size: 25,399 Bytes
210ef30
 
0e9b1cb
 
210ef30
 
 
 
 
 
 
 
 
 
0e9b1cb
210ef30
 
 
 
 
 
 
 
 
0e9b1cb
210ef30
 
0e9b1cb
210ef30
 
 
 
0e9b1cb
 
 
 
 
 
 
 
 
210ef30
0e9b1cb
 
210ef30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e9b1cb
210ef30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
"""The brain over HTTP: auth on every route, visibility, tools, memory, voice, push, cron, data.

Real access-token verification (tokens minted by the real issuer), the in-memory store and the
fake provider. Nothing here runs in dev-auth mode, so every 401/403 below is the production path.
"""

import time
from datetime import UTC, datetime, timedelta

import pytest
from fastapi.testclient import TestClient

from brain import config
from brain.app import create_app
from brain.auth import AuthConfig, User, Verifier, issue_access_token
from brain.deps import Deps
from brain.llm.base import ToolCall
from brain.llm.fake import FakeLlm
from brain.persona.types import Scope
from brain.push.sender import RecordingSender, VapidConfig
from brain.services.chat import ChatService
from brain.store.memory import MemoryStore
from brain.tools.catalog import default_registry

SECRET = "api-test-secret-long-enough-for-signing-tokens-32b+"
OWNER = "karnak@example.com"
USER_ID = "11111111-1111-1111-1111-111111111111"
CLIENT_ID = "test-web-client.apps.googleusercontent.com"
ORIGIN = "https://prova.web.app"
CRON = {"x-prova-cron-secret": "cron-secret"}


def auth_config() -> AuthConfig:
    return AuthConfig(
        secret=SECRET,
        google_client_ids=(CLIENT_ID,),
        allowed_emails=(OWNER,),
        cron_secret="cron-secret",
    )


def token(email: str = OWNER, sub: str = USER_ID) -> str:
    user = User(id=sub, email=email)
    return issue_access_token(auth_config(), user, session_id="test-session", now=time.time())


AUTH = {"Authorization": f"Bearer {token()}"}


class Clock:
    """A settable clock, so gates that depend on the hour can be tested at any hour."""

    def __init__(self, at: datetime) -> None:
        self.at = at

    def __call__(self) -> datetime:
        return self.at


@pytest.fixture
def env():
    store = MemoryStore()
    llm = FakeLlm()
    sender = RecordingSender()
    # Monday 21 Sep 2026, 11:00 IST
    clock = Clock(datetime(2026, 9, 21, 5, 30, tzinfo=UTC))
    auth = auth_config()
    settings = config.Settings()
    deps = Deps(
        settings=settings,
        store=store,
        llm=llm,
        chat=ChatService(store, llm, default_registry(), settings, clock=clock),
        auth=auth,
        verifier=Verifier(auth),
        sender=sender,
        vapid=VapidConfig("pub", "priv", "mailto:x@example.com"),
        allowed_origins=(ORIGIN,),
    )
    with TestClient(create_app(deps)) as client:
        yield client, store, llm, sender, clock


def send(client, text: str, mode: str = "normal", **extra):
    return client.post("/chat", json={"text": text, "mode": mode, **extra}, headers=AUTH)


# --- Auth --------------------------------------------------------------------


def test_health_needs_no_token(env):
    client, *_ = env
    assert client.get("/health").json()["ok"] is True


def test_the_root_page_shows_the_brain_is_up_without_a_token(env):
    client, *_ = env
    response = client.get("/")
    assert response.status_code == 200
    assert response.headers["content-type"].startswith("text/html")
    assert "Backend running" in response.text
    assert "<img" in response.text


@pytest.mark.parametrize(
    ("method", "path"),
    [
        ("post", "/chat"),
        ("get", "/messages"),
        ("get", "/memory"),
        ("get", "/settings"),
        ("post", "/tts"),
        ("get", "/data/export"),
    ],
)
def test_every_route_rejects_a_missing_token(env, method, path):
    client, *_ = env
    response = getattr(client, method)(path)
    assert response.status_code == 401


def test_a_valid_google_account_that_is_not_his_is_forbidden(env):
    client, *_ = env
    response = client.get(
        "/settings", headers={"Authorization": f"Bearer {token('someone@else.com')}"}
    )
    assert response.status_code == 403
    # The web app shows "Private — Prova belongs to someone else" on this code.
    assert response.json()["detail"]["code"] == "not_allowed"


def test_the_user_id_comes_from_the_token_never_the_body(env):
    client, store, *_ = env
    client.post(
        "/chat", json={"text": "hello there", "mode": "normal", "user_id": "attacker"}, headers=AUTH
    )
    assert {row["user_id"] for row in store.messages} == {USER_ID}


# --- CORS --------------------------------------------------------------------


def test_cors_admits_the_exact_origin_only(env):
    client, *_ = env
    ok = client.options(
        "/chat", headers={"Origin": ORIGIN, "Access-Control-Request-Method": "POST"}
    )
    assert ok.headers.get("access-control-allow-origin") == ORIGIN
    bad = client.options(
        "/chat",
        headers={"Origin": "https://evil.example.com", "Access-Control-Request-Method": "POST"},
    )
    assert "access-control-allow-origin" not in bad.headers


# --- Chat and visibility -----------------------------------------------------


def test_a_turn_stores_both_messages_in_the_mode_they_were_sent(env):
    client, store, llm, *_ = env
    llm.queue("Good morning, Karnak. Three items today.")
    body = send(client, "what's on my plate today?").json()
    assert body["message"]["content"] == "Good morning, Karnak. Three items today."
    assert [(m["role"], m["mode"]) for m in store.messages] == [
        ("user", "normal"),
        ("assistant", "normal"),
    ]


def test_normal_mode_history_never_contains_a_gf_message(env):
    client, _, llm, *_ = env
    llm.queue("Uff, miss korchi go", "Noted.")
    send(client, "miss korchi", mode="gf")
    send(client, "status of the report?", mode="normal")

    normal = client.get("/messages?mode=normal", headers=AUTH).json()
    gf = client.get("/messages?mode=gf", headers=AUTH).json()
    assert {m["mode"] for m in normal} == {"normal"}
    assert len(gf) == 4


def test_a_gf_message_is_never_sent_to_the_model_on_a_normal_turn(env):
    client, _, llm, *_ = env
    llm.queue("Tomake chara ghum ashe na", "Understood.")
    send(client, "secret gf line about the balcony", mode="gf")
    send(client, "normal question", mode="normal")
    contents = str(llm.requests[-1]["contents"])
    assert "balcony" not in contents


PRIVATE_QUERY = "what do you call me when I am tired"


def seed_private_memory(store, llm):
    """A private fact embedded from the exact query text, so retrieval scores it at 1.0.

    It has to be a certain hit: the privacy test asserts the fact is ABSENT from Normal mode,
    and a fact that retrieval never found would be absent for the wrong reason.
    """
    import asyncio

    store.memories.append(
        {
            "id": "m1",
            "user_id": USER_ID,
            "scope": Scope.PRIVATE.value,
            "content": "She calls him babu when he is tired.",
            "embedding": asyncio.run(llm.embed(PRIVATE_QUERY)),
            "is_manual": False,
            "use_count": 0,
            "created_at": datetime.now(UTC),
            "last_used_at": datetime.now(UTC),
        }
    )


def test_a_private_memory_never_reaches_a_normal_prompt(env):
    client, store, llm, *_ = env
    seed_private_memory(store, llm)
    llm.queue("Noted.")
    send(client, PRIVATE_QUERY, mode="normal")
    assert "babu when he is tired" not in llm.prompts[-1]


def test_a_tool_call_comes_back_as_a_client_action(env):
    client, _, llm, *_ = env
    llm.queue(
        ToolCall("maps_directions", {"destination": "Digha", "origin": "Kolkata"}),
        "Done. The route is open.",
    )
    body = send(client, "Kolkata theke Digha route dekhao").json()
    assert body["clientActions"][0]["type"] == "open_url"
    assert "maps/dir" in body["clientActions"][0]["url"]
    assert body["activity"] == ["maps_directions: ok"]


def test_the_normal_filter_runs_on_the_real_endpoint(env):
    client, _, llm, *_ = env
    llm.queue("Babu, the report is ready 😘", "Babu, ready 😘")
    body = send(client, "report?").json()
    assert "babu" not in body["message"]["content"].lower()
    assert any("normal filter" in line for line in body["activity"])


def test_an_empty_message_is_rejected(env):
    client, *_ = env
    assert send(client, "   ").status_code == 422


def test_too_many_attachments_is_a_clear_error(env):
    client, *_ = env
    files = [{"name": f"f{i}.php", "content": "x"} for i in range(21)]
    response = send(client, "review these", attachments=files)
    assert response.status_code == 422
    assert response.json()["detail"]["code"] == "attachment"


def test_attached_code_makes_the_turn_technical(env):
    client, _, llm, *_ = env
    llm.queue("Line 3 calls wc_get_order() without the id.")
    send(
        client,
        "why does this fail?",
        attachments=[{"name": "hook.php", "content": "<?php wc_get_order();"}],
    )
    assert "THIS TURN IS TECHNICAL" in llm.prompts[-1]
    assert "FILE: hook.php" in str(llm.requests[-1]["contents"])


def test_usage_is_counted_per_day(env):
    client, _, llm, *_ = env
    llm.queue("ok")
    send(client, "hello there Prova")
    usage = client.get("/usage", headers=AUTH).json()
    assert usage["day"] == "2026-09-21"
    assert usage["total"] >= 1


# --- Memory ------------------------------------------------------------------


def test_memory_extraction_runs_on_every_third_turn(env):
    client, store, llm, *_ = env
    llm.queue("one", "two", "three", "shared|His sister's name is Ria.")
    for text in ("first message here", "second message here", "my sister is called Ria"):
        send(client, text)
    assert [m["content"] for m in store.memories] == ["His sister's name is Ria."]
    assert store.memories[0]["scope"] == "shared"


def test_normal_mode_memory_screen_lists_shared_facts_only(env):
    client, *_ = env
    client.post(
        "/memory",
        json={"content": "His sister's name is Ria.", "scope": "shared", "mode": "gf"},
        headers=AUTH,
    )
    client.post(
        "/memory",
        json={"content": "They watched the rain together.", "scope": "private", "mode": "gf"},
        headers=AUTH,
    )
    normal = client.get("/memory?mode=normal", headers=AUTH).json()
    gf = client.get("/memory?mode=gf", headers=AUTH).json()
    assert [m["content"] for m in normal] == ["His sister's name is Ria."]
    assert len(gf) == 2


def test_a_fact_added_from_normal_mode_is_always_shared(env):
    client, store, *_ = env
    client.post(
        "/memory",
        json={"content": "His dentist is on Friday.", "scope": "private", "mode": "normal"},
        headers=AUTH,
    )
    assert store.memories[0]["scope"] == "shared"


def test_normal_mode_cannot_make_a_fact_shared(env):
    client, store, *_ = env
    made = client.post(
        "/memory",
        json={"content": "A private thing here.", "scope": "private", "mode": "gf"},
        headers=AUTH,
    ).json()
    memory_id = made["memory"]["id"]
    blocked = client.patch(
        f"/memory/{memory_id}", json={"scope": "shared", "mode": "normal"}, headers=AUTH
    )
    assert blocked.status_code == 403
    assert (
        client.patch(
            f"/memory/{memory_id}", json={"scope": "shared", "mode": "gf"}, headers=AUTH
        ).status_code
        == 200
    )


def test_clearing_from_normal_mode_leaves_private_facts_alone(env):
    client, store, *_ = env
    client.post(
        "/memory",
        json={"content": "A shared fact about work.", "scope": "shared", "mode": "gf"},
        headers=AUTH,
    )
    client.post(
        "/memory",
        json={"content": "A private fact about us.", "scope": "private", "mode": "gf"},
        headers=AUTH,
    )
    assert client.delete("/memory?mode=normal", headers=AUTH).json() == {"deleted": 1}
    assert [m["scope"] for m in store.memories] == ["private"]


# --- Settings and character --------------------------------------------------


def test_settings_merge_over_the_defaults(env):
    client, *_ = env
    defaults = client.get("/settings", headers=AUTH).json()
    assert defaults["gfRequiresLock"] is True  # master prompt 8: default on
    assert defaults["privacy"]["normalSeesPrivate"] is False  # master prompt 4.5
    patched = client.patch(
        "/settings", json={"privacy": {"blurOnHidden": False}}, headers=AUTH
    ).json()
    assert patched["privacy"] == {
        "normalSeesPrivate": False,
        "hideNotifContent": True,
        "blurOnHidden": False,
    }


def test_an_unknown_mood_is_refused(env):
    client, *_ = env
    assert client.patch("/settings", json={"mood": "furious"}, headers=AUTH).status_code == 422


def test_gf_persona_text_is_only_returned_in_gf_mode(env):
    client, *_ = env
    normal = client.get("/character?mode=normal", headers=AUTH).json()
    gf = client.get("/character?mode=gf", headers=AUTH).json()
    assert "relationship_layer" not in normal and "system_base" not in normal
    assert "been together a long time" in gf["relationship_layer"]


# --- Voice -------------------------------------------------------------------


def test_tts_returns_audio(env):
    client, *_ = env
    # Gemini speech is opt-in (its free tier is 10 requests a day); auto has no Groq here.
    client.patch("/settings", json={"voice": {"engine": "gemini"}}, headers=AUTH)
    response = client.post(
        "/tts", json={"text": "The report is ready.", "mode": "normal"}, headers=AUTH
    )
    assert response.status_code == 200
    assert response.headers["content-type"] == "audio/wav"


def test_auto_voice_has_no_gemini_speech_so_the_client_uses_the_device_voice(env):
    client, *_ = env
    response = client.post(
        "/tts", json={"text": "The report is ready.", "mode": "normal"}, headers=AUTH
    )
    assert response.status_code == 503
    assert response.json()["detail"]["code"] == "tts_unavailable"


def test_gf_tts_transliterates_once_and_caches_it(env):
    client, store, llm, *_ = env
    client.patch("/settings", json={"voice": {"engine": "gemini"}}, headers=AUTH)
    llm.queue("Kemon acho?", "কেমন আছো?")
    send(client, "hi there", mode="gf")
    message_id = store.messages[-1]["id"]
    client.post("/tts", json={"messageId": message_id, "mode": "gf"}, headers=AUTH)
    assert store.messages[-1]["bengali_text"] == "কেমন আছো?"


def test_a_normal_device_cannot_speak_a_gf_message_by_id(env):
    client, store, llm, *_ = env
    llm.queue("private reply")
    send(client, "gf line here", mode="gf")
    gf_id = store.messages[-1]["id"]
    assert (
        client.post("/tts", json={"messageId": gf_id, "mode": "normal"}, headers=AUTH).status_code
        == 404
    )


def test_stt_fix_romanises_bengali_script_only(env):
    client, _, llm, *_ = env
    llm.queue("kothay chiley etokkhon")
    bengali = client.post("/stt-fix", json={"text": "কোথায় ছিলে এতক্ষণ"}, headers=AUTH).json()
    assert bengali == {"latin": "kothay chiley etokkhon", "bengali": "কোথায় ছিলে এতক্ষণ"}
    latin = client.post("/stt-fix", json={"text": "ki korcho"}, headers=AUTH).json()
    assert latin == {"latin": "ki korcho", "bengali": None}


# --- Push, proactive, reminders ----------------------------------------------


SUBSCRIPTION = {
    "endpoint": "https://push.example.com/abc",
    "keys": {"p256dh": "p", "auth": "a"},
    "deviceLabel": "Phone",
}


def test_subscribing_twice_does_not_duplicate_the_device(env):
    client, store, *_ = env
    client.post("/push/subscribe", json=SUBSCRIPTION, headers=AUTH)
    client.post("/push/subscribe", json=SUBSCRIPTION, headers=AUTH)
    listed = client.get("/push/subscriptions", headers=AUTH).json()
    assert len(listed) == 1
    assert "p256dh" not in listed[0]  # keys never leave the brain


def test_the_test_notification_is_sent(env):
    client, _, _, sender, _ = env
    client.post("/push/subscribe", json=SUBSCRIPTION, headers=AUTH)
    assert client.post("/push/test", headers=AUTH).json()["sent"] == 1
    assert sender.sent[0][1]["tag"] == "test"


def test_cron_routes_need_the_shared_secret(env):
    client, *_ = env
    assert client.post("/proactive").status_code == 401
    assert client.post("/reminders", headers={"x-prova-cron-secret": "wrong"}).status_code == 401
    assert client.post("/reminders", headers=CRON).status_code == 200


def test_a_due_reminder_is_pushed_once_in_normal_wording(env):
    client, store, llm, sender, clock = env
    client.post("/push/subscribe", json=SUBSCRIPTION, headers=AUTH)
    llm.queue(
        ToolCall("set_reminder", {"text": "Client call", "when_iso": "2026-09-21T14:45:00"}),
        "Done.",
    )
    send(client, "remind me about the client call at 2:45")

    clock.at = datetime(2026, 9, 21, 9, 16, tzinfo=UTC)  # 14:46 IST
    assert client.post("/reminders", headers=CRON).json()["sent"] == 1
    assert client.post("/reminders", headers=CRON).json()["sent"] == 0  # never twice
    body = sender.sent[-1][1]["body"]
    assert body.startswith("Reminder: Client call")


def test_a_reminder_set_in_gf_mode_has_a_neutral_lock_screen_body(env):
    client, store, llm, sender, clock = env
    client.post("/push/subscribe", json=SUBSCRIPTION, headers=AUTH)
    llm.queue(
        ToolCall("set_reminder", {"text": "anniversary gift", "when_iso": "2026-09-21T12:00:00"}),
        "Mone rakhbo 😌",
    )
    send(client, "remind me about the gift at noon", mode="gf")
    clock.at = datetime(2026, 9, 21, 6, 31, tzinfo=UTC)  # 12:01 IST
    client.post("/reminders", headers=CRON)
    assert "anniversary" not in sender.sent[-1][1]["body"]


def test_an_expired_push_subscription_is_removed(env):
    client, store, llm, sender, clock = env
    client.post("/push/subscribe", json=SUBSCRIPTION, headers=AUTH)
    sender.status = 410
    client.post("/push/test", headers=AUTH)
    assert store.subscriptions == []


def seed_history(
    store, hours_ago: float, clock: Clock, role: str = "user", proactive: bool = False
):
    store.settings.setdefault(USER_ID, {})
    store.messages.append(
        {
            "id": f"h{len(store.messages)}",
            "user_id": USER_ID,
            "mode": "gf",
            "role": role,
            "content": "earlier",
            "is_proactive": proactive,
            "created_at": clock.at - timedelta(hours=hours_ago),
        }
    )


def test_proactive_sends_a_neutral_push_after_a_long_silence(env):
    client, store, llm, sender, clock = env
    client.post("/push/subscribe", json=SUBSCRIPTION, headers=AUTH)
    seed_history(store, hours_ago=6, clock=clock)
    llm.queue("Ki korcho? Lunch hoyeche?")
    report = client.post("/proactive", headers=CRON).json()
    assert report["sent"] == 1
    payload = sender.sent[-1][1]
    assert payload["body"] == "Prova: new message"  # never the GF text on a lock screen
    assert payload["url"] == "/?badge=gf"
    assert store.messages[-1]["is_proactive"] is True


def test_proactive_is_silent_in_quiet_hours(env):
    client, store, llm, sender, clock = env
    clock.at = datetime(2026, 9, 20, 22, 30, tzinfo=UTC)  # 04:00 IST Monday
    seed_history(store, hours_ago=8, clock=clock)
    report = client.post("/proactive", headers=CRON).json()
    assert report["sent"] == 0
    assert report["skipped"][USER_ID] == "quiet_hours"


def test_an_unanswered_check_in_is_never_stacked(env):
    client, store, llm, sender, clock = env
    seed_history(store, hours_ago=5.5, clock=clock, role="assistant", proactive=True)
    report = client.post("/proactive", headers=CRON).json()
    assert report["skipped"][USER_ID] == "unanswered_backoff"


# --- Data (P8) ---------------------------------------------------------------


def test_export_then_import_round_trips_memories(env):
    client, store, *_ = env
    client.post(
        "/memory", json={"content": "His sister's name is Ria.", "mode": "gf"}, headers=AUTH
    )
    exported = client.get("/data/export", headers=AUTH).json()
    assert exported["format"] == "prova-export"
    assert "embedding" not in str(exported)

    store.memories.clear()
    result = client.post("/data/import", json=exported, headers=AUTH).json()
    assert result["memories"]["added"] == 1
    assert store.memories[0]["content"] == "His sister's name is Ria."


def test_delete_everything_needs_the_confirmation_word(env):
    client, store, llm, *_ = env
    llm.queue("ok")
    send(client, "hello there Prova")
    assert client.post("/data/delete", json={"confirm": "yes"}, headers=AUTH).status_code == 422
    assert client.post("/data/delete", json={"confirm": "DELETE"}, headers=AUTH).json() == {
        "ok": True
    }
    assert store.messages == []


# --- Positive controls --------------------------------------------------------
# The two privacy tests above assert that something is ABSENT. Absence is also what a broken
# retrieval or a broken history would produce, so each gets a twin proving the same data does
# reach the model in GF mode. Without these, the privacy tests could pass for the wrong reason.


def test_control_the_same_private_memory_does_reach_a_gf_prompt(env):
    client, store, llm, *_ = env
    seed_private_memory(store, llm)
    llm.queue("Hmm 😌")
    send(client, PRIVATE_QUERY, mode="gf")
    assert "babu when he is tired" in llm.prompts[-1]


def test_control_normal_sees_private_lets_it_through_when_he_turns_it_on(env):
    client, store, llm, *_ = env
    seed_private_memory(store, llm)
    client.patch("/settings", json={"privacy": {"normalSeesPrivate": True}}, headers=AUTH)
    llm.queue("Noted.")
    send(client, PRIVATE_QUERY, mode="normal")
    assert "babu when he is tired" in llm.prompts[-1]


def test_control_a_gf_message_does_reach_the_model_on_a_gf_turn(env):
    client, _, llm, *_ = env
    llm.queue("Tomake chara ghum ashe na", "Hmm")
    send(client, "secret gf line about the balcony", mode="gf")
    send(client, "another gf line", mode="gf")
    assert "balcony" in str(llm.requests[-1]["contents"])


def test_fixed_lines_come_from_the_brain_in_the_right_voice(env):
    client, *_ = env
    normal = client.get("/character?mode=normal", headers=AUTH).json()["lines"]
    gf = client.get("/character?mode=gf", headers=AUTH).json()["lines"]
    assert (
        normal["videoCallUnavailable"] == "Video calling isn't available yet. Voice call instead?"
    )
    assert "🙈" in gf["videoCallUnavailable"]


# --- The Gemini / Groq switch over HTTP ---------------------------------------


def test_the_chat_brain_setting_defaults_to_auto_and_rejects_junk(env):
    client, *_ = env
    assert client.get("/settings", headers=AUTH).json()["chatProvider"] == "groq"
    assert (
        client.patch("/settings", json={"chatProvider": "groq"}, headers=AUTH).json()[
            "chatProvider"
        ]
        == "groq"
    )
    assert client.patch("/settings", json={"chatProvider": "nope"}, headers=AUTH).status_code == 422


def test_the_setting_actually_chooses_the_provider(env):
    from brain.llm.router import Router

    client, store, llm, *_ = env
    groq = FakeLlm(["answered by groq"])
    router = Router(llm, groq)
    client.app.state.deps.chat.llm = router
    llm.queue("answered by gemini")

    client.patch("/settings", json={"chatProvider": "groq"}, headers=AUTH)
    assert send(client, "hello there Prova").json()["message"]["content"] == "answered by groq"

    client.patch("/settings", json={"chatProvider": "gemini"}, headers=AUTH)
    assert send(client, "hello again Prova").json()["message"]["content"] == "answered by gemini"


def test_a_photo_always_goes_to_gemini_because_the_backup_cannot_see(env):
    from brain.llm.router import Router

    client, store, llm, *_ = env
    groq = FakeLlm(["groq must not see photos"])
    client.app.state.deps.chat.llm = Router(llm, groq)
    llm.queue("gemini saw it")
    client.patch("/settings", json={"chatProvider": "groq"}, headers=AUTH)
    user = USER_ID
    store.images[f"{user}/pic.jpg"] = (b"\xff\xd8", "image/jpeg")
    body = send(client, "look at this", imagePath=f"{user}/pic.jpg").json()
    assert body["message"]["content"] == "gemini saw it"


def test_a_greeting_is_hers_alone_and_stores_no_user_turn(env):
    client, store, llm, *_ = env
    llm.queue("Good evening, Rudro. Nothing is outstanding today.")
    before = len(store.messages)

    body = client.post(
        "/chat", json={"text": "", "mode": "normal", "greeting": True}, headers=AUTH
    ).json()

    assert body["message"]["content"].startswith("Good evening")
    assert body["userMessage"] is None
    # Exactly one row: hers. An invented user line would be two.
    assert len(store.messages) == before + 1
    # Answering a call is not a check-in: marking it proactive would make the phone notify him
    # about a message he is already looking at.
    assert store.messages[-1]["is_proactive"] is False


def test_an_ordinary_empty_message_is_still_refused(env):
    client, *_ = env
    assert (
        client.post("/chat", json={"text": "  ", "mode": "normal"}, headers=AUTH).status_code == 422
    )


def test_a_greeting_forbids_inventing_his_day_and_does_not_use_the_checkin_prompt(env):
    client, _, llm, *_ = env
    llm.queue("Good evening, Rudro. I'm here.")
    client.post("/chat", json={"text": "", "mode": "normal", "greeting": True}, headers=AUTH)

    prompt = llm.prompts[-1] if hasattr(llm, "prompts") else ""
    if prompt:
        assert "ANSWERING A CALL" in prompt
        assert "MESSAGE HIM FIRST" not in prompt
        assert "invented" in prompt