|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import types |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +import nemoguardrails.manifests.catalog as catalog_module |
| 21 | +from nemoguardrails.manifests import ( |
| 22 | + ActionRef, |
| 23 | + Binding, |
| 24 | + ConfigSpecRef, |
| 25 | + RailActions, |
| 26 | + RailCatalog, |
| 27 | + RailConfigSchema, |
| 28 | + RailDirection, |
| 29 | + RailManifest, |
| 30 | + RailManifestRecord, |
| 31 | + RailSpec, |
| 32 | + RailSurface, |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +def _action(name: str = "check") -> ActionRef: |
| 37 | + return ActionRef(name=name, target="pathlib:Path.cwd") |
| 38 | + |
| 39 | + |
| 40 | +def _record(name: str, *, action: ActionRef | None = None, surface_name: str | None = None) -> RailManifestRecord: |
| 41 | + action = action or _action(f"{name}_check") |
| 42 | + surfaces = () |
| 43 | + if surface_name is not None: |
| 44 | + surfaces = ( |
| 45 | + RailSurface( |
| 46 | + name=surface_name, |
| 47 | + direction=RailDirection.INPUT, |
| 48 | + action=action, |
| 49 | + bindings=(Binding.context("text", "user_message"),), |
| 50 | + ), |
| 51 | + ) |
| 52 | + manifest = RailManifest( |
| 53 | + name=name, |
| 54 | + spec=RailSpec(actions=RailActions(refs=(action,)), surfaces=surfaces), |
| 55 | + ) |
| 56 | + return RailManifestRecord(manifest=manifest, source=f"test:{name}") |
| 57 | + |
| 58 | + |
| 59 | +def test_catalog_indexes_manifests_and_surfaces(): |
| 60 | + catalog = RailCatalog((_record("alpha", surface_name="check alpha"), _record("beta"))) |
| 61 | + |
| 62 | + assert set(catalog.manifests) == {"alpha", "beta"} |
| 63 | + assert set(catalog.surfaces()) == {(RailDirection.INPUT, "check alpha")} |
| 64 | + |
| 65 | + |
| 66 | +def test_catalog_rejects_duplicate_manifest_names(): |
| 67 | + with pytest.raises(ValueError, match="already provided"): |
| 68 | + RailCatalog((_record("duplicate"), _record("duplicate"))) |
| 69 | + |
| 70 | + |
| 71 | +def test_catalog_rejects_duplicate_action_names(): |
| 72 | + action = _action("shared") |
| 73 | + |
| 74 | + with pytest.raises(ValueError, match="already provided"): |
| 75 | + RailCatalog((_record("alpha", action=action), _record("beta", action=action))) |
| 76 | + |
| 77 | + |
| 78 | +def test_catalog_rejects_duplicate_surface_keys(): |
| 79 | + with pytest.raises(ValueError, match="already provided"): |
| 80 | + RailCatalog((_record("alpha", surface_name="shared"), _record("beta", surface_name="shared"))) |
| 81 | + |
| 82 | + |
| 83 | +def test_catalog_rejects_duplicate_config_key(): |
| 84 | + shared_spec = ConfigSpecRef(target="pathlib:Path.cwd") |
| 85 | + |
| 86 | + def record(name): |
| 87 | + manifest = RailManifest( |
| 88 | + name=name, spec=RailSpec(config_schema=RailConfigSchema(key="shared", spec=shared_spec)) |
| 89 | + ) |
| 90 | + return RailManifestRecord(manifest=manifest, source=f"test:{name}") |
| 91 | + |
| 92 | + with pytest.raises(ValueError, match="config key"): |
| 93 | + RailCatalog((record("alpha"), record("beta"))) |
| 94 | + |
| 95 | + |
| 96 | +def test_catalog_rejects_surface_with_undeclared_action(): |
| 97 | + declared = _action("declared") |
| 98 | + undeclared = _action("undeclared") |
| 99 | + manifest = RailManifest( |
| 100 | + name="invalid", |
| 101 | + spec=RailSpec( |
| 102 | + actions=RailActions(refs=(declared,)), |
| 103 | + surfaces=(RailSurface(name="invalid", direction="input", action=undeclared),), |
| 104 | + ), |
| 105 | + ) |
| 106 | + |
| 107 | + with pytest.raises(ValueError, match="not declared"): |
| 108 | + RailCatalog((RailManifestRecord(manifest=manifest, source="test:invalid"),)) |
| 109 | + |
| 110 | + |
| 111 | +def test_discover_built_ins_loads_rail_modules(tmp_path, monkeypatch): |
| 112 | + rail_package = tmp_path / "content_safety" |
| 113 | + rail_package.mkdir() |
| 114 | + (rail_package / "rail.py").write_text("RAIL = None\n") |
| 115 | + |
| 116 | + discovered_manifest = RailManifest(name="content_safety") |
| 117 | + fake_module = types.SimpleNamespace(RAIL=discovered_manifest) |
| 118 | + |
| 119 | + def fake_import(module_name): |
| 120 | + assert module_name == "nemoguardrails.library.content_safety.rail" |
| 121 | + return fake_module |
| 122 | + |
| 123 | + monkeypatch.setattr(catalog_module.importlib, "import_module", fake_import) |
| 124 | + |
| 125 | + catalog = RailCatalog.discover_built_ins(library_path=tmp_path) |
| 126 | + |
| 127 | + assert set(catalog.manifests) == {"content_safety"} |
| 128 | + assert catalog.manifests["content_safety"].origin == "nemoguardrails.library.content_safety.rail" |
| 129 | + |
| 130 | + |
| 131 | +def test_discover_built_ins_rejects_non_manifest_rail(tmp_path, monkeypatch): |
| 132 | + rail_package = tmp_path / "broken" |
| 133 | + rail_package.mkdir() |
| 134 | + (rail_package / "rail.py").write_text("RAIL = 1\n") |
| 135 | + |
| 136 | + monkeypatch.setattr( |
| 137 | + catalog_module.importlib, |
| 138 | + "import_module", |
| 139 | + lambda module_name: types.SimpleNamespace(RAIL="not-a-manifest"), |
| 140 | + ) |
| 141 | + |
| 142 | + with pytest.raises(TypeError, match="must define RAIL"): |
| 143 | + RailCatalog.discover_built_ins(library_path=tmp_path) |
| 144 | + |
| 145 | + |
| 146 | +class _FakeDistribution: |
| 147 | + def __init__(self, name): |
| 148 | + self.name = name |
| 149 | + |
| 150 | + |
| 151 | +class _FakeEntryPoint: |
| 152 | + def __init__(self, name, value, manifest, distribution="plugin-dist"): |
| 153 | + self.name = name |
| 154 | + self.value = value |
| 155 | + self.dist = _FakeDistribution(distribution) |
| 156 | + self._manifest = manifest |
| 157 | + |
| 158 | + def load(self): |
| 159 | + return self._manifest |
| 160 | + |
| 161 | + |
| 162 | +def _patch_entry_points(monkeypatch, entry_points): |
| 163 | + class _FakeEntryPoints: |
| 164 | + def select(self, group): |
| 165 | + assert group == "nemoguardrails.rails" |
| 166 | + return list(entry_points) |
| 167 | + |
| 168 | + monkeypatch.setattr(catalog_module.importlib.metadata, "entry_points", lambda: _FakeEntryPoints()) |
| 169 | + |
| 170 | + |
| 171 | +def test_with_plugins_empty_returns_same_catalog(): |
| 172 | + catalog = RailCatalog() |
| 173 | + assert catalog.with_plugins([]) is catalog |
| 174 | + |
| 175 | + |
| 176 | +def test_with_plugins_loads_entry_point(monkeypatch): |
| 177 | + plugin_manifest = RailManifest(name="my_plugin") |
| 178 | + _patch_entry_points(monkeypatch, [_FakeEntryPoint("my_plugin", "my_pkg:RAIL", plugin_manifest)]) |
| 179 | + |
| 180 | + extended = RailCatalog().with_plugins(["my_plugin"]) |
| 181 | + |
| 182 | + assert "my_plugin" in extended.manifests |
| 183 | + assert extended.records["my_plugin"].distribution == "plugin-dist" |
| 184 | + assert extended.records["my_plugin"].built_in is False |
| 185 | + |
| 186 | + |
| 187 | +def test_with_plugins_rejects_missing_plugin(monkeypatch): |
| 188 | + _patch_entry_points(monkeypatch, []) |
| 189 | + |
| 190 | + with pytest.raises(ValueError, match="not installed"): |
| 191 | + RailCatalog().with_plugins(["absent"]) |
| 192 | + |
| 193 | + |
| 194 | +def test_with_plugins_rejects_ambiguous_distribution(monkeypatch): |
| 195 | + manifest = RailManifest(name="dup") |
| 196 | + _patch_entry_points( |
| 197 | + monkeypatch, |
| 198 | + [ |
| 199 | + _FakeEntryPoint("dup", "one:RAIL", manifest, distribution="pkg-one"), |
| 200 | + _FakeEntryPoint("dup", "two:RAIL", manifest, distribution="pkg-two"), |
| 201 | + ], |
| 202 | + ) |
| 203 | + |
| 204 | + with pytest.raises(ValueError, match="multiple distributions"): |
| 205 | + RailCatalog().with_plugins(["dup"]) |
| 206 | + |
| 207 | + |
| 208 | +def test_with_plugins_rejects_name_mismatch(monkeypatch): |
| 209 | + _patch_entry_points(monkeypatch, [_FakeEntryPoint("declared", "pkg:RAIL", RailManifest(name="actual"))]) |
| 210 | + |
| 211 | + with pytest.raises(ValueError, match="returned manifest"): |
| 212 | + RailCatalog().with_plugins(["declared"]) |
0 commit comments