

import os
from pathlib import Path

import verl.utils.fs as fs

def test_record_and_check_directory_structure(tmp_path):

    test_dir = tmp_path / "test_dir"
    test_dir.mkdir()
    (test_dir / "file1.txt").write_text("test")
    (test_dir / "subdir").mkdir()
    (test_dir / "subdir" / "file2.txt").write_text("test")

    record_file = fs._record_directory_structure(test_dir)

    assert os.path.exists(record_file)

    assert fs._check_directory_structure(test_dir, record_file) is True

    (test_dir / "new_file.txt").write_text("test")
    assert fs._check_directory_structure(test_dir, record_file) is False

def test_copy_from_hdfs_with_mocks(tmp_path, monkeypatch):

    monkeypatch.setattr(fs, "is_non_local", lambda path: True)

    def fake_copy(src: str, dst: str, *args, **kwargs):
        dst_path = Path(dst)
        dst_path.parent.mkdir(parents=True, exist_ok=True)
        dst_path.write_bytes(b"")

    monkeypatch.setattr(fs, "copy", fake_copy)

    test_cache = tmp_path / "cache"
    hdfs_path = "hdfs://test/path/file.txt"

    local_path = fs.copy_to_local(hdfs_path, cache_dir=test_cache)
    expected_path = os.path.join(test_cache, fs.md5_encode(hdfs_path), os.path.basename(hdfs_path))
    assert local_path == expected_path
    assert os.path.exists(local_path)

def test_always_recopy_flag(tmp_path, monkeypatch):

    monkeypatch.setattr(fs, "is_non_local", lambda path: True)

    copy_call_count = 0

    def fake_copy(src: str, dst: str, *args, **kwargs):
        nonlocal copy_call_count
        copy_call_count += 1
        dst_path = Path(dst)
        dst_path.parent.mkdir(parents=True, exist_ok=True)
        dst_path.write_bytes(b"")

    monkeypatch.setattr(fs, "copy", fake_copy)

    test_cache = tmp_path / "cache"
    hdfs_path = "hdfs://test/path/file.txt"

    fs.copy_to_local(hdfs_path, cache_dir=test_cache)
    assert copy_call_count == 1

    fs.copy_to_local(hdfs_path, cache_dir=test_cache, always_recopy=True)
    assert copy_call_count == 2

    fs.copy_to_local(hdfs_path, cache_dir=test_cache)
    assert copy_call_count == 2
