test_templating.py python
92 lines 3.4 KB
Raw
sha256:8d7f41aafae41deee70035a92f93602ef1722a290153597451e5932af503a42c docs: queue board-identity follow-ups so they survive the session Human 4 hours ago
1 """Unit tests for template token substitution."""
2
3 from __future__ import annotations
4
5 from pathlib import Path
6
7 import pytest
8
9 from adapters.errors import ConfigError
10 from adapters.templating import (
11 ALLOWED_TOKENS,
12 build_token_map,
13 render_template,
14 substitute_tokens,
15 )
16 from tests.support import load_fixture_config
17
18
19 def test_build_token_map_git_only(git_only_config) -> None:
20 token_map = build_token_map(git_only_config)
21 assert token_map["repo.name"] == "test-git"
22 assert token_map["docs.handover_path"] == "docs/OVERSEER-HANDOVER.md"
23 assert token_map["vcs.regime"] == "git-only"
24 assert token_map["docs.coordination"] == ""
25 assert token_map["vcs.muse.staging_remote"] == ""
26
27
28 def test_build_token_map_muse_git_mirror(muse_git_mirror_config) -> None:
29 token_map = build_token_map(muse_git_mirror_config)
30 assert token_map["repo.name"] == "test-mirror"
31 assert token_map["vcs.git.mirror_branch"] == "muse-mirror"
32 assert token_map["docs.coordination_path"] == "docs/CROSS-REPO-COORDINATION.md"
33
34
35 def test_substitute_tokens_replaces_known_keys(git_only_config) -> None:
36 token_map = build_token_map(git_only_config)
37 text = "Repo {{repo.name}} uses {{vcs.regime}}; handover at {{docs.handover_path}}."
38 result = substitute_tokens(text, token_map)
39 assert "{{" not in result
40 assert "test-git" in result
41 assert "git-only" in result
42 assert "docs/OVERSEER-HANDOVER.md" in result
43
44
45 def test_substitute_tokens_fail_closed_on_unknown() -> None:
46 with pytest.raises(ConfigError, match="unknown or unmapped template token"):
47 substitute_tokens("Hello {{evil.token}}", {}, fail_on_unknown=True)
48
49
50 def test_allowed_tokens_registry_matches_builder_keys(git_only_config) -> None:
51 token_map = build_token_map(git_only_config)
52 assert set(token_map) == ALLOWED_TOKENS
53
54
55 def test_render_handover_template(git_only_config, repo_root: Path) -> None:
56 template = (
57 Path(__file__).resolve().parents[2] / "templates" / "OVERSEER-HANDOVER.template.md"
58 )
59 rendered = render_template(template, git_only_config)
60 assert "test-git" in rendered
61 assert "{{repo.name}}" not in rendered
62 assert "docs/OVERSEER-HANDOVER.md" in rendered
63
64
65 def test_render_all_templates_without_unknown_tokens(
66 git_only_config,
67 muse_only_config,
68 muse_git_mirror_config,
69 ) -> None:
70 templates_dir = Path(__file__).resolve().parents[2] / "templates"
71 base_templates = (
72 "OVERSEER-HANDOVER.template.md",
73 "ROADMAP.template.md",
74 "STANDING-DECISIONS.template.md",
75 "CROSS-REPO-COORDINATION.template.md",
76 )
77 bridge_templates = (
78 "MUSE-BRIDGE-WORKFLOW.template.md",
79 "scripts/muse-bridge-deploy.sh.template",
80 )
81 for config in (git_only_config, muse_only_config):
82 for name in base_templates:
83 rendered = render_template(templates_dir / name, config)
84 assert "{{" not in rendered, f"unsubstituted token in {name} for {config.repo.name}"
85 for name in base_templates + bridge_templates:
86 rendered = render_template(templates_dir / name, muse_git_mirror_config)
87 assert "{{" not in rendered, f"unsubstituted token in {name} for muse+git-mirror"
88
89
90 def test_render_template_missing_file_raises(git_only_config, tmp_path: Path) -> None:
91 with pytest.raises(ConfigError, match="template file missing"):
92 render_template(tmp_path / "missing.template.md", git_only_config)
File History 1 commit
sha256:8d7f41aafae41deee70035a92f93602ef1722a290153597451e5932af503a42c docs: queue board-identity follow-ups so they survive the session Human 4 hours ago