Download test_runtime.py from Thorsu/miha: direct link, hf CLI and curl.
- Browser
- Download file 6.25 kB
-
https://huggingface.co/spaces/Thorsu/miha/resolve/main/test_runtime.py
- Command line
-
hf download hf://spaces/Thorsu/miha/test_runtime.py
-
curl -L -o test_runtime.py https://huggingface.co/spaces/Thorsu/miha/resolve/main/test_runtime.py
6.25 kB
| """Integration tests against real pinned Hub records; no mocks or fixtures.""" | |
| import unittest | |
| from concurrent.futures import ThreadPoolExecutor | |
| from fastapi.testclient import TestClient | |
| from app import app, REVISIONS, LATEST_HASH | |
| class RealRuntime(unittest.TestCase): | |
| def setUpClass(cls): | |
| cls.context = TestClient(app) | |
| cls.client = cls.context.__enter__() | |
| def tearDownClass(cls): | |
| cls.context.__exit__(None, None, None) | |
| def test_live_process_and_full_audit(self): | |
| self.assertEqual(self.client.get('/health').json()['audit_status'], 'PASS') | |
| report = self.client.get('/api/status').json() | |
| self.assertEqual(report['revisions'], REVISIONS) | |
| self.assertEqual(report['canonical_records'], 32) | |
| self.assertEqual(report['examples'], 557) | |
| self.assertEqual(report['bench_overlap'], 0) | |
| self.assertTrue(report['atomic_readback']) | |
| self.assertEqual(report['latest_record']['sha256'], LATEST_HASH) | |
| self.assertEqual(report['latest_record']['examples'], 19) | |
| self.assertEqual(len({r['id'] for r in report['records']}), 32) | |
| def test_model_release_stays_closed(self): | |
| report = self.client.get('/api/status').json()['model_status_snapshot'] | |
| self.assertFalse(report['success']) | |
| self.assertFalse(report['training']['benchmark_complete']) | |
| def test_real_hardware_owner_and_html(self): | |
| data = self.client.get('/api/hardware').json() | |
| self.assertEqual(data['owner'], 'Thorsu') | |
| self.assertEqual(len(data['spaces']), 4) | |
| for space in data['spaces']: | |
| self.assertTrue(space['space'].startswith('Thorsu/')) | |
| page = self.client.get('/') | |
| self.assertEqual(page.status_code, 200) | |
| self.assertIn('MIHA', page.text) | |
| def test_live_health_during_real_rebuild(self): | |
| with ThreadPoolExecutor(max_workers=2) as executor: | |
| rebuild = executor.submit(self.client.post, '/api/audit') | |
| for _ in range(15): | |
| response = self.client.get('/health') | |
| self.assertEqual(response.status_code, 200) | |
| self.assertIn(response.json()['audit_status'], ('RUNNING', 'PASS')) | |
| result = rebuild.result(timeout=120) | |
| self.assertEqual(result.status_code, 200) | |
| self.assertEqual(result.json()['status'], 'PASS') | |
| class CurrentEvidence(unittest.TestCase): | |
| def setUpClass(cls): | |
| from app import refresh_evidence | |
| cls.report = refresh_evidence() | |
| cls.client = TestClient(app) | |
| def test_current_records_and_complete_gpu_registers(self): | |
| from evidence_browser import detail | |
| self.assertIn(self.report['status'], ('PASS', 'PARTIAL')) | |
| if self.report['errors']: | |
| self.assertEqual(self.report['status'], 'PARTIAL') | |
| bad={e['record_id'] for e in self.report['errors']} | |
| self.assertFalse(bad & {r['id'] for r in self.report['records']}) | |
| self.assertGreater(self.report['canonical_records'], 32) | |
| record = detail('miha-22796ce5926a7cb8353fa2af') | |
| import json | |
| nodes = {n['id']: json.loads(n.get('extra_json') or '{}') for n in record['nodes']} | |
| self.assertEqual(len(nodes['project-register']['projects']), 11) | |
| self.assertEqual(len(nodes['measurement-register']['measurements']), 17) | |
| self.assertEqual(len(nodes['ratio-register']['calculations']), 16) | |
| self.assertFalse(self.report['training_started']) | |
| item=next(r for r in self.report['records'] if r['id']==record['record_id']) | |
| self.assertIn('curast',item['search_text'].lower()) | |
| self.assertEqual(self.client.get('/api/evidence').json()['revision'], self.report['revision']) | |
| self.assertEqual(self.client.get('/api/evidence/'+record['record_id']).json(), record) | |
| self.assertEqual(self.client.get('/api/evidence/unknown').status_code, 404) | |
| def test_content_identity_and_embedded_references(self): | |
| import json | |
| from evidence_browser import detail,check_identity_and_references | |
| r=json.loads(json.dumps(detail('miha-22796ce5926a7cb8353fa2af'))) | |
| r['record_id']='miha-wrong' | |
| with self.assertRaises(ValueError):check_identity_and_references(r) | |
| r=json.loads(json.dumps(detail('miha-22796ce5926a7cb8353fa2af'))) | |
| r['backlog_json']=[json.dumps({'source_ids':['missing-source']})] | |
| with self.assertRaises(ValueError):check_identity_and_references(r) | |
| def test_external_links_escape_space_iframe(self): | |
| from html.parser import HTMLParser | |
| class Links(HTMLParser): | |
| def __init__(self): | |
| super().__init__(); self.links=[] | |
| def handle_starttag(self, tag, attrs): | |
| a=dict(attrs) | |
| if tag=='a' and a.get('href','').startswith('https://'): | |
| self.links.append(a) | |
| parser=Links();parser.feed(self.client.get('/').text) | |
| self.assertGreaterEqual(len(parser.links),3) | |
| for a in parser.links: | |
| self.assertEqual(a.get('target'),'_blank') | |
| self.assertIn('noopener',a.get('rel','')) | |
| def test_corrupt_real_record_is_rejected(self): | |
| import json,tempfile | |
| from pathlib import Path | |
| from evidence_browser import detail,inspect_pair | |
| from dataset_pipeline import validate_record | |
| from miha_projection import viewer_row | |
| record=detail('miha-22796ce5926a7cb8353fa2af') | |
| with tempfile.TemporaryDirectory() as root: | |
| canonical=Path(root)/(record['record_id']+'.json') | |
| projection=Path(root)/(record['record_id']+'.jsonl') | |
| projection.write_text(json.dumps(viewer_row(record))) | |
| damaged=json.loads(json.dumps(record));damaged['run']['topic']='tampered' | |
| canonical.write_text(json.dumps(damaged)) | |
| with self.assertRaises(ValueError):inspect_pair(canonical,projection,validate_record,viewer_row) | |
| canonical.write_text(json.dumps(record));projection.write_text('{}') | |
| with self.assertRaises(ValueError):inspect_pair(canonical,projection,validate_record,viewer_row) | |
| if __name__ == '__main__': | |
| unittest.main() | |