//! Maintains the year by day stars table in the README. //! //! The only source of truth is Advent of Code itself: the calendar page of a year, fetched with //! the account's session cookie, marks every day with the number of stars the server has accepted. //! Nothing here looks at local solutions or tests. //! //! This module never talks to the network and never sees the session cookie. A separate step //! (see `.tangled/workflows/stars.yml`) saves each calendar page as `
");
for (day, state) in days {
html.push_str(&format!(
"{day}\n"
));
}
html.push_str("");
html
}
#[test]
fn reads_stars_per_day() {
let html = page(&[(1, " calendar-verycomplete"), (2, " calendar-complete"), (3, "")], true);
let days = parse_calendar(&html, 2025).unwrap();
assert_eq!(days.get(&1), Some(&2));
assert_eq!(days.get(&2), Some(&1));
assert_eq!(days.get(&3), Some(&0));
assert_eq!(days.len(), 3);
}
#[test]
fn rejects_signed_out_page() {
let html = page(&[(1, " calendar-complete")], false);
assert!(parse_calendar(&html, 2025).is_err());
}
#[test]
fn rejects_page_without_calendar_and_out_of_range_days() {
assert!(parse_calendar("x", 2025).is_err());
assert!(parse_calendar(&page(&[(13, "")], true), 2025).is_err());
assert!(parse_calendar(&page(&[(13, "")], true), 2024).is_ok());
}
#[test]
fn table_is_a_year_by_day_matrix_newest_first() {
let mut years = BTreeMap::new();
years.insert(2024, YearStars::from([(1, 2), (2, 1)]));
years.insert(2025, YearStars::from([(1, 2), (12, 1)]));
let table = construct_table(&years);
let lines: Vec<&str> = table.lines().collect();
assert_eq!(lines[0], MARKER);
assert!(lines[5].starts_with("| [2025]"));
assert!(lines[6].starts_with("| [2024]"));
// 2025 has 12 days: day 13 onwards is "-", day 12 shows one star.
assert!(lines[5].contains("| ⭐ | - |"));
assert!(lines[5].ends_with("| 3 |"));
assert!(lines[6].ends_with("| 3 |"));
assert!(!lines[6].contains("| - |"));
assert_eq!(lines.last().copied(), Some(MARKER));
}
#[test]
fn replaces_only_between_markers() {
let readme = format!("intro\n\n{MARKER}\nold\n{MARKER}\n\noutro\n");
let table = format!("{MARKER}\nnew\n{MARKER}");
let updated = replace_table(&readme, &table).unwrap();
assert_eq!(updated, format!("intro\n\n{MARKER}\nnew\n{MARKER}\n\noutro\n"));
assert_eq!(replace_table(&updated, &table).unwrap(), updated);
}
#[test]
fn needs_exactly_two_markers() {
assert!(replace_table("none", "t").is_err());
assert!(replace_table(&format!("{MARKER}\n"), "t").is_err());
assert!(replace_table(&format!("{MARKER}{MARKER}{MARKER}"), "t").is_err());
}
}