Files
paper-atlas/tests/test_mm_source_identity.py
T
codex-bot 09a2d4ed20
Metadata integrity checks / metadata (push) Successful in 4m10s
Audit MM program identities without inventing decisions or import records
2026-11-17 22:49:53 +08:00

102 lines
5.7 KiB
Python

import gzip
import hashlib
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
from paper_atlas.collectors import mm2026
from paper_atlas.audits.source_identity import audit, load_inputs, load_mm_program_inputs
class MMProgramIdentityInputTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.fixture = json.loads(gzip.decompress((Path(__file__).parent/'fixtures/mm2026-source.json.gz').read_bytes()))
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.root = Path(self.tmp.name)
src = self.root/'source'
for name,body in self.fixture['files'].items():
path=src/name;path.parent.mkdir(parents=True,exist_ok=True);path.write_bytes(body.encode())
mm2026.collect(self.root/'program', snapshots=src)
self.report_path = self.root/'program/collection-report.json'
def change_report(self, change):
report=json.loads(self.report_path.read_text());change(report)
self.report_path.write_text(json.dumps(report))
def assert_rejected_before_database(self):
with patch('paper_atlas.audits.source_identity.ReadConnection') as connection:
with self.assertRaises(ValueError):
audit(None,self.root/'audit',pg_service='unused',pg_schema='research',mm_program_report=self.report_path)
connection.assert_not_called()
self.assertFalse((self.root/'audit').exists())
def test_all2143_programs_are_unknown_readonly_inputs_not_import_records(self):
rows,inputs=load_mm_program_inputs(self.report_path)
self.assertEqual(len(rows),2143);self.assertEqual(len(inputs),28)
self.assertEqual(sum(len(r['program_contexts']) for r in rows),4326)
actual={r['program_id']:r for r in rows}
for gold in self.fixture['independent_inventory']['inventory']:
r=actual[gold['paper_id']]
self.assertEqual((r['title'],r['authors'],r['track']),
(gold['title'],gold['authors'],gold['track_literal']))
self.assertEqual(r['external_id'],'2026/'+gold['paper_id'])
self.assertEqual(r['status'],'unknown');self.assertEqual(r['identifiers'],{})
self.assertEqual(r['source_observed_status'],'listed_in_official_program')
self.assertFalse(r['publication_identity_verified']);self.assertFalse(r['final_decision_verified'])
self.assertFalse(r['contribution_type_verified'])
self.assertEqual([c['source_pointer'] for c in r['program_contexts']],gold['locations'])
self.assertEqual({r['program_id'] for r in rows if not r['authors']},{'mfp4971','mfp7801'})
self.assertTrue(all('unknown is not a source decision' in i['manifest']['scope'] for i in inputs))
self.assertFalse(any(self.root.rglob('*.manifest.json')))
def test_untrusted_absolute_file_paths_are_not_followed(self):
self.change_report(lambda r:[f.update(file='/untrusted/'+Path(f['file']).name) for f in r['files']])
rows,inputs=load_mm_program_inputs(self.report_path)
self.assertEqual(len(rows),2143)
self.assertTrue(all(Path(i['file']).parent==self.report_path.parent for i in inputs))
def test_program_files_are_not_silently_accepted_by_regular_ingestion_input_loader(self):
report=json.loads(self.report_path.read_text())
with self.assertRaises(FileNotFoundError):load_inputs([report['files'][0]['file']])
def test_corrupt_jsonl_fails_before_connection_or_output(self):
report=json.loads(self.report_path.read_text());path=Path(report['files'][0]['file'])
path.write_bytes(path.read_bytes()+b' ')
self.assert_rejected_before_database()
def test_rehashed_forged_identifiers_decisions_and_contexts_rejected(self):
original=self.report_path.read_bytes();report=json.loads(original);path=Path(report['files'][0]['file']);raw=path.read_bytes()
for field,value in [('identifiers',{'openreview':'invented'}),('status','accepted'),('contexts',[])]:
path.write_bytes(raw);self.report_path.write_bytes(original)
lines=raw.splitlines();row=json.loads(lines[0]);row[field]=value;lines[0]=json.dumps(row).encode()
changed=b'\n'.join(lines)+b'\n';path.write_bytes(changed)
self.change_report(lambda r:r['files'][0].update(file_sha256=hashlib.sha256(changed).hexdigest()))
with self.subTest(field=field):self.assert_rejected_before_database()
def test_missing_or_repeated_track_rejected(self):
original=self.report_path.read_bytes()
for kind in ('missing','repeated'):
self.report_path.write_bytes(original)
if kind=='missing':self.change_report(lambda r:r['files'].pop())
else:self.change_report(lambda r:r['files'].__setitem__(1,r['files'][0]))
with self.subTest(kind=kind):self.assert_rejected_before_database()
def test_raw_source_corruption_or_symlink_rejected(self):
path=self.report_path.parent/'evidence/program-data.json';raw=path.read_bytes()
path.write_bytes(raw+b' ');self.assert_rejected_before_database()
path.write_bytes(raw);saved=path.with_name('original.json');path.rename(saved);path.symlink_to(saved)
self.assert_rejected_before_database()
def test_mixed_input_modes_rejected_before_connection(self):
with patch('paper_atlas.audits.source_identity.ReadConnection') as connection:
for files,program in [([],None),([Path('unused')],self.report_path)]:
with self.assertRaisesRegex(ValueError,'Choose source files'):
audit(files,self.root/'out',pg_service='unused',pg_schema='research',mm_program_report=program)
connection.assert_not_called()