From c2eade6db582966fa7fc3dd037f952baf3ae1cb5 Mon Sep 17 00:00:00 2001 From: Christophe Besson Date: Fri, 4 Sep 2026 14:33:33 +0200 Subject: fix(packaging): actually ship the TMDB token, on Linux and Windows default.env was empty in every build, for three independent reasons: 1. build-node.sh read QE/node.env, which does not exist. Even pointed at the real file it would have failed: its `grep MESHBAY_TMDB_DEFAULT_TOKEN=` cannot match QE/tmdb.txt, which is a free-form note, not KEY=VALUE. 2. Nothing consumed default.env. packaging/README.md and build-node.sh both claimed `meshbay-node init` copies it to /node.env; grep found the name in exactly two places, the README and the script that writes it. No code implemented the copy, and `EnvironmentFile=-` hid the absence. 3. build-win.ps1 had no env handling at all, so Windows was empty for a different reason than Linux. Now: the build extracts the v4 read token -- tmdb.py sends `Authorization: Bearer`, so it is the JWT, not the 32-char v3 key beside it in the same file -- matching KEY=VALUE first and then by shape, from MESHBAY_TMDB_TOKEN, MESHBAY_TMDB_TOKEN_FILE, QE/node.env, QE/tmdb.txt. It writes default.env 0600 and *fails the build* if no token resolves; MESHBAY_ALLOW_NO_TMDB=1 opts out. An empty default.env is invisible until a user opens Videos and finds no metadata, which is how this shipped empty on two platforms at once. platform.py gains packaged_default_env()/install_node_env()/load_node_env(). init copies the packaged file once, never overwriting an existing node.env, and the daemon loads node.env itself at startup: systemd does this on Linux via EnvironmentFile, but Windows autostart is a Startup-folder .vbs with no equivalent. Already-set variables always win. Also fixes an UnboundLocalError in main(): `config_dir` was assigned at the top of the init branch, which made it function-local for all of main(), while the reset branch calls `config_dir()` as the imported function. init returns before that line, so `meshbay-node reset` could only ever raise. The local is now cfg_dir. Verified end to end on Linux: token baked (239 chars), init writes /node.env 0600 with it. The PowerShell half is written but unrun -- no pwsh on this machine. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01DtfG7z6wHWj8RKHCvxQtY1 --- packages/meshbay-node/tests/test_platform.py | 80 ++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) (limited to 'packages/meshbay-node/tests/test_platform.py') diff --git a/packages/meshbay-node/tests/test_platform.py b/packages/meshbay-node/tests/test_platform.py index f9464c3..7042afb 100644 --- a/packages/meshbay-node/tests/test_platform.py +++ b/packages/meshbay-node/tests/test_platform.py @@ -5,6 +5,7 @@ here by monkeypatching that (and `os.environ`) rather than only on the OS the suite happens to run on. """ +import os import asyncio import sys from pathlib import Path @@ -177,3 +178,82 @@ def test_autostart_run_refuses_off_windows(monkeypatch): monkeypatch.setattr(sys, "platform", "linux") with pytest.raises(RuntimeError, match="Windows-only"): plat.autostart_run() + + +# ── Packaged defaults ──────────────────────────────────────────────────────── + +def test_frozen_build_finds_default_env_beside_the_executable(monkeypatch, tmp_path): + """Where build-node-runtime.ps1 puts it, alongside ffmpeg.""" + exe = tmp_path / "meshbay-node.exe" + exe.write_bytes(b"") + (tmp_path / "default.env").write_text("MESHBAY_TMDB_DEFAULT_TOKEN=eyJtest\n") + monkeypatch.setattr(sys, "frozen", True, raising=False) + monkeypatch.setattr(sys, "executable", str(exe)) + assert plat.packaged_default_env() == tmp_path / "default.env" + + +def test_source_checkout_has_no_packaged_default(monkeypatch, tmp_path): + monkeypatch.setattr(sys, "frozen", False, raising=False) + monkeypatch.setattr(sys, "executable", str(tmp_path / "python")) + monkeypatch.setattr(plat, "Path", Path) + # /opt/meshbay-node/share/default.env is absent on a dev machine + assert plat.packaged_default_env() is None + + +def test_install_node_env_copies_once(monkeypatch, tmp_path): + src = tmp_path / "default.env" + src.write_text("MESHBAY_TMDB_DEFAULT_TOKEN=eyJfirst\n") + monkeypatch.setattr(plat, "packaged_default_env", lambda: src) + cfg = tmp_path / "config" + cfg.mkdir() + + written = plat.install_node_env(cfg) + assert written == cfg / "node.env" + assert "eyJfirst" in written.read_text() + + +def test_install_node_env_never_overwrites_operator_values(monkeypatch, tmp_path): + """An existing node.env holds the operator's own token; clobbering it would + silently downgrade a configured node to the shared default.""" + src = tmp_path / "default.env" + src.write_text("MESHBAY_TMDB_DEFAULT_TOKEN=eyJpackaged\n") + monkeypatch.setattr(plat, "packaged_default_env", lambda: src) + cfg = tmp_path / "config" + cfg.mkdir() + (cfg / "node.env").write_text("MESHBAY_TMDB_DEFAULT_TOKEN=eyJoperator\n") + + assert plat.install_node_env(cfg) is None + assert "eyJoperator" in (cfg / "node.env").read_text() + + +def test_install_node_env_is_a_noop_without_a_package(monkeypatch, tmp_path): + monkeypatch.setattr(plat, "packaged_default_env", lambda: None) + assert plat.install_node_env(tmp_path) is None + assert not (tmp_path / "node.env").exists() + + +def test_load_node_env_sets_names(monkeypatch, tmp_path): + (tmp_path / "node.env").write_text( + "# a comment\n" + "\n" + "MESHBAY_TMDB_DEFAULT_TOKEN=eyJloaded\n" + 'QUOTED="value"\n' + ) + monkeypatch.delenv("MESHBAY_TMDB_DEFAULT_TOKEN", raising=False) + monkeypatch.delenv("QUOTED", raising=False) + assert plat.load_node_env(tmp_path) == 2 + assert os.environ["MESHBAY_TMDB_DEFAULT_TOKEN"] == "eyJloaded" + assert os.environ["QUOTED"] == "value" + + +def test_load_node_env_does_not_override_the_environment(monkeypatch, tmp_path): + """systemd may have loaded the same file already, and an operator export + must win over a packaged default.""" + (tmp_path / "node.env").write_text("MESHBAY_TMDB_DEFAULT_TOKEN=eyJfromfile\n") + monkeypatch.setenv("MESHBAY_TMDB_DEFAULT_TOKEN", "eyJfromenv") + assert plat.load_node_env(tmp_path) == 0 + assert os.environ["MESHBAY_TMDB_DEFAULT_TOKEN"] == "eyJfromenv" + + +def test_load_node_env_tolerates_a_missing_file(tmp_path): + assert plat.load_node_env(tmp_path) == 0 -- cgit v1.2.3