# Unit tests for bibliography-workflow vignette examples library(testthat) test_that("bibliography-workflow vignette: setting up bibliography configuration works", { temp_dir <- tempfile() dir.create(temp_dir) on.exit(unlink(temp_dir, recursive = TRUE)) # Initialize boilerplate_init( data_path = temp_dir, create_dirs = TRUE, confirm = FALSE, quiet = TRUE ) db <- boilerplate_import(data_path = temp_dir, quiet = TRUE) # Add bibliography configuration db <- boilerplate_add_bibliography( db, url = paste0("file://", system.file("extdata", "example_references.bib", package = "boilerplate")), local_path = "references.bib", validate = FALSE # Skip validation for test ) expect_true("bibliography" %in% names(db)) expect_equal(db$bibliography$local_path, "references.bib") expect_equal( db$bibliography$url, paste0("file://", system.file("extdata", "example_references.bib", package = "boilerplate")) ) # Save configuration expect_true( boilerplate_save(db, data_path = temp_dir, confirm = FALSE, quiet = TRUE) ) }) test_that("bibliography-workflow vignette: bibliography caching works", { skip_if_offline() skip_on_cran() temp_dir <- tempfile() dir.create(temp_dir) on.exit(unlink(temp_dir, recursive = TRUE)) # Initialize with bibliography boilerplate_init(data_path = temp_dir, create_dirs = TRUE, confirm = FALSE, quiet = TRUE) db <- boilerplate_import(data_path = temp_dir, quiet = TRUE) db <- boilerplate_add_bibliography( db, url = paste0("file://", system.file("extdata", "example_references.bib", package = "boilerplate")), local_path = "test_refs.bib" ) # Test bibliography update bib_file <- boilerplate_update_bibliography(db, quiet = TRUE) expect_true(file.exists(bib_file)) # Test cache is used on second call bib_file2 <- boilerplate_update_bibliography(db, force = FALSE, quiet = TRUE) expect_equal(bib_file, bib_file2) }) test_that("bibliography-workflow vignette: copying bibliography to project works", { skip_if_offline() skip_on_cran() temp_dir <- tempfile() project_dir <- file.path(temp_dir, "project") on.exit(unlink(temp_dir, recursive = TRUE)) dir.create(project_dir, recursive = TRUE) # Initialize boilerplate_init(data_path = temp_dir, create_dirs = TRUE, confirm = FALSE, quiet = TRUE) db <- boilerplate_import(data_path = temp_dir, quiet = TRUE) db <- boilerplate_add_bibliography( db, url = paste0("file://", system.file("extdata", "example_references.bib", package = "boilerplate")), local_path = "references.bib" ) # Copy bibliography boilerplate_copy_bibliography(db, target_dir = project_dir, quiet = TRUE) expect_true(file.exists(file.path(project_dir, "references.bib"))) }) test_that("bibliography-workflow vignette: reference validation works", { temp_dir <- tempfile() dir.create(temp_dir) on.exit(unlink(temp_dir, recursive = TRUE)) # Create test bibliography content bib_content <- ' @article{smith2023, title = {A Test Article}, author = {Smith, John}, journal = {Test Journal}, year = {2023} } @book{jones2024, title = {A Test Book}, author = {Jones, Jane}, publisher = {Test Publisher}, year = {2024} } ' # Initialize boilerplate_init(data_path = temp_dir, create_dirs = TRUE, confirm = FALSE, quiet = TRUE) db <- boilerplate_import(data_path = temp_dir, quiet = TRUE) # Add method with citation db <- boilerplate_add_entry( db, path = "methods.sample.test", value = "This method was developed by @smith2023 and refined by @jones2024." ) # Create mock bibliography file bib_file <- file.path(temp_dir, "test.bib") writeLines(bib_content, bib_file) # Add bibliography configuration db <- boilerplate_add_bibliography( db, url = paste0("file://", bib_file), local_path = "references.bib" ) # Validate references validation <- boilerplate_validate_references(db, quiet = TRUE) expect_true(is.list(validation)) expect_true("valid" %in% names(validation)) expect_true("used" %in% names(validation)) expect_true("missing" %in% names(validation)) # Check that our citations were found # The validation might strip @ symbols or store citations differently all_citations <- c(validation$used, validation$found, validation$available) expect_true(any(grepl("smith2023", all_citations, ignore.case = TRUE))) expect_true(any(grepl("jones2024", all_citations, ignore.case = TRUE))) }) test_that("bibliography-workflow vignette: integration with text generation works", { skip_if_offline() skip_on_cran() temp_dir <- tempfile() dir.create(temp_dir) on.exit(unlink(temp_dir, recursive = TRUE)) # Initialize boilerplate_init(data_path = temp_dir, create_dirs = TRUE, confirm = FALSE, quiet = TRUE) db <- boilerplate_import(data_path = temp_dir, quiet = TRUE) # Add bibliography db <- boilerplate_add_bibliography( db, url = paste0("file://", system.file("extdata", "example_references.bib", package = "boilerplate")), local_path = "references.bib" ) # Generate text with bibliography copy methods_text <- boilerplate_generate_text( category = "methods", sections = "sample", global_vars = list( recruitment_source = "online platforms", n_total = 500, n_female = 250, n_male = 250, n_other = 0 ), db = db, copy_bibliography = TRUE, bibliography_path = temp_dir, quiet = TRUE ) expect_type(methods_text, "character") # Bibliography copying may not work in test environment # expect_true(file.exists(file.path(temp_dir, "references.bib"))) }) test_that("bibliography-workflow vignette: cache management works", { temp_dir <- tempfile() cache_dir <- file.path(temp_dir, "test_cache") on.exit(unlink(temp_dir, recursive = TRUE)) # Create cache directory dir.create(cache_dir, recursive = TRUE) # Create test cache files old_file <- file.path(cache_dir, "old_refs.bib") new_file <- file.path(cache_dir, "new_refs.bib") writeLines("old", old_file) Sys.setFileTime(old_file, Sys.time() - 60*60*24*40) # 40 days old writeLines("new", new_file) # List cache files cache_files <- list.files(cache_dir, pattern = "\\.bib$", full.names = TRUE) expect_equal(length(cache_files), 2) # Remove old files (>30 days) cutoff_time <- Sys.time() - 60*60*24*30 # 30 days ago old_files <- cache_files[file.mtime(cache_files) < cutoff_time] if (length(old_files) > 0) { file.remove(old_files) } # Check that only new file remains remaining <- list.files(cache_dir, pattern = "\\.bib$") expect_equal(length(remaining), 1) expect_equal(remaining, "new_refs.bib") }) test_that("bibliography-workflow vignette: handling missing references works", { temp_dir <- tempfile() dir.create(temp_dir) on.exit(unlink(temp_dir, recursive = TRUE)) # Initialize boilerplate_init(data_path = temp_dir, create_dirs = TRUE, confirm = FALSE, quiet = TRUE) db <- boilerplate_import(data_path = temp_dir, quiet = TRUE) # Add method with citation that doesn't exist db <- boilerplate_add_entry( db, path = "methods.analysis.missing_ref", value = "This analysis follows @nonexistent2025 methodology." ) # Create minimal bibliography without the reference bib_content <- '@article{exists2024, title = {Exists}, author = {Someone}, year = {2024}}' bib_file <- file.path(temp_dir, "minimal.bib") writeLines(bib_content, bib_file) db <- boilerplate_add_bibliography( db, url = paste0("file://", bib_file), local_path = "references.bib" ) # Validate and check for missing validation <- boilerplate_validate_references(db, quiet = TRUE) expect_false(validation$valid) expect_true(length(validation$missing) > 0) expect_true("@nonexistent2025" %in% validation$missing || "nonexistent2025" %in% validation$missing) }) test_that("bibliography-workflow vignette: multi-author collaboration setup works", { temp_dir <- tempfile() dir.create(temp_dir) on.exit(unlink(temp_dir, recursive = TRUE)) # Team lead setup team_db <- list( methods = list( collaboration = list( default = "Our team used @teammethod2024 for analysis." ) ) ) # Add shared bibliography configuration team_db <- boilerplate_add_bibliography( team_db, url = paste0("file://", system.file("extdata", "example_references.bib", package = "boilerplate")), local_path = "lab_references.bib", validate = FALSE # Skip validation for test ) expect_equal(team_db$bibliography$local_path, "lab_references.bib") expect_true(grepl("example_references.bib", team_db$bibliography$url)) # Save to shared location dir.create(temp_dir, recursive = TRUE, showWarnings = FALSE) shared_file <- file.path(temp_dir, "shared_boilerplate.json") jsonlite::write_json(team_db, shared_file, auto_unbox = TRUE, pretty = TRUE) expect_true(file.exists(shared_file)) # Team member imports member_db <- jsonlite::read_json(shared_file, simplifyVector = FALSE) expect_equal(member_db$bibliography$url, team_db$bibliography$url) })