import json
from pathlib import Path

import magic
from fastapi.testclient import TestClient
from mocka.main import get_application
from mocka.configuration import Configuration, Server
from mocka.artefact import Artefact

client = TestClient(get_application(Configuration(Server("localhost", 8585))))


def test_post_notch():
    notch_model_file_path = Path(__file__).parent / Path("notch/notchFilter.mo")
    mime = magic.Magic(mime=True).from_file(notch_model_file_path)
    model_artefact = Artefact("inline", notch_model_file_path.read_text(), notch_model_file_path.name, mime)

    parameters = {
        "frequency": 500,
        "amplitude": 1,
        "sampling_ratio": 100,
        "tolerance": 1e-9,
        "data_cycles": 2,
        "output_stabilisation_time": 1.0,
        "output": ["time", "V_out.v","V_in.v"],
    }
    parameters_json = json.dumps(parameters, indent=4)
    parameters_artefact = Artefact("inline", parameters_json, "parameters.json", "application/json")
    mock_input = {
        "ctrl": "cin",
        "input": {
            "model": model_artefact.as_dict(),
            "parameters": parameters_artefact.as_dict(),
        }
    }
    response = client.post("/notch/simulation/", json=mock_input)
    assert response.status_code == 200
    # orig = base64.b64decode(rail_image_data.get("content"))
    # with temp_image_loc.open("wb") as f_output:
    #     f_output.write(orig)
    # annotated_image_artefact, data_artefact = rail_finder_algo_one(str(temp_image_loc))

    # data_artefact = Artefact("inline", "", "", "text/plain")
    # assert response.json() ==  {
    #     "ctrl": "ok",
    #     "output": {
    #         "data": data_artefact.as_dict(),
    #     }
    # }
