import json
import tempfile
import unittest
from pathlib import Path
import numpy as np
from tests.helpers import globe_world
from tests.test_engine import small_setup
from worldhistory.cli import main
from worldhistory.engine import Engine
from worldhistory.preview import compare, preview
class PreviewTest(unittest.TestCase):
def test_preview_and_compare(self):
with tempfile.TemporaryDirectory() as d:
a, b = Path(d) / "a", Path(d) / "b"
w, h, races = small_setup(1)
Engine(w, h, races).run(a)
w2, h2, races2 = small_setup(2)
Engine(w2, h2, races2).run(b)
files = preview(a, world=w)
names = {p.name for p in files}
self.assertIn("race_y0060.png", names)
self.assertIn("chart.png", names)
self.assertIn("contact.png", names)
same = compare(a, a, world=w)
self.assertEqual(same["agreement"], 1.0)
diff = compare(a, b, world=w, out=Path(d) / "cmp")
self.assertLessEqual(diff["agreement"], 1.0)
self.assertTrue((Path(d) / "cmp" / "compare.png").exists())
class CliTest(unittest.TestCase):
def test_check_config_errors(self):
with tempfile.TemporaryDirectory() as d:
(Path(d) / "races").mkdir()
(Path(d) / "history.toml").write_text("[run]\nyers = 5\n")
(Path(d) / "races" / "a.toml").write_text('id = "a"\n[demography]\ndensity = 1\ngrowth = 0.1\n'
'[habitat]\nterms = [{ p = "land", w = 1 }]\n')
self.assertEqual(main(["check-config", d]), 1)
(Path(d) / "history.toml").write_text("[run]\nyears = 5\n")
self.assertEqual(main(["check-config", d]), 0)