882632b7ce
- ops: paying marks the current charge paid (paid_for, persisted) instead of jumping to the next date; repeat_sub moves to the next period or closes the subscription. Paid-and-undecided subscriptions leave reminders and show in agenda['paid']. update_sub can set paid; tasks reopen when their status goes back; delete for subs and routines (BroV only, /api/delete); /api/tasks?all=1 and /api/subs?all=1. - Bot: «₽ Оплачено?» on reminders, then «🔁 Да, <date> / Нет, закрыть»; after paying by text it asks the same with buttons. - BroV: «Оплачено?» everywhere; «Оплачено — повторить?» section under the notch; «Весь план»: forms to edit or create tasks (title, description, status, due, company), subscriptions (amount, currency, period, date, reminder, company, paid, active) and routines, delete with confirmation, «+» per list, done tasks on demand. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
179 lines
8.3 KiB
Python
179 lines
8.3 KiB
Python
"""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_paid_then_repeat_or_close(self):
|
|
o = fresh()
|
|
due = (o.today() + timedelta(days=2)).isoformat()
|
|
s = o.add_sub("Claude", 20, due, currency="USD", client="saga")
|
|
self.assertFalse(s["paid"])
|
|
self.assertEqual([x["id"] for x in o.agenda()["soon"]], [s["id"]])
|
|
s = o.pay_sub(s["id"])
|
|
self.assertTrue(s["paid"]) # status persists
|
|
self.assertTrue(o.sub(s["id"])["paid"])
|
|
self.assertEqual(o.pay_sub(s["id"])["id"], s["id"]) # paying twice records once
|
|
self.assertEqual(o.con.execute("SELECT COUNT(*) FROM payments").fetchone()[0], 1)
|
|
a = o.agenda()
|
|
self.assertEqual([x["id"] for x in a["paid"]], [s["id"]])
|
|
self.assertEqual(a["soon"], [])
|
|
self.assertEqual([x for x in o.due_reminders() if x["kind"] == "sub"], [])
|
|
s = o.repeat_sub(s["id"], True)
|
|
self.assertEqual(s["next_date"], add_period(date.fromisoformat(due), "month").isoformat())
|
|
self.assertFalse(s["paid"])
|
|
o.pay_sub(s["id"])
|
|
s = o.repeat_sub(s["id"], False)
|
|
self.assertFalse(s["active"])
|
|
self.assertEqual(o.subs(), [])
|
|
|
|
def test_late_repeat_jumps_past_today(self):
|
|
o = fresh()
|
|
s = o.add_sub("Хостинг", 900, (o.today() - timedelta(days=70)).isoformat())
|
|
o.pay_sub(s["id"])
|
|
s = o.repeat_sub(s["id"])
|
|
self.assertGreaterEqual(date.fromisoformat(s["next_date"]), o.today())
|
|
|
|
def test_edit_everything(self):
|
|
o = fresh()
|
|
t = o.add_task("x")
|
|
o.update_task(t["id"], title="Новое", notes="тело", status="done", due="2026-12-01", client="home")
|
|
self.assertEqual(o.task(t["id"])["status"], "done")
|
|
o.update_task(t["id"], status="todo")
|
|
self.assertIsNone(o.task(t["id"])["done_at"])
|
|
s = o.add_sub("y", 1, "2026-12-01")
|
|
o.update_sub(s["id"], paid=True)
|
|
self.assertTrue(o.sub(s["id"])["paid"])
|
|
o.update_sub(s["id"], paid=False, amount=5, next_date="2026-12-05")
|
|
self.assertEqual((o.sub(s["id"])["paid"], o.sub(s["id"])["amount"]), (False, 5))
|
|
o.delete_sub(s["id"])
|
|
r = o.add_routine("z", 3)
|
|
o.delete_routine(r["id"])
|
|
self.assertEqual((o.subs(active_only=False), o.routines()), ([], []))
|
|
|
|
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": "Записал"}''')
|
|
paid = []
|
|
lines = brain.apply_plan(o, plan, paid)
|
|
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)
|
|
|
|
|
|
class Company(unittest.TestCase):
|
|
def test_sub_company_and_totals(self):
|
|
o = fresh()
|
|
o.add_sub("Claude Code", 20, "2026-10-25", currency="USD", client="Saga")
|
|
o.add_sub("Домен", 1200, "2026-11-15", period="year")
|
|
self.assertEqual(o.subs()[0]["client"], "saga")
|
|
self.assertEqual(o.monthly_by_client(), {"saga": {"USD": 20.0}, "": {"RUB": 100.0}})
|
|
text = fmt.subs(o.subs(), o.monthly_by_client(), date(2026, 10, 10))
|
|
self.assertIn("<b>Сага</b>", text)
|
|
self.assertIn("Без компании", text)
|
|
plan = {"actions": [{"op": "update_sub", "id": 2, "client": "home"}]}
|
|
brain.apply_plan(o, plan)
|
|
self.assertEqual(o.sub(2)["client"], "home")
|
|
|
|
def test_old_database_gets_the_column(self):
|
|
import sqlite3, tempfile
|
|
path = tempfile.mktemp(suffix=".db")
|
|
con = sqlite3.connect(path)
|
|
con.execute("CREATE TABLE subscriptions (id INTEGER PRIMARY KEY, name TEXT NOT NULL, amount REAL NOT NULL DEFAULT 0, "
|
|
"currency TEXT NOT NULL DEFAULT 'RUB', period TEXT NOT NULL DEFAULT 'month', every INTEGER NOT NULL DEFAULT 1, "
|
|
"next_date TEXT NOT NULL, remind_days INTEGER NOT NULL DEFAULT 3, notes TEXT NOT NULL DEFAULT '', "
|
|
"active INTEGER NOT NULL DEFAULT 1)")
|
|
con.execute("INSERT INTO subscriptions (name, next_date) VALUES ('old', '2026-11-01')")
|
|
con.commit(); con.close()
|
|
o = Ops(connect(path))
|
|
self.assertEqual(o.subs()[0]["client"], "")
|
|
os.remove(path)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|