|
| 1 | +# Copyright 2025, 2026 Query Farm LLC - https://query.farm |
| 2 | + |
| 3 | +"""Drift + behavior tests for `vgi.codegen.ts_types`. |
| 4 | +
|
| 5 | +Enforces that `vgi-typescript/src/generated/vgi-protocol-types.ts` in the |
| 6 | +sibling repo matches what the generator would emit right now. Skipped when the |
| 7 | +`vgi-typescript` checkout is not next to vgi-python; ``VGI_TS_GENERATED_TYPES`` |
| 8 | +overrides the file's location. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import io |
| 14 | +import os |
| 15 | +import re |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from vgi.codegen import ts_types |
| 21 | +from vgi.codegen.ts_types import TS_RECORD_TYPES, build_model, emit |
| 22 | + |
| 23 | + |
| 24 | +def _vgi_ts_types_path() -> Path: |
| 25 | + override = os.environ.get("VGI_TS_GENERATED_TYPES") |
| 26 | + if override: |
| 27 | + return Path(override) |
| 28 | + return Path(__file__).resolve().parents[2] / "vgi-typescript" / "src" / "generated" / "vgi-protocol-types.ts" |
| 29 | + |
| 30 | + |
| 31 | +_REGEN_HINT = ( |
| 32 | + "To regenerate, run:\n" |
| 33 | + " uv run --project ~/Development/vgi-python python scripts/regen_generated.py\n" |
| 34 | + "\n" |
| 35 | + "Do not redirect a generator into its destination with `>`: the shell\n" |
| 36 | + "truncates the file before the generator runs, so any failure destroys\n" |
| 37 | + "the checked-in artifact. The script renders to memory first." |
| 38 | +) |
| 39 | + |
| 40 | + |
| 41 | +def _render() -> str: |
| 42 | + buf = io.StringIO() |
| 43 | + emit(buf) |
| 44 | + return buf.getvalue() |
| 45 | + |
| 46 | + |
| 47 | +def test_generator_is_deterministic() -> None: |
| 48 | + """Calling emit() twice produces byte-identical output.""" |
| 49 | + assert _render() == _render(), "ts_types generator is non-deterministic" |
| 50 | + |
| 51 | + |
| 52 | +def test_every_record_gets_builder_and_codec() -> None: |
| 53 | + """Each configured record has an Init type, a builder and an encode/decode pair.""" |
| 54 | + text = _render() |
| 55 | + for cls in TS_RECORD_TYPES: |
| 56 | + name = cls.__name__ |
| 57 | + assert f"export type {name}Init = " in text |
| 58 | + assert f"export function build{name}(init: {name}Init): {name} {{" in text |
| 59 | + assert f"export const encode{name} = " in text |
| 60 | + assert f"export const decode{name} = " in text |
| 61 | + |
| 62 | + |
| 63 | +def test_builder_fields_follow_arrow_schema_order() -> None: |
| 64 | + """A builder lists every ARROW_SCHEMA column, in wire order, with Python's defaults.""" |
| 65 | + text = _render() |
| 66 | + for cls in TS_RECORD_TYPES: |
| 67 | + name = cls.__name__ |
| 68 | + body = re.search(rf"export function build{name}\(.*?\n return \{{\n(.*?)\n \}};", text, re.DOTALL) |
| 69 | + assert body is not None, name |
| 70 | + names = [line.strip().split(":", 1)[0] for line in body.group(1).splitlines()] |
| 71 | + assert names == list(cls.ARROW_SCHEMA.names), name # type: ignore[attr-defined] |
| 72 | + assert " supports_catalog_contents: init.supports_catalog_contents ?? false," in text |
| 73 | + assert ' default_schema: init.default_schema ?? "main",' in text |
| 74 | + |
| 75 | + |
| 76 | +def test_blob_records_are_emitted_and_client_records_reexported() -> None: |
| 77 | + """SchemaContents (a blob no method reaches) is emitted; ts_client's records are re-exported.""" |
| 78 | + by_name = {r.name: r for r in build_model()} |
| 79 | + assert not by_name["SchemaContents"].reexported |
| 80 | + assert by_name["CatalogAttachResult"].reexported |
| 81 | + assert by_name["CatalogContentsResponse"].schema_const == "CatalogContentsResultSchema" |
| 82 | + text = _render() |
| 83 | + assert "export interface SchemaContents {" in text |
| 84 | + assert "export interface CatalogAttachResult {" not in text |
| 85 | + |
| 86 | + |
| 87 | +def test_unsupported_type_raises() -> None: |
| 88 | + """An Arrow type with no mapping fails loudly instead of emitting a wrong type.""" |
| 89 | + import pyarrow as pa |
| 90 | + |
| 91 | + with pytest.raises(ts_types.GeneratorError): |
| 92 | + ts_types._ts_type(pa.struct([pa.field("a", pa.int32())]), "X.a") |
| 93 | + |
| 94 | + |
| 95 | +def test_checked_in_types_match_generator() -> None: |
| 96 | + """Checked-in vgi-protocol-types.ts matches what the generator would emit right now.""" |
| 97 | + path = _vgi_ts_types_path() |
| 98 | + if not path.exists(): |
| 99 | + pytest.skip(f"{path} not found; set VGI_TS_GENERATED_TYPES or check out vgi-typescript next to vgi-python") |
| 100 | + assert path.read_text() == _render(), f"checked-in {path.name} differs from generator output.\n{_REGEN_HINT}" |
0 commit comments