tests/testthat/test-checkpoint.R

library(testthat)
library(mini007)

# A three-Station workflow whose Stations count their calls in `calls`; the
# second one fails while `calls$fail` is TRUE.
guide_workflow <- function(calls, name = "guide") {
  wf <- Workflow$new(name)
  suppressMessages({
    wf$add_station("research", function(x) {
      calls$research <- calls$research + 1L
      paste("Facts about", x)
    })
    wf$add_station("draft", function(x) {
      calls$draft <- calls$draft + 1L
      if (isTRUE(calls$fail)) stop("the model is unreachable")
      paste("Draft:", x)
    })
    wf$add_station("polish", function(x) {
      calls$polish <- calls$polish + 1L
      paste("Polished", x)
    })
    wf$add_route("research", "draft")
    wf$add_route("draft", "polish")
  })
  wf
}

new_calls <- function(fail = FALSE) {
  calls <- new.env()
  calls$research <- 0L
  calls$draft <- 0L
  calls$polish <- 0L
  calls$fail <- fail
  calls
}

quiet <- function(expr) suppressMessages(expr)

# Whether `x` holds a function or an environment anywhere.
holds_code <- function(x) {
  if (is.function(x) || is.environment(x)) return(TRUE)
  if (is.list(x)) return(any(vapply(x, holds_code, logical(1))))
  FALSE
}

test_that("a run saves its state after every step and marks it complete", {
  path <- tempfile(fileext = ".rds")
  calls <- new_calls()
  out <- quiet(guide_workflow(calls)$run("Oran", checkpoint = path))

  expect_equal(out, "Polished Draft: Facts about Oran")
  saved <- readRDS(path)
  expect_s3_class(saved, "mini007_checkpoint")
  expect_equal(saved$status, "complete")
  expect_equal(saved$output, out)
  expect_equal(saved$state$steps_taken, 3L)
  expect_length(saved$state$trace, 3L)
  expect_false(holds_code(unclass(saved)))

  # A finished run is not run again by $resume_from().
  expect_message(again <- guide_workflow(calls)$resume_from(path), "already\\s+complete")
  expect_equal(again, out)
  expect_equal(calls$research, 1L)
})

test_that("a failed run goes on from the Station that failed", {
  path <- tempfile(fileext = ".rds")
  calls <- new_calls(fail = TRUE)
  expect_error(quiet(guide_workflow(calls)$run("Oran", checkpoint = path)), "unreachable")

  saved <- readRDS(path)
  expect_equal(saved$status, "failed")
  expect_match(saved$error, "unreachable")
  expect_equal(saved$state$current, "draft")
  expect_equal(saved$state$current_input, "Facts about Oran")
  expect_equal(saved$state$steps_taken, 1L)

  # After a restart: a new Workflow object, built the same way.
  calls$fail <- FALSE
  wf <- guide_workflow(calls)
  expect_message(out <- wf$resume_from(path), "had stopped")
  expect_equal(out, "Polished Draft: Facts about Oran")
  expect_equal(calls$research, 1L)
  expect_equal(calls$draft, 2L)
  expect_equal(readRDS(path)$status, "complete")
  expect_equal(wf$run_history[[1]]$steps, 3L)
})

test_that("$run() picks up an unfinished run of the same input", {
  path <- tempfile(fileext = ".rds")
  calls <- new_calls(fail = TRUE)
  expect_error(quiet(guide_workflow(calls)$run("Oran", checkpoint = path)))

  calls$fail <- FALSE
  expect_message(out <- guide_workflow(calls)$run("Oran", checkpoint = path), "Picking up")
  expect_equal(out, "Polished Draft: Facts about Oran")
  expect_equal(calls$research, 1L)

  # A finished run, or another input, starts over.
  quiet(guide_workflow(calls)$run("Oran", checkpoint = path))
  expect_equal(calls$research, 2L)
  quiet(guide_workflow(calls)$run("Annaba", checkpoint = path))
  expect_equal(calls$research, 3L)
  expect_equal(readRDS(path)$state$original_input, "Annaba")
})

test_that("a run paused for a human can be answered after a restart", {
  path <- tempfile(fileext = ".rds")
  calls <- new_calls()
  wf <- guide_workflow(calls)
  wf$set_hitl(2, mode = "pause")
  pending <- quiet(wf$run("Oran", checkpoint = path))
  expect_true(is_pending(pending))
  expect_equal(readRDS(path)$status, "awaiting_input")

  restarted <- guide_workflow(calls)
  restarted$set_hitl(2, mode = "pause")
  expect_message(again <- restarted$resume_from(path), "waiting for a human")
  expect_true(is_pending(again))
  expect_equal(again$request_id, pending$request_id)
  expect_equal(again$proposed_output, "Draft: Facts about Oran")

  out <- quiet(restarted$resume_from(path, action = "edit", value = "Draft: Oran, city of Rai"))
  expect_equal(out, "Polished Draft: Oran, city of Rai")
  expect_equal(calls$draft, 1L)
  expect_equal(readRDS(path)$status, "complete")
})

test_that("$resume() of a paused run keeps saving to its checkpoint", {
  path <- tempfile(fileext = ".rds")
  calls <- new_calls()
  wf <- guide_workflow(calls)
  wf$set_hitl(1, mode = "pause")
  pending <- quiet(wf$run("Oran", checkpoint = path))
  out <- quiet(wf$resume(pending$request_id, "continue"))
  expect_equal(out, "Polished Draft: Facts about Oran")
  expect_equal(readRDS(path)$status, "complete")
})

test_that("a paused parallel group is saved without its merge function", {
  path <- tempfile(fileext = ".rds")
  make <- function() {
    wf <- Workflow$new("tour")
    quiet({
      wf$add_station("start", function(x) paste("Tour of", x))
      wf$add_station("north", function(x) paste(x, "north"))
      wf$add_station("south", function(x) paste(x, "south"))
      wf$add_station("end", function(x) paste("Done:", x))
      wf$add_parallel_group("start", c("north", "south"), to = "end",
                            merge_fn = function(r) paste(unlist(r), collapse = " + "))
      wf$set_hitl(1, mode = "pause")
    })
    wf
  }
  pending <- quiet(make()$run("Algeria", checkpoint = path))
  saved <- readRDS(path)
  expect_equal(saved$state$context, list(kind = "parallel", from = "start", group_input = "Tour of Algeria"))
  expect_false(holds_code(unclass(saved)))

  out <- quiet(make()$resume_from(path, action = "continue"))
  expect_equal(out, "Done: Tour of Algeria north + Tour of Algeria south")
})

test_that("checkpoints of another workflow, or broken files, are refused", {
  path <- tempfile(fileext = ".rds")
  calls <- new_calls(fail = TRUE)
  expect_error(quiet(guide_workflow(calls)$run("Oran", checkpoint = path)))

  expect_error(guide_workflow(calls, name = "other")$resume_from(path), "not")
  wf <- Workflow$new("guide")
  quiet(wf$add_station("research", function(x) x))
  expect_error(wf$resume_from(path), "Unknown Station")

  expect_error(Workflow$new("guide")$resume_from(tempfile()), "no checkpoint file")
  junk <- tempfile(fileext = ".rds")
  saveRDS(list(a = 1), junk)
  expect_error(wf$resume_from(junk), "not\\s+a\\s+mini007\\s+Workflow\\s+checkpoint")
  expect_error(wf$resume_from(path, action = "skip"))
  expect_error(guide_workflow(calls)$run("Oran", checkpoint = 1))
})

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.