LifeMate — Personal, Family & Learning Companion
test/personal-recovery-acceptance
chore/phase1-repository-cleanup
deploy/stable-web-apk
deployment-static
design/mobile-accessibility-v2
design/mobile-navigation-review
docs/child-education-access-decision
docs/offline-data-rescue-options
docs/owner-guide-and-delivery-plan
docs/phase1-policy-options
docs/public-review-live-2026-10-15
feat/aparat-learning-library
feat/child-device-safety
feat/child-identity-schema
feat/flutter-mobile-navigation
feat/learning-content-operations
feat/local-signing-preflight
feat/online-family-stage0
feat/personal-encrypted-recovery
feat/private-planner-series-api
feat/public-review-page
feat/standalone-weekly-planner
feat/weekly-planner-android
final-feature-1/iran-family-school
fix/auth-abuse-expiry-safety
fix/auth-expiry-after-lock
fix/backend-request-safety
fix/lifeguide-bootstrap-naming-ci
fix/lifeguide-pages-qa-base-preflight
fix/lifeguide-qa-pages-env-deployment
fix/local-task-persistence
fix/managed-child-session-boundary
fix/personal-jalali-dates
fix/personal-notification-controls
fix/personal-upgrade-delivery-guard
fix/sms-approval-gates
fix/study-duration-display
infra/cloudflare-neon-migration
infra/lifeguide-cloudflare-qa
infra/lifeguide-github-pages-qa
infra/lifeguide-supabase-qa
main
phase-1/foundation-completion
phase-2/product-foundation-identity
phase-3/planner-school-family-core
phase-4/ai-learning-wellbeing
phase-5/hardening-observability-release
test/android-reminder-acceptance
test/personal-notification-settings
test/personal-recovery-acceptance
test/role-entry-apk
| 1 | """End-to-end host checks with fictional data and independently opened SQLite.""" |
| 2 | |
| 3 | from contextlib import redirect_stdout |
| 4 | import hashlib |
| 5 | import io |
| 6 | import json |
| 7 | from pathlib import Path |
| 8 | import sqlite3 |
| 9 | import tempfile |
| 10 | import unittest |
| 11 | from unittest.mock import patch |
| 12 | |
| 13 | from scripts.personal_recovery.common import RecoveryError |
| 14 | from scripts.personal_recovery.snapshot import create_sample_database, export_fixture |
| 15 | from scripts.personal_recovery_cli import demo, main, pack_fixture, restore_fixture |
| 16 | |
| 17 | |
| 18 | class RecoveryCliTests(unittest.TestCase): |
| 19 | def setUp(self): |
| 20 | self.temp = tempfile.TemporaryDirectory() |
| 21 | self.addCleanup(self.temp.cleanup) |
| 22 | self.root = Path(self.temp.name) |
| 23 | # Deliberately fictional test passphrase, not an owner credential. |
| 24 | self.password = "fictional test phrase only" |
| 25 | |
| 26 | def fixture(self): |
| 27 | source = self.root / "source.sqlite" |
| 28 | source.write_bytes(create_sample_database()) |
| 29 | return source |
| 30 | |
| 31 | def test_real_files_close_reopen_full_snapshot_matches_without_source_changes(self): |
| 32 | source = self.fixture() |
| 33 | source_hash = hashlib.sha256(source.read_bytes()).hexdigest() |
| 34 | archive = self.root / "transfer.lgrf" |
| 35 | restored = self.root / "restored.sqlite" |
| 36 | expected = export_fixture(source) |
| 37 | pack_fixture(source, archive, self.password) |
| 38 | restore_fixture(archive, restored, self.password) |
| 39 | self.assertEqual(export_fixture(restored), expected) |
| 40 | self.assertEqual(hashlib.sha256(source.read_bytes()).hexdigest(), source_hash) |
| 41 | self.assertFalse(source.read_bytes()[:64] in archive.read_bytes()) |
| 42 | with sqlite3.connect(f"{restored.as_uri()}?mode=ro", uri=True) as db: |
| 43 | self.assertEqual(db.execute("PRAGMA quick_check").fetchone(), ("ok",)) |
| 44 | self.assertEqual(db.execute("PRAGMA foreign_key_check").fetchall(), []) |
| 45 | for table in ("local_profiles", "local_plans", "local_weekly_slots", "local_occurrences"): |
| 46 | self.assertGreater(db.execute(f"SELECT COUNT(*) FROM {table}").fetchone()[0], 0) |
| 47 | |
| 48 | def test_wrong_password_tamper_and_repeat_never_modify_or_create_destination(self): |
| 49 | source = self.fixture() |
| 50 | archive = self.root / "transfer.lgrf" |
| 51 | destination = self.root / "restored.sqlite" |
| 52 | pack_fixture(source, archive, self.password) |
| 53 | with self.assertRaises(RecoveryError): |
| 54 | restore_fixture(archive, destination, "incorrect fictional phrase") |
| 55 | self.assertFalse(destination.exists()) |
| 56 | raw = bytearray(archive.read_bytes()) |
| 57 | raw[-1] ^= 1 |
| 58 | corrupted = self.root / "corrupted.lgrf" |
| 59 | corrupted.write_bytes(raw) |
| 60 | with self.assertRaises(RecoveryError): |
| 61 | restore_fixture(corrupted, destination, self.password) |
| 62 | self.assertFalse(destination.exists()) |
| 63 | restore_fixture(archive, destination, self.password) |
| 64 | previous = destination.read_bytes() |
| 65 | with self.assertRaises(RecoveryError) as refusal: |
| 66 | restore_fixture(archive, destination, self.password) |
| 67 | self.assertEqual(refusal.exception.code, "destination_exists") |
| 68 | self.assertEqual(destination.read_bytes(), previous) |
| 69 | |
| 70 | def test_unmarked_source_refused_without_archive(self): |
| 71 | source = self.fixture() |
| 72 | with sqlite3.connect(source) as db: |
| 73 | db.execute("PRAGMA application_id=0") |
| 74 | destination = self.root / "refused.lgrf" |
| 75 | with self.assertRaises(RecoveryError): |
| 76 | pack_fixture(source, destination, self.password) |
| 77 | self.assertFalse(destination.exists()) |
| 78 | |
| 79 | def test_missing_acknowledgement_cannot_create_any_files(self): |
| 80 | output = io.StringIO() |
| 81 | with redirect_stdout(output): |
| 82 | code = main(["create-fixture", "--output", str(self.root / "blocked.sqlite")]) |
| 83 | self.assertEqual(code, 2) |
| 84 | self.assertEqual(json.loads(output.getvalue())["reason"], "synthetic_acknowledgement_required") |
| 85 | self.assertEqual(list(self.root.iterdir()), []) |
| 86 | |
| 87 | def test_noninteractive_pack_refuses_without_reading_passphrase(self): |
| 88 | source = self.fixture() |
| 89 | output = io.StringIO() |
| 90 | with patch("sys.stdin.isatty", return_value=False), patch("getpass.getpass") as prompt, redirect_stdout(output): |
| 91 | code = main(["pack", "--source", str(source), "--output", str(self.root / "blocked"), "--acknowledge-synthetic-only"]) |
| 92 | self.assertEqual(code, 2) |
| 93 | self.assertEqual(json.loads(output.getvalue())["reason"], "interactive_terminal_required") |
| 94 | prompt.assert_not_called() |
| 95 | self.assertFalse((self.root / "blocked").exists()) |
| 96 | |
| 97 | def test_failure_output_contains_only_reason_not_payload_paths_or_password(self): |
| 98 | output = io.StringIO() |
| 99 | with patch("scripts.personal_recovery_cli._ask_password", return_value=self.password), redirect_stdout(output): |
| 100 | code = main(["pack", "--source", str(self.root / "missing"), "--output", str(self.root / "none"), "--acknowledge-synthetic-only"]) |
| 101 | self.assertEqual(code, 2) |
| 102 | report = json.loads(output.getvalue()) |
| 103 | self.assertEqual(set(report), {"status", "reason", "syntheticOnly"}) |
| 104 | self.assertNotIn(str(self.root), output.getvalue()) |
| 105 | self.assertNotIn(self.password, output.getvalue()) |
| 106 | |
| 107 | def test_demo_receipt_is_sanitized_and_records_independent_checks(self): |
| 108 | directory = self.root / "demo" |
| 109 | receipt = demo(directory) |
| 110 | self.assertEqual(receipt["result"], "PASS") |
| 111 | self.assertTrue(receipt["syntheticOnly"]) |
| 112 | self.assertTrue(all(value == "PASS" for value in receipt["checks"].values())) |
| 113 | self.assertEqual(receipt["limitations"]["androidSideBySide"], "UNVERIFIED") |
| 114 | self.assertNotIn("display_name", json.dumps(receipt)) |
| 115 | self.assertNotIn("password", json.dumps(receipt).lower()) |
| 116 | with self.assertRaises(RecoveryError): |
| 117 | demo(directory) |
| 118 | |
| 119 | |
| 120 | if __name__ == "__main__": |
| 121 | unittest.main() |