Files
code_of_conquest_dnd/tools/content_build/tests/test_cli.py

84 lines
3.1 KiB
Python

import shutil
from pathlib import Path
from content_build.__main__ import main
FIXTURE_DIR = Path(__file__).parent / "fixtures" / "valid"
def _lore(tmp_path):
lore = tmp_path / "lore"
lore.mkdir()
shutil.copy(FIXTURE_DIR / "world.md", lore / "world.md")
return lore
def test_build_then_check_is_clean(tmp_path):
lore = _lore(tmp_path)
world, server = tmp_path / "world", tmp_path / "server"
argv = ["--lore", str(lore), "--world", str(world), "--server", str(server)]
assert main(argv) == 0
assert (world / "npcs" / "tess.json").exists()
assert (server / "topics" / "big-twist.json").exists()
# a fresh --check against just-written output is clean
assert main(argv + ["--check"]) == 0
def test_check_detects_stale(tmp_path):
lore = _lore(tmp_path)
world, server = tmp_path / "world", tmp_path / "server"
argv = ["--lore", str(lore), "--world", str(world), "--server", str(server)]
assert main(argv) == 0
# mutate a committed artifact -> --check must fail
(world / "canon" / "testburg.json").write_text('{"id": "town.testburg"}\n')
assert main(argv + ["--check"]) == 1
def test_check_detects_missing(tmp_path):
lore = _lore(tmp_path)
world, server = tmp_path / "world", tmp_path / "server"
argv = ["--lore", str(lore), "--world", str(world), "--server", str(server)]
assert main(argv) == 0
(world / "npcs" / "tess.json").unlink()
assert main(argv + ["--check"]) == 1
def test_invalid_bible_fails_build(tmp_path):
lore = tmp_path / "lore"
lore.mkdir()
(lore / "bad.md").write_text(
"```yaml\nid: town.a\ntype: town\nstatus: canon\nsecrecy: 0\n"
"related: [does.not-exist]\nbody: x\n```\n")
argv = ["--lore", str(lore), "--world", str(tmp_path / "w"),
"--server", str(tmp_path / "s")]
assert main(argv) == 1
def test_check_detects_orphan_file(tmp_path):
lore = _lore(tmp_path)
world, server = tmp_path / "world", tmp_path / "server"
argv = ["--lore", str(lore), "--world", str(world), "--server", str(server)]
assert main(argv) == 0
(world / "canon" / "ghost.json").write_text('{"id": "town.ghost"}\n')
assert main(argv + ["--check"]) == 1
def test_check_ignores_legacy_npc_files(tmp_path):
lore = _lore(tmp_path)
world, server = tmp_path / "world", tmp_path / "server"
argv = ["--lore", str(lore), "--world", str(world), "--server", str(server)]
assert main(argv) == 0
(world / "npcs").mkdir(parents=True, exist_ok=True)
(world / "npcs" / "legacy.json").write_text('{"id": "legacy"}\n') # bare id
assert main(argv + ["--check"]) == 0 # legacy file is not a build orphan
def test_check_detects_orphan_generated_npc(tmp_path):
lore = _lore(tmp_path)
world, server = tmp_path / "world", tmp_path / "server"
argv = ["--lore", str(lore), "--world", str(world), "--server", str(server)]
assert main(argv) == 0
(world / "npcs").mkdir(parents=True, exist_ok=True)
(world / "npcs" / "ghost.json").write_text('{"id": "npc.ghost"}\n')
assert main(argv + ["--check"]) == 1 # a generated npc.* orphan in the shared dir IS caught