"""The schedule tool (src/jarvis/tools/builtin/schedule.spec.md) against a fake app backend.""" from __future__ import annotations import time from datetime import datetime, timedelta import pytest from jarvis.reply import turn_state from jarvis.tools.builtin.schedule import ScheduleTool class FakeApp: """Keeps schedules as the app would for one person: add returns the item, list all, delete one.""" 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), "next": kw["at"], **kw} 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["schedule"]] if not found: return {"ok": False, "error": "no such schedule 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_schedule(a) yield a turn_state.reset() def run(**args): return ScheduleTool().run(args, None) @pytest.mark.unit def test_a_timer_in_minutes_is_a_reminder_that_many_minutes_from_now(app): before = time.time() r = run(action="add", text="라면 다 됐어요", in_minutes=5) assert r.success action, kw = app.calls[-1] assert action == "add" and kw["kind"] == "remind" and kw["repeat"] == "none" and kw["text"] == "라면 다 됐어요" assert before + 300 <= kw["at"] <= time.time() + 300 assert "id 1" in r.reply_text @pytest.mark.unit def test_a_local_time_and_a_repeat_reach_the_app(app): at = (datetime.now() + timedelta(days=1)).replace(hour=9, minute=0, second=0, microsecond=0) r = run(action="add_task", kind="task", text="날씨 알려 줘", at=at.strftime("%Y-%m-%d %H:%M"), repeat="daily") assert r.success _, kw = app.calls[-1] assert kw["kind"] == "task" and kw["repeat"] == "daily" and kw["at"] == at.timestamp() assert "repeats daily" in r.reply_text @pytest.mark.unit @pytest.mark.parametrize("args", [ {"text": "x"}, # no time {"text": "x", "at": "tomorrow 7"}, # not a time {"text": "x", "in_minutes": 0}, {"text": "", "in_minutes": 5}, # nothing to say {"text": "x" * 501, "in_minutes": 5}, {"text": "x", "at": "2000-01-01 07:00"}, # passed, and not repeating ]) def test_a_bad_add_is_refused_without_asking_the_app(app, args): r = run(action="add", **args) assert not r.success and r.reply_text.startswith("Error:") assert app.calls == [] @pytest.mark.unit def test_list_and_delete_go_through_the_app(app): run(action="add", text="회의", in_minutes=60) 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 run(action="list").reply_text == "Nothing is scheduled for this person." gone = run(action="delete", id="1") assert not gone.success and "no such schedule" in gone.reply_text @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() r = run(action="add", text="x", in_minutes=5) assert not r.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 "schedule" not in _offered_builtins(s, chat_available=False) assert "schedule" in _offered_builtins(s, chat_available=False, schedule_available=True)