agd-core 0.0.1

Org-mode parsing and task operations for agd
Documentation
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
mod common;

use org_core::org_mode::TreeNode;
use org_core::{OrgConfig, OrgMode, OrgModeError};
use test_utils::fixtures;

use crate::common::create_test_org_mode;

#[test]
fn test_get_outline_simple() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());
    let tree = org_mode
        .get_outline("simple.org")
        .expect("Failed to get outline");

    assert_eq!(tree.label, "Document");
    assert_eq!(tree.children.len(), 3);
    assert_eq!(tree.children[0].label, "First Heading");
    assert_eq!(tree.children[0].level, 1);
    assert_eq!(tree.children[1].label, "Second Heading");
    assert_eq!(tree.children[1].level, 1);
    assert_eq!(tree.children[2].label, "Third Heading");
    assert_eq!(tree.children[2].level, 1);
}

#[test]
fn test_get_outline_nested() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());
    let tree = org_mode
        .get_outline("sample.org")
        .expect("Failed to get outline");

    assert_eq!(tree.label, "Document");
    assert_eq!(tree.children.len(), 2);
    assert_eq!(tree.children[0].label, "Top Level Heading");
    assert_eq!(tree.children[0].level, 1);
    assert_eq!(tree.children[0].children.len(), 2);
    assert_eq!(tree.children[0].children[0].label, "Second Level A");
    assert_eq!(tree.children[0].children[0].level, 2);
    assert_eq!(tree.children[0].children[0].children.len(), 1);
    assert_eq!(
        tree.children[0].children[0].children[0].label,
        "Third Level"
    );
    assert_eq!(tree.children[0].children[0].children[0].level, 3);
}

#[test]
fn test_get_outline_nonexistent_file() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode.get_outline("nonexistent.org");
    assert!(result.is_err());
}

#[test]
fn test_tree_node_creation() {
    let node = TreeNode::new("Test Heading".to_string());
    assert_eq!(node.label, "Test Heading");
    assert_eq!(node.level, 0);
    assert!(node.children.is_empty());

    let node_with_level = TreeNode::new_with_level("Test Heading".to_string(), 2);
    assert_eq!(node_with_level.label, "Test Heading");
    assert_eq!(node_with_level.level, 2);
    assert!(node_with_level.children.is_empty());
}

#[test]
fn test_tree_node_indented_string() {
    let mut parent = TreeNode::new_with_level("Parent".to_string(), 1);
    let child = TreeNode::new_with_level("Child".to_string(), 2);
    parent.children.push(child);

    let result = parent.to_indented_string(0);
    let expected = "* Parent\n  ** Child\n";
    assert_eq!(result, expected);
}

#[test]
fn test_tree_node_serialization() {
    let mut root = TreeNode::new("Document".to_string());
    let mut parent = TreeNode::new_with_level("Parent".to_string(), 1);
    let child = TreeNode::new_with_level("Child".to_string(), 2);
    parent.children.push(child);
    root.children.push(parent);

    let json = serde_json::to_string(&root).expect("Failed to serialize");
    let deserialized: TreeNode = serde_json::from_str(&json).expect("Failed to deserialize");

    assert_eq!(deserialized.label, "Document");
    assert_eq!(deserialized.level, 0);
    assert_eq!(deserialized.children.len(), 1);
    assert_eq!(deserialized.children[0].label, "Parent");
    assert_eq!(deserialized.children[0].level, 1);
    assert_eq!(deserialized.children[0].children.len(), 1);
    assert_eq!(deserialized.children[0].children[0].label, "Child");
    assert_eq!(deserialized.children[0].children[0].level, 2);
}

#[test]
fn test_get_heading_simple() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode
        .get_heading("simple.org", "First Heading")
        .expect("Failed to get heading");

    assert!(result.contains("* First Heading"));
    assert!(result.contains("This is content under the first heading."));
}

#[test]
fn test_get_heading_nested_path() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode
        .get_heading("nested.org", "Project Planning/Phase 1/Setup Tasks")
        .expect("Failed to get nested heading");

    assert!(result.contains("*** Setup Tasks"));
    assert!(result.contains("Install dependencies"));
    assert!(result.contains("Configure environment"));
}

#[test]
fn test_get_heading_nonexistent() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode.get_heading("simple.org", "Nonexistent Heading");

    match result {
        Err(OrgModeError::InvalidHeadingPath(path)) => {
            assert_eq!(path, "Nonexistent Heading");
        }
        _ => panic!("Expected InvalidHeadingPath error"),
    }
}

#[test]
fn test_get_heading_invalid_path() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode.get_heading("nested.org", "Project Planning/Nonexistent/Deep");

    match result {
        Err(OrgModeError::InvalidHeadingPath(path)) => {
            assert_eq!(path, "Project Planning/Nonexistent/Deep");
        }
        _ => panic!("Expected InvalidHeadingPath error"),
    }
}

#[test]
fn test_get_heading_second_level() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode
        .get_heading("nested.org", "Project Planning/Phase 2")
        .expect("Failed to get second level heading");

    assert!(result.contains("** Phase 2"));
    assert!(result.contains("Implementation phase."));
    assert!(result.contains("*** Development"));
    assert!(result.contains("*** Testing"));
}

#[test]
fn test_get_element_by_id_simple_heading() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode
        .get_element_by_id("simple-123")
        .expect("Failed to get element by simple ID");

    assert!(result.contains("* Heading with Simple ID"));
    assert!(result.contains(":ID: simple-123"));
    assert!(result.contains("This heading has a simple ID for testing."));
    assert!(result.contains("** Subheading"));
}

#[test]
fn test_get_element_by_id_uuid_heading() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode
        .get_element_by_id("550e8400-e29b-41d4-a716-446655440000")
        .expect("Failed to get element by UUID ID");

    assert!(result.contains("* Heading with UUID"));
    assert!(result.contains(":ID: 550e8400-e29b-41d4-a716-446655440000"));
    assert!(result.contains("This heading has a UUID-style ID."));
}

#[test]
fn test_get_element_by_id_document_level() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode
        .get_element_by_id("document-level-id-456")
        .expect("Failed to get document-level element by ID");

    assert!(result.contains(":ID: document-level-id-456"));
    assert!(result.contains(":TITLE: Document with ID"));
    assert!(result.contains("This is a document with a document-level ID property."));
}

#[test]
fn test_get_element_by_id_nested_heading() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode
        .get_element_by_id("nested-abc")
        .expect("Failed to get nested element by ID");

    assert!(result.contains("** Nested Heading with ID"));
    assert!(result.contains(":ID: nested-abc"));
    assert!(result.contains("This is a nested heading with an ID."));
}

#[test]
fn test_get_element_by_id_with_multiple_properties() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode
        .get_element_by_id("multi-prop-id")
        .expect("Failed to get element with multiple properties");

    assert!(result.contains("* Multiple Properties Heading"));
    assert!(result.contains(":ID: multi-prop-id"));
    assert!(result.contains(":CREATED: [2023-01-01 Mon]"));
    assert!(result.contains(":CATEGORY: test"));
    assert!(result.contains("This heading has multiple properties including an ID."));
}

#[test]
fn test_get_element_by_id_nonexistent() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode.get_element_by_id("nonexistent-id");

    match result {
        Err(OrgModeError::InvalidElementId(id)) => {
            assert_eq!(id, "nonexistent-id");
        }
        _ => panic!("Expected InvalidElementId error"),
    }
}

#[test]
fn test_get_element_by_id_case_sensitive() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode.get_element_by_id("CaseSensitiveID");
    assert!(result.is_ok());

    let result = org_mode.get_element_by_id("casesensitiveid");
    assert!(result.is_err());

    let result = org_mode.get_element_by_id("CASESENSITIVEID");
    assert!(result.is_err());
}

#[test]
fn test_get_element_by_id_special_characters() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode
        .get_element_by_id("special-chars!@#$%")
        .expect("Failed to get element with special characters in ID");

    assert!(result.contains("* Heading with Special Characters"));
    assert!(result.contains(":ID: special-chars!@#$%"));
}

#[test]
fn test_get_element_by_id_whitespace_handling() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode.get_element_by_id("spaces-around");
    assert!(
        result.is_ok(),
        "Should find ID despite whitespace in org file"
    );
}

#[test]
fn test_get_element_by_id_multi_file_search() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result_a = org_mode.get_element_by_id("project-alpha-001");
    assert!(result_a.is_ok());
    assert!(result_a.unwrap().contains("* Project Alpha"));

    let result_b = org_mode.get_element_by_id("project-beta-002");
    assert!(result_b.is_ok());
    assert!(result_b.unwrap().contains("* Project Beta"));
}

#[test]
fn test_get_element_by_id_duplicate_id_precedence() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let result = org_mode.get_element_by_id("shared-id-test");
    assert!(result.is_ok());
    let content = result.unwrap();
    assert!(content.contains("* Shared ID Test"));
    assert!(content.contains(":ID: shared-id-test"));
}

#[test]
fn test_search_basic_functionality() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let results = org_mode.search("First", None, None).expect("Search failed");
    assert!(!results.is_empty());
    assert!(results.iter().any(|r| r.snippet.contains("First")));
}

#[test]
fn test_search_with_limit() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let results = org_mode
        .search("Heading", Some(2), None)
        .expect("Search failed");
    assert!(results.len() <= 2);
}

#[test]
fn test_search_empty_query() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let results = org_mode.search("", None, None).expect("Search failed");
    assert!(results.is_empty());

    let results = org_mode.search("   ", None, None).expect("Search failed");
    assert!(results.is_empty());
}

#[test]
fn test_search_unicode_characters() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    // Primary test: ensure Unicode text processing doesn't cause character boundary panics

    let results = org_mode.search("test", None, None);
    assert!(results.is_ok(), "Basic search should work without crashing");

    if let Ok(results) = results {
        for result in results {
            assert!(!result.snippet.is_empty());
            assert!(
                result.snippet.chars().count() > 0,
                "Snippet should have at least one character"
            );

            if result.snippet.contains("...") {
                assert!(result.snippet.ends_with("..."));
            }
        }
    }
}

#[test]
fn test_search_unicode_snippet_generation() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    // Search for content that should trigger snippet truncation
    let results = org_mode
        .search("truncation", None, None)
        .expect("Search failed");

    for result in &results {
        assert!(result.snippet.chars().count() > 0);

        if result.snippet.len() > 100 {
            assert!(result.snippet.ends_with("..."));
        }

        assert!(
            result.snippet.chars().count() <= 103,
            "Snippet should not exceed maximum character length"
        );
    }

    // Test with a search that's likely to find long lines with Unicode
    let results = org_mode
        .search("characters", None, None)
        .expect("Search failed");
    for result in &results {
        assert!(!result.snippet.is_empty());
    }
}

#[test]
fn test_search_unicode_edge_cases() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    // Key test: searching for various Unicode characters should not cause panics
    // We don't assert results must be found since fuzzy matching behavior varies

    // Test emoji search (may or may not find results)
    let _results = org_mode.search("🚀", None, None).expect("Search failed");

    // Test mathematical symbols
    let _results = org_mode.search("∑", None, None).expect("Search failed");

    // Test currency symbols
    let _results = org_mode.search("€", None, None).expect("Search failed");

    // Test terms that are more likely to be found
    let results = org_mode
        .search("Emojis", None, None)
        .expect("Search failed");
    if !results.is_empty() {
        // Verify no character corruption in results
        for result in &results {
            assert!(result.snippet.chars().count() > 0);
        }
    }

    // Test mixed scripts
    let _results = org_mode.search("Hello", None, None).expect("Search failed");
    // The key success criterion is no panics during processing
}

#[test]
fn test_search_unicode_boundary_safety() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let results = org_mode
        .search("character boundaries", None, None)
        .expect("Search failed");

    for result in results {
        assert!(result.snippet.is_ascii() || result.snippet.chars().count() > 0);

        // If truncated, should end with "..."
        if result.snippet.len() > 97 {
            assert!(result.snippet.ends_with("..."));
        }
    }
}

#[test]
fn test_search_all_terms_must_match() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    // Test AND logic: all terms must match on the same line
    let results = org_mode.search("First", None, None);
    assert!(results.is_ok(), "Single term search should work");

    // Test multiple terms - focus on not crashing rather than specific behavior
    let results = org_mode.search("test case", None, None);
    // The key success is that this doesn't panic
    assert!(results.is_ok(), "Multi-term search should not crash");

    if let Ok(results) = results {
        for result in results {
            assert!(!result.snippet.is_empty());
        }
    }
}

#[test]
fn test_search_snippet_max_size_default() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    // Test with default snippet size (None should use internal default)
    let results = org_mode
        .search("truncation", None, None)
        .expect("Search failed");

    for result in &results {
        // Default behavior should limit snippets to around 100 characters
        if result.snippet.ends_with("...") {
            // Snippet was truncated, should be around 100 chars + "..."
            assert!(result.snippet.chars().count() <= 103);
        }
    }
}

#[test]
fn test_search_snippet_max_size_custom() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    // Test with very small snippet size
    let results = org_mode
        .search("content", None, Some(20))
        .expect("Search failed");

    for result in &results {
        if result.snippet.ends_with("...") {
            // Should be truncated to 20 chars + "..."
            assert!(result.snippet.chars().count() <= 23);
            assert!(result.snippet.chars().count() >= 20); // At least the truncated part
        } else {
            // If not truncated, should be <= 20 chars
            assert!(result.snippet.chars().count() <= 20);
        }
    }
}

#[test]
fn test_search_snippet_max_size_large() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    // Test with very large snippet size - should not truncate normal content
    let results = org_mode
        .search("heading", None, Some(500))
        .expect("Search failed");

    for result in &results {
        // With such a large limit, most snippets shouldn't be truncated
        // but we can't guarantee all content is < 500 chars, so just verify no crashes
        assert!(!result.snippet.is_empty());
        assert!(result.snippet.chars().count() <= 503); // 500 + "..."
    }
}

#[test]
fn test_search_snippet_max_size_zero() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    // Test edge case with zero snippet size
    let results = org_mode
        .search("test", None, Some(0))
        .expect("Search failed");

    for result in &results {
        // With size 0, should just be "..."
        assert_eq!(result.snippet, "...");
    }
}

#[test]
fn test_search_snippet_max_size_one() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    // Test edge case with snippet size of 1
    let results = org_mode
        .search("test", None, Some(1))
        .expect("Search failed");

    for result in &results {
        if result.snippet.ends_with("...") {
            // Should be 1 char + "..."
            assert_eq!(result.snippet.chars().count(), 4);
        } else {
            // Single character only
            assert_eq!(result.snippet.chars().count(), 1);
        }
    }
}

#[test]
fn test_config_getter() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let config = org_mode.config();
    assert_eq!(
        config.org_directory,
        org_dir.path().to_string_lossy().to_string()
    );
}

#[test]
fn test_list_files_basic() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let files = org_mode
        .list_files(None, None)
        .expect("Failed to list files");

    assert!(!files.is_empty());
    assert!(files.iter().any(|f| f.ends_with(".org")));
}

#[test]
fn test_list_files_with_limit() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let files = org_mode
        .list_files(None, Some(2))
        .expect("Failed to list files with limit");

    assert!(files.len() <= 2);
}

#[test]
fn test_search_with_tags_single_tag() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let results = org_mode
        .search_with_tags("Project", Some(&["work".to_string()]), None, None)
        .expect("Search with tags failed");

    for result in &results {
        assert!(
            result.tags.contains(&"work".to_string()),
            "Result should have work tag"
        );
    }
}

#[test]
fn test_search_with_tags_multiple_tags() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let results = org_mode
        .search_with_tags(
            "Project",
            Some(&["work".to_string(), "personal".to_string()]),
            None,
            None,
        )
        .expect("Search with tags failed");

    for result in &results {
        assert!(
            result.tags.contains(&"work".to_string())
                || result.tags.contains(&"personal".to_string()),
            "Result should have work or personal tag"
        );
    }
}

#[test]
fn test_search_with_tags_no_match() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let results = org_mode
        .search_with_tags(
            "Project",
            Some(&["nonexistent_tag".to_string()]),
            None,
            None,
        )
        .expect("Search with tags failed");

    assert!(results.is_empty());
}

#[test]
fn test_search_with_tags_with_limit() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let results = org_mode
        .search_with_tags("Task", Some(&["work".to_string()]), Some(1), None)
        .expect("Search with tags and limit failed");

    assert!(results.len() <= 1);
}

#[test]
fn test_search_with_tags_none() {
    let org_dir = fixtures::setup_test_org_files().unwrap();
    let org_mode = create_test_org_mode(org_dir.path());

    let results = org_mode
        .search_with_tags("heading", None, None, None)
        .expect("Search with no tag filter failed");

    assert!(!results.is_empty());
}

#[test]
fn test_read_file_directory_error() {
    use std::fs;
    use tempfile::TempDir;

    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let test_dir = temp_dir.path().join("test_subdir");
    fs::create_dir(&test_dir).expect("Failed to create subdirectory");

    let config = OrgConfig {
        org_directory: temp_dir.path().to_str().unwrap().to_string(),
        ..OrgConfig::default()
    };
    let org_mode = OrgMode::new(config).expect("Failed to create OrgMode");

    let result = org_mode.read_file("test_subdir");

    assert!(result.is_err());
    if let Err(e) = result {
        assert!(
            format!("{:?}", e).contains("InvalidInput")
                || format!("{:?}", e).contains("not a file")
        );
    }
}