|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import asyncio |
| 16 | +import json |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from veadk.skills import utils |
| 21 | +from veadk.skills import registry as registry_module |
| 22 | +from veadk.skills.policy import ( |
| 23 | + MAX_SKILL_SPACE_POLICY_BYTES, |
| 24 | + SkillSpacePolicyError, |
| 25 | + parse_skill_space_policy, |
| 26 | +) |
| 27 | +from veadk.skills.skill import Skill |
| 28 | +from veadk.skills.registry import VeSkillRegistry |
| 29 | + |
| 30 | + |
| 31 | +def _skill(skill_id: str | None, name: str) -> Skill: |
| 32 | + return Skill( |
| 33 | + id=skill_id, |
| 34 | + name=name, |
| 35 | + description=f"{name} description", |
| 36 | + path=f"skills/{name}.zip", |
| 37 | + skill_space_id="ss-test", |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize( |
| 42 | + ("raw_value", "message"), |
| 43 | + [ |
| 44 | + ('{"mode":"other","ids":[]}', "mode"), |
| 45 | + ('{"mode":"allow","ids":"skill-1"}', "ids must be a list"), |
| 46 | + ('{"mode":"allow","ids":[""]}', "non-empty strings"), |
| 47 | + ('{"mode":"allow","ids":[],"v":1}', "exactly 'mode' and 'ids'"), |
| 48 | + ('{"ref":"skill-policy-1"}', "exactly 'mode' and 'ids'"), |
| 49 | + ], |
| 50 | +) |
| 51 | +def test_parse_skill_space_policy_rejects_unsupported_shapes(raw_value, message): |
| 52 | + with pytest.raises(SkillSpacePolicyError, match=message): |
| 53 | + parse_skill_space_policy(raw_value) |
| 54 | + |
| 55 | + |
| 56 | +def test_parse_skill_space_policy_rejects_values_over_create_session_limit(): |
| 57 | + raw_value = "x" * (MAX_SKILL_SPACE_POLICY_BYTES + 1) |
| 58 | + |
| 59 | + with pytest.raises(SkillSpacePolicyError, match="must not exceed 8192 bytes"): |
| 60 | + parse_skill_space_policy(raw_value) |
| 61 | + |
| 62 | + |
| 63 | +def test_policy_json_is_compact_and_deduplicated(): |
| 64 | + policy = parse_skill_space_policy( |
| 65 | + '{"mode": "deny", "ids": ["skill-2", "skill-1", "skill-2"]}' |
| 66 | + ) |
| 67 | + |
| 68 | + assert policy.to_json() == '{"mode":"deny","ids":["skill-1","skill-2"]}' |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize( |
| 72 | + ("mode", "ids", "expected"), |
| 73 | + [ |
| 74 | + ("deny", ["skill-2"], ["skill-1"]), |
| 75 | + ("allow", ["skill-2"], ["skill-2"]), |
| 76 | + ("deny", [], ["skill-1", "skill-2"]), |
| 77 | + ("allow", [], []), |
| 78 | + ], |
| 79 | +) |
| 80 | +def test_load_skills_from_cloud_applies_policy( |
| 81 | + monkeypatch: pytest.MonkeyPatch, |
| 82 | + mode: str, |
| 83 | + ids: list[str], |
| 84 | + expected: list[str], |
| 85 | +): |
| 86 | + monkeypatch.setenv( |
| 87 | + "SKILL_SPACE_POLICY", |
| 88 | + json.dumps({"mode": mode, "ids": ids}), |
| 89 | + ) |
| 90 | + monkeypatch.setattr( |
| 91 | + utils, |
| 92 | + "_load_skills_from_space_id", |
| 93 | + lambda _space_id, *, raise_on_error=False: [ |
| 94 | + _skill("skill-1", "one"), |
| 95 | + _skill("skill-2", "two"), |
| 96 | + ], |
| 97 | + ) |
| 98 | + |
| 99 | + skills = utils.load_skills_from_cloud("ss-test") |
| 100 | + |
| 101 | + assert [skill.id for skill in skills] == expected |
| 102 | + |
| 103 | + |
| 104 | +def test_missing_policy_keeps_all_remote_skills(monkeypatch: pytest.MonkeyPatch): |
| 105 | + monkeypatch.delenv("SKILL_SPACE_POLICY", raising=False) |
| 106 | + monkeypatch.setattr( |
| 107 | + utils, |
| 108 | + "_load_skills_from_space_id", |
| 109 | + lambda _space_id, *, raise_on_error=False: [ |
| 110 | + _skill("skill-1", "one"), |
| 111 | + _skill("skill-2", "two"), |
| 112 | + ], |
| 113 | + ) |
| 114 | + |
| 115 | + skills = utils.load_skills_from_cloud("ss-test") |
| 116 | + |
| 117 | + assert [skill.id for skill in skills] == ["skill-1", "skill-2"] |
| 118 | + |
| 119 | + |
| 120 | +def test_policy_excludes_remote_skills_without_stable_ids( |
| 121 | + monkeypatch: pytest.MonkeyPatch, |
| 122 | +): |
| 123 | + monkeypatch.setenv( |
| 124 | + "SKILL_SPACE_POLICY", |
| 125 | + '{"mode":"deny","ids":[]}', |
| 126 | + ) |
| 127 | + monkeypatch.setattr( |
| 128 | + utils, |
| 129 | + "_load_skills_from_space_id", |
| 130 | + lambda _space_id, *, raise_on_error=False: [_skill(None, "missing-id")], |
| 131 | + ) |
| 132 | + |
| 133 | + assert utils.load_skills_from_cloud("ss-test") == [] |
| 134 | + |
| 135 | + |
| 136 | +def test_invalid_policy_disables_remote_skills_without_listing_space( |
| 137 | + monkeypatch: pytest.MonkeyPatch, |
| 138 | +): |
| 139 | + monkeypatch.setenv("SKILL_SPACE_POLICY", '{"mode":"allow","ids":[],"v":1}') |
| 140 | + monkeypatch.setattr( |
| 141 | + utils, |
| 142 | + "_load_skills_from_space_id", |
| 143 | + lambda *_args, **_kwargs: pytest.fail("invalid policy must fail closed"), |
| 144 | + ) |
| 145 | + |
| 146 | + assert utils.load_skills_from_cloud("ss-test") == [] |
| 147 | + |
| 148 | + |
| 149 | +def test_registry_get_skill_cannot_bypass_deny_policy( |
| 150 | + monkeypatch: pytest.MonkeyPatch, |
| 151 | +): |
| 152 | + monkeypatch.setenv( |
| 153 | + "SKILL_SPACE_POLICY", |
| 154 | + '{"mode":"deny","ids":["skill-1"]}', |
| 155 | + ) |
| 156 | + monkeypatch.setattr( |
| 157 | + utils, |
| 158 | + "_load_skills_from_space_id", |
| 159 | + lambda _space_id, *, raise_on_error=False: [_skill("skill-1", "one")], |
| 160 | + ) |
| 161 | + monkeypatch.setattr( |
| 162 | + registry_module, |
| 163 | + "materialize_remote_skill", |
| 164 | + lambda *_args, **_kwargs: pytest.fail("denied Skill must not be downloaded"), |
| 165 | + ) |
| 166 | + |
| 167 | + registry = VeSkillRegistry(skill_source_id="ss-test") |
| 168 | + |
| 169 | + with pytest.raises(ValueError, match="not found"): |
| 170 | + asyncio.run(registry.get_skill(name="one")) |
0 commit comments