##############################################
# dat1
dat1 <- NULL
dat1$data <- array(rnorm(5 * 2 * 61 * 1),
                   c(member = 5, sdate = 2, time = 61, lon = 1)) 
Dates1 <- c(seq(as.Date("01-05-2000", format = "%d-%m-%Y"), 
               as.Date("30-06-2000", format = "%d-%m-%Y"), by = 'day'),
           seq(as.Date("01-05-2001", format = "%d-%m-%Y"), 
               as.Date("30-06-2001", format = "%d-%m-%Y"), by = 'day'))
dat1$attrs$Dates <- Dates1
class(dat1) <- 's2dv_cube'
# dat2
Dates2 <- Dates1
dim(Dates2) <- c(time = 61, sdate = 2)
##############################################

test_that("1. Sanity checks", {
  # CST_AbsToProbs
  expect_error(
    CST_AbsToProbs('x'), 
    paste0("Parameter 'data' must be of the class 's2dv_cube'.")
  )
  expect_warning(
    CST_AbsToProbs(dat1, start = list(21, 4), end = list(21, 6)), 
    paste0("Dimensions in 'data' element 'attrs$Dates' are missed and ",
           "all data would be used."), 
    fixed = TRUE
  )
  # AbsToProbs
  expect_error(
    AbsToProbs('x'), 
    "Parameter 'data' must be numeric."
  )
  expect_warning(
    AbsToProbs(1:10, start = list(21, 4), end = list(21, 6)), 
    paste0("Parameter 'data' doesn't have dimension names and all ", 
           "data will be used.")
  )
  expect_error(
    AbsToProbs(dat1$data, start = c(21, 4), end = c(21, 6)), 
    paste0("Parameter 'start' and 'end' must be lists indicating the ",
           "day and the month of the period start and end.")
  )
  expect_warning(
    AbsToProbs(dat1$data, start = list(21, 4), end = list(21, 6)), 
    paste0("Parameter 'dates' is not provided and all data will be used.")
  )
  expect_warning(
    AbsToProbs(data = dat1$data, dates = Dates1, 
               start = list(21, 4), end = list(21, 6)), 
    paste0("Parameter 'dates' doesn't have dimension names and all data will be used.")
  )
  expect_equal(
    dim(AbsToProbs(data = dat1$data, dates = Dates2, 
                   start = list(21, 4), end = list(21, 6))), 
    c(member = 5, sdate = 2, time = 52, lon = 1)
  )
  expect_equal(
    AbsToProbs(1), 
    1
  )
  expect_equal(
    AbsToProbs(1:10), 
    seq(0.1, 1.0, 0.1)
  )
  expect_equal(
    AbsToProbs(1, memb_dim = 'x'),
    1
  )
  expect_error(
    AbsToProbs(data = NULL),
    "Parameter 'data' must be numeric."
  )
  data <- array(1:24, c(member = 3, sdate = 2, lon = 4))
  expect_equal(
    AbsToProbs(data), 
    array(c(rep(0, 3), rep(1, 3)), c(member = 3, sdate = 2, lon = 4))
  )
})

##############################################
library(CSTools)
library(s2dv)

test_that("2. Seasonal forecasts", {
  exp <- CSTools::lonlat_temp$exp$data[1,1:3,1:3,,1:5,1:5]
  exp_probs <- AbsToProbs(exp)
  expect_equal(
    dim(exp)[3:5], 
    dim(exp_probs)[3:5]
  )
  expect_equal(
    round(exp_probs[1,,1,1,1]), 
    c(1, 0, 1)
  )
  exp <- exp[,1,,,] # one sdate
  expect_error(
    exp1_probs <- AbsToProbs(exp), 
    "Could not find dimension 'sdate' in 1th object provided in 'data'."
  )

  exp1 <- InsertDim(exp, 2, 1, name = 'sdate')
  exp1_probs <- AbsToProbs(exp1)
  expect_equal(
    round(exp1_probs[,1,2,2,2]), 
    c(1, 0, 1)
  )
})
