# Path: metatlas/tools/cheminfo.py
# def get_parent_mass(precursor_mz: float, adduct: str) -> float:
# def get_precursor_mz(parent_mass: float, adduct: str) -> float:
# def is_positive_mode(adduct: str) -> bool:
# def is_valid_inchi_pair(test_inchi: str, test_inchi_key: str) -> bool:
# def is_valid_inchi_smiles_pair(test_inchi: str, test_smiles: str) -> bool:
# def inchi_to_smiles(inchi: str) -> str:
# def smiles_to_inchi(smiles: str) -> str:
# def inchi_or_smiles_to_molecule(molecule_id: str) -> Chem.rdchem.Mol:
# def inchi_or_smiles_to_inchi(molecule_id: str) -> str:
# def inchi_or_smiles_to_smiles(molecule_id: str) -> str:
# def normalize_molecule(mol: Chem.rdchem.Mol) -> Chem.rdchem.Mol:
# def are_equal(molecule1: Chem.rdchem.Mol, molecule2: Chem.rdchem.Mol) -> bool:
# def is_synonym(name: str, synonym_string: str) -> bool:
# def valid_adduct(value: str) -> bool:
# def mol_to_image(mol: Chem.rdchem.Mol, **kwargs) -> widgets.Image:


import matchms
from rdkit import Chem
from metatlas.tools import cheminfo

def test_get_parent_mass01():
    adducts = matchms.importing.load_adducts_dict()
    original_parent = 100
    for name in adducts:
        pre = cheminfo.get_precursor_mz(original_parent, name)
        parent = cheminfo.get_parent_mass(pre, name)
        assert abs(original_parent - parent) < 1e-7


def test_is_valid_inchi_pair():
    assert cheminfo.is_valid_inchi_pair(INCHI, INCHI_KEY)
    assert not cheminfo.is_valid_inchi_pair("", INCHI_KEY)
    assert not cheminfo.is_valid_inchi_pair(INCHI, "")
    assert not cheminfo.is_valid_inchi_pair(f"{INCHI}foobar!", INCHI_KEY)
    assert not cheminfo.is_valid_inchi_pair(INCHI, f"{INCHI_KEY}foobar!")


def test_is_valid_inchi_smiles_pair():
    assert cheminfo.is_valid_inchi_smiles_pair(INCHI, SMILES)
    assert not cheminfo.is_valid_inchi_smiles_pair("", SMILES)
    assert not cheminfo.is_valid_inchi_smiles_pair(INCHI, "")
    assert not cheminfo.is_valid_inchi_smiles_pair(f"{INCHI}foobar!", SMILES)
    assert not cheminfo.is_valid_inchi_smiles_pair(INCHI, f"{SMILES}foobar!")


def test_are_equal():
    mol1 = Chem.inchi.MolFromInchi(INCHI)
    mol2 = Chem.inchi.MolFromInchi("InChI=1S/H2O/h1H2")
    assert cheminfo.are_equal(mol1, mol1)

# Path: metatlas/tools/cheminfo.py
# def get_parent_mass(precursor_mz: float, adduct: str) -> float:
# def get_precursor_mz(parent_mass: float, adduct: str) -> float:
# def is_positive_mode(adduct: str) -> bool:
# def is_valid_inchi_pair(test_inchi: str, test_inchi_key: str) -> bool:
# def is_valid_inchi_smiles_pair(test_inchi: str, test_smiles: str) -> bool:
# def inchi_to_smiles(inchi: str) -> str:
# def smiles_to_inchi(smiles: str) -> str:
# def inchi_or_smiles_to_molecule(molecule_id: str) -> Chem.rdchem.Mol:
# def inchi_or_smiles_to_inchi(molecule_id: str) -> str:
# def inchi_or_smiles_to_smiles(molecule_id: str) -> str:
# def normalize_molecule(mol: Chem.rdchem.Mol) -> Chem.rdchem.Mol:
# def are_equal(molecule1: Chem.rdchem.Mol, molecule2: Chem.rdchem.Mol) -> bool:
# def is_synonym(name: str, synonym_string: str) -> bool:
# def valid_adduct(value: str) -> bool:
# def mol_to_image(mol: Chem.rdchem.Mol, **kwargs) -> widgets.Image:


# Path: tests/unit/test_cheminfo.py
import matchms
from rdkit import Chem
from metatlas.tools import cheminfo


def test_get_parent_mass01():
    adducts = matchms.importing.load_adducts_dict()
    original_parent = 100
    for name in adducts:
        pre = cheminfo.get_precursor_mz(original_parent, name)
        parent = cheminfo.get_parent_mass(pre, name)
        assert abs(original_parent - parent) < 1e-7


def test_is_valid_inchi_pair():
    assert cheminfo.is_valid_inchi_pair(INCHI, INCHI_KEY)
    assert not cheminfo.is_valid_inchi_pair("", INCHI_KEY)
    assert not cheminfo.is_valid_inchi_pair(INCHI, "")
    assert not cheminfo.is_valid_inchi_pair(f"{INCHI}foobar!", INCHI_KEY)
    assert not cheminfo.is_valid_inchi_pair(INCHI, f"{INCHI_KEY}foobar!")


def test_is_valid_inchi_smiles_pair():
    assert cheminfo.is_valid_inchi_smiles_pair(INCHI, SMILES)
    assert not cheminfo.is_valid_inchi_smiles_pair("", SMILES)
    assert not cheminfo.is_valid_inchi_smiles_pair(INCHI, "")
    assert not cheminfo.is_valid_inchi_smiles_pair(f"{INCHI}foobar!", SMILES)
    assert not cheminfo.is_valid_inchi_smiles_pair(INCHI, f"{SMILES}foobar!")


def test_are_equal():
    mol1 = Chem.inchi.MolFromInchi(INCHI)
    mol2 = Chem.inchi.MolFromInchi("InChI=1S/H2O/h1H2")
    assert cheminfo.are_equal(mol1, mol1)

    assert cheminfo.are_equal(mol2, mol2)