#[cfg(test)]
mod integration_tests {
use super::super::*;
use tempfile::tempdir;
use std::time::Duration;
#[test]
fn test_notebook_workflow() {
let mut notebook = Notebook::with_title("集成测试笔记本".to_string());
let code_cell = NotebookCell::new_code("x = 10\ny = x + 5".to_string());
let markdown_cell = NotebookCell::new_markdown("# 计算结果\n\n这是一个简单的计算示例。".to_string());
notebook.add_cell(code_cell);
notebook.add_cell(markdown_cell);
assert_eq!(notebook.cell_count(), 2);
let stats = notebook.statistics();
assert_eq!(stats.total_cells, 2);
assert_eq!(stats.code_cells, 1);
assert_eq!(stats.markdown_cells, 1);
}
#[test]
fn test_execution_engine_integration() {
let mut engine = ExecutionEngine::new();
let mut cell = NotebookCell::new_code("2 + 3".to_string());
let result = engine.execute_cell(&mut cell).unwrap();
assert!(result.is_success());
assert!(cell.get_output().is_some());
let stats = engine.get_statistics();
assert_eq!(stats.total_executions, 1);
assert_eq!(stats.successful_executions, 1);
}
#[test]
fn test_scope_management() {
let mut manager = ScopeManager::new();
let cell_id = uuid::Uuid::new_v4();
manager.define_global_variable(
"pi".to_string(),
crate::core::Expression::Number(crate::core::Number::from(3.14159)),
cell_id
).unwrap();
manager.create_cell_scope(cell_id, "测试单元格".to_string());
manager.set_current_scope(Some(cell_id));
manager.define_variable(
"radius".to_string(),
crate::core::Expression::Number(crate::core::Number::from(5)),
cell_id
).unwrap();
assert!(manager.has_variable("pi"));
assert!(manager.has_variable("radius"));
let vars = manager.export_for_computation();
assert!(vars.contains_key("pi"));
assert!(vars.contains_key("radius"));
}
#[test]
fn test_file_format_integration() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("integration_test.ynb");
let mut notebook = Notebook::with_title("文件格式测试".to_string());
notebook.metadata.author = Some("测试作者".to_string());
notebook.metadata.description = Some("这是一个集成测试笔记本".to_string());
notebook.add_cell(crate::NotebookCell::new_markdown("# 介绍\n\n这是测试笔记本。".to_string()));
notebook.add_cell(crate::NotebookCell::new_code("result = 42".to_string()));
notebook.add_cell(crate::NotebookCell::new_text("这是一个文本单元格。".to_string()));
NotebookSerializer::save_to_file(&mut notebook, &file_path).unwrap();
assert!(file_path.exists());
assert!(!notebook.needs_save());
let loaded_notebook = NotebookDeserializer::load_from_file(&file_path).unwrap();
assert_eq!(loaded_notebook.metadata.title, "文件格式测试");
assert_eq!(loaded_notebook.metadata.author, Some("测试作者".to_string()));
assert_eq!(loaded_notebook.cell_count(), 3);
let file_info = NotebookFormat::get_file_info(&file_path).unwrap();
assert_eq!(file_info.title, "文件格式测试");
assert_eq!(file_info.cell_count, 3);
assert_eq!(file_info.code_cells, 1);
}
#[test]
fn test_export_integration() {
let mut notebook = Notebook::with_title("导出测试".to_string());
notebook.add_cell(crate::NotebookCell::new_markdown("# 导出测试\n\n这是导出功能的测试。".to_string()));
notebook.add_cell(crate::NotebookCell::new_code("x = 1 + 2".to_string()));
let exporter = NotebookExporter::new();
let markdown = exporter.export_to_string(¬ebook, ExportFormat::Markdown).unwrap();
assert!(markdown.contains("# 导出测试"));
assert!(markdown.contains("x = 1 + 2"));
let html = exporter.export_to_string(¬ebook, ExportFormat::Html).unwrap();
assert!(html.contains("<!DOCTYPE html>"));
assert!(html.contains("导出测试"));
let code = exporter.export_to_string(¬ebook, ExportFormat::Code).unwrap();
assert!(code.contains("x = 1 + 2"));
let dir = tempdir().unwrap();
let html_path = dir.path().join("export_test.html");
exporter.export_to_file(¬ebook, &html_path, ExportFormat::Html).unwrap();
assert!(html_path.exists());
}
#[test]
fn test_notebook_manager_integration() {
let mut manager = NotebookManager::new();
let id1 = manager.create_notebook(Some("笔记本1".to_string()));
let id2 = manager.create_notebook(Some("笔记本2".to_string()));
assert_eq!(manager.notebook_count(), 2);
assert_eq!(manager.get_active_notebook().unwrap().metadata.title, "笔记本2");
manager.set_active_notebook(&id1).unwrap();
assert_eq!(manager.get_active_notebook().unwrap().metadata.title, "笔记本1");
if let Some(notebook) = manager.get_active_notebook_mut() {
notebook.add_cell(crate::NotebookCell::new_code("test = 123".to_string()));
}
assert!(manager.has_unsaved_notebooks());
let unsaved = manager.get_unsaved_notebooks();
assert_eq!(unsaved.len(), 1);
let closed = manager.close_notebook(&id1).unwrap();
assert_eq!(closed.metadata.title, "笔记本1");
assert_eq!(manager.notebook_count(), 1);
}
#[test]
fn test_execution_queue_integration() {
let mut engine = ExecutionEngine::new();
let cell1_id = uuid::Uuid::new_v4();
let cell2_id = uuid::Uuid::new_v4();
let cell3_id = uuid::Uuid::new_v4();
engine.queue_cell(cell1_id, vec![]);
engine.queue_cell(cell2_id, vec![cell1_id]);
engine.queue_cell(cell3_id, vec![]);
let cells = vec![
crate::NotebookCell::new_code("x = 10".to_string()),
crate::NotebookCell::new_code("y = x + 5".to_string()),
crate::NotebookCell::new_code("z = 20".to_string()),
];
let dependencies = engine.analyze_dependencies(&cells);
assert!(dependencies.get(&cells[1].id).unwrap().contains(&cells[0].id));
assert!(dependencies.get(&cells[2].id).unwrap().is_empty());
}
#[test]
fn test_template_and_backup() {
let template = NotebookFormat::create_template("模板测试");
assert_eq!(template.metadata.title, "模板测试");
assert_eq!(template.cell_count(), 2);
let dir = tempdir().unwrap();
let original_path = dir.path().join("original.ynb");
let backup_path = dir.path().join("original.ynb.backup");
let notebook = Notebook::with_title("备份测试".to_string());
NotebookSerializer::create_backup(¬ebook, &original_path).unwrap();
assert!(backup_path.exists());
let backup_notebook = NotebookDeserializer::load_from_file(&backup_path).unwrap();
assert_eq!(backup_notebook.metadata.title, "备份测试");
}
#[test]
fn test_search_and_statistics() {
let mut notebook = Notebook::with_title("搜索测试".to_string());
notebook.add_cell(crate::NotebookCell::new_code("x = 42".to_string()));
notebook.add_cell(crate::NotebookCell::new_text("这里包含数字 42".to_string()));
notebook.add_cell(crate::NotebookCell::new_markdown("# 标题\n\n没有特殊内容".to_string()));
notebook.add_cell(crate::NotebookCell::new_code("y = x * 2".to_string()));
let results = notebook.search("42", true);
assert_eq!(results.len(), 2);
let results = notebook.search("x", true);
assert_eq!(results.len(), 2);
let stats = notebook.statistics();
assert_eq!(stats.total_cells, 4);
assert_eq!(stats.code_cells, 2);
assert_eq!(stats.text_cells, 1);
assert_eq!(stats.markdown_cells, 1);
assert!(stats.total_characters > 0);
assert_eq!(stats.execution_rate(), 0.0); assert!(stats.average_cell_length() > 0.0);
}
}