forked from mtusi2026/mtusi2026lab4
72 lines
3.2 KiB
Python
72 lines
3.2 KiB
Python
"""Inject missing metadata and missing bytes in an isolated test repository."""
|
|
|
|
import hashlib
|
|
import io
|
|
import json
|
|
import time
|
|
from datetime import datetime, timedelta, timezone
|
|
from uuid import uuid4
|
|
|
|
from attachments import Objects
|
|
from db import Store
|
|
from reconcile import scan
|
|
|
|
|
|
def main():
|
|
store, objects = Store(), Objects()
|
|
user, chat, missing, used, orphan, fresh = [uuid4() for _ in range(6)]
|
|
raw = b"synthetic reconciler fixture"
|
|
digest = hashlib.sha256(raw).hexdigest()
|
|
all_keys = [objects.key(item) for item in (missing, used, orphan, fresh)]
|
|
|
|
def put(item):
|
|
objects.put(objects.key(item), io.BytesIO(raw), "image/png", len(raw))
|
|
|
|
try:
|
|
with store.pool.connection() as db:
|
|
db.execute("INSERT INTO users (id,username,display_name,password_hash) VALUES(%s,%s,'test','inactive')",
|
|
(user, "reconcile_" + user.hex[:12]))
|
|
db.execute("INSERT INTO chats(id,title) VALUES(%s,'reconciler fixture')", (chat,))
|
|
db.execute("INSERT INTO chat_members VALUES(%s,%s,1)", (chat, user))
|
|
for item in (missing, used):
|
|
db.execute("""
|
|
INSERT INTO attachments(id,chat_id,owner_id,filename,content_type,size_bytes,sha256,created_at)
|
|
VALUES(%s,%s,%s,'fixture.png','image/png',%s,%s,clock_timestamp()-interval '20 seconds')
|
|
""", (item, chat, user, len(raw), digest))
|
|
db.execute("""
|
|
INSERT INTO messages(id,chat_id,sender_id,text,attachment_ids)
|
|
VALUES(%s,%s,%s,'uses a real object',ARRAY[%s]::uuid[])
|
|
""", (uuid4(), chat, user, used))
|
|
put(used)
|
|
put(orphan)
|
|
time.sleep(2.5)
|
|
put(fresh)
|
|
cutoff = datetime.now(timezone.utc) - timedelta(seconds=1.5)
|
|
dry = scan(store, objects, cutoff)
|
|
assert str(orphan) in dry["orphan"] and str(missing) in dry["missing"]
|
|
assert str(fresh) not in dry["orphan"] and objects.has(objects.key(orphan))
|
|
fixed = scan(store, objects, cutoff, apply=True)
|
|
assert str(orphan) in fixed["orphan"] and str(missing) in fixed["missing"]
|
|
assert not objects.has(objects.key(orphan)) and objects.has(objects.key(used))
|
|
assert objects.has(objects.key(fresh))
|
|
again = scan(store, objects, cutoff, apply=True)
|
|
assert str(orphan) not in again["orphan"] and str(missing) not in again["missing"]
|
|
put(missing)
|
|
recovery = scan(store, objects, cutoff, apply=True)
|
|
assert str(missing) in recovery["restored"]
|
|
print(json.dumps({"dry_run": dry, "apply": fixed, "repeat": again, "recover": recovery}, indent=2))
|
|
finally:
|
|
for key in all_keys:
|
|
objects.delete(key)
|
|
with store.pool.connection() as db:
|
|
db.execute("DELETE FROM messages WHERE chat_id=%s", (chat,))
|
|
db.execute("DELETE FROM attachments WHERE chat_id=%s", (chat,))
|
|
db.execute("DELETE FROM chat_members WHERE chat_id=%s", (chat,))
|
|
db.execute("DELETE FROM chats WHERE id=%s", (chat,))
|
|
db.execute("DELETE FROM users WHERE id=%s", (user,))
|
|
store.pool.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|