# Comprehensive test for coverage library(testthat) library(Athlytics) # Create comprehensive test data create_test_activities <- function() { data.frame( activity_id = as.character(1:100), `Activity ID` = as.character(1:100), name = paste("Activity", 1:100), distance = runif(100, 1000, 20000), moving_time = runif(100, 600, 7200), elapsed_time = runif(100, 600, 7200), total_elevation_gain = runif(100, 0, 500), sport_type = sample(c("Run", "Ride"), 100, replace = TRUE), type = sample(c("Run", "Ride"), 100, replace = TRUE), start_date = seq(as.Date("2024-01-01"), by = "day", length.out = 100), start_date_local = seq(as.Date("2024-01-01"), by = "day", length.out = 100), date = seq(as.Date("2024-01-01"), by = "day", length.out = 100), average_heartrate = runif(100, 120, 180), max_heartrate = runif(100, 150, 200), average_watts = runif(100, 150, 350), filename = paste0("activities/", 1:100, ".gpx"), stringsAsFactors = FALSE ) } test_that("All color functions work", { # Test all palette functions expect_length(athlytics_palette_nature(), 9) expect_length(athlytics_palette_academic(), 8) expect_length(athlytics_palette_vibrant(), 8) expect_length(athlytics_palette_science(), 8) expect_length(athlytics_palette_cell(), 8) # Test color zone functions expect_type(athlytics_colors_acwr_zones(), "list") expect_type(athlytics_colors_training_load(), "list") expect_type(athlytics_colors_ef(), "list") # Test theme expect_s3_class(theme_athlytics(), "theme") expect_s3_class(theme_athlytics(14, "Arial"), "theme") # Test scale function expect_s3_class(scale_athlytics("nature", "color"), "Scale") expect_s3_class(scale_athlytics("nature", "fill"), "Scale") expect_s3_class(scale_athlytics("academic", "color"), "Scale") expect_s3_class(scale_athlytics("vibrant", "color"), "Scale") expect_s3_class(scale_athlytics("science", "color"), "Scale") expect_s3_class(scale_athlytics("cell", "color"), "Scale") }) test_that("Utility functions work", { # Test english_month_year expect_equal( english_month_year(as.Date("2024-01-15")), "Jan 2024" ) expect_equal( english_month_year(as.Date(c("2024-01-15", "2024-12-25"))), c("Jan 2024", "Dec 2024") ) # Test with empty dates expect_equal( english_month_year(as.Date(character(0))), character(0) ) }) test_that("Calculate functions handle basic cases", { test_data <- create_test_activities() # Test calculate_acwr with proper date range expect_error({ result <- calculate_acwr( activities_data = test_data, load_metric = "duration_mins", start_date = "2024-01-01", end_date = "2024-04-10" ) }, NA) # Expect no error # Test calculate_exposure with date range expect_error({ result <- calculate_exposure( activities_data = test_data, load_metric = "duration_mins", end_date = "2024-04-10" ) }, NA) # Expect no error # Test calculate_ef with date range expect_error({ result <- calculate_ef( activities_data = test_data, start_date = "2024-01-01", end_date = "2024-04-10" ) }, NA) # Expect no error }) test_that("Plot enhanced functions work", { test_acwr <- data.frame( date = seq(as.Date("2024-01-01"), by = "day", length.out = 30), acwr = runif(30, 0.5, 1.5), atl = runif(30, 50, 100), ctl = runif(30, 60, 80), acwr_smooth = runif(30, 0.7, 1.3), acute_load = runif(30, 100, 200), chronic_load = runif(30, 150, 180) ) # Test plot_acwr_enhanced expect_s3_class( plot_acwr_enhanced(test_acwr), "gg" ) # Test with different parameters expect_s3_class( plot_acwr_enhanced(test_acwr, highlight_zones = FALSE), "gg" ) expect_s3_class( plot_acwr_enhanced(test_acwr, show_ci = FALSE), "gg" ) # Test plot_acwr_comparison expect_s3_class( plot_acwr_comparison(test_acwr, test_acwr), "gg" ) }) test_that("Cohort reference functions work", { ref_data <- data.frame( date = rep(seq(as.Date("2024-01-01"), by = "day", length.out = 30), 10), acwr_smooth = rnorm(300, 1, 0.3), athlete_id = rep(1:10, each = 30) ) # Test cohort_reference result <- cohort_reference(ref_data) expect_s3_class(result, "data.frame") expect_true("percentile" %in% names(result)) # Test with different percentiles result2 <- cohort_reference( ref_data, probs = c(0.25, 0.5, 0.75) ) expect_s3_class(result2, "data.frame") # Test plot_with_reference individual_data <- data.frame( date = seq(as.Date("2024-01-01"), by = "day", length.out = 30), acwr_smooth = rnorm(30, 1, 0.3) ) p <- plot_with_reference( individual = individual_data, reference = result ) expect_s3_class(p, "gg") }) test_that("Internal functions are tested", { # Test internal ACWR functions if they exist if (exists("calculate_daily_load", where = asNamespace("Athlytics"))) { daily_load_fn <- get("calculate_daily_load", envir = asNamespace("Athlytics")) test_data <- create_test_activities() result <- daily_load_fn( test_data, load_metric = "duration_mins", user_ftp = NULL, user_max_hr = NULL, user_resting_hr = NULL ) expect_s3_class(result, "data.frame") } # Test decoupling functions if they exist if (exists("calculate_pace_hr_decoupling", where = asNamespace("Athlytics"))) { decoupling_fn <- get("calculate_pace_hr_decoupling", envir = asNamespace("Athlytics")) stream_data <- data.frame( time = 1:100, heartrate = runif(100, 120, 180), velocity_smooth = runif(100, 2, 5) ) result <- decoupling_fn(stream_data) expect_true(is.numeric(result) || is.logical(result)) } })