# Testing Plugins (`praisonaiui.testing`)

> praisonaiui.testing gives plugin packages an official, isolated test harness so you can verify manifest entries, pages, strict CSP script hashes, static ESM ass

`praisonaiui.testing` gives plugin packages an official, isolated test harness so you can verify manifest entries, pages, strict CSP script hashes, static ESM assets, WebSocket tickets, and ASGI `lifespan` hooks in a few lines — without copying internal server fixtures.

## Quick start (`plugin_test_client`)

```python
from praisonaiui.testing import plugin_test_client
from my_plugin import Plugin


def test_my_plugin_contract():
    plugin = Plugin()
    with plugin_test_client(plugin) as harness:
        # Verifies manifest, declared pages, strict sha256 CSP, static modules,
        # POST /api/auth/ws-ticket, and shell-only /sessions 404 isolation.
        manifest = harness.assert_contract()
        assert manifest["style"] == "app"

        # Exercise plugin REST routes while ASGI lifespan is active.
        resp = harness.client.get("/api/plugins/my_plugin/status")
        assert resp.status_code == 200
```

## What `harness.assert_contract()` checks

| Check | Default | What it verifies |
|---|---|---|
| Plugin status | always | `manifest["plugins"]` includes your plugin with `status == "ok"` |
| Declared pages | always | Every `PageSpec` from `plugin.pages()` appears in `manifest["pages"]` and `GET /<page_id>` returns `200 text/html` |
| Strict CSP (`check_csp`) | `True` | `Content-Security-Policy` contains `'sha256-…'` hashes and never `'unsafe-inline'` in `script-src` |
| Static modules (`check_static`) | `True` | Every `/_plugins/<name>/<module>` URL returns `200` |
| WS ticket (`check_ws_ticket`) | `True` | `POST /api/auth/ws-ticket` mints a non-empty single-use ticket |
| Shell isolation (`check_shell_isolation`) | `True` | Unmounted agent routes (`/sessions`) return `404` |

## Testing authenticated WebSockets & `lifespan`

```python
def test_plugin_websocket_and_lifespan():
    plugin = Plugin()
    with plugin_test_client(plugin) as harness:
        ticket = harness.ws_ticket()
        with harness.client.websocket_connect(
            f"/api/plugins/{plugin.name}/ws/events?ticket={ticket}"
        ) as ws:
            ws.send_text("ping")
            assert ws.receive_text() == "pong"

    # Exiting the context runs Starlette ASGI shutdown and plugin.lifespan teardown.
    assert plugin.closed is True
```

## Testing with custom config or auth

```python
def test_plugin_with_disabled_builtin_and_custom_routes(tmp_path):
    with plugin_test_client(
        Plugin(),
        config={"extensions": {"disabled": ["builtin.approvals"]}},
        data_dir=tmp_path,
    ) as harness:
        manifest = harness.assert_contract()
        assert "builtin.approvals" in manifest["builtins"]["disabled"]
        assert "builtin.notifications" in manifest["builtins"]["unmounted"]
```

## Low-level state isolation (`isolated_aiui_state`)

If you construct `create_app()` or `create_shell_app()` directly in a test, wrap it in `isolated_aiui_state()` so global page registries, themes, configs, `PluginRegistry`, and `AIUI_DATA_DIR` are automatically snapshotted and restored — even if the test raises an exception:

```python
from praisonaiui.testing import isolated_aiui_state


def test_custom_host(tmp_path):
    with isolated_aiui_state(data_dir=tmp_path) as data_dir:
        ...
```

## Related pages

- [Shell Hosting, Lifespan & Strict CSP](https://ui.praison.ai/docs/features/plugins/shell-hosting-and-csp.md)
- [Testing](https://ui.praison.ai/docs/testing.md)
- [API Testing Guide](https://ui.praison.ai/docs/api/testing.md)
