diff options
| author | Christophe Besson <cbesson@gmail.com> | 2026-09-04 03:58:16 +0200 |
|---|---|---|
| committer | Christophe Besson <cbesson@gmail.com> | 2026-09-04 03:58:16 +0200 |
| commit | 220e6e701806213a576ce80fa655cd9cf4a51880 (patch) | |
| tree | 04b1f059044f4e02d1e8cb6c4a6588982cf395c4 /packages/meshbay-node/tests | |
| parent | 5098e6cb54173b27673f5761ce187d799ba36b30 (diff) | |
| download | meshbay-220e6e701806213a576ce80fa655cd9cf4a51880.tar.gz | |
feat: Windows daemon lifecycle (W3) — Startup-folder autostart
The Linux node runs under `systemctl --user`. Windows has no per-user
equivalent that works without elevation: `schtasks /create /sc ONLOGON`
(even `/rl LIMITED /it`) fails with "Access is denied" for a non-admin
user, because a logon trigger touches machine-wide scheduler state.
So autostart is a `.vbs` in the per-user Startup folder instead:
CreateObject("WScript.Shell").Run Chr(34) & "<exe>" & Chr(34), 0, False
wscript runs it at every sign-in, hidden (0) and non-blocking. No admin,
no console window, no new dependency. Verified end to end: the launcher
brings the daemon up with no window and it answers its loopback API.
node/platform.py
autostart_install/remove/status — write / delete / detect the launcher
autostart_run/end — start now (DETACHED|NO_WINDOW) / taskkill
_node_exe — PATH, then next to sys.executable, then argv[0]
node/daemon.py
new `autostart install|remove|start|stop|status` verb
reload (win32) -> POST /api/reload on the loopback API
restart-daemon (win32) -> autostart_end + autostart_run
reset (win32) -> also removes the launcher
client/main.js, preload.js
node:autostart handler + winAutostart* helpers (kept in step with platform.py)
node:service-status (win32) probes the daemon; stop/restart/start use
taskkill + a detached, windowless spawn
Tests: 8 autostart cases in test_platform.py (mocked sys.platform, APPDATA
pointed at tmp); `autostart status` added to the CLI dispatch sweep. Full
meshbay-node suite green on Windows (784 passed / 34 skipped).
Still open: no CTRL_CLOSE_EVENT handler, so a bare taskkill / window close
does not run _shutdown() (SetConsoleCtrlHandler, follow-up). Service mode
(pywin32/NSSM) stays Phase 2.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Diffstat (limited to 'packages/meshbay-node/tests')
| -rw-r--r-- | packages/meshbay-node/tests/test_cli_dispatch.py | 7 | ||||
| -rw-r--r-- | packages/meshbay-node/tests/test_platform.py | 75 |
2 files changed, 80 insertions, 2 deletions
diff --git a/packages/meshbay-node/tests/test_cli_dispatch.py b/packages/meshbay-node/tests/test_cli_dispatch.py index 76eb6d5..606e586 100644 --- a/packages/meshbay-node/tests/test_cli_dispatch.py +++ b/packages/meshbay-node/tests/test_cli_dispatch.py @@ -42,6 +42,7 @@ VERBS = [ ["stun", "list"], ["reload"], ["restart-daemon"], + ["autostart", "status"], ] @@ -141,7 +142,8 @@ def test_the_verb_list_here_matches_the_parser(): @pytest.mark.skipif(sys.platform == "win32", - reason="systemd lifecycle; Windows path (schtasks) is W3, not built") + reason="systemd lifecycle; Windows uses the loopback API / " + "Startup-folder launcher, covered by test_platform.py") @pytest.mark.parametrize("argv,verb", [ (["reload"], "reload"), (["restart-daemon"], "restart"), @@ -170,7 +172,8 @@ def test_lifecycle_commands_delegate_to_systemctl_user( @pytest.mark.skipif(sys.platform == "win32", - reason="systemd lifecycle; Windows path (schtasks) is W3, not built") + reason="systemd lifecycle; Windows uses the loopback API / " + "Startup-folder launcher, covered by test_platform.py") @pytest.mark.parametrize("argv", [["reload"], ["restart-daemon"]]) def test_lifecycle_commands_report_systemctl_failure( argv, stub_daemon, monkeypatch, capsys): diff --git a/packages/meshbay-node/tests/test_platform.py b/packages/meshbay-node/tests/test_platform.py index 9358894..f9464c3 100644 --- a/packages/meshbay-node/tests/test_platform.py +++ b/packages/meshbay-node/tests/test_platform.py @@ -102,3 +102,78 @@ def test_configure_event_loop_selector_opt_in(monkeypatch): plat.configure_event_loop() assert isinstance(asyncio.get_event_loop_policy(), asyncio.WindowsSelectorEventLoopPolicy) + + +# ── Autostart ──────────────────────────────────────────────────────────────── +# +# On Windows autostart is a `.vbs` in the per-user Startup folder (a logon task +# would need elevation). Tests point APPDATA at a tmp dir so the real Startup +# folder is never touched, and force sys.platform. + +@pytest.fixture +def win_startup(monkeypatch, tmp_path): + monkeypatch.setattr(sys, "platform", "win32") + monkeypatch.setenv("APPDATA", str(tmp_path)) + return (tmp_path / "Microsoft" / "Windows" / "Start Menu" / "Programs" + / "Startup" / "MeshBay Node.vbs") + + +def test_autostart_supported_tracks_the_platform(monkeypatch): + monkeypatch.setattr(sys, "platform", "win32") + assert plat.autostart_supported() is True + monkeypatch.setattr(sys, "platform", "linux") + assert plat.autostart_supported() is False + + +def test_autostart_status_is_inert_off_windows(monkeypatch): + monkeypatch.setattr(sys, "platform", "linux") + assert plat.autostart_status() == {"installed": False, "state": ""} + + +def test_autostart_install_writes_a_hidden_launcher_in_the_startup_folder(win_startup): + exe = r"C:\Program Files\MeshBay\meshbay-node.exe" + plat.autostart_install(exe=exe) + + assert win_startup.exists() + assert plat.autostart_status() == {"installed": True, "state": ""} + raw = win_startup.read_bytes() + assert b"\r\n" in raw and b"\n\n" not in raw # CRLF, no stray LF + text = raw.decode("utf-8") + assert f'"{exe}"' in text # path is quote-wrapped + assert "Chr(34)" in text and ", 0, False" in text # hidden, non-blocking + + +def test_autostart_remove_deletes_the_launcher_and_is_idempotent(win_startup): + plat.autostart_install(exe=r"C:\x\meshbay-node.exe") + assert win_startup.exists() + plat.autostart_remove() + assert not win_startup.exists() + plat.autostart_remove() # no error second time + + +def test_autostart_install_needs_a_locatable_launcher(win_startup, monkeypatch): + monkeypatch.setattr(plat, "_node_exe", lambda: None) + with pytest.raises(RuntimeError, match="locate the meshbay-node launcher"): + plat.autostart_install() + + +def test_autostart_install_refuses_off_windows(monkeypatch): + monkeypatch.setattr(sys, "platform", "linux") + with pytest.raises(RuntimeError, match="Windows-only"): + plat.autostart_install(exe="/usr/bin/meshbay-node") + + +def test_autostart_run_launches_the_resolved_exe_detached(win_startup, monkeypatch): + monkeypatch.setattr(plat, "_node_exe", lambda: r"C:\x\meshbay-node.exe") + calls = {} + monkeypatch.setattr(plat.subprocess, "Popen", + lambda argv, **kw: calls.update(argv=argv, kw=kw)) + plat.autostart_run() + assert calls["argv"] == [r"C:\x\meshbay-node.exe"] + assert calls["kw"]["creationflags"] & 0x08000000 # CREATE_NO_WINDOW + + +def test_autostart_run_refuses_off_windows(monkeypatch): + monkeypatch.setattr(sys, "platform", "linux") + with pytest.raises(RuntimeError, match="Windows-only"): + plat.autostart_run() |