feat: V0.1.53 location timeline and durable device conversations

This commit is contained in:
Codex
2026-10-19 07:27:48 +08:00
parent 5c09a2025a
commit ad2614719b
33 changed files with 609 additions and 92 deletions
+141
View File
@@ -0,0 +1,141 @@
"""Durable voice archive, consent boundary, paging, integrity and repair regressions."""
import base64
import hashlib
import io
import time
import unittest
from unittest.mock import patch
import test_family_voice as fixtures
app=fixtures.app
audio=fixtures.audio
class VoiceHistoryTests(unittest.TestCase):
setUp=fixtures.VoiceTests.setUp
stop_server=fixtures.VoiceTests.stop_server
phone=fixtures.VoiceTests.phone
call=fixtures.VoiceTests.call
receive=fixtures.VoiceTests.receive
send=fixtures.VoiceTests.send
def pair_of_phones(self):
a,b=self.phone('甲'),self.phone('乙')
self.receive(b,a)
return a,b
def peer(self,phone):
return self.call(phone,'status')['me']['archiveKey']
def test_long_term_history_survives_delivery_expiry_mute_and_restart(self):
a,b=self.pair_of_phones();msg=self.send(a,b);key=msg['id']
self.call(b,'configure',enabled=False,allowed=[])
with patch('family_voice.time.time',return_value=time.time()+86400*400):app.VOICE.sweep()
# Recipient switch controls new delivery, not their existing conversations.
for p in (a,b):self.assertEqual(self.call(p,'replay',id=key)['audio'],audio())
app.VOICE.initialize()
self.assertEqual(self.call(b,'replay',id=key)['audio'],audio())
history=self.call(a,'history',peer=self.peer(b))
self.assertEqual(len(history['items']),1)
self.assertEqual(history['items'][0]['senderName'],'甲的手机')
self.assertTrue(history['items'][0]['mine'])
self.assertEqual(self.call(a,'status')['conversations'][0]['archiveKey'],self.peer(b))
def test_cloud_verification_required_before_outbox_delete(self):
a,b=self.pair_of_phones();key=self.send(a,b)['id'];archive=app.VOICE.history
local=archive.folder/archive.filename(key);cipher=local.read_bytes()
self.assertNotEqual(cipher,base64.b64decode(audio()))
upload=lambda *args:dict(fs_id=17)
with patch.object(app.BAIDU,'upload',side_effect=upload),patch.object(app.BAIDU,'verify_file',side_effect=ValueError('bad checksum')):
archive.tick()
self.assertTrue(local.exists());self.assertEqual(archive.existing(key)['state'],'retry')
app.DB.execute('UPDATE voice_history SET next_try=0')
with patch.object(app.BAIDU,'upload',side_effect=upload),patch.object(app.BAIDU,'verify_file') as verify:
archive.tick()
verify.assert_called_once()
self.assertFalse(local.exists());row=archive.existing(key);self.assertEqual(row['state'],'cloud')
self.assertIn('/家庭沟通/'+self.aid+'/',row['remote'])
with patch.object(app.BAIDU,'open_file',return_value=io.BytesIO(cipher)):
self.assertEqual(self.call(b,'replay',id=key)['audio'],audio())
with patch.object(app.BAIDU,'open_file',return_value=io.BytesIO(cipher[:-1]+b'!')):
with self.assertRaises(app.Problem):self.call(a,'replay',id=key)
def test_third_person_and_other_family_cannot_read_or_replay(self):
a,b=self.pair_of_phones();c,d=self.phone('同家第三人'),self.phone('别家',self.other)
key=self.send(a,b)['id']
for stranger in (c,d):
self.assertEqual(self.call(stranger,'history',peer=self.peer(a))['items'],[])
self.assertEqual(self.call(stranger,'status')['conversations'],[])
with self.assertRaises(app.Problem):self.call(stranger,'replay',id=key)
for before in (-1,1.5,True,2**63,'0'):
with self.assertRaises(app.Problem):self.call(a,'history',peer=self.peer(b),before=before)
def test_pair_token_rotation_recovers_same_member_but_invalidates_old_token(self):
a,b=self.pair_of_phones();key=self.send(a,b)['id'];old=self.peer(a)
# The real pairing path replaces the row with a new SID/token for the same member.
row=dict(app.DB.execute('SELECT * FROM member_location_shares WHERE id=?',(a['sid'],)).fetchone())
newtoken='x'*43;newsid='c'*32
app.DB.execute('UPDATE member_location_shares SET id=?,token_hash=? WHERE id=?',(newsid,hashlib.sha256(newtoken.encode()).hexdigest(),a['sid']))
repaired=dict(token=newtoken,sid=newsid)
self.assertEqual(self.peer(repaired),old)
self.assertEqual(self.call(repaired,'replay',id=key)['audio'],audio())
with self.assertRaises(app.Problem):self.call(a,'replay',id=key)
# A different member must never inherit the original member's history.
stranger=self.phone('新成员')
self.assertNotEqual(self.peer(stranger),old)
def test_conversation_pages_are_ordered_no_duplicate_or_time_limit(self):
a,b=self.pair_of_phones();actor=app.VOICE.endpoint('phone:'+a['sid']);target=app.VOICE.endpoint('phone:'+b['sid'])
for n in range(121):
with app.LOCK,app.DB:app.VOICE.history.accept(actor['archiveKey']+':'+f'{n:032x}',actor,target,base64.b64decode(audio()),.64)
before=0;seen=[]
while True:
page=self.call(a,'history',peer=self.peer(b),before=before);ids=[r['seq'] for r in page['items']]
self.assertEqual(ids,sorted(ids));self.assertLessEqual(len(ids),50);seen+=ids
before=page['before']
if not before:break
self.assertEqual(len(seen),121);self.assertEqual(len(set(seen)),121)
def test_duplicate_send_after_restart_and_id_collision(self):
a,b=self.pair_of_phones();original=self.send(a,b);app.VOICE.initialize();self.receive(b,a)
same=self.send(a,b);self.assertEqual(original['id'],same['id'])
self.assertEqual(app.DB.execute('SELECT count(*) FROM voice_history').fetchone()[0],1)
self.assertFalse(app.VOICE.messages)
with self.assertRaises(app.Problem):self.send(a,b,audio=audio(11))
self.call(b,'offline');self.call(b,'configure',enabled=False,allowed=[])
self.assertEqual(self.send(a,b)['id'],original['id'])
with self.assertRaises(app.Problem):self.send(a,b,id='e'*32)
def test_outbox_cap_rejects_without_claiming_delivery(self):
a,b=self.pair_of_phones()
with patch('voice_history.LIMIT',1):
with self.assertRaises(app.Problem):self.send(a,b)
self.assertFalse(app.VOICE.messages)
self.assertEqual(app.DB.execute('SELECT count(*) FROM voice_history').fetchone()[0],0)
def test_replay_rechecks_binding_after_cloud_read(self):
a,b=self.pair_of_phones();key=self.send(a,b)['id'];archive=app.VOICE.history;cipher=(archive.folder/archive.filename(key)).read_bytes()
with patch.object(app.BAIDU,'upload',return_value=dict(fs_id=8)),patch.object(app.BAIDU,'verify_file'):archive.tick()
class RevokingStream(io.BytesIO):
def read(self,*args):
app.DB.execute('DELETE FROM member_location_shares WHERE id=?',(b['sid'],))
return super().read(*args)
with patch.object(app.BAIDU,'open_file',return_value=RevokingStream(cipher)):
with self.assertRaises(app.Problem):self.call(b,'replay',id=key)
def test_display_repair_retains_archive_but_family_move_does_not(self):
a=self.phone('家人');device=app.INVENTORY.save(dict(name='显示终端',deviceType='显示终端'),self.owner)
def pair():
grant=app.DISPLAYS.manage(self.owner,dict(action='create',deviceId=device['id']))
return app.DISPLAYS.pair(dict(code=grant['pairingCode']),'voice-history-test')['nativeToken']
token=pair()
def display(t,action,**kwargs):return app.VOICE.native(t,dict(kind='display',action=action,**kwargs))
me=display(token,'status')['me'];display(token,'configure',enabled=True,allowed=['phone:'+a['sid']],consent=True)
key=self.call(a,'send',target=me['id'],audio=audio(),id='d'*32)['id']
repaired=pair();self.assertEqual(display(repaired,'status')['me']['archiveKey'],me['archiveKey'])
self.assertEqual(display(repaired,'replay',id=key)['audio'],audio())
with self.assertRaises(app.Problem):display(token,'replay',id=key)
app.INVENTORY.transfer(dict(source='device',id=device['id'],targetFamilyId=self.bid),self.root)
with self.assertRaises(app.Problem):display(repaired,'replay',id=key)
if __name__=='__main__':unittest.main()