import sys,subprocess,time,json,uuid,io from pathlib import Path ROOT=Path(__file__).resolve().parents[1] sys.path.insert(0,str(ROOT/'tests')) from client import Client,check from PIL import Image def compose(*args,env=None,input=None,timeout=180): import os r=subprocess.run([str(ROOT/'scripts/compose.sh'),*args],cwd=ROOT,env={**os.environ,**(env or {})},input=input,capture_output=True,timeout=timeout) check(r.returncode==0,'Infrastructure command failed: '+r.stderr.decode()[-500:]);return r.stdout.decode() def sql(text):return compose('exec','-T','postgres','psql','-U','messenger','-d','messenger','-At','-c',text).strip() def parsed(c,method,path,expected=200,**kwargs): result=c.call(method,path,expected,**kwargs);return json.loads(result['bytes']) def upload(c,actor,chat,data=None,name='image.png',mime='image/png',expected=202): if data is None:data=(ROOT/'tests/fixtures/sample.png').read_bytes() from urllib.parse import quote return c.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename='+quote(name,safe=''),expected,actor=actor,raw=data,headers={'Content-Type':mime},schema='Attachment' if expected==202 else None) def job_id(attachment):return sql("SELECT id FROM attachment_jobs WHERE attachment_id='"+attachment['id']+"'") def wait(predicate,timeout=120): deadline=time.monotonic()+timeout while time.monotonic()