Files
voculinux/tests/test_paths.py
T
devin-ai-integration[bot]andGitHub 6595b73c55 fix(ui): unbreak Initial Prompt TextView and dedupe save-dialog .txt (#927)
Two small UI fixes found during the v0.18.1 smoke test.

* Settings > Advanced > Initial Prompt never accepted typed input: the wrapping GtkListBoxRow grabs keyboard focus on button press and steals it back from the GtkTextView. Mark the row non-focusable.
* Transcript and log save dialogs could return a doubled .txt.txt name; new paths.collapse_repeated_extension helper normalizes it, with a replace-confirmation when the collapsed path already exists.
2026-10-07 22:30:51 -07:00

72 lines
3.2 KiB
Python

"""Tests for the XDG base-directory helpers in vocalinux.utils.paths."""
import os
from unittest.mock import patch
from vocalinux.utils import paths
class TestXdgHelpers:
def test_defaults_when_env_unset(self):
with patch.dict(os.environ, {}, clear=True):
assert paths.xdg_config_home() == os.path.expanduser("~/.config")
assert paths.xdg_data_home() == os.path.expanduser("~/.local/share")
assert paths.config_dir() == os.path.expanduser("~/.config/vocalinux")
assert paths.data_dir() == os.path.expanduser("~/.local/share/vocalinux")
assert paths.models_dir() == os.path.expanduser("~/.local/share/vocalinux/models")
def test_honours_xdg_environment(self):
env = {
"XDG_CONFIG_HOME": "/run/user/1000/cfg",
"XDG_DATA_HOME": "/run/user/1000/data",
}
with patch.dict(os.environ, env, clear=True):
assert paths.config_dir() == "/run/user/1000/cfg/vocalinux"
assert paths.data_dir() == "/run/user/1000/data/vocalinux"
assert paths.models_dir() == "/run/user/1000/data/vocalinux/models"
def test_empty_env_falls_back_to_default(self):
# The XDG spec says empty values must be treated as unset.
with patch.dict(os.environ, {"XDG_CONFIG_HOME": "", "XDG_DATA_HOME": ""}, clear=True):
assert paths.xdg_config_home() == os.path.expanduser("~/.config")
assert paths.xdg_data_home() == os.path.expanduser("~/.local/share")
def test_flatpak_style_data_home(self):
# Flatpak points XDG_DATA_HOME at ~/.var/app/<app-id>/data.
flatpak_data = "/home/user/.var/app/com.vocalinux.Vocalinux/data"
with patch.dict(os.environ, {"XDG_DATA_HOME": flatpak_data}, clear=True):
assert paths.models_dir() == os.path.join(flatpak_data, "vocalinux", "models")
def test_is_within_directory(self, tmp_path):
nested = tmp_path / "models" / "ggml-tiny.bin"
nested.parent.mkdir()
nested.write_bytes(b"x")
assert paths.is_within_directory(str(nested), str(tmp_path / "models"))
assert not paths.is_within_directory(str(tmp_path / "models"), str(tmp_path / "models"))
assert paths.is_within_directory(
str(tmp_path / "models"), str(tmp_path / "models"), allow_root=True
)
assert not paths.is_within_directory(str(tmp_path / "other"), str(tmp_path / "models"))
assert not paths.is_within_directory("/etc/passwd", str(tmp_path / "models"))
class TestCollapseRepeatedExtension:
def test_collapses_doubled_suffix(self) -> None:
assert paths.collapse_repeated_extension("a.txt.txt", ".txt") == "a.txt"
def test_keeps_single_suffix(self) -> None:
assert paths.collapse_repeated_extension("a.txt", ".txt") == "a.txt"
def test_keeps_no_suffix(self) -> None:
assert paths.collapse_repeated_extension("a", ".txt") == "a"
def test_case_insensitive_match(self) -> None:
assert paths.collapse_repeated_extension("a.TXT.txt", ".txt") == "a.TXT"
def test_full_path(self) -> None:
assert (
paths.collapse_repeated_extension("/home/u/dir/log.txt.txt", ".txt")
== "/home/u/dir/log.txt"
)