133 lines
5.0 KiB
Python
133 lines
5.0 KiB
Python
import json
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
from datetime import datetime, timedelta
|
|
|
|
import generator
|
|
|
|
|
|
class PreviewHtmlTest(unittest.TestCase):
|
|
def test_preview_html_output(self) -> None:
|
|
config_date = "2026-02-06"
|
|
config_time = "15:00"
|
|
story_data = {
|
|
"meta": {
|
|
"name": "Preview Test Story",
|
|
"log_prefix": "LOG",
|
|
},
|
|
"events": [
|
|
{
|
|
"id": "log_01",
|
|
"title": "Kickoff",
|
|
"story": "<b>HELLO</b><br>World",
|
|
}
|
|
],
|
|
}
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
previous_cwd = os.getcwd()
|
|
os.chdir(tmpdir)
|
|
try:
|
|
with open("config.toml", "w", encoding="utf-8") as f:
|
|
f.write(
|
|
"\n".join(
|
|
[
|
|
f'start_date = "{config_date}"',
|
|
f'start_time = "{config_time}"',
|
|
'repo_url = "https://example.com/repo"',
|
|
"blocked_weeks = []",
|
|
"blocked_dates = []",
|
|
"skip_day_after_ascension = false",
|
|
]
|
|
)
|
|
+ "\n"
|
|
)
|
|
|
|
story_path = os.path.join(tmpdir, "story.json")
|
|
with open(story_path, "w", encoding="utf-8") as f:
|
|
json.dump(story_data, f, ensure_ascii=False, indent=2)
|
|
|
|
generator.generate_single_file(story_path, preview_html=True)
|
|
|
|
slug = generator.sanitize_filename(story_data["meta"]["name"])
|
|
preview_path = os.path.join(
|
|
"fredagsbar_output",
|
|
f"PREVIEW_{slug}.html",
|
|
)
|
|
self.assertTrue(os.path.exists(preview_path))
|
|
|
|
with open(preview_path, "r", encoding="utf-8") as f:
|
|
preview_html = f.read()
|
|
|
|
expected_date = generator.next_or_same_friday(
|
|
datetime.strptime(config_date, "%Y-%m-%d").date()
|
|
)
|
|
self.assertIn(expected_date.strftime("%Y-%m-%d"), preview_html)
|
|
self.assertIn("15:00-16:00", preview_html)
|
|
self.assertIn(story_data["events"][0]["title"], preview_html)
|
|
self.assertIn(story_data["events"][0]["story"], preview_html)
|
|
finally:
|
|
os.chdir(previous_cwd)
|
|
|
|
def test_preview_includes_skipped_dates(self) -> None:
|
|
config_date = "2026-02-06"
|
|
config_time = "15:00"
|
|
story_data = {
|
|
"meta": {
|
|
"name": "Preview Skip Story",
|
|
"log_prefix": "LOG",
|
|
},
|
|
"events": [
|
|
{
|
|
"id": "log_01",
|
|
"title": "Kickoff",
|
|
"story": "<b>HELLO</b><br>World",
|
|
}
|
|
],
|
|
}
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
previous_cwd = os.getcwd()
|
|
os.chdir(tmpdir)
|
|
try:
|
|
with open("config.toml", "w", encoding="utf-8") as f:
|
|
f.write(
|
|
"\n".join(
|
|
[
|
|
f'start_date = "{config_date}"',
|
|
f'start_time = "{config_time}"',
|
|
'repo_url = "https://example.com/repo"',
|
|
"blocked_weeks = []",
|
|
'blocked_dates = ["02-06"]',
|
|
"skip_day_after_ascension = false",
|
|
]
|
|
)
|
|
+ "\n"
|
|
)
|
|
|
|
story_path = os.path.join(tmpdir, "story.json")
|
|
with open(story_path, "w", encoding="utf-8") as f:
|
|
json.dump(story_data, f, ensure_ascii=False, indent=2)
|
|
|
|
generator.generate_single_file(story_path, preview_html=True)
|
|
|
|
slug = generator.sanitize_filename(story_data["meta"]["name"])
|
|
preview_path = os.path.join(
|
|
"fredagsbar_output",
|
|
f"PREVIEW_{slug}.html",
|
|
)
|
|
self.assertTrue(os.path.exists(preview_path))
|
|
|
|
with open(preview_path, "r", encoding="utf-8") as f:
|
|
preview_html = f.read()
|
|
|
|
start_date = datetime.strptime(config_date, "%Y-%m-%d").date()
|
|
expected_date = generator.next_or_same_friday(start_date) + timedelta(weeks=1)
|
|
self.assertIn("Skipped dates", preview_html)
|
|
self.assertIn(start_date.strftime("%Y-%m-%d"), preview_html)
|
|
self.assertIn("Manuelt blokeret dato", preview_html)
|
|
self.assertIn(expected_date.strftime("%Y-%m-%d"), preview_html)
|
|
finally:
|
|
os.chdir(previous_cwd)
|