File size: 10,135 Bytes
c0e9d84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""The User-to-Bot ask bridge: tokens, TTL, typed answers and leak safety.

A leaked future pins a tool call open, so the expiry and cancel paths are the
tests that matter most here.
"""

from __future__ import annotations

import asyncio

import pytest

from pyUltroid.agent._bridge import AskBridge, AskOutcome, answer_prefix


class FakeBot:
    def __init__(self):
        self.sent = []
        self._id = 100

    async def send_message(self, chat_id, text, buttons=None, **kw):
        self._id += 1
        self.sent.append({"chat": chat_id, "text": text, "buttons": buttons, "id": self._id})
        return type("M", (), {"id": self._id})()


def _bridge(**kw):
    bot = FakeBot()
    bridge = AskBridge(bot=bot, user=object(), **kw)
    return bridge, bot


def _data_for(bridge, chat, token, value="Yes"):
    return f"ask:{chat}:{token}:0:{value}".encode()


# ── token routing ───────────────────────────────────────────────────────────


def test_answer_prefix_is_chat_scoped():
    assert answer_prefix(42) == b"ask:42:"


def test_ask_sends_a_keyboard_and_waits():
    bridge, bot = _bridge()

    async def scenario():
        task = asyncio.ensure_future(bridge.ask(7, "Your name?", ("Ada", "Grace")))
        await asyncio.sleep(0)
        assert bot.sent  # the question went out on the bot
        token = next(iter(bridge._pending))
        bridge.resolve_callback(_data_for(bridge, 7, token, "Ada"))
        return await task

    outcome = asyncio.run(scenario())
    assert outcome.answered is True
    assert outcome.value == "Ada"
    assert bridge.pending == 0


def test_callback_data_carries_the_token_and_value():
    bridge, _ = _bridge()

    async def scenario():
        task = asyncio.ensure_future(bridge.ask(7, "Pick", ("A", "B")))
        await asyncio.sleep(0)
        session = next(iter(bridge._pending.values()))
        data = session.callback_for("A")
        assert session.data_prefix.decode() in data.decode()
        assert data.endswith(b":A")
        assert len(data) <= 64
        session.resolve("A")
        return await task

    assert asyncio.run(scenario()).value == "A"


def test_foreign_callback_data_is_not_ours():
    bridge, _ = _bridge()
    assert bridge.resolve_callback(b"other:thing:0:x") is None
    assert bridge.owns(b"nope") is False


def test_two_questions_in_one_chat_do_not_collide():
    bridge, _ = _bridge()

    async def scenario():
        first = asyncio.ensure_future(bridge.ask(7, "One", ("a",)))
        second = asyncio.ensure_future(bridge.ask(7, "Two", ("b",)))
        await asyncio.sleep(0)
        assert bridge.pending == 2
        tokens = list(bridge._pending)
        bridge.resolve_callback(_data_for(bridge, 7, tokens[1], "b"))
        bridge.resolve_callback(_data_for(bridge, 7, tokens[0], "a"))
        return await first, await second

    one, two = asyncio.run(scenario())
    assert (one.value, two.value) == ("a", "b")


def test_a_token_cannot_be_replayed_after_retire():
    bridge, _ = _bridge()

    async def scenario():
        task = asyncio.ensure_future(bridge.ask(7, "Q", ("a",)))
        await asyncio.sleep(0)
        token = next(iter(bridge._pending))
        bridge.resolve_callback(_data_for(bridge, 7, token, "a"))
        await task
        return token

    token = asyncio.run(scenario())
    assert bridge.resolve_callback(_data_for(bridge, 7, token, "a")) is None


def test_cross_chat_token_is_rejected():
    """A token minted for chat A must not answer chat B's question."""
    bridge, _ = _bridge()

    async def scenario():
        task = asyncio.ensure_future(bridge.ask(7, "Q", ("a",)))
        await asyncio.sleep(0)
        token = next(iter(bridge._pending))
        assert bridge.resolve_callback(f"ask:9:{token}:0:a".encode()) is None
        next(iter(bridge._pending.values())).resolve("a")
        return await task

    assert asyncio.run(scenario()).value == "a"


# ── typed answers ───────────────────────────────────────────────────────────


def test_typed_reply_answers_the_oldest_question_in_chat():
    bridge, _ = _bridge()

    async def scenario():
        first = asyncio.ensure_future(bridge.ask(7, "One", ()))
        await asyncio.sleep(0)
        second = asyncio.ensure_future(bridge.ask(7, "Two", ()))
        await asyncio.sleep(0)
        bridge.submit_text(7, "Ada")
        bridge.submit_text(7, "Grace")
        return await first, await second

    one, two = asyncio.run(scenario())
    assert (one.value, two.value) == ("Ada", "Grace")


def test_typed_reply_in_another_chat_is_not_consumed():
    bridge, _ = _bridge()

    async def scenario():
        task = asyncio.ensure_future(bridge.ask(7, "Q", ()))
        await asyncio.sleep(0)
        assert bridge.submit_text(8, "wrong chat") is None
        next(iter(bridge._pending.values())).resolve("ok")
        return await task

    assert asyncio.run(scenario()).value == "ok"


def test_typed_reply_with_no_open_question_is_none():
    assert _bridge()[0].submit_text(7, "hi") is None


def test_typed_answer_survives_the_tuple_parse_mode_shape():
    bridge, _ = _bridge()

    async def scenario():
        task = asyncio.ensure_future(bridge.ask(7, "Q", ()))
        await asyncio.sleep(0)
        bridge.submit_text(7, ("Ada", ["bold"]))
        return await task

    assert asyncio.run(scenario()).value == "Ada"


# ── TTL and leak safety ─────────────────────────────────────────────────────


def test_expiry_returns_an_unanswered_outcome_not_an_exception():
    bridge, _ = _bridge(ttl=0.01)
    outcome = asyncio.run(bridge.ask(7, "Q", ("a",)))
    assert outcome.answered is False
    assert outcome.timed_out is True
    assert outcome.reason == "timeout"
    assert bridge.pending == 0


def test_expired_outcome_is_shaped_for_the_model():
    outcome = AskOutcome(question="Q", timed_out=True, reason="timeout", waited=1.0)
    payload = outcome.as_dict()
    assert payload["answered"] is False
    assert payload["user_response"] is None
    assert payload["timed_out"] is True
    assert payload["reason"] == "timeout"


def test_cancelling_the_caller_retires_the_session():
    """A cancelled tool must not leave a future pinning the map."""
    bridge, _ = _bridge(ttl=60)

    async def scenario():
        task = asyncio.ensure_future(bridge.ask(7, "Q", ("a",)))
        await asyncio.sleep(0)
        assert bridge.pending == 1
        task.cancel()
        with pytest.raises(asyncio.CancelledError):
            await task
        return bridge.pending

    assert asyncio.run(scenario()) == 0


def test_cancel_all_settles_everything():
    bridge, _ = _bridge(ttl=60)

    async def scenario():
        tasks = [
            asyncio.ensure_future(bridge.ask(7, "A", ())),
            asyncio.ensure_future(bridge.ask(8, "B", ())),
        ]
        await asyncio.sleep(0)
        assert bridge.cancel_all("shutdown") == 2
        return [await t for t in tasks]

    results = asyncio.run(scenario())
    assert all(r.reason == "shutdown" for r in results)
    assert bridge.pending == 0


def test_watchdog_is_cancelled_on_answer():
    bridge, _ = _bridge(ttl=60)

    async def scenario():
        task = asyncio.ensure_future(bridge.ask(7, "Q", ("a",)))
        await asyncio.sleep(0)
        session = next(iter(bridge._pending.values()))
        assert session.watchdog is not None
        session.resolve("a")
        return await task, session

    _, session = asyncio.run(scenario())
    assert session.watchdog is None


def test_no_ask_leaves_a_session_behind():
    bridge, _ = _bridge()

    async def scenario():
        for i in range(5):
            task = asyncio.ensure_future(bridge.ask(i, "Q", ("a",)))
            await asyncio.sleep(0)
            token = next(iter(bridge._pending))
            bridge.resolve_callback(_data_for(bridge, i, token, "a"))
            await task
        return bridge.pending

    assert asyncio.run(scenario()) == 0


# ── keyboard shape ──────────────────────────────────────────────────────────


def test_choices_render_two_columns_by_default():
    bridge, bot = _bridge()

    async def scenario():
        task = asyncio.ensure_future(bridge.ask(7, "Pick", ("a", "b", "c", "d")))
        await asyncio.sleep(0)
        buttons = bot.sent[0]["buttons"]
        assert len(buttons) == 2 and all(len(r) == 2 for r in buttons)
        for row in buttons:
            for cell in row:
                assert cell.style is not None
        next(iter(bridge._pending.values())).resolve("a")
        return await task

    asyncio.run(scenario())


def test_open_question_sends_no_buttons():
    bridge, bot = _bridge()

    async def scenario():
        task = asyncio.ensure_future(bridge.ask(7, "Your name?"))
        await asyncio.sleep(0)
        assert bot.sent[0]["buttons"] is None
        next(iter(bridge._pending.values())).resolve("Ada")
        return await task

    assert asyncio.run(scenario()).value == "Ada"


def test_note_is_appended_to_the_question():
    bridge, bot = _bridge()

    async def scenario():
        task = asyncio.ensure_future(bridge.ask(7, "Name?", note="first name"))
        await asyncio.sleep(0)
        assert "Name?" in bot.sent[0]["text"]
        assert "first name" in bot.sent[0]["text"]
        next(iter(bridge._pending.values())).resolve("Ada")
        return await task

    asyncio.run(scenario())


def test_ask_without_a_bot_client_is_a_clear_error():
    bridge = AskBridge(user=object())
    bridge._bot = None
    bridge.bot  # forces resolution attempt
    if bridge._bot is None:
        with pytest.raises(RuntimeError, match="no bot client"):
            asyncio.run(bridge.ask(7, "Q"))