+
+#[cfg(test)]
+mod tests {
+ use std::fs::create_dir_all;
+
+ use super::*;
+ use crate::template::Value;
+
+ use test_utilities::*;
+
+ // Archive template values
+
+ #[test]
+ fn test_creates_context_with_empty_archive_entries() {
+ let context = ArchiveEntry::to_template_context(&[]);
+
+ assert_eq!(context["archive_length"], Value::Unsigned(0));
+ if let Value::Collection(posts) = &context["posts"] {
+ assert!(posts.is_empty());
+ } else {
+ panic!("The posts context was not the right type.");
+ }
+ }
+
+ #[test]
+ fn test_creates_context_with_post_slice() {
+ let archive_entry = ArchiveEntry {
+ id: "you-know-what-is-cool".to_string(),
+ slug: "the-archiving-shelves-with-a-spinning-lever-to-move-them".to_string(),
+ };
+
+ let context = ArchiveEntry::to_template_context(&[archive_entry]);
+
+ assert_eq!(context["archive_length"], Value::Unsigned(1));
+ if let Value::Collection(posts) = &context["posts"] {
+ if let Some(post) = posts.first() {
+ assert_eq!(
+ post["id"],
+ Value::String("you-know-what-is-cool".to_string())
+ );
+ } else {
+ panic!("The template context had no posts");
+ }
+ } else {
+ panic!("The posts context was not the right type.");
+ }
+ }
+
+ #[test]
+ fn test_converts_archive_entry_without_title_to_context() {
+ let archive_entry = ArchiveEntry {
+ id: "they-always-show-them-in-spy-films".to_string(),
+ slug: "or-like-universities".to_string(),
+ };
+
+ let context = archive_entry.to_template_value();
+
+ assert_eq!(
+ context["id"],
+ Value::String("they-always-show-them-in-spy-films".to_string())
+ );
+ assert_eq!(
+ context["slug"],
+ Value::String("or-like-universities".to_string())
+ );
+ assert!(!context.contains_key("title"));
+ }
+
+ #[test]
+ fn test_converts_archive_entry_with_title_to_context() {
+ let archive_entry = ArchiveEntry {
+ id: "1736035200000".to_string(),
+ slug: "need-a-warehouse".to_string(),
+ };
+
+ let context = archive_entry.to_template_value();
+
+ assert_eq!(context["id"], Value::String("1736035200000".to_string()));
+ assert_eq!(
+ context["slug"],
+ Value::String("need-a-warehouse".to_string())
+ );
+ assert_eq!(
+ context["title"],
+ Value::String("2025-01-05 need a warehouse".to_string())
+ );
+ }
+
+ // Archive Finder
+ #[test]
+ fn test_finds() {
+ let test_dir = setup_test_dir();
+ let archive_dir = test_dir.join("archive");
+ let template_dir = test_dir.join("templates");
+ let output_dir = test_dir.join("output");
+
+ let first_post_dir = archive_dir.join("1736035200000");
+ create_dir_all(&first_post_dir).expect("Could not first create post test directory");
+ create_test_file(
+ &first_post_dir.join("my-very-cool-post.gmi"),
+ "is full of flowers",
+ );
+
+ let second_post_dir = archive_dir.join("1736121600000");
+ create_dir_all(&second_post_dir).expect("Could not create second post test directory");
+ create_test_file(
+ &second_post_dir.join("my-very-sad-post.gmi"),
+ "is full of regrets",
+ );
+
+ let bad_file_dir = archive_dir.join("1736121600001");
+ create_dir_all(&bad_file_dir).expect("Could not create bad file test directory");
+ create_test_file(&bad_file_dir.join("my-very-bad-file"), "is full of lies");
+
+ create_dir_all(&template_dir).expect("Could not create template test directory");
+ create_dir_all(&output_dir).expect("Could not create output test directory");
+
+ archive(&archive_dir, &template_dir, &output_dir).expect("Archive failed");
+
+ assert_file_contains(
+ &output_dir.join("index.gmi"),
+ "\n=> ./1736035200000/my-very-cool-post.gmi 2025-01-05 my very cool post",
+ );
+ assert_file_contains(
+ &output_dir.join("index.gmi"),
+ "\n=> ./1736121600000/my-very-sad-post.gmi 2025-01-06 my very sad post",
+ );
+ assert_file_contents(
+ &output_dir.join("1736035200000/my-very-cool-post.gmi"),
+ "is full of flowers",
+ );
+ assert_file_contents(
+ &output_dir.join("1736121600000/my-very-sad-post.gmi"),
+ "is full of regrets",
+ );
+ assert!(&output_dir.join("1736121600001/my-very-bad-file").exists());
+ assert_file_does_not_contain(
+ &output_dir.join("index.gmi"),
+ "1736121600001/my-very-bad-file",
+ );
+ }
+}