webcompy-testing 0.0.14__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- webcompy_testing-0.0.14/.gitignore +167 -0
- webcompy_testing-0.0.14/LICENSE +21 -0
- webcompy_testing-0.0.14/PKG-INFO +45 -0
- webcompy_testing-0.0.14/README.md +23 -0
- webcompy_testing-0.0.14/pyproject.toml +36 -0
- webcompy_testing-0.0.14/src/webcompy_testing/__init__.py +62 -0
- webcompy_testing-0.0.14/src/webcompy_testing/_app.py +34 -0
- webcompy_testing-0.0.14/src/webcompy_testing/_asgi.py +109 -0
- webcompy_testing-0.0.14/src/webcompy_testing/_dom.py +149 -0
- webcompy_testing-0.0.14/src/webcompy_testing/_ports.py +1394 -0
- webcompy_testing-0.0.14/src/webcompy_testing/_renderer.py +359 -0
- webcompy_testing-0.0.14/src/webcompy_testing/_restore.py +62 -0
- webcompy_testing-0.0.14/src/webcompy_testing/_scope.py +35 -0
- webcompy_testing-0.0.14/src/webcompy_testing/_utils.py +30 -0
- webcompy_testing-0.0.14/src/webcompy_testing/browser_runner/__init__.py +25 -0
- webcompy_testing-0.0.14/src/webcompy_testing/browser_runner/_runner.py +480 -0
- webcompy_testing-0.0.14/src/webcompy_testing/py.typed +0 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
parts/
|
|
18
|
+
sdist/
|
|
19
|
+
var/
|
|
20
|
+
wheels/
|
|
21
|
+
share/python-wheels/
|
|
22
|
+
*.egg-info/
|
|
23
|
+
.installed.cfg
|
|
24
|
+
*.egg
|
|
25
|
+
MANIFEST
|
|
26
|
+
|
|
27
|
+
# PyInstaller
|
|
28
|
+
# Usually these files are written by a python script from a template
|
|
29
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
30
|
+
*.manifest
|
|
31
|
+
*.spec
|
|
32
|
+
|
|
33
|
+
# Installer logs
|
|
34
|
+
pip-log.txt
|
|
35
|
+
pip-delete-this-directory.txt
|
|
36
|
+
|
|
37
|
+
# Unit test / coverage reports
|
|
38
|
+
htmlcov/
|
|
39
|
+
.tox/
|
|
40
|
+
.nox/
|
|
41
|
+
.coverage
|
|
42
|
+
.coverage.*
|
|
43
|
+
.cache
|
|
44
|
+
nosetests.xml
|
|
45
|
+
coverage.xml
|
|
46
|
+
*.cover
|
|
47
|
+
*.py,cover
|
|
48
|
+
.hypothesis/
|
|
49
|
+
.pytest_cache/
|
|
50
|
+
cover/
|
|
51
|
+
|
|
52
|
+
# Translations
|
|
53
|
+
*.mo
|
|
54
|
+
*.pot
|
|
55
|
+
|
|
56
|
+
# Django stuff:
|
|
57
|
+
*.log
|
|
58
|
+
local_settings.py
|
|
59
|
+
db.sqlite3
|
|
60
|
+
db.sqlite3-journal
|
|
61
|
+
|
|
62
|
+
# Flask stuff:
|
|
63
|
+
instance/
|
|
64
|
+
.webassets-cache
|
|
65
|
+
|
|
66
|
+
# Scrapy stuff:
|
|
67
|
+
.scrapy
|
|
68
|
+
|
|
69
|
+
# PyBuilder
|
|
70
|
+
.pybuilder/
|
|
71
|
+
target/
|
|
72
|
+
|
|
73
|
+
# Jupyter Notebook
|
|
74
|
+
.ipynb_checkpoints
|
|
75
|
+
|
|
76
|
+
# IPython
|
|
77
|
+
profile_default/
|
|
78
|
+
ipython_config.py
|
|
79
|
+
|
|
80
|
+
# uv
|
|
81
|
+
.venv/
|
|
82
|
+
|
|
83
|
+
# pipenv
|
|
84
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
85
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
86
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
87
|
+
# install all needed dependencies.
|
|
88
|
+
Pipfile.lock
|
|
89
|
+
|
|
90
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
|
91
|
+
__pypackages__/
|
|
92
|
+
|
|
93
|
+
# Celery stuff
|
|
94
|
+
celerybeat-schedule
|
|
95
|
+
celerybeat.pid
|
|
96
|
+
|
|
97
|
+
# SageMath parsed files
|
|
98
|
+
*.sage.py
|
|
99
|
+
|
|
100
|
+
# Environments
|
|
101
|
+
.env
|
|
102
|
+
.venv
|
|
103
|
+
env/
|
|
104
|
+
venv/
|
|
105
|
+
ENV/
|
|
106
|
+
env.bak/
|
|
107
|
+
venv.bak/
|
|
108
|
+
|
|
109
|
+
# Spyder project settings
|
|
110
|
+
.spyderproject
|
|
111
|
+
.spyproject
|
|
112
|
+
|
|
113
|
+
# Rope project settings
|
|
114
|
+
.ropeproject
|
|
115
|
+
|
|
116
|
+
# mkdocs documentation
|
|
117
|
+
/site
|
|
118
|
+
|
|
119
|
+
# mypy
|
|
120
|
+
.mypy_cache/
|
|
121
|
+
.dmypy.json
|
|
122
|
+
dmypy.json
|
|
123
|
+
|
|
124
|
+
# Pyre type checker
|
|
125
|
+
.pyre/
|
|
126
|
+
|
|
127
|
+
# pytype static type analyzer
|
|
128
|
+
.pytype/
|
|
129
|
+
|
|
130
|
+
# Cython debug symbols
|
|
131
|
+
cython_debug/
|
|
132
|
+
|
|
133
|
+
# AI coding harness local config
|
|
134
|
+
/opencode.json
|
|
135
|
+
/opencode.jsonc
|
|
136
|
+
/CLAUDE.local.md
|
|
137
|
+
/.claude/settings.local.json
|
|
138
|
+
/.agents/*.local.json
|
|
139
|
+
|
|
140
|
+
# Temporary directory
|
|
141
|
+
**/.tmp/
|
|
142
|
+
|
|
143
|
+
# Generated docs site
|
|
144
|
+
/docs/
|
|
145
|
+
docs_app/dist/
|
|
146
|
+
|
|
147
|
+
# E2E test server log
|
|
148
|
+
.e2e-server.log
|
|
149
|
+
|
|
150
|
+
# WebComPy lock files
|
|
151
|
+
webcompy-lock.json
|
|
152
|
+
|
|
153
|
+
# Playwright MCP
|
|
154
|
+
.playwright-mcp/
|
|
155
|
+
|
|
156
|
+
# Workspace directory for development artifacts
|
|
157
|
+
.workspace/
|
|
158
|
+
|
|
159
|
+
# Browser test screenshots and recordings (root only, allow in static/)
|
|
160
|
+
/*.png
|
|
161
|
+
/*.jpg
|
|
162
|
+
/*.jpeg
|
|
163
|
+
/*.gif
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
# Dual-run / sweep artifacts
|
|
167
|
+
/artifacts/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Kento NIWASE
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: webcompy-testing
|
|
3
|
+
Version: 0.0.14
|
|
4
|
+
Summary: WebComPy testing utilities (TestRenderer, fake ports, ASGI test client)
|
|
5
|
+
Project-URL: Homepage, https://github.com/kniwase/WebComPy
|
|
6
|
+
Project-URL: Documentation, https://webcompy.net/documents/
|
|
7
|
+
Project-URL: LLM Docs, https://webcompy.net/llms.txt
|
|
8
|
+
Author-email: Kento Niwase <kento.niwase@gmail.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: browser,frontend,testing
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Requires-Python: >=3.12
|
|
17
|
+
Requires-Dist: beautifulsoup4
|
|
18
|
+
Requires-Dist: starlette
|
|
19
|
+
Requires-Dist: webcompy-server==0.0.14
|
|
20
|
+
Requires-Dist: webcompy==0.0.14
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# webcompy-testing
|
|
24
|
+
|
|
25
|
+
Headless component testing, fake browser/network ports, ASGI test client helpers, and in-browser PyScript test runner utilities for [WebComPy](https://github.com/kniwase/WebComPy).
|
|
26
|
+
|
|
27
|
+
Key utilities exported from `webcompy_testing`:
|
|
28
|
+
|
|
29
|
+
- `TestRenderer` — Mount and interact with WebComPy components in standard Python without a browser
|
|
30
|
+
- Fake port implementations (`FakeBrowserDOMPort`, `FakeFetchPort`, `FakeEventSourcePort`, `FakeWebSocketPort`, `FakeAsyncSchedulerPort`, and more)
|
|
31
|
+
- `create_test_asgi_app` and `render_app_html` — Server-side rendering and ASGI endpoint testing helpers
|
|
32
|
+
- DI scope helpers and browser runner support (`webcompy_testing.browser_runner`)
|
|
33
|
+
|
|
34
|
+
This distribution is versioned in lockstep with `webcompy` and `webcompy-server`.
|
|
35
|
+
|
|
36
|
+
## Documentation & Resources
|
|
37
|
+
|
|
38
|
+
- [Documentation](https://webcompy.net/documents/)
|
|
39
|
+
- [Testing Guide](https://webcompy.net/documents/server/testing/)
|
|
40
|
+
- [LLM Documentation Index (`llms.txt`)](https://webcompy.net/llms.txt) — bundles: [`llms-small.txt`](https://webcompy.net/llms-small.txt) / [`llms-full.txt`](https://webcompy.net/llms-full.txt)
|
|
41
|
+
- [Source Code & Discussions](https://github.com/kniwase/WebComPy)
|
|
42
|
+
|
|
43
|
+
## License
|
|
44
|
+
|
|
45
|
+
MIT License — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# webcompy-testing
|
|
2
|
+
|
|
3
|
+
Headless component testing, fake browser/network ports, ASGI test client helpers, and in-browser PyScript test runner utilities for [WebComPy](https://github.com/kniwase/WebComPy).
|
|
4
|
+
|
|
5
|
+
Key utilities exported from `webcompy_testing`:
|
|
6
|
+
|
|
7
|
+
- `TestRenderer` — Mount and interact with WebComPy components in standard Python without a browser
|
|
8
|
+
- Fake port implementations (`FakeBrowserDOMPort`, `FakeFetchPort`, `FakeEventSourcePort`, `FakeWebSocketPort`, `FakeAsyncSchedulerPort`, and more)
|
|
9
|
+
- `create_test_asgi_app` and `render_app_html` — Server-side rendering and ASGI endpoint testing helpers
|
|
10
|
+
- DI scope helpers and browser runner support (`webcompy_testing.browser_runner`)
|
|
11
|
+
|
|
12
|
+
This distribution is versioned in lockstep with `webcompy` and `webcompy-server`.
|
|
13
|
+
|
|
14
|
+
## Documentation & Resources
|
|
15
|
+
|
|
16
|
+
- [Documentation](https://webcompy.net/documents/)
|
|
17
|
+
- [Testing Guide](https://webcompy.net/documents/server/testing/)
|
|
18
|
+
- [LLM Documentation Index (`llms.txt`)](https://webcompy.net/llms.txt) — bundles: [`llms-small.txt`](https://webcompy.net/llms-small.txt) / [`llms-full.txt`](https://webcompy.net/llms-full.txt)
|
|
19
|
+
- [Source Code & Discussions](https://github.com/kniwase/WebComPy)
|
|
20
|
+
|
|
21
|
+
## License
|
|
22
|
+
|
|
23
|
+
MIT License — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "webcompy-testing"
|
|
7
|
+
version = "0.0.14"
|
|
8
|
+
description = "WebComPy testing utilities (TestRenderer, fake ports, ASGI test client)"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.12"
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "Kento Niwase", email = "kento.niwase@gmail.com" },
|
|
15
|
+
]
|
|
16
|
+
keywords = ["browser", "frontend", "testing"]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
"Programming Language :: Python :: 3.14",
|
|
21
|
+
"Operating System :: OS Independent",
|
|
22
|
+
]
|
|
23
|
+
dependencies = [
|
|
24
|
+
"webcompy==0.0.14",
|
|
25
|
+
"webcompy-server==0.0.14",
|
|
26
|
+
"starlette",
|
|
27
|
+
"beautifulsoup4",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://github.com/kniwase/WebComPy"
|
|
32
|
+
Documentation = "https://webcompy.net/documents/"
|
|
33
|
+
"LLM Docs" = "https://webcompy.net/llms.txt"
|
|
34
|
+
|
|
35
|
+
[tool.hatch.build.targets.wheel]
|
|
36
|
+
packages = ["src/webcompy_testing"]
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Testing utilities for WebComPy applications.
|
|
2
|
+
|
|
3
|
+
LLM-friendly documentation is published at https://webcompy.net/llms.txt
|
|
4
|
+
(compact: https://webcompy.net/llms-small.txt, full: https://webcompy.net/llms-full.txt).
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from webcompy_server.ports import VirtualDOMEvent
|
|
10
|
+
from webcompy_testing._app import mock_app_run
|
|
11
|
+
from webcompy_testing._asgi import (
|
|
12
|
+
create_test_asgi_app,
|
|
13
|
+
format_html,
|
|
14
|
+
)
|
|
15
|
+
from webcompy_testing._asgi import (
|
|
16
|
+
render_app_html_sync as render_app_html,
|
|
17
|
+
)
|
|
18
|
+
from webcompy_testing._dom import FakeDOMNode
|
|
19
|
+
from webcompy_testing._ports import (
|
|
20
|
+
FakeAsyncSchedulerPort,
|
|
21
|
+
FakeBrowserDOMPort,
|
|
22
|
+
FakeBrowserFFIPort,
|
|
23
|
+
FakeBrowserHostPort,
|
|
24
|
+
FakeCustomElementPort,
|
|
25
|
+
FakeEventSourcePort,
|
|
26
|
+
FakeFetchPort,
|
|
27
|
+
FakeHistoryPort,
|
|
28
|
+
FakeMediaQueryPort,
|
|
29
|
+
FakeTransitionPort,
|
|
30
|
+
FakeWebSocketPort,
|
|
31
|
+
RecordedRequest,
|
|
32
|
+
)
|
|
33
|
+
from webcompy_testing._renderer import TestRenderer, TestRendererResult
|
|
34
|
+
from webcompy_testing._restore import restore_signal_values
|
|
35
|
+
from webcompy_testing._scope import create_test_app
|
|
36
|
+
from webcompy_testing._utils import run_sync
|
|
37
|
+
|
|
38
|
+
__all__ = [
|
|
39
|
+
"FakeAsyncSchedulerPort",
|
|
40
|
+
"FakeBrowserDOMPort",
|
|
41
|
+
"FakeBrowserFFIPort",
|
|
42
|
+
"FakeBrowserHostPort",
|
|
43
|
+
"FakeCustomElementPort",
|
|
44
|
+
"FakeDOMNode",
|
|
45
|
+
"FakeEventSourcePort",
|
|
46
|
+
"FakeFetchPort",
|
|
47
|
+
"FakeHistoryPort",
|
|
48
|
+
"FakeMediaQueryPort",
|
|
49
|
+
"FakeTransitionPort",
|
|
50
|
+
"FakeWebSocketPort",
|
|
51
|
+
"RecordedRequest",
|
|
52
|
+
"TestRenderer",
|
|
53
|
+
"TestRendererResult",
|
|
54
|
+
"VirtualDOMEvent",
|
|
55
|
+
"create_test_app",
|
|
56
|
+
"create_test_asgi_app",
|
|
57
|
+
"format_html",
|
|
58
|
+
"mock_app_run",
|
|
59
|
+
"render_app_html",
|
|
60
|
+
"restore_signal_values",
|
|
61
|
+
"run_sync",
|
|
62
|
+
]
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""Context manager for mocking ``WebComPyApp.run`` in tests."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Iterator
|
|
6
|
+
from contextlib import contextmanager
|
|
7
|
+
from typing import TYPE_CHECKING
|
|
8
|
+
|
|
9
|
+
from webcompy.app._app import WebComPyApp
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from collections.abc import Callable
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@contextmanager
|
|
17
|
+
def mock_app_run() -> Iterator[None]:
|
|
18
|
+
"""Suppress ``WebComPyApp.run`` inside the context.
|
|
19
|
+
|
|
20
|
+
Yields:
|
|
21
|
+
None: Control to the caller while ``WebComPyApp.run`` is replaced
|
|
22
|
+
with a no-op; the original method is restored on exit.
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
original: Callable[..., Any] = WebComPyApp.run
|
|
26
|
+
|
|
27
|
+
def _noop(self: object) -> None:
|
|
28
|
+
pass
|
|
29
|
+
|
|
30
|
+
WebComPyApp.run = _noop # type: ignore[method-assign]
|
|
31
|
+
try:
|
|
32
|
+
yield
|
|
33
|
+
finally:
|
|
34
|
+
WebComPyApp.run = original # type: ignore[method-assign]
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""ASGI test helpers for rendering WebComPy apps to HTML."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING, Any
|
|
6
|
+
|
|
7
|
+
from starlette.applications import Starlette
|
|
8
|
+
from starlette.responses import HTMLResponse
|
|
9
|
+
from starlette.routing import Route
|
|
10
|
+
|
|
11
|
+
from webcompy.di import inject
|
|
12
|
+
from webcompy.ports._keys import ASYNC_SCHEDULER_PORT_KEY
|
|
13
|
+
from webcompy_server import configure_server_context
|
|
14
|
+
from webcompy_server._html import _HtmlElement
|
|
15
|
+
from webcompy_testing._utils import run_sync
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from starlette.requests import Request
|
|
19
|
+
from starlette.types import ASGIApp
|
|
20
|
+
|
|
21
|
+
from webcompy.app._app import WebComPyApp
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def format_html(html: str) -> str:
|
|
25
|
+
"""Format raw HTML for readable output.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
html: Raw HTML string to format.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
Pretty-printed HTML string produced by BeautifulSoup.
|
|
32
|
+
|
|
33
|
+
"""
|
|
34
|
+
from bs4 import BeautifulSoup
|
|
35
|
+
|
|
36
|
+
soup = BeautifulSoup(html, "html.parser")
|
|
37
|
+
return str(soup)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
async def render_app_html(app: WebComPyApp, path: str = "/", **kwargs: Any) -> str:
|
|
41
|
+
from webcompy_server._html import generate_html
|
|
42
|
+
|
|
43
|
+
configure_server_context(app)
|
|
44
|
+
ctx = app.create_render_context(path)
|
|
45
|
+
try:
|
|
46
|
+
scheduler = inject(ASYNC_SCHEDULER_PORT_KEY)
|
|
47
|
+
await scheduler.await_pending()
|
|
48
|
+
return await generate_html(ctx, **kwargs)
|
|
49
|
+
finally:
|
|
50
|
+
ctx.dispose()
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def render_app_html_sync(app: WebComPyApp, path: str = "/", **kwargs: Any) -> str:
|
|
54
|
+
"""Render an app to an HTML string synchronously.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
app: Application to render.
|
|
58
|
+
path: Route path to render.
|
|
59
|
+
**kwargs: Additional options forwarded to ``generate_html``.
|
|
60
|
+
|
|
61
|
+
Returns:
|
|
62
|
+
Rendered HTML string for the requested path.
|
|
63
|
+
|
|
64
|
+
"""
|
|
65
|
+
return run_sync(render_app_html(app, path, **kwargs))
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def create_test_asgi_app(app: WebComPyApp) -> ASGIApp:
|
|
69
|
+
"""Create a Starlette ASGI app that renders the WebComPy app.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
app: Application to serve via the test ASGI app.
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
An ``ASGIApp`` that renders the WebComPy app for incoming requests.
|
|
76
|
+
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
configure_server_context(app)
|
|
80
|
+
|
|
81
|
+
if app.router_mode == "history" and app.routes:
|
|
82
|
+
|
|
83
|
+
async def _send_html(request: Request) -> HTMLResponse:
|
|
84
|
+
path: str = request.path_params.get("path", "")
|
|
85
|
+
ctx = app.create_render_context(path.strip("/"))
|
|
86
|
+
try:
|
|
87
|
+
scheduler = inject(ASYNC_SCHEDULER_PORT_KEY)
|
|
88
|
+
await scheduler.await_pending()
|
|
89
|
+
html = await _HtmlElement("div", {}, ctx._root).render_html()
|
|
90
|
+
return HTMLResponse(html)
|
|
91
|
+
finally:
|
|
92
|
+
ctx.dispose()
|
|
93
|
+
|
|
94
|
+
html_route = Route("/{path:path}", _send_html)
|
|
95
|
+
else:
|
|
96
|
+
|
|
97
|
+
async def _send_html_static(_: Request) -> HTMLResponse:
|
|
98
|
+
ctx = app.create_render_context("/")
|
|
99
|
+
try:
|
|
100
|
+
scheduler = inject(ASYNC_SCHEDULER_PORT_KEY)
|
|
101
|
+
await scheduler.await_pending()
|
|
102
|
+
html = await _HtmlElement("div", {}, ctx._root).render_html()
|
|
103
|
+
return HTMLResponse(html)
|
|
104
|
+
finally:
|
|
105
|
+
ctx.dispose()
|
|
106
|
+
|
|
107
|
+
html_route = Route("/", _send_html_static)
|
|
108
|
+
|
|
109
|
+
return Starlette(routes=[html_route])
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"""Fake DOM node for browserless component testing."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from webcompy.elements.types._base import _utf16_length
|
|
6
|
+
from webcompy_server.ports import VirtualDOMNode
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class FakeDOMNode(VirtualDOMNode):
|
|
10
|
+
"""Provide a test double for ``VirtualDOMNode`` with write tracking.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
tag: Tag name for the node.
|
|
14
|
+
text_content: Initial text content for text/comment nodes.
|
|
15
|
+
node_type: Explicit DOM node type override.
|
|
16
|
+
|
|
17
|
+
Attributes:
|
|
18
|
+
textContent_write_count: Number of times ``textContent`` was
|
|
19
|
+
written on this node.
|
|
20
|
+
setAttribute_count: Number of times ``setAttribute`` was called
|
|
21
|
+
on this node.
|
|
22
|
+
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(self, tag: str = "div", text_content: str | None = None, *, node_type: int | None = None) -> None:
|
|
26
|
+
if node_type is None:
|
|
27
|
+
node_type = 8 if tag.startswith("#comment") else (3 if tag.startswith("#text") else 1)
|
|
28
|
+
super().__init__(tag, node_type=node_type, text_content=text_content)
|
|
29
|
+
self.__webcompy_prerendered_node__: bool = False
|
|
30
|
+
self.textContent_write_count: int = 0
|
|
31
|
+
self.setAttribute_count: int = 0
|
|
32
|
+
|
|
33
|
+
def setAttribute(self, name: str, value: str | None) -> None:
|
|
34
|
+
"""Assign an attribute and increment the write counter.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
name: Attribute name.
|
|
38
|
+
value: Attribute value or ``None`` to remove the attribute.
|
|
39
|
+
|
|
40
|
+
"""
|
|
41
|
+
super().setAttribute(name, value)
|
|
42
|
+
self.setAttribute_count += 1
|
|
43
|
+
|
|
44
|
+
def splitText(self, offset: int) -> FakeDOMNode:
|
|
45
|
+
"""Split a text node at a UTF-16 offset and insert the remainder.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
offset: UTF-16 code-unit offset at which to split.
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
The newly created sibling text node containing the tail.
|
|
52
|
+
|
|
53
|
+
Raises:
|
|
54
|
+
TypeError: If the node is not a text node.
|
|
55
|
+
IndexError: If ``offset`` is out of range.
|
|
56
|
+
|
|
57
|
+
"""
|
|
58
|
+
if self._node_type != 3:
|
|
59
|
+
raise TypeError("splitText is only valid on text nodes")
|
|
60
|
+
text = self._text_content or ""
|
|
61
|
+
length = _utf16_length(text)
|
|
62
|
+
if not 0 <= offset <= length:
|
|
63
|
+
raise IndexError(f"splitText offset {offset} out of range for text of length {length}")
|
|
64
|
+
encoded = text.encode("utf-16-le", "surrogatepass")
|
|
65
|
+
boundary = offset * 2
|
|
66
|
+
new_node = FakeDOMNode(
|
|
67
|
+
"#text",
|
|
68
|
+
text_content=encoded[boundary:].decode("utf-16-le", "surrogatepass"),
|
|
69
|
+
)
|
|
70
|
+
self._text_content = encoded[:boundary].decode("utf-16-le", "surrogatepass")
|
|
71
|
+
parent = self._parent
|
|
72
|
+
if parent is not None:
|
|
73
|
+
siblings = parent._children
|
|
74
|
+
idx = siblings.index(self)
|
|
75
|
+
if idx + 1 < len(siblings):
|
|
76
|
+
parent.insertBefore(new_node, siblings[idx + 1])
|
|
77
|
+
else:
|
|
78
|
+
parent.appendChild(new_node)
|
|
79
|
+
return new_node
|
|
80
|
+
|
|
81
|
+
@VirtualDOMNode.textContent.setter # type: ignore[attr-defined]
|
|
82
|
+
def textContent(self, value: str | None) -> None:
|
|
83
|
+
VirtualDOMNode.textContent.fset(self, value) # type: ignore[misc]
|
|
84
|
+
self.textContent_write_count += 1
|
|
85
|
+
|
|
86
|
+
def __setattr__(self, name: str, value: object) -> None:
|
|
87
|
+
if name == "__webcompy_prerendered_node__" and value:
|
|
88
|
+
object.__setattr__(self, "_webcompy_node", False)
|
|
89
|
+
if name == "innerHTML":
|
|
90
|
+
VirtualDOMNode.__setattr__(self, name, value)
|
|
91
|
+
return
|
|
92
|
+
if (
|
|
93
|
+
name.startswith("_VirtualDOMNode__")
|
|
94
|
+
or name in ("__webcompy_node__", "__webcompy_prerendered_node__")
|
|
95
|
+
or name in ("textContent_write_count", "setAttribute_count")
|
|
96
|
+
or name
|
|
97
|
+
in {
|
|
98
|
+
"nodeName",
|
|
99
|
+
"nodeType",
|
|
100
|
+
"textContent",
|
|
101
|
+
"childNodes",
|
|
102
|
+
"firstChild",
|
|
103
|
+
"lastChild",
|
|
104
|
+
"parentNode",
|
|
105
|
+
"attributes",
|
|
106
|
+
"innerHTML",
|
|
107
|
+
"outerHTML",
|
|
108
|
+
}
|
|
109
|
+
or name.startswith("_")
|
|
110
|
+
):
|
|
111
|
+
object.__setattr__(self, name, value)
|
|
112
|
+
else:
|
|
113
|
+
super().__setattr__(name, value)
|
|
114
|
+
|
|
115
|
+
def __getattribute__(self, name: str) -> object:
|
|
116
|
+
if name == "innerHTML":
|
|
117
|
+
try:
|
|
118
|
+
return object.__getattribute__(self, "_innerHTML")
|
|
119
|
+
except AttributeError:
|
|
120
|
+
return None
|
|
121
|
+
return object.__getattribute__(self, name)
|
|
122
|
+
|
|
123
|
+
def focus(self) -> None:
|
|
124
|
+
"""Simulate focusing this node by updating the containing document's active element.
|
|
125
|
+
|
|
126
|
+
The active element is tracked on the nearest ancestor that holds
|
|
127
|
+
``__webcompy_document_root__`` (the fake document).
|
|
128
|
+
|
|
129
|
+
"""
|
|
130
|
+
node: object | None = self
|
|
131
|
+
while node is not None:
|
|
132
|
+
if isinstance(node, VirtualDOMNode):
|
|
133
|
+
parent = node.parentNode
|
|
134
|
+
if getattr(node, "__webcompy_document_root__", False):
|
|
135
|
+
object.__setattr__(node, "_fake_active_element", self)
|
|
136
|
+
return
|
|
137
|
+
node = parent
|
|
138
|
+
else:
|
|
139
|
+
break
|
|
140
|
+
|
|
141
|
+
def __getattr__(self, name: str) -> object:
|
|
142
|
+
if name.startswith("_VirtualDOMNode__"):
|
|
143
|
+
raise AttributeError(name)
|
|
144
|
+
if name == "isConnected":
|
|
145
|
+
return self.isConnected
|
|
146
|
+
try:
|
|
147
|
+
return self._dom_properties[name]
|
|
148
|
+
except KeyError:
|
|
149
|
+
return object.__getattribute__(self, name)
|