mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 05:59:36 +08:00
Adapt the Phase 1 foundation from PR #1309 and complete scoped API lifecycle, pre-import approval, artifact-bound revocation, recovery, bounded workers and frontend isolation. Keep the stronger ecosystem roadmap explicit in the RFC. Co-authored-by: XZH <cto@xzh.ai>
276 lines
8.6 KiB
Python
276 lines
8.6 KiB
Python
"""CLI visibility for the unified plugin package registry."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import typer
|
|
from typer.testing import CliRunner
|
|
|
|
import deeptutor.plugins.catalog as plugin_catalog
|
|
import deeptutor.plugins.registry as plugin_registry
|
|
from deeptutor_cli.plugin import register
|
|
|
|
|
|
def _plugin_cli(monkeypatch) -> typer.Typer:
|
|
app = typer.Typer()
|
|
register(app)
|
|
return app
|
|
|
|
|
|
def test_plugin_state_lists_package_status(monkeypatch) -> None:
|
|
monkeypatch.setattr(
|
|
plugin_registry,
|
|
"get_plugin_registry",
|
|
lambda: SimpleNamespace(
|
|
list_plugins=lambda: [
|
|
SimpleNamespace(
|
|
id="org.author.example",
|
|
version="1.0.0",
|
|
status="enabled",
|
|
description="Example package",
|
|
)
|
|
]
|
|
),
|
|
)
|
|
app = _plugin_cli(monkeypatch)
|
|
|
|
result = CliRunner().invoke(app, ["state"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert "org.author.example" in result.output
|
|
assert "enabled" in result.output
|
|
|
|
|
|
def test_plugin_search_uses_official_catalog(monkeypatch) -> None:
|
|
monkeypatch.setattr(
|
|
plugin_catalog,
|
|
"search_catalog",
|
|
lambda query: [
|
|
SimpleNamespace(
|
|
id="org.author.example",
|
|
version="1.0.0",
|
|
status="available",
|
|
description_i18n={"en": "Example package"},
|
|
)
|
|
],
|
|
)
|
|
app = _plugin_cli(monkeypatch)
|
|
|
|
result = CliRunner().invoke(app, ["search", "example"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert "org.author.example" in result.output
|
|
assert "available" in result.output
|
|
|
|
|
|
def test_plugin_disable_and_enable_write_state(monkeypatch) -> None:
|
|
calls: list[tuple[str, bool]] = []
|
|
|
|
def set_enabled(plugin_id: str, enabled: bool):
|
|
calls.append((plugin_id, enabled))
|
|
return SimpleNamespace(id=plugin_id, status="enabled" if enabled else "disabled")
|
|
|
|
monkeypatch.setattr(
|
|
plugin_registry,
|
|
"get_plugin_registry",
|
|
lambda: SimpleNamespace(set_enabled=set_enabled),
|
|
)
|
|
app = _plugin_cli(monkeypatch)
|
|
|
|
disabled = CliRunner().invoke(app, ["disable", "org.author.example"])
|
|
enabled = CliRunner().invoke(app, ["enable", "org.author.example"])
|
|
|
|
assert disabled.exit_code == 0, disabled.output
|
|
assert enabled.exit_code == 0, enabled.output
|
|
assert calls == [("org.author.example", False), ("org.author.example", True)]
|
|
|
|
|
|
def test_plugin_show_outputs_package_metadata(monkeypatch) -> None:
|
|
monkeypatch.setattr(
|
|
plugin_registry,
|
|
"get_plugin_registry",
|
|
lambda: SimpleNamespace(
|
|
get_plugin=lambda plugin_id: SimpleNamespace(
|
|
to_dict=lambda: {
|
|
"id": plugin_id,
|
|
"status": "enabled",
|
|
"manifest": {"schema_version": "deeptutor.plugin/v1"},
|
|
}
|
|
)
|
|
),
|
|
)
|
|
app = _plugin_cli(monkeypatch)
|
|
|
|
result = CliRunner().invoke(app, ["show", "org.author.example"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert '"schema_version": "deeptutor.plugin/v1"' in result.output
|
|
|
|
|
|
def test_plugin_approve_writes_permission_state(monkeypatch) -> None:
|
|
calls: list[str] = []
|
|
|
|
def approve(plugin_id: str):
|
|
calls.append(plugin_id)
|
|
return SimpleNamespace(id=plugin_id, status="enabled")
|
|
|
|
monkeypatch.setattr(
|
|
plugin_registry,
|
|
"get_plugin_registry",
|
|
lambda: SimpleNamespace(approve=approve),
|
|
)
|
|
app = _plugin_cli(monkeypatch)
|
|
|
|
result = CliRunner().invoke(app, ["approve", "org.author.example"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert calls == ["org.author.example"]
|
|
assert "permissions approved" in result.output
|
|
|
|
|
|
def test_plugin_install_reports_reviewed_wheel(monkeypatch) -> None:
|
|
calls: list[tuple[Path, str]] = []
|
|
|
|
class FakeManager:
|
|
def install(self, artifact: Path, *, expected_sha256: str):
|
|
calls.append((artifact, expected_sha256))
|
|
return SimpleNamespace(
|
|
plugin_id="org.author.example",
|
|
version="1.0.0",
|
|
action="installed",
|
|
artifact_sha256="0" * 64,
|
|
venv_path=Path("/tmp/plugins/org.author.example/versions/1.0.0/venv"),
|
|
)
|
|
|
|
monkeypatch.setattr("deeptutor.plugins.lifecycle.PluginLifecycleManager", FakeManager)
|
|
app = _plugin_cli(monkeypatch)
|
|
|
|
result = CliRunner().invoke(
|
|
app,
|
|
[
|
|
"install",
|
|
"/tmp/example-1.0.0.whl",
|
|
"--sha256",
|
|
"0" * 64,
|
|
],
|
|
)
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert calls == [(Path("/tmp/example-1.0.0.whl"), "0" * 64)]
|
|
assert '"plugin": "org.author.example"' in result.output
|
|
assert '"next_step": "review permissions' in result.output
|
|
|
|
|
|
def test_plugin_install_uses_reviewed_catalog_pin(monkeypatch) -> None:
|
|
calls: list[dict[str, object]] = []
|
|
|
|
class FakeCatalogManager:
|
|
def install(
|
|
self,
|
|
plugin_id: str,
|
|
*,
|
|
version: str,
|
|
allow_deprecated: bool,
|
|
timeout_seconds: float,
|
|
):
|
|
calls.append(
|
|
{
|
|
"plugin_id": plugin_id,
|
|
"version": version,
|
|
"allow_deprecated": allow_deprecated,
|
|
"timeout_seconds": timeout_seconds,
|
|
}
|
|
)
|
|
return SimpleNamespace(
|
|
artifact_url="https://artifacts.example.com/example-1.0.0.whl",
|
|
lifecycle=SimpleNamespace(
|
|
plugin_id=plugin_id,
|
|
version="1.0.0",
|
|
action="installed",
|
|
artifact_sha256="0" * 64,
|
|
venv_path=Path("/tmp/plugins/org.author.example/versions/1.0/venv"),
|
|
),
|
|
)
|
|
|
|
monkeypatch.setattr("deeptutor.plugins.distribution.CatalogInstallManager", FakeCatalogManager)
|
|
app = _plugin_cli(monkeypatch)
|
|
|
|
result = CliRunner().invoke(
|
|
app,
|
|
[
|
|
"install",
|
|
"org.author.example",
|
|
"--version",
|
|
"1.0.0",
|
|
"--allow-deprecated",
|
|
"--timeout",
|
|
"12",
|
|
],
|
|
)
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert calls == [
|
|
{
|
|
"plugin_id": "org.author.example",
|
|
"version": "1.0.0",
|
|
"allow_deprecated": True,
|
|
"timeout_seconds": 12.0,
|
|
}
|
|
]
|
|
assert '"artifact_url"' in result.output
|
|
assert '"sha256": "' + "0" * 64 in result.output
|
|
|
|
|
|
def test_plugin_catalog_install_rejects_local_digest_override(monkeypatch) -> None:
|
|
class UnexpectedManager:
|
|
def __init__(self) -> None:
|
|
raise AssertionError("remote catalog install must not accept a digest override")
|
|
|
|
monkeypatch.setattr("deeptutor.plugins.distribution.CatalogInstallManager", UnexpectedManager)
|
|
app = _plugin_cli(monkeypatch)
|
|
|
|
result = CliRunner().invoke(app, ["install", "org.author.example", "--sha256", "0" * 64])
|
|
|
|
assert result.exit_code == 1
|
|
assert "--sha256 is only valid for a local wheel" in result.output
|
|
|
|
|
|
def test_plugin_rollback_and_uninstall_use_managed_lifecycle(monkeypatch) -> None:
|
|
actions: list[str] = []
|
|
|
|
class FakeManager:
|
|
def rollback(self, plugin_id: str):
|
|
actions.append(("rollback", plugin_id))
|
|
return SimpleNamespace(
|
|
plugin_id=plugin_id,
|
|
version="1.0.0",
|
|
artifact_sha256="0" * 64,
|
|
venv_path=Path("/tmp/plugins/org.author.example/versions/1.0.0/venv"),
|
|
)
|
|
|
|
def uninstall(self, plugin_id: str):
|
|
actions.append(("uninstall", plugin_id))
|
|
return SimpleNamespace(
|
|
plugin_id=plugin_id,
|
|
version="1.1.0",
|
|
artifact_sha256="0" * 64,
|
|
venv_path=Path("/tmp/plugins/org.author.example"),
|
|
)
|
|
|
|
monkeypatch.setattr("deeptutor.plugins.lifecycle.PluginLifecycleManager", FakeManager)
|
|
app = _plugin_cli(monkeypatch)
|
|
|
|
rollback = CliRunner().invoke(app, ["rollback", "org.author.example"])
|
|
uninstall = CliRunner().invoke(app, ["uninstall", "org.author.example"])
|
|
|
|
assert rollback.exit_code == 0, rollback.output
|
|
assert uninstall.exit_code == 0, uninstall.output
|
|
assert actions == [
|
|
("rollback", "org.author.example"),
|
|
("uninstall", "org.author.example"),
|
|
]
|
|
assert "rolled back to 1.0.0" in rollback.output
|
|
assert "1.1.0 removed" in uninstall.output
|