Repository navigation
Expand file tree
/
Copy pathtest_tool_progress.py
More file actions
1516 lines (1264 loc) · 58.9 KB
/
Copy pathtest_tool_progress.py
File metadata and controls
1516 lines (1264 loc) · 58.9 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Unit tests for tool progress display: _extract_hint_data, _format_tool_hint,
_mcp_display_name, and tool_status_update dedup/normalization logic."""
import json
import logging
import threading
from collections import deque
from unittest.mock import MagicMock, Mock
import pytest
from feishu_bridge.runtime import _extract_hint_data
from feishu_bridge.ui import ResponseHandle
class TestExtractHintData:
# ── Bash ──
def test_bash_returns_basename_only(self):
"""Only executable basename is shown — never full args (may contain secrets)."""
assert _extract_hint_data("Bash", {"command": "git status"}) == "git"
def test_bash_path_command_returns_basename(self):
assert _extract_hint_data("Bash", {"command": "/usr/bin/env python3 -m pytest"}) == "env"
def test_bash_prefers_description(self):
assert _extract_hint_data("Bash", {
"command": "pip install foo",
"description": "Installing deps",
}) == "Installing deps"
def test_bash_prefers_intent_over_command(self):
"""OMP _i is preferred over raw command basename."""
assert _extract_hint_data("Bash", {
"command": "curl -H 'Authorization: Bearer sk-...'",
"_i": "Fetching API data",
}) == "Fetching API data"
def test_bash_empty_command_returns_empty(self):
assert _extract_hint_data("Bash", {"command": ""}) == ""
assert _extract_hint_data("Bash", {}) == ""
def test_bash_empty_command_falls_back_to_intent(self):
assert _extract_hint_data("Bash", {"_i": "Running tests"}) == "Running tests"
# ── Read / Write / Edit ──
def test_read_file_path_alma(self):
"""Alma (Claude Code) uses file_path."""
assert _extract_hint_data("Read", {"file_path": "/home/user/main.py"}) == "/home/user/main.py"
def test_read_path_omp(self):
"""OMP uses path."""
assert _extract_hint_data("Read", {"path": "/home/user/main.py"}) == "/home/user/main.py"
def test_read_prefers_file_path(self):
"""file_path takes precedence over path (Alma compat)."""
assert _extract_hint_data("Read", {
"file_path": "/alma.py",
"path": "/omp.py",
}) == "/alma.py"
def test_edit_path_omp(self):
assert _extract_hint_data("Edit", {"path": "/tmp/foo.txt"}) == "/tmp/foo.txt"
def test_write_path_omp(self):
assert _extract_hint_data("Write", {"path": "/a/b.md"}) == "/a/b.md"
def test_read_empty(self):
assert _extract_hint_data("Read", {}) == ""
# ── Agent / Task ──
def test_agent_description_truncated(self):
desc = "A" * 60
assert _extract_hint_data("Agent", {"description": desc}) == "A" * 40
def test_task_description(self):
assert _extract_hint_data("Task", {"description": "Review code"}) == "Review code"
# ── Grep / Search ──
def test_grep_pattern_truncated(self):
pattern = "x" * 50
assert _extract_hint_data("Grep", {"pattern": pattern}) == "x" * 30
def test_search_alias(self):
assert _extract_hint_data("Search", {"pattern": "TODO"}) == "TODO"
# ── Skill ──
def test_skill_name(self):
assert _extract_hint_data("Skill", {"skill": "done"}) == "done"
# ── WebSearch / WebFetch ──
def test_websearch_query(self):
assert _extract_hint_data("WebSearch", {"query": "python async"}) == "python async"
def test_webfetch_url(self):
assert _extract_hint_data("WebFetch", {"url": "https://example.com"}) == "https://example.com"
# ── Find ──
def test_find_single_path(self):
assert _extract_hint_data("Find", {"paths": ["src/**/*.ts"]}) == "src/**/*.ts"
def test_find_multiple_paths(self):
result = _extract_hint_data("Find", {"paths": ["src/**/*.ts", "tests/**/*.ts"]})
assert result == "src/**/*.ts +1"
def test_find_empty_falls_back_to_intent(self):
assert _extract_hint_data("Find", {"_i": "Finding test files"}) == "Finding test files"
# ── Lsp ──
def test_lsp_action_and_file(self):
result = _extract_hint_data("Lsp", {"action": "definition", "file": "main.py"})
assert result == "definition main.py"
def test_lsp_action_only(self):
assert _extract_hint_data("Lsp", {"action": "diagnostics"}) == "diagnostics"
def test_lsp_falls_back_to_intent(self):
assert _extract_hint_data("Lsp", {"_i": "Finding definitions"}) == "Finding definitions"
# ── Browser ──
def test_browser_action_and_url(self):
result = _extract_hint_data("Browser", {"action": "open", "url": "https://example.com"})
assert result == "open https://example.com"
def test_browser_action_only(self):
assert _extract_hint_data("Browser", {"action": "close"}) == "close"
# ── Eval ──
def test_eval_cell_title(self):
result = _extract_hint_data("Eval", {"cells": [{"title": "imports", "language": "py"}]})
assert result == "imports"
def test_eval_cell_language_fallback(self):
result = _extract_hint_data("Eval", {"cells": [{"language": "py"}]})
assert result == "py"
# ── AstGrep / AstEdit ──
def test_ast_grep_pattern(self):
assert _extract_hint_data("AstGrep", {"pat": "console.log($$$)"}) == "console.log($$$)"
def test_ast_edit_ops_pattern(self):
result = _extract_hint_data("AstEdit", {"ops": [{"pat": "old($A)", "out": "new($A)"}]})
assert result == "old($A)"
# ── Debug ──
def test_debug_action_and_program(self):
result = _extract_hint_data("Debug", {"action": "launch", "program": "./app"})
assert result == "launch ./app"
# ── Universal _i fallback ──
def test_unknown_tool_uses_intent(self):
assert _extract_hint_data("SomeNewTool", {"_i": "Doing work"}) == "Doing work"
def test_unknown_tool_no_intent(self):
assert _extract_hint_data("SomeNewTool", {}) == ""
def test_intent_truncated(self):
intent = "A" * 80
assert _extract_hint_data("SomeNewTool", {"_i": intent}) == "A" * 50
class TestFormatToolHint:
"""Tests for ResponseHandle._format_tool_hint (static method)."""
@pytest.fixture()
def fmt(self):
from feishu_bridge.ui import ResponseHandle
return ResponseHandle._format_tool_hint
def test_read_basename(self, fmt):
assert fmt("Read", "/home/user/project/main.py") == "main.py"
def test_edit_basename(self, fmt):
assert fmt("Edit", "/tmp/foo.txt") == "foo.txt"
def test_empty_returns_empty(self, fmt):
assert fmt("Read", "") == ""
assert fmt("Bash", "") == ""
def test_agent_passthrough(self, fmt):
assert fmt("Agent", "Review code") == "Review code"
def test_unknown_tool_passthrough(self, fmt):
assert fmt("Grep", "pattern") == "pattern"
def test_webfetch_host(self, fmt):
assert fmt("WebFetch", "https://docs.python.org/3/library/os.html") == "docs.python.org/3/library/os.html"
def test_webfetch_host_only(self, fmt):
assert fmt("WebFetch", "https://example.com/") == "example.com"
def test_websearch_truncates(self, fmt):
assert fmt("WebSearch", "short query") == "short query"
long_query = "a" * 60
assert fmt("WebSearch", long_query) == "a" * 40
def test_get_subagent_result_truncates(self, fmt):
short = "subagent-abc"
assert fmt("GetSubagentResult", short) == "subagent-abc"
long = "subagent-01234567-89ab-cdef-0123-456789abcdef"
result = fmt("GetSubagentResult", long)
assert result == "subagent-0123456…"
assert len(result) == 17 # 16 chars + …
class TestMcpDisplayName:
"""Tests for ResponseHandle._mcp_display_name (static method)."""
@pytest.fixture()
def mcp(self):
from feishu_bridge.ui import ResponseHandle
return ResponseHandle._mcp_display_name
def test_standard_mcp(self, mcp):
assert mcp("mcp__hindsight__retain") == "Hindsight: retain"
def test_mcp_with_underscores(self, mcp):
assert mcp("mcp__claude_ai_Google_Drive__authenticate") == "Claude Ai Google Drive: authenticate"
def test_short_mcp_name(self, mcp):
assert mcp("mcp__foo") == "mcp__foo"
def test_non_mcp_passthrough(self, mcp):
assert mcp("Bash") == "Bash"
class TestToolStatusUpdateDedup:
"""Test tool_status_update dedup and normalization via _tool_history."""
@pytest.fixture()
def handle(self):
from unittest.mock import MagicMock
from feishu_bridge.ui import ResponseHandle
h = ResponseHandle.__new__(ResponseHandle)
from collections import deque
h._tool_history = deque(maxlen=8)
h._terminated = False
h._summary_updated = False
h.card_message_id = "msg-1"
h._cardkit_card_id = "card-1"
h._update_summary = MagicMock()
h._render_progress = MagicMock()
h._ensure_card = MagicMock(return_value=True)
return h
def test_dict_input_accumulates(self, handle):
handle.tool_status_update([
{"name": "Read", "hint_data": "/a/foo.py"},
{"name": "Edit", "hint_data": "/a/bar.py"},
])
assert len(handle._tool_history) == 2
assert handle._tool_history[0]["label"] == "读取文件"
assert handle._tool_history[0]["hint"] == "foo.py"
assert handle._tool_history[1]["label"] == "编辑文件"
def test_consecutive_same_tool_dedup(self, handle):
handle.tool_status_update([
{"name": "Read", "hint_data": "/a/foo.py"},
{"name": "Read", "hint_data": "/a/foo.py"},
{"name": "Read", "hint_data": "/a/foo.py"},
])
assert len(handle._tool_history) == 1
assert handle._tool_history[0]["count"] == 3
def test_different_hints_no_dedup(self, handle):
handle.tool_status_update([
{"name": "Read", "hint_data": "/a/foo.py"},
{"name": "Read", "hint_data": "/a/bar.py"},
])
assert len(handle._tool_history) == 2
def test_pi_runner_str_compat(self, handle):
handle.tool_status_update(["ls", "read", "read"])
assert len(handle._tool_history) == 2
assert handle._tool_history[0]["label"] == "ls"
assert handle._tool_history[1]["label"] == "read"
assert handle._tool_history[1]["count"] == 2
def test_agent_excluded(self, handle):
handle.tool_status_update([
{"name": "Agent", "hint_data": "Review code"},
{"name": "TodoWrite", "hint_data": ""},
])
assert len(handle._tool_history) == 0
def test_mcp_tool_display(self, handle):
handle.tool_status_update([
{"name": "mcp__hindsight__retain", "hint_data": ""},
])
assert len(handle._tool_history) == 1
assert handle._tool_history[0]["label"] == "Hindsight: retain"
def test_deque_maxlen_respected(self, handle):
tools = [{"name": f"tool_{i}", "hint_data": ""} for i in range(12)]
handle.tool_status_update(tools)
assert len(handle._tool_history) == 8
def test_summary_updated_from_latest(self, handle):
handle.tool_status_update([
{"name": "Read", "hint_data": "/a/foo.py"},
{"name": "Bash", "hint_data": "git status"},
])
handle._update_summary.assert_called_with("执行命令...")
def test_empty_list_no_crash(self, handle):
handle.tool_status_update([])
assert len(handle._tool_history) == 0
def test_terminated_skips(self, handle):
handle._terminated = True
handle.tool_status_update([{"name": "Read", "hint_data": "/a.py"}])
assert len(handle._tool_history) == 0
def test_summary_updated_does_not_skip(self, handle):
"""Regression: ``_summary_updated`` is one-shot for the CardKit summary
text patch (思考中→输入中). It must NOT gate tool history updates;
every tool call after the first ``text_delta`` had been silently
dropped, so the user saw no tool card.
"""
handle._summary_updated = True
handle.tool_status_update([{"name": "Read", "hint_data": "/a.py"}])
assert len(handle._tool_history) == 1
assert handle._tool_history[0]["name"] == "Read"
def test_backfill_updates_empty_hint(self, handle):
"""_backfill entries update the last matching entry with empty hint."""
handle.tool_status_update([
{"name": "Read", "hint_data": ""},
])
assert handle._tool_history[0]["hint"] == ""
handle.tool_status_update([
{"name": "Read", "hint_data": "/a/foo.py", "_backfill": True},
])
assert handle._tool_history[0]["hint"] == "foo.py"
def test_backfill_skips_if_hint_already_set(self, handle):
"""_backfill doesn't overwrite an existing non-empty hint."""
handle.tool_status_update([
{"name": "Read", "hint_data": "/a/foo.py"},
])
handle.tool_status_update([
{"name": "Read", "hint_data": "/a/bar.py", "_backfill": True},
])
# Should NOT have been changed
assert handle._tool_history[0]["hint"] == "foo.py"
def test_backfill_no_new_entry(self, handle):
"""_backfill entries never create new history entries."""
handle.tool_status_update([
{"name": "Bash", "hint_data": "git status", "_backfill": True},
])
assert len(handle._tool_history) == 0
def test_task_excluded(self, handle):
"""Task tool is excluded from tool history (shown as agent)."""
handle.tool_status_update([
{"name": "Task", "hint_data": ""},
])
assert len(handle._tool_history) == 0
def test_subagent_included_in_tool_history(self, handle):
"""Subagent is rendered in the normal tool card."""
handle.tool_status_update([
{"name": "Subagent", "hint_data": "scout: 分析代码"},
])
assert len(handle._tool_history) == 1
assert handle._tool_history[0]["name"] == "Subagent"
assert handle._tool_history[0]["label"] == "分发子任务"
def test_tool_status_update_subagent_marks_prior_done(self, handle):
"""A new Subagent tool entry closes the prior in-flight tool."""
handle.tool_status_update([
{"name": "WebFetch", "hint_data": "https://example.com"},
])
assert len(handle._tool_history) == 1
assert handle._tool_history[0]["status"] == "running"
handle.tool_status_update([
{"name": "Subagent", "hint_data": ""},
])
assert len(handle._tool_history) == 2
assert handle._tool_history[0]["status"] == "done"
assert handle._tool_history[1]["name"] == "Subagent"
def test_tool_status_update_without_main_card_accumulates_history(self, handle):
"""tool_status_update accumulates _tool_history even when no main card exists.
_render_progress is mocked so cardkit ops don't crash; _update_summary
call is recorded by the mock but the real guard inside would no-op.
"""
handle.card_message_id = None
handle._cardkit_card_id = None
handle.source_message_id = "src-msg-1"
handle.tool_status_update([
{"name": "Read", "hint_data": "/a/foo.py"},
])
# _tool_history should still be populated
assert len(handle._tool_history) == 1
assert handle._tool_history[0]["label"] == "读取文件"
# _render_progress was called (tool card path is independent)
handle._render_progress.assert_called_once()
def test_tool_status_update_no_source_msg_still_accumulates(self, handle):
"""tool_status_update still accumulates _tool_history without source_message_id."""
handle.card_message_id = None
handle._cardkit_card_id = None
handle.source_message_id = None
handle.tool_status_update([
{"name": "Bash", "hint_data": "git status"},
])
assert len(handle._tool_history) == 1
assert handle._tool_history[0]["label"] == "执行命令"
handle._render_progress.assert_called_once()
def test_repeat_after_subagent_creates_new_entry(self, handle):
"""Subagent is no longer skipped, so following tools are separate entries."""
handle.tool_status_update([
{"name": "WebFetch", "hint_data": "https://example.com"},
])
handle.tool_status_update([
{"name": "Subagent", "hint_data": ""},
])
handle.tool_status_update([
{"name": "WebFetch", "hint_data": "https://example.com"},
])
assert len(handle._tool_history) == 3
assert [e["name"] for e in handle._tool_history] == ["WebFetch", "Subagent", "WebFetch"]
assert handle._tool_history[-1]["status"] == "running"
def test_mark_agents_completed_marks_not_clears(self, handle):
"""_mark_agents_completed should mark agents as completed, not clear them."""
handle._active_agents = [
{"status": "in_progress", "description": "分析代码", "subagent_type": "scout"},
]
handle._mark_agents_completed()
assert len(handle._active_agents) == 1
assert handle._active_agents[0]["status"] == "completed"
def test_agent_list_update_then_completed_renders_strikethrough(self, handle):
"""Full lifecycle: launch → complete → rendered as strikethrough."""
handle._active_agents = []
handle.agent_list_update([
{"description": "分析代码", "name": None, "subagent_type": "scout"},
])
assert len(handle._active_agents) == 1
assert handle._active_agents[0]["status"] == "in_progress"
handle._mark_agents_completed()
assert handle._active_agents[0]["status"] == "completed"
# ---- Fix D: _send_tool_card real path (no main card) ----
def test_send_tool_card_no_main_card_replies_with_collapsible_panel():
"""Tool card is sent via IM reply when no main card exists.
Verifies the full path: tool_status_update → _render_progress →
_render_tool_progress → _update_tool_card → _send_tool_card →
client.im.v1.message.reply.
"""
from unittest.mock import MagicMock
from collections import deque
from feishu_bridge.ui import ResponseHandle
handle = ResponseHandle.__new__(ResponseHandle)
handle._tool_history = deque(maxlen=8)
handle._terminated = False
handle._summary_updated = False
handle._active_agents = []
handle._last_todos = None
handle.card_message_id = None
handle._cardkit_card_id = None
handle._cardkit_seq = 0
handle.thread_id = None
handle.source_message_id = "om_test_src"
handle._tool_msg_id = None
# Mock the IM client; _update_summary / _render_agent_progress safely
# no-op when _cardkit_card_id is None.
mock_client = MagicMock()
mock_reply_resp = MagicMock()
mock_reply_resp.success.return_value = True
mock_reply_resp.data.message_id = "msg-tool-test-999"
mock_client.im.v1.message.reply.return_value = mock_reply_resp
handle.client = mock_client
handle.tool_status_update([
{"name": "Bash", "hint_data": "git status"},
])
# _tool_history grows
assert len(handle._tool_history) == 1
assert handle._tool_history[0]["label"] == "执行命令"
# client.im.v1.message.reply was called
mock_client.im.v1.message.reply.assert_called_once()
# The card content sent to IM must contain collapsible_panel
call_args = mock_client.im.v1.message.reply.call_args
req = call_args[0][0] # ReplyMessageRequest
body = req.request_body # ReplyMessageRequestBody
assert "collapsible_panel" in body.content
# _tool_msg_id is set from the successful reply
assert handle._tool_msg_id == "msg-tool-test-999"
# ---- F-1: tool_status_end_update ----
class TestToolStatusEndUpdate:
"""Tests for ResponseHandle.tool_status_end_update."""
@pytest.fixture
def handle(self):
h = ResponseHandle.__new__(ResponseHandle)
h._cardkit_card_id = None
h._tool_history = deque(maxlen=8)
h._tool_msg_id = None
h._seq_lock = threading.Lock()
h._terminated = False
h._summary_updated = False
h._active_agents = []
h._last_todos = None
# Minimal mock so _render_progress doesn't crash
h._update_tool_card = Mock()
h._update_element = Mock()
return h
def test_single_call_start_then_end(self, handle):
"""start → status=running + tool_call_ids={"A"};
end_update(["A"]) → status=done + tool_call_ids=set()."""
handle.tool_status_update([
{"name": "Read", "hint_data": "/a.py", "id": "A"},
])
assert handle._tool_history[0]["status"] == "running"
assert handle._tool_history[0]["tool_call_ids"] == {"A"}
handle.tool_status_end_update(["A"])
assert handle._tool_history[0]["status"] == "done"
assert handle._tool_history[0]["tool_call_ids"] == set()
def test_aggregated_two_starts_then_ends(self, handle):
"""Same label+hint ×2 → count=2 + tool_call_ids={"A","B"};
end A → still running (ids={"B"}); end B → done (ids=set())."""
handle.tool_status_update([
{"name": "Read", "hint_data": "/x.py", "id": "A"},
])
handle.tool_status_update([
{"name": "Read", "hint_data": "/x.py", "id": "B"},
])
assert len(handle._tool_history) == 1
entry = handle._tool_history[0]
assert entry["count"] == 2
assert entry["tool_call_ids"] == {"A", "B"}
assert entry["status"] == "running"
handle.tool_status_end_update(["A"])
assert entry["status"] == "running" # B still pending
assert entry["tool_call_ids"] == {"B"}
handle.tool_status_end_update(["B"])
assert entry["status"] == "done"
assert entry["tool_call_ids"] == set()
def test_id_not_found_no_raise(self, handle, caplog):
"""end_update(["X"]) where X is not in any entry → log warning, no raise."""
handle.tool_status_update([
{"name": "Read", "hint_data": "/a.py", "id": "A"},
])
with caplog.at_level(logging.WARNING):
handle.tool_status_end_update(["X"])
assert "X not found in history" in caplog.text
# Original entry untouched
assert handle._tool_history[0]["status"] == "running"
assert handle._tool_history[0]["tool_call_ids"] == {"A"}
def test_empty_end_ids_noop(self, handle):
"""Empty list → no-op."""
handle.tool_status_update([
{"name": "Read", "hint_data": "/a.py", "id": "A"},
])
handle.tool_status_end_update([])
assert handle._tool_history[0]["status"] == "running"
def test_start_without_id_then_end_without_id(self, handle, caplog):
"""Entry without tool_call_id survives end_update (no matching)."""
handle.tool_status_update([
{"name": "Read", "hint_data": "/a.py"},
])
# Entry has empty tool_call_ids set (no id)
assert handle._tool_history[0]["tool_call_ids"] == set()
with caplog.at_level(logging.WARNING):
handle.tool_status_end_update(["nonexistent"])
assert "nonexistent not found in history" in caplog.text
# status unchanged (can't be marked done without end event; F-3 handles it)
assert handle._tool_history[0]["status"] == "running"
# ---- F-3: throttle removal / _update_tool_card immediate ----
class TestUpdateToolCardNoThrottle:
"""F-3.2: _update_tool_card patches immediately, no throttle."""
@pytest.fixture
def handle(self):
h = ResponseHandle.__new__(ResponseHandle)
h._tool_history = deque(maxlen=8)
h._tool_msg_id = "om_tool_existing"
h._terminated = False
h.client = MagicMock()
# Mock patch to succeed (correct path is im.v1.message.patch)
mock_resp = MagicMock()
mock_resp.success.return_value = True
h.client.im.v1.message.patch.return_value = mock_resp
return h
def test_update_tool_card_no_throttle_patches_every_update(self, handle):
"""5 consecutive _update_tool_card calls → 5 patch calls (no throttle)."""
mock_patch = handle.client.im.v1.message.patch
for i in range(5):
panels = [{"tag": "markdown", "content": f"panel_{i}"}]
handle._update_tool_card(panels)
assert mock_patch.call_count == 5
# Last call should contain the 5th panel
last_call = mock_patch.call_args_list[-1]
req = last_call[0][0]
body_content = req.request_body.content
assert "panel_4" in body_content
# ---- F-3: _finalize_tool_card ----
class TestFinalizeToolCard:
"""F-3.3: _finalize_tool_card marks done and force-patches once."""
@pytest.fixture
def handle(self):
h = ResponseHandle.__new__(ResponseHandle)
h._tool_history = deque(maxlen=8)
h._tool_msg_id = "om_tool_final"
h._tool_card_finalized = False
h._terminated = False
h._seq_lock = threading.Lock()
h._cardkit_card_id = None
h._active_agents = []
h.client = MagicMock()
mock_resp = MagicMock()
mock_resp.success.return_value = True
h.client.im.v1.message.patch.return_value = mock_resp
return h
def test_finalize_tool_card_marks_done_and_force_patches_once(self, handle):
"""2 running entries → finalize marks done, force-patches once."""
# Set up 2 running entries with tool_call_ids
handle._tool_history.append({
"name": "Read", "label": "读取文件", "hint": "foo.py",
"count": 1, "status": "running",
"tool_call_ids": {"A"},
})
handle._tool_history.append({
"name": "Bash", "label": "执行命令", "hint": "git status",
"count": 1, "status": "running",
"tool_call_ids": {"B"},
})
# Call twice — second should no-op
handle._finalize_tool_card()
handle._finalize_tool_card()
# All entries marked done
assert handle._tool_history[0]["status"] == "done"
assert handle._tool_history[0]["tool_call_ids"] == set()
assert handle._tool_history[1]["status"] == "done"
assert handle._tool_history[1]["tool_call_ids"] == set()
# Patch called only once
mock_patch = handle.client.im.v1.message.patch
assert mock_patch.call_count == 1
# status_text = "**完成 (2)**"
call = mock_patch.call_args
req = call[0][0]
body_content = req.request_body.content
assert "**完成 (2)**" in body_content
def test_finalize_tool_card_no_msg_id_noop(self, handle):
"""When _tool_msg_id is None, finalize still marks done but no patch."""
handle._tool_msg_id = None
handle._tool_history.append({
"name": "Read", "label": "读取文件", "hint": "foo.py",
"count": 1, "status": "running",
"tool_call_ids": {"A"},
})
handle._finalize_tool_card()
assert handle._tool_history[0]["status"] == "done"
handle.client.im.v1.message.patch.assert_not_called()
def test_finalize_no_history_no_patch(self, handle):
"""Empty history → no patch."""
handle._finalize_tool_card()
handle.client.im.v1.message.patch.assert_not_called()
# ---- F-3: deliver() finally calls _finalize_tool_card ----
class TestDeliverCallsFinalize:
"""F-3.4: All deliver paths call _finalize_tool_card in finally."""
@pytest.fixture
def handle(self):
h = ResponseHandle.__new__(ResponseHandle)
h._use_cardkit = False
h._cardkit_card_id = None
h.card_message_id = "om_main"
h._terminated = False
h._typing_reaction_id = None
h._flush_ctrl = None
h._card_fallback_timer = None
h._tool_card_finalized = False
h._tool_msg_id = None
h._tool_history = deque(maxlen=8)
h._handle_start_time = 1000.0
h._seq_lock = threading.Lock()
h._cardkit_seq = 0
h.source_message_id = "om_src"
h.thread_id = None
h.chat_id = "oc_chat"
h.bot_id = "bot_1"
h._last_todos = None
h._active_agents = []
h.client = MagicMock()
# Mock _try_patch to succeed
h._try_patch = MagicMock(return_value=True)
# Capture _finalize_tool_card
h._finalize_tool_card = MagicMock()
h._deliver_im_patch = MagicMock(return_value=True)
return h
def test_deliver_calls_finalize_on_im_patch_path(self, handle):
"""Non-cardkit deliver → _finalize_tool_card called once."""
handle.deliver("hello")
handle._finalize_tool_card.assert_called_once()
def test_deliver_calls_finalize_on_cardkit_success(self, handle):
"""CardKit deliver success path → _finalize_tool_card called once."""
handle._use_cardkit = True
handle._cardkit_card_id = "ck_card_1"
# Mock cardkit settings + update to succeed
mock_settings_resp = MagicMock()
mock_settings_resp.success.return_value = True
handle.client.cardkit.v1.card.settings.return_value = mock_settings_resp
mock_update_resp = MagicMock()
mock_update_resp.success.return_value = True
handle.client.cardkit.v1.card.update.return_value = mock_update_resp
handle.deliver("hello")
handle._finalize_tool_card.assert_called_once()
def test_deliver_calls_finalize_on_cardkit_settings_failure_fallback(self, handle):
"""CardKit settings fail → fallback to IM → _finalize_tool_card."""
handle._use_cardkit = True
handle._cardkit_card_id = "ck_card_1"
mock_resp = MagicMock()
mock_resp.success.return_value = False
mock_resp.code = -1
mock_resp.msg = "error"
handle.client.cardkit.v1.card.settings.return_value = mock_resp
handle.deliver("hello")
handle._finalize_tool_card.assert_called_once()
# Fallback to IM was called
handle._deliver_im_patch.assert_called_once()
def test_deliver_calls_finalize_on_cardkit_update_failure_fallback(self, handle):
"""CardKit update fails → fallback to IM → _finalize_tool_card."""
handle._use_cardkit = True
handle._cardkit_card_id = "ck_card_1"
mock_settings_resp = MagicMock()
mock_settings_resp.success.return_value = True
handle.client.cardkit.v1.card.settings.return_value = mock_settings_resp
mock_update_resp = MagicMock()
mock_update_resp.success.return_value = False
mock_update_resp.code = -1
mock_update_resp.msg = "update error"
handle.client.cardkit.v1.card.update.return_value = mock_update_resp
handle.deliver("hello")
handle._finalize_tool_card.assert_called_once()
# Fallback to IM was called
handle._deliver_im_patch.assert_called_once()
# ---- F-5: main card tool panel fallback ----
class TestMainCardToolPanelFallback:
"""F-5: Main card only embeds tool panels on fallback (no standalone tool card)."""
@pytest.fixture
def handle(self):
h = ResponseHandle.__new__(ResponseHandle)
h._use_cardkit = True
h._cardkit_card_id = "ck_card_1"
h.card_message_id = "om_main"
h._handle_start_time = 1000.0
h._seq_lock = threading.Lock()
h._cardkit_seq = 0
h._terminated = False
h._typing_reaction_id = None
h._flush_ctrl = None
h._card_fallback_timer = None
h._tool_msg_id = None
h._tool_history = deque(maxlen=8)
h._tool_card_finalized = False
h.source_message_id = "om_src"
h.thread_id = None
h.chat_id = "oc_chat"
h.bot_id = "bot_1"
h._last_todos = None
h._active_agents = []
h.client = MagicMock()
# Mock _try_patch to succeed
h._try_patch = MagicMock(return_value=True)
# Capture build_cardkit_final_card for inspection
return h
def test_deliver_final_card_no_tool_panels_when_tool_card_exists(
self, handle, monkeypatch):
"""When _tool_msg_id exists, final card has no tool collapsible_panel."""
handle._tool_msg_id = "om_tool"
handle._tool_history.append({
"name": "Bash", "label": "执行命令", "hint": "git",
"count": 1, "status": "running",
"tool_call_ids": {"A"},
})
# Mock cardkit settings + update to succeed
mock_settings_resp = MagicMock()
mock_settings_resp.success.return_value = True
handle.client.cardkit.v1.card.settings.return_value = mock_settings_resp
captured_card = {}
def fake_update(req):
captured_card["data"] = json.loads(req.request_body.card.data)
mock_resp = MagicMock()
mock_resp.success.return_value = True
return mock_resp
handle.client.cardkit.v1.card.update = fake_update
handle.deliver("hello")
body_elements = captured_card["data"].get("body", {}).get("elements", [])
contains_collapsible = any(
el.get("tag") == "collapsible_panel" for el in body_elements
)
assert not contains_collapsible, (
"Final card should NOT contain tool collapsible_panel "
"when standalone tool card exists"
)
def test_deliver_final_card_fallback_embeds_tool_panels_when_no_tool_card(
self, handle, monkeypatch):
"""When _tool_msg_id=None, final card includes tool collapsible_panel."""
handle._tool_msg_id = None
handle._tool_history.append({
"name": "Bash", "label": "执行命令", "hint": "git",
"count": 1, "status": "running",
"tool_call_ids": {"A"},
})
mock_settings_resp = MagicMock()
mock_settings_resp.success.return_value = True
handle.client.cardkit.v1.card.settings.return_value = mock_settings_resp
captured_card = {}
def fake_update(req):
captured_card["data"] = json.loads(req.request_body.card.data)
mock_resp = MagicMock()
mock_resp.success.return_value = True
return mock_resp
handle.client.cardkit.v1.card.update = fake_update
handle.deliver("hello")
body_elements = captured_card["data"].get("body", {}).get("elements", [])
contains_collapsible = any(
el.get("tag") == "collapsible_panel" for el in body_elements
)
assert contains_collapsible, (
"Final card SHOULD contain tool collapsible_panel "
"as fallback when standalone tool card was never created"
)
# ---- F-3.6: _build_tool_panels_for_streaming header dynamic ----
class TestBuildToolPanelsForStreamingHeaders:
"""F-3.6: Panel headers show ✅ for done, ⏳ for running."""
@pytest.fixture
def handle(self):
h = ResponseHandle.__new__(ResponseHandle)
h._tool_history = deque(maxlen=8)
return h
def test_build_tool_panels_for_streaming_done_headers(self, handle):
"""Done entry → header content contains ✅; running → ⏳."""
handle._tool_history.append({
"name": "Read", "label": "读取文件", "hint": "a.py",
"count": 1, "status": "running",
"tool_call_ids": set(),
})
handle._tool_history.append({
"name": "Bash", "label": "执行命令", "hint": "git",
"count": 1, "status": "done",
"tool_call_ids": set(),
})
panels = handle._build_tool_panels_for_streaming()
assert len(panels) == 2
# First panel (running) → ⏳
header_0 = panels[0]["header"]["title"]["content"]
assert "⏳" in header_0
# Second panel (done) → ✅
header_1 = panels[1]["header"]["title"]["content"]
assert "✅" in header_1
def test_all_done_panels_have_checkmark(self, handle):
"""After _finalize_tool_card, all panels show ✅."""
handle._tool_history.append({
"name": "Read", "label": "读取文件", "hint": "a.py",
"count": 1, "status": "done",
"tool_call_ids": set(),
})
handle._tool_history.append({
"name": "Bash", "label": "执行命令", "hint": "git",
"count": 1, "status": "done",
"tool_call_ids": set(),
})
panels = handle._build_tool_panels_for_streaming()
for p in panels:
header = p["header"]["title"]["content"]
assert "✅" in header
assert "⏳" not in header
# ============================================================
# Regression: bridge live bugs (2026-06-18)
# ============================================================
class TestToolStatusUpdateNotGatedBySummary:
"""Regression: ``_summary_updated`` must NOT silently drop tool updates.
The flag is one-shot for the CardKit summary patch (思考中→输入中). Earlier
the same flag also gated ``tool_status_update``, so every tool that fired
AFTER the first ``text_delta`` was silently dropped — the user saw no tool
card and the standalone tool card was never created. Their ``toolcall_end``
counterparts then logged ``id ... not found in history`` warnings.
"""
@pytest.fixture
def handle(self):
h = ResponseHandle.__new__(ResponseHandle)
h._tool_history = deque(maxlen=8)
h._tool_msg_id = None
h._terminated = False
h._summary_updated = True # first text_delta already streamed
h._cardkit_card_id = None
h._active_agents = []
h._last_todos = None
h._seq_lock = threading.Lock()
h.client = MagicMock()
mock_resp = MagicMock()
mock_resp.success.return_value = True
h.client.im.v1.message.create.return_value = mock_resp
h.client.im.v1.message.create.return_value.data.message_id = "om_tool_new"
# _send_tool_card is the slow path; stub it out so we just check history
h._send_tool_card = MagicMock(return_value="om_tool_new")
return h
def test_tool_status_update_runs_after_summary_updated(self, handle):
"""summary_updated=True must NOT skip; entry lands in history."""
handle.tool_status_update([