fix(test): compare native Python delivery generations without rewriting goldens

This commit is contained in:
Tianyi Cui committed 2026-09-16 13:02:13 +08:00
1 parent d3dbcfe710
commit 9c10de50a6
5 files changed
+201 -7

No files matched your search

+58 -5
View File
@@ -1413,6 +1413,7 @@ def smoke_sdk_snapshot(base_url: str, executable: Path, update_snapshots: bool)
files = build_snapshot_files(result, logs, child_ids, root)
compare_snapshot_files(
files, update_snapshots, ADVANCED_SNAPSHOT_DIRECTORY, ADVANCED_SNAPSHOT_FILENAMES,
native_writer_output=True,
)
@@ -1479,6 +1480,7 @@ def smoke_sdk_restart_snapshot(base_url: str, executable: Path, update_snapshots
)
compare_snapshot_files(
files, update_snapshots, RESTART_SNAPSHOT_DIRECTORY, RESTART_SNAPSHOT_FILENAMES,
native_writer_output=True,
)
@@ -2282,6 +2284,7 @@ def project_session_snapshot(records: list[dict[str, object]]) -> list[dict[str,
SESSION_FORMAT_TOKEN = "{{sessionFormatVersion}}"
NATIVE_DELIVERY_FORMAT_TOKEN = "{{sourceSessionFormatVersion}}"
def expand_snapshot_stream_member(member: object) -> list[dict[str, object]]:
@@ -2404,7 +2407,31 @@ def normalize_session_format_comparison(
return normalized
def normalize_snapshot_comparison_text(name: str, content: str) -> str:
def normalize_native_delivery_record(value: object, source_version: int) -> object:
"""Tokenize a native delivery qualifier in one Session event or SDK notification."""
if not isinstance(value, dict):
return value
if value.get("method") == "session.event":
for key in ("payload", "params"):
wrapper = value.get(key)
if isinstance(wrapper, dict) and "event" in wrapper:
return {**value, key: {
**wrapper,
"event": normalize_native_delivery_record(wrapper["event"], source_version),
}}
data = value.get("data")
if value.get("type") != "session-log-deepseek/delivery-accepted" or not isinstance(data, dict):
return value
if data.get("sessionFormatVersion") != source_version:
return value
return {**value, "data": {**data, "sessionFormatVersion": NATIVE_DELIVERY_FORMAT_TOKEN}}
def normalize_snapshot_comparison_text(
name: str,
content: str,
native_writer_version: int | None = None,
) -> str:
"""Normalize Session generation metadata only while comparing committed expected outputs."""
if name.startswith("session") and name.endswith(".jsonl"):
parsed = [json.loads(line) for line in content.splitlines() if line]
@@ -2412,6 +2439,8 @@ def normalize_snapshot_comparison_text(name: str, content: str) -> str:
source_version = header.get("version") if isinstance(header, dict) else None
if not isinstance(source_version, int):
raise AssertionError(f"{name}: snapshot Session header has no integer format version")
if native_writer_version is not None:
parsed = [normalize_native_delivery_record(record, native_writer_version) for record in parsed]
records = [
normalize_session_format_comparison(expanded, source_version)
for record in parsed
@@ -2419,8 +2448,18 @@ def normalize_snapshot_comparison_text(name: str, content: str) -> str:
]
return render_jsonl(records)
if name.endswith(".json"):
value = json.loads(content)
if name == "result.json" and native_writer_version is not None and isinstance(value, dict):
value = {
**value,
**{
key: [normalize_native_delivery_record(record, native_writer_version) for record in value[key]]
for key in ("events", "notifications")
if isinstance(value.get(key), list)
},
}
return json.dumps(
normalize_session_format_comparison(json.loads(content)),
normalize_session_format_comparison(value),
indent=2,
ensure_ascii=False,
) + "\n"
@@ -2432,8 +2471,10 @@ def compare_snapshot_files(
update: bool,
directory: Path,
filenames: tuple[str, ...],
*,
native_writer_output: bool = False,
) -> None:
"""Compare ordered artifact roles and Session content across generations, or write generated filenames."""
"""Compare artifact roles and content, optionally matching each side's native delivery generation."""
scenario = directory.name
def role_name(name: str) -> str:
@@ -2485,12 +2526,24 @@ def compare_snapshot_files(
f"{scenario} snapshot Session roles differ: "
f"expected={sorted(selected_expected)}, actual={sorted(actual_sessions)}",
)
actual_native_version = expected_native_version = None
if native_writer_output:
def common_generation(contents: list[tuple[str, str]]) -> int:
versions = {session_header_version(content, name) for name, content in contents}
if len(versions) != 1:
raise AssertionError(f"{scenario}: native writer comparison requires one Session generation across all roles")
return versions.pop()
actual_native_version = common_generation(list(actual_sessions.values()))
expected_native_version = common_generation([
(path.name, path.read_text(encoding="utf-8")) for path in selected_expected.values()
])
for name, actual in files.items():
parsed = parse_snapshot_session_filename(name)
expected_path = directory / name if parsed is None else selected_expected[parsed[0]]
expected_text = expected_path.read_text(encoding="utf-8")
compared_actual = normalize_snapshot_comparison_text(name, actual)
compared_expected = normalize_snapshot_comparison_text(expected_path.name, expected_text)
compared_actual = normalize_snapshot_comparison_text(name, actual, actual_native_version)
compared_expected = normalize_snapshot_comparison_text(expected_path.name, expected_text, expected_native_version)
if compared_actual == compared_expected:
continue
diff = "".join(difflib.unified_diff(