"""Deciding whether anything changed since the last run.""" import json from language_stats import get_repo_timestamps, has_new_commits, load_state, save_state SAVED = { "repo_timestamps": { "website": "2026-11-01T09:00:00Z", "scripts": "2026-11-05T14:30:00Z", } } def test_repo_timestamps_map_each_repo_to_its_updated_at(): repos = [ {"name": "website", "updated_at": "2026-11-01T09:00:00Z"}, {"name": "scripts", "updated_at": "2026-11-05T14:30:00Z"}, ] assert get_repo_timestamps(repos) == { "website": "2026-11-01T09:00:00Z", "scripts": "2026-11-05T14:30:00Z", } def test_repo_timestamps_leave_out_the_profile_repo(): # Each README push updates .profile, so counting it would make every # run look like it has new commits. repos = [ {"name": ".profile", "updated_at": "2026-11-10T06:00:00Z"}, {"name": "website", "updated_at": "2026-11-01T09:00:00Z"}, ] assert get_repo_timestamps(repos) == {"website": "2026-11-01T09:00:00Z"} def test_repo_without_updated_at_gets_an_empty_timestamp(): assert get_repo_timestamps([{"name": "website"}]) == {"website": ""} def test_no_new_commits_when_every_timestamp_matches(): current = {"website": "2026-11-01T09:00:00Z", "scripts": "2026-11-05T14:30:00Z"} assert has_new_commits(current, SAVED) is False def test_new_commits_when_a_repo_was_updated(): current = {"website": "2026-11-01T09:00:00Z", "scripts": "2026-11-11T08:00:00Z"} assert has_new_commits(current, SAVED) is True def test_new_commits_when_a_repo_was_added(): current = { "website": "2026-11-01T09:00:00Z", "scripts": "2026-11-05T14:30:00Z", "new-tool": "2026-11-11T08:00:00Z", } assert has_new_commits(current, SAVED) is True def test_new_commits_when_a_repo_was_deleted(): current = {"website": "2026-11-01T09:00:00Z"} assert has_new_commits(current, SAVED) is True def test_first_run_without_saved_state_counts_as_new_commits(): assert has_new_commits({"website": "2026-11-01T09:00:00Z"}, {}) is True def test_load_state_returns_empty_dict_when_no_file_exists(): assert load_state() == {} def test_load_state_reads_the_saved_json(state_file): state_file.parent.mkdir(parents=True) state_file.write_text('{"repo_timestamps": {"website": "2026-11-01T09:00:00Z"}}') assert load_state() == {"repo_timestamps": {"website": "2026-11-01T09:00:00Z"}} def test_load_state_treats_a_corrupt_file_as_empty(state_file): state_file.parent.mkdir(parents=True) state_file.write_text("{not valid json") assert load_state() == {} def test_save_state_creates_missing_folders_and_writes_json(state_file): save_state({"repo_timestamps": {"website": "2026-11-01T09:00:00Z"}}) assert json.loads(state_file.read_text()) == { "repo_timestamps": {"website": "2026-11-01T09:00:00Z"} }