aoc-toolkit 0.1.0-alpha.3

A toolkit for solving Advent of Code problems
Documentation
use super::*;

use crate::client::test_support::serve_once;
use std::thread;
use tempfile::tempdir;

#[test]
fn generates_expected_input_path() {
    let directory = tempdir().unwrap();

    let store = InputStore::new(directory.path(), 123, "test-session".to_owned());

    assert_eq!(
        store.input_path(2026, 1),
        directory.path().join("inputs/123/2026/1.txt"),
    );
}

#[test]
fn returns_cached_input_without_http_client() {
    let directory = tempdir().unwrap();

    let mut store = InputStore::new(directory.path(), 123, "test-session".to_owned());

    let path = store.input_path(2026, 1);

    cache::store(&path, "cached-input\n").unwrap();

    let (input, source) = store.get_with_source(2026, 1).unwrap();

    assert_eq!(input, "cached-input\n");
    assert_eq!(source, InputSource::Cached);
    assert!(store.http.is_none());
}

#[test]
fn returns_cache_miss_for_missing_file() {
    let directory = tempdir().unwrap();

    let cached = InputStore::read_cached(&directory.path().join("missing.txt")).unwrap();

    assert_eq!(cached, None);
}

#[test]
fn returns_cache_miss_for_empty_file() {
    let directory = tempdir().unwrap();
    let path = directory.path().join("empty.txt");

    fs::write(&path, "").unwrap();

    assert_eq!(InputStore::read_cached(&path).unwrap(), None,);
}

#[test]
fn creates_cache_directories() {
    let directory = tempdir().unwrap();

    let store = InputStore::new(directory.path(), 123, "test-session".to_owned());

    let path = store.input_path(2026, 5);

    cache::store(&path, "input").unwrap();

    assert_eq!(fs::read_to_string(path).unwrap(), "input",);
}

#[test]
fn reports_invalid_cached_content() {
    let directory = tempdir().unwrap();
    let path = directory.path().join("invalid.txt");

    fs::write(&path, [0xff]).unwrap();

    assert!(matches!(
        InputStore::read_cached(&path),
        Err(ClientError::Io { .. })
    ));
}

#[test]
fn replaces_existing_cached_input() {
    let directory = tempdir().unwrap();
    let path = directory.path().join("2026/1.txt");

    cache::store(&path, "old-input\n").unwrap();
    cache::store(&path, "new-input\n").unwrap();

    assert_eq!(fs::read_to_string(path).unwrap(), "new-input\n");
}

#[test]
fn preserves_input_bytes() {
    let directory = tempdir().unwrap();
    let path = directory.path().join("2026/1.txt");
    let input = "first\r\nsecond\n\n";

    cache::store(&path, input).unwrap();

    assert_eq!(fs::read(path).unwrap(), input.as_bytes());
}

#[test]
fn does_not_leave_temporary_files_after_store() {
    let directory = tempdir().unwrap();
    let path = directory.path().join("1.txt");

    cache::store(&path, "input\n").unwrap();

    assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 1);
}

#[test]
fn resolves_platform_global_cache_root() {
    match dirs::cache_dir() {
        Some(root) => {
            assert_eq!(
                InputStore::global_cache_root().unwrap(),
                root.join("kyrboforge").join("aoc-toolkit").join("inputs")
            );
        }
        None => {
            assert!(matches!(
                InputStore::global_cache_root(),
                Err(ClientError::CacheDirectoryUnavailable)
            ));
        }
    }
}

#[test]
fn isolates_inputs_between_accounts() {
    let directory = tempdir().unwrap();

    let mut first = InputStore::new(directory.path(), 123, "first-session".to_owned());

    let mut second = InputStore::new(directory.path(), 456, "second-session".to_owned());

    cache::store(&first.input_path(2026, 1), "first-input").unwrap();

    cache::store(&second.input_path(2026, 1), "second-input").unwrap();

    assert_eq!(first.get(2026, 1).unwrap(), "first-input");

    assert_eq!(second.get(2026, 1).unwrap(), "second-input");
}

#[test]
fn reuses_cache_after_session_rotation() {
    let directory = tempdir().unwrap();

    let first = InputStore::new(directory.path(), 123, "old-session".to_owned());

    cache::store(&first.input_path(2026, 1), "cached-input").unwrap();

    let mut second = InputStore::new(directory.path(), 123, "new-session".to_owned());

    assert_eq!(second.get(2026, 1).unwrap(), "cached-input");

    assert!(second.http.is_none());
}

#[test]
fn remembers_account_for_session() {
    let directory = tempdir().unwrap();
    let root = directory.path();

    session::SessionStore::remember_account_at(root, "test-session", 123).unwrap();

    assert_eq!(
        session::SessionStore::known_account(root, "test-session",).unwrap(),
        Some(123)
    );

    assert_eq!(
        session::SessionStore::known_account(root, "another-session",).unwrap(),
        None
    );
}

#[test]
fn cache_hits_do_not_acquire_download_lock() {
    let directory = tempdir().unwrap();

    let mut store = InputStore::new(directory.path(), 123, "test-session".to_owned());

    cache::store(&store.input_path(2025, 1), "cached-input").unwrap();

    let (input, source) = store.get_with_source(2025, 1).unwrap();

    assert_eq!(input, "cached-input");
    assert_eq!(source, InputSource::Cached);
    assert!(!store.download_lock.exists());
    assert!(store.http.is_none());
}

#[test]
fn shares_download_lock_between_accounts() {
    let directory = tempdir().unwrap();

    let first = InputStore::new(directory.path(), 123, "first-session".to_owned());

    let second = InputStore::new(directory.path(), 456, "second-session".to_owned());

    assert_eq!(first.download_lock, second.download_lock);
    assert_ne!(first.root, second.root);
}

#[test]
fn downloads_and_reuses_cached_input() {
    let directory = tempdir().unwrap();

    let (url, server) = serve_once(
        "200 OK",
        "Content-Type: text/plain\r\n",
        b"synthetic-input\n",
    );

    let mut store = InputStore::new(directory.path(), 123, "test-session".to_owned());

    store.http = Some(AocHttpClient::for_test(&url).unwrap());

    let (input, source) = store.get_with_source(2025, 1).unwrap();

    assert_eq!(source, InputSource::Downloaded);
    assert_eq!(input, "synthetic-input\n");

    assert_eq!(
        fs::read_to_string(store.input_path(2025, 1),).unwrap(),
        input,
    );

    server.join().unwrap();

    // The HTTP server is no longer available.
    let (cached, source) = store.get_with_source(2025, 1).unwrap();

    assert_eq!(source, InputSource::Cached);
    assert_eq!(cached, input);
}

#[test]
fn does_not_cache_failed_download() {
    let directory = tempdir().unwrap();

    let (url, server) = serve_once(
        "429 Too Many Requests",
        "Content-Type: text/plain\r\n",
        b"rate-limited",
    );

    let mut store = InputStore::new(directory.path(), 123, "test-session".to_owned());

    store.http = Some(AocHttpClient::for_test(&url).unwrap());

    assert!(matches!(
        store.get_with_source(2025, 1),
        Err(ClientError::RateLimited)
    ));

    assert!(!store.input_path(2025, 1).exists());

    server.join().unwrap();
}

#[test]
fn concurrent_stores_download_input_only_once() {
    use std::sync::Barrier;

    let directory = tempdir().unwrap();

    let (url, server) = serve_once("200 OK", "Content-Type: text/plain\r\n", b"shared-input\n");

    let mut first = InputStore::new(directory.path(), 123, "test-session".to_owned());

    let mut second = InputStore::new(directory.path(), 123, "test-session".to_owned());

    first.http = Some(AocHttpClient::for_test(&url).unwrap());

    second.http = Some(AocHttpClient::for_test(&url).unwrap());

    let barrier = Barrier::new(2);

    let (first_result, second_result) = thread::scope(|scope| {
        let first_handle = scope.spawn(|| {
            barrier.wait();

            first.get_with_source(2025, 1).unwrap()
        });

        let second_handle = scope.spawn(|| {
            barrier.wait();

            second.get_with_source(2025, 1).unwrap()
        });

        (first_handle.join().unwrap(), second_handle.join().unwrap())
    });

    assert_eq!(first_result.0, "shared-input\n");
    assert_eq!(second_result.0, "shared-input\n");

    assert!(matches!(
        (first_result.1, second_result.1),
        (InputSource::Downloaded, InputSource::Cached)
            | (InputSource::Cached, InputSource::Downloaded)
    ));

    server.join().unwrap();
}