sync update from LAN repo (source master @ c21a2a4a)
This commit is contained in:
1 parent
ea9dcff9c0
commit
66a443e915
140 files changed
+13777
-1040
No files matched your search
@@ -1,9 +1,16 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.dialects import mysql
|
||||
|
||||
from app.gateway.routers.admin_users import _apply_run_scope, _apply_tool_scope
|
||||
from app.gateway.routers.admin_users import (
|
||||
_apply_run_scope,
|
||||
_apply_tool_scope,
|
||||
_query_leaderboard_dimension_details,
|
||||
)
|
||||
from deerflow.persistence.run.model import RunRow
|
||||
from deerflow.persistence.tool_metrics.model import ToolCallMetricRow
|
||||
|
||||
@@ -41,3 +48,83 @@ def test_tool_aggregation_does_not_join_the_full_runs_table() -> None:
|
||||
assert sql.count("NOT (EXISTS") == 2
|
||||
assert "LEFT OUTER JOIN" not in sql
|
||||
assert " NOT IN " not in sql
|
||||
|
||||
|
||||
class _DetailResult:
|
||||
def __init__(self, *, rows=None, row=None):
|
||||
self._rows = rows or []
|
||||
self._row = row
|
||||
|
||||
def all(self):
|
||||
return self._rows
|
||||
|
||||
def one(self):
|
||||
return self._row
|
||||
|
||||
|
||||
class _DetailSession:
|
||||
def __init__(self):
|
||||
self.statements = []
|
||||
self.results = [
|
||||
_DetailResult(rows=[("web-search", 3, 2, 0, 125.5)]),
|
||||
_DetailResult(row=(3, 1, 2, 0, 125.5)),
|
||||
]
|
||||
|
||||
async def execute(self, statement):
|
||||
self.statements.append(statement)
|
||||
return self.results.pop(0)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_skill_detail_uses_metric_table_and_returns_drawer_aggregates() -> None:
|
||||
session = _DetailSession()
|
||||
|
||||
summary, items = await _query_leaderboard_dimension_details(
|
||||
session,
|
||||
datetime(2026, 11, 1, tzinfo=UTC),
|
||||
datetime(2026, 11, 7, tzinfo=UTC),
|
||||
kind="skill",
|
||||
limit=500,
|
||||
exclude_user_ids=set(),
|
||||
include_scheduled=False,
|
||||
include_admins=True,
|
||||
include_failed=True,
|
||||
)
|
||||
|
||||
assert summary.count == 3
|
||||
assert summary.item_count == 1
|
||||
assert items[0].name == "web-search"
|
||||
assert items[0].avg_duration_ms == 125
|
||||
|
||||
sql = "\n".join(_mysql_sql(statement) for statement in session.statements)
|
||||
assert "TOOL_CALL_METRICS" in sql
|
||||
assert "LEFT OUTER JOIN" not in sql
|
||||
assert " NOT IN " not in sql
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_agent_detail_returns_token_aggregates_from_runs() -> None:
|
||||
session = _DetailSession()
|
||||
session.results = [
|
||||
_DetailResult(rows=[("lead_agent", 4, 2, 960, 0)]),
|
||||
_DetailResult(row=(4, 1, 2, 960, 0)),
|
||||
]
|
||||
|
||||
summary, items = await _query_leaderboard_dimension_details(
|
||||
session,
|
||||
datetime(2026, 11, 1, tzinfo=UTC),
|
||||
datetime(2026, 11, 7, tzinfo=UTC),
|
||||
kind="agent",
|
||||
limit=500,
|
||||
exclude_user_ids=set(),
|
||||
include_scheduled=False,
|
||||
include_admins=True,
|
||||
include_failed=True,
|
||||
)
|
||||
|
||||
assert summary.total_tokens == 960
|
||||
assert items[0].avg_duration_ms == 0
|
||||
|
||||
sql = "\n".join(_mysql_sql(statement) for statement in session.statements)
|
||||
assert "FROM RUNS" in sql
|
||||
assert "SCHEDULED_TASK_RUNS" in sql
|
||||
@@ -405,6 +405,23 @@ async def test_skill_catalog_projection_uses_compact_rows_without_long_fields(
|
||||
assert rows[0]["description"] == "compact metadata"
|
||||
assert "detail" not in rows[0]
|
||||
assert "detail" not in {column.key for column in skill_catalog_cache._SKILL_LIST_COLUMNS}
|
||||
|
||||
offline = SimpleNamespace(
|
||||
name="catalog-offline",
|
||||
description="stays untouched",
|
||||
license=None,
|
||||
category="custom",
|
||||
enabled=True,
|
||||
always_on=False,
|
||||
skill_file=skill_file,
|
||||
)
|
||||
assert await skill_catalog_cache.refresh_skill_catalog_projection(skills=[skill, offline]) == 2
|
||||
assert await skill_catalog_cache.set_skill_catalog_enabled("catalog-demo", False) is True
|
||||
assert await skill_catalog_cache.set_skill_catalog_enabled("missing-skill", False) is False
|
||||
rows = await skill_catalog_cache.load_skill_catalog_projection(user_id="user-1", admin=True)
|
||||
by_name = {row["name"]: row for row in rows or []}
|
||||
assert by_name["catalog-demo"]["enabled"] is False
|
||||
assert by_name["catalog-offline"]["enabled"] is True
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ PNG = bytes.fromhex(
|
||||
"89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c4"
|
||||
"890000000a49444154789c63000100000500010d0a2db40000000049454e44ae426082"
|
||||
)
|
||||
SVG = b'<svg xmlns="http://www.w3.org/2000/svg" width="1920" height="64"><rect width="1920" height="64" fill="#8f1d14"/></svg>'
|
||||
|
||||
|
||||
def _store(monkeypatch: pytest.MonkeyPatch, tmp_path):
|
||||
@@ -41,6 +42,10 @@ def _store(monkeypatch: pytest.MonkeyPatch, tmp_path):
|
||||
|
||||
def test_new_settings_leave_app_identity_empty() -> None:
|
||||
assert SystemSettings().app_identity == AppIdentitySettings()
|
||||
assert SystemSettings().app_identity.header_title_font_mode == "adaptive"
|
||||
assert SystemSettings().app_identity.header_title_font_size == 30
|
||||
assert SystemSettings().app_identity.light_red_header_background_filename is None
|
||||
assert SystemSettings().app_identity.light_red_nav_active_filename is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -84,6 +89,58 @@ async def test_admin_saves_name_without_replacing_the_static_logo(
|
||||
assert current["settings"].app_identity.logo_filename is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_admin_saves_header_title_font_settings(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path,
|
||||
) -> None:
|
||||
current = _store(monkeypatch, tmp_path)
|
||||
|
||||
response = await app_identity_router.update_app_identity_settings(
|
||||
SimpleNamespace(),
|
||||
system_name="研判中心",
|
||||
remove_logo=False,
|
||||
logo=None,
|
||||
header_title_font_mode="fixed",
|
||||
header_title_font_size=34,
|
||||
)
|
||||
|
||||
assert response.header_title_font_mode == "fixed"
|
||||
assert response.header_title_font_size == 34
|
||||
assert current["settings"].app_identity.header_title_font_mode == "fixed"
|
||||
assert current["settings"].app_identity.header_title_font_size == 34
|
||||
|
||||
renamed = await app_identity_router.update_app_identity_settings(
|
||||
SimpleNamespace(),
|
||||
system_name="新研判中心",
|
||||
remove_logo=False,
|
||||
logo=None,
|
||||
)
|
||||
|
||||
assert renamed.header_title_font_mode == "fixed"
|
||||
assert renamed.header_title_font_size == 34
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_header_title_fixed_font_size_is_bounded(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path,
|
||||
) -> None:
|
||||
_store(monkeypatch, tmp_path)
|
||||
|
||||
with pytest.raises(HTTPException) as error:
|
||||
await app_identity_router.update_app_identity_settings(
|
||||
SimpleNamespace(),
|
||||
system_name="研判中心",
|
||||
remove_logo=False,
|
||||
logo=None,
|
||||
header_title_font_mode="fixed",
|
||||
header_title_font_size=41,
|
||||
)
|
||||
|
||||
assert error.value.status_code == 422
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_uploaded_logo_is_served_separately_from_generated_pages(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
@@ -126,6 +183,91 @@ async def test_invalid_logo_is_rejected(monkeypatch: pytest.MonkeyPatch, tmp_pat
|
||||
assert error.value.status_code == 422
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_red_navigation_images_can_be_uploaded_and_served(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path,
|
||||
) -> None:
|
||||
current = _store(monkeypatch, tmp_path)
|
||||
|
||||
saved = await app_identity_router.update_app_identity_settings(
|
||||
SimpleNamespace(),
|
||||
system_name="研判中心",
|
||||
remove_logo=False,
|
||||
logo=None,
|
||||
light_red_header_background=UploadFile(
|
||||
file=BytesIO(SVG),
|
||||
filename="header.svg",
|
||||
),
|
||||
light_red_nav_active=UploadFile(
|
||||
file=BytesIO(PNG),
|
||||
filename="active.png",
|
||||
),
|
||||
)
|
||||
|
||||
assert saved.has_light_red_header_background is True
|
||||
assert saved.has_light_red_nav_active is True
|
||||
assert saved.light_red_header_background_url is not None
|
||||
assert saved.light_red_header_background_url.startswith(
|
||||
"/api/public/app-identity/light-red-header-background?v="
|
||||
)
|
||||
assert saved.light_red_nav_active_url is not None
|
||||
assert saved.light_red_nav_active_url.startswith(
|
||||
"/api/public/app-identity/light-red-nav-active?v="
|
||||
)
|
||||
identity = current["settings"].app_identity
|
||||
header_file = tmp_path / "app-identity" / identity.light_red_header_background_filename
|
||||
active_file = tmp_path / "app-identity" / identity.light_red_nav_active_filename
|
||||
assert header_file.is_file()
|
||||
assert header_file.suffix == ".svg"
|
||||
assert active_file.is_file()
|
||||
|
||||
served_header = await app_identity_router.get_light_red_header_background()
|
||||
served_active = await app_identity_router.get_light_red_nav_active()
|
||||
assert served_header.media_type == "image/svg+xml"
|
||||
assert served_header.headers["content-security-policy"].startswith("sandbox;")
|
||||
assert served_active.media_type == "image/png"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_restoring_red_navigation_images_uses_built_in_defaults(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path,
|
||||
) -> None:
|
||||
current = _store(monkeypatch, tmp_path)
|
||||
await app_identity_router.update_app_identity_settings(
|
||||
SimpleNamespace(),
|
||||
system_name="研判中心",
|
||||
remove_logo=False,
|
||||
logo=None,
|
||||
light_red_header_background=UploadFile(
|
||||
file=BytesIO(PNG),
|
||||
filename="header.png",
|
||||
),
|
||||
light_red_nav_active=UploadFile(
|
||||
file=BytesIO(PNG),
|
||||
filename="active.png",
|
||||
),
|
||||
)
|
||||
identity = current["settings"].app_identity
|
||||
header_file = tmp_path / "app-identity" / identity.light_red_header_background_filename
|
||||
active_file = tmp_path / "app-identity" / identity.light_red_nav_active_filename
|
||||
|
||||
restored = await app_identity_router.update_app_identity_settings(
|
||||
SimpleNamespace(),
|
||||
system_name="研判中心",
|
||||
remove_logo=False,
|
||||
logo=None,
|
||||
remove_light_red_header_background=True,
|
||||
remove_light_red_nav_active=True,
|
||||
)
|
||||
|
||||
assert restored.has_light_red_header_background is False
|
||||
assert restored.has_light_red_nav_active is False
|
||||
assert header_file.exists() is False
|
||||
assert active_file.exists() is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_clearing_app_identity_removes_only_its_logo(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
@@ -144,6 +286,8 @@ async def test_clearing_app_identity_removes_only_its_logo(
|
||||
cleared = await app_identity_router.delete_app_identity_settings(SimpleNamespace())
|
||||
|
||||
assert cleared.system_name == ""
|
||||
assert cleared.header_title_font_mode == "adaptive"
|
||||
assert cleared.header_title_font_size == 30
|
||||
assert cleared.has_logo is False
|
||||
assert logo_file.exists() is False
|
||||
with pytest.raises(HTTPException) as error:
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from app.gateway.routers import artifacts
|
||||
from deerflow.html_runtime import offline_bundle
|
||||
from deerflow.html_runtime.offline_bundle import build_html_offline_bundle
|
||||
from deerflow.uploads.policy import default_upload_policy, set_runtime_upload_policy
|
||||
|
||||
|
||||
@@ -91,3 +95,165 @@ async def test_artifact_preview_rejects_binary_content(tmp_path: Path, monkeypat
|
||||
max_chars=1024,
|
||||
)
|
||||
assert raised.value.status_code == 415
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_html_artifact_offline_bundle_uses_zip_export(
|
||||
tmp_path: Path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
source = tmp_path / "page.html"
|
||||
source.write_text("<!doctype html><title>portable</title>", encoding="utf-8")
|
||||
calls: list[Path] = []
|
||||
|
||||
async def resolve(_thread_id: str, _path: str) -> Path:
|
||||
return source
|
||||
|
||||
async def bundle_response(path: Path):
|
||||
calls.append(path)
|
||||
return artifacts.Response(content=b"zip-content", media_type="application/zip")
|
||||
|
||||
monkeypatch.setattr(artifacts, "aresolve_thread_virtual_path", resolve)
|
||||
monkeypatch.setattr(artifacts, "build_html_offline_bundle_response", bundle_response)
|
||||
response = await artifacts.get_artifact.__wrapped__(
|
||||
thread_id="thread-1",
|
||||
path="mnt/user-data/outputs/page.html",
|
||||
request=SimpleNamespace(),
|
||||
download=True,
|
||||
offline_bundle=True,
|
||||
)
|
||||
|
||||
assert response.media_type == "application/zip"
|
||||
assert calls == [source]
|
||||
|
||||
|
||||
def _write_offline_fixture(path: Path, content: str | bytes) -> None:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
if isinstance(content, bytes):
|
||||
path.write_bytes(content)
|
||||
else:
|
||||
path.write_text(content, encoding="utf-8")
|
||||
|
||||
|
||||
def _offline_runtime_fixture(root: Path) -> Path:
|
||||
runtime = root / "runtime"
|
||||
_write_offline_fixture(runtime / "echarts.min.js", "window.echarts={};")
|
||||
_write_offline_fixture(runtime / "deerflow-runtime.js", "window.__runtimeLoaded=true;")
|
||||
_write_offline_fixture(runtime / "brand-logo-dark.png", b"fallback-logo")
|
||||
_write_offline_fixture(runtime / "LICENSE.echarts.txt", "ECharts license")
|
||||
for name in (
|
||||
"china-provinces.geojson",
|
||||
"japan-prefectures.geojson",
|
||||
"taiwan-counties.geojson",
|
||||
"us-states.geojson",
|
||||
):
|
||||
_write_offline_fixture(runtime / "maps" / name, '{"type":"FeatureCollection","features":[]}')
|
||||
return runtime
|
||||
|
||||
|
||||
def test_html_offline_bundle_rewrites_and_packages_gateway_runtime(
|
||||
tmp_path: Path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
runtime = _offline_runtime_fixture(tmp_path)
|
||||
logo = tmp_path / "branding" / "logo.webp"
|
||||
_write_offline_fixture(logo, b"configured-logo")
|
||||
uploaded_image = tmp_path / "uploads" / "background.png"
|
||||
_write_offline_fixture(uploaded_image, b"uploaded-image")
|
||||
monkeypatch.setattr(
|
||||
offline_bundle,
|
||||
"verify_html_image_token",
|
||||
lambda _token: ("thread-1", "uploads", "background.png"),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
offline_bundle,
|
||||
"resolve_html_image_path",
|
||||
lambda _thread_id, _kind, _filename: uploaded_image,
|
||||
)
|
||||
source = tmp_path / "态势报告.html"
|
||||
source.write_text(
|
||||
"""<!doctype html><html><head>
|
||||
<script src="http://gateway.local/prefix/api/public/html-runtime/echarts.min.js"></script>
|
||||
<script src="http://gateway.local/prefix/api/public/html-runtime/deerflow-runtime.js"></script>
|
||||
<style>.brand{background:url('http://gateway.local/prefix/api/public/html-runtime/generated-page-logo?v=2')}</style>
|
||||
</head><body><script type="application/json" data-df-geo="map">{"map":"china-provinces"}</script>
|
||||
<script type="application/json" data-df-map="tile-map">{"markers":[]}</script>
|
||||
<img src="http://gateway.local/prefix/api/public/html-images/signed.token">
|
||||
<a href="https://example.com/source">来源</a></body></html>""",
|
||||
encoding="utf-8",
|
||||
)
|
||||
destination = tmp_path / "bundle.zip"
|
||||
|
||||
result = build_html_offline_bundle(
|
||||
source,
|
||||
destination,
|
||||
runtime_base_url="http://gateway.local/prefix/api/public/html-runtime",
|
||||
map_tile_url="https://tiles.example/{z}/{x}/{y}.png",
|
||||
generated_logo_path=logo,
|
||||
runtime_dir=runtime,
|
||||
)
|
||||
|
||||
assert destination.is_file()
|
||||
assert "assets/html-runtime/echarts.min.js" in result.bundled_assets
|
||||
assert "https://example.com/source" in result.external_urls
|
||||
assert "https://tiles.example/{z}/{x}/{y}.png" in result.external_urls
|
||||
with zipfile.ZipFile(destination) as archive:
|
||||
names = set(archive.namelist())
|
||||
assert "index.html" in names
|
||||
assert "README.txt" in names
|
||||
assert "manifest.json" in names
|
||||
assert "assets/html-runtime/echarts.min.js" in names
|
||||
assert "assets/html-runtime/deerflow-runtime.js" in names
|
||||
assert "assets/html-runtime/generated-page-logo.webp" in names
|
||||
assert "assets/html-runtime/maps/china-provinces.geojson" in names
|
||||
assert any(name.startswith("assets/html-images/") for name in names)
|
||||
assert "THIRD_PARTY_LICENSES/LICENSE.echarts.txt" in names
|
||||
|
||||
html = archive.read("index.html").decode("utf-8")
|
||||
assert "gateway.local" not in html
|
||||
assert 'src="assets/html-runtime/echarts.min.js"' in html
|
||||
assert "assets/html-runtime/generated-page-logo.webp" in html
|
||||
assert "/api/public/html-images/" not in html
|
||||
|
||||
runtime_script = archive.read("assets/html-runtime/deerflow-runtime.js").decode("utf-8")
|
||||
assert "DEERFLOW_HTML_RUNTIME_CONFIG" in runtime_script
|
||||
assert "assets/html-runtime/maps/china-provinces.geojson" in runtime_script
|
||||
assert "window.__runtimeLoaded=true" in runtime_script
|
||||
|
||||
manifest = json.loads(archive.read("manifest.json"))
|
||||
assert manifest["entry"] == "index.html"
|
||||
assert manifest["source_name"] == "态势报告.html"
|
||||
assert manifest["external_urls"] == [
|
||||
"https://example.com/source",
|
||||
"https://tiles.example/{z}/{x}/{y}.png",
|
||||
]
|
||||
|
||||
|
||||
def test_html_offline_bundle_copies_cognitive_runtime_tree(tmp_path: Path) -> None:
|
||||
runtime = _offline_runtime_fixture(tmp_path)
|
||||
_write_offline_fixture(runtime / "cognitive-situation-map" / "app.js", "window.cognitive=true;")
|
||||
_write_offline_fixture(runtime / "cognitive-situation-map" / "styles.css", "body{color:white}")
|
||||
_write_offline_fixture(runtime / "cognitive-situation-map" / "assets" / "maps" / "taiwan.png", b"map")
|
||||
source = tmp_path / "认知态势图.html"
|
||||
source.write_text(
|
||||
"""<script>window.__DF_SKILL_BASE__="http://gw/api/public/html-runtime/cognitive-situation-map";</script>
|
||||
<link rel="stylesheet" href="http://gw/api/public/html-runtime/cognitive-situation-map/styles.css">
|
||||
<script src="http://gw/api/public/html-runtime/cognitive-situation-map/app.js"></script>""",
|
||||
encoding="utf-8",
|
||||
)
|
||||
destination = tmp_path / "cognitive.zip"
|
||||
|
||||
build_html_offline_bundle(
|
||||
source,
|
||||
destination,
|
||||
runtime_base_url="http://gw/api/public/html-runtime",
|
||||
map_tile_url="https://tiles.example/{z}/{x}/{y}.png",
|
||||
runtime_dir=runtime,
|
||||
)
|
||||
|
||||
with zipfile.ZipFile(destination) as archive:
|
||||
html = archive.read("index.html").decode("utf-8")
|
||||
assert "http://gw" not in html
|
||||
assert "assets/html-runtime/cognitive-situation-map" in html
|
||||
assert "assets/html-runtime/cognitive-situation-map/app.js" in archive.namelist()
|
||||
assert "assets/html-runtime/cognitive-situation-map/assets/maps/taiwan.png" in archive.namelist()
|
||||
@@ -139,8 +139,12 @@ async def test_html_edit_invalidates_metadata_content_and_list(cached_request, m
|
||||
request.state.user.system_role = "admin"
|
||||
state = {"html": "old"}
|
||||
monkeypatch.setattr(html_templates.template_store, "read_template_html", lambda _id: state["html"])
|
||||
# content 端点默认走引擎的「完整文档」读取(结构化模板会组合默认数据)。
|
||||
monkeypatch.setattr(html_templates.template_fill, "read_template_text", lambda _id: state["html"])
|
||||
monkeypatch.setattr(html_templates.template_store, "list_templates", lambda: [{"name": state["html"]}])
|
||||
monkeypatch.setattr(html_templates.template_store, "get_template_meta", lambda _id: {"name": state["html"]})
|
||||
monkeypatch.setattr(
|
||||
html_templates.template_store, "get_template_meta", lambda _id: {"id": _id, "name": state["html"]}
|
||||
)
|
||||
|
||||
def update(_id, **kwargs):
|
||||
state["html"] = kwargs["html"]
|
||||
|
||||
@@ -355,10 +355,7 @@ async def test_weknora_session_endpoint_sets_cookie_and_returns_platform_path(mo
|
||||
return {"id": "local-kb", "weknora_id": "remote-kb"}
|
||||
|
||||
async def list_visible(self, *_: object, **__: object) -> list[dict[str, str]]:
|
||||
return [
|
||||
{"id": "local-kb", "weknora_id": "remote-kb"},
|
||||
{"id": "public-kb", "weknora_id": "other-public-kb"},
|
||||
]
|
||||
raise AssertionError("creating an iframe session must not load every visible knowledge base")
|
||||
|
||||
monkeypatch.setattr(llmwiki_router, "_runtime_for_iframe_proxy", lambda _: object())
|
||||
monkeypatch.setattr(llmwiki_router, "_authorized_mapping", authorized_mapping)
|
||||
@@ -387,7 +384,39 @@ async def test_weknora_session_endpoint_sets_cookie_and_returns_platform_path(mo
|
||||
assert ctx.mapping_id == "local-kb"
|
||||
assert ctx.weknora_id == "remote-kb"
|
||||
assert ctx.can_write is True
|
||||
assert ctx.allowed_weknora_ids == ("remote-kb", "other-public-kb")
|
||||
assert ctx.allowed_weknora_ids == ()
|
||||
assert len(raw) < 1024
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_weknora_embed_picker_hydrates_visible_bases_server_side(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
from app.gateway.routers import llmwiki as llmwiki_router
|
||||
|
||||
calls: list[dict[str, object]] = []
|
||||
|
||||
class Store:
|
||||
async def list_visible(self, user_id: str, *, scope: str, is_admin: bool) -> list[dict[str, str]]:
|
||||
calls.append({"user_id": user_id, "scope": scope, "is_admin": is_admin})
|
||||
return [
|
||||
{"id": "local-kb", "weknora_id": "remote-kb"},
|
||||
{"id": "public-kb", "weknora_id": "other-public-kb"},
|
||||
]
|
||||
|
||||
monkeypatch.setattr(llmwiki_router, "_store", lambda _: Store())
|
||||
ctx = llmwiki_router.WeKnoraEmbedContext(
|
||||
mapping_id="local-kb",
|
||||
weknora_id="remote-kb",
|
||||
user_id="user-a",
|
||||
is_admin=False,
|
||||
can_write=True,
|
||||
exp=4_102_444_800,
|
||||
)
|
||||
|
||||
hydrated = await llmwiki_router._with_visible_embed_knowledge_bases(_request("/platform/knowledge-bases/remote-kb"), ctx)
|
||||
|
||||
assert calls == [{"user_id": "user-a", "scope": "all", "is_admin": False}]
|
||||
assert hydrated.allowed_weknora_ids == ("remote-kb", "other-public-kb")
|
||||
assert "allowed_weknora_ids" not in llmwiki_router._embed_context_payload(hydrated)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -2079,6 +2108,9 @@ async def test_prefixed_weknora_proxy_serves_page_static_assets_and_api_without_
|
||||
async def get_authorized(self, *_: object, **__: object) -> dict[str, str]:
|
||||
return {"id": "local-kb", "weknora_id": "remote-kb"}
|
||||
|
||||
async def list_visible(self, *_: object, **__: object) -> list[dict[str, str]]:
|
||||
return [{"id": "local-kb", "weknora_id": "remote-kb"}]
|
||||
|
||||
runtime = SimpleNamespace(
|
||||
web_base_url="http://weknora-web.test",
|
||||
api_base_url="http://weknora-api.test",
|
||||
@@ -2207,9 +2239,28 @@ async def test_prefixed_weknora_proxy_serves_page_static_assets_and_api_without_
|
||||
api = await client.get("/deerflow/api/llmwiki/weknora-embed/api/v1/auth/me")
|
||||
assert api.status_code == 200
|
||||
|
||||
directory = await client.get("/deerflow/api/llmwiki/weknora-embed/api/v1/knowledge-bases")
|
||||
assert directory.status_code == 200
|
||||
assert directory.json() == {
|
||||
"success": True,
|
||||
"data": [
|
||||
{
|
||||
"id": "remote-kb",
|
||||
"name": "",
|
||||
"description": "",
|
||||
"type": "document",
|
||||
"status": "active",
|
||||
"created_at": None,
|
||||
"updated_at": None,
|
||||
}
|
||||
],
|
||||
"total": 1,
|
||||
}
|
||||
|
||||
api_request = next(request for request in upstream_requests if request[1].startswith(runtime.api_base_url))
|
||||
assert api_request[2]["Authorization"] == "Bearer admin-token"
|
||||
assert api_request[2]["X-Tenant-ID"] == "tenant-1"
|
||||
assert not any(request[1].endswith("/api/v1/knowledge-bases") for request in upstream_requests)
|
||||
|
||||
|
||||
def test_weknora_iframe_hides_current_outer_sidebar_shell() -> None:
|
||||
@@ -2334,7 +2385,6 @@ async def test_weknora_iframe_switch_rechecks_selected_mapping_permissions(
|
||||
is_admin=False,
|
||||
can_write=True,
|
||||
exp=4_102_444_800,
|
||||
allowed_weknora_ids=("remote-kb", "other-visible-kb"),
|
||||
)
|
||||
|
||||
selected = await llmwiki_router._switch_embed_context(
|
||||
@@ -2382,12 +2432,16 @@ async def test_weknora_platform_proxy_reissues_cookie_for_native_kb_switch(
|
||||
seen["target"] = weknora_id
|
||||
return selected
|
||||
|
||||
async def hydrate_visible_bases(_: Request, ctx: object) -> object:
|
||||
return ctx
|
||||
|
||||
async def proxy_request(*_: object, **kwargs: object) -> Response:
|
||||
seen.update(kwargs)
|
||||
return Response("ok")
|
||||
|
||||
monkeypatch.setattr(llmwiki_router, "_read_embed_context", read_context)
|
||||
monkeypatch.setattr(llmwiki_router, "_switch_embed_context", switch_context)
|
||||
monkeypatch.setattr(llmwiki_router, "_with_visible_embed_knowledge_bases", hydrate_visible_bases)
|
||||
monkeypatch.setattr(
|
||||
llmwiki_router,
|
||||
"_runtime_for_iframe_proxy",
|
||||
|
||||
@@ -40,7 +40,7 @@ def test_load_returns_defaults_when_missing(tmp_runtime_home: Path) -> None:
|
||||
assert settings.conversation_context.recent_turn_window_enabled is False
|
||||
assert settings.conversation_context.max_conversation_rounds == 20
|
||||
assert settings.write_file_compatibility.enabled is False
|
||||
assert settings.conversation_context.recent_turn_window_enabled is False
|
||||
assert settings.call_metrics.enabled is True
|
||||
|
||||
|
||||
def test_save_then_load_roundtrips(tmp_runtime_home: Path) -> None:
|
||||
@@ -325,6 +325,28 @@ async def test_admin_can_toggle_write_file_compatibility_without_restart(
|
||||
assert ss.get_write_file_compatibility_settings().enabled is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_admin_can_toggle_call_metrics_without_restart(
|
||||
tmp_runtime_home: Path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
_patch_admin(monkeypatch)
|
||||
|
||||
disabled = await ss_router.update_call_metrics_setting(
|
||||
ss_router.CallMetricsUpdateRequest(enabled=False),
|
||||
SimpleNamespace(),
|
||||
)
|
||||
assert disabled.enabled is False
|
||||
assert ss.get_call_metrics_settings().enabled is False
|
||||
|
||||
enabled = await ss_router.update_call_metrics_setting(
|
||||
ss_router.CallMetricsUpdateRequest(enabled=True),
|
||||
SimpleNamespace(),
|
||||
)
|
||||
assert enabled.enabled is True
|
||||
assert ss.get_call_metrics_settings().enabled is True
|
||||
|
||||
|
||||
def test_prepend_prefix_tags_first_human_without_touching_content() -> None:
|
||||
graph_input = {"messages": [HumanMessage(content="评价一下中美贸易")]}
|
||||
out = _prepend_prompt_prefix(graph_input, "你需要站在中方角度")
|
||||
|
||||
@@ -69,6 +69,7 @@ async def _admit(repo: RoundtableRoomRepository, draft_id: str, user_id: str, **
|
||||
room_session_id=kwargs.get("room_session_id"),
|
||||
enabled=kwargs.get("enabled", True),
|
||||
max_rooms=kwargs.get("max_rooms", 3),
|
||||
lease_seconds=kwargs.get("lease_seconds", 5 * 60),
|
||||
extra_keys=kwargs.get("extra_keys"),
|
||||
)
|
||||
|
||||
@@ -257,6 +258,44 @@ async def test_delayed_heartbeat_cannot_resurrect_a_released_room(rooms) -> None
|
||||
assert next_room["ok"] is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_foreground_lease_defaults_to_five_minutes_and_honors_override(rooms) -> None:
|
||||
repo, factory = rooms
|
||||
before = datetime.now(UTC)
|
||||
admitted = await _admit(
|
||||
repo,
|
||||
"draft-lease",
|
||||
"user-a",
|
||||
room_session_id="lease-session",
|
||||
)
|
||||
assert admitted["ok"] is True
|
||||
key = compute_room_session_key("lease-session")
|
||||
assert key is not None
|
||||
async with factory() as session:
|
||||
row = await session.get(RoundtableRoomLeaseRow, key)
|
||||
assert row is not None and row.lease_until is not None
|
||||
until = row.lease_until if row.lease_until.tzinfo else row.lease_until.replace(tzinfo=UTC)
|
||||
remaining = until - before
|
||||
assert timedelta(minutes=4, seconds=50) <= remaining <= timedelta(minutes=5, seconds=5)
|
||||
|
||||
renewed = await repo.renew(
|
||||
draft_id="draft-lease",
|
||||
user_id="user-a",
|
||||
task_id=None,
|
||||
room_session_id="lease-session",
|
||||
enabled=True,
|
||||
max_rooms=3,
|
||||
lease_seconds=120,
|
||||
)
|
||||
assert renewed["ok"] is True
|
||||
async with factory() as session:
|
||||
row = await session.get(RoundtableRoomLeaseRow, key)
|
||||
assert row is not None and row.lease_until is not None
|
||||
until = row.lease_until if row.lease_until.tzinfo else row.lease_until.replace(tzinfo=UTC)
|
||||
renewed_for = until - datetime.now(UTC)
|
||||
assert timedelta(seconds=90) <= renewed_for <= timedelta(seconds=125)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stale_run_for_leased_draft_does_not_consume_a_second_room(rooms) -> None:
|
||||
"""Run-manager fallback must not double-count the authoritative lease."""
|
||||
@@ -744,6 +783,7 @@ async def test_admin_can_change_roundtable_room_limit_without_restart(
|
||||
assert ss.get_roundtable_room_limit_settings().enabled is True
|
||||
assert ss.get_roundtable_room_limit_settings().max_rooms == 3
|
||||
assert ss.get_roundtable_room_limit_settings().max_hold_minutes == 30
|
||||
assert ss.get_roundtable_room_limit_settings().lease_minutes == 5
|
||||
assert ss.get_roundtable_room_limit_settings().max_cycles == 8
|
||||
assert ss.get_roundtable_room_limit_settings().write_file_compatibility_enabled is False
|
||||
|
||||
@@ -752,6 +792,7 @@ async def test_admin_can_change_roundtable_room_limit_without_restart(
|
||||
enabled=False,
|
||||
max_rooms=5,
|
||||
max_hold_minutes=45,
|
||||
lease_minutes=8,
|
||||
max_cycles=12,
|
||||
),
|
||||
SimpleNamespace(),
|
||||
@@ -759,9 +800,11 @@ async def test_admin_can_change_roundtable_room_limit_without_restart(
|
||||
assert saved.enabled is False
|
||||
assert saved.max_rooms == 5
|
||||
assert saved.max_hold_minutes == 45
|
||||
assert saved.lease_minutes == 8
|
||||
assert saved.max_cycles == 12
|
||||
assert ss.load_system_settings().roundtable_room_limit.max_rooms == 5
|
||||
assert ss.load_system_settings().roundtable_room_limit.max_hold_minutes == 45
|
||||
assert ss.load_system_settings().roundtable_room_limit.lease_minutes == 8
|
||||
assert ss.load_system_settings().roundtable_room_limit.max_cycles == 12
|
||||
assert saved.write_file_compatibility_enabled is False
|
||||
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from fastapi import UploadFile
|
||||
|
||||
from deerflow.skills.storage import LocalSkillStorage
|
||||
from deerflow.skills.types import SkillCategory
|
||||
|
||||
|
||||
def _skill_markdown(version: str) -> str:
|
||||
return f"""---
|
||||
name: demo-skill
|
||||
description: Demo custom skill
|
||||
---
|
||||
|
||||
# Demo {version}
|
||||
"""
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def versioned_skill(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
||||
from deerflow.skills import versioning
|
||||
|
||||
skills_root = tmp_path / "skills"
|
||||
skill_dir = skills_root / "custom" / "demo-skill"
|
||||
skill_dir.mkdir(parents=True)
|
||||
(skill_dir / "SKILL.md").write_text(_skill_markdown("v1"), encoding="utf-8")
|
||||
(skill_dir / "assets").mkdir()
|
||||
(skill_dir / "assets" / "payload.txt").write_text("asset-v1", encoding="utf-8")
|
||||
storage = LocalSkillStorage(host_path=str(skills_root))
|
||||
enabled = SimpleNamespace(enabled=True)
|
||||
monkeypatch.setattr(
|
||||
versioning,
|
||||
"load_system_settings",
|
||||
lambda: SimpleNamespace(skill_versioning=enabled),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
versioning,
|
||||
"get_paths",
|
||||
lambda: SimpleNamespace(base_dir=tmp_path / "runtime"),
|
||||
)
|
||||
return versioning, storage, skill_dir, enabled, tmp_path
|
||||
|
||||
|
||||
def test_versioning_is_noop_while_switch_is_disabled(versioned_skill) -> None:
|
||||
versioning, storage, _skill_dir, enabled, tmp_path = versioned_skill
|
||||
enabled.enabled = False
|
||||
|
||||
assert versioning.record_custom_skill_version("demo-skill", storage=storage, action="upload") is None
|
||||
assert not (tmp_path / "runtime" / "skill-versions").exists()
|
||||
|
||||
|
||||
def test_complete_package_versions_switch_without_resetting_usage(versioned_skill, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
versioning, storage, skill_dir, _enabled, tmp_path = versioned_skill
|
||||
import deerflow.skills.storage as storage_package
|
||||
from deerflow.skills import usage
|
||||
|
||||
monkeypatch.setattr(storage_package, "get_or_new_skill_storage", lambda: storage)
|
||||
monkeypatch.setattr(usage, "_schedule_use_projection_increment", lambda *args: None)
|
||||
|
||||
first = versioning.record_custom_skill_version("demo-skill", storage=storage, action="upload")
|
||||
assert first and first["version"] == 1
|
||||
assert usage.bump_use("demo-skill") is True
|
||||
assert usage.bump_use("demo-skill") is True
|
||||
|
||||
(skill_dir / "SKILL.md").write_text(_skill_markdown("v2"), encoding="utf-8")
|
||||
(skill_dir / "assets" / "payload.txt").write_text("asset-v2", encoding="utf-8")
|
||||
second = versioning.record_custom_skill_version("demo-skill", storage=storage, action="upload_iteration")
|
||||
assert second and second["version"] == 2
|
||||
|
||||
listed = versioning.list_custom_skill_versions("demo-skill", storage=storage)
|
||||
assert listed["active_version"] == 2
|
||||
assert [item["version"] for item in listed["versions"]] == [2, 1]
|
||||
|
||||
versioning.activate_custom_skill_version("demo-skill", 1, storage=storage)
|
||||
assert "Demo v1" in (skill_dir / "SKILL.md").read_text(encoding="utf-8")
|
||||
assert (skill_dir / "assets" / "payload.txt").read_text(encoding="utf-8") == "asset-v1"
|
||||
assert usage.get_entry("demo-skill")["use_count"] == 2
|
||||
|
||||
snapshot_root = tmp_path / "runtime" / "skill-versions" / "demo-skill"
|
||||
assert snapshot_root.is_dir()
|
||||
assert not snapshot_root.is_relative_to(skills_root := storage.get_skills_root_path())
|
||||
assert [skill.name for skill in storage.load_skills(enabled_only=False)] == ["demo-skill"]
|
||||
assert skills_root == tmp_path / "skills"
|
||||
|
||||
|
||||
def test_direct_overwrite_resets_version_chain(versioned_skill) -> None:
|
||||
versioning, storage, skill_dir, _enabled, _tmp_path = versioned_skill
|
||||
versioning.record_custom_skill_version("demo-skill", storage=storage, action="upload")
|
||||
(skill_dir / "SKILL.md").write_text(_skill_markdown("v2"), encoding="utf-8")
|
||||
versioning.record_custom_skill_version("demo-skill", storage=storage, action="upload_iteration")
|
||||
|
||||
(skill_dir / "SKILL.md").write_text(_skill_markdown("replacement"), encoding="utf-8")
|
||||
reset = versioning.reset_custom_skill_versions("demo-skill", storage=storage, summary="direct overwrite")
|
||||
|
||||
assert reset and reset["version"] == 1
|
||||
listed = versioning.list_custom_skill_versions("demo-skill", storage=storage)
|
||||
assert listed["active_version"] == 1
|
||||
assert [item["version"] for item in listed["versions"]] == [1]
|
||||
assert "replacement" in (skill_dir / "SKILL.md").read_text(encoding="utf-8")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("install_mode", "expected_action"),
|
||||
[("iterate", "upload_iteration"), ("overwrite", "reset")],
|
||||
)
|
||||
async def test_same_name_upload_supports_iteration_or_direct_overwrite(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
install_mode: str,
|
||||
expected_action: str,
|
||||
) -> None:
|
||||
from app.gateway.routers import skills as skills_router
|
||||
from deerflow.skills import usage, versioning
|
||||
|
||||
class FakeStorage:
|
||||
def invalidate_skills_cache(self):
|
||||
return None
|
||||
|
||||
def validate_skill_archive(self, *_args, **_kwargs):
|
||||
return {
|
||||
"skill_name": "demo-skill",
|
||||
"conflict_kind": "custom",
|
||||
}
|
||||
|
||||
async def ainstall_skill_from_archive(self, *_args, **kwargs):
|
||||
assert kwargs["overwrite"] is True
|
||||
assert kwargs["target_category"] == SkillCategory.CUSTOM
|
||||
return {
|
||||
"success": True,
|
||||
"skill_name": "demo-skill",
|
||||
"message": "installed",
|
||||
"overwritten": True,
|
||||
"category": "custom",
|
||||
}
|
||||
|
||||
class FakeSkillStore:
|
||||
async def ensure_legacy(self, payload):
|
||||
assert payload["name"] == "demo-skill"
|
||||
return payload
|
||||
|
||||
storage = FakeStorage()
|
||||
calls: list[str] = []
|
||||
|
||||
async def preview_target(**_kwargs):
|
||||
return False, True, True, False, False, "user-1", "owner", "conflict"
|
||||
|
||||
async def no_op(*_args, **_kwargs):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(skills_router, "get_or_new_skill_storage", lambda **_kwargs: storage)
|
||||
monkeypatch.setattr(skills_router, "_preview_install_target", preview_target)
|
||||
monkeypatch.setattr(skills_router, "_current_user_id", lambda _request: "user-1")
|
||||
monkeypatch.setattr(skills_router, "_is_admin_user", lambda _request: False)
|
||||
monkeypatch.setattr(skills_router, "_refresh_catalog_snapshot", no_op)
|
||||
monkeypatch.setattr(skills_router, "refresh_skills_system_prompt_cache_async", no_op)
|
||||
monkeypatch.setattr(skills_router, "_sync_skill_view", no_op)
|
||||
monkeypatch.setattr(usage, "mark_uploaded", lambda _name: None)
|
||||
monkeypatch.setattr(versioning, "skill_versioning_enabled", lambda: True)
|
||||
monkeypatch.setattr(
|
||||
versioning,
|
||||
"record_custom_skill_version",
|
||||
lambda _name, **kwargs: calls.append(kwargs["action"]),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
versioning,
|
||||
"reset_custom_skill_versions",
|
||||
lambda _name, **_kwargs: calls.append("reset"),
|
||||
)
|
||||
|
||||
result = await skills_router.install_skill_upload(
|
||||
request=SimpleNamespace(),
|
||||
file=UploadFile(filename="demo.skill", file=io.BytesIO(b"PK\x03\x04archive")),
|
||||
overwrite=False,
|
||||
install_mode=install_mode,
|
||||
target_category=SkillCategory.CUSTOM,
|
||||
config=SimpleNamespace(),
|
||||
skill_store=FakeSkillStore(),
|
||||
)
|
||||
|
||||
assert result.overwritten is True
|
||||
assert expected_action in calls
|
||||
if install_mode == "iterate":
|
||||
assert calls == ["baseline", "upload_iteration"]
|
||||
else:
|
||||
assert calls == ["reset"]
|
||||
@@ -221,11 +221,22 @@ async def test_update_public_skill_is_admin_only_and_preserves_raw_config(
|
||||
monkeypatch.setattr(skills_router, "get_or_new_skill_storage", lambda **_kwargs: storage)
|
||||
|
||||
refresh_calls: list[bool] = []
|
||||
catalog_refresh_calls: list[object] = []
|
||||
enabled_row_updates: list[tuple[str, bool]] = []
|
||||
|
||||
async def _refresh() -> None:
|
||||
refresh_calls.append(True)
|
||||
|
||||
async def _refresh_catalog(config) -> None:
|
||||
catalog_refresh_calls.append(config)
|
||||
|
||||
async def _set_enabled(name: str, enabled: bool) -> bool:
|
||||
enabled_row_updates.append((name, enabled))
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(skills_router, "refresh_skills_system_prompt_cache_async", _refresh)
|
||||
monkeypatch.setattr(skills_router, "refresh_skill_catalog_projection", _refresh_catalog)
|
||||
monkeypatch.setattr(skills_router, "set_skill_catalog_enabled", _set_enabled)
|
||||
replace_calls: list[tuple[object, object]] = []
|
||||
real_replace = os.replace
|
||||
|
||||
@@ -246,6 +257,7 @@ async def test_update_public_skill_is_admin_only_and_preserves_raw_config(
|
||||
assert forbidden.value.status_code == 403
|
||||
assert config_path.read_bytes() == before
|
||||
assert refresh_calls == []
|
||||
assert enabled_row_updates == []
|
||||
|
||||
response = await skills_router.update_skill(
|
||||
"demo-skill",
|
||||
@@ -264,6 +276,8 @@ async def test_update_public_skill_is_admin_only_and_preserves_raw_config(
|
||||
assert persisted["mcpServers"]["preserved"]["command"] == "$MCP_COMMAND"
|
||||
assert get_extensions_config().skills["demo-skill"].enabled is False
|
||||
assert refresh_calls == [True]
|
||||
assert enabled_row_updates == [("demo-skill", False)]
|
||||
assert catalog_refresh_calls == []
|
||||
config_replaces = [item for item in replace_calls if os.fspath(item[1]) == os.fspath(config_path)]
|
||||
assert len(config_replaces) == 1
|
||||
|
||||
@@ -282,6 +296,8 @@ async def test_update_public_skill_is_admin_only_and_preserves_raw_config(
|
||||
assert persisted["skills"]["demo-skill"]["enabled"] is True
|
||||
assert persisted["concurrentUnknownField"] == {"survives": True}
|
||||
assert refresh_calls == [True, True]
|
||||
assert enabled_row_updates == [("demo-skill", False), ("demo-skill", True)]
|
||||
assert catalog_refresh_calls == []
|
||||
config_replaces = [item for item in replace_calls if os.fspath(item[1]) == os.fspath(config_path)]
|
||||
assert len(config_replaces) == 2
|
||||
|
||||
|
||||
@@ -4,7 +4,15 @@ import pytest
|
||||
from fastapi import HTTPException
|
||||
|
||||
from app.gateway.routers import system_settings as system_settings_router
|
||||
from deerflow.config.system_settings import SystemSettings
|
||||
from deerflow.config.system_settings import (
|
||||
AIWritingGuideCard,
|
||||
AIWritingPageDescription,
|
||||
GeneralQaPageDescription,
|
||||
GeneralQaPageDescriptions,
|
||||
PageDescriptionsSettings,
|
||||
PageHeaderDescription,
|
||||
SystemSettings,
|
||||
)
|
||||
|
||||
|
||||
def test_strategy_project_demo_is_hidden_by_default() -> None:
|
||||
@@ -15,6 +23,10 @@ def test_assistant_knowledge_is_hidden_by_default() -> None:
|
||||
assert SystemSettings().knowledge_base_product_version == "v1"
|
||||
|
||||
|
||||
def test_custom_skill_versioning_is_disabled_by_default() -> None:
|
||||
assert SystemSettings().skill_versioning.enabled is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_non_admin_cannot_update_strategy_project_settings(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
@@ -194,6 +206,263 @@ def test_retry_keep_partial_defaults_to_enabled() -> None:
|
||||
assert SystemSettings().retry_keep_partial.enabled is True
|
||||
|
||||
|
||||
def test_page_descriptions_default_to_product_copy() -> None:
|
||||
descriptions = SystemSettings().page_descriptions
|
||||
assert descriptions.general_qa.full == GeneralQaPageDescription()
|
||||
assert descriptions.general_qa.compact == GeneralQaPageDescription()
|
||||
assert descriptions.knowledge_base == PageHeaderDescription()
|
||||
assert descriptions.ai_writing == AIWritingPageDescription()
|
||||
assert len(descriptions.ai_writing.guide_cards) == 3
|
||||
assert descriptions.strategy_projects == PageHeaderDescription()
|
||||
assert descriptions.deep_research == PageHeaderDescription()
|
||||
assert descriptions.gongwen == PageHeaderDescription()
|
||||
assert descriptions.light_app_management == PageHeaderDescription()
|
||||
assert descriptions.light_app_list == PageHeaderDescription()
|
||||
assert descriptions.agents == PageHeaderDescription()
|
||||
assert descriptions.business_chains == PageHeaderDescription()
|
||||
assert descriptions.skills == PageHeaderDescription()
|
||||
assert descriptions.knowledge_management == PageHeaderDescription()
|
||||
|
||||
|
||||
def test_legacy_general_qa_description_is_shared_by_both_layouts() -> None:
|
||||
descriptions = PageDescriptionsSettings.model_validate(
|
||||
{
|
||||
"general_qa": {
|
||||
"welcome_title": " 原有问答欢迎语 ",
|
||||
"welcome_description": " 原有说明 ",
|
||||
"input_placeholder": " 原有提示 ",
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
expected = GeneralQaPageDescription(
|
||||
welcome_title="原有问答欢迎语",
|
||||
welcome_description="原有说明",
|
||||
input_placeholder="原有提示",
|
||||
)
|
||||
assert descriptions.general_qa.full == expected
|
||||
assert descriptions.general_qa.compact == expected
|
||||
|
||||
legacy_request = system_settings_router.PageDescriptionsUpdateRequest(
|
||||
general_qa=GeneralQaPageDescription(welcome_title="旧版请求"),
|
||||
)
|
||||
assert legacy_request.general_qa == GeneralQaPageDescriptions(
|
||||
full=GeneralQaPageDescription(welcome_title="旧版请求"),
|
||||
compact=GeneralQaPageDescription(welcome_title="旧版请求"),
|
||||
)
|
||||
|
||||
|
||||
def test_ai_writing_guide_cards_are_padded_to_three_items() -> None:
|
||||
descriptions = PageDescriptionsSettings.model_validate(
|
||||
{
|
||||
"ai_writing": {
|
||||
"guide_cards": [
|
||||
{"title": " 自定义卡片 ", "description": " 自定义说明 "},
|
||||
]
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
assert descriptions.ai_writing.guide_cards == [
|
||||
AIWritingGuideCard(title="自定义卡片", description="自定义说明"),
|
||||
AIWritingGuideCard(),
|
||||
AIWritingGuideCard(),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_non_admin_cannot_update_page_descriptions(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
async def ordinary_user(_request):
|
||||
return SimpleNamespace(system_role="user")
|
||||
|
||||
monkeypatch.setattr(
|
||||
system_settings_router,
|
||||
"get_optional_user_from_request",
|
||||
ordinary_user,
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as error:
|
||||
await system_settings_router.update_page_descriptions(
|
||||
system_settings_router.PageDescriptionsUpdateRequest(
|
||||
general_qa=GeneralQaPageDescriptions(
|
||||
full=GeneralQaPageDescription(welcome_title="不可保存"),
|
||||
),
|
||||
knowledge_base=PageHeaderDescription(),
|
||||
ai_writing=AIWritingPageDescription(),
|
||||
),
|
||||
SimpleNamespace(),
|
||||
)
|
||||
|
||||
assert error.value.status_code == 403
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_admin_can_update_page_descriptions(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
current = SystemSettings()
|
||||
saved: list[SystemSettings] = []
|
||||
|
||||
async def admin_user(_request):
|
||||
return SimpleNamespace(system_role="admin")
|
||||
|
||||
monkeypatch.setattr(
|
||||
system_settings_router,
|
||||
"get_optional_user_from_request",
|
||||
admin_user,
|
||||
)
|
||||
monkeypatch.setattr(system_settings_router, "load_system_settings", lambda: current)
|
||||
monkeypatch.setattr(
|
||||
system_settings_router,
|
||||
"save_system_settings",
|
||||
lambda settings: saved.append(settings) or settings,
|
||||
)
|
||||
|
||||
response = await system_settings_router.update_page_descriptions(
|
||||
system_settings_router.PageDescriptionsUpdateRequest(
|
||||
general_qa=GeneralQaPageDescriptions(
|
||||
full=GeneralQaPageDescription(
|
||||
welcome_title=" 欢迎使用工程开发智能问答助手 ",
|
||||
welcome_description=" 请描述您希望完成的工程开发任务。 ",
|
||||
input_placeholder=" 输入问题或任务描述 ",
|
||||
),
|
||||
compact=GeneralQaPageDescription(
|
||||
welcome_title=" 简洁问答欢迎语 ",
|
||||
),
|
||||
),
|
||||
knowledge_base=PageHeaderDescription(
|
||||
title=" 资料库 ",
|
||||
description=" 在这里整理和检索团队资料。 ",
|
||||
),
|
||||
ai_writing=AIWritingPageDescription(
|
||||
title=" 写作工作台 ",
|
||||
description=" 将想法逐步发展为文章。 ",
|
||||
guide_cards=[
|
||||
AIWritingGuideCard(
|
||||
title=" 明确写作意图 ",
|
||||
description=" 用一句话说明想写什么。 ",
|
||||
),
|
||||
AIWritingGuideCard(
|
||||
title=" 对齐写作参数 ",
|
||||
description=" 选择体裁与篇幅。 ",
|
||||
),
|
||||
AIWritingGuideCard(
|
||||
title=" 生成并优化文章 ",
|
||||
description=" 查看并完善草稿。 ",
|
||||
),
|
||||
],
|
||||
),
|
||||
strategy_projects=PageHeaderDescription(
|
||||
title=" 项目工作台 ",
|
||||
description=" 集中管理各类项目成果。 ",
|
||||
),
|
||||
deep_research=PageHeaderDescription(
|
||||
title=" 开始研究 ",
|
||||
description=" 收集资料并生成研究报告。 ",
|
||||
),
|
||||
gongwen=PageHeaderDescription(
|
||||
title=" 公文工作台 ",
|
||||
description=" 对话完成公文起草和校审。 ",
|
||||
),
|
||||
light_app_management=PageHeaderDescription(
|
||||
title=" 轻应用管理 ",
|
||||
description=" 登记和维护轻应用。 ",
|
||||
),
|
||||
light_app_list=PageHeaderDescription(
|
||||
title=" 轻应用列表 ",
|
||||
description=" 快速打开已登记的轻应用。 ",
|
||||
),
|
||||
agents=PageHeaderDescription(
|
||||
title=" 智能体工作台 ",
|
||||
description=" 管理可用智能体。 ",
|
||||
),
|
||||
business_chains=PageHeaderDescription(
|
||||
title=" 业务链目录 ",
|
||||
description=" 管理可复用的智能体编排。 ",
|
||||
),
|
||||
skills=PageHeaderDescription(
|
||||
title=" 技能工作台 ",
|
||||
description=" 浏览和维护技能。 ",
|
||||
),
|
||||
knowledge_management=PageHeaderDescription(
|
||||
title=" 知识服务 ",
|
||||
description=" 管理知识库与访问范围。 ",
|
||||
),
|
||||
),
|
||||
SimpleNamespace(),
|
||||
)
|
||||
|
||||
assert response.general_qa == GeneralQaPageDescriptions(
|
||||
full=GeneralQaPageDescription(
|
||||
welcome_title="欢迎使用工程开发智能问答助手",
|
||||
welcome_description="请描述您希望完成的工程开发任务。",
|
||||
input_placeholder="输入问题或任务描述",
|
||||
),
|
||||
compact=GeneralQaPageDescription(welcome_title="简洁问答欢迎语"),
|
||||
)
|
||||
assert response.knowledge_base == PageHeaderDescription(
|
||||
title="资料库",
|
||||
description="在这里整理和检索团队资料。",
|
||||
)
|
||||
assert response.ai_writing == AIWritingPageDescription(
|
||||
title="写作工作台",
|
||||
description="将想法逐步发展为文章。",
|
||||
guide_cards=[
|
||||
AIWritingGuideCard(
|
||||
title="明确写作意图",
|
||||
description="用一句话说明想写什么。",
|
||||
),
|
||||
AIWritingGuideCard(
|
||||
title="对齐写作参数",
|
||||
description="选择体裁与篇幅。",
|
||||
),
|
||||
AIWritingGuideCard(
|
||||
title="生成并优化文章",
|
||||
description="查看并完善草稿。",
|
||||
),
|
||||
],
|
||||
)
|
||||
assert response.strategy_projects == PageHeaderDescription(
|
||||
title="项目工作台",
|
||||
description="集中管理各类项目成果。",
|
||||
)
|
||||
assert response.deep_research == PageHeaderDescription(
|
||||
title="开始研究",
|
||||
description="收集资料并生成研究报告。",
|
||||
)
|
||||
assert response.gongwen == PageHeaderDescription(
|
||||
title="公文工作台",
|
||||
description="对话完成公文起草和校审。",
|
||||
)
|
||||
assert response.light_app_management == PageHeaderDescription(
|
||||
title="轻应用管理",
|
||||
description="登记和维护轻应用。",
|
||||
)
|
||||
assert response.light_app_list == PageHeaderDescription(
|
||||
title="轻应用列表",
|
||||
description="快速打开已登记的轻应用。",
|
||||
)
|
||||
assert response.agents == PageHeaderDescription(
|
||||
title="智能体工作台",
|
||||
description="管理可用智能体。",
|
||||
)
|
||||
assert response.business_chains == PageHeaderDescription(
|
||||
title="业务链目录",
|
||||
description="管理可复用的智能体编排。",
|
||||
)
|
||||
assert response.skills == PageHeaderDescription(
|
||||
title="技能工作台",
|
||||
description="浏览和维护技能。",
|
||||
)
|
||||
assert response.knowledge_management == PageHeaderDescription(
|
||||
title="知识服务",
|
||||
description="管理知识库与访问范围。",
|
||||
)
|
||||
assert saved[0].page_descriptions.general_qa == response.general_qa
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_retry_keep_partial_get_reflects_saved_settings(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
@@ -266,3 +535,83 @@ async def test_admin_can_update_retry_keep_partial(
|
||||
|
||||
assert response.enabled is False
|
||||
assert saved[0].retry_keep_partial.enabled is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_skill_versioning_get_reflects_saved_setting(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
current = SystemSettings().model_copy(
|
||||
update={
|
||||
"skill_versioning": system_settings_router.SkillVersioningSettings(
|
||||
enabled=True
|
||||
)
|
||||
}
|
||||
)
|
||||
monkeypatch.setattr(system_settings_router, "load_system_settings", lambda: current)
|
||||
|
||||
response = await system_settings_router.get_skill_versioning()
|
||||
|
||||
assert response.enabled is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_non_admin_cannot_update_skill_versioning(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
async def ordinary_user(_request):
|
||||
return SimpleNamespace(system_role="user")
|
||||
|
||||
monkeypatch.setattr(
|
||||
system_settings_router,
|
||||
"get_optional_user_from_request",
|
||||
ordinary_user,
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as error:
|
||||
await system_settings_router.update_skill_versioning(
|
||||
system_settings_router.SkillVersioningUpdateRequest(enabled=True),
|
||||
SimpleNamespace(),
|
||||
)
|
||||
|
||||
assert error.value.status_code == 403
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_admin_can_enable_skill_versioning_and_capture_baselines(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
from deerflow.skills import versioning
|
||||
|
||||
current = SystemSettings()
|
||||
saved: list[SystemSettings] = []
|
||||
reconciled: list[str | None] = []
|
||||
|
||||
async def admin_user(_request):
|
||||
return SimpleNamespace(system_role="admin", id="admin-1")
|
||||
|
||||
monkeypatch.setattr(
|
||||
system_settings_router,
|
||||
"get_optional_user_from_request",
|
||||
admin_user,
|
||||
)
|
||||
monkeypatch.setattr(system_settings_router, "load_system_settings", lambda: current)
|
||||
monkeypatch.setattr(
|
||||
system_settings_router,
|
||||
"save_system_settings",
|
||||
lambda settings: saved.append(settings) or settings,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
versioning,
|
||||
"reconcile_custom_skill_versions",
|
||||
lambda *, author=None: reconciled.append(author) or 0,
|
||||
)
|
||||
|
||||
response = await system_settings_router.update_skill_versioning(
|
||||
system_settings_router.SkillVersioningUpdateRequest(enabled=True),
|
||||
SimpleNamespace(),
|
||||
)
|
||||
|
||||
assert response.enabled is True
|
||||
assert saved[0].skill_versioning.enabled is True
|
||||
assert reconciled == ["admin-1"]
|
||||
@@ -0,0 +1,597 @@
|
||||
"""结构化 HTML 模板(kind=structured,先拿 no.1 落地)的引擎/存储/路由测试。
|
||||
|
||||
覆盖:
|
||||
* seed/meta:结构化 no1 四件套正确入库,单文件模板(如 no2)不受影响;
|
||||
* 契约语义与单文件时代等价:``reference_data`` 读 data.json 与旧 ``const DATA=`` 行
|
||||
解析结果一致,``validate_data`` / ``merge_with_reference`` 行为不变;
|
||||
* 组合:``read_template_text`` / ``fill_template`` 产出无残留标记的自包含文档,
|
||||
数据注入可被页面 ``JSON.parse`` 还原,标题/副标题/更新时间生效;
|
||||
* data.json 持久化(管理页「编辑数据」):整体替换 + 契约校验(顶层键不可增删);
|
||||
* 骨架编辑标记校验、结构化重置;
|
||||
* 旧单文件存储副本的自动升级:未手改 → 升级;手改过 → 保留旧版;
|
||||
* 路由:content 的 doc/raw 双形态、data 的 GET/PUT 与权限。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
from test_api_read_cache import FakeRedis, _config
|
||||
|
||||
from app.gateway import api_read_cache
|
||||
from app.gateway.routers import html_templates
|
||||
from deerflow.runtime.scheduler import template_fill, template_store
|
||||
|
||||
# no1 是当前唯一的结构化模板。
|
||||
TPL = "no1"
|
||||
LEGACY_FILE = template_fill._TEMPLATES_ROOT / TPL / "taiwan_littoral_ui_redwhite_v2.html"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def tmp_store(monkeypatch, tmp_path):
|
||||
"""隔离的运行时存储:全部用例不触碰真实 .deer-flow 目录。"""
|
||||
root = tmp_path / "html-templates"
|
||||
monkeypatch.setattr(template_store, "store_root", lambda: root)
|
||||
template_fill.invalidate_cache()
|
||||
template_store.ensure_seeded()
|
||||
yield root
|
||||
template_fill.invalidate_cache()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def api_request(monkeypatch, tmp_store):
|
||||
"""可直接调路由函数的最小 request(admin 身份,目录缓存走 FakeRedis)。"""
|
||||
redis = FakeRedis()
|
||||
monkeypatch.setattr("deerflow.persistence.engine.get_session_factory", lambda: None)
|
||||
monkeypatch.setattr(api_read_cache, "_api_read_cache_settings_cache", None)
|
||||
monkeypatch.setattr(api_read_cache, "_client", lambda *a, **k: (redis, "test:catalog"))
|
||||
request = SimpleNamespace(
|
||||
app=SimpleNamespace(state=SimpleNamespace(config=_config())),
|
||||
state=SimpleNamespace(user=SimpleNamespace(id="admin-1", system_role="admin")),
|
||||
)
|
||||
|
||||
async def current_user(req):
|
||||
return req.state.user
|
||||
|
||||
monkeypatch.setattr(html_templates, "get_optional_user_from_request", current_user)
|
||||
return request
|
||||
|
||||
|
||||
def _page_data_element(html: str) -> dict:
|
||||
match = re.search(
|
||||
r'<script type="application/json" id="page-data">(.*?)</script>', html, re.DOTALL
|
||||
)
|
||||
assert match is not None, "组合产物缺少 page-data JSON 元素"
|
||||
return json.loads(match.group(1))
|
||||
|
||||
|
||||
# ── seed / meta ──────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_structured_seed_and_single_untouched(tmp_store):
|
||||
meta = template_store.get_template_meta(TPL)
|
||||
assert meta is not None
|
||||
assert meta["kind"] == "structured"
|
||||
assert meta["convertible"] is True
|
||||
assert meta["manifest_var"] == "MANIFEST"
|
||||
assert sorted(p.name for p in (tmp_store / TPL).iterdir()) == [
|
||||
"data.json",
|
||||
"fields.json",
|
||||
"manifest.json",
|
||||
"meta.json",
|
||||
"runtime.js",
|
||||
"template.html",
|
||||
]
|
||||
|
||||
# 单文件模板照旧(kind=single,正文 = 完整文档,无结构化部件)。
|
||||
no2 = template_store.get_template_meta("no2")
|
||||
assert no2 is not None and no2["kind"] == "single"
|
||||
assert not (tmp_store / "no2" / "runtime.js").exists()
|
||||
|
||||
|
||||
def test_reference_data_matches_legacy_const_line(tmp_store):
|
||||
"""data.json 的契约语义必须与单文件时代的 const DATA= 行完全一致。"""
|
||||
legacy = template_fill._extract_const_object(LEGACY_FILE.read_text(encoding="utf-8"), "DATA")
|
||||
ref = template_fill.reference_data(TPL)
|
||||
assert ref == legacy
|
||||
assert template_fill.validate_data(TPL, legacy) == []
|
||||
assert set(template_fill.top_level_keys(TPL)) == set(legacy.keys())
|
||||
|
||||
|
||||
# ── 组合 / 填充 ─────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_read_template_text_composes_default_document(tmp_store):
|
||||
html = template_fill.read_template_text(TPL)
|
||||
for marker in template_fill.STRUCTURED_MARKERS:
|
||||
assert marker not in html
|
||||
assert 'id="page-data"' in html and 'id="page-manifest"' in html
|
||||
assert "const DATA=JSON.parse" in html
|
||||
# 页面内 JSON 编辑器随 runtime 内联。
|
||||
assert 'id="editorOverlay"' in html and 'id="applyJson"' in html
|
||||
assert _page_data_element(html)["overview"] == template_fill.reference_data(TPL)["overview"]
|
||||
|
||||
|
||||
def test_fill_template_structured_injects_data_titles_and_date(tmp_store):
|
||||
ref = template_fill.reference_data(TPL)
|
||||
modified = json.loads(json.dumps(ref))
|
||||
modified["overview"] = {
|
||||
"核心指标": [{"名称": "测试指标", "数值": 7, "单位": "个", "说明": "单元测试"}]
|
||||
}
|
||||
import datetime as dt
|
||||
|
||||
html = template_fill.fill_template(
|
||||
TPL,
|
||||
modified,
|
||||
update_date=dt.date(2026, 11, 4),
|
||||
title="新标题",
|
||||
subtitle="新副标题",
|
||||
)
|
||||
for marker in template_fill.STRUCTURED_MARKERS:
|
||||
assert marker not in html
|
||||
assert "<title>新标题</title>" in html
|
||||
assert "<h1>新标题</h1>" in html
|
||||
assert "新副标题" in html
|
||||
# MANIFEST 系统.更新时间/名称 同步。
|
||||
assert "2026-11-04" in html
|
||||
assert '"名称": "新标题"' in html
|
||||
# 注入的数据可被页面 JSON.parse 原样还原。
|
||||
assert _page_data_element(html)["overview"]["核心指标"][0]["名称"] == "测试指标"
|
||||
|
||||
|
||||
def test_merge_and_validate_semantics_unchanged(tmp_store):
|
||||
# 顶层键缺失 → 硬校验报错(与单文件时代一致)。
|
||||
errors = template_fill.validate_data(TPL, {"overview": {}})
|
||||
assert any("缺少顶层字段" in e for e in errors)
|
||||
# 空内容分支回退模板默认数据。
|
||||
merged = template_fill.merge_with_reference(TPL, {"overview": {}, "regions": []})
|
||||
assert merged == template_fill.reference_data(TPL)
|
||||
# 有内容的分支保留转换结果。
|
||||
merged2 = template_fill.merge_with_reference(
|
||||
TPL, {"overview": {"核心指标": [{"名称": "x", "数值": 1, "单位": "", "说明": ""}]}}
|
||||
)
|
||||
assert merged2["overview"]["核心指标"][0]["名称"] == "x"
|
||||
assert merged2["regions"] == template_fill.reference_data(TPL)["regions"]
|
||||
|
||||
|
||||
# ── data.json 持久化 / 骨架编辑 / 重置 ──────────────────────────────────────
|
||||
|
||||
|
||||
def test_write_read_template_data_roundtrip(tmp_store):
|
||||
ref = template_fill.reference_data(TPL)
|
||||
modified = json.loads(json.dumps(ref))
|
||||
modified["overview"]["核心指标"][0]["名称"] = "持久化指标"
|
||||
meta = template_store.write_template_data(TPL, modified)
|
||||
assert meta["kind"] == "structured"
|
||||
assert template_store.read_template_data(TPL)["overview"]["核心指标"][0]["名称"] == "持久化指标"
|
||||
# 引擎缓存已失效:reference_data / 组合产物立即反映新数据。
|
||||
assert template_fill.reference_data(TPL)["overview"]["核心指标"][0]["名称"] == "持久化指标"
|
||||
assert "持久化指标" in template_fill.read_template_text(TPL)
|
||||
|
||||
|
||||
def test_write_template_manifest_persists_and_composes(tmp_store):
|
||||
"""MANIFEST 持久化(系统名称/导航页面/交互规则):写回 manifest.json 并进入组合产物。"""
|
||||
ref = template_fill.reference_data(TPL)
|
||||
manifest = template_store.read_template_manifest(TPL)
|
||||
assert manifest.get("系统", {}).get("名称") == "台湾滨海防卫知识数据展示系统"
|
||||
manifest["系统"]["名称"] = "改名后的系统"
|
||||
manifest["页面"][0]["名称"] = "首页"
|
||||
template_store.write_template_data(TPL, ref, manifest=manifest)
|
||||
assert template_store.read_template_manifest(TPL)["系统"]["名称"] == "改名后的系统"
|
||||
# 组合产物(MANIFEST 内嵌 JSON)反映新值;导航按钮由页面 JS 依据 MANIFEST 渲染。
|
||||
composed = template_fill.read_template_text(TPL)
|
||||
assert "改名后的系统" in composed
|
||||
assert '"名称": "首页"' in composed
|
||||
# 非对象 MANIFEST 拒绝。
|
||||
with pytest.raises(template_store.TemplateStoreError, match="MANIFEST"):
|
||||
template_store.write_template_data(TPL, ref, manifest=["nope"])
|
||||
# 重置回随包原版。
|
||||
template_store.reset_template(TPL)
|
||||
assert template_store.read_template_manifest(TPL)["系统"]["名称"] == "台湾滨海防卫知识数据展示系统"
|
||||
|
||||
|
||||
def test_write_template_data_rejects_contract_changes(tmp_store):
|
||||
with pytest.raises(template_store.TemplateStoreError, match="契约"):
|
||||
template_store.write_template_data(TPL, {"overview": {}})
|
||||
with pytest.raises(template_store.TemplateStoreError):
|
||||
template_store.write_template_data(TPL, ["not", "an", "object"])
|
||||
# 单文件模板不支持数据编辑。
|
||||
with pytest.raises(template_store.TemplateStoreError, match="结构化"):
|
||||
template_store.write_template_data("no2", {"x": 1})
|
||||
|
||||
|
||||
def test_update_template_skeleton_marker_guard(tmp_store):
|
||||
with pytest.raises(template_store.TemplateStoreError, match="注入标记"):
|
||||
template_store.update_template(TPL, html="<!doctype html><p>标记丢了</p>")
|
||||
# 保留标记的骨架编辑可以通过,且不影响 kind/convertible。
|
||||
skeleton = template_store.read_template_html(TPL)
|
||||
edited = skeleton.replace("JSON 编辑器", "JSON 编辑器·编辑", 1)
|
||||
meta = template_store.update_template(TPL, html=edited)
|
||||
assert meta["kind"] == "structured" and meta["convertible"] is True
|
||||
assert template_fill.read_template_text(TPL).count("JSON 编辑器·编辑") == 1
|
||||
|
||||
|
||||
def test_reset_structured_restores_all_parts(tmp_store):
|
||||
template_store.write_template_data(
|
||||
TPL, _with_first_metric_name("持久化指标")
|
||||
)
|
||||
template_store.update_template(TPL, html=template_store.read_template_html(TPL) + "\n<!-- edited -->")
|
||||
template_store.reset_template(TPL)
|
||||
assert template_store.read_template_data(TPL)["overview"]["核心指标"][0]["名称"] != "持久化指标"
|
||||
assert "<!-- edited -->" not in template_store.read_template_html(TPL)
|
||||
assert template_fill.read_template_text(TPL) # 组合仍可用
|
||||
|
||||
|
||||
def _with_first_metric_name(name: str) -> dict:
|
||||
ref = template_fill.reference_data(TPL)
|
||||
modified = json.loads(json.dumps(ref))
|
||||
modified["overview"]["核心指标"][0]["名称"] = name
|
||||
return modified
|
||||
|
||||
|
||||
# ── 旧单文件存储副本的自动升级 ─────────────────────────────────────────────
|
||||
|
||||
|
||||
def _make_legacy_copy(store_root, *, modified: bool) -> None:
|
||||
import shutil
|
||||
|
||||
tpl_dir = store_root / TPL
|
||||
if tpl_dir.exists():
|
||||
shutil.rmtree(tpl_dir)
|
||||
tpl_dir.mkdir(parents=True)
|
||||
text = LEGACY_FILE.read_text(encoding="utf-8")
|
||||
if modified:
|
||||
text += "\n<!-- hand edit -->"
|
||||
(tpl_dir / "template.html").write_text(text, encoding="utf-8")
|
||||
(tpl_dir / "meta.json").write_text(
|
||||
json.dumps({"id": TPL, "name": "no.1", "builtin": True, "convertible": True}),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
|
||||
def test_auto_upgrade_untouched_legacy_copy(tmp_store):
|
||||
_make_legacy_copy(tmp_store, modified=False)
|
||||
assert template_store.ensure_seeded() is True
|
||||
meta = template_store.get_template_meta(TPL)
|
||||
assert meta["kind"] == "structured"
|
||||
assert (tmp_store / TPL / "runtime.js").is_file()
|
||||
# 升级后的模板可正常组合。
|
||||
assert "__PAGE_" not in template_fill.read_template_text(TPL)
|
||||
|
||||
|
||||
def test_auto_upgrade_skips_hand_modified_copy(tmp_store):
|
||||
_make_legacy_copy(tmp_store, modified=True)
|
||||
template_store.ensure_seeded()
|
||||
meta = template_store.get_template_meta(TPL)
|
||||
assert meta["kind"] == "single"
|
||||
assert "<!-- hand edit -->" in (tmp_store / TPL / "template.html").read_text(encoding="utf-8")
|
||||
# 管理页「重置」可手动升级到结构化版。
|
||||
template_store.reset_template(TPL)
|
||||
assert template_store.get_template_meta(TPL)["kind"] == "structured"
|
||||
assert (tmp_store / TPL / "runtime.js").is_file()
|
||||
|
||||
|
||||
# ── 路由 ────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_router_content_doc_and_raw(tmp_store, api_request):
|
||||
doc = await html_templates.get_html_template_content(TPL, api_request)
|
||||
assert "__PAGE_DATA__" not in doc["html"]
|
||||
assert 'id="page-data"' in doc["html"]
|
||||
raw = await html_templates.get_html_template_content(TPL, api_request, raw=True)
|
||||
assert "__PAGE_DATA__" in raw["html"] and "__PAGE_RUNTIME__" in raw["html"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_router_data_get_put_and_guards(tmp_store, api_request):
|
||||
got = await html_templates.get_html_template_data(TPL, api_request)
|
||||
assert "overview" in got["data"]
|
||||
# MANIFEST 随 GET 返回(管理页「系统设置」模块)。
|
||||
assert got["manifest"]["系统"]["名称"] == "台湾滨海防卫知识数据展示系统"
|
||||
# 模块中文名(按 MANIFEST.页面 解析,管理页「编辑数据」的模块导航用)。
|
||||
labels = {m["key"]: m["label"] for m in got["modules"]}
|
||||
assert labels["overview"] == "总览"
|
||||
assert labels["regions"] == "沿岸区域"
|
||||
assert labels["organization_charts"] == "组织与单位"
|
||||
assert labels["events"] == "演习与项目"
|
||||
assert set(labels) == set(got["data"].keys())
|
||||
|
||||
payload = got["data"]
|
||||
payload["overview"]["核心指标"][0]["名称"] = "路由持久化"
|
||||
manifest = json.loads(json.dumps(got["manifest"]))
|
||||
manifest["系统"]["名称"] = "路由改名系统"
|
||||
meta = await html_templates.update_html_template_data(
|
||||
TPL,
|
||||
html_templates.TemplateDataUpdateRequest(data=payload, manifest=manifest),
|
||||
api_request,
|
||||
)
|
||||
assert meta["kind"] == "structured"
|
||||
assert template_store.read_template_data(TPL)["overview"]["核心指标"][0]["名称"] == "路由持久化"
|
||||
assert template_store.read_template_manifest(TPL)["系统"]["名称"] == "路由改名系统"
|
||||
# 仅传 data(未动 MANIFEST)时不回写 manifest.json。
|
||||
await html_templates.update_html_template_data(
|
||||
TPL, html_templates.TemplateDataUpdateRequest(data=payload), api_request
|
||||
)
|
||||
assert template_store.read_template_manifest(TPL)["系统"]["名称"] == "路由改名系统"
|
||||
|
||||
# 契约被破坏(删顶层键) → 400。
|
||||
broken = {k: v for k, v in payload.items() if k != "regions"}
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
await html_templates.update_html_template_data(
|
||||
TPL, html_templates.TemplateDataUpdateRequest(data=broken), api_request
|
||||
)
|
||||
assert exc.value.status_code == 400
|
||||
|
||||
# 单文件模板 → 400。
|
||||
with pytest.raises(HTTPException) as exc2:
|
||||
await html_templates.get_html_template_data("no2", api_request)
|
||||
assert exc2.value.status_code == 400
|
||||
|
||||
# 非管理员不可写。
|
||||
api_request.state.user = SimpleNamespace(id="u2", system_role="user")
|
||||
with pytest.raises(HTTPException) as exc3:
|
||||
await html_templates.update_html_template_data(
|
||||
TPL, html_templates.TemplateDataUpdateRequest(data=payload), api_request
|
||||
)
|
||||
assert exc3.value.status_code == 403
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_scheduler_fill_path_yields_selfcontained_html(tmp_store):
|
||||
"""调度器模板填充路径的最终产物自包含:单文件、无标记、数据可解析。"""
|
||||
data_obj = template_fill.merge_with_reference(TPL, {"overview": _with_first_metric_name("调度指标")["overview"]})
|
||||
html = template_fill.fill_template(TPL, data_obj, update_date=__import__("datetime").date(2026, 1, 1))
|
||||
assert html.startswith("<!doctype html>") and "__PAGE_" not in html
|
||||
assert "调度指标" in html
|
||||
assert _page_data_element(html)["overview"]["核心指标"][0]["名称"] == "调度指标"
|
||||
|
||||
|
||||
# ── 字段显示 / 自定义模块 ───────────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_field_catalog_and_visibility_roundtrip(tmp_store):
|
||||
catalog = template_store.read_template_fields(TPL)
|
||||
labels = {f["label"] for f in catalog}
|
||||
assert "节点类别" in labels and "来源" in labels
|
||||
assert all(f.get("description") for f in catalog)
|
||||
|
||||
# 隐藏两个字段 → manifest 记 {label: false},其余全部显示。
|
||||
template_store.write_template_field_visibility(TPL, ["节点类别", "来源"])
|
||||
manifest = template_store.read_template_manifest(TPL)
|
||||
assert manifest["字段显示"] == {"节点类别": False, "来源": False}
|
||||
# 组合产物带隐藏配置,runtime 的 F() 据此不渲染。
|
||||
composed = template_fill.read_template_text(TPL)
|
||||
assert '"字段显示"' in composed
|
||||
|
||||
# 目录外字段拒绝。
|
||||
with pytest.raises(template_store.TemplateStoreError, match="未知字段"):
|
||||
template_store.write_template_field_visibility(TPL, ["不存在的字段"])
|
||||
|
||||
# 置空 = 全部显示。
|
||||
template_store.write_template_field_visibility(TPL, [])
|
||||
assert template_store.read_template_manifest(TPL)["字段显示"] == {}
|
||||
|
||||
|
||||
def test_custom_modules_write_and_sanitize(tmp_store):
|
||||
raw = (
|
||||
'<div class="panel"><h3 onclick="evil()">x</h3><script>alert(1)</script>'
|
||||
'<a href="javascript:alert(2)">链接</a><p>正常内容</p></div>'
|
||||
)
|
||||
template_store.write_template_custom_modules(
|
||||
TPL, [{"编号": "c1", "页面": "overview", "标题": "测试模块", "html": raw}]
|
||||
)
|
||||
saved = template_store.read_template_manifest(TPL)["自定义模块"]
|
||||
assert len(saved) == 1
|
||||
html = saved[0]["html"]
|
||||
assert "正常内容" in html
|
||||
assert "<script" not in html and "onclick" not in html and "javascript:" not in html
|
||||
# 组合产物包含模块内容与渲染函数。
|
||||
composed = template_fill.read_template_text(TPL)
|
||||
assert "正常内容" in composed and "renderCustomModules" in composed
|
||||
# 结构校验:缺页面/缺 html 拒绝。
|
||||
with pytest.raises(template_store.TemplateStoreError, match="页面"):
|
||||
template_store.write_template_custom_modules(TPL, [{"html": "<div>x</div>"}])
|
||||
with pytest.raises(template_store.TemplateStoreError, match="html"):
|
||||
template_store.write_template_custom_modules(TPL, [{"页面": "overview"}])
|
||||
|
||||
|
||||
def test_field_extract_locks_and_restore(tmp_store):
|
||||
"""字段抽取锁定:锁定字段的键在抽取结果合并后恢复模板现有值(按条目身份匹配)。"""
|
||||
ref = template_fill.reference_data(TPL)
|
||||
|
||||
# 锁定「记录类别」(org_units 条目的 记录类别 键)与「日期」(events 条目的 日期显示 键)。
|
||||
template_store.write_template_field_locks(TPL, ["记录类别", "日期"])
|
||||
keys = template_store.locked_field_keys(TPL)
|
||||
assert "记录类别" in keys and "日期显示" in keys
|
||||
|
||||
# 模拟抽取结果:改了第一个单位的记录类别、事件日期,并保留身份键(id/名称)。
|
||||
extracted = json.loads(json.dumps(ref))
|
||||
unit = extracted["org_units"][0]
|
||||
old_cat = ref["org_units"][0]["记录类别"]
|
||||
unit["记录类别"] = "被抽取改过的类别"
|
||||
event = extracted["events"][0]
|
||||
event["日期显示"] = "2026-01-01"
|
||||
event["名称"] = ref["events"][0].get("名称") or event.get("名称") or event.get("name") or "同身份"
|
||||
|
||||
restored = template_store.restore_locked_fields(TPL, extracted)
|
||||
# 锁定键恢复旧值。
|
||||
assert restored["org_units"][0]["记录类别"] == old_cat
|
||||
assert restored["events"][0]["日期显示"] == ref["events"][0]["日期显示"]
|
||||
# 未锁定分支不动。
|
||||
assert restored["overview"] == extracted["overview"]
|
||||
# 目录外字段拒绝。
|
||||
with pytest.raises(template_store.TemplateStoreError, match="未知字段"):
|
||||
template_store.write_template_field_locks(TPL, ["不存在字段"])
|
||||
# 清空 = 全部接受抽取。
|
||||
template_store.write_template_field_locks(TPL, [])
|
||||
assert template_store.restore_locked_fields(TPL, extracted) == extracted
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_router_fields_extract_flags(tmp_store, api_request):
|
||||
got = await html_templates.get_html_template_fields(TPL, api_request)
|
||||
by_label = {f["label"]: f for f in got["fields"]}
|
||||
assert by_label["记录类别"]["extract"] is True
|
||||
assert by_label["记录类别"]["has_key"] is True
|
||||
assert by_label["组织与单位概况"]["has_key"] is False # 布局分区
|
||||
|
||||
meta = await html_templates.update_html_template_fields(
|
||||
TPL,
|
||||
html_templates.FieldVisibilityUpdateRequest(hidden=[], extract_locked=["记录类别"]),
|
||||
api_request,
|
||||
)
|
||||
assert meta["kind"] == "structured"
|
||||
got2 = await html_templates.get_html_template_fields(TPL, api_request)
|
||||
by2 = {f["label"]: f for f in got2["fields"]}
|
||||
assert by2["记录类别"]["extract"] is False and by2["日期"]["extract"] is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_router_fields_and_custom_modules(tmp_store, api_request):
|
||||
got = await html_templates.get_html_template_fields(TPL, api_request)
|
||||
assert any(f["label"] == "节点类别" and f["visible"] for f in got["fields"])
|
||||
assert [p["key"] for p in got["pages"]][0] == "overview"
|
||||
assert got["custom_modules"] == []
|
||||
|
||||
meta = await html_templates.update_html_template_fields(
|
||||
TPL, html_templates.FieldVisibilityUpdateRequest(hidden=["节点类别"]), api_request
|
||||
)
|
||||
assert meta["kind"] == "structured"
|
||||
got2 = await html_templates.get_html_template_fields(TPL, api_request)
|
||||
by_label = {f["label"]: f["visible"] for f in got2["fields"]}
|
||||
assert by_label["节点类别"] is False and by_label["来源"] is True
|
||||
|
||||
await html_templates.update_html_template_custom_modules(
|
||||
TPL,
|
||||
html_templates.CustomModuleListUpdateRequest(
|
||||
modules=[{"页面": "overview", "标题": "t", "html": "<div>hi</div>"}]
|
||||
),
|
||||
api_request,
|
||||
)
|
||||
got3 = await html_templates.get_html_template_fields(TPL, api_request)
|
||||
assert got3["custom_modules"][0]["html"] == "<div>hi</div>"
|
||||
|
||||
# 非管理员不可写。
|
||||
api_request.state.user = SimpleNamespace(id="u2", system_role="user")
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
await html_templates.update_html_template_fields(
|
||||
TPL, html_templates.FieldVisibilityUpdateRequest(hidden=[]), api_request
|
||||
)
|
||||
assert exc.value.status_code == 403
|
||||
|
||||
|
||||
# ── 定时任务「抽取信息写回模板数据」开关(html_config.template_data_writeback) ──
|
||||
|
||||
|
||||
def _writeback_task(writeback: bool | None) -> dict[str, Any]:
|
||||
ctx: dict[str, Any] = {"require_html": True, "html_config": {}}
|
||||
if writeback is not None:
|
||||
ctx["html_config"]["template_data_writeback"] = writeback
|
||||
return {
|
||||
"task_id": "t1",
|
||||
"user_id": "u1",
|
||||
"name": "回写联调",
|
||||
"prompt": "更新总览的核心指标名称为「回写指标」",
|
||||
"description": "",
|
||||
"timezone": "UTC",
|
||||
"enabled": True,
|
||||
"execution_context": ctx,
|
||||
}
|
||||
|
||||
|
||||
def _build_writeback_service(monkeypatch, *, converted: dict | None):
|
||||
"""构造 mock 掉检索/转换/归档的 service,返回 (service, write_mock)。"""
|
||||
from datetime import UTC
|
||||
from datetime import datetime as dt
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
from deerflow.persistence.scheduled_tasks import MemoryScheduledTaskStore
|
||||
from deerflow.runtime.scheduler import service as scheduler_service
|
||||
from deerflow.runtime.scheduler.service import ScheduledTaskService
|
||||
|
||||
service = ScheduledTaskService(app=None, store=MemoryScheduledTaskStore(), poll_interval_seconds=60.0)
|
||||
ref = template_fill.reference_data(TPL)
|
||||
source_obj = json.loads(json.dumps(ref))
|
||||
source_obj["overview"] = {"核心指标": [{"名称": "回写指标", "数值": 3, "单位": "个", "说明": ""}]}
|
||||
monkeypatch.setattr(
|
||||
service, "_gather_html_data", AsyncMock(return_value=("一些源数据", []))
|
||||
)
|
||||
if converted is None:
|
||||
async def _fail(*a, **k):
|
||||
raise scheduler_service.TemplateFillError("boom")
|
||||
monkeypatch.setattr(service, "_convert_to_template_data", AsyncMock(side_effect=_fail))
|
||||
else:
|
||||
# 成功分支返回「抽取了新指标」的转换结果(converted 仅作成功/失败哨兵)。
|
||||
monkeypatch.setattr(service, "_convert_to_template_data", AsyncMock(return_value=source_obj))
|
||||
monkeypatch.setattr(service, "_finalize_template_html", AsyncMock(return_value={"ok": True}))
|
||||
monkeypatch.setattr(service, "_archive_html_index", lambda *_a, **_k: None)
|
||||
# write_template_data 是同步函数,用 MagicMock 而非 AsyncMock。
|
||||
write_mock = MagicMock()
|
||||
monkeypatch.setattr(template_store, "write_template_data", write_mock)
|
||||
scheduled_for = dt(2026, 11, 4, 12, 0, tzinfo=UTC)
|
||||
return service, write_mock, scheduled_for, source_obj
|
||||
|
||||
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("writeback,converted,expect_write", [
|
||||
(True, "converted", True),
|
||||
(False, "converted", False),
|
||||
(None, "converted", False), # 老任务缺省关闭
|
||||
(True, None, False), # 转换失败(回退默认数据)不写回
|
||||
])
|
||||
async def test_html_template_reference_writeback_switch(tmp_store, monkeypatch, writeback, converted, expect_write):
|
||||
service, write_mock, scheduled_for, _source = _build_writeback_service(
|
||||
monkeypatch, converted=converted
|
||||
)
|
||||
await service._run_html_template_reference(
|
||||
_writeback_task(writeback),
|
||||
tpl_id=TPL,
|
||||
runtime_thread_id="rt",
|
||||
scheduled_for=scheduled_for,
|
||||
run_id="r1",
|
||||
)
|
||||
if expect_write:
|
||||
assert write_mock.call_count == 1
|
||||
# 只写 data.json:调用参数不含 manifest kwarg(系统名称等 MANIFEST 不动)。
|
||||
args, kwargs = write_mock.call_args
|
||||
assert args[0] == TPL and "manifest" not in kwargs
|
||||
assert args[1]["overview"]["核心指标"][0]["名称"] == "回写指标"
|
||||
else:
|
||||
assert write_mock.call_count == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_writeback_respects_field_locks(tmp_store, monkeypatch):
|
||||
"""锁定字段不接受抽取更新:写回 data.json 的数据里,锁定键保留模板旧值。"""
|
||||
template_store.write_template_field_locks(TPL, ["日期"])
|
||||
service, write_mock, scheduled_for, source_obj = _build_writeback_service(
|
||||
monkeypatch, converted=source_marker()
|
||||
)
|
||||
await service._run_html_template_reference(
|
||||
_writeback_task(True),
|
||||
tpl_id=TPL,
|
||||
runtime_thread_id="rt",
|
||||
scheduled_for=scheduled_for,
|
||||
run_id="r-lock",
|
||||
)
|
||||
assert write_mock.call_count == 1
|
||||
args, _kwargs = write_mock.call_args
|
||||
old_dates = [e.get("日期显示") for e in template_fill.reference_data(TPL)["events"]]
|
||||
new_dates = [e.get("日期显示") for e in args[1]["events"]]
|
||||
assert new_dates == old_dates # 锁定键恢复旧值
|
||||
|
||||
|
||||
def source_marker():
|
||||
ref = template_fill.reference_data(TPL)
|
||||
obj = json.loads(json.dumps(ref))
|
||||
for e in obj["events"]:
|
||||
e["日期显示"] = "2099-12-31"
|
||||
return obj
|
||||
Reference in new issue
Block a user