From b3a7fe0414ae30a973149d76b061e0fb20aa63de Mon Sep 17 00:00:00 2001 From: wangdl Date: Sat, 30 May 2026 21:05:34 +0800 Subject: [PATCH] test: ReadingPosition serde round-trip tests for all 6 variants --- crates/zx_document_core/src/progress.rs | 72 +++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/crates/zx_document_core/src/progress.rs b/crates/zx_document_core/src/progress.rs index bcea440..ee3c0f2 100644 --- a/crates/zx_document_core/src/progress.rs +++ b/crates/zx_document_core/src/progress.rs @@ -28,3 +28,75 @@ pub enum ReadingPosition { }, Unknown, } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_markdown_serde() { + let pos = ReadingPosition::Markdown { + block_id: "h1".into(), + scroll_progress: 0.5, + }; + let json = serde_json::to_string(&pos).unwrap(); + assert!(json.contains("\"type\":\"Markdown\"")); + let back: ReadingPosition = serde_json::from_str(&json).unwrap(); + assert_eq!(back, pos); + } + + #[test] + fn test_text_serde() { + let pos = ReadingPosition::Text { + line_number: 42, + scroll_progress: 0.3, + }; + let json = serde_json::to_string(&pos).unwrap(); + let back: ReadingPosition = serde_json::from_str(&json).unwrap(); + assert_eq!(back, pos); + } + + #[test] + fn test_pdf_serde() { + let pos = ReadingPosition::Pdf { + page_number: 7, + page_progress: 0.8, + overall_progress: 0.35, + }; + let json = serde_json::to_string(&pos).unwrap(); + let back: ReadingPosition = serde_json::from_str(&json).unwrap(); + assert_eq!(back, pos); + } + + #[test] + fn test_image_serde() { + let pos = ReadingPosition::Image { + zoom_scale: 1.5, + offset_x: 100.0, + offset_y: 200.0, + }; + let json = serde_json::to_string(&pos).unwrap(); + let back: ReadingPosition = serde_json::from_str(&json).unwrap(); + assert_eq!(back, pos); + } + + #[test] + fn test_epub_serde() { + let pos = ReadingPosition::Epub { + chapter_id: "ch3".into(), + chapter_progress: 0.6, + overall_progress: 0.25, + }; + let json = serde_json::to_string(&pos).unwrap(); + let back: ReadingPosition = serde_json::from_str(&json).unwrap(); + assert_eq!(back, pos); + } + + #[test] + fn test_unknown_serde() { + let pos = ReadingPosition::Unknown; + let json = serde_json::to_string(&pos).unwrap(); + let back: ReadingPosition = serde_json::from_str(&json).unwrap(); + assert_eq!(back, pos); + } +}