from pathlib import Path import pytest from xlsx_clean.core import resolve_write_year_dir, resolve_year_dir def _touch(path: Path) -> None: path.parent.mkdir(parents=True, exist_ok=True) path.write_bytes(b"") def test_resolve_year_picks_largest_dir_with_matching_sheet(tmp_path: Path): pattern = "ICJ_108 op. [SERIAL].xlsx" _touch(tmp_path / "2025" / "ICJ108 Beige" / "ICJ_108 op. 1.xlsx") _touch(tmp_path / "2026" / "ICJ108 Beige" / "ICJ_108 op. 2.xlsx") placeholder = tmp_path / "[YEAR]" / "ICJ108 Beige" assert resolve_year_dir(placeholder, pattern) == tmp_path / "2026" / "ICJ108 Beige" def test_resolve_year_ignores_larger_empty_year(tmp_path: Path): pattern = "ICJ_108 op. [SERIAL].xlsx" _touch(tmp_path / "2025" / "ICJ108 Beige" / "ICJ_108 op. 1.xlsx") (tmp_path / "2026" / "ICJ108 Beige").mkdir(parents=True) placeholder = tmp_path / "[YEAR]" / "ICJ108 Beige" assert resolve_year_dir(placeholder, pattern) == tmp_path / "2025" / "ICJ108 Beige" def test_resolve_year_jalali_style(tmp_path: Path): pattern = "KRCY1100T Blue [SERIAL].xlsx" _touch(tmp_path / "1402" / "KRCY1100T Blue" / "KRCY1100T Blue 1.xlsx") _touch(tmp_path / "1403" / "KRCY1100T Blue" / "KRCY1100T Blue 2.xlsx") placeholder = tmp_path / "[YEAR]" / "KRCY1100T Blue" assert resolve_year_dir(placeholder, pattern) == tmp_path / "1403" / "KRCY1100T Blue" def test_resolve_year_passthrough_without_token(tmp_path: Path): path = tmp_path / "2025" / "ICJ108 Beige" assert resolve_year_dir(path, "x [SERIAL].xlsx") == path def test_resolve_year_fails_when_no_sheets(tmp_path: Path): (tmp_path / "2026" / "ICJ108 Beige").mkdir(parents=True) with pytest.raises(FileNotFoundError, match="No year directory"): resolve_year_dir(tmp_path / "[YEAR]" / "ICJ108 Beige", "ICJ_108 op. [SERIAL].xlsx") def test_resolve_write_year_picks_largest_even_if_empty(tmp_path: Path): _touch(tmp_path / "2025" / "ICJ108 Beige" / "ICJ_108 op. 1.xlsx") (tmp_path / "2026").mkdir() placeholder = tmp_path / "[YEAR]" / "ICJ108 Beige" assert resolve_write_year_dir(placeholder) == tmp_path / "2026" / "ICJ108 Beige" def test_resolve_write_year_jalali_style(tmp_path: Path): (tmp_path / "1402").mkdir() (tmp_path / "1403").mkdir() placeholder = tmp_path / "[YEAR]" / "KRCY1100T Blue" assert resolve_write_year_dir(placeholder) == tmp_path / "1403" / "KRCY1100T Blue" def test_resolve_write_year_passthrough_without_token(tmp_path: Path): path = tmp_path / "2025" / "ICJ108 Beige" assert resolve_write_year_dir(path) == path def test_resolve_write_year_fails_when_no_digit_dirs(tmp_path: Path): (tmp_path / "notes").mkdir() with pytest.raises(FileNotFoundError, match="No year directory"): resolve_write_year_dir(tmp_path / "[YEAR]" / "ICJ108 Beige") def test_read_and_write_year_differ_when_sheets_only_in_older(tmp_path: Path): pattern = "ICJ_108 op. [SERIAL].xlsx" _touch(tmp_path / "2025" / "ICJ108 Beige" / "ICJ_108 op. 1.xlsx") (tmp_path / "2026").mkdir() placeholder = tmp_path / "[YEAR]" / "ICJ108 Beige" assert resolve_year_dir(placeholder, pattern) == tmp_path / "2025" / "ICJ108 Beige" assert resolve_write_year_dir(placeholder) == tmp_path / "2026" / "ICJ108 Beige"