~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
ci(npm): generate update matrix dynamically from package-lock.json (#4997)
LearningCircuit0958a94a05af
diff --git a/.github/workflows/update-npm-dependencies.yml b/.github/workflows/update-npm-dependencies.ymlindex fa99c10f09..dcf2abcf43 100644--- a/.github/workflows/update-npm-dependencies.yml+++ b/.github/workflows/update-npm-dependencies.yml@@ -18,23 +18,83 @@ on:permissions: {} # Minimal top-level for OSSF Scorecard Token-Permissionsjobs:+ # Discover every dir containing a package-lock.json and emit the list as+ # JSON. This is what makes the matrix dynamic: adding a new test dir with+ # a lockfile is now automatically covered by the weekly sweep, with no+ # need to extend the hand-maintained matrix in this file. The previous+ # static list (PR #4983) was itself a fix for a 6-dir coverage gap; this+ # job prevents the next such gap.+ #+ # Convention encoded in the JSON:+ # - "." (repo root) runs `npm run build`, no tests.+ # - everything under tests/ echoes a skip — every such dir needs a+ # server / API key / auth that CI doesn't have. If a dir ever becomes+ # CI-runnable, promote it to a build/test command here (or via a+ # future marker file).+ # - any OTHER lockfile dir FAILS this job. Silently letting an+ # unrecognized package enter the matrix with no build/test is the+ # exact coverage gap this job exists to prevent; add explicit+ # classification above before such a dir can be swept.+ discover:+ name: 🔍 Discover lockfile dirs+ runs-on: ubuntu-latest+ permissions:+ contents: read+ outputs:+ matrix: ${{ steps.list.outputs.matrix }}+ steps:+ - name: Harden the runner (Audit all outbound calls)+ uses: step-security/harden-runner@9af89fc71515a100421586dfdb3dc9c984fbf411 # v2.19.4+ with:+ egress-policy: audit++ - name: 📰 Checkout+ uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0+ with:+ persist-credentials: false+ ref: main++ # jq is preinstalled on ubuntu-latest; no install step needed.+ # -prune avoids descending into node_modules / .claude / .github+ # (filtering with -not -path would still traverse them).+ - name: List package-lock.json dirs+ id: list+ run: |+ set -euo pipefail+ MATRIX=$(find . \+ \( -path '*/node_modules' -o -path './.claude' -o -path './.github' \) -prune \+ -o -name package-lock.json -printf '%h\n' \+ | sed 's|^\./||' \+ | sort -u \+ | jq -R '{path: ., build_cmd: (if . == "." then "npm run build" else "" end), test_cmd: (if (. == "tests" or startswith("tests/")) then "echo \"Skipping tests in CI - requires server\"" else "" end), cache_path: "package-lock.json"}' \+ | jq -s -c .)+ if [ "$(echo "$MATRIX" | jq 'length')" -eq 0 ]; then+ echo "::error::No package-lock.json files discovered — matrix would be empty"+ exit 1+ fi+ # Fail loud on lockfile dirs that are neither the repo root nor+ # under tests/: silently skipping their build/test is the coverage+ # gap this job prevents. Promote such a dir to explicit commands.+ UNKNOWN=$(echo "$MATRIX" | jq -r '.[] | select(.build_cmd == "" and .test_cmd == "") | .path')+ if [ -n "$UNKNOWN" ]; then+ echo "::error::Unrecognized lockfile dir(s) — neither repo root nor under tests/. Classify build/test commands for:"+ while IFS= read -r d; do echo " - $d"; done <<< "$UNKNOWN"+ exit 1+ fi+ echo "matrix=$MATRIX" >> "$GITHUB_OUTPUT"+ echo "Discovered $(echo "$MATRIX" | jq 'length') dir(s):"+ echo "$MATRIX" | jq -r '.[].path'+update:name: 📦 Update with NPM+ needs: [discover]runs-on: ubuntu-latestpermissions:contents: write # Required for creating PRspull-requests: write # Required for creating PRsstrategy:matrix:- npm_directory:- - { path: '.', build_cmd: 'npm run build', test_cmd: '', cache_path: 'package-lock.json' }- - { path: 'tests', build_cmd: '', test_cmd: 'echo "Skipping API-key tests in CI - requires real API key"', cache_path: 'package-lock.json' }- - { path: 'tests/ui_tests', build_cmd: '', test_cmd: 'echo "Skipping UI tests in CI - requires server"', cache_path: 'package-lock.json' }- - { path: 'tests/puppeteer', build_cmd: '', test_cmd: 'echo "Skipping Puppeteer tests in CI - requires server"', cache_path: 'package-lock.json' }- - { path: 'tests/api_tests_with_login', build_cmd: '', test_cmd: 'echo "Skipping API-with-login tests in CI - requires server + auth"', cache_path: 'package-lock.json' }- - { path: 'tests/infrastructure_tests', build_cmd: '', test_cmd: 'echo "Skipping infrastructure tests in CI - requires server"', cache_path: 'package-lock.json' }- - { path: 'tests/accessibility_tests', build_cmd: '', test_cmd: 'echo "Skipping accessibility tests in CI - requires server"', cache_path: 'package-lock.json' }- - { path: 'tests/ui_tests/playwright', build_cmd: '', test_cmd: 'echo "Skipping playwright UI tests in CI - requires server"', cache_path: 'package-lock.json' }+ npm_directory: ${{ fromJson(needs.discover.outputs.matrix) }}continue-on-error: falsesteps:- name: Harden the runner (Audit all outbound calls)@@ -127,7 +187,9 @@ jobs:echo "changed=false" >> "$GITHUB_OUTPUT"elseecho "changed=true" >> "$GITHUB_OUTPUT"- echo "path=${{ matrix.npm_directory.path }}" >> "$GITHUB_OUTPUT"+ # Use $NPM_DIR (env) rather than a direct GitHub-Actions+ # expression so a path can never be expanded as a shell expr.+ echo "path=$NPM_DIR" >> "$GITHUB_OUTPUT"fi- name: 📝 Create pull requestdiff --git a/changelog.d/+npm-update-matrix-tests.misc.md b/changelog.d/+npm-update-matrix-tests.misc.mdnew file mode 100644index 0000000000..19b550be84--- /dev/null+++ b/changelog.d/+npm-update-matrix-tests.misc.md@@ -0,0 +1 @@+Hardened the dynamic npm-update matrix workflow (PR #4997) and added a guardian + behavioral test suite: discovery now prunes node_modules/.claude/.github during traversal, fails loudly on lockfile dirs that are neither the repo root nor under tests/ (instead of silently skipping their build/test), and passes the discovered path through an env var rather than direct ${{ }} interpolation in the change-detection step. Tests pin the wiring (guardian), execute the real find|jq pipeline against synthetic and real-repo trees (behavioral, zero drift), and cover both the empty-matrix and unknown-path fail-loud paths.diff --git a/tests/ci/test_npm_update_matrix.py b/tests/ci/test_npm_update_matrix.pynew file mode 100644index 0000000000..6bb229f391--- /dev/null+++ b/tests/ci/test_npm_update_matrix.py@@ -0,0 +1,377 @@+"""Tests for the dynamic npm-update matrix in update-npm-dependencies.yml.++Coverage in two layers:++1. Structural ("guardian") tests parse the workflow YAML and pin the wiring+ that makes the matrix dynamic: the ``discover`` job, its ``matrix`` output,+ the ``update`` job consuming ``fromJson(needs.discover.outputs.matrix)``,+ the ``set -euo pipefail`` and empty-matrix guard, and the minimal+ ``contents: read`` permission. These prevent the safety features from being+ silently removed — same philosophy as+ ``tests/ci/test_release_gate_integrity.py``.++2. Behavioral tests extract the discover step's real ``run:`` script from the+ YAML and execute it against synthetic fixture trees via subprocess. This+ proves the find|jq pipeline produces a correct matrix (repo root builds,+ ``tests/*`` dirs skip, excluded dirs ignored) and that the empty-matrix+ guard fails the job — without duplicating the pipeline, so it cannot drift+ from the workflow.+"""++import json+import os+import shutil+import subprocess+import sys+from pathlib import Path++import pytest+import yaml++REPO_ROOT = Path(__file__).resolve().parents[2]+WORKFLOW = REPO_ROOT / ".github" / "workflows" / "update-npm-dependencies.yml"+++def _gnu_find_available() -> bool:+ """True if ``find`` supports the GNU ``-printf`` action.++ Distinguishes GNU findutils (ubuntu-latest, the workflow's runner) from+ BusyBox find (Alpine/containers), which lacks ``-printf`` — otherwise the+ behavioral tests would fail unexpectedly on such hosts instead of skipping.+ """+ try:+ proc = subprocess.run(+ ["find", str(REPO_ROOT), "-maxdepth", "0", "-printf", "."],+ capture_output=True,+ text=True,+ timeout=5,+ )+ return proc.returncode == 0 and proc.stdout == "."+ except (OSError, subprocess.SubprocessError):+ return False+++# The discover pipeline relies on GNU find (``-printf``) and ``jq``, both+# present on the ubuntu-latest CI runner. Probe the platform, jq, AND the+# find variant so the tests skip cleanly on macOS/Windows AND on+# BusyBox-Linux (Alpine) rather than failing unexpectedly.+_REQUIRES_GNU = pytest.mark.skipif(+ sys.platform != "linux"+ or not shutil.which("jq")+ or not _gnu_find_available(),+ reason="discover pipeline needs GNU find (-printf) and jq (ubuntu-latest)",+)++[email protected](scope="module")+def workflow():+ """Load update-npm-dependencies.yml as parsed YAML."""+ assert WORKFLOW.exists(), (+ f"update-npm-dependencies.yml not found at {WORKFLOW}. "+ "If you moved it, update the path — do NOT delete these tests."+ )+ return yaml.safe_load(WORKFLOW.read_text())+++def _discover_run(workflow: dict) -> str:+ """Return the discover job's ``list`` step ``run:`` script."""+ for step in workflow["jobs"]["discover"]["steps"]:+ if step.get("id") == "list":+ return step["run"]+ raise AssertionError(+ "discover job has no step with id 'list' — cannot extract pipeline"+ )+++# ---------------------------------------------------------------------------+# Structural ("guardian") tests — pin the wiring, prevent silent removal+# ---------------------------------------------------------------------------+++class TestWorkflowStructure:+ """Pin the dynamic-matrix wiring and safety features in the workflow."""++ def test_discover_job_exists(self, workflow):+ assert "discover" in workflow.get("jobs", {}), (+ "The 'discover' job is missing — without it the matrix is no "+ "longer generated dynamically and new lockfile dirs silently go "+ "unswept (the regression PR #4997 fixed)."+ )++ def test_update_needs_discover(self, workflow):+ needs = workflow["jobs"]["update"].get("needs", [])+ assert "discover" in needs, (+ "update.needs no longer includes 'discover' — the update job "+ "would race ahead of (or skip) matrix generation."+ )++ def test_matrix_consumes_discover_output(self, workflow):+ matrix = workflow["jobs"]["update"]["strategy"]["matrix"][+ "npm_directory"+ ]+ assert matrix == "${{ fromJson(needs.discover.outputs.matrix) }}", (+ "The matrix no longer consumes the discover output dynamically; "+ f"got: {matrix!r}"+ )++ def test_discover_exposes_matrix_output(self, workflow):+ outputs = workflow["jobs"]["discover"].get("outputs", {})+ assert outputs.get("matrix") == "${{ steps.list.outputs.matrix }}", (+ "discover.outputs.matrix is missing/changed — update cannot read "+ "the generated matrix."+ )++ def test_discover_uses_pipefail(self, workflow):+ run = _discover_run(workflow)+ assert "set -euo pipefail" in run, (+ "discover no longer sets -euo pipefail — a mid-pipe failure in "+ "the find|sed|jq chain could be silently swallowed."+ )++ def test_discover_has_empty_matrix_guard(self, workflow):+ run = _discover_run(workflow)+ assert "-eq 0" in run and "exit 1" in run, (+ "The empty-matrix guard is gone — if find returns nothing, the "+ "update job would silently no-op instead of failing loudly."+ )++ def test_discover_has_unknown_path_guard(self, workflow):+ run = _discover_run(workflow)+ assert "Unrecognized lockfile dir" in run, (+ "The unknown-path guard is gone — a lockfile dir that is neither "+ "the repo root nor under tests/ would silently enter the matrix "+ "with no build/test, recreating the coverage gap this job exists "+ "to prevent."+ )++ def test_discover_permissions_are_read_only(self, workflow):+ perms = workflow["jobs"]["discover"].get("permissions", {})+ assert perms == {"contents": "read"}, (+ "discover should hold the minimal contents:read permission; "+ f"got {perms!r}. A discovery job must not be able to write."+ )++ def test_discover_checkout_is_pinned_and_credentialless(self, workflow):+ for step in workflow["jobs"]["discover"]["steps"]:+ uses = str(step.get("uses", ""))+ if uses.startswith("actions/checkout@"):+ assert step["with"].get("persist-credentials") is False, (+ "discover checkout must use persist-credentials: false."+ )+ ref = uses.split("@", 1)[1]+ assert len(ref) >= 40 and all(+ c in "0123456789abcdef" for c in ref[:40]+ ), f"actions/checkout must be SHA-pinned, got @{ref}"+ return+ raise AssertionError("discover has no actions/checkout step")++ def test_update_still_can_open_prs(self, workflow):+ # Sanity: the update job retains the write permissions it needs to+ # create the dependency-bump PR — don't let a permissions tightening+ # here break the workflow's actual purpose.+ perms = workflow["jobs"]["update"].get("permissions", {})+ assert perms.get("contents") == "write", (+ "update lost contents:write — it can no longer create the PR."+ )+ assert perms.get("pull-requests") == "write", (+ "update lost pull-requests:write — it can no longer create the PR."+ )+++# ---------------------------------------------------------------------------+# Behavioral tests — execute the REAL pipeline from the YAML (zero drift)+# ---------------------------------------------------------------------------+++def _build_tree(root: Path, lockfiles):+ """Materialise a fixture repo containing the given package-lock.json paths."""+ for rel in lockfiles:+ p = root / rel+ p.parent.mkdir(parents=True, exist_ok=True)+ p.write_text("{}")+++def _run_pipeline(run_script: str, root: Path, tmp_path: Path):+ """Run the extracted discover script with cwd=root, capturing GITHUB_OUTPUT."""+ gh_output = tmp_path / "gh_output"+ script_file = tmp_path / "discover.sh"+ script_file.write_text(run_script)+ return subprocess.run(+ ["bash", str(script_file)],+ cwd=str(root),+ env={**os.environ, "GITHUB_OUTPUT": str(gh_output)},+ capture_output=True,+ text=True,+ )+++def _matrix_from_output(tmp_path: Path) -> list[dict]:+ gh_output = tmp_path / "gh_output"+ for line in gh_output.read_text().splitlines():+ if line.startswith("matrix="):+ return json.loads(line[len("matrix=") :])+ raise AssertionError(+ "discover script wrote no matrix= line to GITHUB_OUTPUT"+ )+++@_REQUIRES_GNU+class TestPipelineBehavior:+ """Execute the real find|jq pipeline against fixture trees."""++ def test_classifies_root_and_test_dirs(self, workflow, tmp_path):+ root = tmp_path / "repo"+ _build_tree(+ root,+ [+ "package-lock.json", # repo root -> builds+ "tests/alpha/package-lock.json", # test dir -> skip+ "tests/beta/package-lock.json", # test dir -> skip+ "node_modules/evil/package-lock.json", # excluded+ ".claude/evil/package-lock.json", # excluded+ ".github/evil/package-lock.json", # excluded+ ],+ )+ proc = _run_pipeline(_discover_run(workflow), root, tmp_path)+ assert proc.returncode == 0, proc.stderr++ matrix = _matrix_from_output(tmp_path)+ paths = [e["path"] for e in matrix]+ assert paths == [".", "tests/alpha", "tests/beta"], paths++ by_path = {e["path"]: e for e in matrix}+ # Repo root is the only dir that builds and runs no test-skip echo.+ assert by_path["."]["build_cmd"] == "npm run build"+ assert by_path["."]["test_cmd"] == ""+ # Every tests/* dir skips tests and does not build.+ for d in ("tests/alpha", "tests/beta"):+ assert by_path[d]["build_cmd"] == "", d+ assert by_path[d]["test_cmd"].startswith("echo"), d+ # Excluded dirs never appear in the matrix.+ assert not any(+ "node_modules" in p or p.startswith((".claude", ".github"))+ for p in paths+ ), paths+ # Every entry carries the keys the update job dereferences.+ for e in matrix:+ assert set(e) == {"path", "build_cmd", "test_cmd", "cache_path"}+ assert e["cache_path"] == "package-lock.json"++ def test_unknown_non_tests_path_fails_loudly(self, workflow, tmp_path):+ # A lockfile dir that is neither the repo root nor under tests/ must+ # FAIL discovery rather than silently entering the matrix with no+ # build/test — that would recreate the coverage gap this job prevents.+ root = tmp_path / "repo"+ _build_tree(+ root,+ [+ "package-lock.json", # root (valid)+ "packages/app/package-lock.json", # unrecognized -> must fail+ ],+ )+ proc = _run_pipeline(_discover_run(workflow), root, tmp_path)+ assert proc.returncode != 0, (+ "an unrecognized lockfile dir must fail discovery, not silently "+ "skip build/test"+ )+ # Pin the SPECIFIC failure mode so an unrelated shell error can't+ # satisfy this test accidentally.+ assert "Unrecognized lockfile dir" in proc.stdout, (+ f"expected the unknown-path guard; got stdout={proc.stdout!r} "+ f"stderr={proc.stderr!r}"+ )++ def test_empty_matrix_guard_fails_loudly(self, workflow, tmp_path):+ # Only excluded dirs present => find yields nothing after filters,+ # so the guard must fail the step rather than silently no-op.+ root = tmp_path / "repo"+ _build_tree(+ root,+ [+ "node_modules/only/package-lock.json",+ ".github/only/package-lock.json",+ ],+ )+ proc = _run_pipeline(_discover_run(workflow), root, tmp_path)+ assert proc.returncode != 0, (+ "empty matrix must fail the discover step, not silently no-op"+ )+ assert "No package-lock.json files discovered" in proc.stdout, (+ f"expected the empty-matrix guard; got stdout={proc.stdout!r} "+ f"stderr={proc.stderr!r}"+ )++ def test_truly_empty_repo_fails(self, workflow, tmp_path):+ root = tmp_path / "repo"+ root.mkdir()+ proc = _run_pipeline(_discover_run(workflow), root, tmp_path)+ assert proc.returncode != 0, (+ "a repo with no package-lock.json must fail the discover step"+ )+ assert "No package-lock.json files discovered" in proc.stdout, (+ f"expected the empty-matrix guard; got stdout={proc.stdout!r} "+ f"stderr={proc.stderr!r}"+ )+++# ---------------------------------------------------------------------------+# Real-repo invariant tests — guard against over-greedy exclusions without+# hardcoding a directory list (which would re-introduce the maintenance the+# PR removed and break spuriously whenever test dirs are added or removed).+# ---------------------------------------------------------------------------++[email protected](scope="module")+def real_repo_matrix(workflow, tmp_path_factory):+ """Run the pipeline against this repo ONCE and cache the parsed matrix.++ Executing find over the whole tree (which may include node_modules) is+ noticeably slower than the synthetic fixtures, so the three real-repo+ invariant tests share this single result instead of spawning three runs.+ """+ tmp = tmp_path_factory.mktemp("realrepo")+ proc = _run_pipeline(_discover_run(workflow), REPO_ROOT, tmp)+ assert proc.returncode == 0, proc.stderr+ return _matrix_from_output(tmp)+++@_REQUIRES_GNU+class TestPipelineAgainstRealRepo:+ """Run the real pipeline against this repository's actual tree.++ Unlike TestPipelineBehavior (synthetic fixtures proving the logic in+ isolation), these prove the *current* repo layout doesn't trip an edge+ case — e.g. an exclusion pattern silently dropping a live lockfile dir,+ or an unrecognized package dir slipping through unclassified.+ Assertions are invariants, not an enumerated list, so they stay green+ when test dirs are added or removed.+ """++ def test_repo_root_is_discovered_and_marked_to_build(+ self, real_repo_matrix+ ):+ by_path = {e["path"]: e for e in real_repo_matrix}+ assert "." in by_path, "repo root lockfile was not discovered"+ root = by_path["."]+ assert root["build_cmd"] == "npm run build", root+ assert root["test_cmd"] == "", root++ def test_every_non_root_dir_is_a_tests_dir_skip(self, real_repo_matrix):+ # Convention: only "." builds; every OTHER lockfile dir must be a+ # tests/ dir that skips (the discovery guard rejects anything else).+ # Asserting the tests/ membership codifies the documented convention+ # rather than the looser "anything non-root skips" behaviour.+ for e in real_repo_matrix:+ if e["path"] == ".":+ continue+ assert e["path"] == "tests" or e["path"].startswith("tests/"), e+ assert e["build_cmd"] == "", e+ assert e["test_cmd"].startswith("echo"), e++ def test_discovers_more_than_just_the_root(self, real_repo_matrix):+ # Sanity lower bound: a wipeout from an over-greedy exclusion would+ # collapse the matrix to at most the root entry.+ assert len(real_repo_matrix) >= 2, (+ f"expected root + >=1 tests dir, got {len(real_repo_matrix)}: "+ f"{[e['path'] for e in real_repo_matrix]}"+ )