forked from laurynas-biveinis/org-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorg-mcp-test.el
More file actions
6117 lines (5609 loc) · 239 KB
/
org-mcp-test.el
File metadata and controls
6117 lines (5609 loc) · 239 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
;;; org-mcp-test.el --- Tests for org-mcp -*- lexical-binding: t; -*-
;;; Commentary:
;; Test suite for org-mcp package.
;;; Code:
(require 'ert)
(require 'org-mcp)
(require 'mcp-server-lib-commands)
(require 'mcp-server-lib-ert)
(require 'json)
(setq mcp-server-lib-ert-server-id "org-mcp")
;;; Test Data Constants
;; Initial content strings for various test scenarios
(defconst org-mcp-test--content-empty ""
"Empty org file content.")
(defconst org-mcp-test--content-with-id-id
"550e8400-e29b-41d4-a716-446655440000"
"ID value for org-mcp-test--content-with-id.")
(defconst org-mcp-test--content-with-id-uri
org-mcp-test--content-with-id-id
"Bare URI form for org-mcp-test--content-with-id (tool input).")
(defconst org-mcp-test--content-with-id-resource-uri
(format "org://%s" org-mcp-test--content-with-id-id)
"Resource URI form for org-mcp-test--content-with-id (tool output).")
(defconst org-mcp-test--content-nested-siblings-parent-id
"nested-siblings-parent-id-002"
"ID for Parent Task in org-mcp-test--content-nested-siblings.")
(defconst org-mcp-test--content-nested-siblings
(format
"#+TITLE: My Org Document
* Parent Task
:PROPERTIES:
:ID: %s
:END:
Some parent content.
** First Child 50%% Complete
First child content.
It spans multiple lines.
** Second Child
:PROPERTIES:
:ID: %s
:END:
Second child content.
** Third Child #3"
org-mcp-test--content-nested-siblings-parent-id
org-mcp-test--content-with-id-id)
"Parent with multiple child tasks and doc file header.")
(defconst org-mcp-test--childless-parent-id
"childless-parent-id-003"
"ID for the parent in org-mcp-test--content-childless-parent.")
(defconst org-mcp-test--content-childless-parent
(format
"* Parent Task
:PROPERTIES:
:ID: %s
:END:
Some parent content."
org-mcp-test--childless-parent-id)
"Top-level parent with body but no child headings.")
(defconst org-mcp-test--level2-parent-level3-sibling-id
"level2-parent-level3-sibling-id-001"
"ID for Review org-mcp.el in level2-parent-level3-children.")
(defconst org-mcp-test--content-level2-parent-level3-children
(format
"* Top Level
** Review the package
*** Review org-mcp.el
:PROPERTIES:
:ID: %s
:END:
Main package file"
org-mcp-test--level2-parent-level3-sibling-id)
"Level 2 parent with level 3 children - matches emacs.org structure.")
(defconst org-mcp-test--content-simple-todo
"* TODO Original Task
First line of body.
Second line of body.
Third line of body."
"Simple TODO task with three-line body.")
(defconst org-mcp-test--content-with-id-todo
(format
"* TODO Task with ID
:PROPERTIES:
:ID: %s
:END:
First line of content.
Second line of content.
Third line of content."
org-mcp-test--content-with-id-id)
"Task with an Org ID property, TODO state, and multiline content.")
(defconst org-mcp-test--timestamp-id "20240101T120000"
"Timestamp-format ID value.")
(defconst org-mcp-test--content-timestamp-id
(format
"* TODO Task with timestamp ID
:PROPERTIES:
:ID: %s
:END:
Task content."
org-mcp-test--timestamp-id)
"Task with a timestamp-format ID property.")
(defconst org-mcp-test--content-with-id-no-body
(format
"* TODO Task with ID but no body
:PROPERTIES:
:ID: %s
:END:"
org-mcp-test--timestamp-id)
"Task with an ID property but no body content.")
(defconst org-mcp-test--body-text-multiline
(concat
"This is the body text.\n"
"It has multiple lines.\n"
"With some content.")
"Multi-line body text for testing TODO items with content.")
(defconst org-mcp-test--other-child-id "A1B2C3D4-E5F6-7890-ABCD-EF1234567890"
"ID value for Other Child in afterUri-not-sibling test.")
(defconst org-mcp-test--content-wrong-levels
(format
"* First Parent
Some content in first parent.
* Second Parent
** Other Child
:PROPERTIES:
:ID: %s
:END:
*** Target Headline
This should NOT be found via First Parent/Target Headline path.
* Third Parent
** Target Headline
This is actually a child of Third Parent, not First Parent!"
org-mcp-test--other-child-id)
"Test content with same headline names at different levels.")
(defconst org-mcp-test--content-todo-with-tags
"* TODO Task with Tags :work:urgent:\nTask description."
"TODO task with tags and body.")
(defconst org-mcp-test--content-slash-not-nested-before
"* Parent
** Real Child
Content here.
* Parent/Child
This is a single headline with a slash, not nested under Parent."
"Content with Parent having a child and separate Parent/Child headline.")
(defconst org-mcp-test--content-with-id-repeated-text
"* Test Heading
:PROPERTIES:
:ID: test-id
:END:
First occurrence of pattern.
Some other text.
Second occurrence of pattern.
More text.
Third occurrence of pattern."
"Heading with ID and repeated text patterns.")
(defconst org-mcp-test--content-duplicate-headlines-before
"* Team Updates
** Project Review
First review content.
* Development Tasks
** Project Review
Second review content.
* Planning
** Project Review
Third review content."
"Content with duplicate 'Project Review' headlines under different parents.")
(defconst org-mcp-test--content-hierarchy-before
"* First Section
** Target
Some content.
* Second Section
** Other Item
More content.
** Target
This Target is under Second Section, not First Section."
"Content with duplicate 'Target' headlines under different parents.")
(defconst org-mcp-test--content-todo-keywords-before
"* Project Management
** TODO Review Documents
This task needs to be renamed
** DONE Review Code
This is already done"
"Parent with TODO and DONE children for testing keyword handling.")
;; Expected patterns and validation regexes
;;
;; Note on property drawer patterns: The patterns use ` *` (zero or more
;; spaces) before :PROPERTIES:, :ID:, and :END: lines to maintain compatibility
;; across Emacs versions. Emacs 27.2 indents property drawers with 3 spaces,
;; while Emacs 28+ does not add indentation.
(defconst org-mcp-test--expected-parent-task-from-nested-siblings
(format
"* Parent Task
:PROPERTIES:
:ID: nested-siblings-parent-id-002
:END:
Some parent content.
** First Child 50%% Complete
First child content.
It spans multiple lines.
** Second Child
:PROPERTIES:
:ID: %s
:END:
Second child content.
** Third Child #3"
org-mcp-test--content-with-id-id)
"Expected content when extracting Parent Task from nested-siblings.")
(defconst org-mcp-test--regex-after-sibling-level3
(concat "\\`\\* Top Level\n"
"\\*\\* Review the package\n"
"\\*\\*\\* Review org-mcp\\.el\n"
" *:PROPERTIES:\n"
" *:ID: +" org-mcp-test--level2-parent-level3-sibling-id "\n"
" *:END:\n"
"Main package file\n"
"\\*\\*\\* TODO Review org-mcp-test\\.el +.*:internet:.*\n"
" *:PROPERTIES:\n"
" *:ID: +[a-fA-F0-9-]+\n"
" *:END:\n\\'")
"Expected pattern after adding TODO after level 3 sibling.")
(defconst org-mcp-test--expected-regex-renamed-second-child
(format
(concat
"\\`#\\+TITLE: My Org Document\n"
"\n"
"\\* Parent Task\n"
":PROPERTIES:\n"
":ID: +nested-siblings-parent-id-002\n"
":END:\n"
"Some parent content\\.\n"
"\\*\\* First Child 50%% Complete\n"
"First child content\\.\n"
"It spans multiple lines\\.\n"
"\\*\\* Renamed Second Child\n"
":PROPERTIES:\n"
":ID: +%s\n"
":END:\n"
"Second child content\\.\n"
"\\*\\* Third Child #3\n?\\'")
org-mcp-test--content-with-id-id)
"Regex matching complete buffer after renaming Second Child.")
(defconst org-mcp-test--expected-regex-todo-to-in-progress-with-id
(format
(concat
"\\`"
"\\* IN-PROGRESS Task with ID\n"
":PROPERTIES:\n"
":ID: +%s\n"
":END:\n"
"First line of content\\.\n"
"Second line of content\\.\n"
"Third line of content\\."
"\\'")
org-mcp-test--content-with-id-id)
"Expected regex for TODO to IN-PROGRESS state change with ID.")
(defconst org-mcp-test--expected-timestamp-id-done-regex
(concat
"\\`\\* DONE Task with timestamp ID"
"\\(?:\n:PROPERTIES:\n:ID:[ \t]+[A-Fa-f0-9-]+\n:END:\\)?"
"\\(?:.\\|\n\\)*\\'")
"Regex matching complete buffer after updating timestamp ID task to DONE.")
(defconst org-mcp-test--expected-task-one-in-progress-regex
(concat
"\\`\\* IN-PROGRESS Task One"
"\\(?:\n:PROPERTIES:\n:ID:[ \t]+[A-Fa-f0-9-]+\n:END:\\)?"
"\\(?:.\\|\n\\)*\\'")
"Regex matching complete buffer with Task One in IN-PROGRESS state.")
(defconst org-mcp-test--expected-task-with-id-in-progress-regex
(concat
"\\`\\* IN-PROGRESS Task with ID"
"\\(?:\n:PROPERTIES:\n:ID:[ \t]+[A-Fa-f0-9-]+\n:END:\\)?"
"\\(?:.\\|\n\\)*\\'")
"Regex matching complete buffer with Task with ID in IN-PROGRESS state.")
(defconst org-mcp-test--expected-regex-top-level-with-header
(concat
"\\`#\\+TITLE: My Org Document\n"
"\n"
"\\* TODO New Top Task +.*:urgent:\n"
"\\(?: *:PROPERTIES:\n"
" *:ID: +[^\n]+\n"
" *:END:\n\\)?"
"\n?"
"\\* Parent Task\n"
":PROPERTIES:\n"
":ID: +" org-mcp-test--content-nested-siblings-parent-id "\n"
":END:\n"
"Some parent content\\.\n"
"\\*\\* First Child 50% Complete\n"
"First child content\\.\n"
"It spans multiple lines\\.\n"
"\\*\\* Second Child\n"
":PROPERTIES:\n"
":ID: +" org-mcp-test--content-with-id-id "\n"
":END:\n"
"Second child content\\.\n"
"\\*\\* Third Child #3\n?\\'")
"Regex matching complete buffer after adding top-level TODO with headers.")
(defconst org-mcp-test--regex-child-under-parent
(format
(concat
"^\\* Parent Task\n"
"\\(?: *:PROPERTIES:\n *:ID: +nested-siblings-parent-id-002\n *:END:\n\\)?"
"Some parent content\\.\n"
"\\*\\* First Child 50%% Complete\n"
"First child content\\.\n"
"It spans multiple lines\\.\n"
"\\*\\* Second Child\n"
"\\(?: *:PROPERTIES:\n *:ID: +%s\n *:END:\n\\)?"
"Second child content\\.\n"
"\\*\\* Third Child #3\n"
"\\*\\* TODO Child Task +.*:work:.*\n"
"\\(?: *:PROPERTIES:\n *:ID: +[^\n]+\n *:END:\n\\)?")
org-mcp-test--content-with-id-id)
"Pattern for child TODO (level 2) added under parent (level 1) with existing child (level 2).")
(defconst org-mcp-test--regex-child-into-childless-parent
(concat
"\\`\\* Parent Task\n"
":PROPERTIES:\n"
":ID: +" org-mcp-test--childless-parent-id "\n"
":END:\n"
"Some parent content\\.\n"
"\\*\\* TODO Only Child +.*:work:.*\n"
"\\(?: *:PROPERTIES:\n *:ID: +[^\n]+\n *:END:\n\\)?\\'")
"Pattern for first child (level 2) added under a previously-childless parent (level 1).")
(defconst org-mcp-test--regex-second-child-same-level
(concat
"\\`\\* Top Level\n"
"\\*\\* Review the package\n"
"\\*\\*\\* Review org-mcp\\.el\n"
"\\(?: *:PROPERTIES:\n *:ID: +[^\n]+\n *:END:\n\\)?" ; Review org-mcp.el has ID
"Main package file\n"
"\\*\\*\\* TODO Second Child +.*:work:.*\n"
"\\(?: *:PROPERTIES:\n *:ID: +[^\n]+\n *:END:\n\\)?\\'") ; Second Child may have ID
"Pattern for second child (level 3) added at same level as first child (level 3) under parent (level 2).")
(defconst org-mcp-test--regex-todo-with-body
(concat
"^\\* TODO Task with Body +:[^\n]*\n"
"\\(?: *:PROPERTIES:\n *:ID: +[^\n]+\n *:END:\n\\)?" ; Optional properties
(regexp-quote org-mcp-test--body-text-multiline)
"\n?$")
"Pattern for TODO with body text.")
(defconst org-mcp-test--regex-todo-after-sibling
(concat
"^#\\+TITLE: My Org Document\n\n"
"\\* Parent Task\n"
":PROPERTIES:\n"
":ID: +" org-mcp-test--content-nested-siblings-parent-id "\n"
":END:\n"
"Some parent content\\.\n"
"\\*\\* First Child 50% Complete\n"
":PROPERTIES:\n"
":ID: +[^\n]+\n"
":END:\n"
"First child content\\.\n"
"It spans multiple lines\\.\n\n?"
"\\*\\* TODO New Task After First +:[^\n]*\n"
"\\(?: *:PROPERTIES:\n *:ID: +[^\n]+\n *:END:\n\\)?"
"\\*\\* Second Child\n"
":PROPERTIES:\n"
":ID: +" org-mcp-test--content-with-id-id "\n"
":END:\n"
"Second child content\\.\n"
"\\*\\* Third Child #3\n?\\'")
"Pattern for TODO added after specific sibling.")
(defconst org-mcp-test--regex-todo-without-tags
(concat
"^\\* TODO Task Without Tags *\n" ; No tags, optional spaces
"\\(?: *:PROPERTIES:\n" " *:ID: +[^\n]+\n" " *:END:\n\\)?$")
"Pattern for TODO item without any tags.")
(defconst org-mcp-test--pattern-add-todo-parent-id-uri
(concat
"^\\* Parent Task\n"
"\\(?: *:PROPERTIES:\n"
" *:ID: +[^\n]+\n"
" *:END:\n\\)?"
"Some parent content\\.\n"
"\\*\\* First Child 50% Complete\n"
"First child content\\.\n"
"It spans multiple lines\\.\n"
"\\*\\* Second Child\n"
"\\(?: *:PROPERTIES:\n"
" *:ID: +[^\n]+\n"
" *:END:\n\\)?"
"Second child content\\.\n"
"\\*\\* Third Child #3\n"
"\\*\\* TODO Child via ID +:work:\n"
"\\(?: *:PROPERTIES:\n"
" *:ID: +[^\n]+\n"
" *:END:\n\\)?$")
"Pattern for TODO added via parent ID URI.")
(defconst org-mcp-test--pattern-renamed-simple-todo
(concat
"\\`\\* TODO Updated Task\n"
" *:PROPERTIES:\n"
" *:ID:[ \t]+[A-Fa-f0-9-]+\n"
" *:END:\n"
"First line of body\\.\n"
"Second line of body\\.\n"
"Third line of body\\.\n?\\'")
"Pattern for renamed simple TODO with generated ID.")
(defconst org-mcp-test--pattern-renamed-todo-with-tags
(concat
"^\\* TODO Renamed Task[ \t]+:work:urgent:\n"
" *:PROPERTIES:\n"
" *:ID:[ \t]+[A-Fa-f0-9-]+\n"
" *:END:\n"
"Task description\\.$")
"Pattern for renamed TODO task preserving tags.")
(defconst org-mcp-test--pattern-renamed-headline-no-todo
(format
(concat
"\\`#\\+TITLE: My Org Document\n"
"\n"
"\\* Parent Task\n"
"\\(?: *:PROPERTIES:\n *:ID: +nested-siblings-parent-id-002\n *:END:\n\\)?"
"Some parent content\\.\n"
"\\*\\* Updated Child\n"
" *:PROPERTIES:\n"
" *:ID:[ \t]+[A-Fa-f0-9-]+\n"
" *:END:\n"
"First child content\\.\n"
"It spans multiple lines\\.\n"
"\\*\\* Second Child\n"
"\\(?: *:PROPERTIES:\n *:ID: +%s\n *:END:\n\\)?"
"Second child content\\.\n"
"\\*\\* Third Child #3\n?"
"\\'")
org-mcp-test--content-with-id-id)
"Pattern for renamed headline without TODO state.")
(defconst org-mcp-test--pattern-renamed-headline-with-id
(format
(concat
"\\`#\\+TITLE: My Org Document\n"
"\n"
"\\* Parent Task\n"
"\\(?: *:PROPERTIES:\n *:ID: +nested-siblings-parent-id-002\n *:END:\n\\)?"
"Some parent content\\.\n"
"\\*\\* First Child 50%% Complete\n"
"\\(?: *:PROPERTIES:\n *:ID:[ \t]+[A-Fa-f0-9-]+\n *:END:\n\\)?"
"First child content\\.\n"
"It spans multiple lines\\.\n"
"\\*\\* Second Child\n"
"\\(?: *:PROPERTIES:\n *:ID: +%s\n *:END:\n\\)?"
"Second child content\\.\n"
"\\*\\* Renamed Child\n"
" *:PROPERTIES:\n"
" *:ID:[ \t]+[A-Fa-f0-9-]+\n"
" *:END:\n?\\'")
org-mcp-test--content-with-id-id)
"Pattern for headline renamed with ID creation.")
(defconst org-mcp-test--pattern-renamed-slash-headline
(concat
"\\`\\* Parent\n"
"\\*\\* Real Child\n"
"Content here\\.\n"
"\\* Parent/Child Renamed\n"
" *:PROPERTIES:\n"
" *:ID:[ \t]+[A-Fa-f0-9-]+\n"
" *:END:\n"
"This is a single headline with a slash, not nested under Parent\\.\n?\\'")
"Pattern for renamed headline containing slash character.")
(defconst org-mcp-test--regex-slash-not-nested-after
(concat
"\\`\\* Parent\n"
"\\*\\* Real Child\n"
"Content here\\.\n"
"\\* Parent-Child Renamed\n"
" *:PROPERTIES:\n"
" *:ID:[ \t]+[A-Fa-f0-9-]+\n"
" *:END:\n"
"This is a single headline with a slash, not nested under Parent\\.\n?\\'")
"Regex for slash-not-nested test after renaming Parent/Child.")
(defconst org-mcp-test--regex-percent-after
(format
(concat
"\\`#\\+TITLE: My Org Document\n"
"\n"
"\\* Parent Task\n"
":PROPERTIES:\n"
":ID: +%s\n"
":END:\n"
"Some parent content\\.\n"
"\\*\\* First Child 75%% Complete\n"
" *:PROPERTIES:\n"
" *:ID:[ \t]+[A-Fa-f0-9-]+\n"
" *:END:\n"
"First child content\\.\n"
"It spans multiple lines\\.\n"
"\\*\\* Second Child\n"
":PROPERTIES:\n"
":ID: +%s\n"
":END:\n"
"Second child content\\.\n"
"\\*\\* Third Child #3\n?\\'")
org-mcp-test--content-nested-siblings-parent-id
org-mcp-test--content-with-id-id)
"Expected pattern after renaming headline with percent sign.")
(defconst org-mcp-test--regex-duplicate-first-renamed
(concat
"\\`\\* Team Updates\n"
"\\*\\* Q1 Review\n"
" *:PROPERTIES:\n"
" *:ID:[ \t]+[A-Fa-f0-9-]+\n"
" *:END:\n"
"First review content\\.\n"
"\\* Development Tasks\n"
"\\*\\* Project Review\n"
"Second review content\\.\n"
"\\* Planning\n"
"\\*\\* Project Review\n"
"Third review content\\.\n?\\'")
"Regex for duplicate headlines after renaming first occurrence.")
(defconst org-mcp-test--regex-hierarchy-second-target-renamed
(concat
"\\`\\* First Section\n"
"\\*\\* Target\n"
"Some content\\.\n"
"\\* Second Section\n"
"\\*\\* Other Item\n"
"More content\\.\n"
"\\*\\* Renamed Target\n"
" *:PROPERTIES:\n"
" *:ID:[ \t]+[A-Fa-f0-9-]+\n"
" *:END:\n"
"This Target is under Second Section, not First Section\\.\n?\\'")
"Regex for hierarchy test after renaming second Target.")
(defconst org-mcp-test--regex-add-todo-with-mutex-tags
(concat
"\\`#\\+TITLE: Test Org File\n"
"\n"
"\\* TODO Test Task[ \t]+\\(:[^:\n]+\\)+:\n"
" *:PROPERTIES:\n"
" *:ID:[ \t]+[A-Fa-f0-9-]+\n"
" *:END:\n?\\'")
"Regex for add-todo test accepting any tag order.")
(defconst org-mcp-test--regex-todo-keywords-after
(concat
"\\`\\* Project Management\n"
"\\*\\* TODO Q1 Planning Review\n"
" *:PROPERTIES:\n"
" *:ID:[ \t]+[A-Fa-f0-9-]+\n"
" *:END:\n"
"This task needs to be renamed\n"
"\\*\\* DONE Review Code\n"
"This is already done\n?\\'")
"Regex for todo-keywords test after renaming TODO headline.")
(defconst org-mcp-test--pattern-edit-body-single-line
(format (concat
"\\`#\\+TITLE: My Org Document\n"
"\n"
"\\* Parent Task\n"
":PROPERTIES:\n"
":ID: +nested-siblings-parent-id-002\n"
":END:\n"
"Some parent content\\.\n"
"\\*\\* First Child 50%% Complete\n"
"First child content\\.\n"
"It spans multiple lines\\.\n"
"\\*\\* Second Child\n"
":PROPERTIES:\n"
":ID: +%s\n"
":END:\n"
"Updated second child content\\.\n"
"\\*\\* Third Child #3\n"
"?\\'")
org-mcp-test--content-with-id-id)
"Pattern for single-line edit-body test result.")
(defconst org-mcp-test--pattern-edit-body-multiline
(format (concat
"\\`\\* TODO Task with ID\n"
":PROPERTIES:\n"
":ID: +%s\n"
":END:\n"
"First line of content\\.\n"
"This has been replaced\n"
"with new multiline\n"
"content here\\.\n"
"Third line of content\\.\n"
"?\\'")
org-mcp-test--content-with-id-id)
"Pattern for multiline edit-body test result.")
(defconst org-mcp-test--pattern-edit-body-nested-headlines
(format
(concat
"\\`#\\+TITLE: My Org Document\n"
"\n"
"\\* Parent Task\n"
"\\(?: *:PROPERTIES:\n *:ID: +nested-siblings-parent-id-002\n *:END:\n\\)?"
"Updated parent content\n"
"\\*\\* First Child 50%% Complete\n"
"\\(?: *:PROPERTIES:\n *:ID:[ \t]+[A-Fa-f0-9-]+\n *:END:\n\\)?"
"First child content\\.\n"
"It spans multiple lines\\.\n"
"\\*\\* Second Child\n"
"\\(?: *:PROPERTIES:\n *:ID: +%s\n *:END:\n\\)?"
"Second child content\\.\n"
"\\*\\* Third Child #3\n?"
"\\(?: *:PROPERTIES:\n *:ID:[ \t]+[A-Fa-f0-9-]+\n *:END:\n\\)?"
"\\'")
org-mcp-test--content-with-id-id)
"Pattern for nested headlines edit-body test result.")
(defconst org-mcp-test--pattern-edit-body-empty
(concat
"\\*\\* Third Child #3New content added\\.\n"
" *:PROPERTIES:\n"
" *:ID:[ \t]+[A-Fa-f0-9-]+\n"
" *:END:")
"Pattern for edit-body test with empty body adding content.")
(defconst org-mcp-test--pattern-edit-body-empty-with-props
(format (concat
" *:PROPERTIES:\n"
" *:ID:[ \t]+[A-Fa-f0-9-]+\n"
" *:END:\n"
" *:PROPERTIES:\n"
" *:ID: +%s\n"
" *:END:Content added after properties\\.")
org-mcp-test--timestamp-id)
"Pattern for edit-body with existing properties adding content.")
(defconst org-mcp-test--pattern-edit-body-accept-lower-level
(concat
"\\* Parent Task\n"
" *:PROPERTIES:\n"
" *:ID: +nested-siblings-parent-id-002\n"
" *:END:\n"
"Some parent content\\.\n"
"\\*\\* First Child 50% Complete\n"
"First child content\\.\n"
"It spans multiple lines\\.\n"
"\\*\\* Second Child\n"
" *:PROPERTIES:\n"
" *:ID: +"
org-mcp-test--content-with-id-id
"\n"
" *:END:\n"
"some text\n"
"\\*\\*\\* Subheading content\n"
"\\(?: *:PROPERTIES:\n" ; Subheading gets ID
" *:ID:[ \t]+[A-Fa-f0-9-]+\n"
" *:END:\n\\)?"
"\\*\\* Third Child #3")
"Pattern for edit-body accepting lower-level headlines.")
(defconst org-mcp-test--pattern-tool-read-headline-single
(concat
"\\`\\* Parent/Child\n"
"This is a single headline with a slash, not nested under Parent\\.\n"
"?\\'")
"Pattern for org-read-headline tool single-level path result.")
(defconst org-mcp-test--pattern-tool-read-headline-nested
(concat
"\\`\\*\\* First Child 50% Complete\n"
"First child content\\.\n"
"It spans multiple lines\\.\n"
"?\\'")
"Pattern for org-read-headline tool nested path result.")
(defconst org-mcp-test--pattern-tool-read-by-id
(format
(concat
"\\`\\*\\* Second Child\n"
":PROPERTIES:\n"
":ID: +%s\n"
":END:\n"
"Second child content\\.\n"
"?\\'")
org-mcp-test--content-with-id-id)
"Pattern for org-read-by-id tool result.")
(defconst org-mcp-test--content-id-resource
(concat
"* Section with ID\n"
":PROPERTIES:\n"
":ID: 12345678-abcd-efgh-ijkl-1234567890ab\n"
":END:\n"
"Content of section with ID.")
"Content for ID resource tests.")
(defconst org-mcp-test--clock-task-content
"* TODO Task One\n"
"Initial Org file content for clock tool tests.")
(defconst org-mcp-test--clock-task-with-open-clock
"* TODO Task One\n:LOGBOOK:\nCLOCK: [2026-01-01 Thu 10:00]\n:END:\n"
"Org file content with an unclosed CLOCK entry, used for clock-out tests.")
(defconst org-mcp-test--clock-add-expected-regex
(concat
"\\`\\* TODO Task One\n"
"\\(?::PROPERTIES:\n:ID:[ \t]+[A-Fa-f0-9-]+\n:END:\n\\)?"
":LOGBOOK:\n"
"CLOCK: \\[2026-01-01 [A-Za-z]\\{2,3\\} 10:00\\]"
"--\\[2026-01-01 [A-Za-z]\\{2,3\\} 11:00\\] => 1:00\n"
":END:\n"
"\\'")
"Regex matching the complete file after org-clock-add adds a closed CLOCK entry.")
(defconst org-mcp-test--clock-in-expected-regex
(concat
"\\`\\* TODO Task One\n"
"\\(?::PROPERTIES:\n:ID:[ \t]+[A-Fa-f0-9-]+\n:END:\n\\)?"
":LOGBOOK:\n"
"CLOCK: \\[2026-01-01 [A-Za-z]\\{2,3\\} 10:00\\]\n"
":END:\n"
"\\'")
"Regex matching the complete file after org-clock-in inserts an open CLOCK entry.")
(defconst org-mcp-test--clock-out-expected-regex
(concat
"\\`\\* TODO Task One\n"
"\\(?::PROPERTIES:\n:ID:[ \t]+[A-Fa-f0-9-]+\n:END:\n\\)?"
":LOGBOOK:\n"
"CLOCK: \\[2026-01-01 [A-Za-z]\\{2,3\\} 10:00\\]"
"--\\[2026-01-01 [A-Za-z]\\{2,3\\} 11:00\\] => 1:00\n"
":END:\n"
"\\'")
"Regex matching the complete file after org-clock-out closes the CLOCK entry.")
(defconst org-mcp-test--before-save-hook-initial-content
"* Headline\n\nOriginal body\n"
"Initial org file content for before-save-hook tests.")
;; Test helpers
(defun org-mcp-test--read-file (file)
"Read and return the contents of FILE as a string."
(with-temp-buffer
(insert-file-contents file)
(buffer-string)))
(defun org-mcp-test--verify-file-matches (test-file expected-pattern)
"Verify TEST-FILE content matches EXPECTED-PATTERN regexp."
(should (string-match-p expected-pattern (org-mcp-test--read-file test-file))))
(defmacro org-mcp-test--assert-error-and-file (test-file error-form)
"Assert that ERROR-FORM throws an error and TEST-FILE remains unchanged."
(declare (indent 1) (debug t))
`(let ((original-content (org-mcp-test--read-file ,test-file)))
(should-error ,error-form :type 'mcp-server-lib-tool-error)
(should (string= (org-mcp-test--read-file ,test-file) original-content))))
(defmacro org-mcp-test--with-enabled (&rest body)
"Run BODY with org-mcp enabled, ensuring cleanup."
(declare (indent defun) (debug t))
`(progn
(org-mcp-enable)
(unwind-protect
(mcp-server-lib-ert-with-server :tools t :resources t ,@body)
(org-mcp-disable))))
(defmacro org-mcp-test--with-temp-org-files (file-specs &rest body)
"Create temporary Org files, execute BODY, and ensure cleanup.
FILE-SPECS is a list of file specifications.
Each spec is (VAR CONTENT [FILENAME-PREFIX]).
VAR is the variable to bind the temp file path to.
CONTENT is the initial content to write to the file.
FILENAME-PREFIX is optional, defaults to \"org-mcp-test\".
All created files are automatically added to `org-mcp-allowed-files'.
BODY is executed with org-mcp enabled."
(declare (indent 1))
(let* ((vars (mapcar #'car file-specs))
(temp-vars (mapcar (lambda (v) (gensym (symbol-name v)))
vars))
(bindings (cl-mapcar
(lambda (var temp-var)
`(,var ,temp-var))
vars temp-vars))
(inits (cl-mapcar
(lambda (temp-var spec)
(let ((content (nth 1 spec))
(filename (or (nth 2 spec) "org-mcp-test")))
`(setq ,temp-var
(make-temp-file ,filename nil ".org" ,content))))
temp-vars file-specs))
(cleanups (mapcar
(lambda (temp-var)
`(when ,temp-var
(delete-file ,temp-var)))
temp-vars)))
`(let (,@temp-vars)
(unwind-protect
(progn
,@inits
(let (,@bindings
(org-mcp-allowed-files (list ,@temp-vars)))
(org-mcp-test--with-enabled
,@body)))
,@cleanups))))
(defmacro org-mcp-test--with-id-tracking
(allowed-files id-locations &rest body)
"Set up org-id tracking with ID-LOCATIONS and run BODY.
ALLOWED-FILES is the list of files to bind to `org-mcp-allowed-files'.
ID-LOCATIONS is a list of (ID . FILE) cons cells to register.
Sets up `org-id-track-globally' and `org-id-locations-file',
then registers each ID location."
(declare (indent 2) (debug t))
`(let ((org-id-track-globally t)
(org-id-locations-file nil) ; Prevent saving to disk
(org-id-locations nil)
(org-mcp-allowed-files ,allowed-files))
(dolist (id-loc ,id-locations)
(org-id-add-location (car id-loc) (cdr id-loc)))
,@body))
(defmacro org-mcp-test--with-id-setup (file-var initial-content ids &rest body)
"Create temp file, set up org-id tracking with IDS, run BODY.
FILE-VAR is the variable to bind the temp file path to.
INITIAL-CONTENT is the initial content to write to the file.
IDS is a list of ID strings to register.
Sets up `org-id-track-globally' and `org-id-locations-file',
then registers each ID location and enables MCP for BODY.
The created temp file is automatically added to `org-mcp-allowed-files'."
(declare (indent 2) (debug t))
`(org-mcp-test--with-temp-org-files
((,file-var ,initial-content))
(org-mcp-test--with-id-tracking
(list ,file-var)
(mapcar (lambda (id) (cons id ,file-var)) ,ids)
,@body)))
;; Helper functions for reading MCP resources
(defun org-mcp-test--verify-resource-read (uri text)
"Verify MCP resource at URI being TEXT."
(mcp-server-lib-ert-verify-resource-read
uri `((uri . ,uri)
(text . ,text)
(mimeType . "text/plain"))))
;; Helper functions for testing org-get-todo-config MCP tool
(defun org-mcp-test--check-todo-config-sequence
(seq expected-type expected-keywords)
"Check sequence SEQ has EXPECTED-TYPE and EXPECTED-KEYWORDS."
(should (= (length seq) 2))
(should (equal (alist-get 'type seq) expected-type))
(should (equal (alist-get 'keywords seq) expected-keywords)))
(defun org-mcp-test--check-todo-config-semantic
(sem expected-state expected-final expected-type)
"Check semantic SEM properties.
EXPECTED-STATE is the TODO keyword.
EXPECTED-FINAL is whether it's a final state.
EXPECTED-TYPE is the sequence type."
(should (= (length sem) 3))
(should (equal (alist-get 'state sem) expected-state))
(should (equal (alist-get 'isFinal sem) expected-final))
(should (equal (alist-get 'sequenceType sem) expected-type)))
(defmacro org-mcp-test--with-get-todo-config-result (keywords &rest body)
"Call get-todo-config tool with KEYWORDS and run BODY with result bindings.
Sets `org-todo-keywords' to KEYWORDS, calls the get-todo-config MCP tool,
and binds `sequences' and `semantics' from the result for use in BODY."
(declare (indent 1) (debug t))
`(let ((org-todo-keywords ,keywords))
(org-mcp-test--with-enabled
(let ((result (json-read-from-string
(mcp-server-lib-ert-call-tool "org-get-todo-config" nil))))
(should (= (length result) 2))
(let ((sequences (cdr (assoc 'sequences result)))
(semantics (cdr (assoc 'semantics result))))
,@body)))))
;; Helper functions for testing org-get-tag-config MCP tool
(defmacro org-mcp-test--get-tag-config-and-check
(expected-alist expected-persistent expected-inheritance expected-exclude)
"Call org-get-tag-config tool and check result against expected values.
EXPECTED-ALIST is the expected value for org-tag-alist (string).
EXPECTED-PERSISTENT is the expected value for org-tag-persistent-alist (string).
EXPECTED-INHERITANCE is the expected value for org-use-tag-inheritance (string).
EXPECTED-EXCLUDE is the expected value for
org-tags-exclude-from-inheritance (string)."
(declare (indent defun) (debug t))
`(org-mcp-test--with-enabled
(let ((result
(json-read-from-string
(mcp-server-lib-ert-call-tool "org-get-tag-config" nil))))
(should (= (length result) 4))
(should (equal (alist-get 'org-tag-alist result) ,expected-alist))
(should (equal (alist-get 'org-tag-persistent-alist result)
,expected-persistent))
(should (equal (alist-get 'org-use-tag-inheritance result)
,expected-inheritance))
(should (equal (alist-get 'org-tags-exclude-from-inheritance result)
,expected-exclude)))))
;; Helper functions for testing org-get-allowed-files MCP tool
(defun org-mcp-test--get-allowed-files-and-check (allowed-files expected-files)
"Call org-get-allowed-files tool and verify the result.
ALLOWED-FILES is the value to bind to org-mcp-allowed-files.
EXPECTED-FILES is a list of expected file paths."
(let ((org-mcp-allowed-files allowed-files))
(org-mcp-test--with-enabled
(let* ((result-text
(mcp-server-lib-ert-call-tool "org-get-allowed-files" nil))
(result (json-read-from-string result-text)))
(should (= (length result) 1))
(let ((files (cdr (assoc 'files result))))
(should (vectorp files))
(should (= (length files) (length expected-files)))
(dotimes (i (length expected-files))
(should (string= (aref files i) (nth i expected-files)))))))))
;; Helper functions for testing org-add-todo MCP tool
(defun org-mcp-test--call-add-todo-expecting-error
(test-file title todoState tags body parentUri &optional afterUri)
"Call org-add-todo MCP tool expecting an error and verify file unchanged.
TEST-FILE is the test file path to verify remains unchanged.
TITLE is the headline text.
TODOSTATE is the TODO state.
TAGS is a list of tag strings or nil.
BODY is the body text or nil.
PARENTURI is the URI of the parent item.
AFTERURI is optional URI of sibling to insert after."
(org-mcp-test--assert-error-and-file
test-file
(let* ((params
`((title . ,title)
(todo_state . ,todoState)
(tags . ,tags)
(body . ,body)
(parent_uri . ,parentUri)
(after_uri . ,afterUri)))
(request
(mcp-server-lib-create-tools-call-request
"org-add-todo" nil params))
(response (mcp-server-lib-process-jsonrpc-parsed request mcp-server-lib-ert-server-id))
(result (mcp-server-lib-ert-process-tool-response response)))
;; If we get here, the tool succeeded when we expected failure
(error "Expected error but got success: %s" result))))