|
| 1 | +"""Shared fixtures for tool-safety tests. |
| 2 | +
|
| 3 | +The fixtures here build minimal in-memory policies so tests do not touch |
| 4 | +the filesystem. They also expose the sample scripts shipped under |
| 5 | +``trpc_agent_sdk/tools/safety/examples/samples`` so integration tests can |
| 6 | +re-use them without copying the bodies. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from pathlib import Path |
| 12 | +from typing import Any |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from trpc_agent_sdk.tools.safety._policy import ( |
| 17 | + POLICY_VERSION, |
| 18 | + load_safety_policy_dict, |
| 19 | +) |
| 20 | +from trpc_agent_sdk.tools.safety._models import ( |
| 21 | + SafetyScanRequest, |
| 22 | + ScriptLanguage, |
| 23 | + ToolKind, |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +SAMPLES_DIR = Path(__file__).resolve().parents[3] \ |
| 28 | + / "trpc_agent_sdk" / "tools" / "safety" / "examples" / "samples" |
| 29 | + |
| 30 | + |
| 31 | +def _policy_dict(**overrides: Any) -> dict[str, Any]: |
| 32 | + data: dict[str, Any] = { |
| 33 | + "version": POLICY_VERSION, |
| 34 | + "network": {"allow_domains": ["api.example.com", "*.example.com"]}, |
| 35 | + "commands": { |
| 36 | + "allow": ["python", "ls"], |
| 37 | + "deny": ["rm"], |
| 38 | + }, |
| 39 | + "paths": {"deny": ["/etc/**", "~/.ssh/**", "/root/**"]}, |
| 40 | + "limits": { |
| 41 | + "max_timeout_seconds": 60.0, |
| 42 | + "max_output_bytes": 1024, |
| 43 | + "max_script_bytes": 262144, |
| 44 | + "max_sleep_seconds": 30.0, |
| 45 | + "max_parallel_tasks": 16, |
| 46 | + "max_processes": 8, |
| 47 | + "max_file_write_bytes": 1024, |
| 48 | + }, |
| 49 | + "defaults": { |
| 50 | + "unknown_construct": "needs_human_review", |
| 51 | + "guard_error": "deny", |
| 52 | + "human_review_blocks_execution": True, |
| 53 | + }, |
| 54 | + "dependencies": {"decision": "deny"}, |
| 55 | + "audit": {"enabled": False, "required": False}, |
| 56 | + } |
| 57 | + for key, value in overrides.items(): |
| 58 | + data[key] = value |
| 59 | + return data |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture() |
| 63 | +def policy_dict() -> dict[str, Any]: |
| 64 | + return _policy_dict() |
| 65 | + |
| 66 | + |
| 67 | +@pytest.fixture() |
| 68 | +def policy_factory(): |
| 69 | + """Callable that builds a fresh policy from overrides.""" |
| 70 | + |
| 71 | + return load_safety_policy_dict |
| 72 | + |
| 73 | + |
| 74 | +@pytest.fixture() |
| 75 | +def make_policy(): |
| 76 | + """Build a policy with overrides applied.""" |
| 77 | + |
| 78 | + def _make(**overrides: Any): |
| 79 | + return load_safety_policy_dict(_policy_dict(**overrides)) |
| 80 | + |
| 81 | + return _make |
| 82 | + |
| 83 | + |
| 84 | +@pytest.fixture() |
| 85 | +def default_policy(make_policy): |
| 86 | + return make_policy() |
| 87 | + |
| 88 | + |
| 89 | +@pytest.fixture() |
| 90 | +def scan_request_factory(): |
| 91 | + """Build a SafetyScanRequest with sensible defaults.""" |
| 92 | + |
| 93 | + def _make( |
| 94 | + *, |
| 95 | + tool_name: str = "test_tool", |
| 96 | + tool_kind: ToolKind = ToolKind.UNKNOWN, |
| 97 | + language: ScriptLanguage = ScriptLanguage.UNKNOWN, |
| 98 | + script: str = "", |
| 99 | + argv: tuple[str, ...] = (), |
| 100 | + cwd: str | None = None, |
| 101 | + env: dict[str, str] | None = None, |
| 102 | + metadata: dict[str, Any] | None = None, |
| 103 | + requested_timeout_seconds: float | None = None, |
| 104 | + requested_output_bytes: int | None = None, |
| 105 | + ) -> SafetyScanRequest: |
| 106 | + return SafetyScanRequest( |
| 107 | + tool_name=tool_name, |
| 108 | + tool_kind=tool_kind, |
| 109 | + language=language, |
| 110 | + script=script, |
| 111 | + argv=argv, |
| 112 | + cwd=cwd, |
| 113 | + env=env or {}, |
| 114 | + metadata=metadata or {}, |
| 115 | + requested_timeout_seconds=requested_timeout_seconds, |
| 116 | + requested_output_bytes=requested_output_bytes, |
| 117 | + ) |
| 118 | + |
| 119 | + return _make |
| 120 | + |
| 121 | + |
| 122 | +@pytest.fixture() |
| 123 | +def samples_dir() -> Path: |
| 124 | + return SAMPLES_DIR |
| 125 | + |
| 126 | + |
| 127 | +@pytest.fixture() |
| 128 | +def sample_script(samples_dir): |
| 129 | + """Return the body of a sample script by file name.""" |
| 130 | + |
| 131 | + def _read(name: str) -> str: |
| 132 | + return (samples_dir / name).read_text(encoding="utf-8") |
| 133 | + |
| 134 | + return _read |
0 commit comments