"""The skills API payload: the list stays small, one entry carries its document."""
from datetime import datetime, timezone
from navi.api.routes.skills import _payload
from navi.auth import User
from navi.skills.store import SkillEntry
PROTOCOL = """---
kind: protocol
name: Выключение
description: Порядок выключения.
area: infrastructure
triggers: [выключить сервер]
tags: [power]
steps: [Остановить сервисы, Отмонтировать диски]
preconditions: [Все предупреждены]
post_checks: [Юнитов не осталось]
on_deviation: Остановиться и доложить.
on_blocked: Прервать и доложить.
on_already_done: Проверить и пропустить.
---
Порядок зависимостей.
"""
def _entry(content: str = PROTOCOL, **overrides) -> SkillEntry:
base = dict(
id="pr1",
kind="protocol",
name="Выключение",
description="Порядок выключения.",
triggers=["выключить сервер"],
tags=["power"],
area="infrastructure",
visibility="personal",
owner_id="u1",
content=content,
version=1,
content_hash="h",
created_at=datetime.now(timezone.utc),
updated_at=datetime.now(timezone.utc),
updated_by="u1",
)
base.update(overrides)
return SkillEntry(**base)
def _user(role: str = "user") -> User:
return User(id="u1", email="u1@example.com", role=role)
class TestPayload:
def test_one_entry_carries_the_parsed_document(self):
data = _payload(_entry(), _user())
document = data["document"]
assert document["steps"] == ["Остановить сервисы", "Отмонтировать диски"]
assert document["post_checks"] == ["Юнитов не осталось"]
assert document["on_deviation"] == "Остановиться и доложить."
assert document["triggers"] == ["выключить сервер"]
assert document["body"] == "Порядок зависимостей."
def test_the_list_does_not_carry_content_or_document(self):
data = _payload(_entry(), _user(), include_content=False)
assert "content" not in data
assert "document" not in data
def test_a_document_that_no_longer_parses_leaves_the_raw_text(self):
data = _payload(_entry(content="---\nname: без вида\n---\n"), _user())
assert data["document"] is None
assert data["content"].startswith("---")
def test_can_edit_follows_ownership(self):
assert _payload(_entry(), _user())["can_edit"] is True
other = User(id="u2", email="u2@example.com")
assert _payload(_entry(), other)["can_edit"] is False
shared = _entry(visibility="shared", owner_id="")
assert _payload(shared, _user("admin"))["can_edit"] is True