context("Start() path_glob_permissive check")

test_that("1. expid/member/version", {

years  <- paste0(c(1960:1961), '01-', c(1960:1961), '12')

# from the end, each *: 1. member 2. version 3. member
repos <- paste0('/esarchive/exp/ecearth/$expid$/diags/CMIP/EC-Earth-Consortium/',
                'EC-Earth3/historical/*/Omon/$var$/gn/v*/',
                '$var$_Omon_EC-Earth3_historical_*_gn_$year$.nc')

data <- Start(dat = repos,
              var = 'tosmean',
              expid =  c('a1st', 'a1sx'),
              year = years,
              time = indices(1),
              region = indices(1),
              path_glob_permissive = 6, #TRUE,
              return_vars = list(time = NULL, region = NULL),
              retrieve = T)


  expect_equal(
  dim(data),
  c(dat = 1, var = 1, expid = 2, year = 2, time = 1, region = 1)
  )
  expect_equal(
  dim(attr(data, 'Files')),
  c(dat = 1, var = 1, expid = 2, year = 2)
  )
  expect_equal(
  attr(data, 'Files'),
  array(c("/esarchive/exp/ecearth/a1st/diags/CMIP/EC-Earth-Consortium/EC-Earth3/historical/r7i1p1f1/Omon/tosmean/gn/v20190302/tosmean_Omon_EC-Earth3_historical_r7i1p1f1_gn_196001-196012.nc",
           "/esarchive/exp/ecearth/a1sx/diags/CMIP/EC-Earth-Consortium/EC-Earth3/historical/r10i1p1f1/Omon/tosmean/gn/v20190308/tosmean_Omon_EC-Earth3_historical_r10i1p1f1_gn_196001-196012.nc",
           "/esarchive/exp/ecearth/a1st/diags/CMIP/EC-Earth-Consortium/EC-Earth3/historical/r7i1p1f1/Omon/tosmean/gn/v20190302/tosmean_Omon_EC-Earth3_historical_r7i1p1f1_gn_196101-196112.nc",
           "/esarchive/exp/ecearth/a1sx/diags/CMIP/EC-Earth-Consortium/EC-Earth3/historical/r10i1p1f1/Omon/tosmean/gn/v20190308/tosmean_Omon_EC-Earth3_historical_r10i1p1f1_gn_196101-196112.nc"),
        dim = c(dat = 1, var = 1, expid = 2, year = 2))
  )
  expect_equal(
  data[1, 1, , , 1, 1],
  matrix(c(18.60422, 17.13862, 18.52348, 17.21780), 2, 2), 
  tolerance = 0.0001
  )


# from the end, each *: 1. member 2. version
repos <- paste0('/esarchive/exp/ecearth/$expid$/diags/CMIP/EC-Earth-Consortium/',
                'EC-Earth3/historical/$member$/Omon/$var$/gn/v*/',
                '$var$_Omon_EC-Earth3_historical_*_gn_$year$.nc')

data <- Start(dat = repos,
              var = 'tosmean',
              expid =  c('a1st', 'a1sx'),
              year = years,
              member = indices(1),
              member_depends = 'expid',
              time = indices(1),
              region = indices(1),
              path_glob_permissive = 2, #TRUE,
              return_vars = list(time = NULL, region = NULL),
              retrieve = T)


  expect_equal(
  dim(data),
  c(dat = 1, var = 1, expid = 2, year = 2, member = 1, time = 1, region = 1)
  )
  expect_equal(
  dim(attr(data, 'Files')),
  c(dat = 1, var = 1, expid = 2, year = 2, member = 1)
  )
  expect_equal(
  attr(data, 'Files'),
  array(c("/esarchive/exp/ecearth/a1st/diags/CMIP/EC-Earth-Consortium/EC-Earth3/historical/r7i1p1f1/Omon/tosmean/gn/v20190302/tosmean_Omon_EC-Earth3_historical_r7i1p1f1_gn_196001-196012.nc",
           "/esarchive/exp/ecearth/a1sx/diags/CMIP/EC-Earth-Consortium/EC-Earth3/historical/r10i1p1f1/Omon/tosmean/gn/v20190308/tosmean_Omon_EC-Earth3_historical_r10i1p1f1_gn_196001-196012.nc",
           "/esarchive/exp/ecearth/a1st/diags/CMIP/EC-Earth-Consortium/EC-Earth3/historical/r7i1p1f1/Omon/tosmean/gn/v20190302/tosmean_Omon_EC-Earth3_historical_r7i1p1f1_gn_196101-196112.nc",
           "/esarchive/exp/ecearth/a1sx/diags/CMIP/EC-Earth-Consortium/EC-Earth3/historical/r10i1p1f1/Omon/tosmean/gn/v20190308/tosmean_Omon_EC-Earth3_historical_r10i1p1f1_gn_196101-196112.nc"),
        dim = c(dat = 1, var = 1, expid = 2, year = 2, member = 1))
  )
  expect_equal(
  data[1, 1, , , 1, 1, 1],
  matrix(c(18.60422, 17.13862, 18.52348, 17.21780), 2, 2),
  tolerance = 0.0001
  )
  expect_equal(
  attr(data, 'FileSelectors')$dat1$member,
  list(a1st = 'r7i1p1f1', a1sx = 'r10i1p1f1')
  )

})
