test_cpld_teaching_pipeline.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import json
  2. import subprocess
  3. import sys
  4. from pathlib import Path
  5. ROOT = Path(__file__).resolve().parents[1]
  6. GENERATOR = ROOT / "scripts" / "generate_cpld_teaching_html.py"
  7. VALIDATOR = ROOT / "scripts" / "validate_cpld_teaching_html.py"
  8. CONTENT = ROOT / "workspace" / "teaching_content" / "chapters" / "cpld_teaching_content.json"
  9. def run_cmd(*args: str) -> subprocess.CompletedProcess[str]:
  10. return subprocess.run(
  11. [sys.executable, *args],
  12. cwd=ROOT,
  13. text=True,
  14. capture_output=True,
  15. check=False,
  16. )
  17. def test_generator_creates_all_thirteen_single_file_reports(tmp_path: Path) -> None:
  18. result = run_cmd(str(GENERATOR), "--content", str(CONTENT), "--output-dir", str(tmp_path))
  19. assert result.returncode == 0, result.stderr + result.stdout
  20. files = sorted(tmp_path.glob("cpld_ch*_learning_report_v1.html"))
  21. assert len(files) == 13
  22. assert {f.name for f in files} == {
  23. "cpld_ch1_env_board_learning_report_v1.html",
  24. "cpld_ch2_verilog_fpga_basics_learning_report_v1.html",
  25. "cpld_ch3_td_flow_learning_report_v1.html",
  26. "cpld_ch4_i2c_protocol_learning_report_v1.html",
  27. "cpld_ch5_i2c_bit_shift_learning_report_v1.html",
  28. "cpld_ch6_i2c_control_learning_report_v1.html",
  29. "cpld_ch7_iic_top_eeprom_learning_report_v1.html",
  30. "cpld_ch8_simulation_tb_learning_report_v1.html",
  31. "cpld_ch9_pin_constraint_learning_report_v1.html",
  32. "cpld_ch10_uart_debug_learning_report_v1.html",
  33. "cpld_ch11_pid_24ch_learning_report_v1.html",
  34. "cpld_ch12_temp_control_integration_learning_report_v1.html",
  35. "cpld_ch13_program_debug_runbook_learning_report_v1.html",
  36. }
  37. ch5 = (tmp_path / "cpld_ch5_i2c_bit_shift_learning_report_v1.html").read_text(encoding="utf-8")
  38. assert "SCL_CNT_M = SYS_CLOCK / SCL_CLOCK / 4 - 1" in ch5
  39. assert "i2c_sdat = !i2c_sdat_o && i2c_sdat_oe ? 1'b0 : 1'bz" in ch5
  40. assert "id=\"sclCalcBtn\"" in ch5
  41. ch13 = (tmp_path / "cpld_ch13_program_debug_runbook_learning_report_v1.html").read_text(encoding="utf-8")
  42. assert "No hardware" in ch13
  43. assert "USB Cable is connected" in ch13
  44. assert "id=\"debugTree\"" in ch13
  45. def test_validator_accepts_generated_reports_and_writes_json_receipt(tmp_path: Path) -> None:
  46. generated = run_cmd(str(GENERATOR), "--content", str(CONTENT), "--output-dir", str(tmp_path))
  47. assert generated.returncode == 0, generated.stderr + generated.stdout
  48. receipt = tmp_path / "receipt.json"
  49. validated = run_cmd(str(VALIDATOR), "--dir", str(tmp_path), "--json", "--receipt", str(receipt))
  50. assert validated.returncode == 0, validated.stderr + validated.stdout
  51. payload = json.loads(receipt.read_text(encoding="utf-8"))
  52. assert payload["summary"]["files"] == 13
  53. assert payload["summary"]["errors"] == 0
  54. assert payload["summary"]["warnings"] == 0
  55. assert payload["checks"]["required_anchors"]["passed"] is True
  56. assert payload["checks"]["forbidden_template"]["passed"] is True
  57. assert payload["checks"]["interactive_components"]["passed"] is True
  58. def test_validator_rejects_old_template_repetition(tmp_path: Path) -> None:
  59. bad = tmp_path / "cpld_ch1_env_board_learning_report_v1.html"
  60. repeated = "建议读者先完成前序章节,再在本节动手:打开 TD 工程对照 Top Module。"
  61. bad.write_text(
  62. "<html><body>"
  63. + "".join(f"<section><p>{repeated}</p></section>" for _ in range(12))
  64. + "</body></html>",
  65. encoding="utf-8",
  66. )
  67. result = run_cmd(str(VALIDATOR), "--dir", str(tmp_path), "--json")
  68. assert result.returncode != 0
  69. assert "forbidden template" in (result.stderr + result.stdout).lower()