The limit was 2-3, the Android notification's button count. The record keeps everything; each surface shows what it can and never a part of the options: the app's cards wrap every option as a button and fold a long question, a web push past three options carries optionCount and the service worker offers one Answer… that opens the app, channel plugins get the ask whole. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
70 lines
3.0 KiB
Python
70 lines
3.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))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|