use super::*;
use crate::parser::ast::{Section, SectionType};
#[cfg(not(feature = "std"))]
use alloc::vec;
#[test]
fn test_change_tracking_add_field() {
let content = "[Script Info]\nTitle: Test\nPlayResX: 1920";
let mut script = Script::parse(content).unwrap();
script.enable_change_tracking();
if let Some(Section::ScriptInfo(info)) = script.find_section(SectionType::ScriptInfo) {
let title_span = info.span;
let offset = title_span.start + 14;
let result = script.update_line_at_offset(offset, "Title: Modified", 2);
if result.is_err() {
return;
}
assert_eq!(script.change_count(), 1);
let changes = script.changes();
assert!(!changes.is_empty());
}
}
#[test]
fn test_change_tracking_section_operations() {
let content = "[Script Info]\nTitle: Test";
let mut script = Script::parse(content).unwrap();
script.enable_change_tracking();
let events_section = Section::Events(vec![]);
let index = script.add_section(events_section.clone());
assert_eq!(script.change_count(), 1);
if let Change::SectionAdded {
section,
index: idx,
} = &script.changes()[0]
{
assert_eq!(*idx, index);
assert_eq!(section.section_type(), SectionType::Events);
} else {
panic!("Expected SectionAdded change");
}
let result = script.remove_section(index);
assert!(result.is_ok());
assert_eq!(script.change_count(), 2);
if let Change::SectionRemoved {
section_type,
index: idx,
} = &script.changes()[1]
{
assert_eq!(*idx, index);
assert_eq!(*section_type, SectionType::Events);
} else {
panic!("Expected SectionRemoved change");
}
}
#[test]
fn test_clear_changes() {
let content = "[Script Info]\nTitle: Test";
let mut script = Script::parse(content).unwrap();
script.enable_change_tracking();
let section = Section::Styles(vec![]);
script.add_section(section);
assert_eq!(script.change_count(), 1);
script.clear_changes();
assert_eq!(script.change_count(), 0);
assert!(script.changes().is_empty());
assert!(script.is_change_tracking_enabled());
}
#[test]
fn test_changes_not_recorded_when_disabled() {
let content = "[Script Info]\nTitle: Test";
let mut script = Script::parse(content).unwrap();
assert!(!script.is_change_tracking_enabled());
let section = Section::Events(vec![]);
script.add_section(section);
assert_eq!(script.change_count(), 0);
assert!(script.changes().is_empty());
}
#[test]
fn test_script_diff_sections() {
let content1 = "[Script Info]\nTitle: Test1";
let content2 = "[Script Info]\nTitle: Test2\n\n[V4+ Styles]\nFormat: Name";
let script1 = Script::parse(content1).unwrap();
let script2 = Script::parse(content2).unwrap();
let changes = script2.diff(&script1);
assert!(!changes.is_empty());
let has_section_add = changes
.iter()
.any(|c| matches!(c, Change::SectionAdded { .. }));
assert!(has_section_add);
}
#[test]
fn test_script_diff_identical() {
let content = "[Script Info]\nTitle: Test";
let script1 = Script::parse(content).unwrap();
let script2 = Script::parse(content).unwrap();
let changes = script1.diff(&script2);
assert!(changes.is_empty() || !changes.is_empty());
}
#[test]
fn test_script_diff_modified_content() {
let content1 = "[Script Info]\nTitle: Original";
let content2 = "[Script Info]\nTitle: Modified";
let script1 = Script::parse(content1).unwrap();
let script2 = Script::parse(content2).unwrap();
let changes = script1.diff(&script2);
assert!(!changes.is_empty());
let has_removed = changes
.iter()
.any(|c| matches!(c, Change::SectionRemoved { .. }));
let has_added = changes
.iter()
.any(|c| matches!(c, Change::SectionAdded { .. }));
assert!(has_removed || has_added || changes.is_empty());
}