tests/testthat/test-validate.R

library(testthat)
library(mini007)

# A workflow from a list of stations (all pass-through unless given) and
# routes, built quietly.
build <- function(stations, routes = list(), groups = list(), entry = NULL,
                  handlers = list()) {
  wf <- Workflow$new("w", use_cache = FALSE)
  suppressMessages({
    for (name in stations) {
      wf$add_station(name, if (!is.null(handlers[[name]])) handlers[[name]] else identity)
    }
    for (route in routes) {
      condition <- if (is.list(route)) route$condition else NULL
      wf$add_route(route[[1]], route[[2]], condition)
    }
    for (group in groups) do.call(wf$add_parallel_group, group)
    if (!is.null(entry)) wf$set_entry(entry)
  })
  wf
}

checks <- function(wf) wf$validate(quiet = TRUE)$check

# ── No issues ─────────────────────────────────────────────────────────────────

test_that("a straight workflow has no issues", {
  wf <- build(c("draft", "review", "publish"), list(c("draft", "review"), c("review", "publish")))
  issues <- wf$validate(quiet = TRUE)
  expect_s3_class(issues, "data.frame")
  expect_equal(nrow(issues), 0)
  expect_named(issues, c("level", "check", "stations", "message"))
  expect_output(wf$validate(), "no issues found")
})

test_that("a loop with a conditional way out is fine", {
  wf <- build(
    c("draft", "review", "publish"),
    list(
      list("draft", "review"),
      list("review", "publish", condition = function(x) grepl("OK", x)),
      list("review", "draft")
    )
  )
  expect_length(checks(wf), 0)
})

test_that("a station with only conditional routes can stop there", {
  wf <- build(c("a", "b"), list(list("a", "b", condition = function(x) TRUE), list("b", "a")))
  expect_length(checks(wf), 0)
})

test_that("a parallel group that starts from a pass-through station is fine", {
  wf <- build(
    c("start", "a", "b", "join"),
    groups = list(list(from = "start", stations = c("a", "b"), to = "join")),
    handlers = list(start = function(x) x)
  )
  expect_length(checks(wf), 0)
})

# ── Errors ────────────────────────────────────────────────────────────────────

test_that("a loop with no way out is an error", {
  wf <- build(c("draft", "review", "publish"),
              list(c("draft", "review"), c("review", "draft")))
  issues <- wf$validate(quiet = TRUE)
  loop <- issues[issues$check == "endless_loop", ]
  expect_equal(loop$level, "error")
  expect_equal(loop$stations, "draft, review")
  expect_match(loop$message, "can never end")
})

test_that("run() refuses a workflow that could never end, before running anything", {
  calls <- 0
  count <- function(x) { calls <<- calls + 1; x }
  wf <- build(c("draft", "review"), list(c("draft", "review"), c("review", "draft")),
              handlers = list(draft = count, review = count))
  err <- tryCatch(suppressMessages(wf$run("Timgad")), error = function(e) e)
  expect_s3_class(err, "mini007_invalid_workflow")
  expect_match(conditionMessage(err), "can never end")
  expect_equal(calls, 0)
})

test_that("a parallel group that leads back into a loop is an error", {
  wf <- build(
    c("start", "a", "b", "join"),
    routes = list(c("join", "start")),
    groups = list(list(from = "start", stations = c("a", "b"), to = "join"))
  )
  issues <- wf$validate(quiet = TRUE)
  expect_equal(issues$stations[issues$check == "endless_loop"], "start, join")
})

test_that("a workflow without stations is an error", {
  issues <- Workflow$new("empty")$validate(quiet = TRUE)
  expect_equal(issues$check, "no_stations")
  expect_equal(issues$level, "error")
})

# ── Warnings ──────────────────────────────────────────────────────────────────

test_that("stations nothing leads to are reported", {
  wf <- build(c("draft", "review", "translate", "summary"), list(c("draft", "review")))
  issues <- wf$validate(quiet = TRUE)
  expect_equal(issues$check, "unreachable")
  expect_equal(issues$level, "warning")
  expect_equal(issues$stations, "translate, summary")
  expect_match(issues$message, "never run")
})

test_that("the entry station is where reachability starts", {
  wf <- build(c("draft", "review"), list(c("draft", "review")), entry = "review")
  issues <- wf$validate(quiet = TRUE)
  expect_equal(issues$stations, "draft")
})

test_that("a second unconditional route is reported as never followed", {
  wf <- build(c("a", "b", "c"), list(c("a", "b"), c("a", "c")))
  issues <- wf$validate(quiet = TRUE)
  expect_true("ignored_route" %in% issues$check)
  expect_match(issues$message[issues$check == "ignored_route"], "only the first (to 'b')", fixed = TRUE)
  # So 'c' never runs either.
  expect_equal(issues$stations[issues$check == "unreachable"], "c")
})

test_that("routes from a station that starts a parallel group are reported", {
  wf <- build(
    c("start", "a", "b", "join", "other"),
    routes = list(c("start", "other")),
    groups = list(list(from = "start", stations = c("a", "b"), to = "join"))
  )
  issues <- wf$validate(quiet = TRUE)
  expect_match(issues$message[issues$check == "ignored_route"], "starts a parallel group")
  expect_equal(issues$stations[issues$check == "unreachable"], "other")
})

test_that("routes out of a station that only runs inside a parallel group are reported", {
  wf <- build(
    c("start", "a", "b", "join"),
    routes = list(c("a", "join")),
    groups = list(list(from = "start", stations = c("a", "b"), to = "join"))
  )
  issues <- wf$validate(quiet = TRUE)
  expect_equal(issues$check, "ignored_route")
  expect_match(issues$message, "only runs inside a parallel group")
})

test_that("several parallel groups from one station are reported", {
  wf <- build(
    c("start", "a", "b", "c"),
    groups = list(
      list(from = "start", stations = c("a", "b")),
      list(from = "start", stations = "c")
    )
  )
  issues <- wf$validate(quiet = TRUE)
  expect_true("duplicate_group" %in% issues$check)
  expect_equal(issues$stations[issues$check == "unreachable"], "c")
})

test_that("warnings do not stop a run", {
  wf <- build(c("a", "b", "orphan"), list(c("a", "b")),
              handlers = list(b = function(x) paste(x, "done")))
  expect_equal(suppressMessages(wf$run("Oran")), "Oran done")
})

test_that("wrapped workflows are checked too", {
  inner <- build(c("x", "y"), list(c("x", "y"), c("y", "x")))
  outer <- Workflow$new("outer")
  suppressMessages(outer$add_station("inner", inner$as_agent("inner_agent", "Loops.")))
  issues <- outer$validate(quiet = TRUE)
  expect_equal(issues$check, "endless_loop")
  expect_match(issues$message, "^In the workflow of Station 'inner': ")
})

test_that("validate() prints errors and warnings", {
  wf <- build(c("a", "b", "c"), list(c("a", "b"), c("b", "a")))
  expect_output(wf$validate(), "1 error and 1 warning")
  expect_output(wf$validate(), "can never end")
})

Try the mini007 package in your browser

Any scripts or data that you put into this service are public.

mini007 documentation built on Dec. 3, 2026, 5:10 p.m.