# License: Apache-2.0 License # Created by: Patrick Lumbantobing, VertoX-AI # Copyright (c) 2026 VertoX-AI. All rights reserved. # # This work is licensed under the Apache-2.0 License. # To view a copy of this license, visit # https://www.apache.org/licenses/LICENSE-2.0 from __future__ import annotations import argparse from pathlib import Path from types import SimpleNamespace import pytest from speech_text_data_aligners.cli import annotate as cli from speech_text_data_aligners.core.errors import ConfigurationError class Registry: def __init__(self) -> None: self.calls: list[tuple[str, object]] = [] def create(self, name: str, **kwargs: object) -> object: self.calls.append((name, kwargs["config"])) return SimpleNamespace(name=name) def parse_args(arguments: list[str]) -> argparse.Namespace: parser = argparse.ArgumentParser() cli.configure_parser(parser) return parser.parse_args(arguments) def test_configure_and_run_delegate_all_behavior( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: args = parse_args( [ str(tmp_path / "in.csv"), str(tmp_path / "wavs"), str(tmp_path / "out.csv"), "--backend", "fake", "--backend-config-json", '{"model":"x"}', "--strict", "--overwrite", "--diagnostics", str(tmp_path / "d.json"), "--receipt", str(tmp_path / "r.json"), ] ) calls: list[dict[str, object]] = [] monkeypatch.setattr(cli, "annotate_metadata", lambda **kwargs: calls.append(kwargs)) registry = Registry() assert cli.run(args, registry=registry) == 0 # type: ignore[arg-type] assert registry.calls == [("fake", {"model": "x"})] assert calls[0]["metadata_csv"] == tmp_path / "in.csv" options = calls[0]["options"] assert options.strict and options.overwrite # type: ignore[union-attr] @pytest.mark.parametrize("value", ["not-json", "[]"]) def test_run_rejects_invalid_backend_configuration(value: str, tmp_path: Path) -> None: args = parse_args( [ str(tmp_path / "i"), str(tmp_path / "a"), str(tmp_path / "o"), "--backend", "x", ] ) args.backend_config_json = value with pytest.raises(ConfigurationError): cli.run(args, registry=Registry()) # type: ignore[arg-type] def test_run_uses_default_registry( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: registry = Registry() monkeypatch.setattr(cli, "default_backend_registry", lambda: registry) monkeypatch.setattr(cli, "annotate_metadata", lambda **kwargs: None) args = parse_args( [ str(tmp_path / "i"), str(tmp_path / "a"), str(tmp_path / "o"), "--backend", "x", ] ) assert cli.run(args) == 0 assert registry.calls == [("x", {})]