883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
107 lines
7.7 KiB
Python
107 lines
7.7 KiB
Python
"""Public references are provenance, never permission to fabricate engine data."""
|
|
import hashlib,json,sys,tempfile,unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
from urllib.error import HTTPError
|
|
ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT/'ops'))
|
|
import content_db_reference as ref
|
|
|
|
# Minimal factual excerpt from the retained Capy52322 capture. The sidebar's
|
|
# shifted labels must not override its original detail table.
|
|
HTML=b'''<div class="detail-identity" data-entity-key="spell:52322"><h1>+5 to All Weapons</h1></div>
|
|
<table class="grid" id="spelldetails"><tr><th>School</th><td>Physical</td></tr>
|
|
<tr><th>Mechanic</th><td>n/a</td></tr>
|
|
<tr><th>Effect #1</th><td>(6) Apply Aura #98: Mod Skill Talent (162)<br>Value: 5</td></tr></table>
|
|
<aside><dl><dt>School</dt><dd>n/a</dd><dt>Mechanic</dt><dd>Physical</dd></dl></aside>
|
|
<section><h2>Used By</h2><a href="/itemset/674">Armor of Wrath</a></section>
|
|
<section><h2>Referenced by</h2><a href="/item/47251">Armor piece</a></section>'''
|
|
|
|
class ReferenceTest(unittest.TestCase):
|
|
def test_original_fields_and_typed_relations_preserve_source_meaning(self):
|
|
r=ref.parse('capy','spell',52322,HTML)
|
|
self.assertTrue(r['usable_as_content']);self.assertFalse(r['engine_mapping_verified'])
|
|
self.assertEqual(r['fields']['School'],'Physical');self.assertEqual(r['fields']['Mechanic'],'n/a')
|
|
self.assertEqual(r['effects'][0]['displayed_effect_type'],6);self.assertEqual(r['effects'][0]['displayed_aura_type'],98)
|
|
self.assertEqual([(x['section'],x['target']['kind'],x['target']['id'])for x in r['relations']],[('Used By','itemset',674),('Referenced by','item',47251)])
|
|
self.assertEqual(r['body_sha256'],hashlib.sha256(HTML).hexdigest());self.assertEqual(r,ref.parse('capy','spell',52322,HTML))
|
|
def test_record_identity_and_browser_verification_are_not_missing_data(self):
|
|
self.assertEqual(ref.parse('capy','spell',52323,HTML)['status'],'record-identity-not-proven')
|
|
self.assertEqual(ref.parse('capy','spell',52322,HTML.replace(b'detail-identity',b'related-record'))['status'],'record-identity-not-proven')
|
|
r=ref.parse('capy','spell',52322,HTML+b'<p>Verifying your browser</p>')
|
|
self.assertEqual(r['status'],'browser-verification-required');self.assertFalse(r['usable_as_content'])
|
|
def test_conflicting_primary_fields_remain_explicit(self):
|
|
extra=b'<table class="grid"><tr><th>School</th><td>Frost</td></tr></table>'
|
|
r=ref.parse('capy','spell',52322,HTML+extra)
|
|
self.assertNotIn('School',r['fields']);self.assertEqual(r['field_conflicts']['School'],['Frost','Physical'])
|
|
def test_trigger_links_stay_typed_without_assuming_every_link_is_a_trigger(self):
|
|
extra=b'<table class="grid"><tr><th>Effect #2</th><td>(6) Apply Aura #42: Proc Trigger Spell <a href="/spell/15494">Fury</a></td></tr></table>'
|
|
r=ref.parse('capy','spell',52322,HTML+extra);self.assertEqual(r['effects'][1]['links'][0]['id'],15494)
|
|
self.assertEqual(r['effects'][1]['links'][0]['kind'],'spell')
|
|
def test_foreign_script_empty_and_invalid_id_links_do_not_create_relationships(self):
|
|
for link in('javascript:alert(1)','https://example.org/spell/15494','/spell/0','/spell/-1','/spell/2147483648','/spell/'+'9'*200,''):
|
|
self.assertIsNone(ref.linked_identity(link,'https://db.capycraft.org/spell/52322'),link)
|
|
self.assertEqual(ref.linked_identity('?spell=15494','https://octowow.st/db/?item=11684')['id'],15494)
|
|
for ident in(True,0,-1,2**31):
|
|
with self.assertRaises(ValueError):ref.url('capy','spell',ident)
|
|
def test_scripts_are_not_executed_or_promoted_to_displayed_content(self):
|
|
raw=HTML+b'<script>bad_side_effect();</script>'
|
|
self.assertEqual(ref.parse('capy','spell',52322,raw)['fields'],ref.parse('capy','spell',52322,HTML)['fields'])
|
|
def test_all_published_categories_keep_distinct_identifiers(self):
|
|
for kind in ref.KINDS:
|
|
raw=('<div class="detail-identity" data-entity-key="'+kind+':123"><h1>Record</h1></div>'
|
|
'<section><h2>Related</h2><a href="/spell/123">A distinct spell</a></section>').encode()
|
|
r=ref.parse('capy',kind,123,raw)
|
|
self.assertTrue(r['usable_as_content'],kind)
|
|
self.assertEqual(r['kind'],kind)
|
|
self.assertEqual(r['relations'][0]['target']['kind'],'spell')
|
|
self.assertEqual(ref.url('octo',kind,123),'https://octowow.st/db/?'+kind+'=123')
|
|
def test_related_loot_rewards_and_objectives_are_not_dropped_or_called_triggers(self):
|
|
for section in ('Related','Dropped By','Rewards','Objectives','Sells','Starts','Ends'):
|
|
raw=HTML+('<section><h2>'+section+'</h2><a href="/spell/49368">Passive</a>'
|
|
'<a href="/quest/123">Quest</a></section>').encode()
|
|
r=ref.parse('capy','spell',52322,raw)
|
|
rows=[e for e in r['relations']if e['section']==section]
|
|
self.assertEqual([(e['target']['kind'],e['target']['id'])for e in rows],[('spell',49368),('quest',123)])
|
|
self.assertNotIn('trigger',rows[0])
|
|
def test_identity_heading_and_icon_are_scoped_to_the_actual_record(self):
|
|
raw=b'<h1>Navigation</h1>'+HTML.replace(b'<h1>',b'<img src="/images/icons/large/test.png"><h1>')
|
|
r=ref.parse('capy','spell',52322,raw)
|
|
self.assertEqual(r['name'],'+5 to All Weapons')
|
|
self.assertEqual(r['displayed_icon_url'],'https://db.capycraft.org/images/icons/large/test.png')
|
|
def test_graph_keeps_typed_cycles_and_duplicate_evidence_finite(self):
|
|
spell=ref.parse('capy','spell',52322,HTML)
|
|
item_html=b'<div class="detail-identity" data-entity-key="item:52322"><h1>Item</h1></div><section><h2>Related</h2><a href="/spell/52322">Spell</a></section>'
|
|
item=ref.parse('capy','item',52322,item_html)
|
|
spell['relations'].append(dict(section='Used By',target=dict(kind='item',id=52322),label='Item'))
|
|
graph=ref.correlate([spell,item,spell,item])
|
|
self.assertEqual(len(graph['nodes']),2)
|
|
self.assertIn('spell:52322',[n['key']for n in graph['nodes']])
|
|
self.assertIn('item:52322',[n['key']for n in graph['nodes']])
|
|
edges={(e['source'],e['target'])for e in graph['edges']}
|
|
self.assertIn(('spell:52322','item:52322'),edges);self.assertIn(('item:52322','spell:52322'),edges)
|
|
self.assertTrue(all(len(e['evidence'])==1 for e in graph['edges']))
|
|
self.assertFalse(graph['installed']);self.assertFalse(graph['engine_mapping_verified'])
|
|
def test_preferred_octo_capture_does_not_hide_cross_source_or_same_source_conflicts(self):
|
|
import copy
|
|
capy=ref.parse('capy','spell',52322,HTML)
|
|
octo=copy.deepcopy(capy);octo.update(site='octo',url=ref.url('octo','spell',52322),body_sha256='a'*64)
|
|
octo['fields']['School']='Fire'
|
|
r=ref.correlate([capy,octo])['nodes'][0]
|
|
self.assertEqual(r['preferred_site'],'octo');self.assertEqual(r['field_disagreements']['School'],['Fire','Physical'])
|
|
newer=copy.deepcopy(octo);newer['body_sha256']='b'*64
|
|
r=ref.correlate([capy,octo,newer])['nodes'][0]
|
|
self.assertTrue(r['ambiguous_preferred_capture']);self.assertIsNone(r['preferred_record'])
|
|
def test_capture_hash_mismatch_is_rejected_before_reparsing(self):
|
|
with tempfile.TemporaryDirectory(dir=ROOT/'runtime')as directory:
|
|
p=Path(directory);r=ref.parse('capy','spell',52322,HTML)
|
|
(p/'capy-spell-52322.json').write_text(json.dumps(r));(p/'capy-spell-52322.html').write_bytes(HTML+b'changed')
|
|
with self.assertRaises(ValueError):ref.captured_references([p])
|
|
def test_http404_is_a_lookup_result_not_unreleased_or_a_fabricated_definition(self):
|
|
with tempfile.TemporaryDirectory(dir=ROOT/'runtime')as directory:
|
|
out=Path(directory)/'result.json'
|
|
with patch.object(sys,'argv',['content_db_reference','--site','capy','--kind','spell','--id','473','--output',str(out)]),patch.object(ref,'urlopen',side_effect=HTTPError('https://db.capycraft.org/spell/473',404,'Not Found',{},None)):
|
|
self.assertEqual(ref.main(),2)
|
|
r=json.loads(out.read_text());self.assertEqual(r['status'],'not-indexed-or-unavailable');self.assertFalse(r['usable_as_content']);self.assertNotIn('fields',r)
|
|
|
|
if __name__=='__main__':unittest.main()
|