feat(work): add task-scoped attachment intake and durable file delivery
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
"""Optional cross-checkout test using actual PC transport/signatures, no real Work."""
|
||||
import asyncio
|
||||
import importlib.util
|
||||
import importlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from types import SimpleNamespace
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from test_credential_api import env
|
||||
from test_work_files import create_task,fake_platform
|
||||
from relay.crypto import pem_private
|
||||
|
||||
pytestmark=pytest.mark.parametrize('env',[True],indirect=True)
|
||||
|
||||
|
||||
async def test_three_materials_native_output_lost_receipt_and_original_file_reply(env,tmp_path,monkeypatch):
|
||||
source=os.environ.get('HN_PC_FILES_SOURCE')
|
||||
if not source:pytest.skip('explicit PC source checkout required')
|
||||
source=Path(source);package=source/'integrations/work-relay/service/relay'
|
||||
spec=importlib.util.spec_from_file_location('_pc_work_file_chain',package/'__init__.py',submodule_search_locations=[str(package)])
|
||||
module=importlib.util.module_from_spec(spec);sys.modules[spec.name]=module;spec.loader.exec_module(module)
|
||||
files_module=importlib.import_module(spec.name+'.work_files')
|
||||
credentials=importlib.import_module(spec.name+'.credential_provider')
|
||||
office_spec=importlib.util.spec_from_file_location('_office_chain',source/'capability-slim/runtime/office_workflows.py')
|
||||
office=importlib.util.module_from_spec(office_spec);office_spec.loader.exec_module(office)
|
||||
remote_files,platform_calls=fake_platform(env,monkeypatch)
|
||||
samples=os.environ.get('HN_PC_SAMPLE_DIR');native_python=os.environ.get('HN_OFFICE_PYTHON')
|
||||
names=['材料.docx','表格.xlsx','参考.pdf']if samples else['one.txt','two.csv','three.md']
|
||||
if samples:
|
||||
paths=[Path(samples)/n for n in ['报告模板.docx','材料一.xlsx','文本材料.pdf']]
|
||||
raw_materials=[p.read_bytes()for p in paths]
|
||||
else:raw_materials=[b'synthetic material 1',b'column,value\nx,1',b'# source']
|
||||
mapping={f'file-input-{i}':content for i,content in enumerate(raw_materials)}
|
||||
async def download(message,key,limit):return mapping[key]
|
||||
monkeypatch.setattr(env['platform'],'download_message_file',download)
|
||||
api=env['app'].state.files
|
||||
for i,name in enumerate(names):await api.receive('ou_a','chat-files',f'om-input-{i}',{'file_name':name,'file_key':f'file-input-{i}'})
|
||||
task=create_task(env);api.claim(task)
|
||||
context={k:task[k]for k in ['environment','app_id','owner','host_id','binding_generation','chat_id','source_message_id','conversation_id']}
|
||||
context.update(task_id='task-files',channel='work-dm')
|
||||
device=tmp_path/'pc-device';device.mkdir();(device/'device.json').write_text(json.dumps({'host_id':'a'*32,'owner':'ou_a','binding_generation':task['binding_generation']}))
|
||||
(device/'device.key').write_bytes(pem_private(env['keys']['a'*32]))
|
||||
loop=asyncio.get_running_loop();requests=[];lost=[False]
|
||||
def forward(request):
|
||||
requests.append(request.url.path)
|
||||
future=asyncio.run_coroutine_threadsafe(env['client'].request(request.method,request.url.path,
|
||||
content=request.read(),headers=dict(request.headers)),loop)
|
||||
response=future.result(20)
|
||||
if request.url.path.endswith('/upload')and not lost[0]:
|
||||
lost[0]=True;raise httpx.ReadError('synthetic lost upload receipt')
|
||||
return httpx.Response(response.status_code,headers=dict(response.headers),content=response.content)
|
||||
transport=httpx.Client(base_url='https://relay.example.test',transport=httpx.MockTransport(forward))
|
||||
provider=object.__new__(credentials.CredentialProvider)
|
||||
provider.directory=device;provider.host='a'*32;provider.owner='ou_a';provider.wall=time.time;provider.http=transport
|
||||
provider.release={'environment':'test','app_id':'cli_test'};provider.cache_key=('synthetic',)
|
||||
client=SimpleNamespace(provider=provider,http=transport)
|
||||
home=tmp_path/'pc-home'
|
||||
try:
|
||||
staged=await asyncio.to_thread(files_module.stage_materials,client,context,home)
|
||||
assert len(staged['materials'])==3 and all(m['status']=='ready'for m in staged['materials'])
|
||||
assert sorted(Path(m['path']).read_bytes()for m in staged['materials'])==sorted(raw_materials)
|
||||
root=files_module.task_workspace(context,home)
|
||||
if native_python and samples:
|
||||
doc=next(m['path']for m in staged['materials']if m['name'].endswith('.docx'))
|
||||
output=root/'outputs/converted.pdf'
|
||||
request={'action':'export_pdf','arguments':{'source':doc,'output':str(output)}}
|
||||
completed=await asyncio.to_thread(subprocess.run,[native_python,'-X','utf8',str(source/'capability-slim/runtime/office_workflows.py')],
|
||||
input=json.dumps(request).encode(),capture_output=True,timeout=120)
|
||||
assert completed.returncode==0,completed.stderr
|
||||
assert json.loads(completed.stdout)['status']=='completed',completed.stdout
|
||||
assert output.read_bytes().startswith(b'%PDF')
|
||||
else:
|
||||
output=root/'outputs/result.txt';output.write_text('synthetic final output')
|
||||
office.register_artifacts(root,[output])
|
||||
first=await asyncio.to_thread(files_module.deliver_artifacts,client,context,home)
|
||||
assert first['unknown']==1
|
||||
second=await asyncio.to_thread(files_module.deliver_artifacts,client,context,home)
|
||||
assert second['received']==1 and second['unknown']==0
|
||||
assert requests.count('/work/v2/files/upload')==1 # Read state instead of resending bytes.
|
||||
await api.flush();await api.flush();await api.flush()
|
||||
sent=[x for x in platform_calls if x[0]=='reply_file']
|
||||
assert len(sent)==1 and sent[0][1]=='om-original'
|
||||
assert remote_files[sent[0][2]]==output.read_bytes()
|
||||
assert len([x for x in platform_calls if x[0]=='upload'])==1
|
||||
finally:transport.close()
|
||||
Reference in New Issue
Block a user