Files
UniversalHumanoid/tests/test_layout.py
T
2026-11-03 19:46:08 +08:00

222 lines
8.7 KiB
Python

"""Contracts for manuals, lock, vendor sources and package layout."""
import argparse
import ast
import json
import re
import subprocess
import sys
import tomllib
from pathlib import Path
from universal_humanoid import __version__
from universal_humanoid.cli import parser
from universal_humanoid.registry import TASKS
from universal_humanoid.robot_check import CHECK_IDS, load_spec
ROOT = Path(__file__).resolve().parents[1]
def test_vendored_layout_and_packaging():
assert not (ROOT / "rl/__init__.py").exists()
assert not any(p.is_symlink() for p in (ROOT / "rl").rglob("*")), (
"Vendor real files, not workspace symlinks"
)
for package in ("mjlab", "rsl_rl"):
assert (ROOT / "rl" / package / "__init__.py").is_file()
assert (ROOT / "rl" / package / "LICENSE").is_file()
assert not (ROOT / package).exists()
project = tomllib.loads((ROOT / "pyproject.toml").read_text())
assert project["tool"]["setuptools"]["packages"]["find"]["where"] == ["src", "rl"]
# Upstream mjlab/scripts has no __init__.py; excluding namespaces drops real source from wheels.
assert project["tool"]["setuptools"]["packages"]["find"]["namespaces"] is True
assert __version__ == project["project"]["version"]
deps = project["project"]["dependencies"]
assert not any(re.match(r"(mjlab|rsl[-_]rl)", dep) for dep in deps)
assert (
"robot_specs/*.json" in project["tool"]["setuptools"]["package-data"]["universal_humanoid"]
)
assert set(project["tool"]["ruff"]["extend-exclude"]) == {
"rl/mjlab",
"rl/rsl_rl",
"rl/upstream",
}
def test_vendor_inventory_and_explicit_modified_sources():
result = subprocess.run(
[sys.executable, str(ROOT / "tools/vendor_manifest.py")],
capture_output=True,
text=True,
check=True,
)
assert json.loads(result.stdout)["files"] > 300
manifest = json.loads((ROOT / "rl/VENDOR.json").read_text())
expected = {
"rl/mjlab/sim/sim.py",
"rl/mjlab/tasks/velocity/mdp/velocity_command.py",
"rl/mjlab/asset_zoo/robots/booster_t1/t1_constants.py",
}
assert set(manifest["changed_since_import"]) == expected
vendor_doc = (ROOT / "docs/VENDOR.md").read_text()
for name in expected:
assert "[uh-vendor]" in (ROOT / name).read_text()
assert name in vendor_doc
def test_lock_contains_no_external_workspace_dependency():
lock = tomllib.loads((ROOT / "uv.lock").read_text())
names = {p["name"] for p in lock["package"]}
assert {"torch", "mujoco", "mujoco-warp", "warp-lang"} <= names
assert not {"mjlab", "rsl-rl-lib"} & names
for package in lock["package"]:
source = package["source"]
if "editable" in source:
assert source["editable"] == "."
else:
assert "registry" in source
for wheel in package.get("wheels", []):
assert wheel["hash"].startswith("sha256:")
exported = (ROOT / "requirements-linux-cu130.lock").read_text()
assert "torch==2.14.0+cu130" in exported
assert "--hash=sha256:" in exported
pins = set(re.findall(r"^([a-zA-Z0-9_.-]+)==([^\s;]+)", exported, re.M))
expected = {
(p["name"], p["version"]) for p in lock["package"] if p["name"] != "universal-humanoid"
}
assert pins == expected, "Regenerate the hashed pip export after changing uv.lock"
def test_license_and_locked_ci_contract():
project = tomllib.loads((ROOT / "pyproject.toml").read_text())
license_text = (ROOT / "LICENSE").read_text()
assert "Apache License" in license_text and "Version 2.0, January 2004" in license_text
assert project["project"]["license"] == "Apache-2.0 AND BSD-3-Clause AND MIT"
assert {"LICENSE", "NOTICE.md", "licenses/*.txt"} <= set(project["project"]["license-files"])
assert project["dependency-groups"]["ci"] == project["project"]["optional-dependencies"]["dev"]
ci_lock = (ROOT / "requirements-ci.lock").read_text()
ci_pins = set(re.findall(r"^([a-zA-Z0-9_.-]+)==([^\s;]+)", ci_lock, re.M))
full_pins = set(
re.findall(
r"^([a-zA-Z0-9_.-]+)==([^\s;]+)",
(ROOT / "requirements-linux-cu130.lock").read_text(),
re.M,
)
)
assert ci_pins <= full_pins
names = {name for name, _ in ci_pins}
assert {"pytest", "ruff", "build", "setuptools"} <= names
assert not {"torch", "mujoco", "mujoco-warp", "warp-lang"} & names
assert "--hash=sha256:" in ci_lock
workflow = (ROOT / ".github/workflows/check.yml").read_text()
for command in (
"uv lock --check",
"--locked --only-group ci",
"--no-build-isolation",
"git diff --exit-code",
"tools/vendor_manifest.py",
"tools/check_wheel.py",
"ruff check",
"ruff format --check",
"pytest -q",
):
assert command in workflow
actions = re.findall(r"uses: ([^\s]+)", workflow)
assert len(actions) >= 2
assert all(re.fullmatch(r"[^@]+@[0-9a-f]{40}", action) for action in actions)
assert "contents: read" in workflow and "workflow_dispatch:" in workflow
def test_every_skill_indexed_and_has_real_stage_instructions():
skills = sorted((ROOT / ".agents/skills").glob("*/SKILL.md"))
names = {path.parent.name for path in skills}
assert names == {"setup-env", "new-task", "train", "controls", "deploy", "rl-engineer"}
agents = (ROOT / "AGENTS.md").read_text()
indexed = set(re.findall(r"\]\(\.agents/skills/([^/]+)/SKILL\.md\)", agents))
assert indexed == names
for path in skills:
text = path.read_text()
assert f"name: {path.parent.name}\n" in text
assert "description:" in text and "## " in text
assert "uh " in text or "scripts/uh" in text
for name in ("detect.py", "check.py"):
script = ROOT / ".agents/skills/setup-env/scripts" / name
assert script.is_file()
assert name in (ROOT / ".agents/skills/setup-env/SKILL.md").read_text()
def test_documentation_links_resolve():
documents = [
ROOT / name
for name in (
"README.md",
"README.zh.md",
"AGENTS.md",
"CLAUDE.md",
"NOTICE.md",
"CONTRIBUTING.md",
)
]
documents += list((ROOT / "docs").glob("*.md"))
documents += list((ROOT / ".agents/skills").rglob("*.md"))
for path in documents:
for target in re.findall(r"\[[^\]]*\]\(([^)]+)\)", path.read_text()):
if "://" in target or target.startswith(("#", "mailto:")):
continue
assert (path.parent / target.split("#")[0]).exists(), (path, target)
def test_cli_reference_exactly_matches_parser():
root = parser()
commands = next(a for a in root._actions if isinstance(a, argparse._SubParsersAction)).choices
documented = {}
for line in (ROOT / "docs/CLI.md").read_text().splitlines():
match = re.match(r"\| `([a-z-]+)` \| ([^|]*) \|", line)
if match:
documented[match[1]] = set(re.findall(r"--[a-z-]+", match[2]))
assert set(documented) == set(commands)
for name, cmd in commands.items():
flags = {
flag
for action in cmd._actions
for flag in action.option_strings
if flag != "--help" and flag != "-h"
}
assert documented[name] == flags, name
def test_robot_specs_and_check_ids_are_documented():
for task in TASKS.values():
spec = load_spec(task.id)
assert len(spec["action_joint_order"]) == task.dof
assert len(spec["motors"]) == task.dof
documented = set(re.findall(r"\| `([a-z_]+)` \|", (ROOT / "docs/NEW_ROBOT.md").read_text()))
assert documented == set(CHECK_IDS)
def test_no_private_workspace_fallback_or_sys_path_rewriting():
directories = [ROOT / "src", ROOT / "scripts", ROOT / "tools", ROOT / ".agents/skills"]
paths = [
p
for directory in directories
for p in directory.rglob("*")
if p.is_file()
and "__pycache__" not in p.parts
and (p.suffix in (".py", ".sh") or p.parent.name == "scripts")
]
assert paths
for path in paths:
text = path.read_text()
assert "Embodied_Workspace" not in text, path
assert not re.search(r"[\"']/(home|Users)/", text), path
if path.suffix == ".py":
for node in ast.walk(ast.parse(text)):
if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
assert ast.unparse(node.func) not in {
"sys.path.insert",
"sys.path.append",
"sys.path.extend",
}, path
for path in (ROOT / "rl").rglob("*.py"):
assert "Embodied_Workspace" not in path.read_text(), path