okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

nekomimi.petfix(browser): prefer pinned headless shell for automationf67dd6d

main
3.0 KiB83 linesraw
1"""Browser discovery through the real kernel, without starting a GUI app."""
2
3import unittest
4
5from harness import Albedo, Provider, exclusive, python, text
6
7
8@exclusive
9class BrowserTests(unittest.TestCase):
10    def test_headless_shell_preserves_overrides_headed_launches_and_fallback(self):
11        code = ""
12
13        def script(request):
14            if request["messages"][-1].get("role") == "user":
15                return python(code)
16            return text("done")
17
18        def prepare(app):
19            nonlocal code
20            bin_dir = app.workspace / "bin"
21            bin_dir.mkdir()
22            chrome = bin_dir / "chromium"
23            shell = bin_dir / "headless-shell"
24            shell_link = app.home / "browsers/chrome-headless-shell"
25            shell_link.parent.mkdir()
26            shell_link.symlink_to(shell)
27            for executable in (chrome, shell):
28                executable.write_text(
29                    '#!/bin/sh\necho "test browser exited" >&2\nexit 23\n'
30                )
31                executable.chmod(0o755)
32            for variable in (
33                "ALBEDO_BROWSER_EXECUTABLE",
34                "PRIME_BROWSER_EXECUTABLE",
35                "CHROME_PATH",
36            ):
37                app.daemon.env[variable] = ""
38
39            code = f"""
40from pathlib import Path
41from albedo_plugins.browser import LaunchError
42chrome = {str(chrome)!r}
43shell = {str(shell)!r}
44os.environ['PATH'] = {str(bin_dir)!r}
45async def check(expected, **options):
46    try:
47        b = await browser.spawn(sandbox=False, timeout=3, **options)
48    except LaunchError as error:
49        assert error.details['executable'] == expected, error.to_dict()
50        assert error.details['reason'] == 'process_exited', error.to_dict()
51        assert 'test browser exited' in error.details['stderr_tail'], error.to_dict()
52        assert not Path(error.details['profile']).exists()
53    else:
54        await b.close()
55        raise AssertionError('test browser should exit during startup')
56await check(shell)
57path_shell = Path({str(bin_dir / "chrome-headless-shell")!r})
58path_shell.symlink_to(chrome)
59await check(chrome)
60path_shell.unlink()
61await check(chrome, headless=False)
62await check(chrome, executable=chrome)
63os.environ['ALBEDO_BROWSER_EXECUTABLE'] = chrome
64await check(chrome)
65del os.environ['ALBEDO_BROWSER_EXECUTABLE']
66Path({str(shell_link)!r}).unlink()
67await check(chrome)
68try:
69    await browser.spawn(executable={str(shell_link)!r}, sandbox=False)
70except LaunchError as error:
71    assert error.details['reason'] == 'executable_not_found', error.to_dict()
72else:
73    raise AssertionError('missing explicit browser must not fall back')
74Path({str(app.workspace / "passed")!r}).write_text('passed')
75"""
76
77        provider = Provider(script)
78        self.addCleanup(provider.close)
79        with Albedo(provider, prepare=prepare) as app:
80            session = app.session()
81            app.prompt(session, "try the installed browsers").close()
82            app.idle(session)
83            self.assertTrue((app.workspace / "passed").exists(), app.history(session))