import json import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] GENERATOR = ROOT / "scripts" / "generate_cpld_teaching_html.py" VALIDATOR = ROOT / "scripts" / "validate_cpld_teaching_html.py" CONTENT = ROOT / "workspace" / "teaching_content" / "chapters" / "cpld_teaching_content.json" def run_cmd(*args: str) -> subprocess.CompletedProcess[str]: return subprocess.run( [sys.executable, *args], cwd=ROOT, text=True, capture_output=True, check=False, ) def test_generator_creates_all_thirteen_single_file_reports(tmp_path: Path) -> None: result = run_cmd(str(GENERATOR), "--content", str(CONTENT), "--output-dir", str(tmp_path)) assert result.returncode == 0, result.stderr + result.stdout files = sorted(tmp_path.glob("cpld_ch*_learning_report_v1.html")) assert len(files) == 13 assert {f.name for f in files} == { "cpld_ch1_env_board_learning_report_v1.html", "cpld_ch2_verilog_fpga_basics_learning_report_v1.html", "cpld_ch3_td_flow_learning_report_v1.html", "cpld_ch4_i2c_protocol_learning_report_v1.html", "cpld_ch5_i2c_bit_shift_learning_report_v1.html", "cpld_ch6_i2c_control_learning_report_v1.html", "cpld_ch7_iic_top_eeprom_learning_report_v1.html", "cpld_ch8_simulation_tb_learning_report_v1.html", "cpld_ch9_pin_constraint_learning_report_v1.html", "cpld_ch10_uart_debug_learning_report_v1.html", "cpld_ch11_pid_24ch_learning_report_v1.html", "cpld_ch12_temp_control_integration_learning_report_v1.html", "cpld_ch13_program_debug_runbook_learning_report_v1.html", } ch5 = (tmp_path / "cpld_ch5_i2c_bit_shift_learning_report_v1.html").read_text(encoding="utf-8") assert "SCL_CNT_M = SYS_CLOCK / SCL_CLOCK / 4 - 1" in ch5 assert "i2c_sdat = !i2c_sdat_o && i2c_sdat_oe ? 1'b0 : 1'bz" in ch5 assert "id=\"sclCalcBtn\"" in ch5 assert "class=\"causal-grid\"" not in ch5 assert "前因 Why" not in ch5 assert "机制 How" not in ch5 assert "后果 So what" not in ch5 assert "class=\"explanation-flow\"" in ch5 assert "class=\"evidence-panel\"" in ch5 ch13 = (tmp_path / "cpld_ch13_program_debug_runbook_learning_report_v1.html").read_text(encoding="utf-8") assert "No hardware" in ch13 assert "USB Cable is connected" in ch13 assert "id=\"debugTree\"" in ch13 assert "VREF 不供电" in ch13 assert "class=\"causal-grid\"" not in ch13 def test_validator_accepts_generated_reports_and_writes_json_receipt(tmp_path: Path) -> None: generated = run_cmd(str(GENERATOR), "--content", str(CONTENT), "--output-dir", str(tmp_path)) assert generated.returncode == 0, generated.stderr + generated.stdout receipt = tmp_path / "receipt.json" validated = run_cmd(str(VALIDATOR), "--dir", str(tmp_path), "--json", "--receipt", str(receipt)) assert validated.returncode == 0, validated.stderr + validated.stdout payload = json.loads(receipt.read_text(encoding="utf-8")) assert payload["summary"]["files"] == 13 assert payload["summary"]["errors"] == 0 assert payload["summary"]["warnings"] == 0 assert payload["checks"]["required_anchors"]["passed"] is True assert payload["checks"]["forbidden_template"]["passed"] is True assert payload["checks"]["interactive_components"]["passed"] is True assert payload["checks"]["rigid_causal_grid"]["passed"] is True assert payload["checks"]["mechanical_join"]["passed"] is True def test_validator_rejects_old_template_repetition(tmp_path: Path) -> None: bad = tmp_path / "cpld_ch1_env_board_learning_report_v1.html" repeated = "建议读者先完成前序章节,再在本节动手:打开 TD 工程对照 Top Module。" bad.write_text( "
" + "".join(f"{repeated}