File size: 3,000 Bytes
51e84d2
 
 
 
 
 
 
 
fc1dcbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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", {})]