use std::collections::BTreeSet;
use std::fs;
use std::io::Write;
use std::path::Component;
use std::path::{Path, PathBuf};
use serde::Serialize;
use tempfile::NamedTempFile;
use crate::error::{DaloError, DaloResult};
use crate::lockfile::LockedInstructionPack;
use crate::source::{SourceConfig, SourceKind};
use crate::store::{self, StorePaths};
const START_MARKER_PREFIX: &str = "<!-- dalo:start ";
const END_MARKER_PREFIX: &str = "<!-- dalo:end ";
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct InstructionPack {
pub id: String,
pub version: Option<String>,
pub body: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct InstructionPackReport {
pub pack_id: String,
pub target: PathBuf,
pub action: String,
pub dry_run: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct InstructionPackListReport {
pub active_instruction_packs: Vec<LockedInstructionPack>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct InstructionBlockDrift {
pub source_id: String,
pub pack_id: String,
pub target: PathBuf,
pub kind: InstructionBlockDriftKind,
pub message: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum InstructionBlockDriftKind {
Missing,
Malformed,
Stale,
SourceMissing,
}
fn start_marker(pack_id: &str) -> String {
format!("{START_MARKER_PREFIX}{pack_id} -->")
}
fn end_marker(pack_id: &str) -> String {
format!("{END_MARKER_PREFIX}{pack_id} -->")
}
fn find_block(content: &str, pack_id: &str) -> DaloResult<Option<(usize, usize)>> {
let start = start_marker(pack_id);
let end = end_marker(pack_id);
let starts = content.match_indices(&start).collect::<Vec<_>>();
let ends = content.match_indices(&end).collect::<Vec<_>>();
match (starts.as_slice(), ends.as_slice()) {
([], []) => Ok(None),
([(start_idx, _)], [(end_idx, _)]) if start_idx < end_idx => {
Ok(Some((*start_idx, end_idx + end.len())))
}
([], _) => Err(DaloError::MalformedInstructionBlock {
pack_id: pack_id.to_owned(),
reason: "end marker exists without a matching start marker".to_owned(),
}),
(_, []) => Err(DaloError::MalformedInstructionBlock {
pack_id: pack_id.to_owned(),
reason: "start marker exists without a matching end marker".to_owned(),
}),
_ => Err(DaloError::MalformedInstructionBlock {
pack_id: pack_id.to_owned(),
reason: "expected exactly one ordered start/end marker pair".to_owned(),
}),
}
}
pub fn render_block(content: &str, pack_id: &str, body: &str) -> DaloResult<String> {
let line_ending = line_ending_for(content);
let block = render_managed_block_with_line_ending(pack_id, body, line_ending)?;
Ok(match find_block(content, pack_id)? {
Some((start_idx, end_idx)) => {
format!("{}{}{}", &content[..start_idx], block, &content[end_idx..])
}
None => append_block(content, &block, line_ending),
})
}
#[cfg(test)]
fn render_managed_block(pack_id: &str, body: &str) -> DaloResult<String> {
render_managed_block_with_line_ending(pack_id, body, "\n")
}
fn render_managed_block_with_line_ending(
pack_id: &str,
body: &str,
line_ending: &str,
) -> DaloResult<String> {
validate_body_markers(pack_id, body)?;
let body = normalize_line_endings(
body.trim_matches(|character| character == '\n' || character == '\r'),
line_ending,
);
Ok(format!(
"{}{}{}{}{}",
start_marker(pack_id),
line_ending,
body,
line_ending,
end_marker(pack_id)
))
}
fn validate_body_markers(pack_id: &str, body: &str) -> DaloResult<()> {
if body.contains(START_MARKER_PREFIX) || body.contains(END_MARKER_PREFIX) {
return Err(DaloError::MalformedInstructionBlock {
pack_id: pack_id.to_owned(),
reason: "instruction pack body contains dalo managed-block marker text".to_owned(),
});
}
Ok(())
}
#[must_use]
pub fn instruction_block_drifts(
paths: &StorePaths,
sources: &[SourceConfig],
active: &[LockedInstructionPack],
) -> Vec<InstructionBlockDrift> {
let mut drifts = Vec::new();
for entry in active {
if let Some(drift) = instruction_block_drift(paths, sources, entry) {
drifts.push(drift);
}
}
drifts.sort_by(|left, right| {
left.target
.cmp(&right.target)
.then_with(|| left.source_id.cmp(&right.source_id))
.then_with(|| left.pack_id.cmp(&right.pack_id))
});
drifts
}
fn instruction_block_drift(
paths: &StorePaths,
sources: &[SourceConfig],
entry: &LockedInstructionPack,
) -> Option<InstructionBlockDrift> {
let target = lock_entry_target_path(paths, &entry.target);
let pack = match read_pack_for_lock_entry(paths, sources, entry) {
Ok(pack) => pack,
Err(error) => {
return Some(InstructionBlockDrift {
source_id: entry.source_id.clone(),
pack_id: entry.pack_id.clone(),
target: entry.target.clone(),
kind: InstructionBlockDriftKind::SourceMissing,
message: format!("active instruction pack source could not be read: {error}"),
});
}
};
let content = match fs::read_to_string(&target) {
Ok(content) => content,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return Some(InstructionBlockDrift {
source_id: entry.source_id.clone(),
pack_id: entry.pack_id.clone(),
target: entry.target.clone(),
kind: InstructionBlockDriftKind::Missing,
message: "instruction target file is missing".to_owned(),
});
}
Err(error) => {
return Some(InstructionBlockDrift {
source_id: entry.source_id.clone(),
pack_id: entry.pack_id.clone(),
target: entry.target.clone(),
kind: InstructionBlockDriftKind::Malformed,
message: format!("instruction target file could not be read: {error}"),
});
}
};
let expected = match render_managed_block_with_line_ending(
&entry.pack_id,
&pack.body,
line_ending_for(&content),
) {
Ok(block) => block,
Err(error) => {
return Some(InstructionBlockDrift {
source_id: entry.source_id.clone(),
pack_id: entry.pack_id.clone(),
target: entry.target.clone(),
kind: InstructionBlockDriftKind::SourceMissing,
message: format!("active instruction pack body is invalid: {error}"),
});
}
};
match find_block(&content, &entry.pack_id) {
Ok(Some((start_idx, end_idx))) if content[start_idx..end_idx] == expected => None,
Ok(Some(_)) => Some(InstructionBlockDrift {
source_id: entry.source_id.clone(),
pack_id: entry.pack_id.clone(),
target: entry.target.clone(),
kind: InstructionBlockDriftKind::Stale,
message: "instruction block does not match current pack body".to_owned(),
}),
Ok(None) => Some(InstructionBlockDrift {
source_id: entry.source_id.clone(),
pack_id: entry.pack_id.clone(),
target: entry.target.clone(),
kind: InstructionBlockDriftKind::Missing,
message: "instruction block is missing from target file".to_owned(),
}),
Err(error) => Some(InstructionBlockDrift {
source_id: entry.source_id.clone(),
pack_id: entry.pack_id.clone(),
target: entry.target.clone(),
kind: InstructionBlockDriftKind::Malformed,
message: error.to_string(),
}),
}
}
fn lock_entry_target_path(paths: &StorePaths, target: &Path) -> PathBuf {
if target.is_absolute() {
target.to_path_buf()
} else {
lexically_normalize(&paths.root.join(target))
}
}
fn read_pack_for_lock_entry(
paths: &StorePaths,
sources: &[SourceConfig],
entry: &LockedInstructionPack,
) -> DaloResult<InstructionPack> {
if entry.source_id == "local" {
return read_local_pack(paths, &entry.pack_id);
}
let Some(source) = sources.iter().find(|source| source.id == entry.source_id) else {
return Err(DaloError::unknown_source(
entry.source_id.clone(),
sources.iter().map(|source| source.id.clone()).collect(),
));
};
read_pack_from_dir(&source.path.join("instructions"), &entry.pack_id)
}
fn append_block(content: &str, block: &str, line_ending: &str) -> String {
if content.is_empty() {
return format!("{block}{line_ending}");
}
let double_line_ending = format!("{line_ending}{line_ending}");
let separator = if content.ends_with(&double_line_ending) {
""
} else if content.ends_with(line_ending) {
line_ending
} else {
return format!("{content}{line_ending}{line_ending}{block}{line_ending}");
};
format!("{content}{separator}{block}{line_ending}")
}
pub fn remove_block(content: &str, pack_id: &str) -> DaloResult<String> {
let Some((start_idx, end_idx)) = find_block(content, pack_id)? else {
return Ok(content.to_owned());
};
let line_ending = line_ending_for(content);
let before_raw = &content[..start_idx];
let (before, before_had_line_ending) = strip_line_ending_suffix(before_raw, line_ending);
let after_raw = &content[end_idx..];
let (after, _) = strip_line_ending_prefix(after_raw, line_ending);
Ok(match (before.is_empty(), after.is_empty()) {
(true, _) => after.to_owned(),
(_, true) if before_had_line_ending => format!("{before}{line_ending}"),
(_, true) => before.to_owned(),
_ if before.ends_with(line_ending) || after.starts_with(line_ending) => {
format!("{before}{after}")
}
_ => format!("{before}{line_ending}{after}"),
})
}
fn line_ending_for(content: &str) -> &'static str {
let crlf_count = content.match_indices("\r\n").count();
let bytes = content.as_bytes();
let lf_count = bytes
.iter()
.enumerate()
.filter(|(index, byte)| {
**byte == b'\n' && (*index == 0 || bytes[index.saturating_sub(1)] != b'\r')
})
.count();
if crlf_count > lf_count { "\r\n" } else { "\n" }
}
fn normalize_line_endings(content: &str, line_ending: &str) -> String {
content
.replace("\r\n", "\n")
.replace('\r', "\n")
.replace('\n', line_ending)
}
fn strip_line_ending_suffix<'a>(content: &'a str, line_ending: &str) -> (&'a str, bool) {
if let Some(stripped) = content.strip_suffix(line_ending) {
return (stripped, true);
}
if line_ending != "\r\n"
&& let Some(stripped) = content.strip_suffix("\r\n")
{
return (stripped, true);
}
if let Some(stripped) = content.strip_suffix('\n') {
return (stripped, true);
}
(content, false)
}
fn strip_line_ending_prefix<'a>(content: &'a str, line_ending: &str) -> (&'a str, bool) {
if let Some(stripped) = content.strip_prefix(line_ending) {
return (stripped, true);
}
if line_ending != "\r\n"
&& let Some(stripped) = content.strip_prefix("\r\n")
{
return (stripped, true);
}
if let Some(stripped) = content.strip_prefix('\n') {
return (stripped, true);
}
(content, false)
}
#[must_use]
pub fn has_block(content: &str, pack_id: &str) -> bool {
find_block(content, pack_id).is_ok_and(|block| block.is_some())
}
fn is_valid_pack_id(pack_id: &str) -> bool {
!pack_id.is_empty()
&& pack_id != "."
&& pack_id != ".."
&& pack_id.chars().all(|character| {
character.is_ascii_alphanumeric() || matches!(character, '.' | '_' | '-')
})
}
fn parse_version(body: &str) -> Option<String> {
body.lines()
.take(5)
.find_map(|line| line.trim().strip_prefix("version:"))
.map(|value| value.trim().to_owned())
.filter(|value| !value.is_empty())
}
pub fn read_local_pack(paths: &StorePaths, pack_id: &str) -> DaloResult<InstructionPack> {
if !is_valid_pack_id(pack_id) {
return Err(DaloError::InvalidSourceId {
id: pack_id.to_owned(),
reason: "instruction pack id must be `[A-Za-z0-9._-]` and not `.`/`..`".to_owned(),
});
}
read_pack_from_dir(&paths.local_instructions_dir, pack_id)
}
fn read_pack_from_dir(dir: &Path, pack_id: &str) -> DaloResult<InstructionPack> {
let path = dir.join(format!("{pack_id}.md"));
let body = fs::read_to_string(&path).map_err(|_| DaloError::InstructionPackNotFound {
pack_id: pack_id.to_owned(),
path: path.clone(),
})?;
Ok(InstructionPack {
id: pack_id.to_owned(),
version: parse_version(&body),
body,
})
}
pub fn enable_pack(
paths: &StorePaths,
pack_id: &str,
target: &Path,
dry_run: bool,
) -> DaloResult<InstructionPackReport> {
enable_pack_with_lock_writer(paths, pack_id, target, dry_run, store::write_user_lock)
}
fn enable_pack_with_lock_writer<F>(
paths: &StorePaths,
pack_id: &str,
target: &Path,
dry_run: bool,
write_lock: F,
) -> DaloResult<InstructionPackReport>
where
F: FnOnce(&StorePaths, &crate::lockfile::UserLock) -> DaloResult<()>,
{
let target = normalize_target_path(target)?;
let pack = read_local_pack(paths, pack_id)?;
let existing = read_target(&target)?;
let rendered = render_block(&existing, &pack.id, &pack.body)?;
let mut lock = store::read_user_lock(paths)?;
if !dry_run {
lock.active_instruction_packs
.retain(|entry| !(entry.pack_id == pack.id && targets_match(entry, &target)));
lock.active_instruction_packs.push(LockedInstructionPack {
pack_id: pack.id.clone(),
target: target.clone(),
source_id: "local".to_owned(),
commit: None,
version: pack.version,
});
lock.active_instruction_packs.sort_by(|left, right| {
left.pack_id
.cmp(&right.pack_id)
.then(left.target.cmp(&right.target))
});
let snapshot = target_snapshot(&target)?;
write_target(&target, &rendered)?;
if let Err(error) = write_lock(paths, &lock) {
return Err(restore_target_after_error(snapshot, error));
}
}
Ok(InstructionPackReport {
pack_id: pack.id,
target,
action: "enabled".to_owned(),
dry_run,
})
}
pub fn disable_pack(
paths: &StorePaths,
pack_id: &str,
target: &Path,
dry_run: bool,
) -> DaloResult<InstructionPackReport> {
disable_pack_with_lock_writer(paths, pack_id, target, dry_run, store::write_user_lock)
}
fn disable_pack_with_lock_writer<F>(
paths: &StorePaths,
pack_id: &str,
target: &Path,
dry_run: bool,
write_lock: F,
) -> DaloResult<InstructionPackReport>
where
F: FnOnce(&StorePaths, &crate::lockfile::UserLock) -> DaloResult<()>,
{
let target = normalize_target_path(target)?;
let existing = read_target(&target)?;
let has_block = find_block(&existing, pack_id)?.is_some();
let mut lock = store::read_user_lock(paths)?;
let before = lock.active_instruction_packs.len();
let has_lock_entry = lock
.active_instruction_packs
.iter()
.any(|entry| entry.pack_id == pack_id && targets_match(entry, &target));
let updated = has_block
.then(|| remove_block(&existing, pack_id))
.transpose()?;
let action = if has_block || has_lock_entry {
"disabled"
} else {
"unchanged"
};
lock.active_instruction_packs
.retain(|entry| !(entry.pack_id == pack_id && targets_match(entry, &target)));
if !dry_run {
if let Some(updated) = updated {
let snapshot = target_snapshot(&target)?;
write_target(&target, &updated)?;
if lock.active_instruction_packs.len() != before
&& let Err(error) = write_lock(paths, &lock)
{
return Err(restore_target_after_error(snapshot, error));
}
} else if lock.active_instruction_packs.len() != before {
write_lock(paths, &lock)?;
}
}
Ok(InstructionPackReport {
pack_id: pack_id.to_owned(),
target,
action: action.to_owned(),
dry_run,
})
}
fn normalize_target_path(target: &Path) -> DaloResult<PathBuf> {
let absolute = store::absolute_path(target)?;
Ok(lexically_normalize(&absolute))
}
fn targets_match(entry: &LockedInstructionPack, target: &Path) -> bool {
entry.target == target
|| (entry.target.is_relative()
&& normalize_target_path(&entry.target).is_ok_and(|normalized| normalized == target))
|| (entry.target.is_absolute()
&& fs::canonicalize(&entry.target)
.ok()
.zip(fs::canonicalize(target).ok())
.is_some_and(|(left, right)| left == right))
}
fn lexically_normalize(path: &Path) -> PathBuf {
let mut normalized = PathBuf::new();
for component in path.components() {
match component {
Component::CurDir => {}
Component::ParentDir => {
normalized.pop();
}
Component::Prefix(prefix) => normalized.push(prefix.as_os_str()),
Component::RootDir => normalized.push(component.as_os_str()),
Component::Normal(segment) => normalized.push(segment),
}
}
normalized
}
fn read_target(target: &Path) -> DaloResult<String> {
match fs::read_to_string(target) {
Ok(content) => Ok(content),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(String::new()),
Err(error) => Err(error.into()),
}
}
#[derive(Debug)]
struct TargetSnapshot {
target: PathBuf,
content: Option<String>,
}
fn target_snapshot(target: &Path) -> DaloResult<TargetSnapshot> {
let target = writable_target_path(target)?;
match fs::read_to_string(&target) {
Ok(content) => Ok(TargetSnapshot {
target,
content: Some(content),
}),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(TargetSnapshot {
target,
content: None,
}),
Err(error) => Err(error.into()),
}
}
fn restore_target(snapshot: TargetSnapshot) -> DaloResult<()> {
if let Some(content) = snapshot.content {
write_target(&snapshot.target, &content)
} else {
match fs::remove_file(snapshot.target) {
Ok(()) => Ok(()),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(error) => Err(error.into()),
}
}
}
fn restore_target_after_error(snapshot: TargetSnapshot, original_error: DaloError) -> DaloError {
let target = snapshot.target.clone();
match restore_target(snapshot) {
Ok(()) => original_error,
Err(restore_error) => DaloError::Io(std::io::Error::other(format!(
"{original_error}; also failed to restore instruction target `{}`: {restore_error}",
target.display()
))),
}
}
fn write_target(target: &Path, content: &str) -> DaloResult<()> {
let target = writable_target_path(target)?;
let existing_permissions = fs::metadata(&target)
.map(|metadata| metadata.permissions())
.ok();
let parent = target
.parent()
.filter(|parent| !parent.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."));
fs::create_dir_all(parent)?;
let mut temp_file = NamedTempFile::new_in(parent)?;
temp_file.write_all(content.as_bytes())?;
temp_file.flush()?;
temp_file.as_file().sync_all()?;
temp_file.persist(&target).map_err(|error| error.error)?;
if let Some(permissions) = existing_permissions {
fs::set_permissions(&target, permissions)?;
}
Ok(())
}
fn writable_target_path(target: &Path) -> DaloResult<PathBuf> {
match fs::symlink_metadata(target) {
Ok(metadata) if metadata.file_type().is_symlink() => Ok(fs::canonicalize(target)?),
Ok(_) => Ok(target.to_path_buf()),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(target.to_path_buf()),
Err(error) => Err(error.into()),
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct DiscoveredPack {
pub id: String,
pub source_id: String,
pub version: Option<String>,
pub topics: Vec<String>,
pub enabled: bool,
}
impl DiscoveredPack {
#[must_use]
pub fn pack_ref(&self) -> String {
format!("{}:{}", self.source_id, self.id)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct TopicOverlap {
pub packs: [String; 2],
pub topics: Vec<String>,
}
#[must_use]
pub fn discover_packs(
paths: &StorePaths,
sources: &[SourceConfig],
active: &[LockedInstructionPack],
) -> Vec<DiscoveredPack> {
let enabled: BTreeSet<(&str, &str)> = active
.iter()
.map(|entry| (entry.source_id.as_str(), entry.pack_id.as_str()))
.collect();
let mut packs = Vec::new();
scan_pack_dir(&paths.local_instructions_dir, "local", &enabled, &mut packs);
for source in sources {
if source.kind == SourceKind::Local {
continue;
}
scan_pack_dir(
&source.path.join("instructions"),
&source.id,
&enabled,
&mut packs,
);
}
packs.sort_by(|left, right| {
left.source_id
.cmp(&right.source_id)
.then_with(|| left.id.cmp(&right.id))
});
packs
}
fn scan_pack_dir(
dir: &Path,
source_id: &str,
enabled: &BTreeSet<(&str, &str)>,
out: &mut Vec<DiscoveredPack>,
) {
let Ok(entries) = fs::read_dir(dir) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.extension().and_then(|ext| ext.to_str()) != Some("md") {
continue;
}
let Some(id) = path.file_stem().and_then(|stem| stem.to_str()) else {
continue;
};
if !is_valid_pack_id(id) {
continue;
}
let body = fs::read_to_string(&path).unwrap_or_default();
out.push(DiscoveredPack {
id: id.to_owned(),
source_id: source_id.to_owned(),
version: parse_version(&body),
topics: parse_topics(&body),
enabled: enabled.contains(&(source_id, id)),
});
}
}
fn parse_topics(body: &str) -> Vec<String> {
body.lines()
.take(8)
.find_map(|line| {
let trimmed = line.trim();
trimmed
.strip_prefix("topics:")
.or_else(|| trimmed.strip_prefix("tags:"))
})
.map(|value| {
value
.split(',')
.map(|topic| topic.trim().to_owned())
.filter(|topic| !topic.is_empty())
.collect()
})
.unwrap_or_default()
}
#[must_use]
pub fn topic_overlaps(active: &[DiscoveredPack]) -> Vec<TopicOverlap> {
let mut overlaps = Vec::new();
for (index, left) in active.iter().enumerate() {
for right in active.iter().skip(index + 1) {
let mut shared: Vec<String> = left
.topics
.iter()
.filter(|topic| right.topics.contains(topic))
.cloned()
.collect();
if !shared.is_empty() {
shared.sort();
shared.dedup();
overlaps.push(TopicOverlap {
packs: [left.pack_ref(), right.pack_ref()],
topics: shared,
});
}
}
}
overlaps
}
#[cfg(test)]
mod tests {
use std::os::unix::fs::{MetadataExt, PermissionsExt, symlink};
use super::*;
use proptest::prelude::*;
const PACK: &str = "house-style";
#[test]
fn render_block_should_append_when_absent_and_be_idempotent() {
let original = "# Project\n\nNotes.\n";
let once = render_block(original, PACK, "Use tabs.").expect("render should succeed");
assert!(has_block(&once, PACK));
assert!(once.starts_with("# Project\n\nNotes.\n"));
let twice = render_block(&once, PACK, "Use tabs.").expect("render should succeed");
assert_eq!(once, twice);
}
#[test]
fn render_block_should_preserve_crlf_line_endings() {
let original = "# Project\r\n\r\nNotes.\r\n";
let rendered =
render_block(original, PACK, "Use tabs.\nSecond line.").expect("render should succeed");
assert!(rendered.contains("<!-- dalo:start house-style -->\r\n"));
assert!(rendered.contains("Use tabs.\r\nSecond line."));
assert!(!rendered.replace("\r\n", "").contains('\n'));
}
#[test]
fn render_block_should_only_touch_bytes_inside_markers() {
let original = "TOP CONTENT\n\n<!-- dalo:start house-style -->\nold\n<!-- dalo:end house-style -->\n\nBOTTOM CONTENT\n";
let updated = render_block(original, PACK, "new body").expect("render should succeed");
assert!(updated.starts_with("TOP CONTENT\n\n"));
assert!(updated.ends_with("\n\nBOTTOM CONTENT\n"));
assert!(updated.contains("new body"));
assert!(!updated.contains("old"));
}
proptest! {
#[test]
fn render_block_should_preserve_surrounding_text_and_stay_idempotent(
prefix in "[A-Za-z0-9 ._\\n-]{0,80}",
body in "[A-Za-z0-9 ._\\n-]{0,80}",
suffix in "[A-Za-z0-9 ._\\n-]{0,80}",
) {
let original = format!(
"{}{}\\nold body\\n{}{}",
prefix,
start_marker(PACK),
end_marker(PACK),
suffix
);
let rendered = render_block(&original, PACK, &body).expect("render should succeed");
prop_assert!(rendered.starts_with(&prefix));
prop_assert!(rendered.ends_with(&suffix));
prop_assert_eq!(
render_block(&rendered, PACK, &body).expect("rerender should succeed"),
rendered
);
}
}
#[test]
fn remove_block_should_preserve_surrounding_content() {
let original = "# Header\n\nIntro.\n";
let rendered = render_block(original, PACK, "Body.").expect("render should succeed");
let removed = remove_block(&rendered, PACK).expect("remove should succeed");
assert!(!has_block(&removed, PACK));
assert!(removed.contains("# Header"));
assert!(removed.contains("Intro."));
assert!(!removed.contains("dalo:start"));
}
#[test]
fn remove_block_should_keep_content_on_both_sides() {
let original = "ABOVE\n\n<!-- dalo:start house-style -->\nbody\n<!-- dalo:end house-style -->\n\nBELOW\n";
let removed = remove_block(original, PACK).expect("remove should succeed");
assert!(removed.contains("ABOVE"));
assert!(removed.contains("BELOW"));
assert!(!removed.contains("dalo:"));
}
#[test]
fn remove_block_should_preserve_crlf_seams() {
let original = "ABOVE\r\n\r\n<!-- dalo:start house-style -->\r\nbody\r\n<!-- dalo:end house-style -->\r\n\r\nBELOW\r\n";
let removed = remove_block(original, PACK).expect("remove should succeed");
assert_eq!(removed, "ABOVE\r\n\r\nBELOW\r\n");
}
#[test]
fn remove_block_should_noop_when_absent() {
let original = "# Header\n\nNo blocks here.\n";
assert_eq!(
remove_block(original, PACK).expect("remove should succeed"),
original
);
}
#[test]
fn render_block_should_reject_malformed_markers() {
let malformed = "# Header\n\n<!-- dalo:start house-style -->\nmissing end\n";
let error = render_block(malformed, PACK, "Body.").expect_err("render should fail");
assert!(matches!(error, DaloError::MalformedInstructionBlock { .. }));
}
#[test]
fn render_block_should_reject_duplicate_start_markers() {
let malformed = format!(
"# Header\n\n{}\nold\n{}\nsecond\n{}\n",
start_marker(PACK),
start_marker(PACK),
end_marker(PACK)
);
let error = render_block(&malformed, PACK, "Body.").expect_err("render should fail");
assert!(matches!(error, DaloError::MalformedInstructionBlock { .. }));
}
#[test]
fn render_block_should_reject_end_before_start() {
let malformed = format!(
"# Header\n\n{}\nold\n{}\n",
end_marker(PACK),
start_marker(PACK)
);
let error = render_block(&malformed, PACK, "Body.").expect_err("render should fail");
assert!(matches!(error, DaloError::MalformedInstructionBlock { .. }));
}
#[test]
fn render_block_should_replace_user_edits_inside_managed_block() {
let rendered = render_block("# Header\n", PACK, "Original body.")
.expect("initial render should succeed");
let edited = rendered.replace("Original body.", "User edit inside block.");
let updated =
render_block(&edited, PACK, "Original body.").expect("rerender should succeed");
assert_eq!(updated, rendered);
assert!(!updated.contains("User edit inside block."));
}
#[test]
fn render_block_should_handle_existing_block_at_eof_without_trailing_newline() {
let content = format!("{}\nOld body\n{}", start_marker(PACK), end_marker(PACK));
let updated = render_block(&content, PACK, "New body").expect("render should succeed");
assert_eq!(updated, render_managed_block(PACK, "New body").unwrap());
}
#[test]
fn render_block_should_reject_same_id_marker_in_body() {
let body = format!("Do not emit this marker:\n{}\n", end_marker(PACK));
let error = render_block("# Header\n", PACK, &body).expect_err("render should fail");
assert_body_marker_error(error, PACK);
}
#[test]
fn render_block_should_reject_different_id_marker_in_body() {
let body = format!(
"Example for another pack:\n{}\n",
start_marker("other-pack")
);
let error = render_block("# Header\n", PACK, &body).expect_err("render should fail");
assert_body_marker_error(error, PACK);
}
#[test]
fn enable_pack_should_reject_marker_body_without_rewriting_target() {
let temp = tempfile::tempdir().expect("tempdir should be created");
let store_root = temp.path().join("store");
let target = temp.path().join("AGENTS.md");
store::init_store(store_root.clone(), false).expect("store should be initialized");
let paths = StorePaths::new(store_root);
fs::write(
paths.local_instructions_dir.join(format!("{PACK}.md")),
format!("Before\n{}\nAfter\n", start_marker("other-pack")),
)
.expect("pack should be written");
fs::write(&target, "user-owned content\n").expect("target should be seeded");
let error = enable_pack(&paths, PACK, &target, false).expect_err("enable should fail");
assert_body_marker_error(error, PACK);
assert_eq!(
fs::read_to_string(&target).expect("target should be readable"),
"user-owned content\n"
);
let lock = store::read_user_lock(&paths).expect("lock should be readable");
assert!(lock.active_instruction_packs.is_empty());
}
#[test]
fn enable_pack_should_restore_target_when_lock_write_fails() {
let temp = tempfile::tempdir().expect("tempdir should be created");
let store_root = temp.path().join("store");
let target = temp.path().join("AGENTS.md");
store::init_store(store_root.clone(), false).expect("store should be initialized");
let paths = StorePaths::new(store_root);
fs::write(
paths.local_instructions_dir.join(format!("{PACK}.md")),
"Body\n",
)
.expect("pack should be written");
fs::write(&target, "user-owned content\n").expect("target should be seeded");
let error = enable_pack_with_lock_writer(&paths, PACK, &target, false, |_, _| {
Err(DaloError::Io(std::io::Error::other("lock write failed")))
})
.expect_err("lock write failure should fail enable");
assert!(matches!(error, DaloError::Io(_)));
assert_eq!(
fs::read_to_string(&target).expect("target should be readable"),
"user-owned content\n"
);
assert!(
store::read_user_lock(&paths)
.expect("lock should be readable")
.active_instruction_packs
.is_empty()
);
}
#[test]
fn disable_pack_should_restore_target_when_lock_write_fails() {
let temp = tempfile::tempdir().expect("tempdir should be created");
let store_root = temp.path().join("store");
let target = temp.path().join("AGENTS.md");
store::init_store(store_root.clone(), false).expect("store should be initialized");
let paths = StorePaths::new(store_root);
fs::write(
paths.local_instructions_dir.join(format!("{PACK}.md")),
"Body\n",
)
.expect("pack should be written");
enable_pack(&paths, PACK, &target, false).expect("pack should enable");
let before = fs::read_to_string(&target).expect("target should be readable");
let error = disable_pack_with_lock_writer(&paths, PACK, &target, false, |_, _| {
Err(DaloError::Io(std::io::Error::other("lock write failed")))
})
.expect_err("lock write failure should fail disable");
assert!(matches!(error, DaloError::Io(_)));
assert_eq!(fs::read_to_string(&target).unwrap(), before);
assert_eq!(
store::read_user_lock(&paths)
.expect("lock should be readable")
.active_instruction_packs
.len(),
1
);
}
#[test]
fn instruction_rollback_should_report_restore_failure() {
let temp = tempfile::tempdir().expect("tempdir should be created");
let occupied_target = temp.path().join("AGENTS.md");
fs::create_dir(&occupied_target).expect("target directory should be created");
let snapshot = TargetSnapshot {
target: occupied_target.clone(),
content: Some("previous content\n".to_owned()),
};
let error = restore_target_after_error(
snapshot,
DaloError::Io(std::io::Error::other("lock write failed")),
);
let message = error.to_string();
assert!(message.contains("lock write failed"));
assert!(message.contains("also failed to restore instruction target"));
assert!(message.contains(&occupied_target.display().to_string()));
}
#[test]
fn parse_version_should_read_leading_version_line() {
assert_eq!(
parse_version("version: 1.2.0\n\n# Body\n"),
Some("1.2.0".to_owned())
);
assert_eq!(parse_version("# Body only\n"), None);
}
#[test]
fn is_valid_pack_id_should_reject_traversal() {
assert!(is_valid_pack_id("house-style"));
assert!(!is_valid_pack_id(".."));
assert!(!is_valid_pack_id("bad/slash"));
assert!(!is_valid_pack_id(""));
}
#[test]
fn parse_topics_should_split_comma_separated_tags() {
assert_eq!(
parse_topics("topics: review, formatting, git\n\n# Body\n"),
vec!["review", "formatting", "git"]
);
assert_eq!(parse_topics("tags: a,b\n"), vec!["a", "b"]);
assert!(parse_topics("# No topics\n").is_empty());
}
fn discovered(id: &str, source: &str, topics: &[&str], enabled: bool) -> DiscoveredPack {
DiscoveredPack {
id: id.to_owned(),
source_id: source.to_owned(),
version: None,
topics: topics.iter().map(|topic| (*topic).to_owned()).collect(),
enabled,
}
}
#[test]
fn topic_overlaps_should_name_both_packs_sharing_a_topic() {
let active = vec![
discovered("style", "local", &["formatting", "tone"], true),
discovered("format", "team", &["formatting"], true),
];
let overlaps = topic_overlaps(&active);
assert_eq!(overlaps.len(), 1);
assert_eq!(
overlaps[0].packs,
["local:style".to_owned(), "team:format".to_owned()]
);
assert_eq!(overlaps[0].topics, vec!["formatting".to_owned()]);
}
#[test]
fn topic_overlaps_should_ignore_disjoint_topics() {
let active = vec![
discovered("a", "local", &["security"], true),
discovered("b", "team", &["formatting"], true),
];
assert!(topic_overlaps(&active).is_empty());
}
#[test]
fn discover_packs_should_find_local_packs_and_mark_enabled() {
let temp = tempfile::tempdir().expect("tempdir should be created");
let paths = StorePaths::new(temp.path().to_path_buf());
fs::create_dir_all(&paths.local_instructions_dir).expect("dir should be created");
fs::write(
paths.local_instructions_dir.join("house.md"),
"topics: x\n\nBody\n",
)
.expect("pack should be written");
let active = vec![LockedInstructionPack {
pack_id: "house".to_owned(),
target: PathBuf::from("/tmp/AGENTS.md"),
source_id: "local".to_owned(),
commit: None,
version: None,
}];
let packs = discover_packs(&paths, &[], &active);
assert_eq!(packs.len(), 1);
assert_eq!(packs[0].id, "house");
assert!(packs[0].enabled);
assert_eq!(packs[0].topics, vec!["x".to_owned()]);
}
#[test]
fn read_target_should_treat_missing_file_as_empty_only() {
let temp = tempfile::tempdir().expect("tempdir should be created");
let missing = temp.path().join("AGENTS.md");
assert_eq!(
read_target(&missing).expect("missing target should read as empty"),
""
);
}
#[test]
fn write_target_should_replace_file_via_temp_rename() {
let temp = tempfile::tempdir().expect("tempdir should be created");
let target = temp.path().join("AGENTS.md");
fs::write(&target, "old\n").expect("target should be seeded");
let before_inode = fs::metadata(&target)
.expect("target metadata should be readable")
.ino();
write_target(&target, "new\n").expect("target should be written");
assert_eq!(
fs::read_to_string(&target).expect("target should be readable"),
"new\n"
);
let after_inode = fs::metadata(&target)
.expect("target metadata should be readable")
.ino();
assert_ne!(before_inode, after_inode);
assert_eq!(
fs::read_dir(temp.path())
.expect("parent dir should be readable")
.count(),
1
);
}
#[test]
fn write_target_should_preserve_existing_permissions() {
let temp = tempfile::tempdir().expect("tempdir should be created");
let target = temp.path().join("AGENTS.md");
fs::write(&target, "old\n").expect("target should be seeded");
fs::set_permissions(&target, fs::Permissions::from_mode(0o644))
.expect("permissions should be set");
write_target(&target, "new\n").expect("target should be written");
let mode = fs::metadata(&target)
.expect("target metadata should be readable")
.permissions()
.mode()
& 0o777;
assert_eq!(mode, 0o644);
}
#[test]
fn write_target_should_write_through_symlinked_target() {
let temp = tempfile::tempdir().expect("tempdir should be created");
let canonical_target = temp.path().join("AGENTS.md");
let symlink_target = temp.path().join("CLAUDE.md");
fs::write(&canonical_target, "old\n").expect("target should be seeded");
symlink(&canonical_target, &symlink_target).expect("symlink should be created");
write_target(&symlink_target, "new\n").expect("target should be written");
assert!(
fs::symlink_metadata(&symlink_target)
.expect("symlink metadata should be readable")
.file_type()
.is_symlink()
);
assert_eq!(
fs::read_to_string(&canonical_target).expect("canonical target should be readable"),
"new\n"
);
}
fn assert_body_marker_error(error: DaloError, pack_id: &str) {
let DaloError::MalformedInstructionBlock {
pack_id: actual,
reason,
} = error
else {
panic!("expected malformed instruction block error");
};
assert_eq!(actual, pack_id);
assert!(reason.contains("body contains dalo managed-block marker"));
}
}