use quorum_core::bundle::{
assemble, BundleError, BundleInputs, FileExclusionReason, BEGIN, BUDGET_DIFF, BUDGET_MEMORY,
BUDGET_TOTAL, END,
};
use quorum_core::conventions::ConventionsState;
use quorum_core::discovery::Discovery;
use quorum_core::git::{FileStatus, StagedDiff, StagedFile};
use quorum_core::memory::{
Dismissal, DismissalId, DismissalReason, FindingIdentityHash, PromotionState,
};
fn empty_discovery() -> Discovery {
Discovery {
chosen: None,
ignored: vec![],
chosen_path: None,
}
}
fn file(path: &str, body: &[u8], binary: bool) -> StagedFile {
StagedFile {
path: path.into(),
status: FileStatus::Modified,
index_blob: if binary { None } else { Some(body.to_vec()) },
is_binary: binary,
size_bytes: body.len() as u64,
hunk_count: 1,
}
}
fn file_hunks(path: &str, body: &[u8], hunk_count: u32) -> StagedFile {
StagedFile {
hunk_count,
..file(path, body, false)
}
}
#[test]
fn quorum_state_dir_excluded_from_bundle() {
let files = vec![
file(".quorum/config.toml", b"project_id=\"p_x\"\n", false),
file(
".quorum/dismissals.sqlite",
b"SQLite format 3\0fake-db-bytes\n",
false,
),
file(
".quorum/reviews/2026-05-15T10-00-00Z.json",
b"{\"sid\":\"abc\"}\n",
false,
),
file("src/lib.rs", b"pub fn ok() {}\n", false),
];
let staged = StagedDiff {
unified: String::new(),
files,
is_empty: false,
};
let conv = ConventionsState::Absent;
let disc = empty_discovery();
let res = assemble(&BundleInputs {
staged: &staged,
memory: None,
conventions: &conv,
discovery: &disc,
branch: "main",
head_sha: "abc",
remote_url: None,
local_conventions: &[],
local_convention_bundle_cap: 500,
context: Default::default(),
})
.expect("assemble");
let excluded: Vec<&String> = res
.exclusions
.iter()
.filter_map(|(p, r)| match r {
FileExclusionReason::DenyList(_) => Some(p),
_ => None,
})
.collect();
assert!(
excluded.iter().any(|p| p.as_str() == ".quorum/config.toml"),
"config.toml must be excluded; got {excluded:?}"
);
assert!(excluded
.iter()
.any(|p| p.as_str() == ".quorum/dismissals.sqlite"));
assert!(excluded
.iter()
.any(|p| p.as_str() == ".quorum/reviews/2026-05-15T10-00-00Z.json"));
assert!(!res.prompt.contains("SQLite format 3"));
assert!(!res.prompt.contains("project_id=\"p_x\""));
assert!(res.prompt.contains("src/lib.rs"));
}
#[test]
fn deny_listed_files_excluded_with_marker() {
let files = vec![
file(".env", b"SECRET=hunter2\n", false),
file("src/main.rs", b"fn main() {}\n", false),
file("infra/staging/.env", b"NESTED=true\n", false),
file(".aws/credentials", b"[default]\n", false),
file("services/api/secrets.yml", b"k: v\n", false),
];
let staged = StagedDiff {
unified: String::new(),
files,
is_empty: false,
};
let conv = ConventionsState::Absent;
let disc = empty_discovery();
let res = assemble(&BundleInputs {
staged: &staged,
memory: None,
conventions: &conv,
discovery: &disc,
branch: "main",
head_sha: "abc",
remote_url: None,
local_conventions: &[],
local_convention_bundle_cap: 500,
context: Default::default(),
})
.expect("assemble");
let excluded: Vec<&String> = res
.exclusions
.iter()
.filter_map(|(p, r)| match r {
FileExclusionReason::DenyList(_) => Some(p),
_ => None,
})
.collect();
assert!(excluded.iter().any(|p| p.as_str() == ".env"));
assert!(excluded.iter().any(|p| p.as_str() == "infra/staging/.env"));
assert!(excluded.iter().any(|p| p.as_str() == ".aws/credentials"));
assert!(excluded
.iter()
.any(|p| p.as_str() == "services/api/secrets.yml"));
assert!(res.prompt.contains("src/main.rs"));
assert!(!res.prompt.contains("SECRET=hunter2"));
assert!(!res.prompt.contains("NESTED=true"));
}
#[test]
fn binary_files_excluded() {
let files = vec![file("logo.png", &[0u8; 10], true)];
let staged = StagedDiff {
unified: String::new(),
files,
is_empty: false,
};
let res = assemble(&BundleInputs {
staged: &staged,
memory: None,
conventions: &ConventionsState::Absent,
discovery: &empty_discovery(),
branch: "m",
head_sha: "a",
remote_url: None,
local_conventions: &[],
local_convention_bundle_cap: 500,
context: Default::default(),
})
.unwrap();
assert!(res
.exclusions
.iter()
.any(|(p, r)| p == "logo.png" && matches!(r, FileExclusionReason::Binary)));
}
#[test]
fn oversized_diff_triggers_truncation_marker() {
let huge = "x".repeat(BUDGET_DIFF + 5_000);
let staged = StagedDiff {
unified: huge,
files: vec![],
is_empty: false,
};
let res = assemble(&BundleInputs {
staged: &staged,
memory: None,
conventions: &ConventionsState::Absent,
discovery: &empty_discovery(),
branch: "m",
head_sha: "a",
remote_url: None,
local_conventions: &[],
local_convention_bundle_cap: 500,
context: Default::default(),
})
.unwrap();
assert!(res.diff_truncated);
assert!(res.prompt.contains("[diff truncated"));
}
#[test]
fn bundle_over_total_cap_errors() {
let huge_mem = "m".repeat(50_000);
let huge_conv = "c".repeat(50_000);
let staged = StagedDiff {
unified: "x".repeat(BUDGET_DIFF),
files: vec![file("a.rs", &vec![b'a'; 70_000], false)],
is_empty: false,
};
let res = assemble(&BundleInputs {
staged: &staged,
memory: Some(quorum_core::bundle::MemoryInput {
source_basename: "CLAUDE.md".into(),
content: huge_mem,
}),
conventions: &ConventionsState::Trusted(huge_conv),
discovery: &empty_discovery(),
branch: "m",
head_sha: "a",
remote_url: None,
local_conventions: &[],
local_convention_bundle_cap: 500,
context: Default::default(),
});
match res {
Ok(r) => assert!(r.bytes_used <= BUDGET_TOTAL, "stays under cap"),
Err(BundleError::BundleTooLarge(n, _)) => assert!(n > BUDGET_TOTAL, "reports excess"),
}
}
fn empty_staged() -> StagedDiff {
StagedDiff {
unified: String::new(),
files: vec![],
is_empty: true,
}
}
fn dismissal_fixture(
seed: u8,
title: &str,
body: Option<&str>,
recurrence: u32,
state: PromotionState,
at_ymd: (i32, u8, u8),
last_seen_ymd: (i32, u8, u8),
) -> Dismissal {
let h = FindingIdentityHash([seed; 32]);
let mk = |(y, m, d): (i32, u8, u8)| {
let date = time::Date::from_calendar_date(y, time::Month::try_from(m).unwrap(), d).unwrap();
time::PrimitiveDateTime::new(date, time::Time::MIDNIGHT).assume_utc()
};
Dismissal {
id: DismissalId(seed as i64),
finding_identity_hash: h,
title_snapshot: title.into(),
body_snapshot: body.map(|s| s.to_string()),
source_type_snapshot: "agreement".into(),
models_snapshot: vec!["m1".into()],
branch_snapshot: "main".into(),
reason: DismissalReason::FalsePositive,
note: None,
dismissed_at: mk(at_ymd),
last_seen_at: mk(last_seen_ymd),
last_seen_session_id: None,
recurrence_count: recurrence,
expires_at: None,
repo_head_sha_first: "sha".into(),
promotion_state: state,
}
}
fn assemble_memory_only<'a>(
local: &'a [Dismissal],
cap: usize,
conventions: &'a ConventionsState,
memory: Option<quorum_core::bundle::MemoryInput>,
) -> String {
let staged = empty_staged();
let res = assemble(&BundleInputs {
staged: &staged,
memory,
conventions,
discovery: &empty_discovery(),
branch: "m",
head_sha: "a",
remote_url: None,
local_conventions: local,
local_convention_bundle_cap: cap,
context: Default::default(),
})
.expect("assemble");
res.prompt
}
#[test]
fn ac152_local_only_entries_render_under_header_in_sorted_order() {
let high = dismissal_fixture(
0xAA,
"use ? operator instead of unwrap",
Some("Prefer `?` over `.unwrap()` in production paths."),
7,
PromotionState::LocalOnly,
(2026, 1, 15),
(2026, 5, 10),
);
let low = dismissal_fixture(
0xBB,
"snake_case for module names",
Some("Rust module names should be snake_case."),
3,
PromotionState::LocalOnly,
(2026, 2, 1),
(2026, 5, 9),
);
let prompt = assemble_memory_only(&[high, low], 500, &ConventionsState::Absent, None);
assert!(prompt.contains("## Local conventions (auto-derived)"));
assert!(prompt.contains("Local convention: use ? operator instead of unwrap"));
assert!(prompt.contains("Local convention: snake_case for module names"));
let pos_high = prompt.find("recurrence=7").unwrap();
let pos_low = prompt.find("recurrence=3").unwrap();
assert!(pos_high < pos_low, "high-recurrence row must precede low");
}
#[test]
fn ac153_per_entry_format_matches_spec() {
let d = dismissal_fixture(
0xAB,
"title body",
Some("body content"),
4,
PromotionState::LocalOnly,
(2026, 1, 7),
(2026, 5, 10),
);
let prompt = assemble_memory_only(&[d], 500, &ConventionsState::Absent, None);
assert!(prompt.contains("### Local convention: title body\n"));
assert!(prompt.contains("body content"));
assert!(
prompt.contains("<!-- recurrence=4, since=2026-01-07, hash=ababababab"),
"metadata line missing or wrong shape; prompt was:\n{prompt}"
);
}
#[test]
fn ac154_per_entry_truncation_marker_when_body_overflows_cap() {
let body = "x".repeat(600);
let d = dismissal_fixture(
0xC1,
"long body convention",
Some(&body),
5,
PromotionState::LocalOnly,
(2026, 3, 3),
(2026, 5, 10),
);
let prompt = assemble_memory_only(&[d], 100, &ConventionsState::Absent, None);
assert!(
prompt.contains("[local convention truncated: "),
"per-entry truncation marker missing"
);
assert!(prompt.contains("500 bytes elided"));
assert!(prompt.contains("raise [memory] local_convention_bundle_cap to see full text"));
assert!(prompt.contains("[local convention truncated: c1c1c1c1c1c1"));
}
#[test]
fn ac154_total_section_truncation_marker_when_entries_exceed_budget() {
let big_body = "z".repeat(2000);
let mut rows = Vec::new();
for i in 0u8..15 {
rows.push(dismissal_fixture(
i,
&format!("convention number {i}"),
Some(&big_body),
(15 - i) as u32, PromotionState::LocalOnly,
(2026, 1, 1),
(2026, 5, 10),
));
}
let prompt = assemble_memory_only(&rows, 2048, &ConventionsState::Absent, None);
assert!(
prompt.contains("[memory truncated:"),
"total-section truncation marker missing"
);
assert!(prompt.contains("bytes exceeded 20KB; review only top portion"));
}
#[test]
fn ac155_bridge_dirty_renders_promoted_in_memory_section() {
let d = dismissal_fixture(
0xD0,
"promoted but dirty",
Some("rule body"),
4,
PromotionState::PromotedConvention,
(2026, 1, 10),
(2026, 5, 10),
);
let prompt = assemble_memory_only(&[d], 500, &ConventionsState::PresentButIgnored, None);
assert!(prompt.contains("## Local conventions (auto-derived)"));
assert!(prompt.contains("Local convention: promoted but dirty"));
}
#[test]
fn ac155_bridge_trusted_suppresses_promoted_in_memory_section() {
let d = dismissal_fixture(
0xD1,
"promoted and clean",
Some("rule body"),
4,
PromotionState::PromotedConvention,
(2026, 1, 10),
(2026, 5, 10),
);
let prompt = assemble_memory_only(
&[d],
500,
&ConventionsState::Trusted("# committed conventions\n".into()),
None,
);
assert!(!prompt.contains("Local convention: promoted and clean"));
assert!(!prompt.contains("## Local conventions (auto-derived)"));
}
#[test]
fn ac155_mixed_local_and_bridged_render_under_one_header() {
let local = dismissal_fixture(
0xE0,
"a real local rule",
Some("text"),
5,
PromotionState::LocalOnly,
(2026, 1, 1),
(2026, 5, 10),
);
let bridged = dismissal_fixture(
0xE1,
"bridged from promoted",
Some("text"),
3,
PromotionState::PromotedConvention,
(2026, 1, 1),
(2026, 5, 9),
);
let prompt = assemble_memory_only(
&[local, bridged],
500,
&ConventionsState::PresentButIgnored, None,
);
assert_eq!(
prompt
.matches("## Local conventions (auto-derived)")
.count(),
1
);
assert!(prompt.contains("a real local rule"));
assert!(prompt.contains("bridged from promoted"));
}
#[test]
fn empty_subsection_omits_header() {
let promoted_clean = dismissal_fixture(
0x10,
"lives in conventions.md",
Some("text"),
2,
PromotionState::PromotedConvention,
(2026, 1, 1),
(2026, 5, 10),
);
let prompt = assemble_memory_only(
&[promoted_clean],
500,
&ConventionsState::Trusted("committed\n".into()),
None,
);
assert!(!prompt.contains("## Local conventions (auto-derived)"));
}
#[test]
fn ac170_delimiters_wrap_entire_memory_section() {
let d = dismissal_fixture(
0xF0,
"wrapped under delimiters",
Some("body"),
2,
PromotionState::LocalOnly,
(2026, 1, 1),
(2026, 5, 10),
);
let prompt = assemble_memory_only(&[d], 500, &ConventionsState::Absent, None);
let begin_pos = prompt.find(BEGIN).unwrap();
let end_pos = prompt.find(END).unwrap();
let subsection_pos = prompt.find("## Local conventions (auto-derived)").unwrap();
assert!(begin_pos < subsection_pos);
assert!(subsection_pos < end_pos);
assert_eq!(prompt.matches(BEGIN).count(), 1);
assert_eq!(prompt.matches(END).count(), 1);
}
#[test]
fn ac173_partial_render_side_toggle() {
let d = dismissal_fixture(
0x77,
"toggle me",
Some("toggle body"),
4,
PromotionState::PromotedConvention,
(2026, 4, 1),
(2026, 5, 10),
);
let dirty = assemble_memory_only(
std::slice::from_ref(&d),
500,
&ConventionsState::PresentButIgnored,
None,
);
assert!(dirty.contains("toggle me"));
let clean = assemble_memory_only(
std::slice::from_ref(&d),
500,
&ConventionsState::Trusted("committed\n".into()),
None,
);
assert!(!clean.contains("toggle me"));
let dirty_again = assemble_memory_only(
std::slice::from_ref(&d),
500,
&ConventionsState::PresentButIgnored,
None,
);
assert!(dirty_again.contains("toggle me"));
}
#[test]
fn body_without_snapshot_renders_only_header_and_comment() {
let d = dismissal_fixture(
0x33,
"title only",
None,
2,
PromotionState::LocalOnly,
(2026, 1, 1),
(2026, 5, 10),
);
let prompt = assemble_memory_only(&[d], 500, &ConventionsState::Absent, None);
assert!(prompt.contains("### Local convention: title only\n"));
assert!(prompt.contains("<!-- recurrence=2, since=2026-01-01, hash=3333333333"));
assert!(!prompt.contains("[local convention truncated:"));
}
#[test]
fn body_multibyte_utf8_truncation_lands_on_codepoint_boundary() {
let body = "café".repeat(10); let d = dismissal_fixture(
0x44,
"utf8 title",
Some(&body),
2,
PromotionState::LocalOnly,
(2026, 1, 1),
(2026, 5, 10),
);
let prompt = assemble_memory_only(&[d], 4, &ConventionsState::Absent, None);
assert!(prompt.contains("[local convention truncated:"));
assert!(prompt.is_ascii() || std::str::from_utf8(prompt.as_bytes()).is_ok());
}
#[test]
fn title_truncation_is_codepoint_aware_at_80() {
let title: String = "é".repeat(100);
let d = dismissal_fixture(
0x55,
&title,
None,
1,
PromotionState::LocalOnly,
(2026, 1, 1),
(2026, 5, 10),
);
let prompt = assemble_memory_only(&[d], 500, &ConventionsState::Absent, None);
let expected_title: String = "é".repeat(80);
assert!(prompt.contains(&format!("### Local convention: {expected_title}\n")));
let too_many: String = "é".repeat(81);
assert!(!prompt.contains(&format!("### Local convention: {too_many}")));
}
#[test]
fn ac156_total_bundle_stays_under_cap_with_local_conventions() {
let big = "z".repeat(2048);
let mut rows = Vec::new();
for i in 0u8..30 {
rows.push(dismissal_fixture(
i,
&format!("rule {i}"),
Some(&big),
(30 - i) as u32,
PromotionState::LocalOnly,
(2026, 1, 1),
(2026, 5, 10),
));
}
let staged = StagedDiff {
unified: "x".repeat(BUDGET_DIFF),
files: vec![file("a.rs", &vec![b'a'; 70_000], false)],
is_empty: false,
};
let res = assemble(&BundleInputs {
staged: &staged,
memory: Some(quorum_core::bundle::MemoryInput {
source_basename: "CLAUDE.md".into(),
content: "c".repeat(10_000),
}),
conventions: &ConventionsState::Trusted("c".repeat(5_000)),
discovery: &empty_discovery(),
branch: "m",
head_sha: "a",
remote_url: None,
local_conventions: &rows,
local_convention_bundle_cap: 2048,
context: Default::default(),
});
match res {
Ok(r) => assert!(r.bytes_used <= BUDGET_TOTAL),
Err(BundleError::BundleTooLarge(n, _)) => assert!(n > BUDGET_TOTAL),
}
}
#[test]
fn claude_md_content_precedes_local_conventions_subsection() {
let d = dismissal_fixture(
0x66,
"ordering check",
Some("body"),
2,
PromotionState::LocalOnly,
(2026, 1, 1),
(2026, 5, 10),
);
let prompt = assemble_memory_only(
&[d],
500,
&ConventionsState::Absent,
Some(quorum_core::bundle::MemoryInput {
source_basename: "CLAUDE.md".into(),
content: "CLAUDE_MD_MARKER body".into(),
}),
);
let claude_pos = prompt.find("CLAUDE_MD_MARKER").unwrap();
let subsection_pos = prompt.find("## Local conventions (auto-derived)").unwrap();
assert!(claude_pos < subsection_pos);
}
#[allow(dead_code)]
const _: usize = BUDGET_MEMORY;
fn body_kb(kb: usize, fill: char) -> Vec<u8> {
let line: String = std::iter::repeat(fill).take(79).collect::<String>() + "\n";
line.repeat(kb * 1024 / 80).into_bytes()
}
#[test]
fn ac178_large_markdown_does_not_evict_code_under_review() {
let a_md = body_kb(60, 'a');
let b_md = body_kb(30, 'b');
let x_ts = body_kb(4, 'x');
let x_test_ts = body_kb(2, 't');
let types_ts = body_kb(1, 'y');
let files = vec![
file("a.md", &a_md, false),
file("b.md", &b_md, false),
file("src/x.ts", &x_ts, false),
file("src/x.test.ts", &x_test_ts, false),
file("types.ts", &types_ts, false),
];
let staged = StagedDiff {
unified: String::new(),
files,
is_empty: false,
};
let conv = ConventionsState::Absent;
let disc = empty_discovery();
let res = assemble(&BundleInputs {
staged: &staged,
memory: None,
conventions: &conv,
discovery: &disc,
branch: "main",
head_sha: "abc",
remote_url: None,
local_conventions: &[],
local_convention_bundle_cap: 500,
context: Default::default(),
})
.expect("assemble");
for (path, body) in [
("src/x.ts", &x_ts),
("src/x.test.ts", &x_test_ts),
("types.ts", &types_ts),
] {
assert!(
!res.files_omitted.contains(&path.to_string()),
"{path} must not be omitted; omitted = {:?}",
res.files_omitted
);
let last_line = String::from_utf8(body.clone())
.unwrap()
.lines()
.last()
.unwrap()
.to_string();
assert!(
res.prompt.contains(&last_line),
"{path} must be included in full (last line missing)"
);
}
assert!(
res.files_omitted.contains(&"a.md".to_string()),
"a.md (60KB docs) must be omitted; omitted = {:?}",
res.files_omitted
);
}
use quorum_core::bundle::{class_rank, FileClass, RelatedContext};
use quorum_core::config::BundleConfig;
use std::collections::HashSet;
struct Tree {
dir: tempfile::TempDir,
tracked: HashSet<String>,
}
impl Tree {
fn new(files: &[(&str, &str)]) -> Tree {
let dir = tempfile::tempdir().unwrap();
let mut tracked = HashSet::new();
for (path, body) in files {
let abs = dir.path().join(path);
std::fs::create_dir_all(abs.parent().unwrap()).unwrap();
std::fs::write(&abs, body).unwrap();
tracked.insert((*path).to_string());
}
Tree { dir, tracked }
}
fn untracked(&self, path: &str, body: &str) {
let abs = self.dir.path().join(path);
std::fs::create_dir_all(abs.parent().unwrap()).unwrap();
std::fs::write(&abs, body).unwrap();
}
fn ctx(&self, cfg: BundleConfig) -> RelatedContext<'_> {
RelatedContext {
repo_root: Some(self.dir.path()),
tracked: Some(&self.tracked),
cfg,
}
}
}
fn assemble_with(
files: Vec<StagedFile>,
ctx: RelatedContext<'_>,
) -> quorum_core::bundle::BundleResult {
let staged = StagedDiff {
unified: String::new(),
files,
is_empty: false,
};
let conv = ConventionsState::Absent;
let disc = empty_discovery();
assemble(&BundleInputs {
staged: &staged,
memory: None,
conventions: &conv,
discovery: &disc,
branch: "main",
head_sha: "abc",
remote_url: None,
local_conventions: &[],
local_convention_bundle_cap: 500,
context: ctx,
})
.expect("assemble")
}
fn included_paths(res: &quorum_core::bundle::BundleResult) -> Vec<String> {
res.inclusion_order
.iter()
.filter(|r| r.included)
.map(|r| r.path.clone())
.collect()
}
#[test]
fn ac183_config_allowlist_pulled_in_and_labelled() {
let tree = Tree::new(&[
("package.json", "{\"name\":\"demo\"}\n"),
("tsconfig.json", "{\"compilerOptions\":{}}\n"),
("src/app.ts", "export const a = 1;\n"),
]);
let res = assemble_with(
vec![file("src/app.ts", b"export const a = 2;\n", false)],
tree.ctx(BundleConfig::default()),
);
let inc = included_paths(&res);
assert!(inc.contains(&"package.json".to_string()), "got {inc:?}");
assert!(inc.contains(&"tsconfig.json".to_string()), "got {inc:?}");
assert!(res.prompt.contains("### package.json (unchanged, context)"));
assert!(res
.prompt
.contains("### tsconfig.json (unchanged, context)"));
assert!(res.prompt.contains("### src/app.ts\n"));
assert!(res.prompt.contains("\"name\":\"demo\""));
}
#[test]
fn ac183_allowlist_is_keyed_on_the_changed_language() {
let tree = Tree::new(&[
("Cargo.toml", "[package]\nname = \"x\"\n"),
("package.json", "{}\n"),
("src/lib.rs", "pub fn a() {}\n"),
]);
let res = assemble_with(
vec![file("src/lib.rs", b"pub fn a() {}\n", false)],
tree.ctx(BundleConfig::default()),
);
let inc = included_paths(&res);
assert!(inc.contains(&"Cargo.toml".to_string()), "got {inc:?}");
assert!(
!inc.contains(&"package.json".to_string()),
"a Rust change must not drag in the JS config set; got {inc:?}"
);
}
#[test]
fn ac184_resolution_is_one_hop_only() {
let tree = Tree::new(&[
("src/a.ts", "import { b } from './b';\n"),
(
"src/b.ts",
"import { c } from './c';\nexport const b = 1;\n",
),
("src/c.ts", "export const c = 2;\n"),
]);
let res = assemble_with(
vec![file("src/a.ts", b"import { b } from './b';\n", false)],
tree.ctx(BundleConfig::default()),
);
let inc = included_paths(&res);
assert!(inc.contains(&"src/b.ts".to_string()), "got {inc:?}");
assert!(
!inc.contains(&"src/c.ts".to_string()),
"neighbours of neighbours must never be added; got {inc:?}"
);
}
#[test]
fn ac184_one_hop_for_rust_module_references() {
let tree = Tree::new(&[
("src/main.rs", "mod helper;\nuse crate::deep::thing;\n"),
("src/helper.rs", "mod nested;\npub fn h() {}\n"),
("src/helper/nested.rs", "pub fn n() {}\n"),
("src/deep.rs", "pub struct thing;\n"),
]);
let res = assemble_with(
vec![file(
"src/main.rs",
b"mod helper;\nuse crate::deep::thing;\n",
false,
)],
tree.ctx(BundleConfig::default()),
);
let inc = included_paths(&res);
assert!(inc.contains(&"src/helper.rs".to_string()), "got {inc:?}");
assert!(inc.contains(&"src/deep.rs".to_string()), "got {inc:?}");
assert!(
!inc.contains(&"src/helper/nested.rs".to_string()),
"second hop must not be followed; got {inc:?}"
);
}
#[test]
fn ac185_related_files_never_displace_changed_files() {
let big = "x".repeat(40 * 1024);
let tree = Tree::new(&[
("src/a.ts", "import './ctx1';\nimport './ctx2';\n"),
("src/ctx1.ts", &big),
("src/ctx2.ts", &big),
]);
let cfg = BundleConfig {
total_budget_kb: 100,
..BundleConfig::default()
};
let changed_body = b"import './ctx1';\nimport './ctx2';\n";
let res = assemble_with(vec![file("src/a.ts", changed_body, false)], tree.ctx(cfg));
assert!(
!res.files_omitted.contains(&"src/a.ts".to_string()),
"the changed file must never be evicted by context; omitted = {:?}",
res.files_omitted
);
assert_eq!(res.inclusion_order[0].path, "src/a.ts");
}
#[test]
fn ac186_related_file_cap_binds_and_is_reported() {
let mut files: Vec<(&str, &str)> = vec![("src/a.ts", "")];
let owned: Vec<(String, String)> = (0..10)
.map(|i| {
(
format!("src/m{i}.ts"),
format!("export const m{i} = {i};\n"),
)
})
.collect();
for (p, b) in &owned {
files.push((p.as_str(), b.as_str()));
}
let tree = Tree::new(&files);
let body: String = (0..10).map(|i| format!("import './m{i}';\n")).collect();
let cfg = BundleConfig {
related_file_max: 3,
..BundleConfig::default()
};
let res = assemble_with(
vec![file("src/a.ts", body.as_bytes(), false)],
tree.ctx(cfg),
);
let related: Vec<_> = res
.inclusion_order
.iter()
.filter(|r| r.path != "src/a.ts")
.collect();
assert_eq!(related.len(), 3, "cap must bind at 3");
assert_eq!(res.related_capped_out, 7);
assert!(
res.prompt
.contains("[related-file cap reached: related_file_max=3;"),
"the cap must be visible in the inclusion-order block"
);
}
#[test]
fn ac186_related_file_max_zero_admits_nothing() {
let tree = Tree::new(&[
("Cargo.toml", "[package]\n"),
("src/lib.rs", "pub fn a() {}\n"),
]);
let cfg = BundleConfig {
related_file_max: 0,
..BundleConfig::default()
};
let res = assemble_with(
vec![file("src/lib.rs", b"pub fn a() {}\n", false)],
tree.ctx(cfg),
);
assert_eq!(included_paths(&res), vec!["src/lib.rs".to_string()]);
}
#[test]
fn ac187_denylist_applies_to_related_files() {
let tree = Tree::new(&[
("src/a.ts", "import './secret.env';\n"),
(".env", "API_KEY=sk-live-should-never-ship\n"),
("src/keys.ts", "export const k = 1;\n"),
]);
let cfg = BundleConfig {
include: vec![".env".into(), "src/*.ts".into()],
..BundleConfig::default()
};
let res = assemble_with(
vec![file("src/a.ts", b"import './keys';\n", false)],
tree.ctx(cfg),
);
let inc = included_paths(&res);
assert!(
!inc.contains(&".env".to_string()),
"a deny-listed related file must never be included; got {inc:?}"
);
assert!(!res.prompt.contains("sk-live-should-never-ship"));
assert!(res
.exclusions
.iter()
.any(|(p, r)| p == ".env" && matches!(r, FileExclusionReason::DenyList(_))));
}
#[test]
fn ac188_related_files_respect_size_and_binary_checks() {
let tree = Tree::new(&[("src/a.ts", "")]);
tree.untracked("src/bin.ts", "");
std::fs::write(tree.dir.path().join("src/bin.ts"), [0xffu8, 0x00, b'h']).unwrap();
let mut tracked = tree.tracked.clone();
tracked.insert("src/bin.ts".to_string());
let ctx = RelatedContext {
repo_root: Some(tree.dir.path()),
tracked: Some(&tracked),
cfg: BundleConfig {
include: vec!["src/bin.ts".into()],
..BundleConfig::default()
},
};
let res = assemble_with(vec![file("src/a.ts", b"const a = 1;\n", false)], ctx);
assert!(
!included_paths(&res).contains(&"src/bin.ts".to_string()),
"a binary related file must be excluded"
);
assert!(res
.exclusions
.iter()
.any(|(p, r)| p == "src/bin.ts" && matches!(r, FileExclusionReason::Binary)));
}
#[test]
fn ac188_related_files_respect_max_file_bytes() {
let tree = Tree::new(&[("src/a.ts", "")]);
let huge = "x".repeat(2 * 1024 * 1024 + 1);
tree.untracked("src/huge.ts", &huge);
let mut tracked = tree.tracked.clone();
tracked.insert("src/huge.ts".to_string());
let ctx = RelatedContext {
repo_root: Some(tree.dir.path()),
tracked: Some(&tracked),
cfg: BundleConfig {
include: vec!["src/huge.ts".into()],
..BundleConfig::default()
},
};
let res = assemble_with(vec![file("src/a.ts", b"const a = 1;\n", false)], ctx);
assert!(
!included_paths(&res).contains(&"src/huge.ts".to_string()),
"a related file over MAX_FILE_BYTES must be excluded"
);
assert!(res
.exclusions
.iter()
.any(|(p, r)| p == "src/huge.ts" && matches!(r, FileExclusionReason::TooLarge)));
}
#[test]
fn ac189_related_discovery_never_leaves_the_repo() {
let tree = Tree::new(&[("src/a.ts", "")]);
let outside = tree.dir.path().parent().unwrap().join("outside_secret.ts");
std::fs::write(&outside, "export const leaked = 1;\n").ok();
let res = assemble_with(
vec![file(
"src/a.ts",
b"import '../../outside_secret';\nimport '../outside_secret';\n",
false,
)],
tree.ctx(BundleConfig::default()),
);
assert!(!res.prompt.contains("leaked"));
assert_eq!(included_paths(&res), vec!["src/a.ts".to_string()]);
std::fs::remove_file(&outside).ok();
}
fn symlink_file(target: &std::path::Path, link: &std::path::Path) -> bool {
#[cfg(unix)]
{
std::os::unix::fs::symlink(target, link).expect("symlink");
true
}
#[cfg(windows)]
{
match std::os::windows::fs::symlink_file(target, link) {
Ok(()) => true,
Err(e) => {
eprintln!(
"SKIPPED: cannot create a symlink here ({e}); this test is authoritative on Unix"
);
false
}
}
}
}
fn link_dir(target: &std::path::Path, link: &std::path::Path) {
#[cfg(unix)]
std::os::unix::fs::symlink(target, link).expect("dir symlink");
#[cfg(windows)]
{
let status = std::process::Command::new("cmd")
.args(["/C", "mklink", "/J"])
.arg(link)
.arg(target)
.stdout(std::process::Stdio::null())
.status()
.expect("run mklink /J");
assert!(status.success(), "mklink /J failed");
}
}
fn unsafe_reason(res: &quorum_core::bundle::BundleResult, path: &str) -> Option<&'static str> {
res.exclusions.iter().find_map(|(p, r)| match r {
FileExclusionReason::UnsafePath(why) if p == path => Some(*why),
_ => None,
})
}
#[test]
fn sec1_allowlist_symlink_out_of_repo_is_excluded_and_reported() {
let outside = tempfile::tempdir().unwrap();
let creds = outside.path().join("credentials");
std::fs::write(
&creds,
"[default]\naws_secret_access_key = OUTSIDE-CANARY-1\n",
)
.unwrap();
let mut tree = Tree::new(&[("src/app.ts", "export const a = 1;\n")]);
if !symlink_file(&creds, &tree.dir.path().join("tsconfig.json")) {
return;
}
tree.tracked.insert("tsconfig.json".into());
let res = assemble_with(
vec![file("src/app.ts", b"export const a = 2;\n", false)],
tree.ctx(BundleConfig::default()),
);
assert!(
!res.prompt.contains("OUTSIDE-CANARY-1"),
"credentials behind a symlink were sent:\n{}",
res.prompt
);
assert!(!included_paths(&res).contains(&"tsconfig.json".to_string()));
assert_eq!(
unsafe_reason(&res, "tsconfig.json"),
Some("resolves outside the repository"),
"the skip must be reported; exclusions = {:?}",
res.exclusions
);
}
#[test]
fn sec1_one_hop_symlink_out_of_repo_is_excluded_and_reported() {
let outside = tempfile::tempdir().unwrap();
let key = outside.path().join("id_rsa");
std::fs::write(
&key,
"-----BEGIN OPENSSH PRIVATE KEY-----\nOUTSIDE-CANARY-2\n",
)
.unwrap();
let mut tree = Tree::new(&[("src/lib.rs", "mod foo;\n")]);
if !symlink_file(&key, &tree.dir.path().join("src/foo.rs")) {
return;
}
tree.tracked.insert("src/foo.rs".into());
let res = assemble_with(
vec![file("src/lib.rs", b"mod foo;\npub fn a() {}\n", false)],
tree.ctx(BundleConfig::default()),
);
assert!(!res.prompt.contains("OUTSIDE-CANARY-2"), "{}", res.prompt);
assert_eq!(
unsafe_reason(&res, "src/foo.rs"),
Some("resolves outside the repository"),
"exclusions = {:?}",
res.exclusions
);
}
#[test]
fn sec1_in_repo_symlink_is_still_allowed() {
let mut tree = Tree::new(&[
(
"configs/tsconfig.base.json",
"{\"compilerOptions\":{\"strict\":true}}\n",
),
("src/app.ts", "export const a = 1;\n"),
]);
let link = tree.dir.path().join("tsconfig.json");
if !symlink_file(std::path::Path::new("configs/tsconfig.base.json"), &link) {
return;
}
tree.tracked.insert("tsconfig.json".into());
let res = assemble_with(
vec![file("src/app.ts", b"export const a = 2;\n", false)],
tree.ctx(BundleConfig::default()),
);
assert!(
included_paths(&res).contains(&"tsconfig.json".to_string()),
"an in-repo symlink must still be followed; exclusions = {:?}",
res.exclusions
);
assert!(res
.prompt
.contains("### tsconfig.json (unchanged, context)"));
assert!(res.prompt.contains("\"strict\":true"));
assert_eq!(unsafe_reason(&res, "tsconfig.json"), None);
}
#[test]
fn sec1_in_repo_symlink_to_denied_or_untracked_target_is_excluded() {
let mut tree = Tree::new(&[("src/app.ts", "export const a = 1;\n")]);
tree.untracked(".env", "API_KEY=IN-REPO-CANARY-3\n");
tree.untracked("local.json", "{\"token\":\"UNTRACKED-CANARY-4\"}\n");
let root = tree.dir.path().to_path_buf();
if !symlink_file(std::path::Path::new(".env"), &root.join("tsconfig.json"))
|| !symlink_file(
std::path::Path::new("local.json"),
&root.join("package.json"),
)
{
return;
}
tree.tracked.insert("tsconfig.json".into());
tree.tracked.insert("package.json".into());
let res = assemble_with(
vec![file("src/app.ts", b"export const a = 2;\n", false)],
tree.ctx(BundleConfig::default()),
);
assert!(!res.prompt.contains("IN-REPO-CANARY-3"), "{}", res.prompt);
assert!(!res.prompt.contains("UNTRACKED-CANARY-4"), "{}", res.prompt);
assert_eq!(
unsafe_reason(&res, "tsconfig.json"),
Some("symlink target is deny-listed")
);
assert_eq!(
unsafe_reason(&res, "package.json"),
Some("symlink target is not tracked")
);
}
#[test]
fn sec1_untracked_allowlist_config_is_not_sent() {
let tree = Tree::new(&[
("src/app.ts", "export const a = 1;\n"),
("tsconfig.json", "{\"compilerOptions\":{}}\n"),
]);
tree.untracked("package.json", "{\"name\":\"UNTRACKED-CANARY-5\"}\n");
tree.untracked(
".eslintrc.js",
"module.exports = { c: 'UNTRACKED-CANARY-6' };\n",
);
let res = assemble_with(
vec![file("src/app.ts", b"export const a = 2;\n", false)],
tree.ctx(BundleConfig::default()),
);
let inc = included_paths(&res);
assert!(!res.prompt.contains("UNTRACKED-CANARY-5"), "got {inc:?}");
assert!(!res.prompt.contains("UNTRACKED-CANARY-6"), "got {inc:?}");
assert!(inc.contains(&"tsconfig.json".to_string()), "got {inc:?}");
}
#[test]
fn sec1_directory_link_out_of_repo_is_excluded_and_reported() {
let outside = tempfile::tempdir().unwrap();
std::fs::write(
outside.path().join("creds.ts"),
"export const leaked = 'OUTSIDE-CANARY-7';\n",
)
.unwrap();
let mut tree = Tree::new(&[("src/app.ts", "export const a = 1;\n")]);
link_dir(outside.path(), &tree.dir.path().join("vendor"));
tree.tracked.insert("vendor/creds.ts".into());
let cfg = BundleConfig {
include: vec!["vendor/*.ts".into()],
..BundleConfig::default()
};
let res = assemble_with(
vec![file("src/app.ts", b"export const a = 2;\n", false)],
tree.ctx(cfg),
);
assert!(!res.prompt.contains("OUTSIDE-CANARY-7"), "{}", res.prompt);
assert_eq!(
unsafe_reason(&res, "vendor/creds.ts"),
Some("resolves outside the repository"),
"exclusions = {:?}",
res.exclusions
);
}
#[test]
fn ac190_related_files_false_restores_v033_candidate_set() {
let tree = Tree::new(&[
("Cargo.toml", "[package]\nname = \"x\"\n"),
("src/lib.rs", "mod helper;\n"),
("src/helper.rs", "pub fn h() {}\n"),
]);
let changed = || {
vec![
file("src/lib.rs", b"mod helper;\n", false),
file("README.md", b"# docs\n", false),
]
};
let on = assemble_with(changed(), tree.ctx(BundleConfig::default()));
let off = assemble_with(
changed(),
tree.ctx(BundleConfig {
related_files: false,
..BundleConfig::default()
}),
);
let mut off_paths = included_paths(&off);
off_paths.sort();
assert_eq!(
off_paths,
vec!["README.md".to_string(), "src/lib.rs".to_string()],
"with related_files = false the candidate set is the changed set"
);
assert!(included_paths(&on).contains(&"src/helper.rs".to_string()));
assert_eq!(off.related_capped_out, 0);
}
#[test]
fn ac182_no_file_included_twice() {
let tree = Tree::new(&[
("Cargo.toml", "[package]\nname = \"x\"\n"),
("src/lib.rs", "pub fn a() {}\n"),
]);
let cfg = BundleConfig {
include: vec!["Cargo.toml".into(), "src/*.rs".into()],
..BundleConfig::default()
};
let res = assemble_with(
vec![
file("Cargo.toml", b"[package]\nname = \"x\"\n", false),
file("src/lib.rs", b"pub fn a() {}\n", false),
],
tree.ctx(cfg),
);
let inc = included_paths(&res);
let mut uniq = inc.clone();
uniq.sort();
uniq.dedup();
assert_eq!(inc.len(), uniq.len(), "duplicate paths in {inc:?}");
assert_eq!(
res.prompt.matches("### Cargo.toml").count(),
1,
"Cargo.toml emitted more than once"
);
assert!(!res.prompt.contains("### Cargo.toml (unchanged, context)"));
}
#[test]
fn ac193_include_globs_enter_at_their_implied_class() {
let tree = Tree::new(&[
("src/a.rs", "pub fn a() {}\n"),
("docs/notes.md", "# notes\n"),
("schema/001.sql", "CREATE TABLE t();\n"),
]);
let cfg = BundleConfig {
include: vec!["docs/*.md".into(), "schema/*.sql".into()],
..BundleConfig::default()
};
let res = assemble_with(
vec![file("src/a.rs", b"pub fn a() {}\n", false)],
tree.ctx(cfg),
);
let by_path = |p: &str| {
res.inclusion_order
.iter()
.find(|r| r.path == p)
.unwrap_or_else(|| panic!("{p} missing from {:?}", included_paths(&res)))
};
assert_eq!(by_path("docs/notes.md").class, FileClass::Docs);
assert_eq!(by_path("schema/001.sql").class, FileClass::Source);
assert_eq!(class_rank("schema/001.sql"), FileClass::Source);
assert_eq!(res.inclusion_order[0].path, "src/a.rs");
}
#[test]
fn ac180_inclusion_order_block_is_emitted() {
let tree = Tree::new(&[
("Cargo.toml", "[package]\n"),
("src/lib.rs", "pub fn a() {}\n"),
]);
let res = assemble_with(
vec![
file_hunks("src/lib.rs", b"pub fn a() {}\n", 4),
file_hunks("README.md", b"# hi\n", 1),
],
tree.ctx(BundleConfig::default()),
);
assert!(res.prompt.contains("## File inclusion order"));
assert!(res
.prompt
.contains("src/lib.rs class=source hunks=4 bytes=14 origin=changed included"));
assert!(res
.prompt
.contains("README.md class=docs hunks=1 bytes=5 origin=changed included"));
assert!(res.prompt.contains("Cargo.toml class=config hunks=0"));
assert!(res.prompt.contains("origin=config-allowlist"));
let pos_src = res.prompt.find("src/lib.rs class=").unwrap();
let pos_doc = res.prompt.find("README.md class=").unwrap();
assert!(pos_src < pos_doc, "block must follow the real ordering");
}
#[test]
fn ac181_omission_marker_shape_is_unchanged() {
let big = body_kb(90, 'z');
let res = assemble_with(
vec![
file("src/small.rs", b"pub fn a() {}\n", false),
file("huge.md", &big, false),
],
Default::default(),
);
assert_eq!(res.files_omitted, vec!["huge.md".to_string()]);
assert!(
res.prompt.contains(&format!(
"[file omitted: huge.md, {} bytes; budget exhausted]",
big.len()
)),
"marker shape changed"
);
}
#[test]
fn ac182a_changed_file_bodies_are_line_numbered() {
let res = assemble_with(
vec![file("src/a.rs", b"fn one() {}\nfn two() {}\n", false)],
Default::default(),
);
assert!(res.prompt.contains(" 1 | fn one() {}"));
assert!(res.prompt.contains(" 2 | fn two() {}"));
}
#[test]
fn ac182b_context_files_are_line_numbered_and_explained() {
let tree = Tree::new(&[
("Cargo.toml", "[package]\nname = \"x\"\n"),
("src/lib.rs", "pub fn a() {}\n"),
]);
let res = assemble_with(
vec![file("src/lib.rs", b"pub fn a() {}\n", false)],
tree.ctx(BundleConfig::default()),
);
assert!(res.prompt.contains("### Cargo.toml (unchanged, context)"));
assert!(res.prompt.contains(" 1 | [package]"));
assert!(res.prompt.contains(" 2 | name = \"x\""));
assert!(
res.prompt.contains("working tree") && res.prompt.contains("HEAD"),
"the section must explain what the context line numbers refer to"
);
}
#[test]
fn ac194_raising_total_budget_admits_more_files() {
let changed = || {
vec![
file("src/a.rs", &body_kb(40, 'a'), false),
file("src/b.rs", &body_kb(40, 'b'), false),
file("src/c.rs", &body_kb(40, 'c'), false),
]
};
let small = assemble_with(changed(), Default::default());
let large = assemble_with(
changed(),
RelatedContext {
cfg: BundleConfig {
total_budget_kb: 400,
..BundleConfig::default()
},
..Default::default()
},
);
assert!(
!small.files_omitted.is_empty(),
"120KB of source must not fit the 200KB default file budget"
);
assert!(
large.files_omitted.len() < small.files_omitted.len(),
"raising total_budget_kb must admit more: {:?} vs {:?}",
large.files_omitted,
small.files_omitted
);
}
#[test]
fn tracked_paths_reflects_head() {
let dir = tempfile::tempdir().unwrap();
let repo = git2::Repository::init(dir.path()).unwrap();
std::fs::create_dir_all(dir.path().join("src")).unwrap();
std::fs::write(dir.path().join("src/a.rs"), "pub fn a() {}\n").unwrap();
std::fs::write(dir.path().join("Cargo.toml"), "[package]\n").unwrap();
let mut idx = repo.index().unwrap();
idx.add_path(std::path::Path::new("src/a.rs")).unwrap();
idx.add_path(std::path::Path::new("Cargo.toml")).unwrap();
idx.write().unwrap();
let tree_id = idx.write_tree().unwrap();
let tree = repo.find_tree(tree_id).unwrap();
let sig = git2::Signature::now("t", "t@e").unwrap();
repo.commit(Some("HEAD"), &sig, &sig, "c", &tree, &[])
.unwrap();
std::fs::write(dir.path().join("src/scratch.rs"), "// scratch\n").unwrap();
let tracked = quorum_core::git::tracked_paths(&repo);
assert!(tracked.contains("src/a.rs"));
assert!(tracked.contains("Cargo.toml"));
assert!(
!tracked.contains("src/scratch.rs"),
"untracked files must not be offered as context"
);
}
fn staged_repo(
base: &[(&str, &str)],
staged: &[(&str, Option<&str>)],
) -> (tempfile::TempDir, StagedDiff) {
let dir = tempfile::tempdir().unwrap();
let repo = git2::Repository::init(dir.path()).unwrap();
let write = |path: &str, body: &str| {
let abs = dir.path().join(path);
std::fs::create_dir_all(abs.parent().unwrap()).unwrap();
std::fs::write(abs, body).unwrap();
};
let mut idx = repo.index().unwrap();
for (p, b) in base {
write(p, b);
idx.add_path(std::path::Path::new(p)).unwrap();
}
idx.write().unwrap();
let tree_id = idx.write_tree().unwrap();
{
let tree = repo.find_tree(tree_id).unwrap();
let sig = git2::Signature::now("t", "t@e").unwrap();
repo.commit(Some("HEAD"), &sig, &sig, "base", &tree, &[])
.unwrap();
}
for (p, b) in staged {
match b {
Some(body) => {
write(p, body);
idx.add_path(std::path::Path::new(p)).unwrap();
}
None => {
std::fs::remove_file(dir.path().join(p)).unwrap();
idx.remove_path(std::path::Path::new(p)).unwrap();
}
}
}
idx.write().unwrap();
drop(repo);
let (_repo, diff) = quorum_core::git::staged_diff(dir.path()).unwrap();
(dir, diff)
}
fn assemble_staged(staged: &StagedDiff) -> quorum_core::bundle::BundleResult {
let conv = ConventionsState::Absent;
let disc = empty_discovery();
assemble(&BundleInputs {
staged,
memory: None,
conventions: &conv,
discovery: &disc,
branch: "main",
head_sha: "0000000000000000000000000000000000000000",
remote_url: None,
local_conventions: &[],
local_convention_bundle_cap: 500,
context: Default::default(),
})
.expect("assemble")
}
fn section_of<'a>(unified: &'a str, path: &str) -> &'a str {
let head = format!("diff --git a/{path} b/{path}\n");
let start = unified
.find(&head)
.unwrap_or_else(|| panic!("no section for {path} in:\n{unified}"));
let body_start = start + head.len();
let end = unified[body_start..]
.find("\ndiff --git ")
.map(|i| body_start + i + 1)
.unwrap_or(unified.len());
&unified[start..end]
}
fn short_blob_id(body: &str) -> String {
git2::Oid::hash_object(git2::ObjectType::Blob, body.as_bytes())
.unwrap()
.to_string()[..7]
.to_string()
}
fn diff_part(prompt: &str) -> &str {
prompt.split("## Changed-file contents").next().unwrap()
}
const ENV_OLD: &str = "DB_HOST=prod.internal\nAWS_SECRET_ACCESS_KEY=old\n";
const ENV_NEW: &str = "DB_HOST=prod.internal\n\
AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI_EXAMPLE\n\
STRIPE_SECRET_KEY=sk_live_51H8xQ2\n";
fn assert_no_trace(text: &str, label: &str, blobs: &[&str]) {
for blob in blobs {
for line in blob.lines().filter(|l| !l.is_empty()) {
assert!(
!text.contains(line),
"{label}: denied-file line {line:?} leaked into the bundle:\n{text}"
);
}
let id = short_blob_id(blob);
assert!(
!text.contains(&id),
"{label}: denied blob id {id} (its `index` line) leaked into the bundle:\n{text}"
);
}
}
#[test]
fn sec1_staged_env_contributes_nothing_to_the_diff_body() {
let (_dir, staged) = staged_repo(&[(".env", ENV_OLD)], &[(".env", Some(ENV_NEW))]);
assert!(staged
.unified
.contains("+STRIPE_SECRET_KEY=sk_live_51H8xQ2"));
assert!(staged.unified.contains(&short_blob_id(ENV_NEW)));
let res = assemble_staged(&staged);
assert_no_trace(&res.prompt, "modified .env", &[ENV_OLD, ENV_NEW]);
assert!(
!res.prompt.contains("@@"),
"a hunk header from the denied section survived:\n{}",
res.prompt
);
}
#[test]
fn sec1_deleted_or_renamed_away_env_contributes_nothing_to_the_diff() {
let (_d1, deleted) = staged_repo(&[(".env", ENV_NEW)], &[(".env", None)]);
assert!(deleted
.unified
.contains("-STRIPE_SECRET_KEY=sk_live_51H8xQ2"));
let res = assemble_staged(&deleted);
assert_no_trace(&res.prompt, "deleted .env", &[ENV_NEW]);
assert!(res
.prompt
.contains("[excluded by deny-list: .env, pattern=.env]"));
let renamed_body = format!("{ENV_NEW}EXTRA=1\n");
let (_d2, renamed) = staged_repo(
&[(".env", ENV_NEW)],
&[(".env", None), ("config/env.txt", Some(&renamed_body))],
);
assert!(
renamed.unified.contains("rename from .env"),
"precondition: libgit2 must detect the rename:\n{}",
renamed.unified
);
let res = assemble_staged(&renamed);
let diff = diff_part(&res.prompt);
assert_no_trace(diff, "renamed-away .env", &[ENV_NEW]);
assert!(diff.contains("[excluded by deny-list: .env, pattern=.env]"));
assert!(res
.exclusions
.iter()
.any(|(p, r)| p == ".env" && matches!(r, FileExclusionReason::DenyList(_))));
}
#[test]
fn sec1_content_mentioning_a_denied_path_is_not_mutilated() {
let readme_old = "# Setup\n";
let readme_new = "# Setup\n\
Copy a/.env.example to b/.env before running.\n\
diff --git a/.env b/.env is what git prints for it.\n";
let (_dir, staged) = staged_repo(
&[(".env", ENV_OLD), ("README.md", readme_old)],
&[(".env", Some(ENV_NEW)), ("README.md", Some(readme_new))],
);
let readme = section_of(&staged.unified, "README.md");
assert!(readme.contains("+Copy a/.env.example to b/.env before running."));
let res = assemble_staged(&staged);
assert!(
res.prompt.contains(readme),
"README.md's diff section must reach the bundle byte-for-byte.\n\
expected section:\n{readme}\nbundle:\n{}",
res.prompt
);
assert_no_trace(&res.prompt, ".env beside README", &[ENV_OLD, ENV_NEW]);
}
#[test]
fn sec1_marker_replaces_the_denied_section_in_place() {
let (_dir, staged) = staged_repo(
&[(".env", ENV_OLD), ("src/main.rs", "fn main() {}\n")],
&[
(".env", Some(ENV_NEW)),
("src/main.rs", Some("fn main() { println!(\"hi\"); }\n")),
],
);
let res = assemble_staged(&staged);
let marker = "[excluded by deny-list: .env, pattern=.env]";
let diff = diff_part(&res.prompt);
assert_eq!(
diff.matches(marker).count(),
2,
"expected the header-summary marker plus one in place:\n{diff}"
);
let in_place = format!("{marker}\ndiff --git a/src/main.rs b/src/main.rs\n");
assert!(diff.contains(&in_place), "marker not in place:\n{diff}");
assert!(res
.exclusions
.iter()
.any(|(p, r)| p == ".env" && matches!(r, FileExclusionReason::DenyList(".env"))));
}
#[test]
fn sec1_normal_file_beside_denied_file_is_intact() {
let pem_old = "-----OLD PEM BODY-----\n";
let pem_new = "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASC\n";
let (_dir, staged) = staged_repo(
&[
("src/main.rs", "fn main() {}\n"),
("deploy/server.pem", pem_old),
],
&[
(
"src/main.rs",
Some("fn main() {\n let token = std::env::var(\"API\");\n}\n"),
),
("deploy/server.pem", Some(pem_new)),
],
);
let main = section_of(&staged.unified, "src/main.rs");
assert!(main.contains("@@"), "precondition: main.rs has a hunk");
let res = assemble_staged(&staged);
assert!(
res.prompt.contains(main),
"src/main.rs's section must survive intact.\nexpected:\n{main}\nbundle:\n{}",
res.prompt
);
assert_no_trace(&res.prompt, "server.pem", &[pem_old, pem_new]);
}
#[test]
fn sec1_quoted_header_path_is_still_filtered() {
let (_dir, staged) = staged_repo(
&[("café/.env", ENV_OLD), ("src/lib.rs", "pub fn a() {}\n")],
&[
("café/.env", Some(ENV_NEW)),
("src/lib.rs", Some("pub fn a() -> u8 { 1 }\n")),
],
);
assert!(staged.unified.contains("STRIPE_SECRET_KEY=sk_live_51H8xQ2"));
let res = assemble_staged(&staged);
assert_no_trace(&res.prompt, "quoted café/.env", &[ENV_OLD, ENV_NEW]);
assert!(res
.prompt
.contains("[excluded by deny-list: café/.env, pattern=.env]"));
assert!(res
.prompt
.contains(section_of(&staged.unified, "src/lib.rs")));
}