Skip to content

Commit 3fbb60f

Browse files
Rusty Conoverclaude
andcommitted
codegen: ts_types -- typed builders and codecs for TS protocol records
New vgi.codegen.ts_types emits src/generated/vgi-protocol-types.ts for vgi-typescript. For each record in TS_RECORD_TYPES (CatalogAttachResult, SchemaContents, CatalogContentsResponse; append to extend) it derives, from the record ARROW_SCHEMA and its dataclass defaults: - the interface, or a re-export of the one ts_client already emits (checked to have the same fields, order and nullability, so each shape is defined once); SchemaContents rides as a binary blob no method reaches, so it is emitted in full; - <Name>Init (defaulted fields optional) and build<Name>(), which fills the Python defaults in wire order -- so an appended column such as supports_catalog_contents needs no hand-written default in the SDK; - encode<Name>/decode<Name> over the generated schema const. Registered in scripts/regen_generated.py; tests/test_generated_ts_types.py checks determinism, builder field order vs ARROW_SCHEMA, and drift against the vgi-typescript checkout (VGI_TS_GENERATED_TYPES overrides the path). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent a8794e4 commit 3fbb60f

3 files changed

Lines changed: 456 additions & 0 deletions

File tree

‎scripts/regen_generated.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
("vgi.codegen.rust_types", "vgi-protocol/src/generated/protocol_types.rs", "vgi-rust"),
5858
("vgi.codegen.ts_schemas", "src/generated/vgi-protocol-schemas.ts", "vgi-typescript"),
5959
("vgi.codegen.ts_client", "src/generated/vgi-client.ts", "vgi-typescript"),
60+
("vgi.codegen.ts_types", "src/generated/vgi-protocol-types.ts", "vgi-typescript"),
6061
("vgi.codegen.csharp_types", "src/QueryFarm.Vgi/Protocol/Generated/VgiProtocolTypes.g.cs", "vgi-csharp"),
6162
(
6263
"vgi.codegen.csharp_schemas",

‎tests/test_generated_ts_types.py‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)