import json


def test_camembert_load_is_idempotent_and_reuses_process_session(tmp_path, monkeypatch):
    import camembert_ner_manager as module

    model_dir = tmp_path / "camembert-bio-deid" / "onnx"
    model_dir.mkdir(parents=True)
    (model_dir / "model.onnx").write_bytes(b"fake")
    (model_dir / "config.json").write_text(
        json.dumps({"id2label": {"0": "O", "1": "B-PER"}}),
        encoding="utf-8",
    )
    (model_dir.parent / "VERSION.json").write_text(
        json.dumps({"current_version": "v-test", "versions": {"v-test": {"f1": 1, "recall": 1}}}),
        encoding="utf-8",
    )

    created_sessions = []

    class FakeSessionOptions:
        inter_op_num_threads = 0
        intra_op_num_threads = 0

    class FakeOrt:
        SessionOptions = FakeSessionOptions

        @staticmethod
        def InferenceSession(path, sess_options=None, providers=None):
            session = {"path": path, "providers": providers}
            created_sessions.append(session)
            return session

    class FakeTokenizer:
        @staticmethod
        def from_pretrained(path):
            return {"tokenizer_path": path}

    monkeypatch.setattr(module, "_ORT_AVAILABLE", True)
    monkeypatch.setattr(module, "_TOKENIZERS_AVAILABLE", True)
    monkeypatch.setattr(module, "ort", FakeOrt)
    monkeypatch.setattr(module, "AutoTokenizer", FakeTokenizer)
    module._PROCESS_CACHE.clear()

    first = module.CamembertNerManager(model_dir)
    first.load()
    first.load()

    second = module.CamembertNerManager(model_dir)
    second.load()

    assert len(created_sessions) == 1
    assert first.is_loaded()
    assert second.is_loaded()
    assert first._session is second._session
