"""Meals belong to the person who logged them (v2: several people share one assistant). The v2 brain sets the asker for the turn (jarvis.reply.turn_state.set_user). logMeal stores the meal under that person, fetchMeals and deleteMeal only see that person's meals. Without a user (v1, one person) every meal is the single user's, as before. A v1 database without the column opens unchanged and keeps its meals. """ import json import sqlite3 from unittest.mock import patch import pytest from jarvis.memory.db import Database from jarvis.reply import turn_state from jarvis.tools.builtin.nutrition.delete_meal import DeleteMealTool from jarvis.tools.builtin.nutrition.fetch_meals import FetchMealsTool from jarvis.tools.builtin.nutrition.log_meal import LogMealTool class Cfg: ollama_base_url = "http://x" ollama_chat_model = "m" llm_chat_timeout_sec = 5 use_stdin = False voice_debug = True def _meal_json(desc): return json.dumps({"description": desc, "calories_kcal": 500, "protein_g": 20, "carbs_g": 50, "fat_g": 10, "confidence": 0.8}) def run(tool, db, args, user): turn_state.reset() turn_state.set_user(user) try: return tool.execute(db=db, cfg=Cfg(), tool_args=args, system_prompt="", original_prompt="", redacted_text=(args or {}).get("meal", ""), max_retries=0, user_print=lambda m: None) finally: turn_state.reset() @pytest.fixture def db(tmp_path): d = Database(str(tmp_path / "j.db"), None) yield d d.close() def log(db, user, desc): with patch("jarvis.tools.builtin.nutrition.log_meal.call_llm_direct", return_value=_meal_json(desc)): return run(LogMealTool(), db, {"meal": desc}, user) def test_each_person_sees_only_their_own_meals(db): assert log(db, "alice", "bibimbap").success assert log(db, "bob", "pizza").success alice = run(FetchMealsTool(), db, {}, "alice").reply_text bob = run(FetchMealsTool(), db, {}, "bob").reply_text assert "bibimbap" in alice and "pizza" not in alice assert "pizza" in bob and "bibimbap" not in bob def test_nobody_deletes_someone_elses_meal(db): log(db, "alice", "bibimbap") meal_id = db.conn.execute("SELECT id FROM meals").fetchone()[0] assert not run(DeleteMealTool(), db, {"id": meal_id}, "bob").success assert "bibimbap" in run(FetchMealsTool(), db, {}, "alice").reply_text assert run(DeleteMealTool(), db, {"id": meal_id}, "alice").success assert "bibimbap" not in (run(FetchMealsTool(), db, {}, "alice").reply_text or "") def test_without_a_user_it_is_the_single_v1_user(db): log(db, None, "toast") assert "toast" in run(FetchMealsTool(), db, {}, None).reply_text assert "toast" not in (run(FetchMealsTool(), db, {}, "alice").reply_text or "") def test_a_v1_database_opens_and_keeps_its_meals(tmp_path): path = str(tmp_path / "old.db") con = sqlite3.connect(path) con.execute("CREATE TABLE meals (id INTEGER PRIMARY KEY, ts_utc TEXT NOT NULL, source_app TEXT NOT NULL, " "description TEXT NOT NULL, calories_kcal REAL, protein_g REAL, carbs_g REAL, fat_g REAL, " "fiber_g REAL, sugar_g REAL, sodium_mg REAL, potassium_mg REAL, micros_json TEXT, confidence REAL)") con.execute("INSERT INTO meals(ts_utc, source_app, description, calories_kcal) VALUES " "(strftime('%Y-%m-%dT%H:%M:%f+00:00','now'), 'x', 'old porridge', 300)") con.commit() con.close() d = Database(path, None) try: assert "old porridge" in run(FetchMealsTool(), d, {}, None).reply_text log(d, "alice", "new salad") assert "new salad" in run(FetchMealsTool(), d, {}, "alice").reply_text finally: d.close()