Custom HA integration (domain meshcore_agent) that lets MeshCore nodes manage a todo list over mesh messages: - todo / todo list, todo add, todo done, todo uncheck, todo remove - remind <n> <when> / remind off <n> (relative, clock, weekday, absolute) - replies via meshcore.send_message / send_channel_message - reminders scheduled with async_track_point_in_time and delivered to the sender who set them - todo list also exposed as a HA todo entity - config flow with sender allowlist, allow-all toggle, reply length cap - pure-Python core (core/) covered by pytest; adapter flow tested with stubbed hass
192 lines
6.0 KiB
Python
192 lines
6.0 KiB
Python
"""Tests for the command registry (the todo feature over mesh messages)."""
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
from custom_components.meshcore_agent.core.commands import (
|
|
Context,
|
|
handle_message,
|
|
)
|
|
from custom_components.meshcore_agent.core.store import Reminder, TodoStore
|
|
|
|
NOW = datetime(2026, 12, 1, 10, 0, 0, tzinfo=timezone.utc)
|
|
|
|
|
|
def make_ctx(*texts, target=None):
|
|
return Context(store=TodoStore(), now=NOW, target=target)
|
|
|
|
|
|
def populate(ctx: Context):
|
|
for text in ("Buy milk", "Fix antenna", "Water plants"):
|
|
ctx.store.add(text)
|
|
|
|
|
|
def test_non_command_messages_are_ignored():
|
|
ctx = make_ctx()
|
|
assert handle_message("hey, how are you?", ctx) is None
|
|
assert handle_message("", ctx) is None
|
|
assert handle_message(" ", ctx) is None
|
|
|
|
|
|
def test_todo_list_empty():
|
|
ctx = make_ctx()
|
|
assert "No todos" in handle_message("todo", ctx)
|
|
|
|
|
|
def test_todo_list_shows_items():
|
|
ctx = make_ctx()
|
|
populate(ctx)
|
|
reply = handle_message("todo", ctx)
|
|
assert reply.splitlines() == [
|
|
"1. [ ] Buy milk",
|
|
"2. [ ] Fix antenna",
|
|
"3. [ ] Water plants",
|
|
]
|
|
|
|
|
|
def test_todo_list_marks_completed_and_reminders():
|
|
ctx = make_ctx()
|
|
populate(ctx)
|
|
ctx.store.complete(ctx.store.items[1])
|
|
ctx.store.set_reminder(
|
|
ctx.store.items[0],
|
|
Reminder(at="2026-12-02T10:00:00+00:00", kind="direct", target="ab"),
|
|
)
|
|
reply = handle_message("todo", ctx)
|
|
assert reply.splitlines() == [
|
|
"1. [ ] Buy milk - Wed 02 Dec 10:00",
|
|
"2. [ ] Water plants",
|
|
"3. [x] Fix antenna",
|
|
]
|
|
|
|
|
|
def test_todo_add():
|
|
ctx = make_ctx()
|
|
assert handle_message("todo add Buy milk", ctx) == "Added #1: Buy milk"
|
|
assert handle_message("todo add Fix antenna ", ctx) == "Added #2: Fix antenna"
|
|
assert [i.text for i in ctx.store.items] == ["Buy milk", "Fix antenna"]
|
|
|
|
|
|
def test_todo_add_usage():
|
|
ctx = make_ctx()
|
|
assert "Usage" in handle_message("todo add", ctx)
|
|
|
|
|
|
def test_todo_done_by_number_and_text():
|
|
ctx = make_ctx()
|
|
populate(ctx)
|
|
assert handle_message("todo done 2", ctx) == "Done: Fix antenna"
|
|
assert ctx.store.items[1].is_completed
|
|
assert handle_message("todo done buy m", ctx) == "Done: Buy milk"
|
|
# Numbers now refer to the new display order (open items first).
|
|
assert handle_message("todo done 2", ctx) == "Already done: Buy milk"
|
|
assert handle_message("todo done 1", ctx) == "Done: Water plants"
|
|
|
|
|
|
def test_todo_done_by_full_text():
|
|
ctx = make_ctx()
|
|
populate(ctx)
|
|
assert handle_message("todo done fix antenna", ctx) == "Done: Fix antenna"
|
|
|
|
|
|
def test_todo_done_missing():
|
|
ctx = make_ctx()
|
|
populate(ctx)
|
|
assert "No todo matches" in handle_message("todo done 99", ctx)
|
|
ctx.store.add("Buy bread") # makes the "buy" prefix ambiguous
|
|
assert "No todo matches" in handle_message("todo done buy", ctx) # ambiguous
|
|
assert "Missing todo number" in handle_message("todo done", ctx)
|
|
|
|
|
|
def test_todo_uncheck():
|
|
ctx = make_ctx()
|
|
populate(ctx)
|
|
handle_message("todo done 1", ctx)
|
|
# Buy milk is now the third item in display order (open items first).
|
|
assert handle_message("todo uncheck 3", ctx) == "Reopened: Buy milk"
|
|
assert handle_message("todo uncheck buy", ctx) == "Not done yet: Buy milk"
|
|
|
|
|
|
def test_todo_remove():
|
|
ctx = make_ctx()
|
|
populate(ctx)
|
|
assert handle_message("todo remove 2", ctx) == "Removed: Fix antenna"
|
|
assert [i.text for i in ctx.store.items] == ["Buy milk", "Water plants"]
|
|
assert handle_message("todo rm 1", ctx) == "Removed: Buy milk"
|
|
|
|
|
|
def test_remind_by_number_relative():
|
|
ctx = make_ctx()
|
|
populate(ctx)
|
|
reply = handle_message("remind 1 in 30m", ctx)
|
|
assert "Reminder set for #1 (Buy milk)" in reply
|
|
expected = NOW + timedelta(minutes=30)
|
|
assert ctx.store.items[0].reminder is not None
|
|
parsed = datetime.fromisoformat(ctx.store.items[0].reminder.at)
|
|
assert parsed == expected
|
|
|
|
|
|
def test_remind_by_text_prefix():
|
|
ctx = make_ctx()
|
|
populate(ctx)
|
|
reply = handle_message("remind water plants tomorrow 09:00", ctx)
|
|
assert "Reminder set for #3 (Water plants)" in reply
|
|
parsed = datetime.fromisoformat(ctx.store.items[2].reminder.at)
|
|
assert parsed == (NOW + timedelta(days=1)).replace(hour=9)
|
|
|
|
|
|
def test_remind_stores_target():
|
|
target = Reminder(at="", kind="direct", target="aabbcc")
|
|
ctx = make_ctx(target=target)
|
|
populate(ctx)
|
|
handle_message("remind 1 in 1h", ctx)
|
|
reminder = ctx.store.items[0].reminder
|
|
assert reminder.kind == "direct"
|
|
assert reminder.target == "aabbcc"
|
|
|
|
|
|
def test_remind_off():
|
|
ctx = make_ctx()
|
|
populate(ctx)
|
|
handle_message("remind 1 in 1h", ctx)
|
|
assert handle_message("remind off 1", ctx) == "Reminder cleared: Buy milk"
|
|
assert ctx.store.items[0].reminder is None
|
|
assert handle_message("remind off 1", ctx) == "No reminder set: Buy milk"
|
|
|
|
|
|
def test_remind_usage_errors():
|
|
ctx = make_ctx()
|
|
populate(ctx)
|
|
assert "Usage" in handle_message("remind", ctx)
|
|
assert "Usage" in handle_message("remind 1", ctx) # no when
|
|
assert "Bad time" in handle_message("remind 1 gibberish", ctx)
|
|
assert "No todo matches" in handle_message("remind 9 in 30m", ctx)
|
|
|
|
|
|
def test_help():
|
|
ctx = make_ctx()
|
|
assert handle_message("help", ctx) == handle_message("help", make_ctx())
|
|
assert "todo" in handle_message("help", ctx).lower()
|
|
|
|
|
|
def test_wake_word_prefix():
|
|
ctx = make_ctx()
|
|
populate(ctx)
|
|
assert "Buy milk" in handle_message("agent todo", ctx)
|
|
assert "Buy milk" in handle_message("bot, todo", ctx)
|
|
assert "Usage" in handle_message("agent remind 1", ctx)
|
|
|
|
|
|
def test_alias_commands():
|
|
ctx = make_ctx()
|
|
populate(ctx)
|
|
assert handle_message("todos", ctx) == handle_message("todo", ctx)
|
|
assert handle_message("done 1", ctx) == "Done: Buy milk"
|
|
# After done 1, Fix antenna is #1 in display order.
|
|
assert handle_message("rm 1", ctx) == "Removed: Fix antenna"
|
|
assert "Added #2" in handle_message("add New item", ctx)
|
|
|
|
|
|
def test_unknown_todo_subcommand():
|
|
ctx = make_ctx()
|
|
assert "Unknown todo command" in handle_message("todo zap", ctx)
|