## Summary
An Ark base (host `*.volces.com`) speaks its own dialect on the Images
generations route: `size` is a "1K"/"2K" resolution tier or an explicit
WxH pair bounded by the selected model's pixel window, and the answer
carries the picture behind `data[].url` rather than `b64_json`. Before
this, every call came back with no bytes and answered "no image
returned".
Nine commits, in order:
1. Take the dialect at all: send the tier where the model family takes
one, and take a `data` entry's picture wherever it is -- inline
`b64_json`, data-URI, or behind its URL, fetched through the guarded
fetch like any other provider URL.
2. Keep the asked-for ratio: an identified model gets an explicit WxH
pair fitted to its own pixel window at the 2K budget, instead of a bare
"2K" tier.
3. Fit the pair to the window exactly: a Seedream 3.0-t2i caps each side
at 2048, so 16:9 there is 2048x1152, not 2560x1440.
4. A refused or unreachable generated-picture URL now answers its own
error ("generated, but the picture URL was refused or could not be
fetched", with the url and detail) instead of "no image returned", so an
orchestrating model does not read a billed generation as "no image" and
pay for the picture twice. Scoped to volces bases at first; items 8 and
9 widen it.
5. An Ark inference endpoint id (`ep-...`) keeps the requested aspect
ratio (an endpoint id spells no family keyword). When the model family
is unknown and the sized call answers HTTP 400, the request is retried
once with no size so the endpoint's default frame stands -- a rejected
request is not a billed generation. Identified families are not retried:
their windows are documented, and a 400 there is a real error.
6. VolcEngine joins the image role. Its own address is Ark's
(`https://ark.cn-beijing.volces.com/api/v3`), so the registry now marks
it `image_api`: the roles card offers it for the image role, the
settings write accepts it, and `raven_config` lists it among the choices
for `tools.media.image.provider`. Before, this dialect was reachable
from settings only through a Custom provider pointed at the Ark address.
7. A call with reference images on an Ark base is refused with "image
editing is not supported on Ark" before anything is sent. It used to
post the multipart edit form to Ark, which has no `/images/edits` route,
and come back as an HTTP error.
8. Item 4's error now answers on every Images API base, not only volces
ones: a gateway that answers in URL form has billed the picture too, and
its refused or unreachable picture URL used to read as "no image
returned" or as a bare transport error.
9. A picture URL whose storage answers the download with an error status
(an expired signature's 403, a 404, a 5xx) gets item 4's error too, with
the status and the start of the response body in its detail. It used to
fall through to the generic HTTP error and read as the generation
request's own `HTTP 403`, whose hint tells the model to retry: the
double payment item 4 exists to prevent.
Deliberately left for a follow-up PR: image editing on an Ark base. Ark
has no `/images/edits` route; image-to-image goes through
`/images/generations` with an `image` field. Wiring that dialect is out
of scope here; until then such a call gets the refusal in item 7.
## Type
- [x] Fix
## Verification
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- `uv run pytest tests/test_media_gen_tool.py` -- 141 passed (127
pre-existing, untouched, + 14 new: 2 on the refused-url path, 5 on the
endpoint-id path, 1 for a generation on `provider: volcengine`, 1 for
the edit refusal, 2 for a gateway's unfetchable picture URL, 3 for a
download answered with an error status).
- `uv run pytest tests/test_rpc_model.py tests/test_rpc_console.py
tests/test_raven_config_tool.py` -- 561 passed: the roles card rows (the
expected list gains `volcengine`), the settings write and `raven_config`
each admit VolcEngine.
- `uv run pytest tests/test_ppt_engine_generate_image_tool.py` -- 13
passed (the deck engine consumes this tool; 154 together).
- `make lint-python` -- clean.
- All offline, httpx MockTransport, no live network. The size arithmetic
is hand-enumerated for every public ratio against every window. The new
refusal test pins the SSRF guard on the download path: replacing
`guarded_fetch` with a bare `client.get` fails it.
- The new tests were watched failing first: the three settings tests
with VolcEngine unmarked, the edit-refusal test without the refusal
(dropping either half of its condition fails 17 and 13 other tests), the
two gateway cases with item 4's error still volces-only, and the three
error-status cases while the download still went through
`raise_for_status()` (they answered `HTTP 403`, `HTTP 404` and `HTTP
500`). The exception is the `provider: volcengine` generation test,
which passed before the flag as well, since the borrowing was already
wired; it fails when VolcEngine's default address leaves Ark or when the
URL download is dropped.
- `uv run --all-extras pytest` on `main` at `a2d391ea4` plus this
branch: 32664 passed, 176 skipped, 3 failed, none introduced: the same
three fail at that base on this machine (a LibreOffice conversion, the
node-directory resolution, a read-permission case that root bypasses).
## Risk
- [x] Security impact considered
- [x] Backward compatibility considered
On non-Ark bases only a failure reads differently: a picture URL the
tool cannot fetch answers item 4's error. The ratio pass-through, the
400 retry and the edit refusal are volces-only. VolcEngine now appears
among the image role's providers wherever it is connected. The edit
refusal rests on Ark's documented routes, not on a live call. The video
tool's download has the same shape as item 9 (a completed job whose
content URL answers 403 reads as the request's own `HTTP 403`); that
path predates this PR and is left for a separate change. Rollback is
reverting the squash commit.
## Related Issues
N/A
---------
Co-authored-by: helalove0812-source <helalove0812-source@users.noreply.github.com>
Co-authored-by: Dizhan Xue <xuedizhan17@mails.ucas.ac.cn>
3021 lines
128 KiB
Python
3021 lines
128 KiB
Python
"""Tests for the console surface and for the guarantees the gateway rests on.
|
|
|
|
Everything here started life as a mutation that survived the suite: break the
|
|
implementation, and nothing went red. Each test below is the answer to one of
|
|
those, so the property it pins is one somebody already proved was unguarded.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
import raven.agent.subagent.backends.env as backend_env
|
|
import raven.home as raven_home_module
|
|
from raven.config import update_tools
|
|
from raven.config.env_file import MIRRORED_KEYS
|
|
from raven.config.schema import WEB_VENDOR_ENV_VARS
|
|
from raven.contracts.tool import Tool
|
|
from raven.rpc import folder_dialog
|
|
from raven.rpc.errors import ConfigValidationError
|
|
from raven.rpc.methods import console as console_module
|
|
from raven.rpc.methods.console import _SETTINGS_SIMPLE_KEYS, _hub_marker_name
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# settings.set's whitelist is a security boundary, so it is pinned exactly
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_the_settings_whitelist_holds_no_containment_control() -> None:
|
|
"""These three decide what an attacker who reaches the RPC surface can then
|
|
do, and the surface has no confirmation step. Adding one back should take an
|
|
argument, which is what failing this test forces."""
|
|
for forbidden in ("tools.restrictToWorkspace", "tools.sandbox.backend", "tools.web.proxy"):
|
|
assert forbidden not in _SETTINGS_SIMPLE_KEYS, (
|
|
f"{forbidden} weakens containment and must not be settable over RPC without confirmation"
|
|
)
|
|
|
|
|
|
def test_the_settings_whitelist_is_exactly_this_set() -> None:
|
|
"""Pinned wholesale, not just the denials: a key added here becomes reachable
|
|
from every RPC client at once, so it should be a deliberate edit rather than
|
|
a line that rode in with something else."""
|
|
assert set(_SETTINGS_SIMPLE_KEYS) == {
|
|
"tools.exec.timeout",
|
|
"tools.web.search.apiKey",
|
|
"tools.web.jinaApiKey",
|
|
"tools.web.search.provider",
|
|
"tools.web.fetch.provider",
|
|
*(
|
|
f"tools.web.providers.{vendor}.apiKey"
|
|
for vendor in ("serper", "anysearch", "serpapi", "jina", "tavily", "exa", "brave", "firecrawl", "serply")
|
|
),
|
|
"tools.media.image.apiKey",
|
|
"tools.media.image.model",
|
|
"tools.media.image.quality",
|
|
"tools.media.image",
|
|
# Which configured provider the image tool runs on. The same reach as the
|
|
# pins below: the tool moves to an address and key the operator already
|
|
# configured for that provider, and the name is checked against the ones
|
|
# whose image API the tool can call. It names no address and carries no key.
|
|
"tools.media.image.provider",
|
|
"channels.sendProgress",
|
|
"channels.sendToolHints",
|
|
"memory.memoryTopK",
|
|
"agents.defaults.enablePersonalization",
|
|
"agents.defaults.reasoningEffort",
|
|
# The default-model pins, the same shape as tools.media.image.model
|
|
# above: which model a subsystem uses and which configured provider
|
|
# serves it. Neither half is a containment control, and the provider
|
|
# half is checked against the registry -- but note what it does reach,
|
|
# since that is the argument this list asks for: it can move a
|
|
# subsystem's traffic to another provider the deployment has already
|
|
# credentialed. It cannot introduce one, name an address, or carry a
|
|
# key, so the set of endpoints reachable through it is the set the
|
|
# operator already chose.
|
|
"sessionTitle.model",
|
|
"sessionTitle.provider",
|
|
"translate.model",
|
|
"translate.provider",
|
|
"embedding.model",
|
|
"embedding.provider",
|
|
"vision.model",
|
|
"vision.provider",
|
|
# The pair keys. No new reach over the six leaves above: each delegates
|
|
# to their validators and writes the same two fields. What it adds is
|
|
# that both land in one transaction, which is what a settings surface
|
|
# writes so a pin cannot be persisted half-applied.
|
|
"sessionTitle",
|
|
"translate",
|
|
"embedding",
|
|
"vision",
|
|
# No new reach: the same key has been writable through config.set (the
|
|
# chip and /perm) since the gate landed; this lets the settings panel
|
|
# write the default a new conversation starts on.
|
|
"permissions.mode",
|
|
# The settings page's model page. Two more pins of the sessionTitle
|
|
# shape (curator, skill gate) with their pair keys, the speech and video
|
|
# selections in the image selection's shape, and three integers: the
|
|
# tool-iteration cap, the context window override and the auto-archive
|
|
# age. None names an address or carries a key; the pins reach only
|
|
# providers the deployment has already credentialed.
|
|
"context.curatorModel",
|
|
"context.curatorProvider",
|
|
"skillForge.llmGateModel",
|
|
"skillForge.llmGateProvider",
|
|
"context",
|
|
"skillForge",
|
|
"tools.media.speech",
|
|
"tools.media.video",
|
|
"agents.defaults.maxToolIterations",
|
|
"agents.defaults.contextWindowTokens",
|
|
"sessions.autoArchiveAfterDays",
|
|
}
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# ext.list survives without the skill market
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_the_hub_marker_is_none_when_the_market_is_absent(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""The market is an optional install. Its absence is a normal state, so the
|
|
lookup answers ``None`` rather than raising -- a raise here used to cost the
|
|
whole skill list, not just the two hub fields."""
|
|
import builtins
|
|
|
|
real_import = builtins.__import__
|
|
|
|
def _no_skillhub(name, *args, **kwargs):
|
|
if name == "raven.skill_hub.hub":
|
|
raise ImportError("market not installed")
|
|
return real_import(name, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(builtins, "__import__", _no_skillhub)
|
|
assert _hub_marker_name() is None
|
|
|
|
|
|
def test_the_install_meta_name_is_none_when_the_writer_is_absent(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""The second stamp is read from the module that writes it, on the same
|
|
terms as the first: absent, it costs the bundle half of the hub flag and
|
|
nothing else -- never the skill list."""
|
|
import builtins
|
|
|
|
real_import = builtins.__import__
|
|
|
|
def _no_audit(name, *args, **kwargs):
|
|
if name == "raven.skill_hub.audit":
|
|
raise ImportError("market not installed")
|
|
return real_import(name, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(builtins, "__import__", _no_audit)
|
|
assert console_module._install_meta_name() is None
|
|
|
|
|
|
# The other half of this -- that an installed market yields *its* marker name,
|
|
# so hub-installed skills are recognised -- belongs with the market itself and
|
|
# is not testable here: nothing in this tree provides skillhub.
|
|
|
|
|
|
async def test_ext_list_still_reports_skills_without_the_market(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""The regression this guards: the market import sat inside the loop's try,
|
|
so a missing market did not cost the hub flag -- it cost every skill."""
|
|
|
|
class _Skill:
|
|
def __init__(self, name: str) -> None:
|
|
self.name = name
|
|
self.description = "d"
|
|
self.source = "builtin"
|
|
self.always = False
|
|
self.path = tmp_path / name / "SKILL.md"
|
|
|
|
class _Catalog:
|
|
def gather_all_skills(self):
|
|
return [_Skill("alpha"), _Skill("beta")]
|
|
|
|
# Both config loaders are stubbed: this asserts about skills, and reading
|
|
# the developer's own ~/.raven/config.json made the test fail on any machine
|
|
# whose config is newer than the schema on the branch under test.
|
|
from raven.config import loader as config_loader
|
|
from raven.config import raven as raven_config
|
|
|
|
monkeypatch.setattr(config_loader, "load_config", lambda: SimpleNamespace(tools=SimpleNamespace(mcp_servers={})))
|
|
monkeypatch.setattr(
|
|
raven_config,
|
|
"load_raven_config",
|
|
# Both attributes the handler reads. With only `skill_forge` the
|
|
# plugin block raised on `ec.plugins.disabled` and its `except` swallowed
|
|
# it, so a quarter of ext.list sat unexecuted for the whole suite.
|
|
lambda: SimpleNamespace(skill_forge=None, plugins=SimpleNamespace(disabled=[])),
|
|
)
|
|
monkeypatch.setattr(console_module, "_hub_marker_name", lambda: None)
|
|
loop = SimpleNamespace(
|
|
context=SimpleNamespace(skills=_Catalog()),
|
|
tools=SimpleNamespace(tool_names=[], get=lambda _name: None, schema_hidden_names=frozenset),
|
|
)
|
|
|
|
result = await console_module.ext_list({}, agent_loop_factory=lambda: loop)
|
|
|
|
assert [s["name"] for s in result["skills"]] == ["alpha", "beta"]
|
|
assert all(s["hub"] is False and s["hub_id"] == "" for s in result["skills"])
|
|
|
|
|
|
async def test_ext_list_calls_a_skill_hub_installed_whichever_installer_stamped_it(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""Two installers, two stamps, one flag. The market module writes
|
|
``.skillhub.json`` beside the skill; the context engine and ``use_skill``
|
|
cache a bundle and write ``.install-meta.json`` there instead. The page
|
|
gates removal on ``hub`` alone, so asking about only the first stamp made
|
|
the common install path look local and hid the control for it.
|
|
|
|
The bundle stamp carries a slug, not a market id, so that skill is
|
|
hub-installed with an empty ``hub_id`` -- which is a state the page already
|
|
handles by not fetching market detail.
|
|
"""
|
|
|
|
class _Skill:
|
|
def __init__(self, name: str, source: str) -> None:
|
|
self.name = name
|
|
self.description = "d"
|
|
self.source = source
|
|
self.always = False
|
|
self.path = tmp_path / name / "SKILL.md"
|
|
|
|
market = _Skill("from-market", "hub")
|
|
bundle = _Skill("from-bundle", "hub")
|
|
local = _Skill("by-hand", "workspace")
|
|
for skill in (market, bundle, local):
|
|
skill.path.parent.mkdir(parents=True)
|
|
skill.path.write_text("# s")
|
|
(market.path.parent / ".skillhub.json").write_text('{"id": "uuid-market"}')
|
|
(bundle.path.parent / ".install-meta.json").write_text('{"slug": "acme_bundle", "version": "v0"}')
|
|
|
|
class _Catalog:
|
|
def gather_all_skills(self):
|
|
return [market, bundle, local]
|
|
|
|
from raven.config import loader as config_loader
|
|
from raven.config import raven as raven_config
|
|
|
|
monkeypatch.setattr(config_loader, "load_config", lambda: SimpleNamespace(tools=SimpleNamespace(mcp_servers={})))
|
|
monkeypatch.setattr(
|
|
raven_config,
|
|
"load_raven_config",
|
|
lambda: SimpleNamespace(skill_forge=None, plugins=SimpleNamespace(disabled=[])),
|
|
)
|
|
# Pinned to the literal file names rather than read through the market
|
|
# module: the assertion is about which files on disk mean "installed", and
|
|
# a test that imported the names could not notice them drifting apart.
|
|
monkeypatch.setattr(console_module, "_hub_marker_name", lambda: ".skillhub.json")
|
|
monkeypatch.setattr(console_module, "_install_meta_name", lambda: ".install-meta.json")
|
|
loop = SimpleNamespace(
|
|
context=SimpleNamespace(skills=_Catalog()),
|
|
tools=SimpleNamespace(tool_names=[], get=lambda _name: None),
|
|
)
|
|
|
|
result = await console_module.ext_list({}, agent_loop_factory=lambda: loop)
|
|
|
|
by_name = {s["name"]: s for s in result["skills"]}
|
|
assert (by_name["from-market"]["hub"], by_name["from-market"]["hub_id"]) == (True, "uuid-market")
|
|
assert (by_name["from-bundle"]["hub"], by_name["from-bundle"]["hub_id"]) == (True, "")
|
|
assert (by_name["by-hand"]["hub"], by_name["by-hand"]["hub_id"]) == (False, "")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# system.upgrade refuses outside a running gateway
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
async def test_system_upgrade_refuses_when_no_gateway_is_running() -> None:
|
|
"""The helper waits for *this* process to exit and relaunches it, so it is
|
|
only meaningful inside `raven serve`. On the TUI path the guard is what turns
|
|
a meaningless call into a typed refusal instead of a half-done upgrade."""
|
|
from raven.cli.serve_commands import SERVE
|
|
from raven.rpc.errors import ConfigValidationError
|
|
from raven.rpc.methods.system import system_upgrade
|
|
|
|
assert not SERVE.running, "precondition: no gateway armed in a unit test"
|
|
|
|
with pytest.raises(ConfigValidationError) as excinfo:
|
|
await system_upgrade({})
|
|
assert excinfo.value.data["reason"] == "not_serving"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# the two RPC assemblies must not drift
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
async def test_the_gateway_registers_what_the_umbrella_does() -> None:
|
|
"""`raven serve` builds its dispatcher through ``build_rpc_stack``; the TUI
|
|
builds one through the umbrella. A method registered on one and not the
|
|
other is a client-visible difference nobody would notice until a page called
|
|
it, so the two sets are compared directly rather than against a hand-written
|
|
mirror.
|
|
"""
|
|
from raven.rpc.dispatcher import Dispatcher
|
|
from raven.rpc.methods import register_aligned_methods
|
|
|
|
umbrella = Dispatcher()
|
|
register_aligned_methods(umbrella)
|
|
|
|
import raven.rpc.bootstrap as bootstrap_module
|
|
|
|
async def _sink(_frame) -> None:
|
|
return None
|
|
|
|
stack = await bootstrap_module.build_rpc_stack(_sink)
|
|
try:
|
|
gateway_methods = set(stack.dispatcher.methods())
|
|
finally:
|
|
await asyncio.wait_for(stack.teardown(), timeout=30)
|
|
|
|
missing = set(umbrella.methods()) - gateway_methods
|
|
assert not missing, f"the gateway is missing methods the umbrella registers: {sorted(missing)}"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# cron.save: a create-then-edit round trip, which nothing covered
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _cron_loop(tmp_path: Path):
|
|
"""A loop stub carrying a real CronService over a scratch jobs.json.
|
|
|
|
Real, not a fake: the bug this pins was a kwarg the service does not take,
|
|
and a fake would have accepted anything it was handed.
|
|
"""
|
|
from raven.proactive_engine.schedulers.cron.service import CronService
|
|
|
|
return SimpleNamespace(cron_service=CronService(tmp_path / "jobs.json", allowed_channels=None))
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cron_save_creates_a_job(tmp_path: Path) -> None:
|
|
"""The plain create path -- which raised TypeError on every call, because
|
|
`add_job` was handed a `job_id` parameter it did not have."""
|
|
loop = _cron_loop(tmp_path)
|
|
r = await console_module.cron_save(
|
|
{"kind": "every", "every_seconds": 3600, "name": "hourly", "message": "ping"},
|
|
agent_loop_factory=lambda: loop,
|
|
)
|
|
|
|
assert r["job"]["name"] == "hourly"
|
|
assert r["job"]["id"]
|
|
assert [j["name"] for j in (await console_module.cron_list({}, agent_loop_factory=lambda: loop))["jobs"]] == [
|
|
"hourly"
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cron_save_edits_in_place_and_keeps_the_id(tmp_path: Path) -> None:
|
|
"""An edit must land on the same job: run history lives in the `cron:<id>`
|
|
session, so a fresh id orphans it, and a second row would be a duplicate."""
|
|
loop = _cron_loop(tmp_path)
|
|
created = await console_module.cron_save(
|
|
{"kind": "every", "every_seconds": 3600, "name": "hourly", "message": "ping"},
|
|
agent_loop_factory=lambda: loop,
|
|
)
|
|
job_id = created["job"]["id"]
|
|
|
|
edited = await console_module.cron_save(
|
|
{"kind": "every", "every_seconds": 7200, "name": "two-hourly", "message": "pong", "id": job_id},
|
|
agent_loop_factory=lambda: loop,
|
|
)
|
|
|
|
assert edited["job"]["id"] == job_id
|
|
# The in-place branch is the one path that changes a schedule without going
|
|
# through the create path's `_compute_next_run`, so it is the one that can
|
|
# drift: drop that recompute and an edited job keeps aiming at the fire time
|
|
# its old schedule picked -- "every 1h" changed to "every 12h" would still
|
|
# go off at the top of the hour it was already counting down to.
|
|
assert edited["job"]["next_run_at_ms"] != created["job"]["next_run_at_ms"], (
|
|
"an edit must recompute the next fire from the new schedule"
|
|
)
|
|
jobs = (await console_module.cron_list({}, agent_loop_factory=lambda: loop))["jobs"]
|
|
assert len(jobs) == 1
|
|
assert jobs[0]["name"] == "two-hourly"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_rejected_edit_leaves_the_job_alone(tmp_path: Path) -> None:
|
|
"""The edit path used to remove the job before building its replacement, so
|
|
an edit that failed validation deleted the job it was editing."""
|
|
from raven.rpc.errors import ConfigValidationError
|
|
|
|
loop = _cron_loop(tmp_path)
|
|
created = await console_module.cron_save(
|
|
{"kind": "every", "every_seconds": 3600, "name": "hourly", "message": "ping"},
|
|
agent_loop_factory=lambda: loop,
|
|
)
|
|
job_id = created["job"]["id"]
|
|
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.cron_save(
|
|
{"kind": "at", "at_iso": "1999-01-01T00:00:00", "name": "past", "message": "x", "id": job_id},
|
|
agent_loop_factory=lambda: loop,
|
|
)
|
|
|
|
jobs = (await console_module.cron_list({}, agent_loop_factory=lambda: loop))["jobs"]
|
|
assert [j["id"] for j in jobs] == [job_id]
|
|
assert jobs[0]["name"] == "hourly"
|
|
|
|
|
|
def _cron_job(loop):
|
|
"""One recurring job on the stub loop's service, whose id keys its session."""
|
|
from raven.proactive_engine.schedulers.cron.types import CronSchedule
|
|
|
|
return loop.cron_service.add_job(
|
|
name="hourly",
|
|
schedule=CronSchedule(kind="every", every_ms=3_600_000),
|
|
message="ping",
|
|
channel="tui",
|
|
to="direct",
|
|
)
|
|
|
|
|
|
def _cron_session(monkeypatch: pytest.MonkeyPatch, messages: list[dict]) -> None:
|
|
"""Hand ``cron.runs`` one stored ``cron:<id>`` transcript.
|
|
|
|
The handler reaches for the shared session manager and the config from
|
|
inside the call, so both are replaced here rather than on an object.
|
|
"""
|
|
monkeypatch.setattr("raven.config.loader.load_config", lambda: SimpleNamespace())
|
|
monkeypatch.setattr("raven.rpc.methods.session._safe_invoke_factory", lambda _factory: None)
|
|
monkeypatch.setattr(
|
|
"raven.session.resolve.manager_for",
|
|
lambda _loop, _config: SimpleNamespace(peek=lambda _key: SimpleNamespace(messages=messages)),
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cron_runs_reads_a_failed_run_from_its_turn_marker(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""A failed cron turn leaves two assistant messages behind -- the half
|
|
answer that had already streamed, then the marker naming the failure -- and
|
|
both used to read as a reply, so the run was drawn ok and the job's error
|
|
was tacked on as a second, phantom run."""
|
|
loop = _cron_loop(tmp_path)
|
|
job = _cron_job(loop)
|
|
job.state.last_status = "error"
|
|
job.state.last_error = "RateLimitError: 429 slow down"
|
|
_cron_session(
|
|
monkeypatch,
|
|
[
|
|
{"role": "user", "content": "ping", "timestamp": "2026-09-22T10:00:00"},
|
|
{"role": "assistant", "content": "starting on it"},
|
|
{
|
|
"role": "assistant",
|
|
"content": "(turn failed: RateLimitError: 429 slow down)",
|
|
"turn_ended": {"status": "failed", "reason": "RateLimitError: 429 slow down"},
|
|
},
|
|
],
|
|
)
|
|
|
|
runs = (await console_module.cron_runs({"id": job.id}, agent_loop_factory=lambda: loop))["runs"]
|
|
|
|
assert len(runs) == 1, "the failure is the run, not a row of its own"
|
|
assert runs[0]["ok"] is False
|
|
assert runs[0]["preview"] == "RateLimitError: 429 slow down"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cron_runs_keeps_a_cancelled_runs_text_when_the_marker_names_no_reason(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""A cancel files a marker with no reason, so what the reader gets is what
|
|
the turn had already said -- but the run is still not an ok one."""
|
|
loop = _cron_loop(tmp_path)
|
|
job = _cron_job(loop)
|
|
_cron_session(
|
|
monkeypatch,
|
|
[
|
|
{"role": "user", "content": "ping", "timestamp": "2026-09-22T10:00:00"},
|
|
{"role": "assistant", "content": "starting on it"},
|
|
{
|
|
"role": "assistant",
|
|
"content": "(turn cancelled by the user)",
|
|
"turn_ended": {"status": "cancelled"},
|
|
},
|
|
],
|
|
)
|
|
|
|
runs = (await console_module.cron_runs({"id": job.id}, agent_loop_factory=lambda: loop))["runs"]
|
|
|
|
assert [(r["ok"], r["preview"]) for r in runs] == [(False, "starting on it")]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cron_runs_still_reads_a_delivered_run_as_ok(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
loop = _cron_loop(tmp_path)
|
|
job = _cron_job(loop)
|
|
_cron_session(
|
|
monkeypatch,
|
|
[
|
|
{"role": "user", "content": "ping", "timestamp": "2026-09-22T10:00:00"},
|
|
{"role": "assistant", "content": "reminder delivered"},
|
|
],
|
|
)
|
|
|
|
runs = (await console_module.cron_runs({"id": job.id}, agent_loop_factory=lambda: loop))["runs"]
|
|
|
|
assert [(r["ok"], r["preview"]) for r in runs] == [(True, "reminder delivered")]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# ext.list reports MCP from what this branch actually knows
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ext_list_reports_a_server_by_the_tools_it_registered(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""A server whose tools are registered is connected, and its tools name it.
|
|
|
|
This read `loop.mcp_manager`, which `AgentLoop` does not have, so `manager`
|
|
was always None and every configured server was reported disconnected with
|
|
zero tools -- including one the agent was calling right then.
|
|
"""
|
|
|
|
# A stub rather than the real config: `ext.list` reads exactly one field off
|
|
# it, and loading the machine's own config makes the test depend on whatever
|
|
# the developer happens to have installed. The shape is what
|
|
# `resolve_transport` reads: type, then command, then url.
|
|
def _server(url: str) -> SimpleNamespace:
|
|
return SimpleNamespace(enabled=True, type=None, command=None, url=url)
|
|
|
|
cfg = SimpleNamespace(
|
|
tools=SimpleNamespace(
|
|
mcp_servers={
|
|
"ctx7": _server("https://example.invalid/mcp"),
|
|
"asleep": _server("https://example.invalid/mcp"),
|
|
# An underscore in the name is what a split on underscores
|
|
# cannot survive: it cannot tell where the server ends and the
|
|
# tool begins, and both halves may contain one.
|
|
"github_enterprise": _server("https://example.invalid/mcp"),
|
|
"brave-search": _server("https://example.invalid/mcp"),
|
|
# A character the sanitiser rewrites: the registered name is
|
|
# mcp_data_warehouse_query, which no configured key prefixes.
|
|
"data.warehouse": _server("https://example.invalid/mcp"),
|
|
}
|
|
)
|
|
)
|
|
# `ext.list` imports load_config inside the function, so the name to patch
|
|
# is the one it imports from, not a module-level alias it does not have.
|
|
from raven.config import loader as config_loader
|
|
from raven.config import raven as raven_config
|
|
|
|
monkeypatch.setattr(config_loader, "load_config", lambda: cfg)
|
|
# `ext.list` also loads the raven-level config for skill-forge settings,
|
|
# which this assertion does not care about.
|
|
monkeypatch.setattr(
|
|
raven_config,
|
|
"load_raven_config",
|
|
# Both attributes the handler reads. With only `skill_forge` the
|
|
# plugin block raised on `ec.plugins.disabled` and its `except` swallowed
|
|
# it, so a quarter of ext.list sat unexecuted for the whole suite.
|
|
lambda: SimpleNamespace(skill_forge=None, plugins=SimpleNamespace(disabled=[])),
|
|
)
|
|
monkeypatch.setattr(console_module, "_hub_marker_name", lambda: None)
|
|
|
|
class _Catalog:
|
|
def gather_all_skills(self):
|
|
return []
|
|
|
|
# A real registry, not a names-only stub: ownership now comes from the
|
|
# origin index it holds, and a stub that only lists names cannot show
|
|
# whether the index is being consulted at all.
|
|
from raven.agent.tools.registry import ToolRegistry
|
|
from raven.mcp.naming import MCPToolRef, tool_name
|
|
|
|
class _Stub(Tool):
|
|
def __init__(self, name: str) -> None:
|
|
self._name = name
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
return self._name
|
|
|
|
@property
|
|
def description(self) -> str:
|
|
return "stub"
|
|
|
|
@property
|
|
def parameters(self) -> dict:
|
|
return {"type": "object", "properties": {}}
|
|
|
|
async def execute(self, **kwargs):
|
|
return ""
|
|
|
|
registry = ToolRegistry()
|
|
registry.register(_Stub("read_file"))
|
|
for server, tool in [
|
|
("ctx7", "get_docs"),
|
|
("ctx7", "resolve_id"),
|
|
("github_enterprise", "list_repos"),
|
|
("brave-search", "query"),
|
|
# The case the old prefix match got wrong: the dot is sanitised out of
|
|
# the registered name, so no configured key prefixes it and the server
|
|
# reported as connected with zero tools while the agent called them.
|
|
("data.warehouse", "query"),
|
|
]:
|
|
registered = tool_name(server, tool, taken=registry)
|
|
registry.register(_Stub(registered), origin=MCPToolRef(name=registered, server=server, tool=tool))
|
|
|
|
loop = SimpleNamespace(context=SimpleNamespace(skills=_Catalog()), tools=registry)
|
|
|
|
result = await console_module.ext_list({}, agent_loop_factory=lambda: loop)
|
|
|
|
by_name = {m["name"]: m for m in result["mcp"]}
|
|
assert by_name["ctx7"]["connected"] is True
|
|
assert by_name["ctx7"]["tool_count"] == 2
|
|
# Declared but nothing registered yet: still listed, so an installed plugin
|
|
# does not vanish between a restart and the first turn.
|
|
assert by_name["asleep"]["connected"] is False
|
|
assert by_name["asleep"]["tool_count"] == 0
|
|
|
|
# An underscored server name: reported down while its tool was registered,
|
|
# and its tool attributed to a server called `github` that does not exist.
|
|
assert by_name["github_enterprise"]["connected"] is True
|
|
assert by_name["github_enterprise"]["tool_count"] == 1
|
|
assert by_name["brave-search"]["tool_count"] == 1
|
|
assert by_name["data.warehouse"]["tool_count"] == 1, "a sanitised server name lost its tools"
|
|
assert by_name["data.warehouse"]["connected"] is True
|
|
|
|
# Non-empty is the load-bearing half. `plugin_discovery_sources()` scans the
|
|
# `raven.plugins` entry-point group, which the dev environment fills with
|
|
# `everos-memory`, so discovery always has something to find -- and an empty
|
|
# list therefore means the block raised and its `except` swallowed it, which
|
|
# is what the stub above used to cause. A shape assertion alone would hold
|
|
# vacuously over that empty list.
|
|
assert result["plugins"], "plugin discovery returned nothing; it raised and was swallowed"
|
|
assert all({"id", "display_name", "version", "enabled", "bundled"} <= set(row) for row in result["plugins"]), (
|
|
result["plugins"]
|
|
)
|
|
|
|
owners = {t["name"]: t["mcp_server"] for t in result["tools"]}
|
|
assert owners["mcp_ctx7_get_docs"] == "ctx7"
|
|
assert owners["mcp_github_enterprise_list_repos"] == "github_enterprise"
|
|
assert owners["mcp_brave-search_query"] == "brave-search"
|
|
assert owners["mcp_data_warehouse_query"] == "data.warehouse"
|
|
assert owners["read_file"] is None
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# fs.read reads a window, not the whole file
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _fs_loop(tmp_path: Path):
|
|
return SimpleNamespace(workspace=str(tmp_path), sessions=None)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fs_read_returns_only_the_window_it_was_asked_for(tmp_path: Path) -> None:
|
|
"""The old shape was `read_bytes()[:limit]` -- the whole file allocated to
|
|
return a slice of it, synchronously, on the shared event loop."""
|
|
target = tmp_path / "big.log"
|
|
target.write_bytes(b"x" * 5000)
|
|
|
|
r = await console_module.fs_read(
|
|
{"path": "big.log", "max_bytes": 100}, agent_loop_factory=lambda: _fs_loop(tmp_path)
|
|
)
|
|
|
|
assert len(r["content"]) == 100
|
|
assert r["truncated"] is True
|
|
assert r["size"] == 5000
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fs_read_still_serves_the_head_of_a_file_past_the_ceiling(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""A huge file is the case this exists for, so it gets its window.
|
|
|
|
The bound is on the read, not on the file: `fh.read(limit)` allocates at
|
|
most `limit` whatever the size. Refusing by size would have made exactly the
|
|
motivating case -- a multi-gigabyte log -- unreadable, while adding nothing
|
|
to the allocation guarantee.
|
|
"""
|
|
from raven.rpc import files as files_module
|
|
|
|
monkeypatch.setattr(files_module, "MAX_VIEW_BYTES", 1000)
|
|
target = tmp_path / "huge.log"
|
|
target.write_bytes(b"y" * 4000)
|
|
|
|
r = await console_module.fs_read({"path": "huge.log"}, agent_loop_factory=lambda: _fs_loop(tmp_path))
|
|
|
|
assert len(r["content"]) == 1000, "the clamped window, not the file"
|
|
assert r["truncated"] is True, "the signal that says there is more where this came from"
|
|
assert r["size"] == 4000
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fs_read_clamps_a_caller_supplied_ceiling(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""`max_bytes` came straight off the wire with no upper bound, so it was an
|
|
unbounded allocation request from the caller."""
|
|
from raven.rpc import files as files_module
|
|
|
|
monkeypatch.setattr(files_module, "MAX_VIEW_BYTES", 500)
|
|
target = tmp_path / "mid.log"
|
|
target.write_bytes(b"z" * 400)
|
|
|
|
r = await console_module.fs_read(
|
|
{"path": "mid.log", "max_bytes": 10_000_000_000}, agent_loop_factory=lambda: _fs_loop(tmp_path)
|
|
)
|
|
|
|
assert len(r["content"]) == 400
|
|
assert r["truncated"] is False
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# cron.save with no loop: the handler builds its own service off the config path
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.fixture
|
|
def isolated_config(tmp_path: Path):
|
|
"""A config path of our own -- the cron store hangs off its parent."""
|
|
import json
|
|
|
|
from raven.config.loader import set_config_path
|
|
|
|
# Restored, not cleared: the global outlives this module, and after the rpc
|
|
# rename this file collects ahead of `test_segments.py`, which reads the
|
|
# real config. Clearing left that module reading a different one than it
|
|
# does on its own.
|
|
previous = raven_home_module._current_config_path
|
|
cfg_path = tmp_path / "config.json"
|
|
cfg_path.write_text(json.dumps({"agents": {"defaults": {"workspace": str(tmp_path / "ws")}}}))
|
|
set_config_path(cfg_path)
|
|
yield
|
|
raven_home_module._current_config_path = previous
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_saving_a_job_returns_it(isolated_config: None) -> None:
|
|
"""No `agent_loop_factory`, so this drives the other half of `_cron_service`:
|
|
the branch that builds a service off `get_cron_dir()` when no loop is bound."""
|
|
r = await console_module.cron_save({"kind": "cron", "expr": "0 9 * * *", "name": "standup", "message": "hi"})
|
|
|
|
assert r["job"]["name"] == "standup"
|
|
assert r["job"]["expr"] == "0 9 * * *"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_editing_a_job_keeps_its_id(isolated_config: None) -> None:
|
|
"""The id is the job's run history: it lives in the ``cron:<id>`` session, so
|
|
an edit that mints a fresh id orphans everything the job has ever done."""
|
|
first = await console_module.cron_save({"kind": "cron", "expr": "0 9 * * *", "name": "standup", "message": "hi"})
|
|
job_id = first["job"]["id"]
|
|
|
|
edited = await console_module.cron_save(
|
|
{"id": job_id, "kind": "cron", "expr": "0 10 * * *", "name": "standup", "message": "hi there"}
|
|
)
|
|
|
|
assert edited["job"]["id"] == job_id
|
|
# The stored schedule, not just the recompute its sibling test pins.
|
|
assert edited["job"]["expr"] == "0 10 * * *"
|
|
listed = await console_module.cron_list({})
|
|
assert len(listed["jobs"]) == 1, "an edit must replace the job, not add a second one"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# fs.* roots at the session's working directory, not at agent home
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class _WorkdirLoop:
|
|
"""The two attributes the fs handlers read, with the resolver's contract."""
|
|
|
|
def __init__(self, by_key: dict[str, Path], default: Path):
|
|
self._by_key = by_key
|
|
self._default = default
|
|
self.workspace = default / "never-this"
|
|
self.asked: list[str] = []
|
|
|
|
def peek_session_workdir(self, session_key: str) -> Path:
|
|
self.asked.append(session_key)
|
|
return self._by_key.get(session_key, self._default)
|
|
|
|
|
|
def _loop_factory(loop):
|
|
return lambda: loop
|
|
|
|
|
|
async def test_fs_list_roots_at_the_session_workdir(tmp_path: Path) -> None:
|
|
"""The panel lists where the session's turns actually read and write. Agent
|
|
home is where the old code pointed, and with the default config that is
|
|
``~/.raven/workspace`` -- a directory the launch-directory policy never
|
|
touches, so the tree showed a place no tool call would ever change."""
|
|
launch = tmp_path / "proj"
|
|
launch.mkdir()
|
|
(launch / "notes.md").write_text("x")
|
|
|
|
loop = _WorkdirLoop({}, launch)
|
|
r = await console_module.fs_list({}, agent_loop_factory=_loop_factory(loop))
|
|
|
|
assert r["root"] == str(launch.resolve())
|
|
assert [e["name"] for e in r["entries"]] == ["notes.md"]
|
|
|
|
|
|
async def test_fs_list_asks_for_the_named_session(tmp_path: Path) -> None:
|
|
"""A session pinned to its own directory must list THAT directory: the key
|
|
goes through to the resolver, which is where the override lives."""
|
|
a = tmp_path / "a"
|
|
b = tmp_path / "b"
|
|
a.mkdir()
|
|
b.mkdir()
|
|
(b / "pinned.txt").write_text("x")
|
|
|
|
loop = _WorkdirLoop({"tui:beta": b}, a)
|
|
r = await console_module.fs_list({"session": "tui:beta"}, agent_loop_factory=_loop_factory(loop))
|
|
|
|
assert loop.asked == ["tui:beta"]
|
|
assert [e["name"] for e in r["entries"]] == ["pinned.txt"]
|
|
|
|
|
|
def _agent_home(monkeypatch, home: Path):
|
|
"""Point ``_upload_root`` at ``home``. Patched on the loader module because
|
|
the handler imports the name inside the function."""
|
|
from raven.config import loader as config_loader
|
|
|
|
monkeypatch.setattr(config_loader, "load_config", lambda: SimpleNamespace(workspace_path=str(home)))
|
|
|
|
|
|
async def test_fs_upload_lands_in_agent_home_not_the_session_workdir(tmp_path: Path, monkeypatch) -> None:
|
|
"""Agent home is the one root always writable: a session's working
|
|
directory is wherever the engine was launched from, and an engine the
|
|
desktop shell started inherits ``/``, where ``uploads/`` cannot be created.
|
|
``turn.send`` admits agent home whatever the session's root, with
|
|
``tools.restrict_to_workspace`` on as well."""
|
|
import base64
|
|
|
|
home = tmp_path / "home"
|
|
home.mkdir()
|
|
launch = tmp_path / "proj"
|
|
launch.mkdir()
|
|
_agent_home(monkeypatch, home)
|
|
loop = _WorkdirLoop({}, launch)
|
|
|
|
r = await console_module.fs_upload(
|
|
{"name": "pic.png", "content_b64": base64.b64encode(b"bytes").decode(), "session": "tui:x"},
|
|
agent_loop_factory=_loop_factory(loop),
|
|
)
|
|
|
|
assert r["path"] == "uploads/pic.png"
|
|
assert r["abs_path"] == str(home.resolve() / "uploads" / "pic.png")
|
|
assert (home / "uploads" / "pic.png").read_bytes() == b"bytes"
|
|
assert not (launch / "uploads").exists()
|
|
|
|
|
|
async def test_fs_upload_survives_a_session_workdir_that_takes_no_files(tmp_path: Path, monkeypatch) -> None:
|
|
"""The reported break: the page's engine launched by the desktop shell
|
|
inherits ``/`` as its working directory, so an upload rooted there died on
|
|
``mkdir`` and reached the composer as a bare ``internal_error``."""
|
|
import base64
|
|
|
|
home = tmp_path / "home"
|
|
home.mkdir()
|
|
unwritable = tmp_path / "not-a-dir" / "sub"
|
|
(tmp_path / "not-a-dir").write_text("x")
|
|
_agent_home(monkeypatch, home)
|
|
loop = _WorkdirLoop({}, unwritable)
|
|
|
|
r = await console_module.fs_upload(
|
|
{"name": "image.png", "content_b64": base64.b64encode(b"bytes").decode(), "session": "web:1"},
|
|
agent_loop_factory=_loop_factory(loop),
|
|
)
|
|
|
|
assert (home / "uploads" / "image.png").read_bytes() == b"bytes"
|
|
assert r["path"] == "uploads/image.png"
|
|
|
|
|
|
async def test_fs_upload_survives_a_session_root_it_may_not_read(tmp_path: Path, monkeypatch) -> None:
|
|
"""The name check reads the session's own ``uploads/`` as well, and a pinned
|
|
folder the engine may not read -- one macOS keeps from it, another user's
|
|
directory -- answers with EACCES, which ``Path.exists`` raises instead of
|
|
answering False. Agent home can still name the upload, so the check gives
|
|
way rather than failing it as -32603. Denied by a stub: root reads through
|
|
any mode, so a chmod would prove nothing on the box that runs this."""
|
|
import base64
|
|
|
|
home = tmp_path / "home"
|
|
home.mkdir()
|
|
project = tmp_path / "project"
|
|
(project / "uploads").mkdir(parents=True)
|
|
_agent_home(monkeypatch, home)
|
|
denied = project.resolve()
|
|
refused: list[str] = []
|
|
real_exists = Path.exists
|
|
|
|
def exists(self: Path, *args, **kwargs) -> bool:
|
|
if Path(self).is_relative_to(denied):
|
|
refused.append(Path(self).name)
|
|
raise PermissionError(13, "Permission denied", str(self))
|
|
return real_exists(self, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(Path, "exists", exists)
|
|
|
|
r = await console_module.fs_upload(
|
|
{"name": "image.png", "content_b64": base64.b64encode(b"bytes").decode(), "session": "web:1"},
|
|
agent_loop_factory=_loop_factory(_WorkdirLoop({}, project)),
|
|
)
|
|
|
|
assert refused == ["image.png"], "the session's folder was asked, and refused"
|
|
assert r["path"] == "uploads/image.png"
|
|
assert (home / "uploads" / "image.png").read_bytes() == b"bytes"
|
|
|
|
|
|
async def test_fs_upload_names_the_directory_it_could_not_create(tmp_path: Path, monkeypatch) -> None:
|
|
"""An unwritable agent home is a real configuration, and the page can only
|
|
report what the error carries: unguarded, ``mkdir`` escaped as -32603 with
|
|
the reason only in the server log."""
|
|
import base64
|
|
|
|
from raven.rpc.errors import ConfigValidationError
|
|
|
|
home = tmp_path / "file" / "home"
|
|
(tmp_path / "file").write_text("x")
|
|
_agent_home(monkeypatch, home)
|
|
|
|
with pytest.raises(ConfigValidationError) as excinfo:
|
|
await console_module.fs_upload(
|
|
{"name": "pic.png", "content_b64": base64.b64encode(b"bytes").decode()},
|
|
agent_loop_factory=None,
|
|
)
|
|
|
|
assert "uploads" in str(excinfo.value)
|
|
|
|
|
|
@pytest.mark.parametrize("method", ["fs_upload", "deck_templates_pick"])
|
|
@pytest.mark.parametrize("context", ["draft_launch", "draft_gateway", "draft_pinned", "draft_process", "session"])
|
|
async def test_attachment_deposits_keep_relative_paths_unambiguous(
|
|
tmp_path: Path, monkeypatch, method, context
|
|
) -> None:
|
|
import base64
|
|
|
|
from raven.agent.workdir import WorkdirPolicy, WorkdirResolver
|
|
from raven.rpc.files import viewer_root
|
|
|
|
home = tmp_path / "home"
|
|
launch = tmp_path / "launch"
|
|
channels = tmp_path / "channels"
|
|
pinned = tmp_path / "pinned"
|
|
policy = WorkdirPolicy.LAUNCH_DIR if context == "draft_launch" else WorkdirPolicy.PER_CHANNEL
|
|
resolver = WorkdirResolver(
|
|
policy,
|
|
agent_home=home,
|
|
launch_dir=launch,
|
|
session_root=channels,
|
|
explicit_workdir=launch if context == "draft_process" else None,
|
|
)
|
|
session = "tui:existing" if context == "session" else "tui:"
|
|
project = pinned if context in {"draft_pinned", "session"} else resolver.resolve(session, create=False)
|
|
loop = SimpleNamespace(peek_session_workdir=lambda key: resolver.resolve(key, create=False))
|
|
if context == "session":
|
|
loop = _WorkdirLoop({session: project}, resolver.resolve("", create=False))
|
|
_agent_home(monkeypatch, home)
|
|
(home / "uploads").mkdir(parents=True)
|
|
(project / "uploads").mkdir(parents=True)
|
|
(project / "uploads" / "amber.pptx").write_bytes(b"project original")
|
|
(home / "uploads" / "amber-1.pptx").write_bytes(b"previous upload")
|
|
(project / "uploads" / "amber-2.pptx").write_bytes(b"project second")
|
|
params = {"name": "amber.pptx", "session": session}
|
|
if context in {"draft_pinned", "draft_process"}:
|
|
params["workdir"] = str(pinned)
|
|
if context == "draft_pinned":
|
|
loop = _WorkdirLoop({}, channels / "tui")
|
|
if method == "fs_upload":
|
|
params["content_b64"] = base64.b64encode(b"new attachment").decode()
|
|
expected = b"new attachment"
|
|
else:
|
|
_bundled_templates(monkeypatch, tmp_path / "templates", ("amber",))
|
|
expected = b"PKamber"
|
|
|
|
handler = getattr(console_module, method)
|
|
for suffix in (3, 4):
|
|
result = await handler(params, agent_loop_factory=_loop_factory(loop))
|
|
assert result["path"] == f"uploads/amber-{suffix}.pptx"
|
|
relative = Path(result["path"])
|
|
resolved = viewer_root(project, relative, home) / relative
|
|
assert resolved == Path(result["abs_path"])
|
|
assert resolved.read_bytes() == expected
|
|
assert (project / "uploads" / "amber.pptx").read_bytes() == b"project original"
|
|
assert (project / "uploads" / "amber-2.pptx").read_bytes() == b"project second"
|
|
assert (home / "uploads" / "amber-1.pptx").read_bytes() == b"previous upload"
|
|
|
|
|
|
@pytest.mark.parametrize("method", ["fs_upload", "deck_templates_pick"])
|
|
@pytest.mark.parametrize("workdir", ["relative", "home", "parent", "memory"])
|
|
async def test_attachment_deposits_validate_draft_workdirs(tmp_path: Path, monkeypatch, method, workdir) -> None:
|
|
import base64
|
|
|
|
home = tmp_path / "home"
|
|
_agent_home(monkeypatch, home)
|
|
_bundled_templates(monkeypatch, tmp_path / "templates", ("amber",))
|
|
paths = {"relative": "project", "home": str(home), "parent": str(tmp_path), "memory": str(home / "user_memory")}
|
|
params = {"name": "amber.pptx", "session": "tui:", "workdir": paths[workdir]}
|
|
if method == "fs_upload":
|
|
params["content_b64"] = base64.b64encode(b"attachment").decode()
|
|
|
|
with pytest.raises(ConfigValidationError, match="working directory"):
|
|
await getattr(console_module, method)(params, agent_loop_factory=_loop_factory(None))
|
|
assert not (home / "uploads" / "amber.pptx").exists()
|
|
|
|
|
|
async def test_deck_templates_pick_survives_an_unreadable_session_root(tmp_path: Path, monkeypatch) -> None:
|
|
home = tmp_path / "home"
|
|
project = tmp_path / "project"
|
|
_agent_home(monkeypatch, home)
|
|
_bundled_templates(monkeypatch, tmp_path / "templates", ("amber",))
|
|
real_exists = Path.exists
|
|
refused = []
|
|
|
|
def exists(path: Path, *args, **kwargs) -> bool:
|
|
if path.is_relative_to(project):
|
|
refused.append(path)
|
|
raise PermissionError("session directory is unreadable")
|
|
return real_exists(path, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(Path, "exists", exists)
|
|
result = await console_module.deck_templates_pick(
|
|
{"name": "amber", "session": "tui:pinned"},
|
|
agent_loop_factory=_loop_factory(_WorkdirLoop({"tui:pinned": project}, tmp_path / "default")),
|
|
)
|
|
assert refused == [project / "uploads" / "amber.pptx"]
|
|
assert Path(result["abs_path"]).read_bytes() == b"PKamber"
|
|
|
|
|
|
async def test_fs_root_survives_a_loop_without_the_resolver(tmp_path: Path) -> None:
|
|
"""An older loop object has no ``peek_session_workdir``; the handler falls
|
|
back to its workspace rather than crashing the panel."""
|
|
ws = tmp_path / "ws"
|
|
ws.mkdir()
|
|
loop = SimpleNamespace(workspace=ws)
|
|
|
|
r = await console_module.fs_list({}, agent_loop_factory=_loop_factory(loop))
|
|
|
|
assert r["root"] == str(ws.resolve())
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# fs.reveal -- the viewer's fence, then the host's file manager
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
async def test_fs_reveal_selects_the_file_in_the_host_file_manager(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
launch = tmp_path / "proj"
|
|
launch.mkdir()
|
|
target = launch / "report.md"
|
|
target.write_text("# hi")
|
|
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "raven-home"))
|
|
|
|
spawned: list[list[str]] = []
|
|
monkeypatch.setattr("subprocess.Popen", lambda argv, **kw: spawned.append(argv))
|
|
monkeypatch.setattr("sys.platform", "darwin")
|
|
|
|
loop = _WorkdirLoop({}, launch)
|
|
r = await console_module.fs_reveal({"path": "report.md"}, agent_loop_factory=_loop_factory(loop))
|
|
|
|
assert r == {"ok": True}
|
|
assert spawned == [["open", "-R", str(target.resolve())]]
|
|
|
|
|
|
async def test_fs_reveal_finds_an_uploaded_attachment(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""The page sends one string to two surfaces. A panel row is relative to the
|
|
session's working directory; an attachment chip can carry ``uploads/<name>``,
|
|
which only agent home holds -- so rooting at the workdir alone made Reveal
|
|
fail on the very file the viewer had just rendered from that same string."""
|
|
home = tmp_path / "raven-home"
|
|
(home / "workspace" / "uploads").mkdir(parents=True)
|
|
target = home / "workspace" / "uploads" / "doc.pdf"
|
|
target.write_bytes(b"%PDF-1.4 x")
|
|
launch = tmp_path / "proj"
|
|
launch.mkdir()
|
|
monkeypatch.setenv("RAVEN_HOME", str(home))
|
|
|
|
spawned: list[list[str]] = []
|
|
monkeypatch.setattr("subprocess.Popen", lambda argv, **kw: spawned.append(argv))
|
|
monkeypatch.setattr("sys.platform", "darwin")
|
|
|
|
loop = _WorkdirLoop({}, launch)
|
|
r = await console_module.fs_reveal(
|
|
{"path": "uploads/doc.pdf", "session": "web:1"}, agent_loop_factory=_loop_factory(loop)
|
|
)
|
|
|
|
assert r == {"ok": True}
|
|
assert spawned == [["open", "-R", str(target.resolve())]]
|
|
|
|
|
|
async def test_fs_reveal_refuses_what_the_viewer_refuses(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""One fence for both: a path the page may not render must not pop a Finder
|
|
window either, or the reveal button becomes the read the viewer denied."""
|
|
from raven.rpc.errors import ConfigValidationError
|
|
|
|
home = tmp_path / "raven-home"
|
|
home.mkdir()
|
|
secret = home / "serve.json"
|
|
secret.write_text("{}")
|
|
monkeypatch.setenv("RAVEN_HOME", str(home))
|
|
|
|
spawned: list[list[str]] = []
|
|
monkeypatch.setattr("subprocess.Popen", lambda argv, **kw: spawned.append(argv))
|
|
|
|
loop = _WorkdirLoop({}, tmp_path)
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.fs_reveal({"path": str(secret)}, agent_loop_factory=_loop_factory(loop))
|
|
assert spawned == []
|
|
|
|
|
|
async def test_fs_reveal_shows_the_config_file_and_opens_agent_home(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""The About page's two rows are addresses. Both sit under the state
|
|
directory the path fence refuses, so the page names them and the gateway
|
|
resolves them: the config file selected in its folder, agent home opened."""
|
|
from raven.config.loader import get_config_path, load_config
|
|
|
|
home = tmp_path / "raven-home"
|
|
home.mkdir()
|
|
monkeypatch.setenv("RAVEN_HOME", str(home))
|
|
get_config_path().write_text("{}")
|
|
Path(load_config().workspace_path).mkdir(parents=True, exist_ok=True)
|
|
|
|
spawned: list[list[str]] = []
|
|
monkeypatch.setattr("subprocess.Popen", lambda argv, **kw: spawned.append(argv))
|
|
monkeypatch.setattr("sys.platform", "darwin")
|
|
|
|
loop = _WorkdirLoop({}, tmp_path)
|
|
assert await console_module.fs_reveal({"place": "config"}, agent_loop_factory=_loop_factory(loop)) == {"ok": True}
|
|
assert await console_module.fs_reveal({"place": "workspace"}, agent_loop_factory=_loop_factory(loop)) == {
|
|
"ok": True
|
|
}
|
|
assert spawned == [
|
|
["open", "-R", str(get_config_path().resolve())],
|
|
["open", str(Path(load_config().workspace_path).expanduser().resolve())],
|
|
]
|
|
|
|
|
|
async def test_fs_reveal_refuses_a_place_that_is_not_there(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""A fresh install has no config file yet; that is an error to show, not a Finder window on nothing."""
|
|
from raven.rpc.errors import ConfigValidationError
|
|
|
|
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "raven-home"))
|
|
spawned: list[list[str]] = []
|
|
monkeypatch.setattr("subprocess.Popen", lambda argv, **kw: spawned.append(argv))
|
|
|
|
loop = _WorkdirLoop({}, tmp_path)
|
|
with pytest.raises(ConfigValidationError, match="does not exist"):
|
|
await console_module.fs_reveal({"place": "config"}, agent_loop_factory=_loop_factory(loop))
|
|
assert spawned == []
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("platform", "select", "expected"),
|
|
[
|
|
("win32", True, lambda t: ["explorer", f"/select,{t}"]),
|
|
("win32", False, lambda t: ["explorer", str(t)]),
|
|
("linux", True, lambda t: ["xdg-open", str(t.parent)]),
|
|
("linux", False, lambda t: ["xdg-open", str(t)]),
|
|
],
|
|
)
|
|
def test_show_in_file_manager_speaks_each_host_file_manager(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, platform: str, select: bool, expected
|
|
) -> None:
|
|
"""Selecting is only for a file; a folder is opened. Linux has no select verb, so a file opens its folder."""
|
|
target = tmp_path / "config.json"
|
|
spawned: list[list[str]] = []
|
|
monkeypatch.setattr("subprocess.Popen", lambda argv, **kw: spawned.append(argv))
|
|
monkeypatch.setattr("sys.platform", platform)
|
|
|
|
console_module._show_in_file_manager(target, select=select)
|
|
|
|
assert spawned == [expected(target)]
|
|
|
|
|
|
def test_show_in_file_manager_reports_a_launcher_that_will_not_start(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
from raven.rpc.errors import ConfigValidationError
|
|
|
|
def refuse(argv, **kw):
|
|
raise FileNotFoundError("xdg-open")
|
|
|
|
monkeypatch.setattr("subprocess.Popen", refuse)
|
|
monkeypatch.setattr("sys.platform", "linux")
|
|
with pytest.raises(ConfigValidationError, match="reveal failed"):
|
|
console_module._show_in_file_manager(tmp_path, select=False)
|
|
|
|
|
|
async def test_fs_reveal_names_only_its_own_places(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""A place is a name from a list of two, not a path in disguise."""
|
|
from raven.rpc.errors import ConfigValidationError
|
|
|
|
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "raven-home"))
|
|
spawned: list[list[str]] = []
|
|
monkeypatch.setattr("subprocess.Popen", lambda argv, **kw: spawned.append(argv))
|
|
|
|
loop = _WorkdirLoop({}, tmp_path)
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.fs_reveal({"place": "serve.json"}, agent_loop_factory=_loop_factory(loop))
|
|
assert spawned == []
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# fs.open -- the same fence, then the host's application
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
async def test_fs_open_hands_the_file_to_the_named_application(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
launch = tmp_path / "proj"
|
|
launch.mkdir()
|
|
target = launch / "deck.pptx"
|
|
target.write_bytes(b"PK\x03\x04")
|
|
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "raven-home"))
|
|
|
|
spawned: list[list[str]] = []
|
|
monkeypatch.setattr("subprocess.Popen", lambda argv, **kw: spawned.append(argv))
|
|
monkeypatch.setattr("sys.platform", "darwin")
|
|
|
|
loop = _WorkdirLoop({}, launch)
|
|
r = await console_module.fs_open({"path": "deck.pptx", "app": "Keynote"}, agent_loop_factory=_loop_factory(loop))
|
|
|
|
assert r == {"ok": True, "app": "Keynote"}
|
|
# The name is ONE argv element beside the resolved path, never concatenated.
|
|
assert spawned == [["open", "-a", "Keynote", str(target.resolve())]]
|
|
|
|
|
|
async def test_fs_open_without_an_application_uses_the_host_default(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
launch = tmp_path / "proj"
|
|
launch.mkdir()
|
|
target = launch / "sheet.xlsx"
|
|
target.write_bytes(b"PK\x03\x04")
|
|
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "raven-home"))
|
|
|
|
spawned: list[list[str]] = []
|
|
monkeypatch.setattr("subprocess.Popen", lambda argv, **kw: spawned.append(argv))
|
|
monkeypatch.setattr("sys.platform", "darwin")
|
|
|
|
loop = _WorkdirLoop({}, launch)
|
|
r = await console_module.fs_open({"path": "sheet.xlsx"}, agent_loop_factory=_loop_factory(loop))
|
|
|
|
# No `app` key at all rather than a null one: the reader picked nothing.
|
|
assert r == {"ok": True}
|
|
assert spawned == [["open", str(target.resolve())]]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"app",
|
|
[
|
|
"/bin/sh",
|
|
"../../usr/bin/python3",
|
|
# These three start with a letter, so only the separator rule can stop
|
|
# them -- without it the "name" would name an arbitrary binary.
|
|
"usr/bin/sh",
|
|
"Keynote/../../bin/sh",
|
|
"C:\\Windows\\System32\\cmd.exe",
|
|
"-a",
|
|
"Keynote; rm -rf /",
|
|
"Keynote && curl evil",
|
|
"Keynote$(whoami)",
|
|
"Key\nnote",
|
|
"",
|
|
" ",
|
|
"K" * 65,
|
|
],
|
|
)
|
|
async def test_fs_open_refuses_anything_that_is_not_an_application_name(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, app: str
|
|
) -> None:
|
|
"""The application is a NAME. It becomes an argv element next to a resolved
|
|
path and never reaches a shell, but a separator would name an arbitrary
|
|
binary and a leading dash would smuggle a flag into ``open`` -- and a name
|
|
shaped like a command is one worth refusing whatever cannot come of it."""
|
|
from raven.rpc.errors import ConfigValidationError
|
|
|
|
launch = tmp_path / "proj"
|
|
launch.mkdir()
|
|
(launch / "deck.pptx").write_bytes(b"PK\x03\x04")
|
|
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "raven-home"))
|
|
|
|
spawned: list[list[str]] = []
|
|
monkeypatch.setattr("subprocess.Popen", lambda argv, **kw: spawned.append(argv))
|
|
monkeypatch.setattr("sys.platform", "darwin")
|
|
|
|
loop = _WorkdirLoop({}, launch)
|
|
if app.strip():
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.fs_open({"path": "deck.pptx", "app": app}, agent_loop_factory=_loop_factory(loop))
|
|
assert spawned == []
|
|
else:
|
|
# Blank is not a refusal, it is "no choice made" -- the host default.
|
|
await console_module.fs_open({"path": "deck.pptx", "app": app}, agent_loop_factory=_loop_factory(loop))
|
|
assert spawned == [["open", str((launch / "deck.pptx").resolve())]]
|
|
|
|
|
|
async def test_fs_open_accepts_the_names_real_applications_have(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
launch = tmp_path / "proj"
|
|
launch.mkdir()
|
|
(launch / "a.py").write_text("x = 1")
|
|
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "raven-home"))
|
|
|
|
spawned: list[list[str]] = []
|
|
monkeypatch.setattr("subprocess.Popen", lambda argv, **kw: spawned.append(argv))
|
|
monkeypatch.setattr("sys.platform", "darwin")
|
|
|
|
loop = _WorkdirLoop({}, launch)
|
|
for app in ["Visual Studio Code", "Sublime Text 4", "IntelliJ IDEA CE", "Cursor", "Xcode.app"]:
|
|
await console_module.fs_open({"path": "a.py", "app": app}, agent_loop_factory=_loop_factory(loop))
|
|
assert [x[2] for x in spawned] == [
|
|
"Visual Studio Code",
|
|
"Sublime Text 4",
|
|
"IntelliJ IDEA CE",
|
|
"Cursor",
|
|
"Xcode.app",
|
|
]
|
|
|
|
|
|
async def test_fs_open_refuses_what_the_viewer_refuses(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""One fence for all three verbs. A path the page may not render must not be
|
|
handed to an application either, or the open button becomes the read the
|
|
viewer denied -- with a program of the caller's choosing attached."""
|
|
from raven.rpc.errors import ConfigValidationError
|
|
|
|
home = tmp_path / "raven-home"
|
|
home.mkdir()
|
|
secret = home / "serve.json"
|
|
secret.write_text("{}")
|
|
monkeypatch.setenv("RAVEN_HOME", str(home))
|
|
|
|
spawned: list[list[str]] = []
|
|
monkeypatch.setattr("subprocess.Popen", lambda argv, **kw: spawned.append(argv))
|
|
|
|
loop = _WorkdirLoop({}, tmp_path)
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.fs_open({"path": str(secret)}, agent_loop_factory=_loop_factory(loop))
|
|
assert spawned == []
|
|
|
|
|
|
async def test_fs_open_on_linux_runs_the_application_and_falls_back_to_xdg(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
launch = tmp_path / "proj"
|
|
launch.mkdir()
|
|
target = launch / "notes.odt"
|
|
target.write_bytes(b"PK\x03\x04")
|
|
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "raven-home"))
|
|
|
|
spawned: list[list[str]] = []
|
|
monkeypatch.setattr("subprocess.Popen", lambda argv, **kw: spawned.append(argv))
|
|
monkeypatch.setattr("sys.platform", "linux")
|
|
|
|
loop = _WorkdirLoop({}, launch)
|
|
await console_module.fs_open({"path": "notes.odt", "app": "code"}, agent_loop_factory=_loop_factory(loop))
|
|
await console_module.fs_open({"path": "notes.odt"}, agent_loop_factory=_loop_factory(loop))
|
|
|
|
assert spawned == [["code", str(target.resolve())], ["xdg-open", str(target.resolve())]]
|
|
|
|
|
|
async def test_fs_open_reports_a_launcher_that_is_not_there(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""A missing launcher is a message, not a traceback: Popen raises OSError
|
|
and the page has to be able to say why nothing happened."""
|
|
from raven.rpc.errors import ConfigValidationError
|
|
|
|
launch = tmp_path / "proj"
|
|
launch.mkdir()
|
|
(launch / "deck.pptx").write_bytes(b"PK\x03\x04")
|
|
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "raven-home"))
|
|
|
|
def boom(argv, **kw):
|
|
raise OSError("no such file")
|
|
|
|
monkeypatch.setattr("subprocess.Popen", boom)
|
|
monkeypatch.setattr("sys.platform", "darwin")
|
|
|
|
loop = _WorkdirLoop({}, launch)
|
|
with pytest.raises(ConfigValidationError, match="open failed"):
|
|
await console_module.fs_open({"path": "deck.pptx"}, agent_loop_factory=_loop_factory(loop))
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# channels.status carries the field specs; channels.configure writes them
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
async def test_channels_status_rows_carry_their_field_specs(isolated_config: None) -> None:
|
|
"""The page's configure form is drawn from these rows: every field the
|
|
channel takes rides along -- name, whether it is secret, whether a value is
|
|
stored -- and never the value itself, because the row crosses the wire on
|
|
every status poll."""
|
|
r = await console_module.channels_status({})
|
|
|
|
tg = next(c for c in r["channels"] if c["name"] == "telegram")
|
|
keys = {f["key"] for f in tg["fields"]}
|
|
assert "token" in keys
|
|
assert "enabled" not in keys, "the switch is not a form field"
|
|
token = next(f for f in tg["fields"] if f["key"] == "token")
|
|
assert token["secret"] is True
|
|
assert token["set"] is False
|
|
assert "value" not in token
|
|
|
|
|
|
async def test_channels_configure_stores_the_field_and_status_says_so(isolated_config: None) -> None:
|
|
import json as _json
|
|
|
|
from raven.config.loader import get_config_path
|
|
|
|
r = await console_module.channels_configure({"name": "telegram", "fields": {"token": "123:abc"}})
|
|
assert r == {"applied": True}
|
|
|
|
raw = _json.loads(get_config_path().read_text())
|
|
assert raw["channels"]["telegram"]["token"] == "123:abc"
|
|
|
|
status = await console_module.channels_status({})
|
|
tg = next(c for c in status["channels"] if c["name"] == "telegram")
|
|
token = next(f for f in tg["fields"] if f["key"] == "token")
|
|
assert token["set"] is True
|
|
assert "123:abc" not in _json.dumps(status), "secret values must never ride the status wire"
|
|
|
|
|
|
async def test_channels_configure_skips_blanks_and_refuses_unknown_fields(isolated_config: None) -> None:
|
|
"""A blank box means "left as is": the form sends every field it showed, so
|
|
writing the empties would erase a stored secret on every unrelated save."""
|
|
from raven.rpc.errors import ConfigValidationError
|
|
|
|
r = await console_module.channels_configure({"name": "telegram", "fields": {"token": " "}})
|
|
assert r == {"applied": False}
|
|
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.channels_configure({"name": "telegram", "fields": {"not_a_field": "x"}})
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.channels_configure({"name": "telegram", "fields": {"enabled": True}})
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.channels_configure({"name": "nope", "fields": {"token": "x"}})
|
|
|
|
|
|
async def test_channels_configure_connects_and_disconnects(isolated_config: None) -> None:
|
|
"""Connecting and disconnecting were both client-side flags that the next
|
|
status poll overwrote: no RPC could write `enabled`, so the buttons moved
|
|
nothing on disk and the row snapped back to whatever the config still said.
|
|
"""
|
|
import json as _json
|
|
|
|
from raven.config.loader import get_config_path
|
|
|
|
def stored_enabled() -> bool:
|
|
raw = _json.loads(get_config_path().read_text())
|
|
return bool(raw["channels"]["telegram"].get("enabled"))
|
|
|
|
async def reported_on() -> bool:
|
|
status = await console_module.channels_status({})
|
|
return next(c for c in status["channels"] if c["name"] == "telegram")["enabled"]
|
|
|
|
r = await console_module.channels_configure({"name": "telegram", "fields": {"token": "123:abc"}, "enabled": True})
|
|
assert r == {"applied": True, "outcome": "unreachable"}
|
|
assert stored_enabled() is True
|
|
assert await reported_on() is True
|
|
|
|
r = await console_module.channels_configure({"name": "telegram", "fields": {}, "enabled": False})
|
|
assert r == {"applied": True, "outcome": "unreachable"}
|
|
assert stored_enabled() is False
|
|
assert await reported_on() is False
|
|
|
|
raw = _json.loads(get_config_path().read_text())
|
|
assert raw["channels"]["telegram"]["token"] == "123:abc", "disconnecting must keep the credentials"
|
|
|
|
|
|
async def test_channels_configure_asks_the_gateway_to_start_the_adapter(isolated_config: None) -> None:
|
|
"""The switch is config; the adapter is the gateway's, in another process.
|
|
Writing the flag alone was the whole of "connect", so a channel turned on
|
|
here did nothing until the next launch -- for a scan-login entrance that
|
|
meant no QR could ever be fetched and nobody could sign in from the UI.
|
|
"""
|
|
asked: list[tuple[str, bool, bool]] = []
|
|
|
|
async def fake_start(name: str, *, enabled: bool = True, restart: bool = False) -> str:
|
|
asked.append((name, enabled, restart))
|
|
return "started" if enabled else "stopped"
|
|
|
|
import raven.gateway.live_probe as probe
|
|
|
|
probe_start = probe.channel_start
|
|
probe.channel_start = fake_start
|
|
try:
|
|
# Credentials came with the switch, so an adapter that is already up has
|
|
# to be rebuilt: it holds the slice it was built with, and the corrected
|
|
# token would sit in config while the live one kept the rejected one.
|
|
r = await console_module.channels_configure({"name": "telegram", "fields": {"token": "1:a"}, "enabled": True})
|
|
assert asked == [("telegram", True, True)]
|
|
assert r == {"applied": True, "outcome": "started"}
|
|
r = await console_module.channels_configure({"name": "telegram", "fields": {}, "enabled": False})
|
|
assert asked[-1] == ("telegram", False, False)
|
|
assert r == {"applied": True, "outcome": "stopped"}
|
|
# The bare switch is not a rebuild: flipping on what is already on must
|
|
# leave a working adapter where it is.
|
|
await console_module.channels_configure({"name": "telegram", "fields": {}, "enabled": True})
|
|
assert asked[-1] == ("telegram", True, False)
|
|
# Nor is a form whose boxes were all left blank -- nothing was written,
|
|
# so there is nothing the adapter is out of date with.
|
|
await console_module.channels_configure({"name": "telegram", "fields": {"token": " "}, "enabled": True})
|
|
assert asked[-1] == ("telegram", True, False)
|
|
# A credential correction with no switch in it does not restart anything.
|
|
r = await console_module.channels_configure({"name": "telegram", "fields": {"token": "2:b"}})
|
|
assert len(asked) == 4
|
|
assert r == {"applied": True}, "no switch, nothing started: no outcome to report"
|
|
finally:
|
|
probe.channel_start = probe_start
|
|
|
|
|
|
async def test_channels_configure_still_applies_when_no_gateway_answers(isolated_config: None) -> None:
|
|
"""No gateway is the ordinary case for a first run: the config write stands
|
|
and the next launch honours it, so the failure to reach one must not turn a
|
|
successful write into an error."""
|
|
import json as _json
|
|
|
|
import raven.gateway.live_probe as probe
|
|
from raven.config.loader import get_config_path
|
|
|
|
async def boom(name: str, *, enabled: bool = True, restart: bool = False) -> str:
|
|
raise OSError("no gateway here")
|
|
|
|
probe_start = probe.channel_start
|
|
probe.channel_start = boom
|
|
try:
|
|
r = await console_module.channels_configure({"name": "telegram", "fields": {"token": "1:a"}, "enabled": True})
|
|
finally:
|
|
probe.channel_start = probe_start
|
|
assert r == {"applied": True, "outcome": "unreachable"}, "the one case where the next launch is the remedy"
|
|
assert _json.loads(get_config_path().read_text())["channels"]["telegram"]["enabled"] is True
|
|
|
|
|
|
async def test_channels_configure_carries_the_gateway_refusal_and_what_to_do(isolated_config: None) -> None:
|
|
"""A channel the gateway would not start has a reason, and the reader needs
|
|
it: the word was dropped here, so a missing SDK reached the page as a bare
|
|
red "not started" while the sentence that fixes it sat in the gateway log.
|
|
"""
|
|
from raven.gateway.manager import missing_dep_hint
|
|
|
|
async def refusing(name: str, *, enabled: bool = True, restart: bool = False) -> str:
|
|
return "missing_dep"
|
|
|
|
import raven.gateway.live_probe as probe
|
|
|
|
probe_start = probe.channel_start
|
|
probe.channel_start = refusing
|
|
try:
|
|
r = await console_module.channels_configure({"name": "telegram", "fields": {"token": "1:a"}, "enabled": True})
|
|
finally:
|
|
probe.channel_start = probe_start
|
|
assert r["applied"] is True, "the config write stands whatever the adapter did"
|
|
assert r["outcome"] == "missing_dep"
|
|
assert r["detail"] == missing_dep_hint()
|
|
|
|
|
|
async def test_channels_configure_refuses_an_empty_request(isolated_config: None) -> None:
|
|
from raven.rpc.errors import ConfigValidationError
|
|
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.channels_configure({"name": "telegram", "fields": {}})
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.channels_configure({"name": "telegram"})
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# fs.* answers about the state dir the way the viewer does -- both directions
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _fs_loop(root):
|
|
from types import SimpleNamespace
|
|
|
|
return SimpleNamespace(workspace=str(root))
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fs_refuses_the_state_dir_when_the_root_sits_above_it(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""Under the launch-directory policy the root is the cwd, so started from a
|
|
home directory the state dir is a *child* of it -- and `serve.json` holds a
|
|
token that mints unlimited nonces, so it outlives the cookie it is not
|
|
supposed to be worth."""
|
|
from raven.rpc.errors import ConfigValidationError
|
|
from raven.rpc.methods import console as console_module
|
|
|
|
monkeypatch.setenv("HOME", str(tmp_path))
|
|
(tmp_path / ".raven").mkdir()
|
|
(tmp_path / ".raven" / "serve.json").write_text('{"token": "SECRET"}')
|
|
(tmp_path / "notes.md").write_text("ordinary project file")
|
|
loop = _fs_loop(tmp_path)
|
|
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.fs_read({"path": ".raven/serve.json"}, agent_loop_factory=lambda: loop)
|
|
|
|
ok = await console_module.fs_read({"path": "notes.md"}, agent_loop_factory=lambda: loop)
|
|
assert ok["content"] == "ordinary project file"
|
|
|
|
listed = await console_module.fs_list({"path": ""}, agent_loop_factory=lambda: loop)
|
|
assert [e["name"] for e in listed["entries"]] == ["notes.md"], "the state dir must not be offered"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fs_still_serves_the_workspace_when_it_is_inside_the_state_dir(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""The default workspace lives AT `~/.raven/workspace`, so a fence without
|
|
the viewer's carve-out refuses every file the agent itself wrote -- the same
|
|
asymmetry as the leak above, mirrored: the viewer renders a file the panel
|
|
will not list."""
|
|
from raven.rpc.methods import console as console_module
|
|
|
|
monkeypatch.setenv("HOME", str(tmp_path))
|
|
ws = tmp_path / ".raven" / "workspace"
|
|
ws.mkdir(parents=True)
|
|
(ws / "report.md").write_text("the agent wrote this")
|
|
loop = _fs_loop(ws)
|
|
|
|
listed = await console_module.fs_list({"path": ""}, agent_loop_factory=lambda: loop)
|
|
assert [e["name"] for e in listed["entries"]] == ["report.md"]
|
|
|
|
got = await console_module.fs_read({"path": "report.md"}, agent_loop_factory=lambda: loop)
|
|
assert got["content"] == "the agent wrote this"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# deliverables.list: what a conversation handed over, after nobody was watching
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _file(tmp_path: Path, name: str) -> Path:
|
|
"""A real file for the registry to point at: `missing` is stat'd, so a
|
|
fixture that never touched the disk would report every row as gone."""
|
|
target = tmp_path / name
|
|
target.write_text(name)
|
|
return target
|
|
|
|
|
|
async def test_deliverables_list_answers_for_the_conversation_not_the_transcript(tmp_path: Path) -> None:
|
|
"""The registry is the answer after a reconnect or a compaction: a client
|
|
reads the manifests off turn events while it is connected, and this is what
|
|
is left when it was not."""
|
|
from raven.agent.tools.deliverables import DeliverableStore
|
|
|
|
store = DeliverableStore(tmp_path / "deliverables.json")
|
|
mine = _file(tmp_path, "brief.md")
|
|
theirs = _file(tmp_path, "other.md")
|
|
store.register(
|
|
path=str(mine),
|
|
name="brief.md",
|
|
media_type="text/markdown",
|
|
size=12,
|
|
conversation="tui:s1",
|
|
title="The brief",
|
|
description="what it is",
|
|
)
|
|
store.register(
|
|
path=str(theirs),
|
|
name="other.md",
|
|
media_type="text/markdown",
|
|
size=9,
|
|
conversation="tui:s2",
|
|
)
|
|
loop = SimpleNamespace(deliverables=store)
|
|
|
|
result = await console_module.deliverables_list({"session_key": "tui:s1"}, agent_loop_factory=lambda: loop)
|
|
|
|
assert [f["name"] for f in result["files"]] == ["brief.md"], "another conversation's file is not this one's"
|
|
file = result["files"][0]
|
|
assert file["title"] == "The brief"
|
|
assert file["description"] == "what it is"
|
|
assert file["missing"] is False
|
|
# A token URL, never a path: the download route resolves the token and the
|
|
# path never travels in a URL.
|
|
assert file["download_path"].startswith("/files/download?token=")
|
|
assert str(mine) not in file["download_path"]
|
|
assert file["created_at"]
|
|
|
|
|
|
async def test_deliverables_list_says_a_file_is_gone_rather_than_dropping_it(tmp_path: Path) -> None:
|
|
"""It is still something this conversation handed over. Saying so is more
|
|
use than a row that quietly disappears."""
|
|
from raven.agent.tools.deliverables import DeliverableStore
|
|
|
|
store = DeliverableStore(tmp_path / "deliverables.json")
|
|
target = _file(tmp_path, "gone.md")
|
|
store.register(
|
|
path=str(target),
|
|
name="gone.md",
|
|
media_type="text/markdown",
|
|
size=4,
|
|
conversation="tui:s1",
|
|
)
|
|
target.unlink()
|
|
loop = SimpleNamespace(deliverables=store)
|
|
|
|
result = await console_module.deliverables_list({"session_key": "tui:s1"}, agent_loop_factory=lambda: loop)
|
|
|
|
assert [f["missing"] for f in result["files"]] == [True]
|
|
|
|
|
|
async def test_deliverables_list_is_empty_without_a_key_or_a_store() -> None:
|
|
"""A draft has no conversation to ask about, and a runtime built without the
|
|
web channel has no registry -- neither is an error."""
|
|
loop = SimpleNamespace(deliverables=None)
|
|
|
|
assert (await console_module.deliverables_list({"session_key": ""}, agent_loop_factory=lambda: loop))["files"] == []
|
|
assert (await console_module.deliverables_list({"session_key": "tui:s1"}))["files"] == []
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# settings.set keeps the shell env mirror in step
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.fixture
|
|
def settings_cfg(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
|
"""A config this suite may write, with ``~`` pointed somewhere disposable.
|
|
|
|
``settings.set`` now refreshes ``~/.raven/env`` for every mirrored web
|
|
key, so an unisolated home would have this rewrite the developer's real one.
|
|
"""
|
|
path = tmp_path / "config.json"
|
|
path.write_text("{}", encoding="utf-8")
|
|
# Two bindings, not one: ``_write_raw_key`` imports ``get_config_path``
|
|
# inside the call, while ``update_tools`` bound it at import -- and the
|
|
# mirror reads the keys back through the latter.
|
|
monkeypatch.setattr("raven.config.loader.get_config_path", lambda: path)
|
|
monkeypatch.setattr(update_tools, "get_config_path", lambda: path)
|
|
monkeypatch.setattr(Path, "home", lambda: tmp_path)
|
|
return path
|
|
|
|
|
|
#: Every key whose write must refresh the mirror, paired with the variable it
|
|
#: is expected to land as. The vendor half is derived from the same mapping the
|
|
#: mirror is, so a new vendor arrives here with a case already; the pin below
|
|
#: catches a key that reaches ``MIRRORED_KEYS`` by any other route.
|
|
_MIRRORED_KEY_ENV_VARS = [
|
|
("tools.web.search.apiKey", "SERPER_API_KEY"),
|
|
("tools.web.jinaApiKey", "JINA_API_KEY"),
|
|
*((f"tools.web.providers.{vendor}.apiKey", var) for vendor, var in WEB_VENDOR_ENV_VARS.items()),
|
|
]
|
|
|
|
|
|
def test_the_mirror_cases_cover_every_mirrored_key() -> None:
|
|
"""The parametrisation below is only as good as its coverage of the tuple
|
|
``settings.set`` actually gates on, and the two pre-vendor leaves in it are
|
|
listed by hand at both ends."""
|
|
assert [key for key, _ in _MIRRORED_KEY_ENV_VARS] == list(MIRRORED_KEYS)
|
|
|
|
|
|
@pytest.fixture
|
|
def retaken(monkeypatch: pytest.MonkeyPatch) -> list[bool]:
|
|
"""Each time a write takes the login-shell capture again, recorded rather than run.
|
|
|
|
The real one runs the login shell of whoever started pytest.
|
|
"""
|
|
calls: list[bool] = []
|
|
|
|
def refresh() -> bool:
|
|
calls.append(True)
|
|
return True
|
|
|
|
monkeypatch.setattr(backend_env, "refresh_login_shell_env", refresh)
|
|
return calls
|
|
|
|
|
|
@pytest.mark.usefixtures("retaken")
|
|
@pytest.mark.parametrize(("key", "env_var"), _MIRRORED_KEY_ENV_VARS)
|
|
async def test_settings_set_refreshes_the_shell_env_mirror(
|
|
settings_cfg: Path, tmp_path: Path, key: str, env_var: str
|
|
) -> None:
|
|
"""Every mirrored key, because the settings page can write any of them.
|
|
|
|
Parametrised rather than asserted on one: the jina half has no other write
|
|
path at all outside the wizard, and a per-vendor slot has none either, so a
|
|
mirror wired for Serper alone would look right and leave those keys
|
|
permanently stale.
|
|
"""
|
|
await console_module.settings_set({"key": key, "value": "rotated-1"})
|
|
|
|
mirror = tmp_path / ".raven" / "env"
|
|
assert f"export {env_var}=rotated-1" in mirror.read_text(encoding="utf-8")
|
|
|
|
await console_module.settings_set({"key": key, "value": "rotated-2"})
|
|
body = mirror.read_text(encoding="utf-8")
|
|
assert f"export {env_var}=rotated-2" in body
|
|
assert "rotated-1" not in body
|
|
|
|
|
|
async def test_settings_set_of_an_unrelated_key_does_not_create_the_mirror(settings_cfg: Path, tmp_path: Path) -> None:
|
|
await console_module.settings_set({"key": "tools.exec.timeout", "value": 42})
|
|
|
|
assert not (tmp_path / ".raven" / "env").exists()
|
|
|
|
|
|
@pytest.mark.parametrize("key", MIRRORED_KEYS)
|
|
async def test_a_mirrored_key_write_takes_the_login_shell_capture_again(
|
|
settings_cfg: Path, retaken: list[bool], key: str
|
|
) -> None:
|
|
"""A sub-agent reads the mirror through that capture, and a running raven keeps the one it took."""
|
|
await console_module.settings_set({"key": key, "value": "rotated"})
|
|
|
|
assert retaken == [True]
|
|
|
|
|
|
async def test_an_unrelated_key_write_leaves_the_login_shell_capture_alone(
|
|
settings_cfg: Path, retaken: list[bool]
|
|
) -> None:
|
|
await console_module.settings_set({"key": "tools.exec.timeout", "value": 42})
|
|
|
|
assert retaken == []
|
|
|
|
|
|
@pytest.mark.skipif(sys.platform == "win32", reason="the mirror is a POSIX shell file")
|
|
async def test_a_rotated_key_reaches_the_capture_the_next_spawn_reads(
|
|
settings_cfg: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""Rewriting the mirror alone left every later spawn on the key it replaced until a restart.
|
|
|
|
The login shell is a real bash that sources the mirror and nothing else, so
|
|
the test reads neither the host's profile nor its login shell.
|
|
"""
|
|
bash = shutil.which("bash")
|
|
if bash is None:
|
|
pytest.skip("needs bash to source the mirror")
|
|
mirror = tmp_path / ".raven" / "env"
|
|
real_run = subprocess.run
|
|
|
|
def run(argv, *args, **kwargs):
|
|
if list(argv[1:]) == ["-lic", "env -0"]:
|
|
argv = [bash, "--noprofile", "--norc", "-c", f'. "{mirror}"; env -0']
|
|
return real_run(argv, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(backend_env, "_LOGIN_ENV", None)
|
|
monkeypatch.setattr(backend_env, "_LOGIN_ENV_FAILED", False)
|
|
monkeypatch.setattr(backend_env, "_on_windows", lambda: False)
|
|
monkeypatch.setenv("SHELL", "/bin/bash")
|
|
monkeypatch.setattr(subprocess, "run", run)
|
|
key = "tools.web.providers.tavily.apiKey"
|
|
|
|
await console_module.settings_set({"key": key, "value": "tvly-first"})
|
|
assert backend_env.login_shell_env()["TAVILY_API_KEY"] == "tvly-first"
|
|
|
|
await console_module.settings_set({"key": key, "value": "tvly-second"})
|
|
assert backend_env.login_shell_env()["TAVILY_API_KEY"] == "tvly-second"
|
|
|
|
|
|
async def test_settings_set_writes_the_config_in_the_shared_formatting(settings_cfg: Path) -> None:
|
|
"""The bytes every ``update_*`` writer lays down: two-space indent, raw
|
|
UTF-8 rather than ASCII escapes, no trailing newline. A save that drifted
|
|
from this would rewrite every line of the file on the next change."""
|
|
await console_module.settings_set({"key": "plugins.disabled", "value": ["caf\u00e9-plugin"]})
|
|
|
|
expected = '{\n "plugins": {\n "disabled": [\n "caf\u00e9-plugin"\n ]\n }\n}'
|
|
assert settings_cfg.read_bytes() == expected.encode("utf-8")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ext_list_carries_the_manager_state_and_the_authorization_url(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""The pull has to answer for a connect that started before any client.
|
|
|
|
`mcp.status` and `oauth.pending` carry the state and the URL, and
|
|
`WsGateway.broadcast` drops both while its socket set is empty -- which is
|
|
every connect started at assembly time, since the page attaches after. Built
|
|
from the registry alone, this reported a server parked at the browser step as
|
|
`disconnected` with no URL, and the page draws its authorization action off
|
|
exactly those two fields.
|
|
"""
|
|
|
|
def _server(url: str) -> SimpleNamespace:
|
|
return SimpleNamespace(enabled=True, type=None, command=None, url=url)
|
|
|
|
cfg = SimpleNamespace(
|
|
tools=SimpleNamespace(
|
|
mcp_servers={
|
|
"parked": _server("https://example.invalid/mcp"),
|
|
# No record with the manager at all: still listed from config.
|
|
"unknown": _server("https://example.invalid/mcp"),
|
|
}
|
|
)
|
|
)
|
|
from raven.config import loader as config_loader
|
|
from raven.config import raven as raven_config
|
|
|
|
monkeypatch.setattr(config_loader, "load_config", lambda: cfg)
|
|
monkeypatch.setattr(
|
|
raven_config,
|
|
"load_raven_config",
|
|
lambda: SimpleNamespace(skill_forge=None, plugins=SimpleNamespace(disabled=[])),
|
|
)
|
|
monkeypatch.setattr(console_module, "_hub_marker_name", lambda: None)
|
|
|
|
from raven.mcp import oauth as oauth_module
|
|
|
|
monkeypatch.setattr(
|
|
oauth_module,
|
|
"pending_url",
|
|
lambda name: "https://auth.invalid/authorize?state=abc" if name == "parked" else None,
|
|
)
|
|
|
|
class _Catalog:
|
|
def gather_all_skills(self):
|
|
return []
|
|
|
|
class _Manager:
|
|
def status(self):
|
|
return [
|
|
{
|
|
"name": "parked",
|
|
"transport": "streamableHttp",
|
|
"state": "auth_required",
|
|
"connected": False,
|
|
"tool_count": 0,
|
|
"error": "waiting for browser authorization",
|
|
}
|
|
]
|
|
|
|
from raven.agent.tools.registry import ToolRegistry
|
|
|
|
loop = SimpleNamespace(
|
|
context=SimpleNamespace(skills=_Catalog()),
|
|
tools=ToolRegistry(),
|
|
mcp_manager_if_started=_Manager(),
|
|
)
|
|
|
|
result = await console_module.ext_list({}, agent_loop_factory=lambda: loop)
|
|
by_name = {m["name"]: m for m in result["mcp"]}
|
|
|
|
assert by_name["parked"]["state"] == "auth_required"
|
|
assert by_name["parked"]["auth_url"] == "https://auth.invalid/authorize?state=abc"
|
|
assert by_name["parked"]["error"] == "waiting for browser authorization"
|
|
# Config still decides enabled: the manager's record predates a toggle.
|
|
assert by_name["parked"]["enabled"] is True
|
|
|
|
# A configured server the manager has never seen keeps the old reading.
|
|
assert by_name["unknown"]["state"] == "disconnected"
|
|
assert by_name["unknown"]["auth_url"] is None
|
|
|
|
|
|
async def test_a_language_switch_reaches_the_running_process(tmp_path, monkeypatch) -> None:
|
|
"""Writing the file is half of it: `t()` answers from a module-level
|
|
language that only the CLI seeds, so a console switch that stopped at the
|
|
file left every later reply in the old language until a restart."""
|
|
from raven import i18n
|
|
from raven.config import update as config_update
|
|
|
|
monkeypatch.setattr(config_update, "set_language", lambda value: "en")
|
|
monkeypatch.setattr(i18n, "_language", "en")
|
|
|
|
result = await console_module.settings_set({"key": "language", "value": "zh"})
|
|
|
|
assert result["applied"] is True
|
|
assert i18n.current_language() == "zh"
|
|
|
|
|
|
async def test_a_rejected_language_changes_nothing(tmp_path, monkeypatch) -> None:
|
|
from raven import i18n
|
|
from raven.config import update as config_update
|
|
from raven.rpc.errors import ConfigValidationError as RpcConfigValidationError
|
|
|
|
monkeypatch.setattr(config_update, "set_language", lambda value: "en")
|
|
monkeypatch.setattr(i18n, "_language", "en")
|
|
|
|
with pytest.raises(RpcConfigValidationError):
|
|
await console_module.settings_set({"key": "language", "value": "de"})
|
|
|
|
assert i18n.current_language() == "en"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# ext.list reports availability, not membership
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _console_loop(workspace: Path, monkeypatch: pytest.MonkeyPatch, raw: dict, **extra):
|
|
"""A real on-disk config, and a loop assembled from it the production way.
|
|
|
|
Both halves matter. A hand-built stand-in is what let this defect hide: an
|
|
object carrying ``tool_names`` and nothing else answers every availability
|
|
question by omission, which is the one thing under test. And the surface
|
|
explains an unavailable tool by reading the config file, so the file has to
|
|
be the same one the loop was built from or the two answer about different
|
|
deployments.
|
|
"""
|
|
import json
|
|
|
|
from raven.agent.loop import AgentLoop
|
|
from raven.config.loader import load_config
|
|
from tests._wiring import wire
|
|
from tests.test_tool_capabilities import _StubProvider
|
|
|
|
for var in ("SERPER_API_KEY", "OPENROUTER_API_KEY"):
|
|
monkeypatch.delenv(var, raising=False)
|
|
cfg_path = workspace / "config.json"
|
|
cfg_path.write_text(json.dumps(raw), encoding="utf-8")
|
|
monkeypatch.setattr("raven.home._current_config_path", cfg_path)
|
|
|
|
config = load_config(cfg_path)
|
|
kw = {}
|
|
if config.tools.web.search.api_key:
|
|
kw["search_api_key"] = config.tools.web.search.api_key
|
|
loop = AgentLoop(
|
|
provider=_StubProvider(),
|
|
workspace=workspace,
|
|
model="stub",
|
|
**wire(media_config=config.effective_media_config(), **kw, **extra),
|
|
)
|
|
# No withheld source installed here on purpose: AgentLoop installs its own,
|
|
# which reads the live config -- the file above -- and unions the off switch
|
|
# with the unconfigured names. Overriding it with a disabled-only lambda was
|
|
# enough to make the credential-gate cases pass for the wrong reason.
|
|
return loop
|
|
|
|
|
|
async def _ext_rows(loop, monkeypatch: pytest.MonkeyPatch) -> dict[str, dict]:
|
|
from raven.config import raven as raven_config
|
|
|
|
monkeypatch.setattr(
|
|
raven_config,
|
|
"load_raven_config",
|
|
lambda: SimpleNamespace(skill_forge=None, plugins=SimpleNamespace(disabled=[])),
|
|
)
|
|
monkeypatch.setattr(console_module, "_hub_marker_name", lambda: None)
|
|
result = await console_module.ext_list({}, agent_loop_factory=lambda: loop)
|
|
return {t["name"]: t for t in result["tools"]}
|
|
|
|
|
|
_CRED_TOOLS = (
|
|
# The vendor slot, not the pre-vendor leaf: a key is held per vendor now,
|
|
# and the row has to send the deployer to the slot the default reads.
|
|
("web_search", "tools.web.providers.serper.apiKey", "SERPER_API_KEY"),
|
|
("image_generate", "tools.media.image.apiKey", "OPENROUTER_API_KEY"),
|
|
("text_to_speech", "tools.media.speech.apiKey", "OPENROUTER_API_KEY"),
|
|
("video_generate", "tools.media.video.apiKey", "OPENROUTER_API_KEY"),
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(("tool", "setting", "env"), _CRED_TOOLS)
|
|
async def test_an_unconfigured_tool_is_reported_unavailable_with_what_it_needs(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, tool: str, setting: str, env: str
|
|
) -> None:
|
|
"""A credential-less tool is registered and withheld rather than left out,
|
|
so membership stopped meaning the model can call it. Reporting ``enabled``
|
|
off membership told a deployer the capability was on while the model was
|
|
never offered it -- and said nothing about what to set.
|
|
|
|
The setting names the *key* field: for the media family the config path is
|
|
the model, and pointing there sends the reader to a line holding no key.
|
|
"""
|
|
loop = _console_loop(tmp_path, monkeypatch, {})
|
|
|
|
assert loop.tools.has(tool), "this case is about a REGISTERED tool the model is not offered"
|
|
assert not loop.tools.offers_by_name(tool)
|
|
|
|
row = (await _ext_rows(loop, monkeypatch))[tool]
|
|
assert row["enabled"] is False
|
|
assert row["needs"] == {"setting": setting, "env": env}
|
|
|
|
|
|
async def test_a_configured_tool_is_reported_available_with_nothing_owed(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""The other direction, so the fix cannot pass by reporting everything off."""
|
|
loop = _console_loop(
|
|
tmp_path,
|
|
monkeypatch,
|
|
# The media tool's own section, not a chat OpenRouter key: a key
|
|
# configured for chat deliberately does not enable a billed media tool.
|
|
{"tools": {"web": {"search": {"apiKey": "sk-serper"}}, "media": {"image": {"apiKey": "sk-img"}}}},
|
|
)
|
|
|
|
rows = await _ext_rows(loop, monkeypatch)
|
|
for tool in ("web_search", "image_generate"):
|
|
assert loop.tools.offers_by_name(tool), tool
|
|
assert rows[tool]["enabled"] is True, tool
|
|
assert rows[tool]["needs"] is None, tool
|
|
|
|
|
|
@pytest.mark.parametrize("tool", ["web_search", "text_to_speech"])
|
|
async def test_a_switched_off_tool_is_not_reported_as_missing_a_credential(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, tool: str
|
|
) -> None:
|
|
"""Unavailable has two unrelated causes and the surface must not merge them.
|
|
|
|
A switched-off tool usually has its key set, so answering the operator's
|
|
own off switch with "set this credential" points at a line already filled
|
|
in. Only the credential gate earns a ``needs``; the switch is reported by
|
|
``enabled`` alone, which is what keeps the row switchable.
|
|
"""
|
|
loop = _console_loop(
|
|
tmp_path,
|
|
monkeypatch,
|
|
{
|
|
"tools": {
|
|
"web": {"search": {"apiKey": "sk-serper"}},
|
|
"media": {"speech": {"apiKey": "sk-tts"}},
|
|
"disabledTools": ["web_search", "text_to_speech"],
|
|
}
|
|
},
|
|
)
|
|
|
|
assert loop.tools.has(tool)
|
|
assert not loop.tools.offers_by_name(tool), "the off switch has to bite for this case to mean anything"
|
|
|
|
row = (await _ext_rows(loop, monkeypatch))[tool]
|
|
assert row["enabled"] is False
|
|
assert row["needs"] is None, "a configured tool the operator switched off owes no credential"
|
|
|
|
|
|
async def test_a_tool_needing_no_credential_stays_reported_available(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""A plain tool owes nothing, so it must not grow a needs row from the roster."""
|
|
loop = _console_loop(tmp_path, monkeypatch, {})
|
|
|
|
row = (await _ext_rows(loop, monkeypatch))["read_file"]
|
|
assert row["enabled"] is True
|
|
assert row["needs"] is None
|
|
|
|
|
|
@pytest.mark.parametrize("quality", ["", "low", "medium", "high"])
|
|
async def test_image_selection_saves_both_fields_and_preserves_credentials(settings_cfg, quality):
|
|
import json
|
|
|
|
image = {"apiKey": "secret", "apiBase": "https://openrouter.ai/api/v1", "model": "old", "quality": "low"}
|
|
settings_cfg.write_text(json.dumps({"tools": {"media": {"image": image}}}))
|
|
result = await console_module.settings_set(
|
|
{
|
|
"key": "tools.media.image",
|
|
"value": {"model": "openai/gpt-image-2", "quality": quality},
|
|
}
|
|
)
|
|
actual = json.loads(settings_cfg.read_text())["tools"]["media"]["image"]
|
|
assert actual == {**image, "model": "openai/gpt-image-2", "quality": quality}
|
|
assert result == {"applied": True, "previous": {"model": "old", "quality": "low"}}
|
|
|
|
|
|
async def test_image_selection_saves_the_provider_it_names(settings_cfg):
|
|
"""The image role may run on a provider other than OpenRouter; the empty name is
|
|
the OpenRouter default, which is how a pick moves back to it."""
|
|
import json
|
|
|
|
image = {"apiKey": "secret", "model": "old", "quality": "low"}
|
|
settings_cfg.write_text(json.dumps({"tools": {"media": {"image": image}}}))
|
|
await console_module.settings_set(
|
|
{"key": "tools.media.image", "value": {"model": "gpt-image-2", "quality": "", "provider": "openai"}}
|
|
)
|
|
actual = json.loads(settings_cfg.read_text())["tools"]["media"]["image"]
|
|
assert actual == {**image, "model": "gpt-image-2", "quality": "", "provider": "openai"}
|
|
|
|
await console_module.settings_set(
|
|
{"key": "tools.media.image", "value": {"model": "openai/gpt-image-2", "quality": "", "provider": ""}}
|
|
)
|
|
assert json.loads(settings_cfg.read_text())["tools"]["media"]["image"]["provider"] == ""
|
|
|
|
|
|
async def test_the_image_role_may_run_on_volcengine(settings_cfg):
|
|
"""VolcEngine's address is Volces Ark, whose Images dialect the image tool
|
|
speaks, so the roles card's pick of it is saved like any other provider's."""
|
|
import json
|
|
|
|
selection = {"model": "doubao-seedream-5-0-pro", "quality": "", "provider": "volcengine"}
|
|
await console_module.settings_set({"key": "tools.media.image", "value": selection})
|
|
image = json.loads(settings_cfg.read_text())["tools"]["media"]["image"]
|
|
assert (image["model"], image["provider"]) == ("doubao-seedream-5-0-pro", "volcengine")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"key,value",
|
|
[
|
|
("tools.media.image.model", 42),
|
|
("tools.media.image.quality", "ultra"),
|
|
("tools.media.image.quality", None),
|
|
("tools.media.image.quality", []),
|
|
("tools.media.image", {"model": "new", "quality": "ultra"}),
|
|
("tools.media.image", {"model": "new"}),
|
|
("tools.media.image", {"model": "new", "quality": "", "apiBase": "https://other.test"}),
|
|
# A provider the image tool cannot call, and one that does not exist.
|
|
("tools.media.image", {"model": "new", "quality": "", "provider": "anthropic"}),
|
|
("tools.media.image", {"model": "new", "quality": "", "provider": "no-such-vendor"}),
|
|
("tools.media.image.provider", "anthropic"),
|
|
("tools.media.image.provider", 7),
|
|
# Speech and video keep to OpenRouter: their request shapes are its own.
|
|
("tools.media.speech", {"model": "new", "quality": "", "provider": "openai"}),
|
|
("tools.media.video", {"model": "new", "quality": "", "provider": "openai"}),
|
|
],
|
|
)
|
|
async def test_invalid_image_selection_leaves_config_untouched(settings_cfg, key, value):
|
|
before = settings_cfg.read_bytes()
|
|
with pytest.raises(console_module.ConfigValidationError):
|
|
await console_module.settings_set({"key": key, "value": value})
|
|
assert settings_cfg.read_bytes() == before
|
|
|
|
|
|
@pytest.mark.parametrize("key,value", [("model", "vendor/custom-image"), ("quality", "medium"), ("provider", "openai")])
|
|
async def test_image_selection_leaves_are_writable(settings_cfg, key, value):
|
|
import json
|
|
|
|
await console_module.settings_set({"key": f"tools.media.image.{key}", "value": value})
|
|
assert json.loads(settings_cfg.read_text())["tools"]["media"]["image"][key] == value
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# fs.dirs -- the folder picker's directory walk
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _agent_home(monkeypatch, home: Path) -> None:
|
|
from raven.config import loader as config_loader
|
|
|
|
monkeypatch.setattr(config_loader, "load_config", lambda: SimpleNamespace(workspace_path=home))
|
|
|
|
|
|
async def test_fs_dirs_lists_subdirectories_only_and_marks_agent_home(tmp_path: Path, monkeypatch) -> None:
|
|
"""Directories only, dotfiles omitted, sorted by name; the agent's own home
|
|
is listed but marked not ok, since `session.create` would refuse it."""
|
|
root = tmp_path / "root"
|
|
for name in ("Zeta", "alpha", ".hidden", "agent"):
|
|
(root / name).mkdir(parents=True)
|
|
(root / "notes.md").write_text("x")
|
|
_agent_home(monkeypatch, root / "agent")
|
|
|
|
r = await console_module.fs_dirs({"path": str(root / "alpha")})
|
|
assert r["path"] == str((root / "alpha").resolve())
|
|
assert r["parent"] == str(root.resolve())
|
|
assert r["home"] == str(Path.home())
|
|
assert r["ok"] is True
|
|
|
|
r = await console_module.fs_dirs({"path": str(root)})
|
|
assert [e["name"] for e in r["entries"]] == ["agent", "alpha", "Zeta"]
|
|
by_name = {e["name"]: e for e in r["entries"]}
|
|
assert by_name["alpha"]["ok"] is True
|
|
assert by_name["alpha"]["path"] == str((root / "alpha").resolve())
|
|
assert by_name["agent"]["ok"] is False
|
|
# The parent of the agent's home is refused too (see validate_override), so
|
|
# the listing itself says it cannot be used even though its children can.
|
|
assert r["ok"] is False
|
|
|
|
|
|
async def test_fs_dirs_starts_at_the_user_home_and_stops_at_the_root(tmp_path: Path, monkeypatch) -> None:
|
|
"""No path means the home directory, which is where a person's projects
|
|
are; the filesystem root has no parent to offer."""
|
|
monkeypatch.setattr(Path, "home", classmethod(lambda cls: tmp_path))
|
|
_agent_home(monkeypatch, tmp_path / ".raven" / "workspace")
|
|
|
|
r = await console_module.fs_dirs({})
|
|
assert r["path"] == str(tmp_path.resolve())
|
|
|
|
top = await console_module.fs_dirs({"path": str(Path(tmp_path.anchor))})
|
|
assert top["parent"] is None
|
|
|
|
|
|
async def test_fs_dirs_marks_the_listed_directory_itself(tmp_path: Path, monkeypatch) -> None:
|
|
"""The picker's "use this folder" button follows `ok` on the listing, so a
|
|
person standing inside raven's data is told before pressing it."""
|
|
home = tmp_path / ".raven" / "workspace"
|
|
(home / "skills").mkdir(parents=True)
|
|
(tmp_path / "proj").mkdir()
|
|
_agent_home(monkeypatch, home)
|
|
|
|
inside = await console_module.fs_dirs({"path": str(home / "skills")})
|
|
assert inside["ok"] is False
|
|
above = await console_module.fs_dirs({"path": str(tmp_path)})
|
|
assert above["ok"] is False
|
|
beside = await console_module.fs_dirs({"path": str(tmp_path / "proj")})
|
|
assert beside["ok"] is True
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# fs.pick_dir -- the host's own folder dialog
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _dialog_answers(monkeypatch, answer: str | None) -> list[list[str]]:
|
|
"""Stand in for the dialog: record the command, answer with a folder or a dismissal.
|
|
|
|
The command itself is stood in for as well, because which one a host has is
|
|
not what these cases are about: a headless runner has neither zenity nor
|
|
kdialog, and without this they failed at `pick_dir_argv` before reaching
|
|
the answer they were written to pin.
|
|
"""
|
|
asked: list[list[str]] = []
|
|
|
|
async def run(argv: list[str]) -> str | None:
|
|
asked.append(argv)
|
|
return answer
|
|
|
|
monkeypatch.setattr(console_module, "pick_dir_argv", lambda: ["dialog"])
|
|
monkeypatch.setattr(console_module, "_run_pick_dir", run)
|
|
return asked
|
|
|
|
|
|
async def test_fs_pick_dir_hands_back_the_chosen_folder_resolved_and_judged(tmp_path: Path, monkeypatch) -> None:
|
|
"""The folder comes back absolute and resolved, with the same `ok` fs.dirs
|
|
puts on an entry, so the page needs no second call to know the create
|
|
would take it. macOS prints the path with a trailing slash; it is gone."""
|
|
(tmp_path / "proj").mkdir()
|
|
_agent_home(monkeypatch, tmp_path / ".raven" / "workspace")
|
|
asked = _dialog_answers(monkeypatch, str(tmp_path / "proj") + "/")
|
|
|
|
r = await console_module.fs_pick_dir({})
|
|
assert r == {"path": str((tmp_path / "proj").resolve()), "ok": True}
|
|
assert len(asked) == 1
|
|
|
|
|
|
async def test_fs_pick_dir_marks_the_agents_own_data_not_ok(tmp_path: Path, monkeypatch) -> None:
|
|
home = tmp_path / ".raven" / "workspace"
|
|
(home / "skills").mkdir(parents=True)
|
|
_agent_home(monkeypatch, home)
|
|
_dialog_answers(monkeypatch, str(home / "skills"))
|
|
|
|
r = await console_module.fs_pick_dir({})
|
|
assert r["ok"] is False
|
|
assert r["path"] == str((home / "skills").resolve())
|
|
|
|
|
|
async def test_fs_pick_dir_reads_a_dismissed_dialog_as_no_folder(monkeypatch) -> None:
|
|
"""Cancel is not an error: the answer carries no path, and the page leaves
|
|
the menu where it was."""
|
|
_dialog_answers(monkeypatch, None)
|
|
assert await console_module.fs_pick_dir({}) == {"ok": False}
|
|
|
|
|
|
async def test_fs_pick_dir_refuses_a_host_with_no_dialog(monkeypatch) -> None:
|
|
monkeypatch.setattr(console_module, "pick_dir_argv", lambda: None)
|
|
with pytest.raises(ConfigValidationError, match="no folder dialog"):
|
|
await console_module.fs_pick_dir({})
|
|
|
|
|
|
async def test_fs_pick_dir_refuses_a_path_that_is_not_a_directory(tmp_path: Path, monkeypatch) -> None:
|
|
(tmp_path / "notes.md").write_text("x")
|
|
_agent_home(monkeypatch, tmp_path / ".raven" / "workspace")
|
|
_dialog_answers(monkeypatch, str(tmp_path / "notes.md"))
|
|
with pytest.raises(ConfigValidationError, match="not a directory"):
|
|
await console_module.fs_pick_dir({})
|
|
|
|
|
|
async def test_run_pick_dir_reads_what_the_dialog_printed_and_nothing_else() -> None:
|
|
"""The runner is the one piece a stub cannot stand in for: it is where a
|
|
dialog's stdout becomes a path, and where silence becomes no path. A
|
|
python that prints and one that does not stand in for the two answers,
|
|
with no dialog and no desktop needed to ask.
|
|
"""
|
|
said = await console_module._run_pick_dir([sys.executable, "-c", "print(' /tmp/picked ')"])
|
|
assert said == "/tmp/picked"
|
|
assert await console_module._run_pick_dir([sys.executable, "-c", "pass"]) is None
|
|
|
|
|
|
async def test_fs_pick_dir_reports_a_dialog_that_would_not_start(monkeypatch) -> None:
|
|
"""A dialog that cannot be launched at all fails the call rather than
|
|
reading as a dismissal: nobody was asked, so answering "no folder" would
|
|
leave the menu looking as though they had said no.
|
|
"""
|
|
|
|
async def boom(argv: list[str]) -> str | None:
|
|
raise OSError("dialog: not executable")
|
|
|
|
monkeypatch.setattr(console_module, "pick_dir_argv", lambda: ["dialog"])
|
|
monkeypatch.setattr(console_module, "_run_pick_dir", boom)
|
|
with pytest.raises(ConfigValidationError, match="folder dialog failed"):
|
|
await console_module.fs_pick_dir({})
|
|
|
|
|
|
def test_fs_pick_dir_knows_a_dialog_for_each_desktop(monkeypatch) -> None:
|
|
"""One command per platform, the Linux one only where a tool is on PATH and
|
|
there is a display to show it on (see the next case)."""
|
|
monkeypatch.setattr(folder_dialog.sys, "platform", "darwin")
|
|
assert folder_dialog.pick_dir_argv()[0] == "osascript"
|
|
monkeypatch.setattr(folder_dialog.sys, "platform", "win32")
|
|
assert folder_dialog.pick_dir_argv()[0] == "powershell"
|
|
monkeypatch.setattr(folder_dialog.sys, "platform", "linux")
|
|
monkeypatch.setenv("DISPLAY", ":0")
|
|
monkeypatch.setattr(folder_dialog.shutil, "which", lambda name: name == "kdialog")
|
|
assert folder_dialog.pick_dir_argv()[0] == "kdialog"
|
|
monkeypatch.setattr(folder_dialog.shutil, "which", lambda name: False)
|
|
assert folder_dialog.pick_dir_argv() is None
|
|
|
|
|
|
def test_fs_pick_dir_finds_no_dialog_on_a_linux_host_with_no_display(monkeypatch) -> None:
|
|
"""A tool on PATH is no dialog without a display to put it on: neither one
|
|
can start without, and a dialog that exits having printed nothing reads as
|
|
a dismissal, so the page's click would do nothing at all. Wayland alone is
|
|
a display."""
|
|
monkeypatch.setattr(folder_dialog.sys, "platform", "linux")
|
|
monkeypatch.setattr(folder_dialog.shutil, "which", lambda name: True)
|
|
monkeypatch.delenv("DISPLAY", raising=False)
|
|
monkeypatch.delenv("WAYLAND_DISPLAY", raising=False)
|
|
assert folder_dialog.pick_dir_argv() is None
|
|
assert folder_dialog.folder_dialog_available() is False
|
|
monkeypatch.setenv("WAYLAND_DISPLAY", "wayland-0")
|
|
assert folder_dialog.pick_dir_argv()[0] == "zenity"
|
|
assert folder_dialog.folder_dialog_available() is True
|
|
|
|
|
|
async def test_fs_dirs_refuses_a_relative_path_and_a_file(tmp_path: Path, monkeypatch) -> None:
|
|
_agent_home(monkeypatch, tmp_path / "home")
|
|
(tmp_path / "f.txt").write_text("x")
|
|
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.fs_dirs({"path": "relative/dir"})
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.fs_dirs({"path": str(tmp_path / "f.txt")})
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.fs_dirs({"path": str(tmp_path / "missing")})
|
|
|
|
|
|
async def test_fs_dirs_reports_a_directory_it_cannot_read(tmp_path: Path, monkeypatch) -> None:
|
|
_agent_home(monkeypatch, tmp_path / "home")
|
|
target = tmp_path / "sealed"
|
|
target.mkdir()
|
|
|
|
def boom(path):
|
|
raise PermissionError("no")
|
|
|
|
monkeypatch.setattr(console_module.os, "scandir", boom)
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.fs_dirs({"path": str(target)})
|
|
|
|
|
|
async def test_fs_dirs_finds_a_directory_that_sorts_past_the_files(tmp_path: Path, monkeypatch) -> None:
|
|
"""The cap counts directories found, so the files a folder sits behind
|
|
cannot hide it. Bounding the names examined instead would answer "no
|
|
subfolders" here, and the picker has no typed path to reach it by."""
|
|
_agent_home(monkeypatch, tmp_path / "home")
|
|
root = tmp_path / "root"
|
|
root.mkdir()
|
|
for i in range(600):
|
|
(root / f"f{i:04d}.txt").write_text("x")
|
|
(root / "zzz-dir").mkdir()
|
|
|
|
r = await console_module.fs_dirs({"path": str(root)})
|
|
assert [e["name"] for e in r["entries"]] == ["zzz-dir"]
|
|
|
|
|
|
async def test_fs_dirs_returns_at_most_500_directories(tmp_path: Path, monkeypatch) -> None:
|
|
"""The cap still bounds the answer, and it keeps the first 500 by name so
|
|
the same directory lists the same way on every machine."""
|
|
_agent_home(monkeypatch, tmp_path / "home")
|
|
root = tmp_path / "root"
|
|
root.mkdir()
|
|
for i in range(600):
|
|
(root / f"d{i:04d}").mkdir()
|
|
|
|
r = await console_module.fs_dirs({"path": str(root)})
|
|
names = [e["name"] for e in r["entries"]]
|
|
assert len(names) == 500
|
|
assert names == sorted(names)
|
|
assert names[0] == "d0000"
|
|
assert names[-1] == "d0499"
|
|
|
|
|
|
async def test_fs_dirs_skips_a_child_that_vanishes_mid_listing(tmp_path: Path, monkeypatch) -> None:
|
|
_agent_home(monkeypatch, tmp_path / "home")
|
|
root = tmp_path / "root"
|
|
(root / "keep").mkdir(parents=True)
|
|
(root / "gone").mkdir()
|
|
|
|
class _Entry:
|
|
def __init__(self, child: Path) -> None:
|
|
self.name = child.name
|
|
self.path = str(child)
|
|
|
|
def is_dir(self) -> bool:
|
|
if self.name == "gone":
|
|
raise OSError("vanished")
|
|
return True
|
|
|
|
class _Scan:
|
|
def __enter__(self):
|
|
return iter([_Entry(root / "keep"), _Entry(root / "gone")])
|
|
|
|
def __exit__(self, *exc) -> bool:
|
|
return False
|
|
|
|
monkeypatch.setattr(console_module.os, "scandir", lambda path: _Scan())
|
|
r = await console_module.fs_dirs({"path": str(root)})
|
|
assert [e["name"] for e in r["entries"]] == ["keep"]
|
|
|
|
|
|
async def test_ext_list_reports_how_each_server_authenticates_and_whether_it_can(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""The panel draws its 'needs setup' chip and its credential form from two
|
|
facts the row used to lack: the auth mode and whether the credential is
|
|
there. Read from config and the credential store, never the catalog."""
|
|
|
|
def _server(**kw) -> SimpleNamespace:
|
|
base = dict(
|
|
enabled=True, type=None, command=None, url="https://example.invalid/mcp", auth="none", headers={}, env={}
|
|
)
|
|
base.update(kw)
|
|
return SimpleNamespace(**base)
|
|
|
|
cfg = SimpleNamespace(
|
|
tools=SimpleNamespace(
|
|
mcp_servers={
|
|
"oauthed": _server(auth="oauth"),
|
|
"oauth_bare": _server(auth="oauth"),
|
|
"keyed": _server(auth="apikey", headers={"Authorization": "Bearer x"}),
|
|
"key_bare": _server(auth="apikey", headers={"Authorization": ""}),
|
|
"plain": _server(),
|
|
}
|
|
)
|
|
)
|
|
from raven.config import loader as config_loader
|
|
from raven.config import raven as raven_config
|
|
|
|
monkeypatch.setattr(config_loader, "load_config", lambda: cfg)
|
|
monkeypatch.setattr(
|
|
raven_config,
|
|
"load_raven_config",
|
|
lambda: SimpleNamespace(skill_forge=None, plugins=SimpleNamespace(disabled=[])),
|
|
)
|
|
monkeypatch.setattr(console_module, "_hub_marker_name", lambda: None)
|
|
from raven.mcp import oauth as oauth_module
|
|
|
|
monkeypatch.setattr(oauth_module, "pending_url", lambda name: None)
|
|
monkeypatch.setattr(oauth_module, "has_stored_tokens", lambda name, scope=None: name == "oauthed")
|
|
|
|
class _Catalog:
|
|
def gather_all_skills(self):
|
|
return []
|
|
|
|
class _Manager:
|
|
def status(self):
|
|
return []
|
|
|
|
from raven.agent.tools.registry import ToolRegistry
|
|
|
|
loop = SimpleNamespace(
|
|
context=SimpleNamespace(skills=_Catalog()), tools=ToolRegistry(), mcp_manager_if_started=_Manager()
|
|
)
|
|
rows = {m["name"]: m for m in (await console_module.ext_list({}, agent_loop_factory=lambda: loop))["mcp"]}
|
|
|
|
assert (rows["oauthed"]["auth"], rows["oauthed"]["credentialed"]) == ("oauth", True)
|
|
assert (rows["oauth_bare"]["auth"], rows["oauth_bare"]["credentialed"]) == ("oauth", False)
|
|
assert (rows["keyed"]["auth"], rows["keyed"]["credentialed"]) == ("apikey", True)
|
|
assert (rows["key_bare"]["auth"], rows["key_bare"]["credentialed"]) == ("apikey", False)
|
|
assert (rows["plain"]["auth"], rows["plain"]["credentialed"]) == ("none", True)
|
|
|
|
|
|
def test_mcp_credential_state_reads_a_broken_token_store_as_not_credentialed(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
from types import SimpleNamespace
|
|
|
|
import raven.mcp.oauth as oauth
|
|
|
|
def broken(name: str) -> bool:
|
|
raise OSError("store unreadable")
|
|
|
|
monkeypatch.setattr(oauth, "has_stored_tokens", broken)
|
|
assert console_module._mcp_credential_state("svc", SimpleNamespace(auth="oauth")) == ("oauth", False)
|
|
|
|
|
|
def test_configured_provider_section_reads_an_unreadable_config_as_absent(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
from raven.config import loader
|
|
|
|
def broken(path):
|
|
raise OSError("gone")
|
|
|
|
monkeypatch.setattr(loader, "read_raw_or_raise", broken)
|
|
assert console_module._configured_provider_section("anthropic") is False
|
|
|
|
|
|
async def test_ext_list_marks_only_the_tools_whose_switch_the_loop_ignores(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""`builtin` answers the question the switch answers, not a schema question.
|
|
|
|
Hidden from the schema and withheld from the model are different
|
|
mechanisms: ``offers()`` consults ``withheld_names()``, which reads
|
|
``tools.disabledTools`` whatever the schema shows. Asking the hidden set
|
|
drew the DAG controls as fixed while their switch worked -- measured on a
|
|
real gateway, putting ``cancel_dag`` in that list flips its ``enabled`` to
|
|
false.
|
|
|
|
What the loop really refuses is an entry naming an MCP resource or prompt
|
|
meta-tool, which it registers and withdraws on its own
|
|
(``_report_reserved_disabled_tools``), plus the two tool-search meta-tools,
|
|
fixed by product decision as the doorway.
|
|
"""
|
|
from raven.agent.tools.tool_search import META_TOOL_NAMES
|
|
from raven.mcp.prompts import PROMPT_TOOL_NAMES
|
|
from raven.mcp.resources import RESOURCE_TOOL_NAMES
|
|
|
|
loop = _console_loop(tmp_path, monkeypatch, {"providers": {}})
|
|
rows = await _ext_rows(loop, monkeypatch)
|
|
|
|
fixed = META_TOOL_NAMES | RESOURCE_TOOL_NAMES | PROMPT_TOOL_NAMES
|
|
assert "tool_call" in rows and rows["tool_call"]["builtin"] is True
|
|
|
|
hidden = loop.tools.schema_hidden_names()
|
|
assert hidden, "no hidden tool registered: the case below would pass on an empty set"
|
|
for name in hidden - fixed:
|
|
if name in rows:
|
|
assert rows[name]["builtin"] is False, f"{name} answers to the switch, so it carries one"
|
|
|
|
switchable = [n for n, r in rows.items() if n not in fixed and "builtin" in r]
|
|
assert switchable, "no switchable tool reported"
|
|
assert all(rows[n]["builtin"] is False for n in switchable)
|
|
|
|
|
|
async def test_ext_list_reports_tool_search_even_when_the_fold_is_off(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""A meta-tool the loop skipped is still the page's to draw.
|
|
|
|
``tool_search`` is registered only when progressive tool disclosure is on
|
|
(``tools.toolSearch.enabled``, which ships on); ``tool_call`` is registered
|
|
either way. Reporting only what the registry holds left an install that
|
|
turned the fold off with a card named after tool search containing the one
|
|
meta-tool that is not tool search, and nothing said the feature existed. The
|
|
same reasoning as ``_gated_tools``: absent from the list reads as deleted.
|
|
|
|
The fold is switched off here rather than left to the default, which is what
|
|
puts the tool in the absent state this case is about.
|
|
"""
|
|
from raven.agent.tools.tool_search import TOOL_CALL_NAME, TOOL_SEARCH_NAME
|
|
from raven.config.schema import ToolSearchConfig
|
|
|
|
loop = _console_loop(tmp_path, monkeypatch, {"providers": {}}, tool_search_config=ToolSearchConfig(enabled=False))
|
|
assert TOOL_SEARCH_NAME not in loop.tools.tool_names, (
|
|
"the fold is on in this fixture, so the absent case below is not being exercised"
|
|
)
|
|
|
|
rows = await _ext_rows(loop, monkeypatch)
|
|
assert TOOL_SEARCH_NAME in rows, "the page cannot draw a row it is never told about"
|
|
# True because this page writes `tools.disabledTools`, and no entry there
|
|
# registers this tool: its switch is `tools.toolSearch.enabled`.
|
|
assert rows[TOOL_SEARCH_NAME]["builtin"] is True
|
|
assert rows[TOOL_SEARCH_NAME]["enabled"] is False
|
|
assert rows[TOOL_CALL_NAME]["builtin"] is True
|
|
|
|
|
|
async def test_ext_list_reports_a_registered_meta_tool_once(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""The absent-row pass must not double a tool the registry already holds."""
|
|
from raven.agent.tools.tool_search import TOOL_CALL_NAME
|
|
|
|
loop = _console_loop(tmp_path, monkeypatch, {"providers": {}})
|
|
from raven.config import raven as raven_config
|
|
|
|
monkeypatch.setattr(
|
|
raven_config,
|
|
"load_raven_config",
|
|
lambda: SimpleNamespace(skill_forge=None, plugins=SimpleNamespace(disabled=[])),
|
|
)
|
|
monkeypatch.setattr(console_module, "_hub_marker_name", lambda: None)
|
|
payload = await console_module.ext_list({}, agent_loop_factory=lambda: loop)
|
|
names = [t["name"] for t in payload["tools"]]
|
|
assert names.count(TOOL_CALL_NAME) == 1
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# deck.templates.*: the picker's list, and a pick that lands as an upload would
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _bundled_templates(monkeypatch, root: Path, names: tuple[str, ...]) -> None:
|
|
"""Stand in for the installed engine's template directory, and for a host
|
|
that cannot draw covers -- the picture is the renderer's business, tested
|
|
where it lives; what these handlers owe is the list and the copy."""
|
|
from raven.rpc import deck_templates
|
|
|
|
root.mkdir(parents=True, exist_ok=True)
|
|
for name in names:
|
|
(root / f"{name}.pptx").write_bytes(b"PK" + name.encode())
|
|
monkeypatch.setattr(deck_templates, "templates_dir", lambda: root)
|
|
monkeypatch.setattr(deck_templates, "_rasteriser_available", lambda: False)
|
|
|
|
|
|
async def test_deck_templates_list_names_every_bundled_template_in_a_stable_order(tmp_path: Path, monkeypatch) -> None:
|
|
_bundled_templates(monkeypatch, tmp_path / "tpl", ("warm_bauhaus_quarterly_review", "amber_wave_quarterly_summary"))
|
|
|
|
r = await console_module.deck_templates_list({}, agent_loop_factory=_loop_factory(None))
|
|
|
|
assert r["available"] is True
|
|
assert [row["name"] for row in r["templates"]] == ["amber_wave_quarterly_summary", "warm_bauhaus_quarterly_review"]
|
|
assert r["templates"][0]["label"] == "Amber Wave Quarterly Summary"
|
|
assert r["templates"][0]["size"] == len(b"PKamber_wave_quarterly_summary")
|
|
# No renderer here, so no picture and nothing on its way -- and still a
|
|
# list, because a picker with names alone still picks.
|
|
assert all(row["cover"] is None for row in r["templates"])
|
|
assert r["pending"] is False
|
|
|
|
|
|
async def test_deck_templates_list_answers_at_once_and_draws_the_covers_behind_it(tmp_path: Path, monkeypatch) -> None:
|
|
"""Ten templates are a minute and a half of LibreOffice on first sight. The
|
|
list does not wait for that: it answers with what is on disk, says a cover
|
|
is pending, and the next ask finds the picture."""
|
|
import base64
|
|
|
|
from raven.rpc import deck_templates
|
|
|
|
_bundled_templates(monkeypatch, tmp_path / "tpl", ("gold_panel_year_end_summary",))
|
|
monkeypatch.setattr(deck_templates, "_rasteriser_available", lambda: True)
|
|
monkeypatch.setattr(deck_templates, "cover_cache_dir", lambda: tmp_path / "covers")
|
|
drawn: list[str] = []
|
|
|
|
async def draw(template, *_):
|
|
drawn.append(template.name)
|
|
target = deck_templates.cover_cache_dir() / f"{deck_templates._drawn_key(template.path)}.jpg"
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
target.write_bytes(b"\xff\xd8jpeg")
|
|
return target
|
|
|
|
monkeypatch.setattr(deck_templates, "cover_for", draw)
|
|
|
|
first = await console_module.deck_templates_list({}, agent_loop_factory=_loop_factory(None))
|
|
assert first["pending"] is True and first["templates"][0]["cover"] is None
|
|
await asyncio.sleep(0)
|
|
again = await console_module.deck_templates_list({}, agent_loop_factory=_loop_factory(None))
|
|
assert again["pending"] is False
|
|
assert again["templates"][0]["cover"] == "data:image/jpeg;base64," + base64.b64encode(b"\xff\xd8jpeg").decode()
|
|
assert drawn == ["gold_panel_year_end_summary"], "one render per template, not one per ask"
|
|
|
|
|
|
async def test_deck_templates_list_draws_at_most_three_covers_at_once(tmp_path: Path, monkeypatch) -> None:
|
|
"""A cold gallery asks for every template in one walk. The covers are still
|
|
drawn behind the answer, but three LibreOffice at a time, not ten: the
|
|
fourth waits for one of the first three to finish."""
|
|
from raven.rpc import deck_templates, pdf_preview
|
|
|
|
names = tuple(f"template_{n}" for n in range(10))
|
|
_bundled_templates(monkeypatch, tmp_path / "tpl", names)
|
|
monkeypatch.setattr(deck_templates, "_rasteriser_available", lambda: True)
|
|
monkeypatch.setattr(deck_templates, "cover_cache_dir", lambda: tmp_path / "covers")
|
|
monkeypatch.setattr(deck_templates, "_failed", set())
|
|
monkeypatch.setattr(deck_templates, "_drawing", {})
|
|
monkeypatch.setattr(deck_templates, "_render_gate", asyncio.Semaphore(deck_templates.COVER_RENDERS_AT_ONCE))
|
|
in_flight = 0
|
|
peak = 0
|
|
release = asyncio.Event()
|
|
|
|
async def slow_pdf(path, **_):
|
|
nonlocal in_flight, peak
|
|
in_flight += 1
|
|
peak = max(peak, in_flight)
|
|
await release.wait()
|
|
in_flight -= 1
|
|
return path
|
|
|
|
def rasterise(pdf, target):
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
target.write_bytes(b"\xff\xd8jpeg")
|
|
|
|
monkeypatch.setattr(pdf_preview, "pdf_for", slow_pdf)
|
|
monkeypatch.setattr(deck_templates, "_rasterise_first_page", rasterise)
|
|
|
|
first = await console_module.deck_templates_list({}, agent_loop_factory=_loop_factory(None))
|
|
assert first["pending"] is True
|
|
for _ in range(5):
|
|
await asyncio.sleep(0)
|
|
assert peak == deck_templates.COVER_RENDERS_AT_ONCE, "ten misses start three conversions, not ten"
|
|
release.set()
|
|
await asyncio.gather(*deck_templates._drawing.values())
|
|
again = await console_module.deck_templates_list({}, agent_loop_factory=_loop_factory(None))
|
|
assert again["pending"] is False and all(row["cover"] for row in again["templates"])
|
|
assert peak == deck_templates.COVER_RENDERS_AT_ONCE
|
|
|
|
|
|
async def test_deck_templates_list_stops_asking_for_a_cover_that_cannot_be_drawn(tmp_path: Path, monkeypatch) -> None:
|
|
"""A render that fails is not retried on the next ask, and the answer stops
|
|
saying pending -- or the page would poll forever and start LibreOffice each
|
|
time for a picture that never comes."""
|
|
from raven.rpc import deck_templates, pdf_preview
|
|
|
|
_bundled_templates(monkeypatch, tmp_path / "tpl", ("teal_illustrated_work_analysis",))
|
|
monkeypatch.setattr(deck_templates, "_rasteriser_available", lambda: True)
|
|
monkeypatch.setattr(deck_templates, "cover_cache_dir", lambda: tmp_path / "covers")
|
|
monkeypatch.setattr(deck_templates, "_failed", set())
|
|
asked: list[str] = []
|
|
|
|
async def no_pdf(source, **_kw):
|
|
asked.append(source.name)
|
|
raise pdf_preview.PdfPreviewUnavailableError("no soffice")
|
|
|
|
monkeypatch.setattr(pdf_preview, "pdf_for", no_pdf)
|
|
|
|
first = await console_module.deck_templates_list({}, agent_loop_factory=_loop_factory(None))
|
|
assert first["pending"] is True
|
|
await asyncio.sleep(0)
|
|
await asyncio.sleep(0)
|
|
again = await console_module.deck_templates_list({}, agent_loop_factory=_loop_factory(None))
|
|
assert again["pending"] is False and again["templates"][0]["cover"] is None
|
|
assert asked == ["teal_illustrated_work_analysis.pptx"]
|
|
|
|
|
|
async def test_deck_templates_list_is_empty_and_unavailable_without_the_engine(monkeypatch) -> None:
|
|
from raven.rpc import deck_templates
|
|
|
|
monkeypatch.setattr(deck_templates, "templates_dir", lambda: None)
|
|
|
|
r = await console_module.deck_templates_list({}, agent_loop_factory=_loop_factory(None))
|
|
|
|
assert r == {"templates": [], "available": False, "pending": False}
|
|
|
|
|
|
async def test_deck_templates_pick_lands_under_uploads_as_fs_upload_answers(tmp_path: Path, monkeypatch) -> None:
|
|
"""The route to the deck engine opens on a .pptx the turn hands over, and
|
|
turn.send admits the paths fs.upload mints; a pick is made into one of those."""
|
|
home = tmp_path / "home"
|
|
home.mkdir()
|
|
_agent_home(monkeypatch, home)
|
|
_bundled_templates(monkeypatch, tmp_path / "tpl", ("mint_memphis_thesis_defense",))
|
|
|
|
r = await console_module.deck_templates_pick(
|
|
{"name": "mint_memphis_thesis_defense"}, agent_loop_factory=_loop_factory(None)
|
|
)
|
|
|
|
assert r["path"] == "uploads/mint_memphis_thesis_defense.pptx"
|
|
assert r["abs_path"] == str(home.resolve() / "uploads" / "mint_memphis_thesis_defense.pptx")
|
|
assert r["size"] == len(b"PKmint_memphis_thesis_defense")
|
|
assert (home / "uploads" / "mint_memphis_thesis_defense.pptx").read_bytes() == b"PKmint_memphis_thesis_defense"
|
|
|
|
# Picked twice, the second copy sits beside the first rather than over it,
|
|
# the way a second upload of the same name does.
|
|
again = await console_module.deck_templates_pick(
|
|
{"name": "mint_memphis_thesis_defense.pptx"}, agent_loop_factory=_loop_factory(None)
|
|
)
|
|
assert again["path"] == "uploads/mint_memphis_thesis_defense-1.pptx"
|
|
|
|
|
|
async def test_deck_templates_pick_says_why_when_the_copy_cannot_land(tmp_path: Path, monkeypatch) -> None:
|
|
from raven.rpc import deck_templates
|
|
from raven.rpc.errors import ConfigValidationError
|
|
|
|
home = tmp_path / "home"
|
|
home.mkdir()
|
|
_agent_home(monkeypatch, home)
|
|
_bundled_templates(monkeypatch, tmp_path / "tpl", ("mint_memphis_thesis_defense",))
|
|
|
|
def full_disk(template, uploads, *_, **kwargs):
|
|
raise OSError(28, "No space left on device")
|
|
|
|
monkeypatch.setattr(deck_templates, "deposit", full_disk)
|
|
with pytest.raises(ConfigValidationError, match="cannot place the template under uploads"):
|
|
await console_module.deck_templates_pick(
|
|
{"name": "mint_memphis_thesis_defense"}, agent_loop_factory=_loop_factory(None)
|
|
)
|
|
|
|
|
|
async def test_deck_templates_pick_refuses_a_name_that_is_not_a_bundled_template(tmp_path: Path, monkeypatch) -> None:
|
|
from raven.rpc.errors import ConfigValidationError
|
|
|
|
home = tmp_path / "home"
|
|
home.mkdir()
|
|
_agent_home(monkeypatch, home)
|
|
_bundled_templates(monkeypatch, tmp_path / "tpl", ("mint_memphis_thesis_defense",))
|
|
|
|
for name in ("", "nope", "../mint_memphis_thesis_defense", "tpl/mint_memphis_thesis_defense", ".hidden"):
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.deck_templates_pick({"name": name}, agent_loop_factory=_loop_factory(None))
|
|
assert not (home / "uploads").exists()
|
|
|
|
|
|
async def test_deck_templates_pages_renders_every_page_once_and_refuses_a_stranger(tmp_path: Path, monkeypatch) -> None:
|
|
from raven.rpc import deck_templates
|
|
from raven.rpc.errors import ConfigValidationError
|
|
|
|
_bundled_templates(monkeypatch, tmp_path / "tpl", ("blue_minimal_general_analysis",))
|
|
monkeypatch.setattr(deck_templates, "_rasteriser_available", lambda: True)
|
|
monkeypatch.setattr(deck_templates, "cover_cache_dir", lambda: tmp_path / "covers")
|
|
rendered: list[str] = []
|
|
|
|
async def pdf_for(source, **_kw):
|
|
return source
|
|
|
|
def every_page(pdf, stem):
|
|
out = []
|
|
for n in (1, 2, 3):
|
|
target = stem.with_name(f"{stem.name}-p{n:02d}.jpg")
|
|
if not target.is_file():
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
target.write_bytes(b"jpeg%d" % n)
|
|
rendered.append(target.name)
|
|
out.append(target)
|
|
return out
|
|
|
|
monkeypatch.setattr(
|
|
deck_templates.pdf_preview
|
|
if hasattr(deck_templates, "pdf_preview")
|
|
else __import__("raven.rpc.pdf_preview", fromlist=["x"]),
|
|
"pdf_for",
|
|
pdf_for,
|
|
)
|
|
monkeypatch.setattr(deck_templates, "_rasterise_every_page", every_page)
|
|
|
|
r = await console_module.deck_templates_pages(
|
|
{"name": "blue_minimal_general_analysis"}, agent_loop_factory=_loop_factory(None)
|
|
)
|
|
assert r["pages"] == [
|
|
"data:image/jpeg;base64," + __import__("base64").b64encode(b"jpeg%d" % n).decode() for n in (1, 2, 3)
|
|
]
|
|
again = await console_module.deck_templates_pages(
|
|
{"name": "blue_minimal_general_analysis"}, agent_loop_factory=_loop_factory(None)
|
|
)
|
|
assert again == r and len(rendered) == 3, "the second ask reads the cache"
|
|
|
|
with pytest.raises(ConfigValidationError):
|
|
await console_module.deck_templates_pages({"name": "nope"}, agent_loop_factory=_loop_factory(None))
|
|
|
|
|
|
def test_viewer_root_serves_a_session_its_own_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""The session's own directory answers first, whatever the path is called."""
|
|
session = tmp_path / "project"
|
|
(session / "uploads").mkdir(parents=True)
|
|
(session / "uploads" / "shot.png").write_bytes(b"session copy")
|
|
home = tmp_path / "agent-home"
|
|
(home / "uploads").mkdir(parents=True)
|
|
(home / "uploads" / "shot.png").write_bytes(b"agent home copy")
|
|
monkeypatch.setattr(console_module, "_upload_root", lambda: home)
|
|
|
|
assert console_module.viewer_root(session, Path("uploads/shot.png")) == session
|
|
|
|
|
|
def test_viewer_root_finds_an_upload_a_pinned_session_cannot_see(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""An upload is relative to agent home, not to the session that asked for it.
|
|
|
|
`fs.upload` deposits there whichever session asked and answers with a path
|
|
relative to it, while the viewer resolves a relative path against the
|
|
session's own working directory. For a session pinned elsewhere the two
|
|
part company, and a picture the reader attached came back 404.
|
|
"""
|
|
session = tmp_path / "project"
|
|
session.mkdir()
|
|
home = tmp_path / "agent-home"
|
|
(home / "uploads").mkdir(parents=True)
|
|
(home / "uploads" / "shot.png").write_bytes(b"the picture")
|
|
monkeypatch.setattr(console_module, "_upload_root", lambda: home)
|
|
|
|
assert console_module.viewer_root(session, Path("uploads/shot.png")) == home
|
|
|
|
|
|
def test_viewer_root_leaves_every_other_path_with_the_session(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""Only an upload may come from the other root.
|
|
|
|
A file the session does not have stays the session's question: answering it
|
|
from agent home would widen what the viewer serves past the one ambiguity
|
|
this is for, and a missing file has to read as missing.
|
|
"""
|
|
session = tmp_path / "project"
|
|
session.mkdir()
|
|
home = tmp_path / "agent-home"
|
|
(home / "notes").mkdir(parents=True)
|
|
(home / "notes" / "secret.md").write_text("not this session's")
|
|
(home / "uploads").mkdir(parents=True)
|
|
monkeypatch.setattr(console_module, "_upload_root", lambda: home)
|
|
|
|
assert console_module.viewer_root(session, Path("notes/secret.md")) == session
|
|
assert console_module.viewer_root(session, Path("uploads/absent.png")) == session
|
|
|
|
|
|
def test_viewer_root_tells_an_upload_by_where_its_path_lands(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""An upload is a path that lands in agent home's ``uploads/``, in either spelling.
|
|
|
|
The first segment is not the test: ``uploads/../notes/secret.md`` starts like
|
|
an upload and lands beside it, and a link left under ``uploads/`` can point
|
|
anywhere, so read by its spelling either reached past the one ambiguity this
|
|
is for. The absolute spelling of a real upload is an upload too, and it is
|
|
what the composer stages.
|
|
"""
|
|
session = tmp_path / "project"
|
|
session.mkdir()
|
|
home = tmp_path / "agent-home"
|
|
(home / "notes").mkdir(parents=True)
|
|
secret = home / "notes" / "secret.md"
|
|
secret.write_text("not this session's")
|
|
(home / "uploads").mkdir()
|
|
shot = home / "uploads" / "shot.png"
|
|
shot.write_bytes(b"the picture")
|
|
(home / "uploads" / "link.md").symlink_to(secret)
|
|
monkeypatch.setattr(console_module, "_upload_root", lambda: home)
|
|
|
|
assert console_module.viewer_root(session, Path("uploads/../notes/secret.md")) == session
|
|
assert console_module.viewer_root(session, Path("uploads/link.md")) == session
|
|
assert console_module.viewer_root(session, home / "uploads" / "link.md") == session
|
|
assert console_module.viewer_root(session, secret) == session
|
|
assert console_module.viewer_root(session, shot) == home
|
|
|
|
|
|
def test_viewer_root_follows_a_linked_agent_home(tmp_path: Path) -> None:
|
|
"""``workspace_path`` is agent home as configured, not resolved, so it can be
|
|
reached through a link (``turn.send`` hands it over as is): an upload under
|
|
it is still an upload, in either spelling."""
|
|
from raven.rpc.files import viewer_root
|
|
|
|
session = tmp_path / "project"
|
|
session.mkdir()
|
|
real = tmp_path / "agent-home"
|
|
(real / "uploads").mkdir(parents=True)
|
|
(real / "uploads" / "shot.png").write_bytes(b"the picture")
|
|
linked = tmp_path / "linked-home"
|
|
linked.symlink_to(real)
|
|
|
|
assert viewer_root(session, Path("uploads/shot.png"), linked) == linked
|
|
assert viewer_root(session, real / "uploads" / "shot.png", linked) == linked
|