use anyhow::Result;
use log::info;
use notion2md::notion_client::Client;
use notion2md::builder::NotionToMarkdownBuilder;
use std::fs;
use std::path::PathBuf;
use std::time::Instant;
const TEST_PAGE_ID: &str = "1aeb266e0c708060a6fec6eb458e1379";
const TEST_OUTPUT_PAGE_TITLE: &str = "test";
const TEST_OUTPUT_DIR: &str = "target/test_output";
#[tokio::test]
async fn test_page_conversion() -> Result<()> {
env_logger::builder()
.filter_level(log::LevelFilter::Info)
.is_test(true)
.init();
let start_total = Instant::now();
info!("テスト開始");
dotenv::dotenv().ok();
let notion_token = std::env::var("NOTION_TOKEN").expect("NOTION_TOKEN must be set");
let start_setup = Instant::now();
let obsidian_dir = PathBuf::from(TEST_OUTPUT_DIR);
std::fs::create_dir_all(&obsidian_dir).expect("Failed to create test output directory");
let notion_client = Client::new(notion_token.clone(), None)?;
let converter =
NotionToMarkdownBuilder::new(notion_client).build();
info!("セットアップ完了: {:?}", start_setup.elapsed());
let start_conversion = Instant::now();
let markdown_text = converter.convert_page(TEST_PAGE_ID).await?;
info!("ページ変換完了: {:?}", start_conversion.elapsed());
let output_file_path = obsidian_dir.join(format!("{}.md", TEST_OUTPUT_PAGE_TITLE));
fs::write(&output_file_path, markdown_text).expect("Failed to write test_output.md");
info!(
"Markdownファイルの保存完了: {:?}",
start_conversion.elapsed()
);
let expected_content =
fs::read_to_string("tests/cases/test_expected.md").expect("Failed to read test.md");
let converted_content =
fs::read_to_string(format!("{}/{}.md", TEST_OUTPUT_DIR, TEST_OUTPUT_PAGE_TITLE))
.expect("Failed to read test_output.md");
let normalized_converted = converted_content.replace("\r\n", "\n");
let normalized_expected = expected_content.replace("\r\n", "\n");
assert_eq!(
normalized_converted.trim(),
normalized_expected.trim(),
"Converted content does not match expected content"
);
info!("テスト合計実行時間: {:?}", start_total.elapsed());
Ok(())
}