Update local frontend and backend changes
This commit is contained in:
1 parent
faca2c9a2d
commit
a27206c543
35 files changed
+1756
-334
No files matched your search
@@ -0,0 +1,206 @@
|
||||
"""Tests for editing archived scheduled-task run files."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from deerflow.config import paths as paths_module
|
||||
from deerflow.persistence.scheduled_tasks import MemoryScheduledTaskStore
|
||||
from deerflow.runtime.scheduler.service import ScheduledTaskService
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def isolated_paths(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setenv("DEER_FLOW_HOME", str(tmp_path))
|
||||
monkeypatch.setattr(paths_module, "_paths", None)
|
||||
yield tmp_path
|
||||
monkeypatch.setattr(paths_module, "_paths", None)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def store() -> MemoryScheduledTaskStore:
|
||||
return MemoryScheduledTaskStore()
|
||||
|
||||
|
||||
def _build_service(store: MemoryScheduledTaskStore) -> ScheduledTaskService:
|
||||
service = ScheduledTaskService(app=None, store=store, poll_interval_seconds=60.0)
|
||||
service._element_client = None # noqa: SLF001
|
||||
return service
|
||||
|
||||
|
||||
def _seed_task(store: MemoryScheduledTaskStore, *, task_id: str, user_id: str, published: bool = False) -> None:
|
||||
store._tasks[task_id] = { # noqa: SLF001
|
||||
"task_id": task_id,
|
||||
"user_id": user_id,
|
||||
"scheduler_thread_id": f"thread-{user_id}",
|
||||
"name": "demo",
|
||||
"prompt": "hi",
|
||||
"schedule_text": "0 9 * * *",
|
||||
"cron_expr": "0 9 * * *",
|
||||
"timezone": "UTC",
|
||||
"enabled": True,
|
||||
"next_run_at": datetime.now(UTC),
|
||||
"execution_context": {},
|
||||
"published": published,
|
||||
"published_at": datetime.now(UTC) if published else None,
|
||||
}
|
||||
|
||||
|
||||
async def _seed_run_with_file(
|
||||
store: MemoryScheduledTaskStore,
|
||||
isolated_paths: Path,
|
||||
*,
|
||||
run_id: str,
|
||||
task_id: str,
|
||||
user_id: str,
|
||||
file_name: str,
|
||||
file_text: str,
|
||||
mime_type: str = "text/markdown",
|
||||
) -> dict[str, Any]:
|
||||
archive_dir = isolated_paths / "scheduled-task-runs" / run_id / "files"
|
||||
archive_dir.mkdir(parents=True)
|
||||
file_path = archive_dir / file_name
|
||||
file_path.write_text(file_text, encoding="utf-8")
|
||||
|
||||
file_entry = {
|
||||
"name": file_name,
|
||||
"path": str(file_path),
|
||||
"mime_type": mime_type,
|
||||
"kind": "markdown" if file_name.endswith(".md") else "artifact",
|
||||
"size": file_path.stat().st_size,
|
||||
}
|
||||
result = {"title": "demo", "content": "ignored", "format": "markdown", "files": [file_entry]}
|
||||
|
||||
scheduled_for = datetime.now(UTC)
|
||||
await store.create_run_once(
|
||||
{
|
||||
"id": run_id,
|
||||
"task_id": task_id,
|
||||
"user_id": user_id,
|
||||
"scheduler_thread_id": f"thread-{user_id}",
|
||||
"scheduled_for": scheduled_for,
|
||||
"status": "succeeded",
|
||||
"started_at": scheduled_for,
|
||||
}
|
||||
)
|
||||
await store.update_run(run_id, {"result": result, "finished_at": scheduled_for})
|
||||
return file_entry
|
||||
|
||||
|
||||
# ---------- can_edit flag on get_run ----------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_run_marks_owner_as_can_edit(store, isolated_paths):
|
||||
_seed_task(store, task_id="t1", user_id="alice", published=True)
|
||||
await _seed_run_with_file(store, isolated_paths, run_id="r1", task_id="t1", user_id="alice", file_name="a.md", file_text="# hi")
|
||||
service = _build_service(store)
|
||||
|
||||
run = await service.get_run("alice", "r1")
|
||||
assert run is not None
|
||||
assert run["can_edit"] is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_run_marks_subscriber_as_read_only(store, isolated_paths):
|
||||
_seed_task(store, task_id="t1", user_id="alice", published=True)
|
||||
await _seed_run_with_file(store, isolated_paths, run_id="r1", task_id="t1", user_id="alice", file_name="a.md", file_text="# hi")
|
||||
service = _build_service(store)
|
||||
await service.subscribe_task("bob", "t1", notify_element=False)
|
||||
|
||||
run = await service.get_run("bob", "r1")
|
||||
assert run is not None
|
||||
assert run["can_edit"] is False
|
||||
|
||||
|
||||
# ---------- update_run_file ----------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_owner_can_overwrite_md_file(store, isolated_paths):
|
||||
_seed_task(store, task_id="t1", user_id="alice")
|
||||
file_entry = await _seed_run_with_file(
|
||||
store,
|
||||
isolated_paths,
|
||||
run_id="r1",
|
||||
task_id="t1",
|
||||
user_id="alice",
|
||||
file_name="report.md",
|
||||
file_text="# original",
|
||||
)
|
||||
service = _build_service(store)
|
||||
|
||||
new_text = "# updated\n\nA new paragraph."
|
||||
meta = await service.update_run_file("alice", "r1", 0, new_text)
|
||||
|
||||
assert meta["name"] == "report.md"
|
||||
assert meta["size"] == len(new_text.encode("utf-8"))
|
||||
|
||||
# The on-disk file is overwritten.
|
||||
assert Path(file_entry["path"]).read_text(encoding="utf-8") == new_text
|
||||
|
||||
# The size in the run row is refreshed.
|
||||
run = await service.get_run("alice", "r1")
|
||||
assert run is not None
|
||||
assert run["result"]["files"][0]["size"] == meta["size"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_subscriber_cannot_edit(store, isolated_paths):
|
||||
_seed_task(store, task_id="t1", user_id="alice", published=True)
|
||||
await _seed_run_with_file(store, isolated_paths, run_id="r1", task_id="t1", user_id="alice", file_name="a.md", file_text="# hi")
|
||||
service = _build_service(store)
|
||||
await service.subscribe_task("bob", "t1", notify_element=False)
|
||||
|
||||
with pytest.raises(PermissionError):
|
||||
await service.update_run_file("bob", "r1", 0, "# evil")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unrelated_user_gets_not_found(store, isolated_paths):
|
||||
_seed_task(store, task_id="t1", user_id="alice")
|
||||
await _seed_run_with_file(store, isolated_paths, run_id="r1", task_id="t1", user_id="alice", file_name="a.md", file_text="# hi")
|
||||
service = _build_service(store)
|
||||
|
||||
with pytest.raises(PermissionError):
|
||||
await service.update_run_file("eve", "r1", 0, "# evil")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_run_returns_filenotfound(store, isolated_paths):
|
||||
service = _build_service(store)
|
||||
with pytest.raises(FileNotFoundError):
|
||||
await service.update_run_file("alice", "missing-run", 0, "# x")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_invalid_file_index_rejected(store, isolated_paths):
|
||||
_seed_task(store, task_id="t1", user_id="alice")
|
||||
await _seed_run_with_file(store, isolated_paths, run_id="r1", task_id="t1", user_id="alice", file_name="a.md", file_text="# hi")
|
||||
service = _build_service(store)
|
||||
|
||||
with pytest.raises(FileNotFoundError):
|
||||
await service.update_run_file("alice", "r1", 5, "# x")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_non_text_file_rejected(store, isolated_paths):
|
||||
_seed_task(store, task_id="t1", user_id="alice")
|
||||
await _seed_run_with_file(
|
||||
store,
|
||||
isolated_paths,
|
||||
run_id="r1",
|
||||
task_id="t1",
|
||||
user_id="alice",
|
||||
file_name="diagram.png",
|
||||
file_text="not really a png but stored as such",
|
||||
mime_type="image/png",
|
||||
)
|
||||
service = _build_service(store)
|
||||
|
||||
with pytest.raises(ValueError, match="text-previewable"):
|
||||
await service.update_run_file("alice", "r1", 0, "# x")
|
||||
@@ -0,0 +1,241 @@
|
||||
"""Tests for skill upload validation + install via the storage layer."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from deerflow.skills.installer import SkillAlreadyExistsError
|
||||
from deerflow.skills.storage.local_skill_storage import LocalSkillStorage
|
||||
|
||||
|
||||
def _make_skill_zip(
|
||||
*,
|
||||
skill_name: str = "demo-skill",
|
||||
description: str = "Use this skill when demoing things.",
|
||||
license_value: str | None = "MIT",
|
||||
allowed_tools: list[str] | None = None,
|
||||
extra_files: dict[str, str] | None = None,
|
||||
include_skill_md: bool = True,
|
||||
bad_yaml: bool = False,
|
||||
nest_under_root: bool = True,
|
||||
) -> bytes:
|
||||
"""Build an in-memory .skill ZIP archive for tests."""
|
||||
buf = io.BytesIO()
|
||||
root = f"{skill_name}/" if nest_under_root else ""
|
||||
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||
if include_skill_md:
|
||||
if bad_yaml:
|
||||
content = "---\nname: ???: bad\n---\nhi"
|
||||
else:
|
||||
lines = ["---", f"name: {skill_name}", f"description: {description}"]
|
||||
if license_value:
|
||||
lines.append(f"license: {license_value}")
|
||||
if allowed_tools:
|
||||
lines.append("allowed-tools:")
|
||||
for tool in allowed_tools:
|
||||
lines.append(f" - {tool}")
|
||||
lines.extend(["---", "# " + skill_name, "Hello world."])
|
||||
content = "\n".join(lines)
|
||||
zf.writestr(f"{root}SKILL.md", content)
|
||||
for rel, body in (extra_files or {}).items():
|
||||
zf.writestr(f"{root}{rel}", body)
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def storage(tmp_path: Path) -> LocalSkillStorage:
|
||||
skills_root = tmp_path / "skills"
|
||||
(skills_root / "public").mkdir(parents=True)
|
||||
(skills_root / "custom").mkdir(parents=True)
|
||||
return LocalSkillStorage(host_path=str(skills_root))
|
||||
|
||||
|
||||
def _write_archive(tmp_path: Path, data: bytes, *, suffix: str = ".skill") -> Path:
|
||||
archive = tmp_path / f"upload{suffix}"
|
||||
archive.write_bytes(data)
|
||||
return archive
|
||||
|
||||
|
||||
# ---------- validate_skill_archive ----------
|
||||
|
||||
|
||||
def test_validate_returns_metadata_and_no_conflict(storage: LocalSkillStorage, tmp_path: Path):
|
||||
archive = _write_archive(
|
||||
tmp_path,
|
||||
_make_skill_zip(allowed_tools=["bash", "read_file"]),
|
||||
)
|
||||
|
||||
result = storage.validate_skill_archive(archive)
|
||||
|
||||
assert result["ok"] is True
|
||||
assert result["skill_name"] == "demo-skill"
|
||||
assert result["description"] == "Use this skill when demoing things."
|
||||
assert result["license"] == "MIT"
|
||||
assert result["allowed_tools"] == ["bash", "read_file"]
|
||||
assert result["conflict"] is False
|
||||
assert result["conflict_kind"] is None
|
||||
assert result["warnings"] == []
|
||||
|
||||
|
||||
def test_validate_flags_script_files_as_warning(storage: LocalSkillStorage, tmp_path: Path):
|
||||
archive = _write_archive(
|
||||
tmp_path,
|
||||
_make_skill_zip(extra_files={"scripts/hi.py": "print('hi')\n"}),
|
||||
)
|
||||
|
||||
result = storage.validate_skill_archive(archive)
|
||||
|
||||
assert result["ok"] is True
|
||||
assert result["warnings"] and "script" in result["warnings"][0].lower()
|
||||
|
||||
|
||||
def test_validate_detects_custom_conflict(storage: LocalSkillStorage, tmp_path: Path):
|
||||
# Pre-install one with the same name into custom/
|
||||
target = storage.get_skills_root_path() / "custom" / "demo-skill"
|
||||
target.mkdir(parents=True)
|
||||
(target / "SKILL.md").write_text(
|
||||
"---\nname: demo-skill\ndescription: existing\n---\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
archive = _write_archive(tmp_path, _make_skill_zip())
|
||||
result = storage.validate_skill_archive(archive)
|
||||
|
||||
assert result["conflict"] is True
|
||||
assert result["conflict_kind"] == "custom"
|
||||
|
||||
|
||||
def test_validate_detects_public_conflict(storage: LocalSkillStorage, tmp_path: Path):
|
||||
target = storage.get_skills_root_path() / "public" / "demo-skill"
|
||||
target.mkdir(parents=True)
|
||||
(target / "SKILL.md").write_text(
|
||||
"---\nname: demo-skill\ndescription: existing\n---\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
archive = _write_archive(tmp_path, _make_skill_zip())
|
||||
result = storage.validate_skill_archive(archive)
|
||||
|
||||
assert result["conflict"] is True
|
||||
assert result["conflict_kind"] == "public"
|
||||
|
||||
|
||||
def test_validate_rejects_bad_zip(storage: LocalSkillStorage, tmp_path: Path):
|
||||
archive = _write_archive(tmp_path, b"not a zip at all")
|
||||
with pytest.raises(ValueError, match="not a valid ZIP"):
|
||||
storage.validate_skill_archive(archive)
|
||||
|
||||
|
||||
def test_validate_rejects_missing_skill_md(storage: LocalSkillStorage, tmp_path: Path):
|
||||
archive = _write_archive(tmp_path, _make_skill_zip(include_skill_md=False))
|
||||
with pytest.raises(ValueError, match="SKILL.md"):
|
||||
storage.validate_skill_archive(archive)
|
||||
|
||||
|
||||
def test_validate_rejects_bad_frontmatter(storage: LocalSkillStorage, tmp_path: Path):
|
||||
archive = _write_archive(tmp_path, _make_skill_zip(bad_yaml=True))
|
||||
with pytest.raises(ValueError):
|
||||
storage.validate_skill_archive(archive)
|
||||
|
||||
|
||||
# ---------- ainstall_skill_from_archive (overwrite) ----------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_install_writes_to_custom_dir(
|
||||
storage: LocalSkillStorage, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
):
|
||||
# Bypass the LLM security scan so the test stays hermetic.
|
||||
async def _noop_scan(*args, **kwargs): # noqa: ANN001, ARG001
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(
|
||||
"deerflow.skills.installer._scan_skill_archive_contents_or_raise",
|
||||
_noop_scan,
|
||||
)
|
||||
|
||||
archive = _write_archive(tmp_path, _make_skill_zip())
|
||||
result = await storage.ainstall_skill_from_archive(archive, require_skill_suffix=False)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["skill_name"] == "demo-skill"
|
||||
target = storage.get_skills_root_path() / "custom" / "demo-skill" / "SKILL.md"
|
||||
assert target.exists()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_install_conflict_without_overwrite(
|
||||
storage: LocalSkillStorage, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
):
|
||||
async def _noop_scan(*args, **kwargs): # noqa: ANN001, ARG001
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(
|
||||
"deerflow.skills.installer._scan_skill_archive_contents_or_raise",
|
||||
_noop_scan,
|
||||
)
|
||||
|
||||
archive = _write_archive(tmp_path, _make_skill_zip())
|
||||
await storage.ainstall_skill_from_archive(archive, require_skill_suffix=False)
|
||||
|
||||
with pytest.raises(SkillAlreadyExistsError):
|
||||
await storage.ainstall_skill_from_archive(archive, require_skill_suffix=False)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_install_overwrite_replaces_existing(
|
||||
storage: LocalSkillStorage, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
):
|
||||
async def _noop_scan(*args, **kwargs): # noqa: ANN001, ARG001
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(
|
||||
"deerflow.skills.installer._scan_skill_archive_contents_or_raise",
|
||||
_noop_scan,
|
||||
)
|
||||
|
||||
(tmp_path / "v1").mkdir(exist_ok=True)
|
||||
(tmp_path / "v2").mkdir(exist_ok=True)
|
||||
archive_v1 = _write_archive(tmp_path / "v1", _make_skill_zip(description="version one"))
|
||||
archive_v2 = _write_archive(tmp_path / "v2", _make_skill_zip(description="version two"))
|
||||
|
||||
await storage.ainstall_skill_from_archive(archive_v1, require_skill_suffix=False)
|
||||
await storage.ainstall_skill_from_archive(
|
||||
archive_v2, overwrite=True, require_skill_suffix=False
|
||||
)
|
||||
|
||||
skill_md = (
|
||||
storage.get_skills_root_path() / "custom" / "demo-skill" / "SKILL.md"
|
||||
).read_text(encoding="utf-8")
|
||||
assert "version two" in skill_md
|
||||
assert "version one" not in skill_md
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_install_refuses_to_overwrite_public_skill(
|
||||
storage: LocalSkillStorage, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
):
|
||||
async def _noop_scan(*args, **kwargs): # noqa: ANN001, ARG001
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(
|
||||
"deerflow.skills.installer._scan_skill_archive_contents_or_raise",
|
||||
_noop_scan,
|
||||
)
|
||||
|
||||
public_dir = storage.get_skills_root_path() / "public" / "demo-skill"
|
||||
public_dir.mkdir(parents=True)
|
||||
(public_dir / "SKILL.md").write_text(
|
||||
"---\nname: demo-skill\ndescription: built-in\n---\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
archive = _write_archive(tmp_path, _make_skill_zip())
|
||||
with pytest.raises(SkillAlreadyExistsError, match="public"):
|
||||
await storage.ainstall_skill_from_archive(
|
||||
archive, overwrite=True, require_skill_suffix=False
|
||||
)
|
||||
Reference in new issue
Block a user