import asyncio import hashlib import json import pytest from test_credential_api import env from test_device_lifecycle import routed,device,instruction,reply,send_event,event,wait_for_reply from test_work_files import fake_platform pytestmark=pytest.mark.parametrize('env',[True],indirect=True) def file_event(env,message_id='om-file',chat='chat-a',owner='ou_a'): value=event(env,message_id,'',chat=chat,owner=owner) value['event']['message'].update(message_type='file',content=json.dumps({'file_name':'source.txt','file_key':'file-source'})) return value async def test_file_then_instruction_uses_encrypted_routing_and_capability_gate(routed,monkeypatch): env=routed;_,calls=fake_platform(env,monkeypatch) connection=await device(env,work_files=True) await send_event(env,file_event(env));await wait_for_reply(env['platform'],1) await send_event(env,event(env,'om-instruction','请处理刚才的材料')) payload=await instruction(env,connection) assert payload['file_transfer']=='work-files-v1' task_id='msg_'+hashlib.sha256(b'om-instruction').hexdigest() assert payload['task_context']['task_id']==task_id rows=env['store'].db.execute('SELECT task_id,status FROM work_files_v1').fetchall() assert len(rows)==1 and rows[0]['task_id']==task_id and rows[0]['status']=='assigned' await reply(env,connection,payload,'synthetic result') await wait_for_reply(env['platform'],2) assert len([x for x in calls if x[0]=='download'])==1 async def test_old_client_does_not_receive_a_task_that_silently_omits_materials(routed,monkeypatch): env=routed;fake_platform(env,monkeypatch);connection=await device(env) await send_event(env,file_event(env));await wait_for_reply(env['platform'],1) await send_event(env,event(env,'om-instruction','请处理材料'));await wait_for_reply(env['platform'],2) assert '升级' in env['platform'].replies[-1][1] assert env['store'].db.execute('SELECT status FROM work_files_v1').fetchone()['status']=='staged' assert env['store'].db.execute('SELECT COUNT(*) AS n FROM work_tasks_v2').fetchone()['n']==0 async def test_invalid_upload_metadata_is_rejected_before_binary_body_is_read(env): consumed=[] async def content(): consumed.append(True);yield b'not read' response=await env['client'].post('/work/v2/files/upload',content=content()) assert response.status_code==422 and not consumed