use std::fs::{self, File};
use std::io::Write;
use crate::{evaluate_file, Session};

#[test]
fn file_test() {
    fs::create_dir_all("./test_files").unwrap();

    let mut f = File::create("./test_files/test.sjfl").unwrap();
    f.write_all(b"{[1, 2, 3, 4]: \"the content doesn't matter that much...\"}").unwrap();

    let mut session = Session::new_dir("./test_files").unwrap();

    let v1 = evaluate_file("test", &mut session).unwrap();
    fs::remove_file("./test_files/test.bsjfl").unwrap();
    let v2 = evaluate_file("test", &mut session).unwrap();
    let v3 = evaluate_file("test", &mut session).unwrap();

    fs::remove_file("./test_files/test.sjfl").unwrap();

    let mut f = File::create("./test_files/test.sjfl").unwrap();
    f.write_all(b"{[1, 2, 3, 5]: \"the content doesn't matter that much...\"}").unwrap();

    let mut session = Session::new_dir("./test_files").unwrap();
    let v4 = evaluate_file("test", &mut session).unwrap();
    fs::remove_file("./test_files/test.bsjfl").unwrap();
    let _ = evaluate_file("test", &mut session).unwrap();
    let v5 = evaluate_file("test", &mut session).unwrap();

    assert_eq!(v1, v2);
    assert_eq!(v2, v3);
    assert!(v1 != v4);
    assert_eq!(v4, v5);

    fs::remove_file("./test_files/test.sjfl").unwrap();
    fs::remove_file("./test_files/test.bsjfl").unwrap();

    fs::remove_dir("./test_files").unwrap();
}