-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_runtime_ui_slice11.py
More file actions
158 lines (137 loc) · 6.96 KB
/
test_runtime_ui_slice11.py
File metadata and controls
158 lines (137 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from __future__ import annotations
import json
import urllib.parse
import urllib.request
from Implementations.Reference.Runtime.python.cli import execute_example11_contract
from Implementations.Reference.Runtime.python.ui_runtime import (
ButtonBrowserUiRuntime,
ButtonRuntimeCore,
build_runtime,
default_example11_contract_path,
default_example11_wfrog_path,
)
def test_python_example11_headless_button_switch_when_pressed() -> None:
runtime = ButtonRuntimeCore(
contract_path=default_example11_contract_path(),
wfrog_path=default_example11_wfrog_path(),
)
artifact = runtime.execution_artifact()
assert artifact["contract_ref"]["source_ref"]["example_id"] == "11_button_switch_when_pressed"
assert artifact["outputs"]["public"]["switched"] is False
assert artifact["outputs"]["ui"]["trigger_button"] is False
assert artifact["outputs"]["ui"]["switched_indicator"] is False
artifact = runtime.execute(True)
assert artifact["execution_summary"]["mode"] == "button_switch_when_pressed"
assert artifact["execution_summary"]["trigger_pressed"] is True
assert artifact["outputs"]["public"]["switched"] is True
assert artifact["outputs"]["ui"]["trigger_button"] is True
assert artifact["outputs"]["ui"]["switched_indicator"] is True
artifact = runtime.execute(False)
assert artifact["execution_summary"]["trigger_pressed"] is False
assert artifact["outputs"]["public"]["switched"] is True
assert artifact["outputs"]["ui"]["trigger_button"] is True
assert artifact["outputs"]["ui"]["switched_indicator"] is True
artifact = runtime.execute(True)
assert artifact["execution_summary"]["trigger_pressed"] is True
assert artifact["outputs"]["public"]["switched"] is False
assert artifact["outputs"]["ui"]["trigger_button"] is False
assert artifact["outputs"]["ui"]["switched_indicator"] is False
def test_python_example11_cli_contract_entrypoint() -> None:
artifact = execute_example11_contract(True)
assert artifact["outputs"]["public"]["switched"] is True
assert artifact["outputs"]["ui"]["switched_indicator"] is True
assert artifact["outputs"]["ui"]["trigger_button"] is True
def test_python_example11_browser_ui_consumes_default_svg_and_source_styles() -> None:
runtime = ButtonBrowserUiRuntime(
contract_path=default_example11_contract_path(),
wfrog_path=default_example11_wfrog_path(),
open_browser=False,
)
html = runtime.render_html()
assert "Button Switch When Pressed" in html
assert "Example 11 - .frog switch_when_pressed Button value" in html
assert "Python reference runtime" in html
assert "button switch contract executor" in html
assert 'data-execution-path="python_button_switch_when_pressed_contract_executor"' in html
assert "data-widget-id='trigger_button'" in html
assert "data-widget-id='switched_indicator'" in html
assert "data-widget-id='pressed_indicator'" not in html
assert "data-class-ref='frog.widgets.button'" in html
assert "data-class-ref='frog.widgets.boolean_indicator'" in html
assert "data-asset-route='/asset/button_rectangular_svg'" in html
assert "data-asset-route='/asset/boolean_circular_svg'" in html
assert "data-frog-template=\"frog.realizations.default.button.rectangular\"" in html
assert "data-frog-asset-consumed='true'" in html
assert "data-frog-visual-law='wfrog-realization-state-map'" in html
assert "data-frog-mechanical-action='switch_when_pressed'" in html
assert "data-frog-pressed-applies-when-value-true='true'" in html
assert "data-frog-pressed-applies-while-active='false'" in html
assert "data-frog-hover-applies-when-value-false-only='false'" in html
assert "data-frog-part='face' data-frog-event='pressed' data-frog-public-input-id='trigger_value'" in html
assert "name='trigger_value' value='true'" in html
assert "--frog-button-face-fill:#e2e8f0;" in html
assert "--frog-button-face-hover-fill:#f1f5f9;" in html
assert "--frog-button-face-pressed-fill:#e2e8f0;" in html
assert "--frog-button-face-stroke-width:1px;" in html
assert "--frog-button-pressed-inset:0px;" in html
assert "--frog-button-state-text-font-weight:400;" in html
assert "--boolean-text-font-weight:400;" in html
assert ".boolean-indicator[data-class-ref='frog.widgets.boolean_indicator']" in html
assert 'mechanicalAction !== "switch_when_pressed"' in html
assert 'publishEvent("press")' in html
assert 'publishEvent("release")' in html
assert "fetch(\"/event\"" in html
assert "pointerdown" in html
assert ">OFF</span>" in html
assert ">FALSE</span>" in html
assert "fallback" not in html
assert "type='checkbox'" not in html
assert "type='submit'" not in html
assert "Current runtime snapshot" not in html
assert "<pre>" not in html
runtime.run_once(True)
html = runtime.render_html()
assert "data-frog-visual-state='true'" in html
assert "aria-pressed='true'" in html
assert ">ON</span>" in html
assert ">TRUE</span>" in html
def test_python_example11_event_endpoint_toggles_stored_value() -> None:
runtime = ButtonBrowserUiRuntime(
contract_path=default_example11_contract_path(),
wfrog_path=default_example11_wfrog_path(),
open_browser=False,
)
httpd, thread = runtime.serve_in_thread()
host, port = httpd.server_address
try:
body = urllib.parse.urlencode({"frog_event": "press", "trigger_value": "false"}).encode("utf-8")
request = urllib.request.Request(
f"http://{host}:{port}/event",
data=body,
headers={"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"},
method="POST",
)
with urllib.request.urlopen(request, timeout=5) as response:
artifact = json.loads(response.read().decode("utf-8"))
assert artifact["outputs"]["public"]["switched"] is True
assert artifact["outputs"]["ui"]["trigger_button"] is True
assert artifact["outputs"]["ui"]["switched_indicator"] is True
body = urllib.parse.urlencode({"frog_event": "press", "trigger_value": "true"}).encode("utf-8")
request = urllib.request.Request(
f"http://{host}:{port}/event",
data=body,
headers={"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"},
method="POST",
)
with urllib.request.urlopen(request, timeout=5) as response:
artifact = json.loads(response.read().decode("utf-8"))
assert artifact["outputs"]["public"]["switched"] is False
assert artifact["outputs"]["ui"]["trigger_button"] is False
assert artifact["outputs"]["ui"]["switched_indicator"] is False
finally:
httpd.shutdown()
httpd.server_close()
thread.join(timeout=5)
def test_python_runtime_builder_dispatches_example11() -> None:
runtime = build_runtime(example="11", open_browser=False)
assert isinstance(runtime, ButtonBrowserUiRuntime)