use super::*;
use crate::core::automation::{AutomationDraft, ManagedAutomation, Schedule};
#[test]
fn edited_files_increment_revision_and_queue_the_custom_script_for_backup() {
let temp = tempfile::tempdir().unwrap();
let root = temp.path().join("runtime");
let workspace = temp.path().join("workspace");
std::fs::create_dir(&workspace).unwrap();
let mut record = ManagedAutomation {
id: format!("agent_hub:{}", uuid::Uuid::new_v4()),
revision: 1,
draft: AutomationDraft {
package_options: None,
files: vec![
super::super::AutomationScriptFile {
path: "bundle/main.py".into(),
content: "print('original')".into(),
},
super::super::AutomationScriptFile {
path: "bundle/helper.py".into(),
content: "VALUE = 1".into(),
},
],
name: "Cleanup".into(),
description: None,
provider: Provider::System,
content: "exit 0".into(),
script_dir: None,
workdir: Some(workspace.to_string_lossy().into_owned()),
schedule: Schedule::Manual,
enabled: false,
},
source: super::super::native::source().into(),
identifier: String::new(),
location: String::new(),
};
let (identifier, location) = super::super::native::paths(&root, &record).unwrap();
record.identifier = identifier;
record.location = location.to_string_lossy().into_owned();
store::save(&root, vec![record.clone()]).unwrap();
let file = payload::write(
&super::super::native::directory(&root, &record),
&record.draft,
&record.id,
true,
)
.unwrap();
std::fs::write(&file, "printf customized").unwrap();
std::fs::write(workspace.join("bundle/main.py"), "print('customized')").unwrap();
std::fs::write(workspace.join("bundle/helper.py"), "VALUE = 2").unwrap();
sync_at(&root).unwrap();
let registry = store::read_registry(&root).unwrap();
assert_eq!(registry.tasks[0].revision, 2);
assert_eq!(registry.tasks[0].draft.content, "printf customized");
assert_eq!(
registry.pending_backup[&record.id]
.as_ref()
.unwrap()
.content,
"printf customized"
);
assert_eq!(
registry.tasks[0].draft.files[0].content,
"print('customized')"
);
assert_eq!(registry.tasks[0].draft.files[1].content, "VALUE = 2");
assert_eq!(
registry.pending_backup[&record.id]
.as_ref()
.unwrap()
.files
.len(),
2
);
assert!(!registry.tasks[0].draft.enabled);
sync_at(&root).unwrap();
assert_eq!(store::load(&root).unwrap()[0].revision, 2);
assert_eq!(std::fs::read_to_string(&file).unwrap(), "printf customized");
std::fs::write(&file, "").unwrap();
assert!(
sync_at(&root).is_err(),
"invalid files must not be backed up as stale content"
);
assert_eq!(store::load(&root).unwrap()[0].revision, 2);
}
#[test]
fn reads_wait_out_a_busy_store_then_fall_back_while_writes_still_refuse() {
let temp = tempfile::tempdir().unwrap();
let root = temp.path();
std::fs::write(root.join("tasks.json"), r#"{"version":1,"tasks":[]}"#).unwrap();
let held = store::Lock::acquire(root).unwrap();
assert!(!sync_for_read(root, std::time::Duration::from_millis(200)).unwrap());
assert!(sync_at(root).is_err());
let release = std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(150));
drop(held);
});
assert!(sync_for_read(root, std::time::Duration::from_secs(5)).unwrap());
release.join().unwrap();
}