xchecker 1.2.0

Spec pipeline with receipts and gateable JSON contracts
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
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
#![cfg(feature = "test-utils")]
//! Comprehensive tests for PacketBuilder (FR-PKT-001 through FR-PKT-007)
//!
//! **WHITE-BOX TEST**: This test uses internal module APIs (`packet::{...}`, `types::Priority`)
//! and may break with internal refactors. These tests are intentionally white-box to validate
//! internal implementation details. See FR-TEST-4 for white-box test policy.
//!
//! This test suite verifies:
//! - FR-PKT-001: Deterministic ordering (sorted file paths)
//! - FR-PKT-002: Priority-based selection (Upstream > High > Medium > Low)
//! - FR-PKT-003: LIFO ordering within priority classes
//! - FR-PKT-004: Byte and line counting during assembly
//! - FR-PKT-005: Limit enforcement (exit 7 on overflow)
//! - FR-PKT-006: Packet manifest generation on overflow
//! - FR-PKT-007: --debug-packet flag behavior

use anyhow::Result;
use camino::Utf8PathBuf;
use std::fs;
use tempfile::TempDir;
use xchecker::packet::{
    ContentSelector, DEFAULT_PACKET_MAX_BYTES, DEFAULT_PACKET_MAX_LINES, PacketBuilder,
};
use xchecker::test_support;
use xchecker::types::Priority;

/// Test FR-PKT-001: Deterministic ordering with sorted file paths
#[test]
fn test_deterministic_ordering_sorted_paths() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;

    // Create files in non-alphabetical order
    fs::write(base_path.join("zebra.md"), "# Zebra")?;
    fs::write(base_path.join("alpha.md"), "# Alpha")?;
    fs::write(base_path.join("beta.md"), "# Beta")?;

    let selector = ContentSelector::new()?;
    let files = selector.select_files(&base_path)?;

    // Files should be sorted alphabetically within same priority
    let paths: Vec<String> = files.iter().map(|f| f.path.to_string()).collect();

    // All these files have the same priority (Low), so they should be in LIFO order
    // which means reverse alphabetical
    assert_eq!(paths.len(), 3);
    assert!(paths[0].contains("zebra.md"));
    assert!(paths[1].contains("beta.md"));
    assert!(paths[2].contains("alpha.md"));

    Ok(())
}

/// Test FR-PKT-002: Priority-based selection (Upstream > High > Medium > Low)
#[test]
fn test_priority_based_selection() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;

    // Create files with different priorities
    fs::write(base_path.join("config.toml"), "# Low priority")?;
    fs::write(base_path.join("README.md"), "# Medium priority")?;
    fs::write(base_path.join("SPEC-001.md"), "# High priority")?;
    fs::write(base_path.join("design.core.yaml"), "# Upstream priority")?;

    let selector = ContentSelector::new()?;
    let files = selector.select_files(&base_path)?;

    // Verify priority ordering: Upstream -> High -> Medium -> Low
    assert_eq!(files.len(), 4);
    assert_eq!(files[0].priority, Priority::Upstream);
    assert!(files[0].path.to_string().contains("design.core.yaml"));

    assert_eq!(files[1].priority, Priority::High);
    assert!(files[1].path.to_string().contains("SPEC-001.md"));

    assert_eq!(files[2].priority, Priority::Medium);
    assert!(files[2].path.to_string().contains("README.md"));

    assert_eq!(files[3].priority, Priority::Low);
    assert!(files[3].path.to_string().contains("config.toml"));

    Ok(())
}

/// Test FR-PKT-003: LIFO ordering within priority classes
#[test]
fn test_lifo_ordering_within_priority() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;

    // Create multiple files with same priority (High)
    fs::write(base_path.join("SPEC-001.md"), "# Spec 1")?;
    fs::write(base_path.join("SPEC-002.md"), "# Spec 2")?;
    fs::write(base_path.join("SPEC-003.md"), "# Spec 3")?;

    let selector = ContentSelector::new()?;
    let files = selector.select_files(&base_path)?;

    // All files have High priority, should be in LIFO (reverse) order
    assert_eq!(files.len(), 3);
    assert_eq!(files[0].priority, Priority::High);
    assert_eq!(files[1].priority, Priority::High);
    assert_eq!(files[2].priority, Priority::High);

    // LIFO means reverse alphabetical order
    assert!(files[0].path.to_string().contains("SPEC-003.md"));
    assert!(files[1].path.to_string().contains("SPEC-002.md"));
    assert!(files[2].path.to_string().contains("SPEC-001.md"));

    Ok(())
}

/// Test FR-PKT-004: Byte and line counting during assembly
#[test]
fn test_byte_and_line_counting() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    // Create a file with known content
    let content = "Line 1\nLine 2\nLine 3\n";
    fs::write(base_path.join("test.md"), content)?;

    let mut builder = PacketBuilder::new()?;
    let packet = builder.build_packet(&base_path, "test", &context_dir, None)?;

    // Verify budget tracking
    assert!(packet.budget_used.bytes_used > 0);
    assert!(packet.budget_used.lines_used > 0);

    // Budget should include file content plus formatting
    assert!(packet.budget_used.bytes_used >= content.len());
    assert!(packet.budget_used.lines_used >= 3); // At least 3 lines from content

    Ok(())
}

/// Test FR-PKT-005: Limit enforcement with exit 7 on overflow
#[test]
fn test_limit_enforcement_overflow() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    // Create a large upstream file that exceeds budget
    let large_content = "data: value\n".repeat(1000);
    fs::write(base_path.join("large.core.yaml"), &large_content)?;

    // Use very small limits to trigger overflow
    let mut builder = PacketBuilder::with_limits(100, 5)?;
    let result = builder.build_packet(&base_path, "test", &context_dir, None);

    // Should fail with PacketOverflow error
    assert!(result.is_err());

    // Verify it's a packet overflow error by checking the error chain
    let err = result.unwrap_err();
    let err_string = format!("{:?}", err);
    // The error should contain information about packet overflow
    assert!(
        err_string.contains("PacketOverflow")
            || err_string.contains("packet")
            || err_string.to_lowercase().contains("overflow")
            || err_string.contains("budget")
            || err_string.contains("limit")
    );

    Ok(())
}

/// Test FR-PKT-005: Upstream files always included, regular files evicted
#[test]
fn test_upstream_non_evictable() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    // Create small upstream file and large regular file
    fs::write(base_path.join("small.core.yaml"), "key: value")?;
    fs::write(base_path.join("large.md"), "# Large\n".repeat(200))?;

    // Use limits that allow upstream but not the large regular file
    let mut builder = PacketBuilder::with_limits(300, 30)?;
    let packet = builder.build_packet(&base_path, "test", &context_dir, None)?;

    // Upstream file should be included
    assert!(
        packet
            .evidence
            .files
            .iter()
            .any(|f| f.path.contains("small.core.yaml"))
    );

    // Large regular file should be excluded
    assert!(
        !packet
            .evidence
            .files
            .iter()
            .any(|f| f.path.contains("large.md"))
    );

    Ok(())
}

/// Test FR-PKT-006: Packet preview always written to context/
#[test]
fn test_packet_preview_always_written() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    fs::write(base_path.join("test.md"), "# Test")?;

    let mut builder = PacketBuilder::new()?;
    let _packet = builder.build_packet(&base_path, "requirements", &context_dir, None)?;

    // Verify context file was written
    let context_file = context_dir.join("requirements-packet.txt");
    assert!(context_file.exists());

    let preview_content = fs::read_to_string(&context_file)?;
    assert!(preview_content.contains("test.md"));

    Ok(())
}

/// Test FR-PKT-006: Packet preview written even on overflow
///
/// Note: This tests budget overflow during packet building, NOT file selection
/// failure. When an upstream file exceeds max_file_size, it fails early before
/// any preview is written. Budget overflow happens when files fit individually
/// but exceed the total budget when combined.
#[test]
fn test_packet_preview_written_on_overflow() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    // Create upstream file that fits within max_file_size (200 bytes) but exceeds
    // the packet budget (100 bytes) after formatting. Each file gets header added.
    // Use small upstream file that will exceed budget after processing.
    let upstream_content = "key: value\n".repeat(5); // ~55 bytes fits in 200 byte file limit
    fs::write(base_path.join("data.core.yaml"), &upstream_content)?;

    // Add another upstream file to push over budget
    let upstream2 = "other: data\n".repeat(5); // Another ~60 bytes
    fs::write(base_path.join("more.core.yaml"), &upstream2)?;

    // Builder with 200 byte file limit but only 100 byte packet budget
    let mut builder = PacketBuilder::with_limits(100, 50)?;
    let _result = builder.build_packet(&base_path, "test", &context_dir, None);

    // Preview should be written because budget overflow happens during packet
    // building (after files are read), not during file selection
    let context_file = context_dir.join("test-packet.txt");
    assert!(context_file.exists());

    Ok(())
}

/// Test FR-PKT-004: Verify packet evidence includes all required fields
#[test]
fn test_packet_evidence_completeness() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    fs::write(base_path.join("test.md"), "# Test")?;
    fs::write(base_path.join("config.yaml"), "key: value")?;

    let mut builder = PacketBuilder::new()?;
    let packet = builder.build_packet(&base_path, "test", &context_dir, None)?;

    // Verify evidence structure
    assert_eq!(packet.evidence.max_bytes, DEFAULT_PACKET_MAX_BYTES);
    assert_eq!(packet.evidence.max_lines, DEFAULT_PACKET_MAX_LINES);
    assert_eq!(packet.evidence.files.len(), 2);

    // Verify each file evidence has required fields
    for file_evidence in &packet.evidence.files {
        assert!(!file_evidence.path.is_empty());
        assert!(!file_evidence.blake3_pre_redaction.is_empty());
        assert_eq!(file_evidence.blake3_pre_redaction.len(), 64); // Full BLAKE3 hash
    }

    Ok(())
}

/// Test FR-PKT-002: Priority assignment for different file types
#[test]
fn test_priority_assignment_comprehensive() -> Result<()> {
    let selector = ContentSelector::new()?;

    // Test Upstream priority (.core.yaml files)
    assert_eq!(
        selector.get_priority(Utf8PathBuf::from("test.core.yaml").as_path()),
        Priority::Upstream
    );
    assert_eq!(
        selector.get_priority(Utf8PathBuf::from("docs/design.core.yaml").as_path()),
        Priority::Upstream
    );

    // Test High priority (SPEC, ADR, REPORT)
    assert_eq!(
        selector.get_priority(Utf8PathBuf::from("SPEC-001.md").as_path()),
        Priority::High
    );
    assert_eq!(
        selector.get_priority(Utf8PathBuf::from("docs/ADR-002.md").as_path()),
        Priority::High
    );
    assert_eq!(
        selector.get_priority(Utf8PathBuf::from("REPORT-final.md").as_path()),
        Priority::High
    );

    // Test Medium priority (README, SCHEMA)
    assert_eq!(
        selector.get_priority(Utf8PathBuf::from("README.md").as_path()),
        Priority::Medium
    );
    assert_eq!(
        selector.get_priority(Utf8PathBuf::from("docs/SCHEMA.yaml").as_path()),
        Priority::Medium
    );

    // Test Low priority (misc files)
    assert_eq!(
        selector.get_priority(Utf8PathBuf::from("config.toml").as_path()),
        Priority::Low
    );
    assert_eq!(
        selector.get_priority(Utf8PathBuf::from("src/main.rs").as_path()),
        Priority::Low
    );

    Ok(())
}

/// Test FR-PKT-001: Deterministic ordering across multiple runs
#[test]
fn test_deterministic_ordering_multiple_runs() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;

    // Create multiple files
    fs::write(base_path.join("file1.md"), "# File 1")?;
    fs::write(base_path.join("file2.md"), "# File 2")?;
    fs::write(base_path.join("file3.md"), "# File 3")?;

    let selector = ContentSelector::new()?;

    // Run selection multiple times
    let files1 = selector.select_files(&base_path)?;
    let files2 = selector.select_files(&base_path)?;
    let files3 = selector.select_files(&base_path)?;

    // Results should be identical
    assert_eq!(files1.len(), files2.len());
    assert_eq!(files2.len(), files3.len());

    for i in 0..files1.len() {
        assert_eq!(files1[i].path, files2[i].path);
        assert_eq!(files2[i].path, files3[i].path);
        assert_eq!(files1[i].priority, files2[i].priority);
    }

    Ok(())
}

/// Test FR-PKT-004: Budget tracking with multiple files
#[test]
fn test_budget_tracking_multiple_files() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    // Create multiple files with known sizes
    fs::write(base_path.join("file1.md"), "12345")?; // 5 bytes
    fs::write(base_path.join("file2.md"), "67890")?; // 5 bytes
    fs::write(base_path.join("file3.md"), "abcde")?; // 5 bytes

    let mut builder = PacketBuilder::new()?;
    let packet = builder.build_packet(&base_path, "test", &context_dir, None)?;

    // Verify all files included
    assert_eq!(packet.evidence.files.len(), 3);

    // Verify budget includes all content plus formatting
    assert!(packet.budget_used.bytes_used >= 15); // At least 15 bytes from content
    assert!(packet.budget_used.bytes_used < DEFAULT_PACKET_MAX_BYTES);
    assert!(!packet.budget_used.is_exceeded());

    Ok(())
}

/// Test FR-PKT-005: Byte limit enforcement
#[test]
fn test_byte_limit_enforcement() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    // Create file that exceeds byte limit
    let content = "x".repeat(200);
    fs::write(base_path.join("large.core.yaml"), &content)?;

    let mut builder = PacketBuilder::with_limits(100, 1000)?;
    let result = builder.build_packet(&base_path, "test", &context_dir, None);

    assert!(result.is_err());

    Ok(())
}

/// Test FR-PKT-005: Line limit enforcement
#[test]
fn test_line_limit_enforcement() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    // Create file with many lines
    let content = "line\n".repeat(100);
    fs::write(base_path.join("many_lines.core.yaml"), &content)?;

    let mut builder = PacketBuilder::with_limits(100000, 10)?;
    let result = builder.build_packet(&base_path, "test", &context_dir, None);

    assert!(result.is_err());

    Ok(())
}

/// Test FR-PKT-002: Mixed priority files with budget constraints
#[test]
fn test_mixed_priority_budget_constraints() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    // Create files with different priorities
    fs::write(base_path.join("upstream.core.yaml"), "key: value")?; // Upstream
    fs::write(base_path.join("SPEC.md"), "# Spec")?; // High
    fs::write(base_path.join("README.md"), "# Readme")?; // Medium
    fs::write(base_path.join("large.md"), "x".repeat(500))?; // Low, large

    // Set limits that allow upstream, high, medium but not low
    let mut builder = PacketBuilder::with_limits(200, 50)?;
    let packet = builder.build_packet(&base_path, "test", &context_dir, None)?;

    // Verify higher priority files included
    assert!(
        packet
            .evidence
            .files
            .iter()
            .any(|f| f.path.contains("upstream.core.yaml"))
    );
    assert!(
        packet
            .evidence
            .files
            .iter()
            .any(|f| f.path.contains("SPEC.md"))
    );

    // Low priority large file should be excluded
    assert!(
        !packet
            .evidence
            .files
            .iter()
            .any(|f| f.path.contains("large.md"))
    );

    Ok(())
}

/// Test FR-PKT-001: File path sorting within same priority
#[test]
fn test_file_path_sorting_same_priority() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;

    // Create multiple files with same priority (all .md files are Low priority)
    fs::write(base_path.join("zebra.md"), "z")?;
    fs::write(base_path.join("alpha.md"), "a")?;
    fs::write(base_path.join("middle.md"), "m")?;

    let selector = ContentSelector::new()?;
    let files = selector.select_files(&base_path)?;

    // All files have same priority, should be in LIFO (reverse alphabetical) order
    assert_eq!(files.len(), 3);
    let paths: Vec<String> = files
        .iter()
        .map(|f| f.path.file_name().unwrap().to_string())
        .collect();

    // LIFO ordering means reverse alphabetical
    assert_eq!(paths[0], "zebra.md");
    assert_eq!(paths[1], "middle.md");
    assert_eq!(paths[2], "alpha.md");

    Ok(())
}

/// Test FR-PKT-004: BLAKE3 hash calculation for file evidence
#[test]
fn test_blake3_hash_in_evidence() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    fs::write(base_path.join("test.md"), "test content")?;

    let mut builder = PacketBuilder::new()?;
    let packet = builder.build_packet(&base_path, "test", &context_dir, None)?;

    // Verify BLAKE3 hash is present and correct format
    assert_eq!(packet.evidence.files.len(), 1);
    let hash = &packet.evidence.files[0].blake3_pre_redaction;

    // BLAKE3 hash should be 64 hex characters
    assert_eq!(hash.len(), 64);
    assert!(hash.chars().all(|c| c.is_ascii_hexdigit()));

    Ok(())
}

/// Test FR-PKT-006: Packet content format
#[test]
fn test_packet_content_format() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    fs::write(base_path.join("test.md"), "# Test Content")?;

    let mut builder = PacketBuilder::new()?;
    let packet = builder.build_packet(&base_path, "test", &context_dir, None)?;

    // Verify packet content includes file separator
    assert!(packet.content.contains("=== "));
    assert!(packet.content.contains("test.md"));
    assert!(packet.content.contains("==="));

    Ok(())
}

/// Test FR-PKT-006: Packet manifest generation on overflow
///
/// Note: This tests budget overflow during packet building. Files must fit
/// within max_file_size individually but exceed total packet budget together.
#[test]
fn test_packet_manifest_on_overflow() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    // Create upstream files that fit within file limit but together exceed packet budget
    // "data: value\n" is 12 bytes, x5 = 60 bytes < 100 byte file limit
    let content1 = "data: value\n".repeat(5);
    fs::write(base_path.join("first.core.yaml"), &content1)?;

    // Second file to push over the 100 byte packet budget
    let content2 = "more: stuff\n".repeat(5);
    fs::write(base_path.join("second.core.yaml"), &content2)?;

    let mut builder = PacketBuilder::with_limits(100, 5)?;
    let _result = builder.build_packet(&base_path, "test", &context_dir, None);

    // Verify manifest file was written
    let manifest_file = context_dir.join("test-packet.manifest.json");
    assert!(manifest_file.exists());

    // Verify manifest content
    let manifest_content = fs::read_to_string(&manifest_file)?;
    assert!(manifest_content.contains("overflow"));
    assert!(manifest_content.contains("budget"));
    assert!(manifest_content.contains("max_bytes"));
    assert!(manifest_content.contains("max_lines"));
    assert!(manifest_content.contains("used_bytes"));
    assert!(manifest_content.contains("used_lines"));
    assert!(manifest_content.contains("files"));
    assert!(
        manifest_content.contains("first.core.yaml")
            || manifest_content.contains("second.core.yaml")
    );

    // Verify manifest does NOT contain actual file content
    assert!(!manifest_content.contains("data: value"));

    Ok(())
}

/// Test FR-PKT-006: Manifest contains only metadata, no content
///
/// Note: Files must fit within max_file_size individually but exceed packet
/// budget together to trigger the manifest writing on overflow.
#[test]
fn test_manifest_no_content_leak() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    // Create upstream files that fit within file limit (100 bytes) but together
    // exceed packet budget (50 bytes) when combined with formatting
    // "unique: val\n" is 12 bytes, x3 = 36 bytes, fits in 100 byte limit
    let content1 = "unique: val\n".repeat(3);
    let file_path = base_path.join("config.core.yaml");
    fs::write(&file_path, &content1)?;

    // Second file to help trigger overflow
    let content2 = "other: data\n".repeat(3);
    fs::write(base_path.join("more.core.yaml"), &content2)?;

    assert!(file_path.exists(), "Test file must exist");

    let mut builder = PacketBuilder::with_limits(100, 3)?;
    let result = builder.build_packet(&base_path, "test", &context_dir, None);

    assert!(result.is_err(), "Packet should have overflowed");

    // Read manifest
    let manifest_file = context_dir.join("test-packet.manifest.json");
    assert!(manifest_file.exists(), "Manifest file should exist");

    let manifest_content = fs::read_to_string(&manifest_file)?;

    // Verify no raw content in manifest
    assert!(!manifest_content.contains("unique: val"));

    // Verify manifest has metadata
    assert!(
        manifest_content.contains("config.core.yaml")
            || manifest_content.contains("more.core.yaml")
    );
    assert!(manifest_content.contains("blake3_pre_redaction"));
    assert!(manifest_content.contains("priority"));

    Ok(())
}

/// Test FR-PKT-007: Debug packet writing
#[test]
fn test_debug_packet_writing() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    fs::write(base_path.join("test.md"), "# Test Content")?;

    let mut builder = PacketBuilder::new()?;
    let packet = builder.build_packet(&base_path, "test", &context_dir, None)?;

    // Manually write debug packet (simulating --debug-packet flag)
    builder.write_debug_packet(&packet.content, "test", &context_dir)?;

    // Verify debug packet file was written
    let debug_file = context_dir.join("test-packet-debug.txt");
    assert!(debug_file.exists());

    // Verify debug packet contains full content
    let debug_content = fs::read_to_string(&debug_file)?;
    assert!(debug_content.contains("Test Content"));
    assert!(debug_content.contains("test.md"));

    Ok(())
}

/// Test FR-PKT-007: Debug packet not written if secrets detected
#[test]
fn test_debug_packet_not_written_on_secret() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");
    let token = test_support::github_pat();

    // Create file with a secret
    fs::write(base_path.join("secret.md"), format!("token: {}", token))?;

    let mut builder = PacketBuilder::new()?;
    let result = builder.build_packet(&base_path, "test", &context_dir, None);

    // Should fail due to secret detection
    assert!(result.is_err());

    // Debug packet should NOT be written (secret scan failed)
    let debug_file = context_dir.join("test-packet-debug.txt");
    assert!(!debug_file.exists());

    Ok(())
}

/// Test FR-PKT-006: Manifest includes file priorities
///
/// Note: Files must fit within max_file_size individually but exceed packet
/// budget together to trigger manifest writing on overflow.
#[test]
fn test_manifest_includes_priorities() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    // Create files that fit within file limit (100 bytes) individually
    // but together exceed packet budget when combined with formatting
    fs::write(base_path.join("upstream.core.yaml"), "x".repeat(50))?;
    fs::write(base_path.join("another.core.yaml"), "y".repeat(50))?;

    let mut builder = PacketBuilder::with_limits(100, 5)?;
    let _result = builder.build_packet(&base_path, "test", &context_dir, None);

    // Read manifest
    let manifest_file = context_dir.join("test-packet.manifest.json");
    let manifest_content = fs::read_to_string(&manifest_file)?;

    // Verify priorities are included
    assert!(manifest_content.contains("Upstream") || manifest_content.contains("priority"));
    assert!(
        manifest_content.contains("upstream.core.yaml")
            || manifest_content.contains("another.core.yaml")
    );

    Ok(())
}

/// Test FR-PKT-006: Manifest includes BLAKE3 hashes
#[test]
fn test_manifest_includes_blake3_hashes() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;
    let context_dir = base_path.join("context");

    // Create upstream files that fit within file limit (100 bytes) individually
    // but together exceed packet budget when combined with formatting
    fs::write(base_path.join("first.core.yaml"), "x".repeat(50))?;
    fs::write(base_path.join("second.core.yaml"), "y".repeat(50))?;

    let mut builder = PacketBuilder::with_limits(100, 5)?;
    let _result = builder.build_packet(&base_path, "test", &context_dir, None);

    // Read manifest
    let manifest_file = context_dir.join("test-packet.manifest.json");
    let manifest_content = fs::read_to_string(&manifest_file)?;

    // Verify BLAKE3 hashes are included
    assert!(manifest_content.contains("blake3_pre_redaction"));

    // Verify hash format (should be 64 hex characters)
    // Extract hash value and verify it's valid hex
    assert!(manifest_content.contains("\"blake3_pre_redaction\":"));

    Ok(())
}

/// Test FR-PKT-001: Comprehensive deterministic ordering test
#[test]
fn test_comprehensive_deterministic_ordering() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let base_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())?;

    // Create files with mixed priorities and names
    fs::write(base_path.join("z-file.core.yaml"), "upstream")?;
    fs::write(base_path.join("a-file.core.yaml"), "upstream")?;
    fs::write(base_path.join("SPEC-Z.md"), "high")?;
    fs::write(base_path.join("SPEC-A.md"), "high")?;
    fs::write(base_path.join("README-Z.md"), "medium")?;
    fs::write(base_path.join("README-A.md"), "medium")?;
    fs::write(base_path.join("z-misc.md"), "low")?;
    fs::write(base_path.join("a-misc.md"), "low")?;

    let selector = ContentSelector::new()?;
    let files = selector.select_files(&base_path)?;

    // Verify priority ordering
    // Priority enum: Upstream(0) > High(1) > Medium(2) > Low(3)
    // So numerically, priority values should be non-decreasing
    let mut last_priority_value = 0u8; // Start with Upstream = 0
    for file in &files {
        let current_priority_value = match file.priority {
            Priority::Upstream => 0,
            Priority::High => 1,
            Priority::Medium => 2,
            Priority::Low => 3,
        };
        // Priority value should never decrease (stay same or increase)
        assert!(
            current_priority_value >= last_priority_value,
            "Priority ordering violated: {:?} came after priority value {}",
            file.priority,
            last_priority_value
        );
        last_priority_value = current_priority_value;
    }

    // Within each priority, verify LIFO (reverse alphabetical) ordering
    let upstream_files: Vec<_> = files
        .iter()
        .filter(|f| f.priority == Priority::Upstream)
        .collect();
    if upstream_files.len() > 1 {
        for i in 0..upstream_files.len() - 1 {
            assert!(upstream_files[i].path >= upstream_files[i + 1].path);
        }
    }

    Ok(())
}