tests/testthat/test-examples2.R

test_that("example list includes the showcases", {
  expect_true(all(c("data-explorer", "task-runner", "terminal-dashboard", "editor", "accessibility-demo") %in% run_example()))
})

test_that("data-explorer filters, sorts and shows details", {
  skip_on_cran()
  pilot <- test_app(load_example("data-explorer"), 120, 36)
  on.exit(pilot$stop(), add = TRUE)
  pilot$step()
  text <- paste(pilot$screen_text(), collapse = "\n")
  expect_match(text, "Order explorer")
  expect_match(text, "20,000")
  tbl <- pilot$query_one("#table")
  expect_identical(tbl$row_count, 20000L)
  # Search
  pilot$press("/")
  pilot$type("returned")
  pilot$press("enter")
  expect_lt(tbl$row_count, 20000L)
  expect_gt(tbl$row_count, 100L)
  expect_true(all(pilot$query_one("#table")$data$status[tbl$view] == "returned"))
  expect_identical(pilot$app$focused$id, "table")
  expect_length(pilot$app$notifications(), 1)
  # Sorting by the revenue column via the keyboard.
  pilot$press("right", "right", "right", "right", "right", "right", "s")
  expect_identical(tbl$sort_state$column, "revenue")
  # Details follow the cursor.
  pilot$press("down")
  expect_match(paste(pilot$screen_text(), collapse = "\n"), "Selected order")
  # Region filter through the dropdown, and reset through the palette.
  pilot$press("r", "enter", "down", "enter")
  pilot$press("ctrl+p")
  pilot$type("reset")
  pilot$press("enter")
  expect_identical(tbl$row_count, 20000L)
  # Help and exit.
  pilot$press("f1")
  expect_s3_class(pilot$app$screen, "HelpScreen")
  pilot$press("escape", "q")
  expect_true(pilot$exited)
})

test_that("task-runner runs, reports progress and fails gracefully", {
  skip_on_cran()
  pilot <- test_app(load_example("task-runner"), 100, 30)
  on.exit(pilot$stop(), add = TRUE)
  pilot$step()
  expect_match(paste(pilot$screen_text(), collapse = "\n"), "Choose a task")
  # Select the "Generated progress" task and run it.
  pilot$focus("#tasks")
  pilot$press("down", "down", "enter")
  expect_match(as.character(pilot$query_one("#status")$text), "Running")
  expect_true(pilot$query_one("#cancel")$disabled == FALSE)
  pilot$wait_for_workers(120)
  pilot$step()
  expect_match(as.character(pilot$query_one("#status")$text), "Done")
  expect_gt(pilot$query_one("#progress")$value, 0.9)
  log <- pilot$query_one("#log")$lines
  expect_true(any(grepl("20 batches", log)))
  expect_true(any(grepl("all batches written", log)))  # message() on stderr
  # The failing job.
  pilot$focus("#tasks")
  pilot$press("down", "enter")
  pilot$wait_for_workers(120)
  pilot$step()
  expect_match(as.character(pilot$query_one("#status")$text), "Failed")
  expect_true(any(grepl("disk quota", pilot$query_one("#log")$lines)))
  # Cancel a long task.
  pilot$focus("#tasks")
  pilot$press("up", "up", "up", "enter")
  expect_length(pilot$app$workers(), 1)
  pilot$press("c")
  expect_length(pilot$app$workers(), 0)
  expect_match(as.character(pilot$query_one("#status")$text), "Cancelled")
  pilot$press("q")
  expect_true(pilot$exited)
})

test_that("terminal-dashboard updates on a timer and cycles themes", {
  pilot <- test_app(load_example("terminal-dashboard"), 110, 36)
  on.exit(pilot$stop(), add = TRUE)
  pilot$advance(5)
  expect_match(pilot$query_one("#rps")$value, "^[0-9]+$")
  expect_gt(length(pilot$query_one("#rps-spark")$data), 5)
  expect_gt(pilot$query_one("#events")$item_count(), 0)
  text <- paste(pilot$screen_text(), collapse = "\n")
  expect_match(text, "Service dashboard")
  expect_match(text, "billing")
  pilot$press("t")
  expect_identical(pilot$app$theme$name, "light")
  pilot$press("p")
  n <- length(pilot$query_one("#rps-spark")$data)
  pilot$advance(3)
  expect_identical(length(pilot$query_one("#rps-spark")$data), n)
  pilot$press("q")
  expect_true(pilot$exited)
})

test_that("the dashboard runs without colour", {
  pilot <- test_app(load_example("terminal-dashboard"), 110, 36, color_mode = "none")
  on.exit(pilot$stop(), add = TRUE)
  pilot$advance(2)
  expect_match(paste(pilot$screen_text(), collapse = "\n"), "Service dashboard")
})

test_that("editor edits, undoes, searches and reports the position", {
  pilot <- test_app(load_example("editor"), 80, 24)
  on.exit(pilot$stop(), add = TRUE)
  pilot$step()
  ed <- pilot$query_one("#editor")
  expect_true(ed$focused)
  expect_match(pilot$query_one("#position")$text, "Ln 1, Col 1")
  pilot$type("Hi ")
  expect_identical(substr(ed$value, 1, 3), "Hi ")
  expect_match(pilot$query_one("#position")$text, "modified")
  pilot$press("ctrl+z")
  expect_false(identical(substr(ed$value, 1, 3), "Hi "))
  pilot$press("ctrl+f")
  pilot$type("filler")
  expect_match(ed$selection, "filler")
  pilot$press("escape")
  pilot$press("ctrl+g")
  expect_false(inherits(pilot$app$screen, "ModalScreen"))
  pilot$type("5")
  pilot$press("enter")
  expect_identical(ed$cursor_row, 5L)
  pilot$press("ctrl+q")
  expect_true(pilot$exited)
})

test_that("accessibility-demo cycles themes and works without colour", {
  pilot <- test_app(load_example("accessibility-demo"), 90, 30)
  on.exit(pilot$stop(), add = TRUE)
  expect_match(pilot$query_one("#theme")$text, "default")
  pilot$press("t")
  expect_identical(pilot$app$theme$name, "dark")
  pilot$press("t", "t")
  expect_identical(pilot$app$theme$name, "high-contrast")
  expect_match(pilot$query_one("#theme")$text, "high-contrast")
  pilot$press("m")
  expect_true(motion_reduced(pilot$app))
  mono <- test_app(load_example("accessibility-demo"), 90, 30, color_mode = "none")
  on.exit(mono$stop(), add = TRUE)
  expect_match(paste(mono$screen_text(), collapse = "\n"), "Accessibility preview")
  mono$press("q")
  expect_true(mono$exited)
})

Try the termr package in your browser

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

termr documentation built on Oct. 21, 2026, 5:13 p.m.