# Copyright 2024 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Conformance runner for node_resolution.yaml test cases.""" from __future__ import annotations from collections.abc import Mapping import copy from dataclasses import dataclass import inspect import json from pathlib import Path from typing import Any, Callable import yaml from a2ui.core.catalog import Catalog, FunctionImplementation from a2ui.core.processing import MessageProcessor from a2ui.core.resolution import ( ComponentNode, NodeResolver, ResolvedBinding, is_component_node, is_writable, ) from a2ui.core.state import ComponentModel, SurfaceModel _CASE_KEYS = frozenset({ "name", "description", "features", "action", "fixture", "expect", "steps", }) _FIXTURE_KEYS = frozenset({ "catalog", "components", "data", "surface", }) _EXPECT_KEYS = frozenset({ "created_emissions", "nodes", "emissions", "metadata_emissions", "destroyed", "same_nodes", "replaced_nodes", "data", "events", "functions", "errors", }) _NODE_FIELD_KEYS = frozenset({ "component_id", "type", "state", "data_path", "metadata", "isValid", "validationErrors", "validationResults", "props", }) _STEP_KEYS_BY_OP: dict[str, frozenset[str]] = { "set_data": frozenset({"op", "path", "value", "expect"}), "update_components": frozenset({"op", "components", "expect"}), "remove_component": frozenset({"op", "component_id", "expect"}), "process_messages": frozenset({"op", "messages", "expect"}), "write": frozenset({"op", "node", "property", "value", "expect"}), "invoke": frozenset({"op", "node", "property", "metadata", "expect"}), "dispose": frozenset({"op", "expect"}), } class UnknownConformanceKeyError(AssertionError): """Raised when a conformance YAML dictionary contains an unrecognized key.""" def _assert_known_keys( obj: Mapping[str, Any], allowed: frozenset[str], where: str, context: str, ) -> None: for key in obj: if key not in allowed: raise UnknownConformanceKeyError(f"Unknown {where} '{key}' in {context}") @dataclass class _Watch: path: str emissions: int = 0 metadata_emissions: int = 0 destroyed: int = 0 last_props: Any = None last_metadata: Any = None stop: Callable[[], None] = lambda: None def _normalize(value: Any, path: str) -> Any: if is_component_node(value): return {"node": path} if isinstance(value, ResolvedBinding): return { "value": _normalize(value.value, path), "writable": is_writable(value), } if callable(value): return {"action": True} if isinstance(value, list): return [_normalize(item, f"{path}/{i}") for i, item in enumerate(value)] if isinstance(value, dict): return {k: _normalize(v, f"{path}/{k}") for k, v in value.items()} return value def _mounted_nodes(resolver: NodeResolver) -> dict[str, ComponentNode]: nodes: dict[str, ComponentNode] = {} seen: set[ComponentNode] = set() def visit(node: ComponentNode, path: str) -> None: assert ( node not in seen ), f"{path}: each mounted position must have a distinct node" assert not node.disposed, f"{path}: mounted node is disposed" seen.add(node) nodes[path] = node sibling_ids: set[str] = set() def children(val: Any, child_path: str) -> None: if is_component_node(val): assert ( val.instance_id not in sibling_ids ), f"{child_path}: duplicate sibling instance_id" sibling_ids.add(val.instance_id) visit(val, child_path) elif isinstance(val, list): for i, item in enumerate(val): children(item, f"{child_path}/{i}") elif isinstance(val, dict) and not isinstance(val, ResolvedBinding): for k, item in val.items(): children(item, f"{child_path}/{k}") children(node.props, path) if resolver.root is not None: visit(resolver.root, "root") return nodes def _required_node(nodes: dict[str, ComponentNode], path: str) -> ComponentNode: assert path in nodes, f"No node at {path}" return nodes[path] def _check_nodes( nodes: dict[str, ComponentNode], expected: dict[str, Any], reason: str, ) -> None: expected_nodes = expected.get("nodes") or {} assert sorted(nodes.keys()) == sorted( expected_nodes.keys() ), f"{reason}: node paths {sorted(nodes.keys())} != {sorted(expected_nodes.keys())}" for path, fields in expected_nodes.items(): _assert_known_keys(fields, _NODE_FIELD_KEYS, f"node '{path}' key", reason) node = _required_node(nodes, path) actual = { "component_id": node.component_id, "type": node.type, "state": node.state, "data_path": node.data_path, "metadata": node.metadata, "isValid": node.is_valid, "validationErrors": node.validation_errors, "validationResults": node.validation_results, } for key in ( "component_id", "type", "state", "data_path", "metadata", "isValid", "validationErrors", "validationResults", ): if key in fields: assert actual[key] == fields[key], ( f"{reason}: {path}.{key} expected {fields[key]!r}, got" f" {actual[key]!r}" ) for key, expected_val in (fields.get("props") or {}).items(): assert key in node.props, f"{reason}: {path}.{key} is absent" norm_actual = _normalize(node.props[key], f"{path}/{key}") assert ( norm_actual == expected_val ), f"{reason}: {path}.{key} expected {expected_val!r}, got {norm_actual!r}" def _update_components( surface: SurfaceModel, catalog: Catalog, components: list[dict[str, Any]], ) -> None: for comp in components: comp_id = comp["id"] comp_type = comp["component"] metadata = copy.deepcopy(comp["metadata"]) if "metadata" in comp else None properties = { k: v for k, v in comp.items() if k not in ("id", "component", "metadata") } existing = surface.components_model.get(comp_id) if existing is not None: assert ( existing.type == comp_type ), f"update_components cannot change {comp_id}'s type" existing.apply_update(properties, metadata) else: surface.components_model.add_component( ComponentModel( comp_id, comp_type, catalog, properties, metadata=metadata ) ) def _save_bindings( nodes: dict[str, ComponentNode], ) -> list[tuple[ResolvedBinding[Any], Any]]: saved: list[tuple[ResolvedBinding[Any], Any]] = [] def visit(val: Any) -> None: if isinstance(val, ResolvedBinding): saved.append((val, _normalize(val.value, ""))) elif isinstance(val, list): for item in val: visit(item) elif isinstance(val, dict) and not is_component_node(val): for item in val.values(): visit(item) for node in nodes.values(): visit(node.props) return saved def _build_recording_catalog( catalog_json_path: Path, functions_log: list[dict[str, Any]] ) -> Catalog: raw_json = json.loads(catalog_json_path.read_text(encoding="utf-8")) parsed = Catalog.from_json(raw_json) wrapped_functions: dict[str, Any] = {} for fn_name, fn_api in (parsed.functions or {}).items(): def make_impl(name: str) -> Callable[..., Any]: def _impl(args: dict[str, Any], _ctx: Any = None, _sig: Any = None) -> Any: functions_log.append({"name": name, "args": _normalize(args, "")}) return None return _impl wrapped_functions[fn_name] = FunctionImplementation( name=fn_api.name, description=getattr(fn_api, "description", None), return_type=getattr(fn_api, "return_type", "any"), schema=getattr(fn_api, "schema", None), execute=make_impl(fn_name), ) parsed.functions = wrapped_functions return parsed def _watch_nodes( nodes: dict[str, ComponentNode], watches: dict[ComponentNode, _Watch], on_props_updated: Callable[[], None] | None = None, ) -> None: for path, node in nodes.items(): existing = watches.get(node) if existing is not None: existing.path = path continue w = _Watch(path=path) def on_props(props: dict[str, Any], watch: _Watch = w) -> None: watch.emissions += 1 watch.last_props = _normalize(props, watch.path) if on_props_updated is not None: on_props_updated() def on_meta(meta: dict[str, Any] | None, watch: _Watch = w) -> None: watch.metadata_emissions += 1 watch.last_metadata = copy.deepcopy(meta) def on_destroy(_: Any, watch: _Watch = w) -> None: watch.destroyed += 1 sub_props = node.on_updated.subscribe(on_props) sub_meta = node.on_metadata_changed.subscribe(on_meta) sub_destroy = node.on_destroyed.subscribe(on_destroy) w.stop = lambda sp=sub_props, sm=sub_meta, sd=sub_destroy: ( sp.unsubscribe(), sm.unsubscribe(), sd.unsubscribe(), ) watches[node] = w def _watch_created_nodes( resolver: NodeResolver | None, created_emissions: dict[ComponentNode, int], created_stops: list[Callable[[], None]], ) -> None: if resolver is None: return for node in _mounted_nodes(resolver).values(): if node in created_emissions: continue created_emissions[node] = 0 def on_created_props(_props: dict[str, Any], n: ComponentNode = node) -> None: created_emissions[n] = created_emissions.get(n, 0) + 1 _watch_created_nodes(resolver, created_emissions, created_stops) sub = node.on_updated.subscribe(on_created_props) created_stops.append(sub.unsubscribe) def _collect_watch_counts( watches: dict[ComponentNode, _Watch], surviving: set[ComponentNode], reason: str, ) -> tuple[dict[str, int], dict[str, int], dict[str, int]]: emissions: dict[str, int] = {} metadata_emissions: dict[str, int] = {} destroyed: dict[str, int] = {} for node, w in watches.items(): if w.emissions and node in surviving: emissions[w.path] = emissions.get(w.path, 0) + w.emissions assert w.last_props == _normalize( node.props, w.path ), f"{reason}: {w.path} last emission" if w.metadata_emissions and node in surviving: metadata_emissions[w.path] = ( metadata_emissions.get(w.path, 0) + w.metadata_emissions ) assert ( w.last_metadata == node.metadata ), f"{reason}: {w.path} last metadata emission" if w.destroyed: destroyed[w.path] = destroyed.get(w.path, 0) + w.destroyed assert node.disposed, f"{reason}: {w.path} destruction without disposal" return emissions, metadata_emissions, destroyed def _check_errors( errors: list[dict[str, Any]], expected: dict[str, Any], reason: str ) -> None: if "errors" not in expected: return exp_errors = expected["errors"] or [] assert len(errors) == len( exp_errors ), f"{reason}: errors count expected {len(exp_errors)}, got {len(errors)}: {errors}" for actual_err, exp_err in zip(errors, exp_errors): for k, v in exp_err.items(): assert ( actual_err.get(k) == v ), f"{reason}: error.{k} expected {v!r}, got {actual_err.get(k)!r}" def _check_node_identity( before: dict[str, ComponentNode], after: dict[str, ComponentNode], expected: dict[str, Any], reason: str, ) -> None: for path in expected.get("same_nodes") or (): assert _required_node(after, path) is _required_node( before, path ), f"{reason}: {path} identity" for path in expected.get("replaced_nodes") or (): assert _required_node(after, path) is not _required_node( before, path ), f"{reason}: {path} replacement" def _check_step_state( resolver: NodeResolver | None, surface: SurfaceModel | None, watches: dict[ComponentNode, _Watch], created_emissions: dict[ComponentNode, int], events: list[dict[str, Any]], functions: list[dict[str, Any]], errors: list[dict[str, Any]], expected: dict[str, Any], before: dict[str, ComponentNode], reason: str, ) -> None: _assert_known_keys(expected, _EXPECT_KEYS, "expect key", reason) after = _mounted_nodes(resolver) if resolver is not None else {} _check_nodes(after, expected, reason) before_nodes = set(before.values()) emissions, metadata_emissions, destroyed = _collect_watch_counts( watches, set(after.values()), reason ) exp_emissions = expected.get("emissions") or {} assert ( emissions == exp_emissions ), f"{reason}: emissions expected {exp_emissions}, got {emissions}" if "created_emissions" in expected: actual_created = { path: created_emissions.get(node, 0) for path, node in after.items() if node not in before_nodes } exp_created = expected.get("created_emissions") or {} assert ( actual_created == exp_created ), f"{reason}: created_emissions expected {exp_created}, got {actual_created}" if "metadata_emissions" in expected: exp_meta = expected.get("metadata_emissions") or {} assert ( metadata_emissions == exp_meta ), f"{reason}: metadata_emissions expected {exp_meta}, got {metadata_emissions}" exp_destroyed = expected.get("destroyed") or {} assert ( destroyed == exp_destroyed ), f"{reason}: destroyed expected {exp_destroyed}, got {destroyed}" _check_node_identity(before, after, expected, reason) for path, val in (expected.get("data") or {}).items(): assert surface is not None, f"{reason}: no surface for data check {path}" assert ( _normalize(surface.data_model.get(path), path) == val ), f"{reason}: data {path}" exp_events = expected.get("events") or [] assert events == exp_events, f"{reason}: events expected {exp_events}, got {events}" exp_functions = expected.get("functions") or [] assert ( functions == exp_functions ), f"{reason}: functions expected {exp_functions}, got {functions}" _check_errors(errors, expected, reason) def _execute_step_op( step: dict[str, Any], before: dict[str, ComponentNode], surface: SurfaceModel | None, catalog: Catalog, processor: MessageProcessor, resolver: NodeResolver | None, reason: str, ) -> None: op = step["op"] allowed_step_keys = _STEP_KEYS_BY_OP.get(op) if allowed_step_keys is None: raise AssertionError(f"Unknown node operation: {op}") _assert_known_keys(step, allowed_step_keys, f"step ({op}) key", reason) if op == "set_data": assert surface is not None, f"{reason}: no surface" surface.data_model.set(step["path"], step["value"]) elif op == "update_components": assert surface is not None, f"{reason}: no surface" _update_components(surface, catalog, step["components"]) elif op == "remove_component": assert surface is not None, f"{reason}: no surface" surface.components_model.remove_component(step["component_id"]) elif op == "process_messages": processor.process_messages(step["messages"]) elif op == "write": binding = _required_node(before, step["node"]).props.get(step["property"]) assert isinstance(binding, ResolvedBinding) and is_writable( binding ), f"{reason}: not a writable binding" binding.set(step["value"]) elif op == "invoke": action = _required_node(before, step["node"]).props.get(step["property"]) assert callable(action), f"{reason}: not an action" raw_meta = copy.deepcopy(step["metadata"]) if "metadata" in step else None res = action(metadata=raw_meta) if raw_meta is not None else action() if inspect.isawaitable(res): import asyncio asyncio.run(res) elif op == "dispose": assert resolver is not None, f"{reason}: no resolver" resolver.dispose() def run_node_resolution_case(test_case: dict[str, Any], conformance_root: Path) -> None: """Executes a single resolve_nodes conformance test case.""" case_context = f"core/node_resolution.yaml#{test_case.get('name', '')}" _assert_known_keys(test_case, _CASE_KEYS, "case key", case_context) assert test_case.get("action") == "resolve_nodes" fixture_path = conformance_root / test_case["fixture"] fixture = yaml.safe_load(fixture_path.read_text(encoding="utf-8")) _assert_known_keys(fixture, _FIXTURE_KEYS, "fixture key", case_context) functions: list[dict[str, Any]] = [] events: list[dict[str, Any]] = [] errors: list[dict[str, Any]] = [] catalog = _build_recording_catalog(conformance_root / fixture["catalog"], functions) processor = MessageProcessor([catalog]) watches: dict[ComponentNode, _Watch] = {} created_emissions: dict[ComponentNode, int] = {} created_stops: list[Callable[[], None]] = [] surface: SurfaceModel | None = None resolver: NodeResolver | None = None cleanup_callbacks: list[Callable[[], None]] = [] def attach_surface(s: SurfaceModel) -> None: nonlocal surface, resolver if s.id != "s" or resolver is not None: return surface = s resolver = NodeResolver(s, catalog) r = resolver sub_root = r.on_root_replaced.subscribe( lambda _: _watch_created_nodes(r, created_emissions, created_stops) ) sub_actions = s.on_action.subscribe( lambda action: events.append({ "name": action.get("name"), "source_component_id": action.get("sourceComponentId"), "context": _normalize(action.get("context") or {}, ""), **( {"metadata": copy.deepcopy(action["metadata"])} if action.get("metadata") is not None else {} ), }) ) sub_errors = s.on_error.subscribe(lambda err: errors.append(dict(err))) cleanup_callbacks.extend([ sub_root.unsubscribe, sub_actions.unsubscribe, sub_errors.unsubscribe, ]) sub_surface_created = processor.model.on_surface_created.subscribe(attach_surface) cleanup_callbacks.append(sub_surface_created.unsubscribe) if fixture.get("surface", True): seeded = SurfaceModel( surface_id="s", default_catalog=catalog, available_catalogs={catalog.catalog_id: catalog}, protocol_version=catalog.protocol_version, ) seeded.data_model.set("/", fixture.get("data") or {}) _update_components(seeded, catalog, fixture.get("components") or []) processor.model.add_surface(seeded) try: _check_step_state( resolver, surface, watches, created_emissions, events, functions, errors, test_case["expect"], {}, f"{test_case['name']} initial", ) for index, step in enumerate(test_case.get("steps") or []): op = step["op"] reason = f"{test_case['name']} step {index} ({op})" before = _mounted_nodes(resolver) if resolver is not None else {} _watch_nodes( before, watches, lambda: _watch_created_nodes( resolver, created_emissions, created_stops ), ) for w in watches.values(): w.emissions = 0 w.metadata_emissions = 0 w.destroyed = 0 w.last_props = None w.last_metadata = None for stop in created_stops: stop() created_stops.clear() created_emissions.clear() events.clear() functions.clear() errors.clear() bindings = _save_bindings(before) _execute_step_op( step, before, surface, catalog, processor, resolver, reason ) _check_step_state( resolver, surface, watches, created_emissions, events, functions, errors, step["expect"], before, reason, ) for binding, saved_val in bindings: assert ( _normalize(binding.value, "") == saved_val ), f"{reason}: prior binding snapshot changed" finally: for stop in created_stops: stop() if resolver is not None: resolver.dispose() if surface is not None: surface.dispose() for w in watches.values(): w.stop() for cb in cleanup_callbacks: cb()