"""
Smoke test: render `template_application_form.docx` with pseudo `data_blank.json`-shaped context.

Run from repo root or be0:
  cd be0 && python -m unittest src.test.test_docx_pseudo_fill -v

Skips when docxtpl is missing or the Word template path is not on disk (e.g. CI without fe0 mount).
"""

from __future__ import annotations

import json
import unittest
from pathlib import Path

_JSON = Path(__file__).resolve().parent / "pseudo_data_blank.json"


class DocxPseudoFillTests(unittest.TestCase):
    def test_pseudo_json_loads_and_matches_blank_shape(self) -> None:
        ctx = json.loads(_JSON.read_text(encoding="utf-8"))
        self.assertIn("trang_bia", ctx)
        self.assertIn("mau_01", ctx)
        self.assertIn("mau_02", ctx)
        self.assertIn("mau_03", ctx)
        self.assertIn("mau_04", ctx)
        self.assertIn("ban_cam_ket", ctx)
        self.assertTrue(str(ctx["trang_bia"]["ten_sang_kien"]).startswith("[TEST]"))

    def test_docx_render_returns_bytes(self) -> None:
        try:
            from src.be01.fill_application_form import fill_application_form_docx
        except ImportError as e:
            self.skipTest(f"be01 import failed: {e}")

        ctx = json.loads(_JSON.read_text(encoding="utf-8"))
        try:
            out = fill_application_form_docx(ctx)
        except FileNotFoundError as e:
            self.skipTest(str(e))
        except ModuleNotFoundError as e:
            if "docxtpl" in str(e).lower():
                self.skipTest(str(e))
            raise

        self.assertIsInstance(out, (bytes, bytearray))
        self.assertGreater(len(out), 4000)


if __name__ == "__main__":
    unittest.main()
