279 lines
17 KiB
Python
279 lines
17 KiB
Python
import contextlib
|
|
import io
|
|
import json
|
|
import runpy
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
import unittest
|
|
|
|
m=runpy.run_path(str(Path(__file__).with_name('pages')))
|
|
|
|
class ProductivityTests(unittest.TestCase):
|
|
def test_deleted_capture_message_retains_draft_without_retry_loop(self):
|
|
ident='b5550cab-22d4-54eb-b06d-3b432d3f5101'
|
|
original={'request_id':ident,'title':'Private title','notes':'Private notes'}
|
|
for status,code,message in [(410,'deleted','--recreate-deleted'),(409,'capture_missing','consistency repair'),(412,'conflict','unchanged retry'),(409,'note_exists','not overwritten')]:
|
|
profile=SimpleNamespace(data={'task_outbox':{ident:original.copy()}},save=lambda:None)
|
|
calls=[]
|
|
def send(*args,**kw):
|
|
calls.append(kw)
|
|
raise m['Failure']('old generic message',status,{'error':code})
|
|
output=io.StringIO()
|
|
with contextlib.redirect_stderr(output),self.assertRaises(m['Failure']) as error:
|
|
m['tasks'](SimpleNamespace(json=send),profile,['retry',ident])
|
|
self.assertIn(message,str(error.exception))
|
|
self.assertNotIn('Private',str(error.exception)+output.getvalue())
|
|
self.assertNotIn('retry with:',output.getvalue())
|
|
self.assertEqual(profile.data['task_outbox'][ident],original)
|
|
self.assertEqual(len(calls),1)
|
|
|
|
def test_recreate_deleted_persists_successor_and_retries_same_id(self):
|
|
import uuid
|
|
ident='b5550cab-22d4-54eb-b06d-3b432d3f5101'
|
|
replacement=str(uuid.uuid5(uuid.NAMESPACE_URL,'pages:task-recreate:'+ident))
|
|
payload={'request_id':ident,'title':'Private title','wiki_body':'Private body','wiki_suffix':'2030/01/'+ident}
|
|
saved,calls=[],[]
|
|
profile=SimpleNamespace(data={'task_outbox':{ident:payload.copy()}})
|
|
profile.save=lambda:saved.append(json.loads(json.dumps(profile.data)))
|
|
def send(route,**kw):
|
|
calls.append(kw['data'].copy())
|
|
self.assertEqual(saved[-1]['task_outbox'][replacement],kw['data'])
|
|
self.assertNotIn(ident,saved[-1]['task_outbox'])
|
|
if len(calls)==1:raise m['Failure']('Connection lost')
|
|
return {'item':{'uuid':replacement}}
|
|
output,error=io.StringIO(),io.StringIO()
|
|
with contextlib.redirect_stdout(output),contextlib.redirect_stderr(error):
|
|
with self.assertRaises(m['Failure']):m['tasks'](SimpleNamespace(json=send),profile,['retry',ident,'--recreate-deleted'])
|
|
self.assertEqual(len(profile.data['task_outbox']),1)
|
|
m['tasks'](SimpleNamespace(json=send),profile,['retry',replacement])
|
|
self.assertEqual(calls[0],calls[1])
|
|
self.assertEqual(calls[0]['request_id'],replacement)
|
|
self.assertEqual(calls[0]['replaces_deleted'],ident)
|
|
self.assertEqual(calls[0]['wiki_suffix'],payload['wiki_suffix'])
|
|
self.assertEqual(calls[0]['wiki_body'],payload['wiki_body'])
|
|
self.assertEqual(profile.data['task_outbox'],{})
|
|
self.assertIn(replacement,error.getvalue())
|
|
self.assertNotIn('Private',error.getvalue())
|
|
|
|
def test_recreate_deleted_save_failure_keeps_original_and_sends_nothing(self):
|
|
ident='b5550cab-22d4-54eb-b06d-3b432d3f5101'
|
|
payload={'request_id':ident,'title':'Draft'}
|
|
def fail():raise m['Failure']('disk full')
|
|
profile=SimpleNamespace(data={'task_outbox':{ident:payload.copy()}},save=fail)
|
|
calls=[]
|
|
with self.assertRaises(m['Failure']):m['tasks'](SimpleNamespace(json=lambda *a,**kw:calls.append(kw)),profile,['retry',ident,'--recreate-deleted'])
|
|
self.assertEqual(calls,[])
|
|
self.assertEqual(profile.data['task_outbox'],{ident:payload})
|
|
|
|
def test_recreate_deleted_never_clobbers_different_successor_draft(self):
|
|
import uuid
|
|
ident='b5550cab-22d4-54eb-b06d-3b432d3f5101'
|
|
replacement=str(uuid.uuid5(uuid.NAMESPACE_URL,'pages:task-recreate:'+ident))
|
|
pending={ident:{'request_id':ident,'title':'Original'},replacement:{'request_id':replacement,'title':'Different'}}
|
|
before=json.loads(json.dumps(pending))
|
|
calls=[]
|
|
profile=SimpleNamespace(data={'task_outbox':pending},save=lambda:calls.append('save'))
|
|
with self.assertRaises(m['Failure']):m['tasks'](SimpleNamespace(json=lambda *a,**kw:calls.append('send')),profile,['retry',ident,'--recreate-deleted'])
|
|
self.assertEqual(calls,[])
|
|
self.assertEqual(pending,before)
|
|
|
|
def test_mail_path_priority_and_dates(self):
|
|
import datetime,hashlib
|
|
ident='06cb3a24-61f8-4a9f-a0bf-89eea9632f25'
|
|
suffix=m['task_mail_suffix'];now=datetime.datetime(2030,2,1,tzinfo=datetime.timezone.utc)
|
|
for marker in ('[hogehoge 00123]','Re: [hogehoge:123]','Fwd: [hogehoge#123]'):
|
|
raw=('Subject: '+marker+' Title\r\n\r\nbody').encode()
|
|
self.assertEqual(suffix(raw,ident,now),'hogehoge/123')
|
|
raw=b'Subject: Plain\r\nDate: Sat, 31 Jan 2026 23:30:00 -0800\r\nMessage-ID: <id@example.invalid>\r\n\r\nbody'
|
|
self.assertEqual(suffix(raw,ident,now),'2026/01/'+ident)
|
|
noid=raw.replace(b'Message-ID: <id@example.invalid>\r\n',b'')
|
|
self.assertEqual(suffix(noid,ident,now),'2026/01/'+hashlib.sha256(noid).hexdigest())
|
|
for date in (b'',b'Date: broken\r\n'):
|
|
raw=b'Subject: [../unsafe 123] Title\r\n'+date+b'\r\nbody'
|
|
self.assertEqual(suffix(raw,ident,now),'2030/02/'+hashlib.sha256(raw).hexdigest())
|
|
|
|
def test_pending_mail_keeps_month_but_replaces_changed_options(self):
|
|
from unittest.mock import patch
|
|
raw=b'Subject: Test\r\nMessage-ID: <same@example.invalid>\r\n\r\nbody'
|
|
calls=[]
|
|
def send(route,**kw):
|
|
calls.append(kw['data'].copy())
|
|
raise m['Failure']('offline')
|
|
client=SimpleNamespace(base='https://example.invalid',json=send)
|
|
profile=SimpleNamespace(data={'username':'test'},save=lambda:None)
|
|
for month in ('2030/01','2030/02'):
|
|
with patch.dict(m['tasks'].__globals__,task_mail_suffix=lambda raw,ident:month+'/'+ident),patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(raw))),contextlib.redirect_stderr(io.StringIO()),self.assertRaises(m['Failure']):
|
|
m['tasks'](client,profile,['from-mail'])
|
|
self.assertEqual(calls[0],calls[1])
|
|
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(raw))),contextlib.redirect_stderr(io.StringIO()),self.assertRaises(m['Failure']):
|
|
m['tasks'](client,profile,['from-mail','--wiki-prefix','/changed'])
|
|
self.assertEqual(len(calls),3)
|
|
self.assertEqual(calls[2]['request_id'],calls[0]['request_id'])
|
|
self.assertEqual(calls[2]['wiki_prefix'],'/changed')
|
|
self.assertEqual(next(iter(profile.data['task_outbox'].values())),calls[2])
|
|
|
|
def test_changed_capture_is_saved_before_send_and_retained_on_conflict(self):
|
|
ident='b5550cab-22d4-54eb-b06d-3b432d3f5101'
|
|
saved,calls=[],[]
|
|
old={'request_id':ident,'title':'Old draft','notes':'Old private notes'}
|
|
profile=SimpleNamespace(data={'task_outbox':{ident:old}})
|
|
profile.save=lambda:saved.append(json.loads(json.dumps(profile.data)))
|
|
def send(route,**kw):
|
|
payload=kw['data'].copy();calls.append(payload)
|
|
self.assertEqual(saved[-1]['task_outbox'][ident],payload)
|
|
if len(calls)==1:raise m['Failure']('conflict',412,{'error':'conflict'})
|
|
return {'item':{'uuid':ident}}
|
|
client=SimpleNamespace(json=send)
|
|
out,err=io.StringIO(),io.StringIO()
|
|
with contextlib.redirect_stdout(out),contextlib.redirect_stderr(err):
|
|
with self.assertRaises(m['Failure']) as failure:
|
|
m['tasks'](client,profile,['add','New draft','--request-id',ident,'--notes','New private notes'])
|
|
self.assertEqual(failure.exception.status,412)
|
|
self.assertEqual(profile.data['task_outbox'][ident],calls[0])
|
|
self.assertEqual(len(calls),1) # No automatic server overwrite/retry.
|
|
m['tasks'](client,profile,['retry',ident])
|
|
self.assertEqual(calls[0],calls[1])
|
|
self.assertEqual(calls[0]['title'],'New draft')
|
|
self.assertEqual(profile.data['task_outbox'],{})
|
|
self.assertIn('Replaced the retained draft',err.getvalue())
|
|
self.assertNotIn('private notes',err.getvalue())
|
|
self.assertEqual(json.loads(out.getvalue())['item']['uuid'],ident)
|
|
|
|
def test_replacing_draft_does_not_send_when_local_save_fails(self):
|
|
ident='b5550cab-22d4-54eb-b06d-3b432d3f5101'
|
|
def fail_save():raise m['Failure']('local write failed')
|
|
profile=SimpleNamespace(data={'task_outbox':{ident:{'request_id':ident,'title':'Old','notes':''}}},save=fail_save)
|
|
calls=[]
|
|
client=SimpleNamespace(json=lambda *a,**kw:calls.append(kw))
|
|
with self.assertRaises(m['Failure']):m['tasks'](client,profile,['add','New','--request-id',ident])
|
|
self.assertEqual(calls,[])
|
|
|
|
def test_full_outbox_allows_replacement_but_not_new_id(self):
|
|
import uuid
|
|
pending={str(uuid.uuid4()):{'title':'Old'} for _ in range(20)}
|
|
ident=next(iter(pending))
|
|
profile=SimpleNamespace(data={'task_outbox':pending},save=lambda:None)
|
|
calls=[]
|
|
def send(route,**kw):
|
|
calls.append(kw['data'].copy())
|
|
raise m['Failure']('offline')
|
|
client=SimpleNamespace(json=send)
|
|
with contextlib.redirect_stderr(io.StringIO()),self.assertRaises(m['Failure']):
|
|
m['tasks'](client,profile,['add','Replacement','--request-id',ident])
|
|
self.assertEqual(len(calls),1)
|
|
self.assertEqual(pending[ident]['title'],'Replacement')
|
|
with self.assertRaises(m['Failure']):m['tasks'](client,profile,['add','Another'])
|
|
self.assertEqual(len(calls),1)
|
|
self.assertEqual(len(pending),20)
|
|
|
|
def test_reimported_mail_replaces_body_and_removes_obsolete_options(self):
|
|
from unittest.mock import patch
|
|
raw=b'Subject: Old\r\nMessage-ID: <same@example.invalid>\r\n\r\nOLD BODY'
|
|
calls=[]
|
|
def send(route,**kw):
|
|
calls.append(kw['data'].copy())
|
|
raise m['Failure']('offline')
|
|
client=SimpleNamespace(base='https://example.invalid',json=send)
|
|
profile=SimpleNamespace(data={'username':'test'},save=lambda:None)
|
|
changed=raw.replace(b'Subject: Old',b'Subject: New').replace(b'OLD BODY',b'NEW BODY')
|
|
for mail,args in [(raw,['--wiki-prefix','/old']),(changed,['--wiki-prefix','/new']),(changed,['--metadata-only'])]:
|
|
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(mail))),contextlib.redirect_stderr(io.StringIO()),self.assertRaises(m['Failure']):
|
|
m['tasks'](client,profile,['from-mail',*args])
|
|
self.assertEqual(len({c['request_id'] for c in calls}),1)
|
|
self.assertEqual(calls[1]['title'],'New')
|
|
self.assertIn('NEW BODY',calls[1]['wiki_body'])
|
|
self.assertNotIn('OLD BODY',calls[1]['wiki_body'])
|
|
for key in ('wiki_body','wiki_prefix','wiki_suffix'):self.assertNotIn(key,calls[2])
|
|
self.assertEqual(next(iter(profile.data['task_outbox'].values())),calls[2])
|
|
|
|
def test_mail_privacy_and_long_subject_override(self):
|
|
raw=b'Subject: Test\r\nFrom: Person <person@example.invalid>\r\nMessage-ID: <id@example.invalid>\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nPRIVATE BODY'
|
|
title,notes,ident,body=m['task_mail'](raw)
|
|
self.assertEqual(title,'Test');self.assertNotIn('PRIVATE BODY',notes);self.assertIn('Message-ID:',notes)
|
|
self.assertIn('PRIVATE BODY',body)
|
|
self.assertIsNone(m['task_mail'](raw,False)[3])
|
|
raw=raw.replace(b'Subject: Test',b'Subject: '+b'a'*301)
|
|
self.assertLessEqual(len(m['task_mail'](raw)[0].encode()),300)
|
|
with self.assertRaises(m['Failure']):m['task_mail'](raw,True,'x'*301)
|
|
self.assertEqual(m['task_mail'](raw,False,'Override')[0],'Override')
|
|
|
|
def test_japanese_subject_and_mailing_list_markers(self):
|
|
from email.message import EmailMessage
|
|
from email.header import Header
|
|
for encoding in ('utf-8','iso-2022-jp'):
|
|
subject='Re: [hogehoge 00123] [team:456] 日本語の依頼 [重要]'
|
|
raw=('Subject: '+Header(subject,encoding).encode()+'\r\nMessage-ID: <same@example.invalid>\r\n\r\nbody').encode('ascii')
|
|
title,notes,ident,body=m['task_mail'](raw)
|
|
self.assertEqual(title,'Re: 日本語の依頼 [重要]')
|
|
self.assertEqual(ident,'<same@example.invalid>');self.assertIn('Subject: '+subject,body)
|
|
for subject in ('[重要] 日本語','報告 [list 123]','[release 1.2] 更新'):
|
|
self.assertEqual(m['mail_subject'](subject),subject)
|
|
msg=EmailMessage();msg['Subject']='[list 123] '+('日本語'*100);msg.set_content('Body')
|
|
title,_,_,body=m['task_mail'](msg.as_bytes())
|
|
self.assertLessEqual(len(title.encode()),300);self.assertTrue(title.endswith('…'))
|
|
self.assertIn('日本語'*100,body)
|
|
self.assertEqual(m['task_mail'](msg.as_bytes(),True,'[list 123] 手動指定')[0],'[list 123] 手動指定')
|
|
|
|
def test_html_mail_and_limits(self):
|
|
raw=b'Subject: HTML\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<head><style>secret</style></head><p>Hello</p><script>evil()</script><img src="https://example.invalid/image"><p>World & friends</p>'
|
|
body=m['task_mail'](raw)[3]
|
|
self.assertIn('Hello\n',body);self.assertIn('World & friends',body)
|
|
for value in ('secret','evil','https://'):self.assertNotIn(value,body)
|
|
for body in (b'a'*(256<<10),b'bad\0text'):
|
|
with self.assertRaises(m['Failure']):m['task_mail'](b'Subject: Test\r\n\r\n'+body)
|
|
|
|
def test_mail_payload_wiki_and_metadata_only(self):
|
|
from unittest.mock import patch
|
|
raw=b'Subject: Test\r\nMessage-ID: <same@example.invalid>\r\n\r\nPRIVATE BODY'
|
|
calls=[]
|
|
client=SimpleNamespace(base='https://example.invalid',json=lambda route,**kw:calls.append(kw['data'].copy()) or {'item':{'uuid':kw['data']['request_id']}})
|
|
profile=SimpleNamespace(data={'username':'test'},save=lambda:None)
|
|
for args in (['--wiki-prefix','/private/mail'],['--metadata-only']):
|
|
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(raw))),contextlib.redirect_stdout(io.StringIO()):
|
|
m['tasks'](client,profile,['from-mail',*args])
|
|
self.assertIn('PRIVATE BODY',calls[0]['wiki_body']);self.assertNotIn('PRIVATE BODY',calls[0]['notes'])
|
|
self.assertEqual(calls[0]['wiki_prefix'],'/private/mail')
|
|
self.assertNotIn('wiki_body',calls[1]);self.assertEqual(calls[0]['request_id'],calls[1]['request_id'])
|
|
|
|
def test_multipart_prefers_plain_and_skips_attachment(self):
|
|
from email.message import EmailMessage
|
|
msg=EmailMessage();msg['Subject']='Mixed';msg.set_content('Plain body')
|
|
msg.add_alternative('<p>HTML alternate</p>',subtype='html')
|
|
msg.add_attachment(b'ATTACHMENT',maintype='application',subtype='octet-stream',filename='secret.bin')
|
|
body=m['task_mail'](msg.as_bytes())[3]
|
|
self.assertIn('Plain body',body);self.assertNotIn('alternate',body);self.assertNotIn('ATTACHMENT',body)
|
|
|
|
def test_capture_retains_request_until_explicit_retry(self):
|
|
calls=[]
|
|
def send(route,**kwargs):
|
|
calls.append(kwargs['data'].copy())
|
|
if len(calls)==1:raise m['Failure']('offline')
|
|
return {'item':{'uuid':kwargs['data']['request_id']}}
|
|
profile=SimpleNamespace(data={},save=lambda:None)
|
|
client=SimpleNamespace(json=send)
|
|
with contextlib.redirect_stdout(io.StringIO()),contextlib.redirect_stderr(io.StringIO()):
|
|
with self.assertRaises(m['Failure']):m['tasks'](client,profile,['add','Offline task'])
|
|
ident=next(iter(profile.data['task_outbox']))
|
|
m['tasks'](client,profile,['retry',ident])
|
|
self.assertEqual(calls[0],calls[1]);self.assertEqual(profile.data['task_outbox'],{})
|
|
|
|
def test_neomutt_query_columns_are_not_forgeable(self):
|
|
client=SimpleNamespace(json=lambda *a,**kw:{'items':[{'name':'A\tB\nC','emails':['a@example.invalid'],'organization':'Org\nInjected'}]})
|
|
out=io.StringIO()
|
|
with contextlib.redirect_stdout(out):m['contacts'](client,['query','A','--neomutt'])
|
|
rows=out.getvalue().splitlines();self.assertEqual(len(rows),2);self.assertEqual(len(rows[1].split('\t')),3)
|
|
|
|
def test_command_help_without_credentials(self):
|
|
for cmd in [('task','--help'),('contacts','--help'),('task','add','--help')]:
|
|
with contextlib.redirect_stdout(io.StringIO()),self.assertRaises(SystemExit) as raised:m['main'](list(cmd))
|
|
self.assertEqual(raised.exception.code,0)
|
|
|
|
def test_neomutt_query_leading_dash_is_data(self):
|
|
calls=[]
|
|
def query(route):
|
|
calls.append(route)
|
|
return {'items':[{'name':'Person','emails':['person@example.invalid'],'organization':''}]}
|
|
with contextlib.redirect_stdout(io.StringIO()):
|
|
m['contacts'](SimpleNamespace(json=query),['query','--neomutt','--','--unexpected'])
|
|
self.assertIn('q=--unexpected',calls[0])
|