|
| 1 | +"""Drift + determinism tests for `vgi.codegen.cpp_request_builders`. |
| 2 | +
|
| 3 | +Sibling of ``test_generated_cpp_schemas.py``. Enforces that |
| 4 | +``vgi/src/generated/vgi_request_builders.hpp`` in the sibling ``vgi`` repo |
| 5 | +matches what the generator would emit right now. When it fails, the error |
| 6 | +message prints the regeneration command. |
| 7 | +
|
| 8 | +If the ``vgi`` repo isn't present next to vgi-python, the drift test is |
| 9 | +skipped (the determinism test still runs — it needs no external repo). |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import io |
| 15 | +import os |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from vgi.codegen.cpp_request_builders import emit |
| 21 | + |
| 22 | + |
| 23 | +def _vgi_generated_path() -> Path: |
| 24 | + """Locate ``vgi/src/generated/vgi_request_builders.hpp`` next to this repo.""" |
| 25 | + override = os.environ.get("VGI_REQUEST_BUILDERS_HPP") |
| 26 | + if override: |
| 27 | + return Path(override) |
| 28 | + return Path(__file__).resolve().parents[2] / "vgi" / "src" / "generated" / "vgi_request_builders.hpp" |
| 29 | + |
| 30 | + |
| 31 | +_REGEN_HINT = ( |
| 32 | + "To regenerate, run:\n" |
| 33 | + " uv run --project ~/Development/vgi-python python -m vgi.codegen.cpp_request_builders \\\n" |
| 34 | + " > ~/Development/vgi/src/generated/vgi_request_builders.hpp" |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +def test_generator_is_deterministic() -> None: |
| 39 | + """Running the generator twice produces byte-identical output.""" |
| 40 | + out1 = io.StringIO() |
| 41 | + emit(out1) |
| 42 | + out2 = io.StringIO() |
| 43 | + emit(out2) |
| 44 | + assert out1.getvalue() == out2.getvalue(), ( |
| 45 | + "generator is non-deterministic — sorting or collection order is unstable" |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def test_checked_in_generated_hpp_matches_generator() -> None: |
| 50 | + """The .hpp checked into the vgi repo must match the current generator output.""" |
| 51 | + path = _vgi_generated_path() |
| 52 | + if not path.exists(): |
| 53 | + pytest.skip(f"{path} not found; set VGI_REQUEST_BUILDERS_HPP or check out vgi repo next to vgi-python") |
| 54 | + |
| 55 | + expected = io.StringIO() |
| 56 | + emit(expected) |
| 57 | + actual = path.read_text() |
| 58 | + |
| 59 | + if expected.getvalue() != actual: |
| 60 | + # Identify the first divergence to make the failure scannable. |
| 61 | + exp_lines = expected.getvalue().splitlines() |
| 62 | + act_lines = actual.splitlines() |
| 63 | + diff_index = next( |
| 64 | + (i for i, (e, a) in enumerate(zip(exp_lines, act_lines, strict=False)) if e != a), |
| 65 | + min(len(exp_lines), len(act_lines)), |
| 66 | + ) |
| 67 | + context = "\n".join( |
| 68 | + f" {tag} {line}" |
| 69 | + for tag, line in ( |
| 70 | + ("-", act_lines[diff_index] if diff_index < len(act_lines) else "(EOF)"), |
| 71 | + ("+", exp_lines[diff_index] if diff_index < len(exp_lines) else "(EOF)"), |
| 72 | + ) |
| 73 | + ) |
| 74 | + raise AssertionError( |
| 75 | + f"{path} differs from generator output starting at line {diff_index + 1}:\n{context}\n{_REGEN_HINT}", |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +def test_emits_at_least_a_few_builders() -> None: |
| 80 | + """Sanity check — generator should produce a non-trivial number of inline definitions. |
| 81 | +
|
| 82 | + Guards against a future refactor that accidentally turns the generator into a no-op. |
| 83 | + The exact count drifts as the protocol evolves, so we only assert "at least 30". |
| 84 | + """ |
| 85 | + out = io.StringIO() |
| 86 | + emit(out) |
| 87 | + text = out.getvalue() |
| 88 | + count = text.count("\ninline std::shared_ptr<arrow::RecordBatch> Build") |
| 89 | + assert count >= 30, f"generator emitted only {count} builders — expected at least 30" |
0 commit comments