test_that("Caching works", {

    skip_if_offline(host = "retrosheet.org")

    # Delete any previously cached data
    unlink("testdata", recursive = TRUE)

    # With caching
    schedule_1 <- getRetrosheet("schedule", 1995, cache = "testdata")
    schedule_1a <- getRetrosheet("schedule", 1995, cache = "testdata/") # Test with trailing slash
    roster_1 <- getRetrosheet("roster", 1995, cache = "testdata")
    game_1 <- getRetrosheet("game", 2012, cache = "testdata")
    play_1 <- getRetrosheet("play", 2012, "SFN", cache = "testdata")

    # Without caching
    schedule_2 <- getRetrosheet("schedule", 1995)
    roster_2 <- getRetrosheet("roster", 1995)
    game_2 <- getRetrosheet("game", 2012)
    play_2 <- getRetrosheet("play", 2012, "SFN")

    expect_equal(schedule_1, schedule_2)
    expect_equal(schedule_1, schedule_1a)
    expect_equal(roster_1, roster_2)
    expect_equal(game_1, game_2)
    expect_equal(play_1, play_2)

    # Re-using cached data should give a message about using a local cache
    expect_message(getRetrosheet("schedule", 1995, cache = "testdata"), "Using local cache: testdata/schedule/1995SKED.zip")

})

test_that("Schedule downloading works", {

    skip_if_offline(host = "retrosheet.org")

    schedule <- getRetrosheet("schedule", 1995, cache = "testdata")
    schedule_splits <- getRetrosheet("schedule", 1995, schedSplit = "TimeOfDay")

    # In 1995, there were 28 teams, each team played a 144 game schedule
    expect_equal(nrow(schedule), 28 * 144 / 2)

    # There were 601 day games, 60 evening games, and 1355 night games
    expect_equal(sapply(schedule_splits, nrow), c(D = 601L, E = 60L, N = 1355L))

})

test_that("Roster downloading works", {

    skip_if_offline(host = "retrosheet.org")

    roster <- getRetrosheet("roster", 1995, cache = "testdata")

    # In 1995, there should be 28 regular teams
    # The two All Star teams (NLS + ALS) are no longer included in these data
    expect_equal(length(roster), 28)

    # Toronto has 39 players on their roster
    expect_equal(nrow(roster$TOR), 39)

})

test_that("Game downloading works", {

    skip_if_offline(host = "retrosheet.org")

    game <- getRetrosheet("game", 2012, cache = "testdata")

    # Each "game" has 161 different attributes
    expect_equal(length(game), 161)

    # In 2012, there were 30 teams, each played 162 games (and each game has two teams)
    expect_equal(nrow(game), 30 * 162 / 2)

})

test_that("Play downloading works", {

    skip_if_offline(host = "retrosheet.org")

    play <- getRetrosheet("play", 2012, "SFN", cache = "testdata")

    # SFN played 81 games in 2012
    expect_equal(length(play), 81)

    # In the first game of the year, there were...
    expect_equal(nrow(play[[1]]$play), 68)     # 68 plays
    expect_equal(nrow(play[[1]]$sub), 4)       # 4 substitutions
    expect_equal(nrow(play[[1]]$start), 2 * 9) # 18 starters

})

test_that("Schedule parses pre-2024 format correctly", {

    skip_if_offline(host = "retrosheet.org")

    schedule <- getRetrosheet("schedule", 1995)

    expect_equal(ncol(schedule), 12)
    expect_false("Location" %in% names(schedule))

})

test_that("Schedule parses 2024+ format correctly", {

    skip_if_offline(host = "retrosheet.org")

    schedule <- getRetrosheet("schedule", 2025)

    expect_equal(ncol(schedule), 13)
    expect_true("Location" %in% names(schedule))
    # 2025 season opened in Tokyo — some games should have a non-empty Location
    expect_true(any(nchar(trimws(schedule$Location)) > 0, na.rm = TRUE))

})

test_that("Schedule loads for most recent completed season without error", {

    skip_if_offline(host = "retrosheet.org")

    most_recent_year <- as.integer(format(Sys.Date(), "%Y")) - 1
    schedule <- getRetrosheet("schedule", most_recent_year)

    expect_true(is.data.frame(schedule))
    expect_gt(nrow(schedule), 0)
    expect_true(all(c("Date", "HmTeam", "VisTeam", "GameNo") %in% names(schedule)))

})

# Delete any previously cached data
unlink("testdata", recursive = TRUE)
