//! Byte-equivalence test for the React target against the JS baseline.

use std::path::PathBuf;

use mizan_codegen::config::{Config, SourceConfig};
use mizan_codegen::emit::CodegenTarget;
use mizan_codegen::emit::react::ReactAdapter;
use mizan_codegen::fetch::parse_ir_from_str;


fn load_ir() -> mizan_codegen::ir::MizanIR {
    let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/afi_ir.kdl");
    parse_ir_from_str(&std::fs::read_to_string(&path).unwrap()).unwrap()
}


fn fixture_config() -> Config {
    Config {
        project_id: None,
        output: PathBuf::from("/tmp"),
        targets: vec!["react".to_string()],
        source: SourceConfig { fastapi: None, django: None, rust: None, script: None },
        rust_kernel: None,
        rust_crate_name: None,
    }
}


#[test]
fn react_target_byte_match() {
    let ir = load_ir();
    let files = ReactAdapter.emit(&ir, &fixture_config());
    assert_eq!(files.len(), 1);
    let actual = &files[0].content;

    let expected_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures/baselines/react/react.tsx");
    let expected = std::fs::read_to_string(&expected_path).unwrap();

    if *actual != expected {
        for (lineno, (a, b)) in actual.lines().zip(expected.lines()).enumerate() {
            if a != b {
                panic!(
                    "react.tsx diverges at line {}:\n  expected: {b:?}\n  actual:   {a:?}",
                    lineno + 1,
                );
            }
        }
        panic!(
            "react.tsx diverges in length: actual={} expected={}",
            actual.len(), expected.len(),
        );
    }
}
