Files
ha-freshharvest/tests/test_translations.py
T
flan 3e880ff2c1
Validate / hassfest (push) Failing after 23s
Validate / pytest (push) Successful in 9s
Validate / HACS (push) Failing after 1m21s
Expose every order component as its own entity and prepare for release
Each cost line is now an entity rather than an attribute: subtotal, box price,
add-ons, tax and delivery fee, plus a total and free-delivery-remaining for the
open order, a delivery-day sensor, and a binary sensor that turns off at the
cutoff. Entities share a base class and declare a scope, so a description
states only the field it reads.

Also parses the driver tip, Bounty savings and the free-delivery threshold. The
threshold is carried across orders because the progress bar only renders on
carts that have not met it.

Adds LICENSE, hacs.json, a CI workflow, a pre-publish audit script, and a test
that cross-checks every entity's translation_key against both translation
files. Manifest URLs now point at GitHub rather than a private forge.
2026-08-03 19:32:50 +00:00

67 lines
2.4 KiB
Python

"""Every entity's translation_key must exist, and nothing may be orphaned.
These parse the platform sources with `ast` rather than importing them, so the
suite still runs without Home Assistant installed.
"""
from __future__ import annotations
import ast
import json
from pathlib import Path
import pytest
COMPONENT = Path(__file__).parent.parent / "custom_components" / "freshharvest"
PLATFORMS = {"sensor": "sensor.py", "binary_sensor": "binary_sensor.py"}
def declared_keys(filename: str) -> set[str]:
"""Collect every `translation_key="..."` literal in a module."""
tree = ast.parse((COMPONENT / filename).read_text(encoding="utf-8"))
return {
node.value.value
for node in ast.walk(tree)
if isinstance(node, ast.keyword)
and node.arg == "translation_key"
and isinstance(node.value, ast.Constant)
and isinstance(node.value.value, str)
}
@pytest.fixture(name="strings")
def strings_fixture() -> dict:
return json.loads((COMPONENT / "strings.json").read_text(encoding="utf-8"))
def test_en_matches_strings(strings):
"""Custom integrations read translations/en.json; it must not drift."""
english = json.loads(
(COMPONENT / "translations" / "en.json").read_text(encoding="utf-8")
)
assert english == strings
@pytest.mark.parametrize("platform,filename", PLATFORMS.items())
def test_every_entity_has_a_name(strings, platform, filename):
missing = declared_keys(filename) - set(strings["entity"][platform])
assert not missing, f"{platform} keys with no translation: {sorted(missing)}"
@pytest.mark.parametrize("platform,filename", PLATFORMS.items())
def test_no_orphaned_translations(strings, platform, filename):
orphans = set(strings["entity"][platform]) - declared_keys(filename)
assert not orphans, f"{platform} translations with no entity: {sorted(orphans)}"
def test_manifest_is_well_formed():
manifest = json.loads((COMPONENT / "manifest.json").read_text(encoding="utf-8"))
for key in ("domain", "name", "version", "documentation", "issue_tracker"):
assert manifest.get(key), f"manifest missing {key}"
assert manifest["domain"] == "freshharvest"
# A private forge URL would be unreachable for anyone installing this.
for key in ("documentation", "issue_tracker"):
assert manifest[key].startswith("https://github.com/"), (
f"{key} must be a public URL, got {manifest[key]}"
)