Files
computer-playbook/tests/unit/python/roles/test-e2e-playwright/test_timeouts_helper.py
T
kevinveenbirkenbachandClaude Opus 5.5 6f8dcc40cc feat(test-e2e-playwright): run each role on one of its served networks
When a node serves a role on several networks, the runner picks one of the
role's vhosts at random and keeps that pick for the flake retry. The .env is
rendered for the picked vhost, so APP_BASE_URL follows its network, and
CANONICAL_DOMAIN moves to its sibling there. Reports of a pick outside the
canonical network land under <application_id>+<network>.

network-leak.spec.js is staged next to every role spec and skipped unless the
role is served on more than one network. It fails when a request leaves the
network of the document that issued it, or when the browser blocks mixed
content. Only the SSO issuer may cross networks, so an onion pick reaches it
past the Tor proxy.

Onion detection reads the host of APP_BASE_URL before CANONICAL_DOMAIN, so
roles without CANONICAL_DOMAIN also get the onion timeouts and Tor retries.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-24 21:59:07 +02:00

141 lines
4.1 KiB
Python

"""Unit tests for roles/test-e2e-playwright/files/timeouts.js.
The helper is dependency-free plain CommonJS. We copy it into a temp dir
and call it from a short Node script with controlled env, then assert the
computed timeout / onion detection.
"""
import os
import shutil
import subprocess
import tempfile
import textwrap
import unittest
from pathlib import Path
from utils.domains.default_primary import default_domain_primary
from . import PROJECT_ROOT
HELPER_PATH = str(
PROJECT_ROOT / "roles" / "test-e2e-playwright" / "files" / "timeouts.js"
)
DOMAIN = default_domain_primary()
def _have_node():
return shutil.which("node") is not None
@unittest.skipUnless(_have_node(), "node is not available in PATH")
class TestTimeoutsHelper(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp(prefix="timeouts-helper-")
shutil.copy(HELPER_PATH, str(Path(self.tmpdir) / "timeouts.js"))
def tearDown(self):
shutil.rmtree(self.tmpdir, ignore_errors=True)
def _run(self, expr, env):
script = textwrap.dedent(
f"""
const t = require("./timeouts");
process.stdout.write("RESULT:" + JSON.stringify({expr}) + "\\n");
"""
)
script_path = str(Path(self.tmpdir) / "run.js")
with Path(script_path).open("w") as f:
f.write(script)
proc = subprocess.run(
["node", "run.js"],
capture_output=True,
text=True,
env={**os.environ, **env},
cwd=self.tmpdir,
timeout=10,
check=False,
)
self.assertEqual(
proc.returncode, 0, msg=f"stderr={proc.stderr}\nstdout={proc.stdout}"
)
return proc.stdout
def test_clearnet_default_factor_is_identity(self):
out = self._run(
"t.resolveTimeout(60000)",
{"CANONICAL_DOMAIN": f'"x.{DOMAIN}"'},
)
self.assertIn("RESULT:60000", out)
def test_onion_applies_default_multiplier(self):
out = self._run(
"t.resolveTimeout(60000)",
{"CANONICAL_DOMAIN": '"x.abc123.onion"'},
)
self.assertIn("RESULT:300000", out)
def test_global_factor_scales_clearnet(self):
out = self._run(
"t.resolveTimeout(60000)",
{
"CANONICAL_DOMAIN": f"x.{DOMAIN}",
"PLAYWRIGHT_TIMEOUT_FACTOR": "2",
},
)
self.assertIn("RESULT:120000", out)
def test_factor_and_onion_multiplier_compose(self):
out = self._run(
"t.resolveTimeout(60000)",
{
"CANONICAL_DOMAIN": '"x.abc123.onion"',
"PLAYWRIGHT_TIMEOUT_FACTOR": "2",
"PLAYWRIGHT_ONION_TIMEOUT_MULTIPLIER": "3",
},
)
self.assertIn("RESULT:360000", out)
def test_invalid_factor_falls_back_to_one(self):
out = self._run(
"t.resolveTimeout(60000)",
{
"CANONICAL_DOMAIN": f"x.{DOMAIN}",
"PLAYWRIGHT_TIMEOUT_FACTOR": "0",
},
)
self.assertIn("RESULT:60000", out)
def test_is_onion_target_detects_quoted_and_bare(self):
self.assertIn(
"RESULT:true",
self._run("t.isOnionTarget()", {"CANONICAL_DOMAIN": '"x.abc.onion"'}),
)
self.assertIn(
"RESULT:true",
self._run("t.isOnionTarget()", {"CANONICAL_DOMAIN": "x.abc.onion"}),
)
self.assertIn(
"RESULT:false",
self._run("t.isOnionTarget()", {"CANONICAL_DOMAIN": f'"x.{DOMAIN}"'}),
)
def test_is_onion_target_reads_app_base_url_first(self):
self.assertIn(
"RESULT:true",
self._run("t.isOnionTarget()", {"APP_BASE_URL": '"https://x.abc.onion/"'}),
)
self.assertIn(
"RESULT:false",
self._run(
"t.isOnionTarget()",
{
"APP_BASE_URL": f'"https://x.{DOMAIN}/"',
"CANONICAL_DOMAIN": '"x.abc.onion"',
},
),
)
if __name__ == "__main__":
unittest.main()