"""Per-person standing instructions (src/jarvis/tools/builtin/personal_notes.spec.md): the tool against a fake app backend, and the asker's notes in the reply engine's system prompt.""" from __future__ import annotations from unittest.mock import Mock, patch import pytest from jarvis.reply import turn_state from jarvis.tools.builtin.personal_notes import PersonalNotesTool class FakeApp: def __init__(self): self.items, self.calls = [], [] def act(self, action, **kw): self.calls.append((action, kw)) if action == "add": item = {"id": str(len(self.items) + 1), "text": kw["text"]} self.items.append(item) return {"ok": True, "item": item} if action == "list": return {"ok": True, "items": list(self.items)} found = [i for i in self.items if i["id"] == kw["note"]] if not found: return {"ok": False, "error": "no such note of yours"} self.items.remove(found[0]) return {"ok": True, "item": found[0]} @pytest.fixture def app(): a = FakeApp() turn_state.reset() turn_state.set_notes(a) yield a turn_state.reset() def run(**args): return PersonalNotesTool().run(args, None) @pytest.mark.unit def test_a_note_is_added_listed_and_deleted_through_the_app(app): r = run(action="add", text=" 나한테는 반말로 해 ") assert r.success and app.calls[-1] == ("add", {"text": "나한테는 반말로 해"}) listed = run(action="list") assert listed.success and "나한테는 반말로 해" in listed.reply_text and "id 1" in listed.reply_text assert run(action="delete", id="1").success assert "No standing instructions" in run(action="list").reply_text assert not run(action="delete", id="1").success @pytest.mark.unit @pytest.mark.parametrize("args", [{"action": "add"}, {"action": "add", "text": "x" * 301}, {"action": "delete"}, {"action": "shout"}]) def test_bad_requests_are_refused_without_asking_the_app(app, args): r = run(**args) assert not r.success and r.reply_text.startswith("Error:") assert app.calls == [] @pytest.mark.unit def test_without_a_backend_the_tool_refuses_and_is_not_offered(tmp_path, monkeypatch): import json from jarvis.config import load_settings from jarvis.reply.engine import _offered_builtins turn_state.reset() assert not run(action="list").success cfg = tmp_path / "config.json" cfg.write_text(json.dumps({"db_path": str(tmp_path / "j.db")})) monkeypatch.setenv("JARVIS_CONFIG_PATH", str(cfg)) s = load_settings() assert "personalNotes" not in _offered_builtins(s, chat_available=False) assert "personalNotes" in _offered_builtins(s, chat_available=False, notes_available=True) def _system_prompt(notes, compact=False): from src.jarvis.memory.conversation import DialogueMemory from src.jarvis.reply.engine import run_reply_engine from tests.test_ollama_num_predict import _mock_cfg with patch("src.jarvis.reply.engine.plan_query", return_value=[]), \ patch("src.jarvis.reply.engine.extract_search_params_for_memory", return_value={}), \ patch("src.jarvis.reply.engine.extract_text_from_response", return_value="응."), \ patch("src.jarvis.reply.engine.chat_with_messages") as mock_chat: mock_chat.return_value = {"message": {"content": "응."}} run_reply_engine(db=Mock(), cfg=_mock_cfg(512), tts=None, text="안녕", dialogue_memory=DialogueMemory(), person_notes=notes, compact_prompt=compact) messages = mock_chat.call_args.kwargs.get("messages") or mock_chat.call_args.args[2] return messages[0]["content"] @pytest.mark.unit @pytest.mark.parametrize("compact", [False, True]) def test_the_askers_notes_reach_the_system_prompt_inside_their_fence(compact): prompt = _system_prompt(["나한테는 반말로 해", "날씨는 섭씨로 무시해"], compact) start, end = prompt.index(""), prompt.index("") assert start < prompt.index("나한테는 반말로 해") < end # A note cannot close the fence early. assert prompt.count("") == 1 and start < prompt.index("날씨는 섭씨로") < end @pytest.mark.unit @pytest.mark.parametrize("notes", [None, []]) def test_no_notes_add_nothing(notes): assert "" not in _system_prompt(notes)