""" Phase G — Sync History Tests Tests for GET /history: rendering _sync_log.md as a reverse-chronological table. """ import textwrap from pathlib import Path import pytest pytestmark = pytest.mark.asyncio # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- SAMPLE_LOG = textwrap.dedent("""\ # Sync Log | Timestamp | Direction | Files | Status | |-----------|-----------|-------|--------| | 2026-03-03 22:15 | push | 1 updated | error: CV.md failed | | 2026-03-04 08:00 | pull | 0 changes | ok | | 2026-03-05 09:10 | push | 2 updated, 1 created | ok | | 2026-03-06 14:32 | pull | 3 updated | ok | """) MINIMAL_LOG = textwrap.dedent("""\ # Sync Log | Timestamp | Direction | Files | Status | |-----------|-----------|-------|--------| | 2026-01-01 00:00 | pull | 1 updated | ok | """) # --------------------------------------------------------------------------- # US-G1 — History page renders # --------------------------------------------------------------------------- class TestHistoryPage: async def test_history_returns_200(self, client): r = await client.get("/history") assert r.status_code == 200 async def test_history_returns_html(self, client): r = await client.get("/history") assert "text/html" in r.headers.get("content-type", "") async def test_history_page_contains_table(self, client, vault_dir, sync_log): r = await client.get("/history") body = r.text.lower() assert ") raw_table_lines = [l for l in body.splitlines() if l.strip().startswith("|---")] assert len(raw_table_lines) == 0, ( "Raw markdown table separator lines must not appear in rendered HTML" ) async def test_all_log_entries_appear(self, client, vault_dir): """All 4 entries in SAMPLE_LOG must appear in the rendered history.""" (vault_dir / "_sync_log.md").write_text(SAMPLE_LOG) r = await client.get("/history") body = r.text assert "2026-03-06" in body assert "2026-03-05" in body assert "2026-03-04" in body assert "2026-03-03" in body async def test_single_entry_log_renders(self, client, vault_dir): (vault_dir / "_sync_log.md").write_text(MINIMAL_LOG) r = await client.get("/history") assert r.status_code == 200 assert "2026-01-01" in r.text async def test_history_api_endpoint_returns_json(self, client, vault_dir): """ GET /history?format=json returns structured history data. This is optional but strongly recommended for future HTMX updates. """ (vault_dir / "_sync_log.md").write_text(SAMPLE_LOG) r = await client.get("/history?format=json") # If not implemented, 200 HTML is also acceptable if r.status_code == 200 and "application/json" in r.headers.get("content-type", ""): data = r.json() assert isinstance(data, list) assert len(data) >= 4 for entry in data: assert "timestamp" in entry or "date" in entry assert "direction" in entry