use super::common::assert_output;
use crate::engine::Flow;
use crate::vis_js;
use std::fs;
use std::path::PathBuf;
use tower_lsp::lsp_types::Url;
#[test]
fn generate_blog_architecture_json() -> std::io::Result<()> {
let input_path = "../examples/dsbg.tect";
let content = fs::read_to_string(input_path).expect("Failed to read dsbg.tect");
let path = fs::canonicalize(PathBuf::from(input_path))?;
let uri = Url::from_file_path(path).unwrap();
let mut workspace = crate::analyzer::Workspace::new();
workspace.analyze(uri, Some(content.clone()));
let structure = &workspace.structure;
let mut flow = Flow::new(true);
let graph = flow.simulate(structure);
let formatter = serde_json::ser::PrettyFormatter::with_indent(b" ");
let mut arch_buf = Vec::new();
let mut arch_ser = serde_json::Serializer::with_formatter(&mut arch_buf, formatter.clone());
serde::Serialize::serialize(&graph, &mut arch_ser).unwrap();
let arch_json = String::from_utf8(arch_buf).expect("Generated JSON was not valid UTF-8");
let mut functions: Vec<_> = structure.catalog.values().collect();
functions.sort_by_key(|f| f.uid);
let mut func_buf = Vec::new();
let mut func_ser = serde_json::Serializer::with_formatter(&mut func_buf, formatter);
serde::Serialize::serialize(&functions, &mut func_ser).unwrap();
let func_json = String::from_utf8(func_buf).expect("Generated JSON was not valid UTF-8");
let html_content = vis_js::generate_interactive_html(&graph);
let output_dir = "../examples/test_outputs";
fs::create_dir_all(output_dir)?;
fs::write(format!("{}/architecture.json", output_dir), &arch_json)?;
fs::write(format!("{}/functions.json", output_dir), &func_json)?;
fs::write(format!("{}/architecture.html", output_dir), &html_content)?;
assert_output("../examples/expected_outputs/architecture.json", arch_json);
assert_output("../examples/expected_outputs/functions.json", func_json);
Ok(())
}