import hashlib, json, unittest
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1]
class Fixtures(unittest.TestCase):
    def test_envelope_hashes(self):
        for f in (ROOT/'fixtures').glob('*.json'):
            e=json.loads(f.read_text());self.assertEqual(hashlib.sha256(e['payload_json'].encode()).hexdigest(),e['content_hash'],f.name)
    def test_all_vessel_and_snapshot(self):
        for f in (ROOT/'fixtures').glob('*.json'):
            e=json.loads(f.read_text());p=json.loads(e['payload_json']);inner=p.get('header',p.get('chunk',p))
            self.assertEqual(e['vessel_id'],inner['vessel_id'],f.name)
            self.assertTrue(e['entity_key'].startswith(inner['snapshot_id']),f.name)
    def test_review_only_target(self):
        code=(ROOT/'receiver/ym-sync-catalog-v2-atomic-review.php').read_text()
        self.assertIn("const DB_NAME = 'yachtmemory_catalog_v2_test';",code)
        self.assertIn('SELECT vessel_id FROM telemetry_vessels WHERE vessel_id=? FOR UPDATE',code)
    def test_conflicting_same_sequence(self):
        a=json.loads((ROOT/'fixtures/conflict-header-a.json').read_text())
        b=json.loads((ROOT/'fixtures/conflict-header-b.json').read_text())
        self.assertEqual(a['seq_no'],b['seq_no']);self.assertNotEqual(a['content_hash'],b['content_hash'])
