Files
nanobot/tests/cli/test_wsl_browser.py
T
chengyongru 41f2eeb7ba fix(webui): support WSL browser launch and clearer startup output (#6150)
* feat(webui): add local first-run access setup

* feat(webui): defer password setup until network access

Keep generated local credentials and provide the full browser login link in every launch mode. Require a chosen password when enabling network access, preserve existing authentication, and show pending listener changes until restart.

Remove mandatory first-run initialization gates and simplify startup output while retaining gateway warnings and errors.

* fix(webui): align access settings with channel contracts

* fix(webui): clarify startup guidance and spacing

* fix(webui): keep setup guidance readable in narrow terminals

* fix(webui): clarify startup and preserve remote password access

* fix(webui): move access explanations into settings hints

* fix(webui): require strong ASCII passwords and reject malformed auth

* fix(webui): simplify access password character rules

* test(webui): reflect pending restart after password save

* fix(webui): open Windows browser from WSL

* fix(webui): clarify public access copy

* refactor(webui): focus on WSL launch and startup output

* fix(webui): preserve platform browser fallback
2026-10-12 10:53:11 +08:00

60 lines
2.8 KiB
Python

"""Exercise the Windows file boundary used by the WSL browser handoff."""
import base64
import os
import shutil
import subprocess
import pytest
from nanobot.cli.windows_browser import _WSL_OPEN_SCRIPT, _redirect_html
@pytest.mark.skipif(
not shutil.which("powershell.exe") or not (os.name == "nt" or os.environ.get("WSL_DISTRO_NAME")),
reason="Requires Windows PowerShell, natively or through WSL",
)
@pytest.mark.parametrize("opens", [True, False])
def test_windows_handoff_file_is_private_readable_and_removed(opens):
# Keep the native filesystem and helper lifecycle; replace only desktop
# activation so this regression never opens a user's browser during pytest.
script = _WSL_OPEN_SCRIPT.replace(
"return ShellExecuteEx(ref info);", f"return {str(opens).lower()};",
).replace("Start-Sleep -Seconds 120", "Start-Sleep -Milliseconds 30")
check = r'''
$acl = $guard.GetAccessControl()
$rules = $acl.GetAccessRules($true, $true, [Security.Principal.SecurityIdentifier])
if (-not $acl.AreAccessRulesProtected -or $rules.Count -ne 1 -or
$acl.GetOwner([Security.Principal.SecurityIdentifier]) -ne $user -or
$rules[0].IdentityReference -ne $user -or $rules[0].IsInherited -or
$rules[0].AccessControlType -ne [Security.AccessControl.AccessControlType]::Allow) {
throw 'Private ACL missing'
}
$reader = [IO.File]::OpenRead($path)
try { if ($reader.Length -ne $content.Length) { throw 'Incomplete redirect' } }
finally { $reader.Dispose() }
$blocked = $false
try { [IO.File]::Delete($path) } catch [IO.IOException] { $blocked = $true }
if (-not $blocked) { throw 'Active redirect was not protected from deletion' }
$blocked = $false
try { [IO.File]::OpenWrite($path).Dispose() } catch [IO.IOException] { $blocked = $true }
if (-not $blocked) { throw 'Active redirect was writable' }
[Console]::Write('private;')
'''
script = script.replace(" if (-not [NanobotBrowser]::Open($path))", check + " if (-not [NanobotBrowser]::Open($path))")
script = script.replace(
" if ($owned) { try { [IO.File]::Delete($path) } catch {} }",
" if ($owned) { [IO.File]::Delete($path) }\n"
" [Console]::Write('removed=' + (-not [IO.File]::Exists($path)))",
)
content = _redirect_html('http://localhost/#/?bootstrapSecret="/><script>$(test)&中文</script>')
result = subprocess.run(
["powershell.exe", "-NoProfile", "-NonInteractive", "-STA", "-EncodedCommand",
base64.b64encode(script.encode("utf-16-le")).decode("ascii")],
input=base64.b64encode(content) + b"\n", capture_output=True, timeout=15,
)
assert result.returncode == (0 if opens else 1), result.stderr
assert result.stdout == b"private;" + (b"1" if opens else b"") + b"removed=True"