Files
mtusi2026lab4/tests/review_regressions.py
T

65 lines
4.1 KiB
Python

"""Five review regressions, real Compose stack. JPEG fixture generation needs Pillow."""
from client import Client, check, run
from track2 import sql, mc
from pathlib import Path
from PIL import Image
import io, os, struct, zlib, uuid, hashlib, time, urllib.request
def png_large():
def chunk(kind, data):
return struct.pack('>I', len(data))+kind+data+struct.pack('>I',zlib.crc32(kind+data)&0xffffffff)
width=700
data=b''.join(b'\0'+os.urandom(width*3) for _ in range(width))
base=b'\x89PNG\r\n\x1a\n'+chunk(b'IHDR',struct.pack('>IIBBBBB',width,width,8,2,0,0,0))+chunk(b'IDAT',zlib.compress(data))
extra=1770843-len(base)-12-12
return base+chunk(b'tEXt',b'proof\0'+b'x'*(extra-6))+chunk(b'IEND',b'')
def main():
print('2026-11-06 Europe/Moscow; current working tree; synthetic data only',flush=True)
c=Client(base_url=os.environ.get('BASE_URL','http://127.0.0.1:8080'),timeout=30)
for _ in range(90):
try:
c.call('GET','/health/ready',schema='Health');break
except Exception: time.sleep(1)
else: raise AssertionError('API not ready')
a,b,e,chat=c.bootstrap()
png=Path('tests/fixtures/sample.png').read_bytes()
out=io.BytesIO(); Image.new('RGB',(32,24),(12,50,100)).save(out,format='JPEG'); jpeg=out.getvalue()
path='/api/v1/chats/'+chat['id']+'/attachments?filename='
def upload(data,mime='image/png',filename='proof.png',expected=201):
return c.call('POST',path+filename,expected,actor=a,raw=data,headers={'Content-Type':mime},schema='Attachment' if expected==201 else None)
def count(): return sql("select count(*) from attachments where chat_id='"+chat['id']+"';")
before=int(count()); attachments=[]; instances=set()
for data,mime,name in [(png,'image/png','proof.png'),(jpeg,'image/jpeg','proof.jpg')]*5:
response=upload(data,mime,name); att=response['json']; attachments.append(att)
instances.add(response['headers']['X-Instance-Id'])
got=c.call('GET','/api/v1/attachments/'+att['id'],actor=b,schema='Attachment')['json']
check(got['content_type']==mime and got['status']=='ready','bad MIME/status')
check(c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=b)['bytes']==data,'bytes differ')
check(int(count())-before==10,'unexpected metadata count')
print('PASS PNG/JPEG: 10 uploads -> 201; 10 metadata GET -> 200; 10 downloads identical; DB +10; instances='+str(len(instances)),flush=True)
plain=c.message(a,chat,'plain')['json'];check(plain['attachment_ids']==[],'missing empty attachment_ids')
attached=c.message(a,chat,'',attachments=[attachments[0]['id']])['json'];check(attached['attachment_ids']==[attachments[0]['id']],'attachment lost')
other=c.call('POST','/api/v1/chats',201,actor=a,body={'title':'other','member_ids':[a['id']]},schema='Chat')['json']
for actor,target,ids in [(a,other,[attachments[0]['id']]),(a,chat,[str(uuid.uuid4())]),(e,chat,[attachments[0]['id']])]:
c.call('POST','/api/v1/chats/'+target['id']+'/messages',404,actor=actor,body={'text':'x','attachment_ids':ids},headers={'Idempotency-Key':uuid.uuid4().hex})
print('PASS messages: plain attachment_ids=[]; file-only POST -> 201; cross-chat/unknown/outsider -> 404',flush=True)
before=count(); objects=mc('mc ls proof/messenger-attachments/')
for _ in range(10): upload(png[:33],expected=415)
upload(png,filename='..%2Fx.png',expected=400)
check(count()==before,'rejected upload persisted metadata')
check(mc('mc ls proof/messenger-attachments/')==objects,'rejected upload changed objects')
print('PASS sample.png[:33]: 10 x 415; ../x.png: 400; DB and S3 unchanged',flush=True)
large=png_large();check(len(large)==1770843,'fixture size mismatch')
with Image.open(io.BytesIO(large)) as image: image.load()
att=upload(large)['json']
result=c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=b)
check(result['bytes']==large,'large PNG differs')
print('PASS valid PNG exactly 1770843 bytes: POST -> 201 through LB; download checksum matches',flush=True)
print('PASS all five review regressions',flush=True)
if __name__=='__main__': run(main)