"""
Tests for wittrans

SPDX-FileCopyrightText: 2024 Jiri Grönroos <jiri.gronroos@iki.fi>

SPDX-License-Identifier: LGPL-3.0-or-later
"""

from pathlib import Path

from wittrans.paths import get_flatpak_paths_from_base


def _make_lc_messages(base: Path, *parts: str) -> Path:
    """Create an LC_MESSAGES directory and return the path."""
    lc = base.joinpath(*parts)
    lc.mkdir(parents=True)
    return lc


def _active(branch: Path) -> Path:
    """Return the active directory under a branch, creating it if needed."""
    active = branch / "active"
    active.mkdir(parents=True, exist_ok=True)
    return active


class TestGetFlatpakPathsFromBase:
    def test_find_stable_branch(self, tmp_path):
        """Translations installed under the 'stable' branch must be discovered."""
        lc = _make_lc_messages(
            _active(tmp_path / "org.gnome.Calculator/x86_64/stable"),
            "files/share/locale/fi/LC_MESSAGES",
        )

        result = get_flatpak_paths_from_base(tmp_path, {"fi"})

        assert lc in result

    def test_find_master_branch(self, tmp_path):
        """Translations installed under the 'master' branch must be discovered."""
        lc = _make_lc_messages(
            _active(tmp_path / "org.gnome.Calculator.Devel/x86_64/master"),
            "files/share/locale/fi/LC_MESSAGES",
        )

        result = get_flatpak_paths_from_base(tmp_path, {"fi"})

        assert lc in result
