99 lines
4.3 KiB
Python
99 lines
4.3 KiB
Python
"""Exercise real HTTP uploads/downloads against a synthetic Athena API; no LLM/GPU."""
|
|
import importlib.util
|
|
import io
|
|
import json
|
|
from pathlib import Path
|
|
import tempfile
|
|
import threading
|
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
import unittest
|
|
from unittest.mock import patch
|
|
import wave
|
|
|
|
spec = importlib.util.spec_from_file_location('audio_separator_plugin', Path(__file__).with_name('separator.py'))
|
|
plugin = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(plugin)
|
|
|
|
|
|
def wav():
|
|
stream = io.BytesIO()
|
|
with wave.open(stream, 'wb') as w:
|
|
w.setnchannels(2)
|
|
w.setsampwidth(2)
|
|
w.setframerate(44100)
|
|
w.writeframes(b'\x00' * 17640)
|
|
return stream.getvalue()
|
|
|
|
|
|
class Server(BaseHTTPRequestHandler):
|
|
starts = 0
|
|
downloads = 0
|
|
wrong_job = False
|
|
|
|
def log_message(self, *args):
|
|
pass
|
|
|
|
def send(self, body):
|
|
self.send_response(200)
|
|
self.end_headers()
|
|
self.wfile.write(body)
|
|
|
|
def do_POST(self):
|
|
assert self.headers['Authorization'] == 'Bearer test-only'
|
|
body = self.rfile.read(int(self.headers['Content-Length']))
|
|
assert b'RIFF' in body and plugin.MODEL.encode() in body and b'vocals' in body
|
|
type(self).starts += 1
|
|
self.send(json.dumps({'id': 'a' * 32, 'state': 'running'}).encode())
|
|
|
|
def do_GET(self):
|
|
assert self.headers['Authorization'] == 'Bearer test-only'
|
|
if 'audio?' in self.path:
|
|
type(self).downloads += 1
|
|
self.send(wav())
|
|
else:
|
|
self.send(json.dumps({'job': {'id': ('b' if self.wrong_job else 'a') * 32,
|
|
'state': 'complete', 'files': [
|
|
{'index': 0, 'name': 'input_(Vocals).wav'},
|
|
{'index': 1, 'name': 'rest.wav'}]}}).encode())
|
|
|
|
|
|
class SeparationTests(unittest.TestCase):
|
|
def test_upload_decode_download_idempotency_and_session_boundaries(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
root = Path(temp)
|
|
(root / 'workspace').mkdir()
|
|
source = root / 'workspace' / 'test.wav'
|
|
source.write_bytes(wav())
|
|
http = ThreadingHTTPServer(('127.0.0.1', 0), Server)
|
|
thread = threading.Thread(target=http.serve_forever, daemon=True)
|
|
thread.start()
|
|
try:
|
|
with patch.object(plugin, 'home', return_value=root), patch.object(plugin, 'cwd', return_value=root / 'workspace'):
|
|
settings = dict(base_url=f'http://127.0.0.1:{http.server_port}', api_key='test-only', session_id='own-test')
|
|
result = json.loads(plugin.separate({'file_path': str(source)}, **settings))
|
|
self.assertEqual(result['state'], 'complete', result)
|
|
self.assertEqual({f['name'] for f in result['files']}, {'Vocals.wav', 'Instrumental.wav'})
|
|
self.assertEqual(result['delivery'].count('MEDIA:'), 2)
|
|
self.assertTrue(all(Path(f['path']).read_bytes() == wav() for f in result['files']))
|
|
self.assertEqual(json.loads(plugin.separate({'file_path': str(source)}, **settings)), result)
|
|
self.assertEqual(Server.starts, 1)
|
|
self.assertEqual(Server.downloads, 2)
|
|
foreign = dict(settings, session_id='another-session')
|
|
self.assertEqual(json.loads(plugin.separate({'job_id': result['job_id']}, **foreign))['state'], 'error')
|
|
self.assertEqual(json.loads(plugin.separate({'file_path': '/etc/shadow'}, **settings))['state'], 'error')
|
|
# Force a status refresh and verify a replaced remote job is never downloaded.
|
|
record_path = next((root / 'audio-separation').glob('*/*/job.json'))
|
|
record = json.loads(record_path.read_text())
|
|
record.pop('result')
|
|
record_path.write_text(json.dumps(record))
|
|
Server.wrong_job = True
|
|
self.assertEqual(json.loads(plugin.separate({'job_id': result['job_id']}, **settings))['state'], 'error')
|
|
self.assertEqual(Server.downloads, 2)
|
|
finally:
|
|
http.shutdown()
|
|
http.server_close()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|