about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/actions/create.rs71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/actions/create.rs b/src/actions/create.rs
index bc7f7b0..2b664b3 100644
--- a/src/actions/create.rs
+++ b/src/actions/create.rs
@@ -21,3 +21,74 @@ pub fn create(command_options: ActionOptions, fs: &impl Fs, timestamp: u64) -> R
 
     Ok(())
 }
+
+#[cfg(test)]
+mod tests {
+    use std::path::Path;
+
+    use crate::{actions::ActionOptions, diff::ContentChange, filesystem::mock::{EntryMock, FsMock, FsState}, history::{FileChange, FileChangeVariant, FileHistory, RepositoryChange, RepositoryHistory}};
+
+    use super::create;
+
+    #[test]
+    fn create_empty() {
+        let now = 0xC0FFEE;
+        let fs_mock = FsMock::new();
+        let options = ActionOptions::from_path(".");
+
+        let expected_index = RepositoryHistory::default().encode().unwrap();
+        
+        create(options, &fs_mock, now).expect("Action failed.");
+
+        fs_mock.assert_match(FsState::new(vec![
+            EntryMock::dir("./.ka"),
+            EntryMock::file("./.ka/index", &expected_index),
+            EntryMock::dir("./.ka/files"),
+        ]));
+    }
+
+    #[test]
+    fn create_basic() {
+        let now = 0xC0FFEE;
+        let mut fs_mock = FsMock::new();
+        let options = ActionOptions::from_path(".");
+
+        let expected_index = {
+            let mut history = RepositoryHistory::default();
+            history.add_change(RepositoryChange {
+                affected_files: vec![Path::new("./test").into()],
+                timestamp: now,
+            });
+            history.cursor = 1;
+            history.encode().unwrap()
+        };
+
+        let expected_file_history = {
+            let mut history = FileHistory::default();
+            let change = ContentChange::Inserted {
+                at: 0,
+                new_content: vec![1, 2, 3],
+            };
+            history.add_change(FileChange {
+                change_index: 1,
+                variant: FileChangeVariant::Updated(vec![change]),
+            });
+            history.encode().unwrap()
+        };
+
+        fs_mock.set_state(FsState::new(vec![
+            EntryMock::file("./test", &vec![1, 2, 3])
+        ]));
+
+        create(options, &fs_mock, now).expect("Action failed.");
+
+        fs_mock.assert_match(FsState::new(vec![
+            EntryMock::file("./test", &vec![1, 2, 3]),
+
+            EntryMock::dir("./.ka"),
+            EntryMock::file("./.ka/index", &expected_index),
+            EntryMock::dir("./.ka/files"),
+            EntryMock::file("./.ka/files/test", &expected_file_history),
+        ]))
+    }
+}
\ No newline at end of file