import hashlib import importlib.util import json from pathlib import Path import tempfile import unittest from unittest.mock import patch import zipfile ROOT=Path(__file__).resolve().parents[1] spec=importlib.util.spec_from_file_location('release',ROOT/'scripts/release.py');release=importlib.util.module_from_spec(spec);spec.loader.exec_module(release) class ReleaseTests(unittest.TestCase): def test_gitea_github_stable_manifest_and_immutable_download_contract(self): a=release.urls(release.DEFAULT_REPOSITORY,'0.5.0');b=release.urls(release.DEFAULT_REPOSITORY,'0.5.1') self.assertEqual(a['manifest'],b['manifest']);self.assertNotEqual(a['download'],b['download']) github=release.urls('https://github.com/example/adaptive-music','0.5.0') self.assertEqual(github['manifest'],'https://raw.githubusercontent.com/example/adaptive-music/main/module.json') self.assertIn('/releases/download/v0.5.0/adaptive-music.zip',github['download']) for invalid in ['http://example.test/repo','https://localhost/repo','https://127.0.0.1/repo','https://' + 'user:secret' + '@example.test/repo']: with self.assertRaises(ValueError):release.urls(invalid,'0.5.0') def test_complete_offline_bundle_is_repeatable_and_checksums_match(self): with tempfile.TemporaryDirectory() as folder: root=Path(folder);bundle=release.build_release(output=root);first=bundle.read_bytes();release.build_release(output=root) self.assertEqual(first,bundle.read_bytes()) with zipfile.ZipFile(bundle) as outer: self.assertTrue({'adaptive-music.zip','module.json','adaptive-music-source.zip','README.md','INSTALL.md','CHANGELOG.md','LICENSE','THIRD-PARTY-NOTICES','SHA256SUMS'}.issubset(outer.namelist())) standalone=json.loads(outer.read('module.json')) for row in outer.read('SHA256SUMS').decode().splitlines(): digest,name=row.split(' ',1);self.assertEqual(hashlib.sha256(outer.read(name)).hexdigest(),digest) import io with zipfile.ZipFile(io.BytesIO(outer.read('adaptive-music.zip'))) as module: self.assertEqual(json.loads(module.read('adaptive-music/module.json')),standalone) self.assertIn('adaptive-music/vendor/sendspin-5.0.0.mjs',module.namelist()) with zipfile.ZipFile(io.BytesIO(outer.read('adaptive-music-source.zip'))) as source: self.assertIn('adaptive-music-source/gateway/ma-client.mjs',source.namelist()) self.assertFalse(any('/.local/' in n or '/node_modules/' in n for n in source.namelist())) release.validate_package(root/'adaptive-music.zip',standalone) (root/'licenses').mkdir(exist_ok=True) (root/'licenses/unmanaged.txt').write_text('must not be swept into the release') with self.assertRaises(ValueError):release.build_release(output=root) def test_tampered_manifest_or_missing_asset_is_rejected(self): manifest=release.release_manifest() with self.assertRaises(ValueError):release.release_manifest(version='9.9.9') with tempfile.TemporaryDirectory() as folder: entries=release.module_entries(manifest);entries.pop('adaptive-music/src/audio/peer-audio.mjs') path=Path(folder)/'bad.zip';release.write_zip(path,entries) with self.assertRaises(ValueError):release.validate_package(path) bad={**manifest,'download':'https://example.test/latest.zip'} with self.assertRaises(ValueError):release.validate_manifest(bad) def test_source_archive_rebuild_does_not_need_a_git_remote_or_metadata(self): manifest=release.release_manifest() with patch.object(release.subprocess,'check_output',side_effect=FileNotFoundError): entries=release.source_entries(manifest) self.assertIn('adaptive-music-source/src/audio/gm-host.mjs',entries) self.assertIn('adaptive-music-source/SOURCE_REVISION.txt',entries)