test_that("PipeOpFDAScaleRange - basic properties", {
  pop = po("fda.scalerange")
  expect_pipeop(pop)
  expect_identical(pop$id, "fda.scalerange")
})

test_that("PipeOpFDAScaleRange works", {
  task = tsk("fuel")
  pop = po("fda.scalerange")
  task_scale = train_pipeop(pop, list(task))[[1L]]
  new_data = task_scale$data()
  expect_task(task_scale)
  expect_shape(new_data, dim = c(129L, 4L))
  expect_identical(task_scale$n_features, task$n_features)
  expect_named(new_data, names(new_data))
  expect_numeric(tf::tf_arg(new_data$NIR), lower = 0, upper = 1)
  expect_numeric(tf::tf_arg(new_data$UVVIS), lower = 0, upper = 1)
  expect_identical(new_data, predict_pipeop(pop, list(task))[[1L]]$data())

  # different range works
  pop = po("fda.scalerange", lower = -1, upper = 1)
  task_scale = train_pipeop(pop, list(task))[[1L]]
  new_data = task_scale$data()
  expect_equal(tf::tf_domain(new_data$NIR), c(-1, 1))
  expect_equal(range(tf::tf_arg(new_data$NIR)), c(-1, 1))
  expect_equal(tf::tf_domain(new_data$UVVIS), c(-1, 1))
  expect_equal(range(tf::tf_arg(new_data$UVVIS)), c(-1, 1))

  # throws error if new data has different domain
  pop = po("fda.scalerange")
  train_pipeop(pop, list(task))
  expect_error(
    predict_pipeop(pop, list(task_scale)),
    "Domain of new data does not match the domain of the training data."
  )

  # irregular data works
  task = tsk("dti")
  pop = po("fda.scalerange", lower = -1, upper = 1)
  new_data = train_pipeop(pop, list(task))[[1L]]$data()
  expect_equal(tf::tf_domain(new_data$cca), c(-1, 1))
  expect_equal(tf::tf_domain(new_data$rcst), c(-1, 1))
})
