Matches original script APIs against MethodSpec registrations in Rex-EMoolator and keeps review evidence separate from registration. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
73 lines
3.4 KiB
Python
73 lines
3.4 KiB
Python
import json
|
|
|
|
import pytest
|
|
|
|
pytest.importorskip('fastapi')
|
|
from fastapi.testclient import TestClient
|
|
from ams.api.app import create_app
|
|
from ams.api.routes.tracker import inventory_status, order_snapshots
|
|
from ams.snapshot import Snapshot
|
|
|
|
|
|
def test_fresh_snapshot_preferred_and_conflict_reported():
|
|
old = Snapshot({'binary': {'sha256': 'a', 'name': 'golden'}})
|
|
fresh = Snapshot({'binary': {'sha256': 'a', 'name': 'batch', 'extraction': {'fingerprint': 'v2'}}})
|
|
ordered, warnings = order_snapshots([old, fresh])
|
|
assert ordered == [fresh, old]
|
|
assert len(warnings) == 1
|
|
assert 'nie scalono' in warnings[0]
|
|
assert order_snapshots([old, old]) == ([old], [])
|
|
|
|
|
|
def test_batch_library_and_game_aliases_reach_compat(tmp_path):
|
|
from ams.compat import _library_name
|
|
payload = {'binary': {'sha256': 'a'*64, 'name': 'Game__hash__PIKLIB8.DLL',
|
|
'library': 'PIKLIB8.DLL', 'games': ['Game A','Game B']}, 'types': []}
|
|
assert _library_name(Snapshot(payload)) == 'PIKLIB8.DLL'
|
|
with TestClient(create_app(database_url=f'sqlite:///{tmp_path}/compat.db')) as client:
|
|
assert client.post('/snapshots',json=payload).status_code == 201
|
|
data = client.get('/compat?format=json').json()
|
|
assert data['games'] == ['Game A', 'Game B']
|
|
assert len(data['libraries']) == 1
|
|
assert data['libraries'][0]['name'] == 'PIKLIB8.DLL'
|
|
|
|
|
|
def test_inventory_counts_by_hash_and_scope(tmp_path):
|
|
path = tmp_path / 'inventory.json'
|
|
path.write_text(json.dumps({'builds': [
|
|
{'sha256': 'AAA', 'scope': 'engine', 'analysis': 'pending'},
|
|
{'sha256': 'bbb', 'scope': 'engine'},
|
|
{'sha256': 'ccc', 'scope': 'plugin'},
|
|
{'sha256': 'ddd', 'scope': 'native_dependency'},
|
|
]}))
|
|
result = inventory_status(path, [{'sha256': 'aaa'}, {'sha256': 'eee'}])
|
|
assert result['scopes']['engine'] == {'target': 2, 'loaded': 1, 'pending': 1}
|
|
assert result['scopes']['plugin']['pending'] == 1
|
|
assert result['scopes']['native_dependency']['pending'] == 1
|
|
assert result['builds'][0]['snapshot_loaded'] is True
|
|
assert result['unlisted_snapshots'] == 1
|
|
|
|
|
|
def test_recursive_endpoint_prefers_batch(tmp_path, monkeypatch):
|
|
from ams.api.routes import tracker
|
|
snapshots = tmp_path / 'snapshots'
|
|
nested = snapshots / 'batch' / 'by-hash'
|
|
nested.mkdir(parents=True)
|
|
sha = 'a' * 64
|
|
(snapshots / 'golden.snapshot.json').write_text(json.dumps({'binary': {'sha256': sha, 'name': 'old'}}))
|
|
(nested / 'fresh.snapshot.json').write_text(json.dumps({'binary': {'sha256': sha, 'name': 'fresh', 'extraction': {'fingerprint': 'v2'}}}))
|
|
inventory = tmp_path / 'inventory.json'
|
|
inventory.write_text(json.dumps({'builds': [{'sha256': sha, 'scope': 'engine'}]}))
|
|
monkeypatch.setenv('AMS_EMULATOR_ROOT', str(tmp_path))
|
|
monkeypatch.setenv('AMS_TRACKER_SNAPSHOTS', str(snapshots))
|
|
monkeypatch.setenv('AMS_TRACKER_INVENTORY', str(inventory))
|
|
monkeypatch.delenv('AMS_TRACKER_REVIEWS', raising=False)
|
|
monkeypatch.setattr(tracker, 'scan_emulator', lambda root: {'types': {}, 'warnings': []})
|
|
with TestClient(create_app(database_url=f'sqlite:///{tmp_path}/test.db')) as client:
|
|
response = client.get('/tracker')
|
|
assert response.status_code == 200, response.text
|
|
result = response.json()
|
|
assert [b['name'] for b in result['binaries']] == ['fresh']
|
|
assert result['inventory']['scopes']['engine']['loaded'] == 1
|
|
assert any('nie scalono' in w for w in result['warnings'])
|