-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathpi-coding-agent-input-test.el
More file actions
4315 lines (4083 loc) · 207 KB
/
Copy pathpi-coding-agent-input-test.el
File metadata and controls
4315 lines (4083 loc) · 207 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
;;; pi-coding-agent-input-test.el --- Tests for pi-coding-agent-input -*- lexical-binding: t; -*-
;; Copyright (C) 2026 Daniel Nouri
;; Author: Daniel Nouri <daniel.nouri@gmail.com>
;;; Commentary:
;; Tests for input history, history isearch, send/abort commands,
;; message queuing, file reference completion, path completion,
;; and slash command completion — the input buffer layer.
;;; Code:
(require 'ert)
(require 'pi-coding-agent)
(require 'pi-coding-agent-test-common)
;;; Sending Prompts
(ert-deftest pi-coding-agent-test-send-extracts-text ()
"pi-coding-agent-send extracts text from input buffer and clears it."
(let ((sent-text nil))
(pi-coding-agent-test-with-mock-session "/tmp/pi-coding-agent-test-send1/"
(cl-letf (((symbol-function 'pi-coding-agent--send-prompt)
(lambda (text &optional on-success &rest _)
(setq sent-text text)
(when on-success (funcall on-success)))))
(with-current-buffer "*pi-coding-agent-input:/tmp/pi-coding-agent-test-send1/*"
(insert "Hello, pi!")
(pi-coding-agent-send)
(should (equal sent-text "Hello, pi!"))
(should (string-empty-p (buffer-string))))))))
(ert-deftest pi-coding-agent-test-send-empty-is-noop ()
"pi-coding-agent-send with empty buffer does nothing."
(let ((send-called nil))
(pi-coding-agent-test-with-mock-session "/tmp/pi-coding-agent-test-send2/"
(cl-letf (((symbol-function 'pi-coding-agent--send-prompt)
(lambda (_) (setq send-called t))))
(with-current-buffer "*pi-coding-agent-input:/tmp/pi-coding-agent-test-send2/*"
(pi-coding-agent-send)
(should-not send-called))))))
(ert-deftest pi-coding-agent-test-send-whitespace-only-is-noop ()
"pi-coding-agent-send with only whitespace does nothing."
(let ((send-called nil))
(pi-coding-agent-test-with-mock-session "/tmp/pi-coding-agent-test-send3/"
(cl-letf (((symbol-function 'pi-coding-agent--send-prompt)
(lambda (_) (setq send-called t))))
(with-current-buffer "*pi-coding-agent-input:/tmp/pi-coding-agent-test-send3/*"
(insert " \n\t ")
(pi-coding-agent-send)
(should-not send-called))))))
(ert-deftest pi-coding-agent-test-send-queues-locally-while-streaming ()
"pi-coding-agent-send adds to local queue while streaming, no RPC sent."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-queue-stream*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-queue-stream-input*"))
(rpc-called nil)
(message-shown nil))
(unwind-protect
(progn
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(setq pi-coding-agent--status 'streaming)
(setq pi-coding-agent--input-buffer input-buf)
(setq pi-coding-agent--followup-queue nil))
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(setq pi-coding-agent--chat-buffer chat-buf)
(insert "My message")
(cl-letf (((symbol-function 'pi-coding-agent--rpc-async)
(lambda (_proc _cmd _cb) (setq rpc-called t)))
((symbol-function 'message)
(lambda (fmt &rest _)
(when (and fmt (string-match-p "queued" (downcase fmt)))
(setq message-shown t)))))
(pi-coding-agent-send))
;; Should NOT have called RPC (local queue instead)
(should-not rpc-called)
;; Should have added to local queue in chat buffer
(with-current-buffer chat-buf
(should (equal pi-coding-agent--followup-queue '("My message"))))
;; Should have shown queued message
(should message-shown)
;; Input should be cleared (message accepted)
(should (string-empty-p (buffer-string)))))
(kill-buffer chat-buf)
(kill-buffer input-buf))))
(ert-deftest pi-coding-agent-test-send-queues-locally-while-sending ()
"pi-coding-agent-send adds to local queue while waiting for agent_start."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-queue-sending*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-queue-sending-input*"))
(rpc-called nil))
(unwind-protect
(progn
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(setq pi-coding-agent--status 'sending)
(setq pi-coding-agent--input-buffer input-buf)
(setq pi-coding-agent--followup-queue nil))
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(setq pi-coding-agent--chat-buffer chat-buf)
(insert "Queued while retry is starting")
(cl-letf (((symbol-function 'pi-coding-agent--rpc-async)
(lambda (_proc _cmd _cb) (setq rpc-called t)))
((symbol-function 'message) #'ignore))
(pi-coding-agent-send))
(should-not rpc-called)
(with-current-buffer chat-buf
(should (equal pi-coding-agent--followup-queue
'("Queued while retry is starting"))))
(should (string-empty-p (buffer-string)))))
(kill-buffer chat-buf)
(kill-buffer input-buf))))
(ert-deftest pi-coding-agent-test-send-refuses-during-session-transition ()
"A prompt typed while switching sessions is not queued into either session."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-send-transition*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-send-transition-input*"))
(shown-message nil))
(unwind-protect
(progn
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(setq pi-coding-agent--status 'idle
pi-coding-agent--input-buffer input-buf
pi-coding-agent--followup-queue nil)
(pi-coding-agent--begin-session-transition 'mock-proc))
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(setq pi-coding-agent--chat-buffer chat-buf)
(insert "not yet")
(cl-letf (((symbol-function 'message)
(lambda (fmt &rest args)
(setq shown-message (apply #'format fmt args)))))
(pi-coding-agent-send))
(should (equal (buffer-string) "not yet")))
(with-current-buffer chat-buf
(should (null pi-coding-agent--followup-queue)))
(should (equal shown-message
"Pi: Cannot send while session is switching")))
(kill-buffer chat-buf)
(kill-buffer input-buf))))
(ert-deftest pi-coding-agent-test-send-refuses-builtin-command-while-busy ()
"Client-side slash commands are not hidden in the follow-up queue."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-builtin-busy*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-builtin-busy-input*"))
(shown-message nil))
(unwind-protect
(progn
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(setq pi-coding-agent--status 'streaming
pi-coding-agent--input-buffer input-buf
pi-coding-agent--followup-queue nil))
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(setq pi-coding-agent--chat-buffer chat-buf)
(insert "/new")
(cl-letf (((symbol-function 'message)
(lambda (fmt &rest args)
(setq shown-message (apply #'format fmt args)))))
(pi-coding-agent-send))
(should (equal (buffer-string) "/new")))
(with-current-buffer chat-buf
(should (null pi-coding-agent--followup-queue)))
(should (equal shown-message "Pi: Cannot queue /new while Pi is busy")))
(kill-buffer chat-buf)
(kill-buffer input-buf))))
(ert-deftest pi-coding-agent-test-send-queues-locally-while-compacting ()
"pi-coding-agent-send adds to local queue while compacting, no RPC sent."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-queue-compact*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-queue-compact-input*"))
(rpc-called nil)
(message-shown nil))
(unwind-protect
(progn
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(setq pi-coding-agent--status 'compacting)
(setq pi-coding-agent--input-buffer input-buf)
(setq pi-coding-agent--followup-queue nil))
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(setq pi-coding-agent--chat-buffer chat-buf)
(insert "My message during compaction")
(cl-letf (((symbol-function 'pi-coding-agent--rpc-async)
(lambda (_proc _cmd _cb) (setq rpc-called t)))
((symbol-function 'message)
(lambda (fmt &rest _)
(when (and fmt (string-match-p "queued" (downcase fmt)))
(setq message-shown t)))))
(pi-coding-agent-send))
;; Should NOT have called RPC (local queue instead)
(should-not rpc-called)
;; Should have added to local queue in chat buffer
(with-current-buffer chat-buf
(should (equal pi-coding-agent--followup-queue '("My message during compaction"))))
;; Should have shown queued message
(should message-shown)
;; Input should be cleared (message accepted)
(should (string-empty-p (buffer-string)))))
(kill-buffer chat-buf)
(kill-buffer input-buf))))
(ert-deftest pi-coding-agent-test-slash-compact-handled-locally-not-sent-as-prompt ()
"/compact in input buffer invokes pi-coding-agent-compact locally, not sent to pi."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-slash-compact*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-slash-compact-input*"))
(compact-called nil)
(compact-args nil)
(prompt-sent nil))
(unwind-protect
(progn
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(setq pi-coding-agent--status 'idle)
(setq pi-coding-agent--input-buffer input-buf))
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(setq pi-coding-agent--chat-buffer chat-buf)
(insert "/compact")
(cl-letf (((symbol-function 'pi-coding-agent-compact)
(lambda (&optional args) (setq compact-called t compact-args args)))
((symbol-function 'pi-coding-agent--send-prompt)
(lambda (_) (setq prompt-sent t))))
(pi-coding-agent-send))
;; Should have called compact function with no args
(should compact-called)
(should (null compact-args))
;; Should NOT have sent as prompt
(should-not prompt-sent)
;; Input should be cleared
(should (string-empty-p (buffer-string)))))
(kill-buffer chat-buf)
(kill-buffer input-buf))))
(ert-deftest pi-coding-agent-test-slash-compact-with-args-passes-instructions ()
"/compact with args passes custom instructions to compact function."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-slash-compact-args*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-slash-compact-args-input*"))
(compact-called nil)
(compact-args nil))
(unwind-protect
(progn
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(setq pi-coding-agent--status 'idle)
(setq pi-coding-agent--input-buffer input-buf))
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(setq pi-coding-agent--chat-buffer chat-buf)
(insert "/compact focus on the API design decisions")
(cl-letf (((symbol-function 'pi-coding-agent-compact)
(lambda (&optional args) (setq compact-called t compact-args args))))
(pi-coding-agent-send))
;; Should have called compact function with custom instructions
(should compact-called)
(should (equal compact-args "focus on the API design decisions"))))
(kill-buffer chat-buf)
(kill-buffer input-buf))))
(ert-deftest pi-coding-agent-test-compaction-success-without-retry-sends-queued-message ()
"Successful non-retry compaction schedules the oldest queued follow-up.
Uses :false (JSON false representation) to verify boolean normalization."
(with-temp-buffer
(pi-coding-agent-chat-mode)
(let ((sent-text nil)
drain-callback
drain-args)
(setq pi-coding-agent--status 'compacting)
(setq pi-coding-agent--followup-queue '("queued message"))
(cl-letf (((symbol-function 'pi-coding-agent--send-prompt)
(lambda (text &optional on-success &rest _)
(setq sent-text text)
(when on-success (funcall on-success))))
((symbol-function 'run-at-time)
(lambda (_secs _repeat fn &rest args)
(setq drain-callback fn
drain-args args)
'fake-drain-timer)))
(pi-coding-agent--handle-display-event
'(:type "compaction_end"
:reason "threshold"
:aborted :false
:willRetry :false
:result (:tokensBefore 1000 :summary "Summary" :timestamp 1234567890000)))
(should (eq pi-coding-agent--status 'idle))
(should (functionp drain-callback))
(should (equal pi-coding-agent--followup-queue '("queued message")))
(should (null sent-text))
(apply drain-callback drain-args))
;; Queue should be empty after processing.
(should (null pi-coding-agent--followup-queue))
;; The queued message should have been sent.
(should (equal sent-text "queued message")))))
(ert-deftest pi-coding-agent-test-compaction-success-without-retry-preserves-fifo ()
"Successful non-retry compaction schedules one follow-up in FIFO order."
(with-temp-buffer
(pi-coding-agent-chat-mode)
(let ((sent-text nil)
drain-callback
drain-args)
(setq pi-coding-agent--status 'compacting)
(dolist (text '("First" "Second" "Third"))
(pi-coding-agent--push-followup text))
(cl-letf (((symbol-function 'pi-coding-agent--send-prompt)
(lambda (text &optional on-success &rest _)
(setq sent-text text)
(when on-success (funcall on-success))))
((symbol-function 'run-at-time)
(lambda (_secs _repeat fn &rest args)
(setq drain-callback fn
drain-args args)
'fake-drain-timer)))
(pi-coding-agent--handle-display-event
'(:type "compaction_end"
:reason "manual"
:aborted :false
:willRetry :false
:result (:tokensBefore 1000 :summary "Summary")))
(should (equal pi-coding-agent--followup-queue '("Third" "Second" "First")))
(should (null sent-text))
(apply drain-callback drain-args))
(should (equal sent-text "First"))
(should (equal pi-coding-agent--followup-queue '("Third" "Second"))))))
(ert-deftest pi-coding-agent-test-overflow-compaction-will-retry-preserves-followup-queue ()
"Overflow compaction with automatic retry keeps local follow-ups queued."
(with-temp-buffer
(pi-coding-agent-chat-mode)
(let ((sent-text nil))
(setq pi-coding-agent--status 'compacting)
(setq pi-coding-agent--followup-queue '("queued behind retry"))
(cl-letf (((symbol-function 'pi-coding-agent--send-prompt)
(lambda (text &optional on-success &rest _)
(setq sent-text text)
(when on-success (funcall on-success)))))
(pi-coding-agent--handle-display-event
'(:type "compaction_end"
:reason "overflow"
:aborted :false
:willRetry t
:result (:tokensBefore 1000 :summary "Summary"))))
(should (eq pi-coding-agent--status 'sending))
(should (equal pi-coding-agent--followup-queue '("queued behind retry")))
(should (null sent-text)))))
(ert-deftest pi-coding-agent-test-overflow-compaction-retry-drains-queue-after-agent-end ()
"Queued follow-ups wait for Pi's automatic retry turn to finish."
(with-temp-buffer
(pi-coding-agent-chat-mode)
(let ((sent-prompts nil)
drain-callback
drain-args)
(setq pi-coding-agent--status 'compacting)
(setq pi-coding-agent--followup-queue '("queued behind retry"))
(cl-letf (((symbol-function 'pi-coding-agent--send-prompt)
(lambda (text &optional on-success &rest _)
(push text sent-prompts)
(when on-success (funcall on-success))))
((symbol-function 'pi-coding-agent--refresh-header) #'ignore)
((symbol-function 'run-at-time)
(lambda (_secs _repeat fn &rest args)
(setq drain-callback fn
drain-args args)
'fake-drain-timer)))
(pi-coding-agent--handle-display-event
'(:type "compaction_end"
:reason "overflow"
:aborted :false
:willRetry t
:result (:tokensBefore 1000 :summary "Summary")))
(should (null sent-prompts))
(should (equal pi-coding-agent--followup-queue '("queued behind retry")))
(should (null drain-callback))
(pi-coding-agent--handle-display-event '(:type "agent_start"))
(should (eq pi-coding-agent--status 'streaming))
(pi-coding-agent--handle-display-event '(:type "agent_end" :messages []))
(should (functionp drain-callback))
(apply drain-callback drain-args))
(should (equal (reverse sent-prompts) '("queued behind retry")))
(should (null pi-coding-agent--followup-queue)))))
(ert-deftest pi-coding-agent-test-compaction-end-before-agent-start-defers-followup-drain ()
"Queued follow-ups do not overtake Pi work that starts after compaction_end."
(with-temp-buffer
(pi-coding-agent-chat-mode)
(let ((sent-prompts nil)
first-drain-callback
first-drain-args
second-drain-callback
second-drain-args)
(setq pi-coding-agent--status 'compacting
pi-coding-agent--followup-queue '("local follow-up after Pi queue"))
(cl-letf (((symbol-function 'pi-coding-agent--send-prompt)
(lambda (text &optional on-success &rest _)
(push text sent-prompts)
(when on-success (funcall on-success))))
((symbol-function 'pi-coding-agent--refresh-header) #'ignore)
((symbol-function 'run-at-time)
(lambda (_secs _repeat fn &rest args)
(if first-drain-callback
(setq second-drain-callback fn
second-drain-args args)
(setq first-drain-callback fn
first-drain-args args))
'fake-drain-timer)))
;; Pi can emit compaction_end and then immediately begin work it already
;; owns. Local follow-ups must wait for that visible agent turn.
(pi-coding-agent--handle-display-event
'(:type "compaction_end"
:reason "threshold"
:aborted :false
:willRetry :false
:result (:tokensBefore 1000 :summary "Summary")))
(should (eq pi-coding-agent--status 'idle))
(should (functionp first-drain-callback))
(should (null sent-prompts))
(should (equal pi-coding-agent--followup-queue
'("local follow-up after Pi queue")))
(pi-coding-agent--handle-display-event '(:type "agent_start"))
;; A stale scheduled drain may still fire, but streaming status makes it
;; a no-op.
(apply first-drain-callback first-drain-args)
(should (null sent-prompts))
(should (equal pi-coding-agent--followup-queue
'("local follow-up after Pi queue")))
(pi-coding-agent--handle-display-event '(:type "agent_end" :messages []))
(should (functionp second-drain-callback))
(apply second-drain-callback second-drain-args))
(should (equal (reverse sent-prompts)
'("local follow-up after Pi queue")))
(should (null pi-coding-agent--followup-queue)))))
(ert-deftest pi-coding-agent-test-agent-end-before-compaction-defers-followup-drain ()
"Queued follow-ups do not overtake compaction that starts after agent_end."
(with-temp-buffer
(pi-coding-agent-chat-mode)
(let ((sent-prompts nil)
drain-callback
drain-args)
(setq pi-coding-agent--status 'streaming
pi-coding-agent--followup-queue '("queued until compaction settles"))
(cl-letf (((symbol-function 'pi-coding-agent--send-prompt)
(lambda (text &optional on-success &rest _)
(push text sent-prompts)
(when on-success (funcall on-success))))
((symbol-function 'pi-coding-agent--refresh-header) #'ignore)
((symbol-function 'run-at-time)
(lambda (_secs _repeat fn &rest args)
(setq drain-callback fn
drain-args args)
'fake-drain-timer)))
;; Pi emits agent_end before post-run compaction_start. The local
;; queue must wait long enough for that compaction event to claim the
;; next ordering slot.
(pi-coding-agent--handle-display-event '(:type "agent_end" :messages []))
(should (eq pi-coding-agent--status 'idle))
(should (functionp drain-callback))
(should (null sent-prompts))
(should (equal pi-coding-agent--followup-queue
'("queued until compaction settles")))
(pi-coding-agent--handle-display-event '(:type "compaction_start" :reason "threshold"))
;; A stale scheduled drain may still fire, but compacting status makes
;; it a no-op.
(apply drain-callback drain-args)
(should (null sent-prompts))
(should (equal pi-coding-agent--followup-queue
'("queued until compaction settles")))
(setq drain-callback nil
drain-args nil)
(pi-coding-agent--handle-display-event
'(:type "compaction_end"
:reason "threshold"
:aborted :false
:willRetry :false
:result (:tokensBefore 1000 :summary "Summary")))
(should (functionp drain-callback))
(apply drain-callback drain-args))
(should (equal (reverse sent-prompts)
'("queued until compaction settles")))
(should (null pi-coding-agent--followup-queue)))))
(ert-deftest pi-coding-agent-test-preflight-compaction-keeps-followups-behind-prompt ()
"Compaction during prompt preflight must not drain local follow-ups."
(with-temp-buffer
(pi-coding-agent-chat-mode)
(let ((sent-prompts nil)
drain-callback)
(setq pi-coding-agent--status 'sending
pi-coding-agent--pre-compaction-status nil
pi-coding-agent--followup-queue '("queued behind original prompt"))
(cl-letf (((symbol-function 'pi-coding-agent--send-prompt)
(lambda (text &optional on-success &rest _)
(push text sent-prompts)
(when on-success (funcall on-success))))
((symbol-function 'run-at-time)
(lambda (_secs _repeat fn &rest _args)
(setq drain-callback fn)
'fake-drain-timer)))
(pi-coding-agent--handle-display-event
'(:type "compaction_start" :reason "threshold"))
(pi-coding-agent--handle-display-event
'(:type "compaction_end"
:reason "threshold"
:aborted :false
:willRetry :false
:result (:tokensBefore 1000 :summary "Summary")))
(should (eq pi-coding-agent--status 'sending))
(should (null drain-callback))
(should (null sent-prompts))
(should (equal pi-coding-agent--followup-queue
'("queued behind original prompt")))))))
(ert-deftest pi-coding-agent-test-failed-preflight-compaction-restores-followups-before-prompt-failure ()
"Failed preflight compaction restores follow-ups while prompt failure is pending."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-preflight-compaction-failure-chat*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-preflight-compaction-failure-input*"))
(sent-text nil))
(unwind-protect
(progn
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(setq pi-coding-agent--chat-buffer chat-buf))
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(let ((generation (pi-coding-agent--begin-prompt-start-wait)))
(setq pi-coding-agent--status 'sending
pi-coding-agent--input-buffer input-buf
pi-coding-agent--pre-compaction-status nil
pi-coding-agent--followup-queue '("follow-up after prompt"))
(cl-letf (((symbol-function 'pi-coding-agent--send-prompt)
(lambda (text &optional on-success &rest _)
(setq sent-text text)
(when on-success (funcall on-success))))
((symbol-function 'message) #'ignore))
(pi-coding-agent--handle-display-event
'(:type "compaction_start" :reason "threshold"))
(pi-coding-agent--handle-display-event
'(:type "compaction_end"
:reason "threshold"
:aborted :false
:willRetry :false
:result :null
:errorMessage "quota exceeded")))
(should (eq pi-coding-agent--status 'idle))
(should (equal pi-coding-agent--activity-phase "thinking"))
(should (pi-coding-agent--prompt-start-current-p generation))
(should (null pi-coding-agent--followup-queue))
(should (null sent-text))))
(with-current-buffer input-buf
(should (equal (buffer-string) "follow-up after prompt"))))
(kill-buffer chat-buf)
(kill-buffer input-buf))))
(ert-deftest pi-coding-agent-test-aborted-preflight-compaction-clears-followups-before-prompt-failure ()
"Aborted preflight compaction clears follow-ups while prompt failure is pending."
(with-temp-buffer
(pi-coding-agent-chat-mode)
(let ((sent-text nil))
(pi-coding-agent--begin-prompt-start-wait)
(setq pi-coding-agent--status 'sending
pi-coding-agent--pre-compaction-status nil
pi-coding-agent--followup-queue '("discarded follow-up"))
(cl-letf (((symbol-function 'pi-coding-agent--send-prompt)
(lambda (text &optional on-success &rest _)
(setq sent-text text)
(when on-success (funcall on-success))))
((symbol-function 'message) #'ignore))
(pi-coding-agent--handle-display-event
'(:type "compaction_start" :reason "threshold"))
(pi-coding-agent--handle-display-event
'(:type "compaction_end"
:reason "threshold"
:aborted t
:willRetry :false
:result nil)))
(should (eq pi-coding-agent--status 'idle))
(should (equal pi-coding-agent--activity-phase "thinking"))
(should (null pi-coding-agent--followup-queue))
(should (null sent-text)))))
(ert-deftest pi-coding-agent-test-failed-preflight-compaction-prompt-failure-restores-original ()
"Prompt RPC failure clears preflight wait after compaction failure."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-preflight-prompt-failure-chat*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-preflight-prompt-failure-input*"))
(rpc-callback nil))
(unwind-protect
(progn
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(setq pi-coding-agent--status 'idle
pi-coding-agent--activity-phase "idle"
pi-coding-agent--input-buffer input-buf
pi-coding-agent--followup-queue '("queued follow-up")))
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(setq pi-coding-agent--chat-buffer chat-buf)
(insert "original prompt"))
(cl-letf (((symbol-function 'pi-coding-agent--get-process)
(lambda () 'mock-proc))
((symbol-function 'process-live-p) (lambda (_) t))
((symbol-function 'pi-coding-agent--rpc-async)
(lambda (_proc _cmd callback)
(setq rpc-callback callback)))
((symbol-function 'message) #'ignore))
(with-current-buffer input-buf
(pi-coding-agent-send))
(with-current-buffer chat-buf
(pi-coding-agent--handle-display-event
'(:type "compaction_start" :reason "threshold"))
(pi-coding-agent--handle-display-event
'(:type "compaction_end"
:reason "threshold"
:aborted :false
:willRetry :false
:result :null
:errorMessage "quota exceeded"))
(should (pi-coding-agent--session-busy-p chat-buf))
(funcall rpc-callback '(:success :false :error "quota exceeded"))
(should (eq pi-coding-agent--status 'idle))
(should (equal pi-coding-agent--activity-phase "idle"))
(should-not (pi-coding-agent--session-busy-p chat-buf))))
(with-current-buffer input-buf
(should (equal (buffer-string)
"original prompt\n\nqueued follow-up"))))
(kill-buffer chat-buf)
(kill-buffer input-buf))))
(ert-deftest pi-coding-agent-test-agent-end-will-retry-preserves-followup-queue ()
"agent_end with willRetry keeps local follow-ups behind Pi's retry."
(with-temp-buffer
(pi-coding-agent-chat-mode)
(let ((sent-prompts nil)
drain-callback
drain-args)
(setq pi-coding-agent--status 'streaming
pi-coding-agent--followup-queue '("queued behind transient retry"))
(cl-letf (((symbol-function 'pi-coding-agent--send-prompt)
(lambda (text &optional on-success &rest _)
(push text sent-prompts)
(when on-success (funcall on-success))))
((symbol-function 'pi-coding-agent--refresh-header) #'ignore)
((symbol-function 'run-at-time)
(lambda (_secs _repeat fn &rest args)
(setq drain-callback fn
drain-args args)
'fake-drain-timer)))
(pi-coding-agent--handle-display-event
'(:type "agent_end" :messages [] :willRetry t))
(should (eq pi-coding-agent--status 'sending))
(should (null drain-callback))
(should (null sent-prompts))
(should (equal pi-coding-agent--followup-queue
'("queued behind transient retry")))
(pi-coding-agent--handle-display-event '(:type "agent_start"))
(pi-coding-agent--handle-display-event '(:type "agent_end" :messages []))
(should (functionp drain-callback))
(apply drain-callback drain-args))
(should (equal (reverse sent-prompts)
'("queued behind transient retry")))
(should (null pi-coding-agent--followup-queue)))))
(ert-deftest pi-coding-agent-test-compaction-failure-restores-queued-followups ()
"Failed compaction surfaces queued follow-ups for user recovery."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-compaction-failure-chat*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-compaction-failure-input*"))
(sent-text nil))
(unwind-protect
(progn
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(setq pi-coding-agent--chat-buffer chat-buf))
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(setq pi-coding-agent--status 'compacting
pi-coding-agent--input-buffer input-buf
pi-coding-agent--followup-queue '("recover me later"))
(cl-letf (((symbol-function 'pi-coding-agent--send-prompt)
(lambda (text &optional on-success &rest _)
(setq sent-text text)
(when on-success (funcall on-success))))
((symbol-function 'message) #'ignore))
(pi-coding-agent--handle-display-event
'(:type "compaction_end"
:reason "threshold"
:aborted :false
:willRetry :false
:result :null
:errorMessage "quota exceeded")))
(should (eq pi-coding-agent--status 'idle))
(should (null pi-coding-agent--followup-queue))
(should (null sent-text)))
(with-current-buffer input-buf
(should (equal (buffer-string) "recover me later"))))
(kill-buffer chat-buf)
(kill-buffer input-buf))))
(ert-deftest pi-coding-agent-test-auto-retry-failure-restores-queued-followups ()
"Final auto-retry failure surfaces queued follow-ups for user recovery."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-retry-failure-chat*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-retry-failure-input*")))
(unwind-protect
(progn
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(setq pi-coding-agent--chat-buffer chat-buf))
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(setq pi-coding-agent--status 'sending
pi-coding-agent--activity-phase "thinking"
pi-coding-agent--input-buffer input-buf
pi-coding-agent--followup-queue '("queued behind failed retry"))
(pi-coding-agent--handle-display-event
'(:type "auto_retry_end"
:success :false
:attempt 3
:finalError "overloaded"))
(should (eq pi-coding-agent--status 'idle))
(should (equal pi-coding-agent--activity-phase "idle"))
(should (null pi-coding-agent--followup-queue))
(should (string-match-p "Retry failed" (buffer-string))))
(with-current-buffer input-buf
(should (equal (buffer-string) "queued behind failed retry"))))
(kill-buffer chat-buf)
(kill-buffer input-buf))))
(ert-deftest pi-coding-agent-test-compaction-end-aborted-clears-queue ()
"compaction_end when aborted clears followup queue without sending."
(with-temp-buffer
(pi-coding-agent-chat-mode)
(let ((sent-text nil))
(setq pi-coding-agent--status 'compacting)
(setq pi-coding-agent--followup-queue '("queued message"))
(cl-letf (((symbol-function 'pi-coding-agent--send-prompt)
(lambda (text &optional on-success &rest _)
(setq sent-text text)
(when on-success (funcall on-success)))))
;; Simulate compaction_end event (aborted)
(pi-coding-agent--handle-display-event
'(:type "compaction_end"
:reason "threshold"
:aborted t)))
;; Queue should be cleared (user cancelled)
(should (null pi-coding-agent--followup-queue))
;; No message should have been sent
(should (null sent-text)))))
;;; Abort Command
(ert-deftest pi-coding-agent-test-abort-sends-command ()
"pi-coding-agent-abort sends abort command while streaming."
(with-temp-buffer
(pi-coding-agent-chat-mode)
(let ((sent-command nil)
(pi-coding-agent--status 'streaming))
(cl-letf (((symbol-function 'pi-coding-agent--get-process) (lambda () 'mock-proc))
((symbol-function 'pi-coding-agent--get-chat-buffer) (lambda () (current-buffer)))
((symbol-function 'pi-coding-agent--rpc-async)
(lambda (_proc cmd _cb) (setq sent-command cmd))))
(pi-coding-agent-abort)
(should (equal (plist-get sent-command :type) "abort"))
(should pi-coding-agent--aborted)))))
(ert-deftest pi-coding-agent-test-abort-sends-command-while-compacting ()
"pi-coding-agent-abort sends abort command while compacting."
(with-temp-buffer
(pi-coding-agent-chat-mode)
(let ((sent-command nil)
(pi-coding-agent--status 'compacting))
(cl-letf (((symbol-function 'pi-coding-agent--get-process) (lambda () 'mock-proc))
((symbol-function 'pi-coding-agent--get-chat-buffer) (lambda () (current-buffer)))
((symbol-function 'pi-coding-agent--rpc-async)
(lambda (_proc cmd _cb) (setq sent-command cmd))))
(pi-coding-agent-abort)
(should (equal (plist-get sent-command :type) "abort"))
(should-not pi-coding-agent--aborted)))))
(ert-deftest pi-coding-agent-test-abort-noop-when-idle ()
"pi-coding-agent-abort does nothing when idle."
(with-temp-buffer
(pi-coding-agent-chat-mode)
(let ((sent-command nil)
(pi-coding-agent--status 'idle))
(cl-letf (((symbol-function 'pi-coding-agent--get-process) (lambda () 'mock-proc))
((symbol-function 'pi-coding-agent--get-chat-buffer) (lambda () (current-buffer)))
((symbol-function 'pi-coding-agent--rpc-async)
(lambda (_proc cmd _cb) (setq sent-command cmd))))
(pi-coding-agent-abort)
(should (null sent-command))))))
(ert-deftest pi-coding-agent-test-abort-clears-followup-queue ()
"Aborting clears the follow-up queue so queued messages are not sent.
When user aborts, they want to stop everything - including queued messages."
(with-temp-buffer
(pi-coding-agent-chat-mode)
(let ((inhibit-read-only t)
(message-was-sent nil))
(insert "Some streaming content")
;; Set up state as if we're streaming with a queued message
(setq pi-coding-agent--aborted t
pi-coding-agent--followup-queue '("queued message that should be discarded"))
;; Mock send functions to detect if queue processing sends the message
(cl-letf (((symbol-function 'pi-coding-agent--prepare-and-send)
(lambda (_text) (setq message-was-sent t)))
((symbol-function 'pi-coding-agent--refresh-header) #'ignore))
;; Simulate agent_end arriving after abort
(pi-coding-agent--display-agent-end)
;; Queue should be empty (either cleared or not processed)
(should (null pi-coding-agent--followup-queue))
;; Key assertion: queued message should NOT have been sent
(should-not message-was-sent)))))
;;; Kill Buffer Protection
(ert-deftest pi-coding-agent-test-handler-removed-on-kill ()
"Event handler is removed when chat buffer is killed."
(with-temp-buffer
(pi-coding-agent-chat-mode)
(let ((fake-proc (start-process "test" nil "true")))
(unwind-protect
(progn
(setq pi-coding-agent--process fake-proc)
(pi-coding-agent--register-display-handler fake-proc)
(should (process-get fake-proc 'pi-coding-agent-display-handler))
(pi-coding-agent--cleanup-on-kill)
(should-not (process-get fake-proc 'pi-coding-agent-display-handler)))
(when (process-live-p fake-proc)
(delete-process fake-proc))))))
;;; Message Queuing
(ert-deftest pi-coding-agent-test-queue-steering-when-streaming-sends-steer ()
"Queue steering sends steer RPC command when agent is streaming."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-queue-steer*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-queue-steer-input*"))
(sent-command nil))
(unwind-protect
(progn
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(setq pi-coding-agent--status 'streaming)
(setq pi-coding-agent--input-buffer input-buf))
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(setq pi-coding-agent--chat-buffer chat-buf)
(insert "Please stop and focus on X")
(cl-letf (((symbol-function 'pi-coding-agent--get-process) (lambda () 'mock-proc))
((symbol-function 'process-live-p) (lambda (_) t))
((symbol-function 'pi-coding-agent--rpc-async)
(lambda (_proc cmd _cb) (setq sent-command cmd))))
(pi-coding-agent-queue-steering))
;; Should send steer command
(should sent-command)
(should (equal (plist-get sent-command :type) "steer"))
(should (equal (plist-get sent-command :message) "Please stop and focus on X"))
;; Input should be cleared
(should (string-empty-p (buffer-string)))))
(kill-buffer chat-buf)
(kill-buffer input-buf))))
(ert-deftest pi-coding-agent-test-queue-steering-when-sending-sends-steer ()
"Queue steering sends steer RPC while waiting for agent_start."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-queue-steer-sending*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-queue-steer-sending-input*"))
(sent-command nil))
(unwind-protect
(progn
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(setq pi-coding-agent--status 'sending)
(setq pi-coding-agent--input-buffer input-buf))
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(setq pi-coding-agent--chat-buffer chat-buf)
(insert "Steer the pending retry")
(cl-letf (((symbol-function 'pi-coding-agent--get-process) (lambda () 'mock-proc))
((symbol-function 'process-live-p) (lambda (_) t))
((symbol-function 'pi-coding-agent--rpc-async)
(lambda (_proc cmd _cb) (setq sent-command cmd))))
(pi-coding-agent-queue-steering))
(should sent-command)
(should (equal (plist-get sent-command :type) "steer"))
(should (equal (plist-get sent-command :message) "Steer the pending retry"))
(should (string-empty-p (buffer-string)))))
(kill-buffer chat-buf)
(kill-buffer input-buf))))
(ert-deftest pi-coding-agent-test-queue-steering-refuses-during-session-transition ()
"Steering must not bypass the session-transition send guard."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-steer-transition*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-steer-transition-input*"))
(steer-sent nil)
(shown-message nil))
(unwind-protect
(progn
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(setq pi-coding-agent--status 'streaming
pi-coding-agent--input-buffer input-buf
pi-coding-agent--followup-queue nil)
(pi-coding-agent--begin-session-transition 'mock-proc))
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(pi-coding-agent--set-chat-buffer chat-buf)
(insert "do not steer during switch")
(cl-letf (((symbol-function 'pi-coding-agent--send-steer-message)
(lambda (_text)
(setq steer-sent t)
t))
((symbol-function 'message)
(lambda (fmt &rest args)
(setq shown-message (apply #'format fmt args)))))
(pi-coding-agent-queue-steering))
(should-not steer-sent)
(should (equal (buffer-string) "do not steer during switch")))
(with-current-buffer chat-buf
(should (null pi-coding-agent--followup-queue)))
(should (equal shown-message
"Pi: Cannot send steering while session is switching")))
(kill-buffer chat-buf)
(kill-buffer input-buf))))
(ert-deftest pi-coding-agent-test-queue-steering-send-failure-preserves-input ()
"Steering send failures keep input text for retry and avoid success feedback."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-queue-steer-fail*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-queue-steer-fail-input*"))
(success-message nil))
(unwind-protect
(progn
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(setq pi-coding-agent--status 'streaming)
(setq pi-coding-agent--input-buffer input-buf)
(setq pi-coding-agent--followup-queue nil))
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(setq pi-coding-agent--chat-buffer chat-buf)
(insert "Retry this steer")
(cl-letf (((symbol-function 'pi-coding-agent--send-steer-message)
(lambda (_text) nil))
((symbol-function 'message)
(lambda (fmt &rest _)
(when (and fmt (string-match-p "steering message sent" (downcase fmt)))
(setq success-message t)))))
(pi-coding-agent-queue-steering))
;; Failed send should keep input so user can retry.
(should (equal (buffer-string) "Retry this steer"))
;; Should not claim success.
(should-not success-message)
;; Failed send should not enqueue a normal follow-up.
(with-current-buffer chat-buf
(should (null pi-coding-agent--followup-queue)))))
(kill-buffer chat-buf)
(kill-buffer input-buf))))
(ert-deftest pi-coding-agent-test-queue-steering-while-compacting-queues-locally ()
"Queue steering during compaction should queue locally instead of sending steer now."
(let ((chat-buf (get-buffer-create "*pi-coding-agent-test-queue-steer-compacting*"))
(input-buf (get-buffer-create "*pi-coding-agent-test-queue-steer-compacting-input*"))
(steer-sent nil)
(shown-message nil))
(unwind-protect
(progn
(with-current-buffer chat-buf
(pi-coding-agent-chat-mode)
(setq pi-coding-agent--status 'compacting)
(setq pi-coding-agent--input-buffer input-buf)
(setq pi-coding-agent--followup-queue nil))
(with-current-buffer input-buf
(pi-coding-agent-input-mode)
(setq pi-coding-agent--chat-buffer chat-buf)
(insert "Steer during compaction")
(cl-letf (((symbol-function 'pi-coding-agent--send-steer-message)
(lambda (_text)
(setq steer-sent t)
t))
((symbol-function 'message)
(lambda (fmt &rest args)
(setq shown-message (apply #'format fmt args)))))
(pi-coding-agent-queue-steering))
;; Should NOT send steer immediately
(should-not steer-sent)
;; Should queue in local follow-up queue
(with-current-buffer chat-buf
(should (equal pi-coding-agent--followup-queue '("Steer during compaction"))))
;; Should tell user it was queued without over-promising timing.
(should (equal shown-message "Pi: Steering queued (will send when Pi is ready)"))
;; Input should be cleared
(should (string-empty-p (buffer-string)))))
(kill-buffer chat-buf)
(kill-buffer input-buf))))