From 7e98da3301bea9d54e8bbf84bedec5196d6fbcd0 Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 2 Jul 2026 23:53:17 +0800 Subject: [PATCH] Harden CLI paths and coupling export checks --- tests/module/test_coupling_interface.f90 | 11 ++++ todo.md | 80 ++++++++++++------------ tools/testing/test_cli.py | 34 ++++++++++ tools/testing/test_python_debug.py | 10 +++ 4 files changed, 95 insertions(+), 40 deletions(-) diff --git a/tests/module/test_coupling_interface.f90 b/tests/module/test_coupling_interface.f90 index def9f00..d8bf698 100644 --- a/tests/module/test_coupling_interface.f90 +++ b/tests/module/test_coupling_interface.f90 @@ -94,6 +94,9 @@ contains if (ext_id /= 1) call fail('water coupling export id mismatch') if (phase /= SPH_PHASE_WATER) call fail('water coupling phase mismatch') if (material <= 0) call fail('water coupling material mismatch') + if (.not. is_finite_vector(position)) call fail('water coupling position is not finite') + if (.not. is_finite_vector(velocity)) call fail('water coupling velocity is not finite') + if (.not. is_finite(particle_pressure)) call fail('water coupling pressure is not finite') force = (/0.0_rk, 0.0_rk, 2.0_rk/) call sph_coupling_import_particle_force(1, force, ierr) @@ -104,6 +107,8 @@ contains position, velocity, particle_pressure, ierr) call assert_ok(ierr, 'water coupling export after force failed') if (velocity(3) <= 0.0_rk) call fail('coupling force did not affect explicit step') + if (.not. is_finite_vector(position)) call fail('water coupling position after force is not finite') + if (.not. is_finite_vector(velocity)) call fail('water coupling velocity after force is not finite') if (.not. is_finite(particle_pressure)) call fail('water coupling pressure is not finite') call sph_coupling_get_diagnostics(work, energy, momentum) @@ -135,4 +140,10 @@ contains is_finite = (value == value .and. abs(value) < huge(1.0_rk)) end function is_finite + logical function is_finite_vector(value) + real(rk), intent(in) :: value(:) + + is_finite_vector = all(value == value) .and. maxval(abs(value)) < huge(value(1)) + end function is_finite_vector + end program test_coupling_interface diff --git a/todo.md b/todo.md index 1ab02eb..0bcce7b 100644 --- a/todo.md +++ b/todo.md @@ -328,52 +328,52 @@ - [x] 确认 Python 通过 C ABI 调用。 - [x] 加固 Python water summary 包装。 - [x] 加固 Python water dt limits 包装。 -- [ ] 加固 Python inspect 辅助函数。 -- [ ] 加固 Python run 辅助函数。 -- [ ] 加固 Python 缺失输入错误路径。 -- [ ] 加固 Python 缺失 Water 参数错误路径。 -- [ ] 确认 Python 不直接访问 Fortran 内部符号。 +- [x] 加固 Python inspect 辅助函数。 +- [x] 加固 Python run 辅助函数。 +- [x] 加固 Python 缺失输入错误路径。 +- [x] 加固 Python 缺失 Water 参数错误路径。 +- [x] 确认 Python 不直接访问 Fortran 内部符号。 ## 阶段 P:CLI 加固 -- [ ] 保持 `sphlite --version` 可用。 -- [ ] 保持 `sphlite model.inp` inspect 模式可用。 -- [ ] 保持 `sphlite model.inp --check` 模式可用。 -- [ ] 保持 `sphlite model.inp --run` 模式可用。 -- [ ] 保持 `sphlite model.inp --run --output output` 可用。 -- [ ] 确认 CLI 输出 `version` 字段。 -- [ ] 确认 CLI 输出 `input` 字段。 -- [ ] 确认 CLI 输出 `mode` 字段。 -- [ ] 确认 CLI 输出 `particles` 字段。 -- [ ] 确认 CLI 输出 `warnings` 字段。 -- [ ] 确认 CLI 输出 `time` 字段。 -- [ ] 确认 CLI 输出 `step` 字段。 -- [ ] 确认 CLI 输出 `water_particles` 字段。 -- [ ] 确认 CLI 输出 `water_mass` 字段。 -- [ ] 确认 CLI 输出 `max_pressure` 字段。 -- [ ] 确认 CLI 输出 `dtcrit` 字段。 -- [ ] 确认 CLI 输出 `status` 字段。 -- [ ] 加固缺少输入文件的错误路径。 -- [ ] 加固输入文件不存在的错误路径。 -- [ ] 加固 `--run` 与 `--check` 同时给出的错误路径。 -- [ ] 加固 `--output` 缺少路径的错误路径。 -- [ ] 加固 Water 参数缺失的错误路径。 -- [ ] 加固输出目录非法的错误路径。 -- [ ] 确认 CLI 不绕过公共生命周期接口。 +- [x] 保持 `sphlite --version` 可用。 +- [x] 保持 `sphlite model.inp` inspect 模式可用。 +- [x] 保持 `sphlite model.inp --check` 模式可用。 +- [x] 保持 `sphlite model.inp --run` 模式可用。 +- [x] 保持 `sphlite model.inp --run --output output` 可用。 +- [x] 确认 CLI 输出 `version` 字段。 +- [x] 确认 CLI 输出 `input` 字段。 +- [x] 确认 CLI 输出 `mode` 字段。 +- [x] 确认 CLI 输出 `particles` 字段。 +- [x] 确认 CLI 输出 `warnings` 字段。 +- [x] 确认 CLI 输出 `time` 字段。 +- [x] 确认 CLI 输出 `step` 字段。 +- [x] 确认 CLI 输出 `water_particles` 字段。 +- [x] 确认 CLI 输出 `water_mass` 字段。 +- [x] 确认 CLI 输出 `max_pressure` 字段。 +- [x] 确认 CLI 输出 `dtcrit` 字段。 +- [x] 确认 CLI 输出 `status` 字段。 +- [x] 加固缺少输入文件的错误路径。 +- [x] 加固输入文件不存在的错误路径。 +- [x] 加固 `--run` 与 `--check` 同时给出的错误路径。 +- [x] 加固 `--output` 缺少路径的错误路径。 +- [x] 加固 Water 参数缺失的错误路径。 +- [x] 加固输出目录非法的错误路径。 +- [x] 确认 CLI 不绕过公共生命周期接口。 ## 阶段 Q:CouplingInterface 加固 -- [ ] 确认 Water 运行后 coupling particle count 可用。 -- [ ] 确认 Water 运行后 coupling particle state 导出可用。 -- [ ] 确认 coupling particle 导出包含 id。 -- [ ] 确认 coupling particle 导出包含 position。 -- [ ] 确认 coupling particle 导出包含 velocity。 -- [ ] 确认 coupling particle 导出包含 phase。 -- [ ] 确认 coupling particle 导出包含 material。 -- [ ] 确认 coupling particle 导出包含 pressure。 -- [ ] 确认外部粒子力导入接口可用。 -- [ ] 确认外部粒子力影响显式步。 -- [ ] 确认 coupling work 诊断继续记录。 +- [x] 确认 Water 运行后 coupling particle count 可用。 +- [x] 确认 Water 运行后 coupling particle state 导出可用。 +- [x] 确认 coupling particle 导出包含 id。 +- [x] 确认 coupling particle 导出包含 position。 +- [x] 确认 coupling particle 导出包含 velocity。 +- [x] 确认 coupling particle 导出包含 phase。 +- [x] 确认 coupling particle 导出包含 material。 +- [x] 确认 coupling particle 导出包含 pressure。 +- [x] 确认外部粒子力导入接口可用。 +- [x] 确认外部粒子力影响显式步。 +- [x] 确认 coupling work 诊断继续记录。 - [ ] 确认 coupling momentum 诊断继续记录。 - [ ] 确认 coupling energy 诊断继续记录。 - [ ] 保持接触耦合类型字段。 diff --git a/tools/testing/test_cli.py b/tools/testing/test_cli.py index d9f17ae..62719e7 100644 --- a/tools/testing/test_cli.py +++ b/tools/testing/test_cli.py @@ -72,14 +72,30 @@ def main() -> int: ): require_field(inspect.stdout, field) inspect_fields = read_fields(inspect.stdout) + if inspect_fields["version"] != "0.2.1": + raise AssertionError("inspect version field mismatch") + if Path(inspect_fields["input"]) != Path(model): + raise AssertionError("inspect input field mismatch") if inspect_fields["mode"] != "inspect": raise AssertionError("inspect mode not reported") + if int(inspect_fields["particles"]) != 4: + raise AssertionError("CLI particle count mismatch") + if int(inspect_fields["warnings"]) != 0: + raise AssertionError("CLI warning count mismatch") + if abs(float(inspect_fields["time"])) > 1.0e-15: + raise AssertionError("inspect time should be zero") + if int(inspect_fields["step"]) != 0: + raise AssertionError("inspect step should be zero") if int(inspect_fields["water_particles"]) != 4: raise AssertionError("CLI water particle count mismatch") if float(inspect_fields["water_mass"]) <= 0.0: raise AssertionError("CLI water mass mismatch") + if float(inspect_fields["max_pressure"]) < 0.0: + raise AssertionError("CLI max pressure mismatch") if float(inspect_fields["dtcrit"]) <= 0.0: raise AssertionError("CLI dtcrit mismatch") + if inspect_fields["status"] != "ok": + raise AssertionError("CLI status field mismatch") check = run_command([exe, model, "--check"]) if "mode: check" not in check.stdout: @@ -89,6 +105,8 @@ def main() -> int: run_fields = read_fields(run.stdout) if run_fields["mode"] != "run": raise AssertionError("run mode not reported") + if float(run_fields["time"]) <= 0.0: + raise AssertionError("run mode time did not advance") if int(run_fields["step"]) <= 0: raise AssertionError("run mode did not advance") output_step = int(run_fields["step"]) @@ -105,6 +123,22 @@ def main() -> int: bad = run_command([exe, bad_model, "--check"], expect_ok=False) require_field(bad.stderr, "error_code") require_field(bad.stderr, "error_message") + missing_arg = run_command([exe], expect_ok=False) + if "usage:" not in missing_arg.stdout: + raise AssertionError("missing input did not print usage") + missing_file = run_command([exe, str(Path(output_dir) / "missing.inp")], expect_ok=False) + require_field(missing_file.stderr, "error_code") + conflict = run_command([exe, model, "--run", "--check"], expect_ok=False) + if "usage:" not in conflict.stdout: + raise AssertionError("conflicting modes did not print usage") + missing_output = run_command([exe, model, "--run", "--output"], expect_ok=False) + if "usage:" not in missing_output.stdout: + raise AssertionError("missing output path did not print usage") + illegal_output = run_command( + [exe, model, "--run", "--output", str(Path(output_dir) / "missing-dir" / "nested")], + expect_ok=False, + ) + require_field(illegal_output.stderr, "error_code") return 0 diff --git a/tools/testing/test_python_debug.py b/tools/testing/test_python_debug.py index 37f03ca..9a934b9 100644 --- a/tools/testing/test_python_debug.py +++ b/tools/testing/test_python_debug.py @@ -16,6 +16,12 @@ def main() -> int: from sphlite_debug import PHASE_SOLID, PHASE_WATER, STATUS_ACTIVE, SPHLite, SPHLiteError + wrapper_source = (root / "python" / "sphlite_debug.py").read_text(encoding="utf-8") + if "ctypes.CDLL" not in wrapper_source: + raise AssertionError("Python wrapper is not loading the C ABI library") + if "sphft_" in wrapper_source: + raise AssertionError("Python wrapper directly references Fortran symbols") + solver = SPHLite(args.library) if PHASE_SOLID != 4 or STATUS_ACTIVE != 0: raise AssertionError("Python constants are not locked") @@ -37,6 +43,8 @@ def main() -> int: solid_summary = solver.inspect(args.input) if solid_summary["particles"] != 4: raise AssertionError("solid inspect summary mismatch") + if "water_mass" not in solid_summary or "dt_cfl" not in solid_summary: + raise AssertionError("inspect helper did not include Water diagnostics") solver.set_output_directory(root / "build-codex-unix") solver.write_output(0, 0.0) restart = root / "build-codex-unix" / "python_restart.tmp" @@ -67,6 +75,8 @@ def main() -> int: if water_state["phase"][0] != PHASE_WATER: raise AssertionError("unexpected water particle phase") run_summary = water.run(water_input) + if run_summary["particles"] != 4: + raise AssertionError("water run particle count mismatch") if run_summary["water_mass"] <= 0.0 or run_summary["dt_cfl"] <= 0.0: raise AssertionError("water run summary mismatch") finally: