use proptest::prelude::*;
use ricecoder_storage::{ProjectStore, ResourceType};
use tempfile::TempDir;
fn resource_name_strategy() -> impl Strategy<Value = String> {
r"[a-zA-Z0-9_\-]+" .prop_map(|s| format!("{}.txt", s))
.prop_filter("Name should be non-empty", |s| !s.is_empty())
}
fn folder_name_strategy() -> impl Strategy<Value = String> {
r"[a-zA-Z0-9_\-]+" .prop_filter("Name should be non-empty", |s| !s.is_empty())
}
fn resource_content_strategy() -> impl Strategy<Value = Vec<u8>> {
prop::collection::vec(any::<u8>(), 0..1000)
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(100))]
#[test]
fn prop_project_resource_storage_consistency(
resource_type in prop_oneof![
Just(ResourceType::Template),
Just(ResourceType::Standard),
Just(ResourceType::Spec),
Just(ResourceType::Steering),
],
name in resource_name_strategy(),
content in resource_content_strategy(),
) {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let store = ProjectStore::new(temp_dir.path().to_path_buf());
store.initialize().expect("Failed to initialize store");
let stored_path = store
.store_resource(resource_type, &name, &content)
.expect("Failed to store resource");
assert!(stored_path.exists(), "Stored file should exist");
assert_eq!(
stored_path.parent().unwrap(),
store.resource_path(resource_type),
"File should be in correct resource directory"
);
let retrieved = store
.retrieve_resource(resource_type, &name)
.expect("Failed to retrieve resource");
assert_eq!(
retrieved, content,
"Retrieved content should match stored content"
);
}
#[test]
fn prop_folder_creation_on_demand(folder_name in folder_name_strategy()) {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let store = ProjectStore::new(temp_dir.path().to_path_buf());
store.initialize().expect("Failed to initialize store");
assert!(
!store.folder_exists(&folder_name),
"Folder should not exist initially"
);
let folder_path = store
.create_folder(&folder_name)
.expect("Failed to create folder");
assert!(folder_path.exists(), "Folder should exist after creation");
assert!(
store.folder_exists(&folder_name),
"folder_exists should return true after creation"
);
assert!(
folder_path.is_dir(),
"Created path should be a directory"
);
}
#[test]
fn prop_folder_creation_idempotent(folder_name in folder_name_strategy()) {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let store = ProjectStore::new(temp_dir.path().to_path_buf());
store.initialize().expect("Failed to initialize store");
let path1 = store
.create_folder(&folder_name)
.expect("First create failed");
let path2 = store
.create_folder(&folder_name)
.expect("Second create failed");
let path3 = store
.create_folder(&folder_name)
.expect("Third create failed");
assert_eq!(path1, path2, "First and second paths should match");
assert_eq!(path2, path3, "Second and third paths should match");
assert!(store.folder_exists(&folder_name));
}
#[test]
fn prop_project_resource_storage_idempotent(
resource_type in prop_oneof![
Just(ResourceType::Template),
Just(ResourceType::Standard),
],
name in resource_name_strategy(),
content in resource_content_strategy(),
) {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let store = ProjectStore::new(temp_dir.path().to_path_buf());
store.initialize().expect("Failed to initialize store");
store
.store_resource(resource_type, &name, &content)
.expect("First store failed");
store
.store_resource(resource_type, &name, &content)
.expect("Second store failed");
store
.store_resource(resource_type, &name, &content)
.expect("Third store failed");
let retrieved = store
.retrieve_resource(resource_type, &name)
.expect("Failed to retrieve resource");
assert_eq!(
retrieved, content,
"Content should be identical after multiple stores"
);
}
#[test]
fn prop_project_resource_existence_accurate(
resource_type in prop_oneof![
Just(ResourceType::Template),
Just(ResourceType::Standard),
],
name in resource_name_strategy(),
content in resource_content_strategy(),
) {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let store = ProjectStore::new(temp_dir.path().to_path_buf());
store.initialize().expect("Failed to initialize store");
assert!(
!store.resource_exists(resource_type, &name),
"Resource should not exist initially"
);
store
.store_resource(resource_type, &name, &content)
.expect("Failed to store resource");
assert!(
store.resource_exists(resource_type, &name),
"Resource should exist after storing"
);
store
.delete_resource(resource_type, &name)
.expect("Failed to delete resource");
assert!(
!store.resource_exists(resource_type, &name),
"Resource should not exist after deletion"
);
}
}