ops: BroV Ops service for the homelab VM (tasks, subscriptions, routines)
- SQLite: tasks with subtasks and time tracking, subscriptions + payments, routines + history, reminder log. All rules in core.Ops (tested: ops/tests). - brain: free text → JSON actions via the local claude CLI (Haiku, the subscription); only whitelisted operations are applied, nothing is deleted. - Telegram bot (owner only): text and voice (faster-whisper on the VM), /today /tasks /subs /routines /stop, morning digest and daily reminders with Done/Paid/Tomorrow. - HTTP API for BroV (bearer token): agenda, tasks, subs, routines, say, action. - deploy/install.sh: user brovops, venv, systemd; a proxy-only mihomo (127.0.0.1:7890, checksum-verified) so Telegram and Claude work from Russia without a VPN on the VM. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
"""python3 -m unittest discover -s ops/tests (no third-party packages needed)"""
|
||||
import sys, os, threading, unittest
|
||||
from datetime import date, timedelta
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
||||
|
||||
from brovops.core import Ops, OpsError, add_period
|
||||
from brovops.db import connect
|
||||
from brovops import brain, fmt
|
||||
|
||||
|
||||
def fresh():
|
||||
return Ops(connect(":memory:"))
|
||||
|
||||
|
||||
class Periods(unittest.TestCase):
|
||||
def test_month_end_clamps(self):
|
||||
self.assertEqual(add_period(date(2026, 1, 31), "month"), date(2026, 2, 28))
|
||||
self.assertEqual(add_period(date(2026, 12, 15), "month", 2), date(2027, 2, 15))
|
||||
self.assertEqual(add_period(date(2028, 2, 29), "year"), date(2029, 2, 28))
|
||||
|
||||
|
||||
class Tasks(unittest.TestCase):
|
||||
def test_tree_timer_and_done_cascade(self):
|
||||
o = fresh()
|
||||
p = o.add_task("Принтер в Саге", client="saga", due=o.today().isoformat())
|
||||
c = o.add_task("Заказать картридж", parent_id=p["id"])
|
||||
o.start_timer(c["id"])
|
||||
self.assertEqual(o.running_task_id(), c["id"])
|
||||
o.start_timer(p["id"]) # only one timer at a time
|
||||
self.assertEqual(o.running_task_id(), p["id"])
|
||||
tree = o.task_tree()
|
||||
self.assertEqual(tree[0]["children"][0]["title"], "Заказать картридж")
|
||||
o.complete_task(p["id"]) # parent done → subtasks done, timer stops
|
||||
self.assertIsNone(o.running_task_id())
|
||||
self.assertEqual(o.task(c["id"])["status"], "done")
|
||||
self.assertEqual(o.task_tree(), [])
|
||||
|
||||
def test_bad_input(self):
|
||||
o = fresh()
|
||||
with self.assertRaises(OpsError):
|
||||
o.add_task(" ")
|
||||
with self.assertRaises(OpsError):
|
||||
o.add_task("x", due="15 ноября")
|
||||
with self.assertRaises(OpsError):
|
||||
o.add_task("x", parent_id=999)
|
||||
|
||||
|
||||
class Subs(unittest.TestCase):
|
||||
def test_pay_moves_next_date_past_today(self):
|
||||
o = fresh()
|
||||
long_ago = (o.today() - timedelta(days=70)).isoformat()
|
||||
s = o.add_sub("Хостинг", 900, long_ago)
|
||||
s = o.pay_sub(s["id"])
|
||||
self.assertGreater(date.fromisoformat(s["next_date"]), o.today())
|
||||
self.assertEqual(o.con.execute("SELECT COUNT(*) FROM payments").fetchone()[0], 1)
|
||||
|
||||
def test_monthly_cost(self):
|
||||
o = fresh()
|
||||
o.add_sub("Домен", 1200, "2026-11-15", period="year")
|
||||
o.add_sub("VPS", 5, "2026-11-01", currency="usd")
|
||||
self.assertEqual(o.monthly_cost(), {"RUB": 100.0, "USD": 5.0})
|
||||
|
||||
|
||||
class Agenda(unittest.TestCase):
|
||||
def test_buckets_and_reminders_once_a_day(self):
|
||||
o = fresh()
|
||||
t = o.today()
|
||||
o.add_task("Просрочка", due=(t - timedelta(days=2)).isoformat())
|
||||
o.add_sub("Хостинг", 900, (t + timedelta(days=2)).isoformat(), remind_days=3)
|
||||
o.add_routine("Бэкап NAS", 7, t.isoformat())
|
||||
a = o.agenda()
|
||||
self.assertEqual([x["title"] for x in a["overdue"]], ["Просрочка"])
|
||||
self.assertEqual([x["title"] for x in a["due_today"]], ["Бэкап NAS"])
|
||||
self.assertEqual([x["title"] for x in a["soon"]], ["Хостинг"])
|
||||
self.assertIn("Просрочено", fmt.agenda(a))
|
||||
due = o.due_reminders()
|
||||
self.assertEqual(len(due), 3)
|
||||
for it in due:
|
||||
o.mark_reminded(it["kind"], it["id"])
|
||||
self.assertEqual(o.due_reminders(), [])
|
||||
r = o.done_routine(1)
|
||||
self.assertEqual(r["next_date"], (t + timedelta(days=7)).isoformat())
|
||||
|
||||
|
||||
class Brain(unittest.TestCase):
|
||||
def test_plan_with_subtasks_and_refusals(self):
|
||||
o = fresh()
|
||||
plan = brain.parse_plan('''Вот: {"actions": [
|
||||
{"op": "add_task", "title": "Переезд Planet9", "key": "p", "client": "planet9"},
|
||||
{"op": "add_task", "title": "Купить свитч", "parent_key": "p", "due": "2026-11-01"},
|
||||
{"op": "delete_everything"},
|
||||
{"op": "add_sub", "name": "Домен", "amount": 1200, "next_date": "2026-11-15", "period": "year"}
|
||||
], "reply": "Записал"}''')
|
||||
lines = brain.apply_plan(o, plan)
|
||||
self.assertTrue(lines[1].startswith("+ задача #2 Купить свитч (подзадача #1)"))
|
||||
self.assertTrue(lines[2].startswith("✗"))
|
||||
self.assertEqual(o.subs()[0]["name"], "Домен")
|
||||
self.assertIn("#1 Переезд Planet9", brain.snapshot(o))
|
||||
|
||||
def test_handle_text_with_fake_claude(self):
|
||||
o = fresh()
|
||||
brain.ask_claude = lambda prompt, *a, **k: '{"actions":[{"op":"add_routine","title":"Обновления","every_days":30}],"reply":"Ок"}'
|
||||
out = brain.handle_text(o, "обновлять сервер раз в месяц", threading.Lock())
|
||||
self.assertIn("Ок", out)
|
||||
self.assertEqual(o.routines()[0]["every_days"], 30)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user