-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtest_ai-code-mcp-debug-tools.el
More file actions
592 lines (551 loc) · 27 KB
/
test_ai-code-mcp-debug-tools.el
File metadata and controls
592 lines (551 loc) · 27 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
;;; test_ai-code-mcp-debug-tools.el --- Tests for ai-code-mcp-debug-tools -*- lexical-binding: t; -*-
;; SPDX-License-Identifier: Apache-2.0
;;; Commentary:
;; Tests for optional MCP debugging tools.
;;; Code:
(require 'ert)
(require 'cl-lib)
(require 'json)
(require 'seq)
(unless (featurep 'magit)
(defun magit-toplevel (&optional _dir) nil)
(defun magit-get-current-branch () nil)
(defun magit-git-lines (&rest _args) nil)
(provide 'magit))
(require 'ai-code-mcp-server nil t)
(require 'ai-code-mcp-debug-tools nil t)
(defun ai-code-test-mcp-debug-tools--content-text (result)
"Extract text content from RESULT."
(alist-get 'text
(car (alist-get 'content result))))
(defun ai-code-test-mcp-debug-tools--read-json-payload (result)
"Decode the JSON text content from RESULT."
(let ((json-object-type 'alist)
(json-array-type 'vector)
(json-key-type 'symbol))
(json-read-from-string
(ai-code-test-mcp-debug-tools--content-text result))))
(defconst ai-code-test-mcp-debug-tools--tool-names
'("get_feature_load_state"
"get_function_info"
"get_last_error_backtrace"
"get_recent_messages"
"get_variable_binding_info"
"get_variable_value")
"Expected MCP debug tool names.")
(ert-deftest ai-code-test-mcp-debug-tools-source-uses-common-module ()
"Debug MCP modules should depend on the shared common module."
(with-temp-buffer
(insert-file-contents "ai-code-mcp-debug-tools.el")
(goto-char (point-min))
(should (search-forward "(require 'ai-code-mcp-common" nil t))
(goto-char (point-min))
(should-not (search-forward "(require 'seq)" nil t))
(should-not (search-forward "(defun ai-code-mcp--json-bool" nil t))
(goto-char (point-min))
(should-not (search-forward "(defun ai-code-mcp--message-lines" nil t))
(goto-char (point-min))
(should-not (search-forward "(defvar ai-code-mcp-server-tool-setup-functions nil)" nil t))))
(ert-deftest ai-code-test-mcp-common-module-defines-shared-helpers ()
"The shared MCP common module should define the extracted helpers."
(with-temp-buffer
(insert-file-contents "ai-code-mcp-common.el")
(goto-char (point-min))
(should (search-forward "(defvar ai-code-mcp-server-tool-setup-functions" nil t))
(goto-char (point-min))
(should (search-forward "(defun ai-code-mcp--json-bool" nil t))
(goto-char (point-min))
(should (search-forward "(defun ai-code-mcp--message-lines" nil t))))
(ert-deftest ai-code-test-mcp-server-source-uses-common-module-for-setup-registry ()
"The MCP server source should use the shared setup registry declaration."
(with-temp-buffer
(insert-file-contents "ai-code-mcp-server.el")
(goto-char (point-min))
(should (search-forward "(require 'ai-code-mcp-common" nil t))
(goto-char (point-min))
(should-not (search-forward "(defvar ai-code-mcp-server-tool-setup-functions" nil t))))
(ert-deftest ai-code-test-mcp-debug-tools-register-by-default ()
"Optional debug tools should register by default."
(let ((ai-code-mcp-server-tools nil))
(should ai-code-mcp-debug-tools-enabled)
(let* ((tools-result (ai-code-mcp-dispatch "tools/list"))
(tool-names (sort (mapcar (lambda (tool)
(alist-get 'name tool))
(alist-get 'tools tools-result))
#'string<)))
(dolist (tool-name ai-code-test-mcp-debug-tools--tool-names)
(should (member tool-name tool-names))))))
(ert-deftest ai-code-test-mcp-debug-tools-do-not-register-eval-elisp-by-default ()
"Eval should remain opt-in even when debug tools are enabled."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t))
(let* ((tools-result (ai-code-mcp-dispatch "tools/list"))
(tool-names (mapcar (lambda (tool)
(alist-get 'name tool))
(alist-get 'tools tools-result))))
(should-not (member "eval_elisp" tool-names)))))
(ert-deftest ai-code-test-mcp-debug-tools-skip-registration-when-disabled ()
"Optional debug tools should not register when disabled."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled nil))
(let* ((tools-result (ai-code-mcp-dispatch "tools/list"))
(tool-names (sort (mapcar (lambda (tool)
(alist-get 'name tool))
(alist-get 'tools tools-result))
#'string<)))
(dolist (tool-name ai-code-test-mcp-debug-tools--tool-names)
(should-not (member tool-name tool-names))))))
(ert-deftest ai-code-test-mcp-debug-tools-register-eval-when-enabled ()
"Eval should register when the explicit opt-in is enabled."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(ai-code-mcp-debug-tools-enable-eval-elisp t))
(let* ((tools-result (ai-code-mcp-dispatch "tools/list"))
(tool-names (mapcar (lambda (tool)
(alist-get 'name tool))
(alist-get 'tools tools-result))))
(should (member "eval_elisp" tool-names)))))
(ert-deftest ai-code-test-mcp-debug-tools-session-enable-overrides-global-disable ()
"A session override should allow debug tools without changing the global default."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled nil)
(ai-code-mcp-debug-tools-enable-eval-elisp nil)
(ai-code-mcp-debug-tools--session-overrides (make-hash-table :test 'equal)))
(ai-code-mcp-debug-tools-enable-for-session "session-1" t)
(let* ((ai-code-mcp--current-session-id "session-1")
(payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
'((name . "eval_elisp")
(arguments . ((code . "(+ 1 2)"))))))))
(should (equal t (alist-get 'ok payload)))
(should (equal "3" (alist-get 'value_repr payload))))
(let ((ai-code-mcp--current-session-id "session-2"))
(should-error
(ai-code-mcp-dispatch
"tools/call"
'((name . "get_recent_messages")
(arguments . ()))))
(should-error
(ai-code-mcp-dispatch
"tools/call"
'((name . "eval_elisp")
(arguments . ((code . "(+ 1 2)")))))))))
(ert-deftest ai-code-test-mcp-tools-list-warns-eval-elisp-is-unrestricted ()
"Eval tool metadata should warn about unrestricted side effects."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(ai-code-mcp-debug-tools-enable-eval-elisp t))
(let* ((tools-result (ai-code-mcp-dispatch "tools/list"))
(eval-tool (seq-find
(lambda (tool)
(equal "eval_elisp" (alist-get 'name tool)))
(alist-get 'tools tools-result)))
(description (alist-get 'description eval-tool)))
(should eval-tool)
(should (string-match-p "arbitrary Emacs Lisp" description))
(should (string-match-p "side effects" description)))))
(ert-deftest ai-code-test-mcp-eval-elisp-uses-target-buffer ()
"Eval should run against the requested buffer context."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(ai-code-mcp-debug-tools-enable-eval-elisp t)
(buffer (generate-new-buffer " *ai-code-mcp-eval*")))
(unwind-protect
(progn
(with-current-buffer buffer
(insert "hello\n"))
(let ((payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
`((name . "eval_elisp")
(arguments . ((code . "(buffer-name)")
(buffer_name . ,(buffer-name buffer)))))))))
(should (equal t (alist-get 'ok payload)))
(should (equal "\" *ai-code-mcp-eval*\""
(alist-get 'value_repr payload)))
(should (equal "string"
(alist-get 'value_type payload)))))
(when (buffer-live-p buffer)
(kill-buffer buffer)))))
(ert-deftest ai-code-test-mcp-eval-elisp-defaults-to-selected-window-buffer ()
"Eval should use the selected window buffer when no buffer context is given."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(ai-code-mcp-debug-tools-enable-eval-elisp t)
(ai-code-mcp--sessions (make-hash-table :test 'equal))
(session-id "mcp-eval-session")
(session-buffer (generate-new-buffer " *ai-code-mcp-session*"))
(target-buffer (generate-new-buffer " *ai-code-mcp-target*")))
(unwind-protect
(save-window-excursion
(switch-to-buffer target-buffer)
(with-current-buffer session-buffer
(rename-buffer "mcp-session-buffer" t))
(with-current-buffer target-buffer
(rename-buffer "mcp-target-buffer" t))
(ai-code-mcp-register-session session-id default-directory session-buffer)
(let* ((ai-code-mcp--current-session-id session-id)
(payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
'((name . "eval_elisp")
(arguments . ((code . "(buffer-name)"))))))))
(should (equal t (alist-get 'ok payload)))
(should (equal "\"mcp-target-buffer\""
(alist-get 'value_repr payload)))))
(when (buffer-live-p session-buffer)
(kill-buffer session-buffer))
(when (buffer-live-p target-buffer)
(kill-buffer target-buffer)))))
(ert-deftest ai-code-test-mcp-eval-elisp-allows-mutation ()
"Eval should allow mutation symbols when eval is enabled."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(ai-code-mcp-debug-tools-enable-eval-elisp t)
(buffer (generate-new-buffer " *ai-code-mcp-eval-insert*")))
(unwind-protect
(let ((payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
`((name . "eval_elisp")
(arguments . ((code . "(progn (insert \"boom\") (buffer-string))")
(buffer_name . ,(buffer-name buffer)))))))))
(should (equal t (alist-get 'ok payload)))
(should (equal "\"boom\"" (alist-get 'value_repr payload))))
(when (buffer-live-p buffer)
(kill-buffer buffer)))))
(ert-deftest ai-code-test-mcp-eval-elisp-survives-killed-target-buffer ()
"Eval should still return JSON when the target buffer is killed."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(ai-code-mcp-debug-tools-enable-eval-elisp t)
(buffer (generate-new-buffer " *ai-code-mcp-eval-kill*")))
(let ((payload
(unwind-protect
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
`((name . "eval_elisp")
(arguments . ((code . "(kill-buffer (current-buffer))")
(buffer_name . ,(buffer-name buffer)))))))
(when (buffer-live-p buffer)
(kill-buffer buffer)))))
(should (equal t (alist-get 'ok payload)))
(should (equal "t" (alist-get 'value_repr payload)))
(should-not (alist-get 'context_after payload)))))
(ert-deftest ai-code-test-mcp-get-variable-value-returns-bound-variable ()
"Variable value tool should stringify the requested Emacs variable."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(ai-code-mcp-diagnostics-backend 'flymake))
(let ((result (ai-code-mcp-dispatch
"tools/call"
'((name . "get_variable_value")
(arguments . ((variable_name . "ai-code-mcp-diagnostics-backend")))))))
(should (equal "flymake"
(ai-code-test-mcp-debug-tools--content-text result))))))
(ert-deftest ai-code-test-mcp-get-variable-value-reports-missing-variable-without-interning ()
"Unknown variable names should not be interned and should return a friendly error."
(let* ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(variable-name "ai-code-test-mcp-missing-variable")
(result nil))
(when (intern-soft variable-name)
(ert-fail "Test requires a missing symbol name"))
(setq result
(ai-code-mcp-dispatch
"tools/call"
`((name . "get_variable_value")
(arguments . ((variable_name . ,variable-name))))))
(should (equal (format "Variable not found: %s" variable-name)
(ai-code-test-mcp-debug-tools--content-text result)))
(should-not (intern-soft variable-name))))
(ert-deftest ai-code-test-mcp-get-variable-value-reports-unbound-variable ()
"Unbound variable names should return a friendly error."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(variable-name "ai-code-test-mcp-unbound-variable"))
(unwind-protect
(let ((symbol (intern variable-name)))
(setplist symbol nil)
(makunbound symbol)
(let ((result (ai-code-mcp-dispatch
"tools/call"
`((name . "get_variable_value")
(arguments . ((variable_name . ,variable-name)))))))
(should (equal (format "Variable is unbound: %s" variable-name)
(ai-code-test-mcp-debug-tools--content-text result)))))
(unintern variable-name obarray))))
(ert-deftest ai-code-test-mcp-tools-list-describes-variable-value-as-printed-representation ()
"Variable value tool metadata should match the returned representation."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t))
(let* ((tools-result (ai-code-mcp-dispatch "tools/list"))
(variable-tool (seq-find
(lambda (tool)
(equal "get_variable_value" (alist-get 'name tool)))
(alist-get 'tools tools-result))))
(should variable-tool)
(should (equal "Get the printed representation of an Emacs variable value by name."
(alist-get 'description variable-tool))))))
(ert-deftest ai-code-test-mcp-get-variable-binding-info-reports-default-and-local-values ()
"Variable binding info should report current and default values."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(variable-name "ai-code-test-mcp-buffer-local-variable")
(buffer (generate-new-buffer " *ai-code-mcp-binding-info*")))
(unwind-protect
(progn
(set-default (intern variable-name) 2)
(with-current-buffer buffer
(setq-local ai-code-test-mcp-buffer-local-variable 8))
(let* ((payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
`((name . "get_variable_binding_info")
(arguments . ((variable_name . ,variable-name)
(buffer_name . ,(buffer-name buffer))))))))
(documentation-summary
(alist-get 'documentation_summary payload)))
(should (equal t (alist-get 'exists payload)))
(should (equal t (alist-get 'buffer_local payload)))
(should (equal "8" (alist-get 'current_value_repr payload)))
(should (equal "2" (alist-get 'default_value_repr payload)))
(should (equal (buffer-name buffer)
(alist-get 'buffer_name payload)))
(should (stringp documentation-summary))))
(when (buffer-live-p buffer)
(kill-buffer buffer))
(when (intern-soft variable-name)
(unintern variable-name obarray)))))
(ert-deftest ai-code-test-mcp-get-variable-binding-info-reports-missing-variable ()
"Variable binding info should report a missing variable without interning it."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(variable-name "ai-code-test-mcp-missing-binding-variable"))
(when (intern-soft variable-name)
(ert-fail "Test requires a missing symbol name"))
(let ((payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
`((name . "get_variable_binding_info")
(arguments . ((variable_name . ,variable-name))))))))
(should (equal :json-false (alist-get 'exists payload)))
(should-not (alist-get 'current_value_repr payload))
(should-not (intern-soft variable-name)))))
(ert-deftest ai-code-test-mcp-get-function-info-reports-alias-and-advice-state ()
"Function info should report alias and advice metadata."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(base-name "ai-code-test-mcp-base-function")
(alias-name "ai-code-test-mcp-aliased-function")
(advice-name "ai-code-test-mcp-around-advice"))
(unwind-protect
(progn
(fset (intern base-name) (lambda () :ok))
(defalias (intern alias-name) (intern base-name))
(fset (intern advice-name)
(lambda (fn &rest args)
(apply fn args)))
(advice-add (intern alias-name) :around (intern advice-name))
(let ((payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
`((name . "get_function_info")
(arguments . ((function_name . ,alias-name))))))))
(should (equal t (alist-get 'exists payload)))
(should (equal "lambda" (alist-get 'kind payload)))
(should (equal t (alist-get 'advised payload)))
(should (equal base-name (alist-get 'aliased_to payload)))))
(when (fboundp (intern-soft alias-name))
(ignore-errors
(advice-remove (intern alias-name) (intern advice-name)))
(fmakunbound (intern alias-name)))
(when (fboundp (intern-soft base-name))
(fmakunbound (intern base-name)))
(when (fboundp (intern-soft advice-name))
(fmakunbound (intern advice-name)))
(when (intern-soft alias-name)
(unintern alias-name obarray))
(when (intern-soft base-name)
(unintern base-name obarray))
(when (intern-soft advice-name)
(unintern advice-name obarray)))))
(ert-deftest ai-code-test-mcp-get-function-info-reports-missing-functions ()
"Function info should report missing function symbols cleanly."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(function-name "ai-code-test-mcp-missing-function"))
(when (intern-soft function-name)
(ert-fail "Test requires a missing function symbol"))
(let ((payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
`((name . "get_function_info")
(arguments . ((function_name . ,function-name))))))))
(should (equal :json-false (alist-get 'exists payload)))
(should-not (intern-soft function-name)))))
(ert-deftest ai-code-test-mcp-function-kind-does-not-resolve-autoloads ()
"Autoload classification should not force indirect resolution."
(let ((symbol (make-symbol "ai-code-test-mcp-autoload-function")))
(unwind-protect
(progn
(fset symbol '(autoload "ai-code-test-autoload-lib" nil nil nil))
(cl-letf (((symbol-function 'indirect-function)
(lambda (&rest _args)
(ert-fail "autoload classification should not resolve"))))
(should (equal "autoload"
(ai-code-mcp--function-kind symbol)))))
(when (fboundp symbol)
(fmakunbound symbol)))))
(ert-deftest ai-code-test-mcp-get-feature-load-state-reports-loaded-feature-details ()
"Feature load state should report loaded features and their providers."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t))
(let* ((payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
'((name . "get_feature_load_state")
(arguments . ((feature_name . "json")))))))
(provided-by-files (append (alist-get 'provided_by_files payload) nil))
(load-path-matches (append (alist-get 'load_path_matches payload) nil)))
(should (equal t (alist-get 'loaded payload)))
(should (stringp (alist-get 'library_path payload)))
(should provided-by-files)
(should load-path-matches)
(should-not (alist-get 'load_history_entries payload)))))
(ert-deftest ai-code-test-mcp-get-feature-load-state-reports-missing-features ()
"Feature load state should report missing features without errors."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(feature-name "ai-code-test-mcp-missing-feature"))
(when (intern-soft feature-name)
(ert-fail "Test requires a missing feature symbol"))
(let ((payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
`((name . "get_feature_load_state")
(arguments . ((feature_name . ,feature-name))))))))
(should (equal :json-false (alist-get 'loaded payload)))
(should-not (alist-get 'library_path payload)))))
(ert-deftest ai-code-test-mcp-get-feature-load-state-rejects-invalid-feature-name ()
"Feature load state should reject non-string feature names clearly."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t))
(let ((payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
'((name . "get_feature_load_state")
(arguments . ((feature_name . 7))))))))
(should (equal :json-false (alist-get 'loaded payload)))
(should (equal "feature_name must be a non-empty string"
(alist-get 'error_message payload)))
(should-not (alist-get 'library_path payload))
(should-not (alist-get 'load_path_matches payload)))))
(ert-deftest ai-code-test-mcp-get-recent-messages-returns-latest-messages ()
"Recent messages should return the latest entries from `*Messages*'."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t))
(message "ai-code-mcp-server-test-message")
(let* ((payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
'((name . "get_recent_messages")
(arguments . ((limit . 1)))))))
(messages (alist-get 'messages payload)))
(should (equal t (alist-get 'ok payload)))
(should (= 1 (length messages)))
(should (string-match-p "ai-code-mcp-server-test-message"
(aref messages 0))))))
(ert-deftest ai-code-test-mcp-get-last-error-backtrace-reports-empty-state ()
"Last error backtrace should report when no error has been captured."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(ai-code-mcp--last-error-record nil))
(let ((payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
'((name . "get_last_error_backtrace")
(arguments . ()))))))
(should (equal :json-false (alist-get 'recorded payload)))
(should-not (alist-get 'error_message payload))
(should-not (alist-get 'frames payload)))))
(ert-deftest ai-code-test-mcp-get-last-error-backtrace-returns-recorded-error ()
"Last error backtrace should return the recorded error snapshot."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(ai-code-mcp--last-error-record nil))
(cl-letf (((symbol-function 'backtrace-frames)
(lambda (&optional _base)
'((t ai-code-test-mcp-frame-a nil nil)
(t ai-code-test-mcp-frame-b ("x") nil)))))
(ai-code-mcp--record-command-error '(error "Boom") 'command t))
(let* ((payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
'((name . "get_last_error_backtrace")
(arguments . ())))))
(frames (append (alist-get 'frames payload) nil)))
(should (equal t (alist-get 'recorded payload)))
(should (equal "error" (alist-get 'error_symbol payload)))
(should (equal "Boom" (alist-get 'error_message payload)))
(should (equal "command" (alist-get 'context payload)))
(should (= 2 (alist-get 'frame_count payload)))
(should (string-match-p "ai-code-test-mcp-frame-a"
(car frames))))))
(ert-deftest ai-code-test-mcp-eval-elisp-allows-previously-denied-symbols ()
"Eval should allow all symbols when enabled, including funcall."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(ai-code-mcp-debug-tools-enable-eval-elisp t))
(let ((payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
'((name . "eval_elisp")
(arguments . ((code . "(funcall #'+ 1 2)"))))))))
(should (equal t (alist-get 'ok payload)))
(should (equal "3" (alist-get 'value_repr payload))))))
(ert-deftest ai-code-test-mcp-eval-elisp-defines-function ()
"Eval should define a function and make it callable."
(let ((ai-code-mcp-server-tools nil)
(ai-code-mcp-debug-tools-enabled t)
(ai-code-mcp-debug-tools-enable-eval-elisp t)
(func-name "ai-code-test-mcp-eval-defined-func"))
(unwind-protect
(let ((payload
(ai-code-test-mcp-debug-tools--read-json-payload
(ai-code-mcp-dispatch
"tools/call"
`((name . "eval_elisp")
(arguments . ((code . "(defun ai-code-test-mcp-eval-defined-func () 42)"))))))))
(should (equal t (alist-get 'ok payload)))
(should-not (alist-get 'mode payload))
(should (fboundp (intern func-name)))
(should (= 42 (funcall (intern func-name)))))
(when (fboundp (intern-soft func-name))
(fmakunbound (intern func-name))))))
(ert-deftest ai-code-test-readme-documents-eval-elisp-debug-options ()
"README should mention the eval_elisp debugging options."
(with-temp-buffer
(insert-file-contents "README.org")
(dolist (option '("=capture_messages=" "=include_backtrace="))
(goto-char (point-min))
(should (search-forward option nil t)))))
(provide 'test_ai-code-mcp-debug-tools)
;;; test_ai-code-mcp-debug-tools.el ends here