test_cpld_teaching_pipeline.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. assert "class=\"causal-grid\"" not in ch5
  42. assert "前因 Why" not in ch5
  43. assert "机制 How" not in ch5
  44. assert "后果 So what" not in ch5
  45. assert "class=\"explanation-flow\"" in ch5
  46. assert "class=\"evidence-panel\"" in ch5
  47. ch13 = (tmp_path / "cpld_ch13_program_debug_runbook_learning_report_v1.html").read_text(encoding="utf-8")
  48. assert "No hardware" in ch13
  49. assert "USB Cable is connected" in ch13
  50. assert "id=\"debugTree\"" in ch13
  51. assert "VREF 不供电" in ch13
  52. assert "class=\"causal-grid\"" not in ch13
  53. def test_validator_accepts_generated_reports_and_writes_json_receipt(tmp_path: Path) -> None:
  54. generated = run_cmd(str(GENERATOR), "--content", str(CONTENT), "--output-dir", str(tmp_path))
  55. assert generated.returncode == 0, generated.stderr + generated.stdout
  56. receipt = tmp_path / "receipt.json"
  57. validated = run_cmd(str(VALIDATOR), "--dir", str(tmp_path), "--json", "--receipt", str(receipt))
  58. assert validated.returncode == 0, validated.stderr + validated.stdout
  59. payload = json.loads(receipt.read_text(encoding="utf-8"))
  60. assert payload["summary"]["files"] == 13
  61. assert payload["summary"]["errors"] == 0
  62. assert payload["summary"]["warnings"] == 0
  63. assert payload["checks"]["required_anchors"]["passed"] is True
  64. assert payload["checks"]["forbidden_template"]["passed"] is True
  65. assert payload["checks"]["interactive_components"]["passed"] is True
  66. assert payload["checks"]["rigid_causal_grid"]["passed"] is True
  67. assert payload["checks"]["mechanical_join"]["passed"] is True
  68. def test_validator_rejects_old_template_repetition(tmp_path: Path) -> None:
  69. bad = tmp_path / "cpld_ch1_env_board_learning_report_v1.html"
  70. repeated = "建议读者先完成前序章节,再在本节动手:打开 TD 工程对照 Top Module。"
  71. bad.write_text(
  72. "<html><body>"
  73. + "".join(f"<section><p>{repeated}</p></section>" for _ in range(12))
  74. + "</body></html>",
  75. encoding="utf-8",
  76. )
  77. result = run_cmd(str(VALIDATOR), "--dir", str(tmp_path), "--json")
  78. assert result.returncode != 0
  79. assert "forbidden template" in (result.stderr + result.stdout).lower()