hermes-agent/tests/ci/test_lockfile_diff.py
ethernet f8ddf4fd86
feat(ci): semantic package-lock.json diff as an upserted PR comment (#65206)
git diff on a lockfile is unreadable: npm reorders entries, rewrites
integrity hashes, and moves packages between nesting levels, so a
one-line package.json bump produces a thousand-line textual diff.

scripts/ci/lockfile_diff.py instead parses the `packages` map out of
both versions of every tracked package-lock.json (via `git show`),
reduces each to {install path: version}, and set-diffs the maps —
reorder/hash churn vanishes, leaving only actual version movement
(added / removed / updated, with nested dedup copies tracked
separately).

The lockfile-diff workflow posts the result as a Markdown table in a
PR comment gated behind a hidden marker: subsequent pushes PATCH the
existing comment instead of stacking new ones, and a push that reverts
all lockfile changes updates the comment to say so. Advisory only —
never fails on findings; fork PRs (read-only token) degrade to a
warning.

Wired through the ci.yml orchestrator with a new npm_lock lane in
classify_changes.py (fails open on .github/ changes per the existing
contract).
2026-07-16 03:18:15 +00:00

111 lines
4.1 KiB
Python

"""Tests for scripts/ci/lockfile_diff.py.
The differ's job is semantic comparison: reordering and integrity-hash
churn in the lockfile text must produce an empty diff, while actual
version movement must show up as added/removed/updated regardless of
where in the file it appears.
"""
from __future__ import annotations
import importlib.util
import json
from pathlib import Path
_PATH = Path(__file__).resolve().parents[2] / "scripts" / "ci" / "lockfile_diff.py"
_spec = importlib.util.spec_from_file_location("lockfile_diff", _PATH)
if _spec is None or _spec.loader is None:
raise ImportError("Failed to load lockfile_diff.py")
_mod = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_mod)
def _lock(packages: dict[str, dict]) -> str:
return json.dumps(
{
"name": "hermes",
"lockfileVersion": 3,
"packages": {"": {"name": "hermes"}, **packages},
}
)
BASE = _lock(
{
"node_modules/react": {"version": "18.2.0", "integrity": "sha512-aaa"},
"node_modules/left-pad": {"version": "1.3.0", "integrity": "sha512-bbb"},
"node_modules/foo/node_modules/react": {"version": "17.0.2"},
}
)
def test_parse_skips_root_and_versionless():
text = _lock({"node_modules/linked": {"link": True}})
parsed = _mod.parse_lockfile(text)
assert parsed == {} # root entry and versionless link both skipped
def test_reorder_and_hash_churn_is_empty_diff():
# Same packages, reordered, different integrity hashes — the exact noise
# that makes textual lockfile diffs unreadable.
reordered = _lock(
{
"node_modules/foo/node_modules/react": {"version": "17.0.2"},
"node_modules/left-pad": {"version": "1.3.0", "integrity": "sha512-XYZ"},
"node_modules/react": {"version": "18.2.0", "integrity": "sha512-ZZZ"},
}
)
d = _mod.diff_locks(_mod.parse_lockfile(BASE), _mod.parse_lockfile(reordered))
assert d == {"added": [], "removed": [], "updated": []}
assert _mod.render_markdown({"package-lock.json": d}) == ""
def test_add_remove_update_all_detected():
head = _lock(
{
"node_modules/react": {"version": "18.3.1"}, # updated
"node_modules/is-even": {"version": "1.0.0"}, # added
"node_modules/foo/node_modules/react": {"version": "17.0.2"}, # unchanged
# left-pad removed
}
)
d = _mod.diff_locks(_mod.parse_lockfile(BASE), _mod.parse_lockfile(head))
assert d["added"] == [("node_modules/is-even", "1.0.0")]
assert d["removed"] == [("node_modules/left-pad", "1.3.0")]
assert d["updated"] == [("node_modules/react", "18.2.0", "18.3.1")]
def test_nested_dedup_is_distinct_entry():
# The same package at two nesting levels must be tracked separately —
# bumping only the nested copy must not look like a top-level change.
head = _lock(
{
"node_modules/react": {"version": "18.2.0"},
"node_modules/left-pad": {"version": "1.3.0"},
"node_modules/foo/node_modules/react": {"version": "17.0.3"},
}
)
d = _mod.diff_locks(_mod.parse_lockfile(BASE), _mod.parse_lockfile(head))
assert d["updated"] == [("node_modules/foo/node_modules/react", "17.0.2", "17.0.3")]
def test_render_markdown_contains_marker_and_versions():
d = _mod.diff_locks(
_mod.parse_lockfile(BASE),
_mod.parse_lockfile(_lock({"node_modules/react": {"version": "19.0.0"}})),
)
md = _mod.render_markdown({"apps/desktop/package-lock.json": d})
assert md.startswith(_mod.COMMENT_MARKER) # workflow finds its comment by prefix
assert "⚠️" in md
assert "`apps/desktop/package-lock.json`" in md
assert "`18.2.0`" in md and "`19.0.0`" in md
# nested display name keeps the parent chain visible
assert "nested under foo" in md
def test_render_markdown_omits_unchanged_lockfiles():
changed = _mod.diff_locks({}, {"node_modules/x": "1.0.0"})
unchanged = _mod.diff_locks({}, {})
md = _mod.render_markdown({"a/package-lock.json": changed, "b/package-lock.json": unchanged})
assert "a/package-lock.json" in md
assert "b/package-lock.json" not in md