Files
ai-agent/cli/test_notify_ask.py
gabrielvidalandClaude Sonnet 5.5 7c983e9c94 feat(notify): an agent can cancel its ask; the time waited moves bottom right
- `notify cancel ask_… [--reason R]` (also `notify-ask cancel`) withdraws a
  pending ask: POST /api/ask/{id}/cancel on the lab, the sidecar outbox's
  cancel (now with its reason) on a worker, mirrored to the hub record.
  The ask turns `cancelled` (cancelledAt / cancelledBy / cancelReason), its
  waiters wake, the push leaves every device, and `notify wait` exits 3.
- The person can cancel too: a Cancel (second tap confirms) on the thread's
  ask card and on the home *Waiting for feedback* card; an ask cancelled from
  the viewer ends a worker's wait the same way.
- A cancelled ask's card greys out with a `cancelled` badge and
  "Cancelled by the agent — <reason>" / "Cancelled by you".
- The live time-waited counter no longer sits beside the question (each tick
  changed its width and reflowed the text): bottom right, next to Cancel and
  Dismiss, on the home card and in the bell.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-30 16:51:22 +01:00

114 lines
5.0 KiB
Python

"""`notify ask` (notify.py): what it accepts and what it posts.
``python3 -m unittest test_notify_ask`` from ``cli/``."""
import io
import os
import unittest
from contextlib import redirect_stderr, redirect_stdout
from unittest import mock
import notify
class TestNotifyAsk(unittest.TestCase):
ENV = {"CLAUDE_SESSION_ID": "sid-1", "AI_AGENT_OUTBOX_URL": "", "NOTIFY_AS": ""}
def run_cli(self, argv):
calls = []
def fake_call(self_, url, data=None, **kw):
calls.append((url, data))
return 200, {"ok": True, "id": (data or {}).get("id")}
out, err = io.StringIO(), io.StringIO()
with mock.patch.dict(os.environ, self.ENV, clear=False), \
mock.patch.object(notify.Transport, "_call", fake_call), \
redirect_stdout(out), redirect_stderr(err):
rc = notify.main(["ask", *argv])
return rc, calls, out.getvalue(), err.getvalue()
def test_six_options_and_a_long_question_are_posted_whole(self):
question = "Where should this build go?\n" + "Read this first. " * 100
six = ["Staging", "Production", "Canary", "Preview", "Later", "Cancel"]
rc, calls, out, _ = self.run_cli([question, *six, "--background"])
self.assertEqual(rc, 0)
url, data = calls[0]
self.assertTrue(url.endswith("/api/ask"))
self.assertEqual((data["question"], data["options"]), (question.strip(), six))
self.assertIn(f"ASK_ID={data['id']}", out)
def test_two_and_three_options_as_before(self):
for opts in (["Yes", "No"], ["Merge", "Wait", "Drop"]):
rc, calls, _, _ = self.run_cli(["Ship?", *opts, "--background"])
self.assertEqual((rc, calls[0][1]["options"]), (0, opts))
def test_the_limits_are_refused_before_anything_is_sent(self):
nine = [f"o{i}" for i in range(9)]
for argv, said in (
(["Q?", "only"], "usage"),
(["Q?", *nine], "2 to 8 options (9 given)"),
(["x" * (notify.ASK_MAX_QUESTION_CHARS + 1), "a", "b"], "question too long"),
(["Q?", "a", "b" * (notify.ASK_MAX_OPTION_CHARS + 1)], "option too long")):
rc, calls, _, err = self.run_cli(argv)
self.assertEqual((rc, calls), (2, []))
self.assertIn(said, err)
def test_the_cli_and_the_hub_agree_on_the_numbers(self):
import pathlib
import re
src = (pathlib.Path(notify.__file__).resolve().parent.parent
/ "backend" / "notifications" / "service.py").read_text()
for name in ("ASK_MAX_OPTION_CHARS", "ASK_MAX_QUESTION_CHARS"):
m = re.search(rf"^{name} = (\d+)$", src, re.M)
self.assertEqual(int(m.group(1)), getattr(notify, name))
m = re.search(r"^ASK_MIN_OPTIONS, ASK_MAX_OPTIONS = (\d+), (\d+)$", src, re.M)
self.assertEqual((int(m.group(1)), int(m.group(2))),
(notify.ASK_MIN_OPTIONS, notify.ASK_MAX_OPTIONS))
class TestNotifyCancel(unittest.TestCase):
AID = "ask_0123456789ab"
def run_cli(self, argv, env=None, reply=(200, {"status": "cancelled"})):
calls = []
def fake_call(self_, url, data=None, **kw):
calls.append((url, data))
return reply
out, err = io.StringIO(), io.StringIO()
with mock.patch.dict(os.environ, {**TestNotifyAsk.ENV, **(env or {})}, clear=False), \
mock.patch.object(notify.Transport, "_call", fake_call), \
redirect_stdout(out), redirect_stderr(err):
rc = notify.main(argv)
return rc, calls, out.getvalue(), err.getvalue()
def test_cancel_posts_to_the_hub_with_its_reason(self):
rc, calls, out, _ = self.run_cli(["cancel", self.AID, "--reason", "not needed"])
self.assertEqual(rc, 0)
self.assertTrue(calls[0][0].endswith(f"/api/ask/{self.AID}/cancel"))
self.assertEqual(calls[0][1], {"reason": "not needed"})
self.assertIn(f"cancelled {self.AID}", out)
def test_on_a_worker_it_goes_to_the_outbox(self):
rc, calls, _, _ = self.run_cli(["cancel", self.AID],
env={"AI_AGENT_OUTBOX_URL": "http://127.0.0.1:9/x"})
self.assertEqual((rc, calls[0][0]), (0, f"http://127.0.0.1:9/x/outbox/{self.AID}/cancel"))
def test_the_notify_ask_alias_takes_cancel(self):
rc, calls, _, _ = self.run_cli(["cancel", self.AID], env={"NOTIFY_AS": "ask"})
self.assertEqual(rc, 0)
self.assertTrue(calls[0][0].endswith("/cancel"))
def test_an_ask_that_already_ended_is_not_an_error(self):
rc, _, out, err = self.run_cli(["cancel", self.AID],
reply=(200, {"status": "answered", "answer": "Yes"}))
self.assertEqual((rc, out.strip()), (0, "Yes"))
self.assertIn("already ended (answered)", err)
def test_only_ask_ids(self):
for bad in ("ntf_0123456789ab", "nope"):
self.assertEqual(self.run_cli(["cancel", bad])[0], 2)
if __name__ == "__main__":
unittest.main()