import pytest
import importlib
import numpy as np
import xarray as xr
code = importlib.import_module('preproc.nc2timeseries_interp')


def counter(model):
    import os
    import fnmatch
    dir_path = r'./tmp/{}-feather'.format(model)
    count = len(fnmatch.filter(os.listdir(dir_path), '*.*'))
    return count

# def test_convert2timeseries():
#     assert code.convert2timeseries('test', fmt='feather', months=None) == None
#     assert counter('test') > 0

def test_preprocess():
    # Create sample dataset with 10 timesteps
    data = xr.DataArray(
        np.random.randn(10, 3, 3),
        dims=("time", "lat", "lon"),
        coords={"time": range(10), "lat": [10, 20, 30], "lon": [-120, -110, -100]},
    )
    ds = xr.Dataset({"var": data})

    # Test that preprocess function keeps only first 5 timesteps
    preprocessed_ds = code.preprocess(ds, n=5)
    assert preprocessed_ds.dims == {"time": 5, "lat": 3, "lon": 3}
    assert preprocessed_ds.coords["time"].values.tolist() == [0, 1, 2, 3, 4]
    # Test that preprocess function keeps only first 3 timesteps
    preprocessed_ds = code.preprocess(ds, n=3)
    assert preprocessed_ds.dims == {"time": 3, "lat": 3, "lon": 3}
    assert preprocessed_ds.coords["time"].values.tolist() == [0, 1, 2]

