import json,os,socket,subprocess,sys,tempfile,time,unittest from pathlib import Path from types import SimpleNamespace from unittest.mock import patch from applio import Applio,applio_model from execution_setup import assess class Tests(unittest.TestCase): def setUp(self): self.tmp=tempfile.TemporaryDirectory();self.active=[];self.leases=0 self.runtime=SimpleNamespace(status=lambda:{'installed':False},paths=lambda:(Path('/missing'),Path('/missing'))) self.scheduler=SimpleNamespace(gpu_mode='voice',image_reservation=self.reserve) self.profile=dict(id='test',name='RVC',kind='voice',runnable=True,model={'kind':'voice','file':'voice.pth','runtime_metadata':{'architecture':'rvc'}}) self.profiles=SimpleNamespace(status=lambda:{'profiles':[self.profile]}) self.a=Applio(Path(self.tmp.name),self.runtime,self.scheduler,self.profiles,lambda:self.active) def tearDown(self):self.a.stop();self.tmp.cleanup() def reserve(self,*args,**kwargs): self.leases+=1 def release():self.leases-=1 return release def connect(self): c=socket.socket(socket.AF_UNIX);c.connect(str(self.a.socket));c.sendall(b'{"action":"reserve"}\n');return c,json.loads(c.makefile('rb').readline()) def test_requires_active_profile_and_voice_mode(self): c,d=self.connect();self.assertIn('error',d);c.close();self.assertEqual(self.leases,0) self.active=['test'];self.scheduler.gpu_mode='music';c,d=self.connect();self.assertIn('error',d);c.close();self.assertEqual(self.leases,0) def test_disconnect_releases_gpu_and_competing_request_does_not_clear_active(self): self.active=['test'] with patch('image_test.probe',return_value=[dict(name='RTX 3060',uuid='GPU-test')]): c,d=self.connect();self.assertEqual(d['gpu'],'GPU-test');self.assertEqual(self.leases,1) other,result=self.connect();self.assertIn('error',result);other.close();time.sleep(.05);self.assertTrue(self.a.active) with self.assertRaises(ValueError):self.a.configure({'device':'5080'}) c.close() for _ in range(100): if not self.a.active:break time.sleep(.01) self.assertEqual(self.leases,0);self.assertFalse(self.a.active) def test_real_profile_is_runnable_only_after_runtime_ready(self): from catalog import Catalog from profiles import Profiles root=Path(self.tmp.name);catalog=Catalog(root/'models');folder=catalog.root/('a'*64);folder.mkdir(parents=True);(folder/'model.pth').write_bytes(b'test');(folder/'entry.json').write_text(json.dumps(dict(repo='local/mikes-applio',revision='b'*64,file='voice.pth',kind='voice',size=4,sha256='b'*64,runtime_metadata={'architecture':'rvc'}))) profiles=Profiles(root/'profiles.json',catalog);row=profiles.save(dict(id=None,revision=0,name='Applio RVC',api_id='applio-rvc',kind='voice',model_id='a'*64,parameters={})) profiles.applio_blockers=lambda p:['Laufzeit fehlt'] self.assertFalse(profiles.status()['profiles'][0]['runnable']) profiles.applio_blockers=lambda p:[] self.assertTrue(profiles.status()['profiles'][0]['runnable']) def test_rvc_recognition_does_not_guess_every_pth(self): self.assertTrue(applio_model(self.profile['model'])) self.assertFalse(applio_model(dict(kind='voice',file='voice.pth'))) result=assess(self.profile['model'],{'applio':True}) self.assertEqual(result['setup_steps'][1]['state'],'complete');self.assertEqual(result['components'],[]) class WorkerTests(unittest.TestCase): def test_worker_imports_separate_runtime_and_serves_repeated_requests(self): with tempfile.TemporaryDirectory() as directory: root=Path(directory);module=root/'rvc/infer';module.mkdir(parents=True) (root/'rvc/__init__.py').write_text('');(module/'__init__.py').write_text('') (module/'infer.py').write_text('class VoiceConverter:\n def convert_audio(self,**kwargs): pass\n') result=subprocess.run([sys.executable,str(Path(__file__).with_name('applio_worker.py'))],input='{}\n{}\n',text=True,capture_output=True,cwd='/',env=dict(os.environ,APPLIO_WORKDIR=str(root)),timeout=10,check=True) self.assertEqual([json.loads(line) for line in result.stdout.splitlines()],[{'ready':True},{'ok':True},{'ok':True}]) if __name__=='__main__':unittest.main()