86 lines
2.8 KiB
Python
86 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Focused tests for the canonical consultation-domain registry."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from scripts.consultation_domain_registry import (
|
|
CANONICAL_DOMAINS,
|
|
DEFAULT_THEMES,
|
|
DOMAIN_ALIASES,
|
|
normalize_domain,
|
|
normalize_themes,
|
|
)
|
|
|
|
|
|
def test_canonical_domain_registry_is_complete_and_ordered() -> None:
|
|
assert CANONICAL_DOMAINS == (
|
|
"career",
|
|
"marriage",
|
|
"wealth",
|
|
"health",
|
|
"education",
|
|
"migration",
|
|
"family",
|
|
"annual",
|
|
"timing",
|
|
"general",
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("value", "expected"),
|
|
[
|
|
("relationship", "marriage"),
|
|
("finance", "wealth"),
|
|
("money", "wealth"),
|
|
("health-pressure", "health"),
|
|
("health_pressure", "health"),
|
|
("home", "migration"),
|
|
("relocation", "migration"),
|
|
("varshaphala", "annual"),
|
|
],
|
|
)
|
|
def test_normalize_domain_accepts_required_aliases(value: str, expected: str) -> None:
|
|
assert normalize_domain(value) == expected
|
|
|
|
|
|
def test_normalize_themes_accepts_canonical_and_aliases_deduplicated_in_input_order() -> None:
|
|
assert normalize_themes(
|
|
["timing", "relationship", "marriage", "money", "wealth", "general", "health_pressure"]
|
|
) == ["timing", "marriage", "wealth", "general", "health"]
|
|
|
|
|
|
def test_normalize_themes_keeps_product_defaults() -> None:
|
|
assert normalize_themes(None) == list(DEFAULT_THEMES)
|
|
assert normalize_themes("all") == list(DEFAULT_THEMES)
|
|
assert normalize_themes([]) == list(DEFAULT_THEMES)
|
|
|
|
|
|
@pytest.mark.parametrize("value", ["spirituality", "灵性", "unknown-domain"])
|
|
def test_normalize_themes_rejects_unknown_domains_instead_of_falling_back(value: str) -> None:
|
|
with pytest.raises(ValueError, match="Unknown theme"):
|
|
normalize_themes(["career", value])
|
|
|
|
|
|
def test_typescript_registry_matches_python_canonical_domains_and_aliases() -> None:
|
|
import json
|
|
import re
|
|
from pathlib import Path
|
|
|
|
source = (Path(__file__).resolve().parents[1] / "frontend/src/lib/consultation-domain-registry.ts").read_text()
|
|
canonical_match = re.search(r"consultationDomainIds = \[([\s\S]*?)\] as const", source)
|
|
assert canonical_match is not None
|
|
typescript_domains = tuple(re.findall(r'"([^"\n]+)"', canonical_match.group(1)))
|
|
assert typescript_domains == CANONICAL_DOMAINS
|
|
|
|
definitions = re.findall(r'\{ id: "([a-z]+)"[^\n]*?aliases: (\[[^\]]*\])', source)
|
|
assert tuple(domain for domain, _aliases in definitions) == CANONICAL_DOMAINS
|
|
typescript_aliases: dict[str, str] = {}
|
|
for domain, raw_aliases in definitions:
|
|
for alias in json.loads(raw_aliases):
|
|
if alias != domain:
|
|
typescript_aliases[alias] = domain
|
|
assert typescript_aliases == DOMAIN_ALIASES
|