use mnote_editor_core::{BlockType, DocumentBlock, DocumentModel, VisibilitySnapshot};

#[test]
fn document_model_preserves_parent_structure_and_outline_visibility() {
    let document = DocumentModel::new(vec![
        DocumentBlock::new("heading_root", BlockType::Heading)
            .with_heading_level(1)
            .with_text("根标题")
            .with_collapsed(true),
        DocumentBlock::new("paragraph_hidden", BlockType::Paragraph)
            .with_parent("heading_root", 1)
            .with_text("折叠后应隐藏"),
        DocumentBlock::new("heading_visible", BlockType::Heading)
            .with_heading_level(2)
            .with_text("次级标题"),
        DocumentBlock::new("todo_visible", BlockType::Todo)
            .with_parent("heading_visible", 1)
            .with_text("仍然可见"),
    ]);

    assert_eq!(document.blocks().len(), 4);
    assert_eq!(
        document
            .children_of(Some("heading_root"))
            .into_iter()
            .map(|block| block.id.as_str())
            .collect::<Vec<_>>(),
        vec!["paragraph_hidden"]
    );

    let projection = VisibilitySnapshot::derive(&document);

    assert_eq!(
        projection.visible_block_ids,
        vec![
            "heading_root".to_string(),
            "heading_visible".to_string(),
            "todo_visible".to_string(),
        ]
    );
    assert_eq!(projection.outline.len(), 2);
    assert_eq!(projection.outline[0].block_id, "heading_root");
    assert_eq!(projection.outline[0].title, "根标题");
    assert_eq!(projection.outline[1].block_id, "heading_visible");
    assert_eq!(projection.outline[1].level, 2);
}
