Datasets:
File size: 11,220 Bytes
870b85a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | import copy
import hashlib
import json
from pathlib import Path
import tempfile
import unittest
class FusionSupportTests(unittest.TestCase):
def setUp(self):
temporary = tempfile.TemporaryDirectory()
self.addCleanup(temporary.cleanup)
self.evidence_path = Path(temporary.name) / 'surface.json'
self.rug = dict(entity_id='rug', role='rug', room_id='living',
asset_id='native-rug', asset_sha256='a'*64, runtime_sha256='b'*64,
pool_candidate_key='rug-record',
canonical_transform=[1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1])
self.table = dict(entity_id='table', role='dining_table', room_id='living',
asset_id='native-table', support_entity_id='rug', base_z_m=.02,
derived_support_relation=dict(
source='actual_floor_cover_mesh_and_furniture_bottom_contacts',
stage='joint_search_before_freeze', authored_support_parent_entity_id=None,
added_support_parent_entity_id='rug', original_floor_height_m=0.,
vertical_translation_m=.02, original_task_relations_retained=True),
selected_support_surface=dict(status='measured', parent_entity_id='rug',
parent_asset_id='native-rug', source_region_modified=False,
evidence_sha256='c'*64, surface_z_world_m=.02,
parent_source_identity={key: copy.deepcopy(self.rug[key]) for key in
('asset_id', 'asset_sha256', 'runtime_sha256', 'canonical_transform')}))
def evidence(self):
return dict(schema='rhg.layout_surface_feedback.v1',
shared_attribute_records_modified=False, records={'rug-record': dict(
identity=copy.deepcopy(self.table['selected_support_surface']['parent_source_identity']),
triangles_xyz_m=[[[-1, -1, .02], [1, -1, .02], [1, 1, .02]],
[[-1, -1, .02], [1, 1, .02], [-1, 1, .02]]],
upward_planes=[dict(height_m=.02, area_m2=4,
bbox_m=[[-1, -1, .02], [1, 1, .02]])],
source_entity_id='surface_rug-record', source_scene_id='fresh_source_preparation',
source_scale=1., method='actual_imported_upward_triangles_at_identity_pose',
pose_modified=False)})
def write_evidence(self, value=None):
self.evidence_path.write_text(json.dumps(self.evidence() if value is None else value))
sha = hashlib.sha256(self.evidence_path.read_bytes()).hexdigest()
self.table['selected_support_surface'].update(
evidence_path=str(self.evidence_path), evidence_sha256=sha)
return sha
def diagnostics(self, before, after, pins, *, allow_derived_support=True):
from engine.run_chain import output_diagnostics
request = dict(mode='locked', furniture=[dict(
entity_id=e['entity_id'], asset_id=e['asset_id']) for e in before['entities']])
return output_diagnostics(before, after, request, inputs=pins,
allow_derived_support=allow_derived_support)
def manifest(self):
return dict(floor_plan={}, entities=copy.deepcopy([self.table, self.rug]))
def parent(self, row):
from engine.support_contract import authored_support_parent
return authored_support_parent(row, {'table': row, 'rug': self.rug})
def test_measured_derived_rug_contact_retains_authored_floor(self):
self.assertIsNone(self.parent(self.table))
self.assertEqual(self.table['support_entity_id'], 'rug')
def test_unmeasured_or_rebound_rug_contacts_cannot_rewrite_authored_parent(self):
for mutate in [lambda e:e.pop('selected_support_surface'),
lambda e:e.update(base_z_m=.3),
lambda e:e.update(supported_by_entity_id='other'),
lambda e:e['derived_support_relation'].update(original_task_relations_retained=False),
lambda e:e['selected_support_surface']['parent_source_identity'].update(asset_sha256='0'*64)]:
row = copy.deepcopy(self.table)
mutate(row)
with self.subTest(row=row), self.assertRaises(ValueError):
self.parent(row)
def test_actual_surface_evidence_is_hashed_and_bound_before_acceptance(self):
from engine.support_contract import authored_support_parent
sha = self.write_evidence()
pins = {}
self.assertIsNone(authored_support_parent(self.table,
{'rug': self.rug}, inputs=pins, verify_evidence=True))
self.assertEqual(pins[str(self.evidence_path.resolve())], sha)
self.evidence_path.write_text('changed')
with self.assertRaises(ValueError):
authored_support_parent(self.table, {'rug': self.rug},
inputs=pins, verify_evidence=True)
def test_evidence_requires_schema_and_current_parent_record(self):
from engine.support_contract import authored_support_parent
mutations = [lambda e: e.update(schema='unrelated.evidence.v1'),
lambda e: e.update(records={}),
lambda e: e.update(records=[]),
lambda e: e['records'].update({'other-record': e['records'].pop('rug-record')}),
lambda e: e['records'].update({'rug-record': None})]
for mutate in mutations:
evidence = self.evidence()
mutate(evidence)
self.write_evidence(evidence)
pins = {}
with self.subTest(evidence=evidence), self.assertRaises(ValueError):
authored_support_parent(self.table, {'rug': self.rug},
inputs=pins, verify_evidence=True)
self.assertEqual(pins, {})
def test_current_evidence_record_must_match_full_parent_identity(self):
from engine.support_contract import authored_support_parent
for key, value in (('asset_id', 'other-rug'), ('asset_sha256', '0'*64),
('runtime_sha256', '0'*64), ('canonical_transform', [0]*16)):
evidence = self.evidence()
evidence['records']['rug-record']['identity'][key] = value
self.write_evidence(evidence)
pins = {}
with self.subTest(key=key), self.assertRaises(ValueError):
authored_support_parent(self.table, {'rug': self.rug},
inputs=pins, verify_evidence=True)
self.assertEqual(pins, {})
def test_evidence_cannot_rebind_an_existing_pin(self):
from engine.support_contract import authored_support_parent
self.write_evidence()
pins = {str(self.evidence_path.resolve()): '0'*64}
before = dict(pins)
with self.assertRaises(ValueError):
authored_support_parent(self.table, {'rug': self.rug},
inputs=pins, verify_evidence=True)
self.assertEqual(pins, before)
def test_existing_derived_relation_is_verified_and_pinned(self):
sha = self.write_evidence()
before = self.manifest()
pins = {}
self.assertEqual(self.diagnostics(before, copy.deepcopy(before), pins), [])
self.assertEqual(pins, {str(self.evidence_path.resolve()): sha})
def test_existing_derived_relation_rejects_changed_evidence(self):
self.write_evidence()
before = self.manifest()
self.evidence_path.write_text('changed')
pins = {}
self.assertTrue(self.diagnostics(before, copy.deepcopy(before), pins))
self.assertEqual(pins, {})
def test_existing_derived_relation_rejects_missing_evidence(self):
self.write_evidence()
before = self.manifest()
self.evidence_path.unlink()
self.assertTrue(self.diagnostics(before, copy.deepcopy(before), {}))
def test_existing_relation_cannot_replace_its_evidence_record(self):
self.write_evidence()
before = self.manifest()
evidence = self.evidence()
evidence['records']['other-record'] = evidence['records'].pop('rug-record')
self.write_evidence(evidence)
pins = {}
self.assertTrue(self.diagnostics(before, self.manifest(), pins))
self.assertEqual(pins, {})
def test_existing_malformed_derivation_cannot_skip_validation(self):
self.write_evidence()
before = self.manifest()
for relation in ({}, False, 'measured', dict(self.table['derived_support_relation'],
original_task_relations_retained=False)):
after = copy.deepcopy(before)
after['entities'][0]['derived_support_relation'] = relation
with self.subTest(relation=relation):
self.assertTrue(self.diagnostics(before, after, {}))
def test_existing_rug_parent_cannot_drop_its_derivation_metadata(self):
self.write_evidence()
before = self.manifest()
for remove in (False, True):
after = copy.deepcopy(before)
if remove:
after['entities'][0].pop('derived_support_relation')
else:
after['entities'][0]['derived_support_relation'] = None
with self.subTest(remove=remove):
self.assertTrue(self.diagnostics(before, after, {}))
def test_valid_added_support_requires_geometry_gate(self):
sha = self.write_evidence()
after = self.manifest()
before = copy.deepcopy(after)
for key in ('support_entity_id', 'derived_support_relation', 'selected_support_surface'):
before['entities'][0].pop(key)
for enabled in (False, True):
pins = {}
with self.subTest(enabled=enabled):
errors = self.diagnostics(before, after, pins, allow_derived_support=enabled)
self.assertEqual(bool(errors), not enabled)
self.assertEqual(pins, {str(self.evidence_path.resolve()): sha})
def test_derived_support_cannot_replace_an_authored_parent(self):
self.write_evidence()
after = self.manifest()
parent = dict(self.rug, entity_id='authored-parent', role='side_table')
after['entities'].append(parent)
for key in ('supported_by_entity_id', 'support_entity_id', 'parent_entity_id'):
before = copy.deepcopy(after)
table = before['entities'][0]
for field in ('support_entity_id', 'derived_support_relation', 'selected_support_surface'):
table.pop(field)
table[key] = parent['entity_id']
with self.subTest(key=key):
self.assertTrue(any(e['code'] == 'entity_contract_changed'
for e in self.diagnostics(before, after, {})))
def test_different_parent_transform_cannot_rebind_surface(self):
self.rug['canonical_transform'] = list(range(16))
self.table['selected_support_surface']['parent_source_identity']['canonical_transform'] = [0]*16
with self.assertRaises(ValueError):
self.parent(self.table)
if __name__ == '__main__':
unittest.main()
|