# tests/testthat/test-pci.R
# Test each classical index against hand-computed values.

test_that("Cp is computed correctly for a centered process", {
  # USL=63, LSL=57, sigma=1 -> (63-57)/(6*1) = 1.0

  expect_equal(pci("Cp", LSL = 57, USL = 63, sigma = 1), 1.0)
  # sigma=0.5 -> 6/(6*0.5) = 2.0
  expect_equal(pci("Cp", LSL = 57, USL = 63, sigma = 0.5), 2.0)
  # sigma=1.5 -> 6/9 = 0.6667
  expect_equal(pci("Cp", LSL = 57, USL = 63, sigma = 1.5), 6 / 9,
               tolerance = 1e-4)
})

test_that("Cpk is computed correctly for centered and off-center processes", {
  # Centered: mu=60, sigma=1 -> min(3/3, 3/3) = 1.0
  expect_equal(pci("Cpk", LSL = 57, USL = 63, mu = 60, sigma = 1), 1.0)
  # Off-center: mu=61 -> min(2/3, 4/3) = 0.6667
  expect_equal(pci("Cpk", LSL = 57, USL = 63, mu = 61, sigma = 1),
               2 / 3, tolerance = 1e-4)
  # Off-center the other way: mu=58 -> min(5/3, 1/3) = 0.3333
  expect_equal(pci("Cpk", LSL = 57, USL = 63, mu = 58, sigma = 1),
               1 / 3, tolerance = 1e-4)
})

test_that("Cpu is computed correctly", {
  expect_equal(pci("Cpu", USL = 63, mu = 60, sigma = 1), 1.0)
  expect_equal(pci("Cpu", USL = 63, mu = 61, sigma = 1), 2 / 3,
               tolerance = 1e-4)
})

test_that("Cpl is computed correctly", {
  expect_equal(pci("Cpl", LSL = 57, mu = 60, sigma = 1), 1.0)
  expect_equal(pci("Cpl", LSL = 57, mu = 61, sigma = 1), 4 / 3,
               tolerance = 1e-4)
})

test_that("Cpm is computed correctly", {
  # Centered on target: mu=60, target=60, sigma=1
  # -> 6 / (6*sqrt(1+0)) = 1.0

  expect_equal(pci("Cpm", LSL = 57, USL = 63, mu = 60, sigma = 1, target = 60),
               1.0)
  # Off-target: mu=60, target=59, sigma=1
  # -> 6 / (6*sqrt(1 + 1)) = 6/(6*sqrt(2)) = 1/sqrt(2) ~ 0.7071
  expect_equal(pci("Cpm", LSL = 57, USL = 63, mu = 60, sigma = 1, target = 59),
               1 / sqrt(2), tolerance = 1e-4)
})

test_that("Cpmk is computed correctly", {
  # Centered on target: -> 1.0
  expect_equal(pci("Cpmk", LSL = 57, USL = 63, mu = 60, sigma = 1, target = 60),
               1.0)
  # Off-center, off-target: mu=61, target=59, sigma=1
  # tau = 3*sqrt(1 + 4) = 3*sqrt(5)
  # min((63-61)/tau, (61-57)/tau) = min(2, 4) / (3*sqrt(5)) = 2/(3*sqrt(5))
  expect_equal(pci("Cpmk", LSL = 57, USL = 63, mu = 61, sigma = 1, target = 59),
               2 / (3 * sqrt(5)), tolerance = 1e-4)
})

test_that("Pp is computed correctly", {
  # s=1.5 -> 6/(6*1.5) = 0.6667
  expect_equal(pci("Pp", LSL = 57, USL = 63, s = 1.5), 6 / 9,
               tolerance = 1e-4)
})

test_that("Ppk is computed correctly", {
  # Centered: xbar=60, s=1.5 -> min(3/4.5, 3/4.5) = 0.6667
  expect_equal(pci("Ppk", LSL = 57, USL = 63, xbar = 60, s = 1.5),
               3 / 4.5, tolerance = 1e-4)
})

test_that("Ppu is computed correctly", {
  expect_equal(pci("Ppu", USL = 63, xbar = 60, s = 1.5), 3 / 4.5,
               tolerance = 1e-4)
})

test_that("Ppl is computed correctly", {
  expect_equal(pci("Ppl", LSL = 57, xbar = 60, s = 1.5), 3 / 4.5,
               tolerance = 1e-4)
})

test_that("Z (sigma level) is computed correctly", {
  # Centered: min(3/1, 3/1) = 3.0
  expect_equal(pci("Z", LSL = 57, USL = 63, mu = 60, sigma = 1), 3.0)
  # Off-center: mu=61 -> min(2, 4) = 2.0
  expect_equal(pci("Z", LSL = 57, USL = 63, mu = 61, sigma = 1), 2.0)
})

test_that("convenience wrappers return the same values as pci()", {
  expect_equal(cp(LSL = 57, USL = 63, sigma = 1),
               pci("Cp", LSL = 57, USL = 63, sigma = 1))
  expect_equal(cpk(LSL = 57, USL = 63, mu = 60, sigma = 1),
               pci("Cpk", LSL = 57, USL = 63, mu = 60, sigma = 1))
  expect_equal(cpu(USL = 63, mu = 60, sigma = 1),
               pci("Cpu", USL = 63, mu = 60, sigma = 1))
  expect_equal(cpl(LSL = 57, mu = 60, sigma = 1),
               pci("Cpl", LSL = 57, mu = 60, sigma = 1))
  expect_equal(cpm(LSL = 57, USL = 63, mu = 60, sigma = 1, target = 60),
               pci("Cpm", LSL = 57, USL = 63, mu = 60, sigma = 1, target = 60))
  expect_equal(cpmk(LSL = 57, USL = 63, mu = 60, sigma = 1, target = 60),
               pci("Cpmk", LSL = 57, USL = 63, mu = 60, sigma = 1, target = 60))
  expect_equal(pp(LSL = 57, USL = 63, s = 1.5),
               pci("Pp", LSL = 57, USL = 63, s = 1.5))
  expect_equal(ppk(LSL = 57, USL = 63, xbar = 60, s = 1.5),
               pci("Ppk", LSL = 57, USL = 63, xbar = 60, s = 1.5))
  expect_equal(ppu(USL = 63, xbar = 60, s = 1.5),
               pci("Ppu", USL = 63, xbar = 60, s = 1.5))
  expect_equal(ppl(LSL = 57, xbar = 60, s = 1.5),
               pci("Ppl", LSL = 57, xbar = 60, s = 1.5))
  expect_equal(z_level(LSL = 57, USL = 63, mu = 60, sigma = 1),
               pci("Z", LSL = 57, USL = 63, mu = 60, sigma = 1))
})
