File size: 6,157 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
"""Real saved Blender fixtures through the bounded generation controller."""
import copy
import json
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest

from engine.repair_execution import execute_repairs

BASE = Path(__file__).resolve().parents[1]
BLENDER = Path('/Applications/Blender.app/Contents/MacOS/Blender')


@unittest.skipUnless(BLENDER.is_file(), 'Local saved-scene regression needs Blender')
class SavedGeometryChainTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.temp = tempfile.TemporaryDirectory()
        cls.root = Path(cls.temp.name)
        cls.initial = cls.root / 'initial'
        command = [str(BLENDER), '--background', '--python-exit-code', '1', '--python',
                   str(BASE / 'real_test/generate_geometry_fixture.py'), '--',
                   '--output', str(cls.initial), '--initial']
        result = subprocess.run(command, capture_output=True, text=True, timeout=60)
        if result.returncode:
            raise AssertionError(result.stdout + result.stderr)
        cls.manifest = json.loads((cls.initial / 'generated.json').read_text())
        cls.request = dict(schema='rhg.scene_request.v1', mode='locked',
            task_kind='scene_generation', task_text='Procedural saved-geometry fixture only',
            entities=copy.deepcopy(cls.manifest['entities']),
            furniture=[dict(entity_id=e['entity_id'], asset_id=e['asset_id'])
                       for e in cls.manifest['entities']])
        cls.request_path = cls.root / 'request.json'
        cls.request_path.write_text(json.dumps(cls.request))

    @classmethod
    def tearDownClass(cls):
        cls.temp.cleanup()

    def run_case(self, *, always_collide=False):
        name = 'exhausted' if always_collide else 'repaired'
        command = [str(BLENDER), '--background', '--python-exit-code', '1', '--python',
            str(BASE / 'real_test/generate_geometry_fixture.py'), '--',
            '--manifest', '{manifest}', '--request', '{request}', '--output', '{attempt_dir}']
        if always_collide:
            command.append('--always-collide')
        config = dict(schema='rhg.frontier-chain-config.v1',
            manifest=str(self.initial / 'generated.json'), request=str(self.request_path),
            repair_execution=dict(enabled=True, max_attempts=2, total_timeout_s=90),
            geometry_validation=dict(enabled=True, blender=str(BLENDER), timeout_s=30),
            generation=dict(command=command, manifest_output='generated.json', timeout_s=30,
                            native_search_required=False))
        config_path = self.root / (name + '.config.json')
        config_path.write_text(json.dumps(config))
        output = self.root / name
        return execute_repairs(config_path, output, execute=True), output

    def test_rotation_and_fresh_contact_close_the_saved_scene_repair_loop(self):
        result, output = self.run_case()
        self.assertEqual(result['count'], 2, result)
        self.assertTrue(result['geometry_current'], result)
        self.assertTrue(result['geometry_passed'], result)
        self.assertTrue(result['preservation_verified'], result)
        first = json.loads((output/'round_01/attempt_01/geometry/result.json').read_text())
        second = json.loads((output/'round_02/attempt_01/geometry/result.json').read_text())
        self.assertFalse(first['geometry_passed'])
        self.assertTrue(second['geometry_passed'], second)
        self.assertNotEqual(first['scene_sha256'], second['scene_sha256'])
        self.assertFalse(result['physical_qualified'])
        a = json.loads((output/'round_01/attempt_01/generated.json').read_text())
        b = json.loads((output/'round_02/attempt_01/generated.json').read_text())
        self.assertEqual(a['entities'][2], b['entities'][2])
        self.assertNotEqual(a['entities'][0]['yaw_deg'], b['entities'][0]['yaw_deg'])

    def test_invalid_geometry_exhausts_budget_without_acceptance(self):
        result, _ = self.run_case(always_collide=True)
        self.assertEqual(result['count'], 2, result)
        self.assertEqual(result['status'], 'budget_exhausted', result)
        self.assertFalse(result['geometry_passed'], result)
        self.assertFalse(result['whole_task_acceptance'])

    def test_generation_entry_enables_geometry_without_mutating_original_config(self):
        config = dict(schema='rhg.frontier-chain-config.v1',
            manifest=str(self.initial/'generated.json'), request=str(self.request_path))
        path = self.root/'entry.config.json'
        original = json.dumps(config).encode()
        path.write_bytes(original)
        output = self.root/'entry'
        result = subprocess.run([sys.executable, '-B', str(BASE/'real_test/run_geometry_chain.py'),
            '--config', str(path), '--output', str(output)], capture_output=True,
            text=True, timeout=15)
        self.assertEqual(result.returncode, 0, result.stdout+result.stderr)
        self.assertEqual(path.read_bytes(), original)
        effective = json.loads((output/'effective_config.json').read_text())
        self.assertIs(effective['geometry_validation']['enabled'], True)

    def test_missing_saved_scene_cannot_pass_the_geometry_gate(self):
        from engine.run_chain import run_chain
        path = self.root/'missing-scene.config.json'
        command = [sys.executable, '-c',
            'import shutil,sys; from pathlib import Path; shutil.copyfile(sys.argv[1],Path(sys.argv[2])/"generated.json")',
            '{manifest}', '{attempt_dir}']
        config = dict(schema='rhg.frontier-chain-config.v1',
            manifest=str(self.initial/'generated.json'), request=str(self.request_path),
            geometry_validation={}, generation=dict(command=command,
                manifest_output='generated.json', timeout_s=5))
        path.write_text(json.dumps(config))
        result = run_chain(path, self.root/'missing-scene', execute=True)
        self.assertFalse(result['output_contract_passed'], result)
        self.assertFalse(result['geometry_current'], result)


if __name__ == '__main__':
    unittest.main()