Files
pages/pages-cli/test_task_integration.py
Anya Tokugawa 4049acf76e
Build and publish container images / images (push) Successful in 12m0s
Release container images / release (push) Successful in 19s
fix(tasks): recover deleted captures without reviving tombstones
2026-11-08 19:26:50 +09:00

112 lines
7.1 KiB
Python

"""Called by the Go synthetic HTTP fixture; no production credentials."""
import contextlib
import io
import json
from pathlib import Path
import runpy
import sys
import uuid
from types import SimpleNamespace
from unittest.mock import patch
from email.message import EmailMessage
def run():
m=runpy.run_path(str(Path(__file__).with_name('pages')))
fixture=json.load(sys.stdin)
client=m['Client'](fixture['origin'],fixture['token'])
profile=m['Profile']('test',fixture['root'])
def task(*words):
output=io.StringIO()
with contextlib.redirect_stdout(output):m['tasks'](client,profile,list(words))
return json.loads(output.getvalue())
with profile.locked():
prefs=task('preferences','--due-days','2','--estimate','60','--complexity','3')
assert prefs['preferences']['due_days']==2
item=task('add','CLI task')['item'];ident=item['uuid']
assert item['estimate_minutes']==60 and item['due_on']
task('start',ident);assert task('status')['item']['task_uuid']==ident
task('stop');assert task('status')['item'] is None
item=task('update',ident,'--title','Updated CLI task')['item'];assert item['title']=='Updated CLI task'
task('done',ident)
report=task('report','--json');assert sum(d['points'] for d in report['days'])==7
assert any(i['uuid']==ident for i in report['tasks'])
task('preferences','--wiki-prefix','/private/mail')
message=EmailMessage();message['Subject']='[hogehoge 123] 日本語の件名と依頼';message['Message-ID']='<integration@example.invalid>';message.set_content('Private mail body {{children}}')
mail=message.as_bytes()
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(mail))):
captured=task('from-mail')
assert captured['item']['page_uuid'] and captured['page_path']=='/private/mail/hogehoge/123'
assert captured['item']['title']=='日本語の件名と依頼'
assert 'Private mail body' not in captured['item']['notes']
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(mail))):
assert task('from-mail')['item']['page_uuid']==captured['item']['page_uuid']
note=client.json(m['TASK_API']+'items/'+captured['item']['uuid']+'/note')
assert note['linked'] and note['path']==captured['page_path']
raw,_=client.raw('path:'+captured['page_path'])
assert '日本語の件名と依頼' in raw.decode() and 'Private mail body' in raw.decode()
message.replace_header('Subject','連番なしの日本語');message.replace_header('Message-ID','<other@example.invalid>');message['Date']='Sat, 31 Jan 2026 23:30:00 -0800'
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(message.as_bytes()))):dated=task('from-mail')
assert dated['page_path']=='/private/mail/2026/01/'+dated['item']['uuid']
del message['Message-ID']
import hashlib
raw_mail=message.as_bytes()
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(raw_mail))):hashed=task('from-mail')
assert hashed['page_path']=='/private/mail/2026/01/'+hashlib.sha256(raw_mail).hexdigest()
path=Path(fixture['root'])/'person.vcf'
path.write_text('BEGIN:VCARD\r\nVERSION:4.0\r\nUID:urn:test:cli\r\nFN:CLI Person\r\nEMAIL:cli@example.invalid\r\nEND:VCARD\r\n')
output=io.StringIO()
with contextlib.redirect_stdout(output):m['contacts'](client,['import',str(path)])
contact=json.loads(output.getvalue())['item'];output=io.StringIO()
with contextlib.redirect_stdout(output):m['contacts'](client,['query','CLI','--neomutt'])
assert 'cli@example.invalid\tCLI Person\t' in output.getvalue()
output=io.StringIO()
with contextlib.redirect_stdout(output):m['contacts'](client,['export',contact['uuid']])
assert 'UID:urn:test:cli' in output.getvalue()
# Failed captures can be replaced locally; committed receipts remain protected.
request_id=str(uuid.uuid4())
with contextlib.redirect_stderr(io.StringIO()):
try:task('add','Rejected draft','--request-id',request_id,'--estimate','-1')
except m['Failure'] as error:assert error.status==422
else:raise AssertionError('Invalid estimate accepted')
assert profile.data['task_outbox'][request_id]['estimate_minutes']==-1
corrected=task('add','Corrected draft','--request-id',request_id,'--estimate','15')['item']
assert corrected['uuid']==request_id and corrected['title']=='Corrected draft'
assert request_id not in profile.data['task_outbox']
try:task('add','Changed after commit','--request-id',request_id,'--estimate','30')
except m['Failure'] as error:assert error.status==412
else:raise AssertionError('Committed capture silently overwritten')
assert profile.data['task_outbox'][request_id]['title']=='Changed after commit'
stored=task('show',request_id)['item']
assert stored['title']=='Corrected draft' and stored['estimate_minutes']==15
# Deleted receipts must not be mistaken for editable conflicts. An
# explicit recreation preserves the old tombstone and changes identity.
client.request(m['TASK_API']+'items/'+request_id,method='DELETE',etag='"'+str(stored['version'])+'"')
with contextlib.redirect_stderr(io.StringIO()):
try:task('retry',request_id)
except m['Failure'] as error:
assert error.status==410 and '--recreate-deleted' in str(error)
else:raise AssertionError('Deleted task implicitly recreated')
recreated=task('retry',request_id,'--recreate-deleted')['item']
replacement=str(uuid.uuid5(uuid.NAMESPACE_URL,'pages:task-recreate:'+request_id))
assert recreated['uuid']==replacement and recreated['title']=='Changed after commit'
assert recreated['estimate_minutes']==30 and not profile.data.get('task_outbox')
try:task('show',request_id)
except m['Failure'] as error:assert error.status==404
else:raise AssertionError('Deleted UUID resurrected')
# Reimport a deleted mail with a changed body: reconnect the surviving
# Wiki note, without overwriting its independent content or revision.
mail_id=captured['item']['uuid']
client.request(m['TASK_API']+'items/'+mail_id,method='DELETE',etag='"1"')
changed_mail=mail.replace(b'Private mail body',b'Changed private body')
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(changed_mail))),contextlib.redirect_stderr(io.StringIO()):
try:task('from-mail')
except m['Failure'] as error:assert error.status==410
else:raise AssertionError('Deleted mail task recreated implicitly')
with contextlib.redirect_stderr(io.StringIO()):recreated_mail=task('retry',mail_id,'--recreate-deleted')
assert recreated_mail['item']['page_uuid']==captured['item']['page_uuid']
assert recreated_mail['page_path']==captured['page_path']
body,_=client.raw('path:'+recreated_mail['page_path'])
assert 'Private mail body' in body.decode() and 'Changed private body' not in body.decode()
if __name__=='__main__':run()