Skip to content
34 changes: 34 additions & 0 deletions tests/test_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
#
"""Unit tests for th_cli/colorize.py."""

import click
import pytest

from th_cli.colorize import (
ColorConfig,
HierarchyEnum,
TextTypeEnum,
colorize_change,
colorize_cmd_help,
colorize_dump,
colorize_error,
Expand Down Expand Up @@ -436,3 +438,35 @@ def test_non_empty_with_colors_enabled(self):
set_colors_enabled(True)
result = italic("hello")
assert "hello" in result


# ---------------------------------------------------------------------------
# colorize_change
# ---------------------------------------------------------------------------


@pytest.mark.unit
class TestColorizeChange:
def test_plain_when_colors_disabled(self):
set_colors_enabled(False)
assert colorize_change("added", " + TC_A_1_1.py") == " + TC_A_1_1.py"

@pytest.mark.parametrize("kind", ["added", "modified", "removed"])
def test_styled_text_keeps_the_content_when_colors_enabled(self, kind):
set_colors_enabled(True)
result = colorize_change(kind, " ~ TC_A_1_1.py")
assert result != " ~ TC_A_1_1.py"
assert click.unstyle(result) == " ~ TC_A_1_1.py"

def test_each_kind_has_its_own_color(self):
set_colors_enabled(True)
results = {kind: colorize_change(kind, "x") for kind in ("added", "modified", "removed")}
assert len(set(results.values())) == 3

def test_get_change_color(self):
cfg = ColorConfig()
assert cfg.get_change_color("added") == "green"
assert cfg.get_change_color("MODIFIED") == "yellow"
assert cfg.get_change_color("removed") == "red"
assert cfg.get_change_color("unknown") == "white"

195 changes: 194 additions & 1 deletion tests/test_rescan_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@
from th_cli.exceptions import ConfigurationError


def _rescan_response(test_collections: api_models.TestCollections, **changes: object) -> api_models.TestCollections:
changes_by_kind: dict[str, object] = {"added": [], "modified": [], "removed": [], "errors": []}
changes_by_kind.update(changes)
return api_models.TestCollections(
test_collections=test_collections.test_collections,
changes=api_models.RescanChanges(collections=list(test_collections.test_collections), **changes_by_kind),
)


def _change(collection: str, test_case: str) -> api_models.TestCaseChange:
return api_models.TestCaseChange(collection=collection, test_suite="suite", test_case=test_case)


@pytest.mark.unit
@pytest.mark.cli
class TestRescanTestsCommand:
Expand All @@ -42,7 +55,7 @@ def test_rescan_tests_success(
"""Test successful rescan of test collections."""
# Arrange
api = mock_sync_apis.test_collections_api.rescan_test_collections_api_v1_test_collections_rescan_post
api.return_value = sample_test_collections
api.return_value = _rescan_response(sample_test_collections)

with patch("th_cli.commands.rescan_tests.get_client", return_value=mock_api_client):
with patch("th_cli.commands.rescan_tests.SyncApis", return_value=mock_sync_apis):
Expand All @@ -52,9 +65,189 @@ def test_rescan_tests_success(
# Assert
assert result.exit_code == 0
assert "Rescanned test collections successfully" in result.output
assert "No changes detected." in result.output
api.assert_called_once()
mock_api_client.close.assert_called_once()

def _invoke(
self,
cli_runner: CliRunner,
mock_sync_apis: Mock,
mock_api_client: Mock,
response: api_models.TestCollections,
) -> str:
api = mock_sync_apis.test_collections_api.rescan_test_collections_api_v1_test_collections_rescan_post
api.return_value = response

with patch("th_cli.commands.rescan_tests.get_client", return_value=mock_api_client):
with patch("th_cli.commands.rescan_tests.SyncApis", return_value=mock_sync_apis):
result = cli_runner.invoke(rescan_tests)

assert result.exit_code == 0
return result.output

def test_rescan_tests_reports_changes_grouped_by_collection(
self,
cli_runner: CliRunner,
mock_sync_apis: Mock,
mock_api_client: Mock,
sample_test_collections: api_models.TestCollections,
) -> None:
"""Added, modified and removed test cases are listed under their collection,
and the collections without changes are summarized."""
names = list(sample_test_collections.test_collections)
changed, unchanged = names[0], names[1:]
response = _rescan_response(
sample_test_collections,
added=[_change(changed, "TC-FAKE-1.1-custom")],
modified=[_change(changed, "TC-ACE-1.3-custom")],
removed=[_change(changed, "TC-OLD-1.1-custom")],
)

output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, response)

assert f"Rescanned test collections successfully ({len(names)} found)" in output
assert "No changes detected" not in output
lines = output.splitlines()
assert changed in lines
assert any(line.startswith(" + TC-FAKE-1.1-custom") and line.endswith("added") for line in lines)
assert any(line.startswith(" ~ TC-ACE-1.3-custom") and line.endswith("modified") for line in lines)
assert any(line.startswith(" - TC-OLD-1.1-custom") and line.endswith("removed") for line in lines)
if unchanged:
assert f"No changes in: {', '.join(unchanged)}" in output

def test_rescan_tests_no_changes(
self,
cli_runner: CliRunner,
mock_sync_apis: Mock,
mock_api_client: Mock,
sample_test_collections: api_models.TestCollections,
) -> None:
output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, _rescan_response(sample_test_collections))

count = len(sample_test_collections.test_collections)
assert f"Rescanned test collections successfully ({count} found). No changes detected." in output
assert "Scripts that could not be parsed" not in output

def test_rescan_tests_reports_parse_errors(
self,
cli_runner: CliRunner,
mock_sync_apis: Mock,
mock_api_client: Mock,
sample_test_collections: api_models.TestCollections,
) -> None:
"""Scripts that failed to parse are listed with the reason, also when nothing else changed."""
response = _rescan_response(
sample_test_collections,
errors=[
api_models.ScriptError(script="TC_BAD_1_1.py", reason="Syntax error: invalid syntax (line 3)"),
api_models.ScriptError(reason="Failed running command:\n boom"),
],
)

output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, response)

assert "No changes detected." in output
assert "Scripts that could not be parsed (skipped):" in output
assert " ! TC_BAD_1_1.py: Syntax error: invalid syntax (line 3)" in output
assert " ! Failed running command: boom" in output

def test_rescan_tests_reports_custom_file_changes(
self,
cli_runner: CliRunner,
mock_sync_apis: Mock,
mock_api_client: Mock,
sample_test_collections: api_models.TestCollections,
) -> None:
"""Added, modified and removed files in custom/ are listed, flagging the ones that replace an SDK module."""
response = _rescan_response(
sample_test_collections,
custom_files=api_models.CustomFileChanges(
added=[api_models.CustomFileChange(name="TC_SUTestBase.py", replaces_sdk=True)],
modified=[api_models.CustomFileChange(name="TC_ACE_1_3.py")],
removed=[api_models.CustomFileChange(name="TC_OLD_1_1.py")],
),
)

output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, response)

assert "Custom files" in output
assert " + TC_SUTestBase.py" in output
assert "added (replaces sdk/TC_SUTestBase.py)" in output
assert " ~ TC_ACE_1_3.py" in output
assert "modified" in output
assert " - TC_OLD_1_1.py" in output
assert "removed" in output
# Only files that replace an SDK module get the note.
assert "sdk/TC_ACE_1_3.py" not in output
assert "sdk/TC_OLD_1_1.py" not in output

def test_rescan_tests_custom_file_changes_alone_are_not_reported_as_no_changes(
self,
cli_runner: CliRunner,
mock_sync_apis: Mock,
mock_api_client: Mock,
sample_test_collections: api_models.TestCollections,
) -> None:
"""A changed helper has no test case, but the rescan did pick up a change."""
response = _rescan_response(
sample_test_collections,
custom_files=api_models.CustomFileChanges(modified=[api_models.CustomFileChange(name="Helper.py")]),
)

output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, response)

count = len(sample_test_collections.test_collections)
assert f"Rescanned test collections successfully ({count} found)" in output
assert "No changes detected" not in output
assert " ~ Helper.py" in output

def test_rescan_tests_removed_file_that_replaced_an_sdk_module(
self,
cli_runner: CliRunner,
mock_sync_apis: Mock,
mock_api_client: Mock,
sample_test_collections: api_models.TestCollections,
) -> None:
response = _rescan_response(
sample_test_collections,
custom_files=api_models.CustomFileChanges(
removed=[api_models.CustomFileChange(name="Helper.py", replaces_sdk=True)]
),
)

output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, response)

assert "removed (was replacing sdk/Helper.py)" in output

def test_rescan_tests_no_custom_file_changes_prints_nothing_extra(
self,
cli_runner: CliRunner,
mock_sync_apis: Mock,
mock_api_client: Mock,
sample_test_collections: api_models.TestCollections,
) -> None:
response = _rescan_response(sample_test_collections, custom_files=api_models.CustomFileChanges())

output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, response)

assert "No changes detected." in output
assert "Custom files" not in output

def test_rescan_tests_backend_without_change_report(
self,
cli_runner: CliRunner,
mock_sync_apis: Mock,
mock_api_client: Mock,
sample_test_collections: api_models.TestCollections,
) -> None:
"""A response without `changes` (older backend) falls back to the plain summary."""
output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, sample_test_collections)

count = len(sample_test_collections.test_collections)
assert f"Rescanned test collections successfully ({count} found)" in output
assert "No changes detected" not in output

def test_rescan_tests_no_response(
self,
cli_runner: CliRunner,
Expand Down
42 changes: 40 additions & 2 deletions th_cli/api_lib_autogen/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# generated by datamodel-codegen:
# filename: tmp6b_blt9g.json
# timestamp: 2026-10-28T16:52:46+00:00
# filename: tmpjqwdpe1o.json
# timestamp: 2026-11-07T00:09:37+00:00

from __future__ import annotations

Expand Down Expand Up @@ -46,6 +46,17 @@ class Name(RootModel[str]):
root: Annotated[str, Field(title="Name")]


class CustomFileChange(BaseModel):
name: Annotated[str, Field(title="Name")]
replaces_sdk: Annotated[bool | None, Field(title="Replaces Sdk")] = False


class CustomFileChanges(BaseModel):
added: Annotated[list[CustomFileChange] | None, Field(default_factory=list, title="Added")]
modified: Annotated[list[CustomFileChange] | None, Field(default_factory=list, title="Modified")]
removed: Annotated[list[CustomFileChange] | None, Field(default_factory=list, title="Removed")]


class Id(RootModel[int]):
root: Annotated[int, Field(title="Id")]

Expand Down Expand Up @@ -76,13 +87,24 @@ class PICSItem(BaseModel):
enabled: Annotated[bool, Field(title="Enabled")]


class ScriptError(BaseModel):
script: Annotated[str | None, Field(title="Script")] = None
reason: Annotated[str, Field(title="Reason")]


class THConfig(BaseModel):
prompt_timeout_seconds: Annotated[int | None, Field(title="Prompt Timeout Seconds")] = 60
enable_realtime_python_test_logs: Annotated[bool | None, Field(title="Enable Realtime Python Test Logs")] = None
enable_container_logs: Annotated[bool | None, Field(title="Enable Container Logs")] = None
sdk_container_apps_dir: Annotated[str | None, Field(title="Sdk Container Apps Dir")] = None


class TestCaseChange(BaseModel):
collection: Annotated[str, Field(title="Collection")]
test_suite: Annotated[str, Field(title="Test Suite Id")]
test_case: Annotated[str, Field(title="Test Case Id")]


class PublicId(RootModel[str]):
root: Annotated[str, Field(title="Public Id")]

Expand Down Expand Up @@ -279,6 +301,21 @@ class PICSCluster(BaseModel):
]


class RescanChanges(BaseModel):
added: Annotated[list[TestCaseChange], Field(title="Added")]
modified: Annotated[list[TestCaseChange], Field(title="Modified")]
removed: Annotated[list[TestCaseChange], Field(title="Removed")]
errors: Annotated[list[ScriptError], Field(title="Errors")]
collections: Annotated[list[str], Field(title="Collections")]
custom_files: Annotated[
CustomFileChanges | None,
Field(
default_factory=lambda: CustomFileChanges.model_validate({"added": [], "modified": [], "removed": []}),
title="Custom Files",
),
]


class TestCase(BaseModel):
metadata: TestMetadata

Expand Down Expand Up @@ -431,6 +468,7 @@ class TestCollection(BaseModel):

class TestCollections(BaseModel):
test_collections: Annotated[dict[str, TestCollection], Field(title="Test Collections")]
changes: RescanChanges | None = None


class TestRunExecutionToExport(BaseModel):
Expand Down
Loading
Loading