# SPDX-License-Identifier: 0BSD import time import urllib.request from unittest.mock import patch import pytest from meshchatx.src.backend.repository_server_manager import ( RepositoryServerManager, build_repository_index_html, bundled_pip_targets, download_bundled_wheels_to_directory, meshchat_bundle_project_root, stage_local_meshchatx_wheel_into_bundled_dir, ) def test_bundled_pip_targets_includes_extra_from_env(monkeypatch): monkeypatch.setenv("MESHCHAT_REPOSITORY_EXTRA_PIP", "foo, bar") names = bundled_pip_targets() assert "foo" in names assert "bar" in names assert "rns" in names assert "rnspure" in names def test_seed_copies_bundled_wheels_from_public(tmp_path): identity = tmp_path / "identity" identity.mkdir() public = tmp_path / "public" bundled_pub = public / "repository-server-bundled" / "bundled" bundled_pub.mkdir(parents=True) (bundled_pub / "from_build.whl").write_bytes(b"wheeldata") mgr = RepositoryServerManager(str(identity), public_dir=str(public)) rows = mgr.list_entries() bundled_names = [r["name"] for r in rows if r["source"] == "bundled"] assert "from_build.whl" in bundled_names def test_seed_skips_when_wheel_already_present_same_size(tmp_path): identity = tmp_path / "identity" identity.mkdir() public = tmp_path / "public" bundled_pub = public / "repository-server-bundled" / "bundled" bundled_pub.mkdir(parents=True) (bundled_pub / "same.whl").write_bytes(b"aaaaaaaaaaaa") dest = identity / "repository-server" / "bundled" dest.mkdir(parents=True) (dest / "same.whl").write_bytes(b"bbbbbbbbbbbb") RepositoryServerManager(str(identity), public_dir=str(public)) assert (dest / "same.whl").read_bytes() == b"bbbbbbbbbbbb" def test_seed_replaces_bundled_when_public_wheel_size_differs(tmp_path): identity = tmp_path / "identity" identity.mkdir() public = tmp_path / "public" bundled_pub = public / "repository-server-bundled" / "bundled" bundled_pub.mkdir(parents=True) (bundled_pub / "mesh.whl").write_bytes(b"slim_wheel_from_build") dest = identity / "repository-server" / "bundled" dest.mkdir(parents=True) (dest / "mesh.whl").write_bytes(b"fat_old") RepositoryServerManager(str(identity), public_dir=str(public)) assert (dest / "mesh.whl").read_bytes() == b"slim_wheel_from_build" def test_meshchat_bundle_project_root_exists(): root = meshchat_bundle_project_root() assert root is not None assert (root / "pyproject.toml").is_file() assert (root / "meshchatx").is_dir() def test_stage_local_meshchatx_wheel_copies_newest(tmp_path): root = tmp_path / "proj" root.mkdir() (root / "pyproject.toml").write_text( 'name = "reticulum-meshchatx"\nversion = "0.0.0"\n', encoding="utf-8" ) (root / "meshchatx").mkdir() dist = root / "dist" dist.mkdir() older = dist / "reticulum_meshchatx-1.0.0-py3-none-any.whl" newer = dist / "reticulum_meshchatx-2.0.0-py3-none-any.whl" older.write_bytes(b"old") time.sleep(0.02) newer.write_bytes(b"new") dest = tmp_path / "bundled" dest.mkdir() with patch( "meshchatx.src.backend.repository_server_manager.meshchat_bundle_project_root", return_value=root, ): out = stage_local_meshchatx_wheel_into_bundled_dir(dest) assert out is not None assert out.name == newer.name assert (dest / newer.name).read_bytes() == b"new" assert not (dest / older.name).exists() def test_build_repository_index_html_lists_files(tmp_path): bundled = tmp_path / "bundled" uploads = tmp_path / "uploads" bundled.mkdir() uploads.mkdir() (bundled / "pkg.whl").write_bytes(b"x") (uploads / "note.txt").write_bytes(b"ab") html_out = build_repository_index_html(str(bundled), str(uploads)) assert "MeshChatX repository" in html_out assert "pkg.whl" in html_out assert "note.txt" in html_out assert 'href="bundled/' in html_out assert 'href="uploads/' in html_out def test_save_list_delete_upload(tmp_path): mgr = RepositoryServerManager(str(tmp_path)) ok, err = mgr.save_upload("test.whl", b"abc") assert ok and err is None rows = mgr.list_entries() assert len(rows) == 1 assert rows[0]["name"] == "test.whl" assert rows[0]["source"] == "upload" ok2, err2 = mgr.delete_upload("test.whl") assert ok2 and err2 is None assert mgr.list_entries() == [] def test_save_rejects_bad_filename(tmp_path): mgr = RepositoryServerManager(str(tmp_path)) ok, err = mgr.save_upload("../evil.whl", b"x") assert not ok @pytest.mark.parametrize( "name", [ "x