from mnemosynecore import secrets


def test_resolve_secret_prefers_test_file(monkeypatch):
    monkeypatch.setattr(secrets, "get_secret_test", lambda conn_id, dir_path=None: {"from": "test"})
    monkeypatch.setattr(secrets, "get_secret", lambda conn_id: {"from": "prod"})
    assert secrets.resolve_secret("CID") == {"from": "test"}


def test_resolve_secret_fallback_to_prod(monkeypatch):
    def fail(*_args, **_kwargs):
        raise FileNotFoundError()

    monkeypatch.setattr(secrets, "get_secret_test", fail)
    monkeypatch.setattr(secrets, "get_secret", lambda conn_id: {"from": "prod"})
    assert secrets.resolve_secret("CID") == {"from": "prod"}


def test_resolve_secret_test_supports_creds_path(monkeypatch):
    captured = {}

    def fake_get_secret_test(conn_id, dir_path=None, creds_path=None):
        captured["creds_path"] = creds_path
        return {"from": "test"}

    monkeypatch.setattr(secrets, "get_secret_test", fake_get_secret_test)
    assert secrets.resolve_secret_test("CID", creds_path="/tmp/cid.json") == {"from": "test"}
    assert captured["creds_path"] == "/tmp/cid.json"
