import unittest import io import json import tempfile from contextlib import redirect_stdout from pathlib import Path from unittest.mock import patch import screen_library as sl def plan_of(*boxes): return dict(rooms=[dict(room_id='r%d' % i, room_type='x', bounds_m=list(b)) for i, b in enumerate(boxes)]) class PinchedFloor(unittest.TestCase): def test_two_rooms_that_meet_at_a_corner_pinch_the_floor(self): self.assertTrue(sl.pinched_floor(plan_of((0, 0, 4, 4), (4, 4, 8, 8)))) self.assertTrue(sl.pinched_floor(plan_of((0, 4, 4, 8), (4, 0, 8, 4)))) def test_a_third_room_in_the_missing_quadrant_closes_it(self): self.assertFalse(sl.pinched_floor(plan_of((0, 0, 4, 4), (4, 4, 8, 8), (4, 0, 8, 4)))) def test_rooms_that_share_a_wall_and_an_l_shaped_outline_are_fine(self): self.assertFalse(sl.pinched_floor(plan_of((0, 0, 4, 4), (4, 0, 8, 4)))) self.assertFalse(sl.pinched_floor(plan_of((0, 0, 8, 4), (0, 4, 4, 8)))) # the reflex corner has three quadrants filled def test_a_void_in_the_middle_that_touches_nothing_else_is_not_a_pinch(self): ring = plan_of((0, 0, 9, 3), (0, 3, 3, 6), (6, 3, 9, 6), (0, 6, 9, 9)) self.assertFalse(sl.pinched_floor(ring)) def test_a_void_whose_outline_meets_the_outside_at_a_single_point_is_a_pinch(self): closed = plan_of((0, 0, 6, 3), (0, 3, 3, 6), (6, 3, 9, 6), (3, 6, 9, 9), (0, 6, 3, 9), (6, 0, 9, 3)) self.assertFalse(sl.pinched_floor(closed)) # a ring of six rooms around the 3 x 3 void in the middle open_corner = plan_of((0, 0, 6, 3), (0, 3, 3, 6), (6, 3, 9, 6), (0, 6, 3, 9), (3, 6, 6, 9)) self.assertTrue(sl.pinched_floor(open_corner)) # the room at 6,0-9,3 is gone: at (6,3) two rooms touch on a diagonal class MinimumSides(unittest.TestCase): LIMITS = {'living': 2.8, 'bedroom': 2.5, 'kitchen': 1.8, 'hallway': 1.0, 'default': 1.8} def test_a_room_uses_its_own_entry_then_its_group_then_the_default(self): self.assertEqual(sl.min_side_for(self.LIMITS, 'living'), 2.8) for bedroom in ('master', 'guest', 'bedroom_3', 'bedroom_4'): self.assertEqual(sl.min_side_for(self.LIMITS, bedroom), 2.5) self.assertEqual(sl.min_side_for(self.LIMITS, 'hallway_2'), 1.0) self.assertEqual(sl.min_side_for(self.LIMITS, 'bath'), 1.8) def test_a_wall_move_may_not_leave_a_room_narrower_than_its_limit(self): plan = plan_of((0, 0, 3, 4)) plan['rooms'][0]['room_id'] = 'master' self.assertTrue(sl.valid_bounds(plan, 'master', [0, 0, 2.6, 4], self.LIMITS)) self.assertFalse(sl.valid_bounds(plan, 'master', [0, 0, 2.4, 4], self.LIMITS)) class WallStub(unittest.TestCase): def test_wall_left_beside_an_ordinary_portal(self): self.assertAlmostEqual(sl.wall_stub({'overlap_interval_m': [0.0, 2.0], 'door_interval_m': [0.4, 1.4]}), 0.4) self.assertAlmostEqual(sl.wall_stub({'overlap_interval_m': [0.0, 1.0], 'open_interval_m': [0.0, 1.0]}), 0.0) self.assertAlmostEqual(sl.wall_stub({'overlap_interval_m': [1.0, 3.0], 'door_interval_m': [1.7, 2.7]}), 0.3) def test_adjacencies_without_a_portal_and_balcony_openings_have_no_stub(self): self.assertIsNone(sl.wall_stub({'overlap_interval_m': [0.0, 2.0]})) self.assertIsNone(sl.wall_stub({'overlap_interval_m': [0.0, 3.0], 'open_interval_m': [0.45, 2.55], 'balcony_side_clearance_m': 0.45})) class UsableWallScreening(unittest.TestCase): def setUp(self): self.plan = dict(plan_id='blocked', rooms=[dict(room_id='living', room_type='living_room', bounds_m=[0, 0, 4, 4])]) self.final = dict(self.plan, doors=[dict(room_ids=['living'], wall_orientation=o, wall_line_m=line, door_interval_m=[0, 4]) for o, line in [('H', 0), ('H', 4), ('V', 0), ('V', 4)]]) self.options = dict(fix=False, limits=sl.DEFAULT_LIMITS_M, allow_missing_windows=True, topology_gate=False, min_score=None) self.result = dict(reason=None, final=self.final, score=dict(score=8), connectivity={}) def test_reject_is_default_and_records_room_reason(self): with patch.object(sl, 'evaluate', return_value=self.result): self.assertEqual(sl.screen_one((self.plan, self.options)), ('blocked', None, 'no_usable_wall:living')) def test_flag_keeps_plan_with_wall_diagnostics_and_off_skips_check(self): with patch.object(sl, 'evaluate', return_value=self.result): for mode in ('flag', 'off'): _, plan, reason = sl.screen_one((self.plan, dict(self.options, usable_walls=mode))) self.assertIsNone(reason) check = plan['rplan_screening']['usable_wall_check'] if mode == 'flag': self.assertFalse(check['passed']) self.assertEqual(check['failures'], ['no_usable_wall:living']) else: self.assertIsNone(check) def test_check_uses_final_native_geometry_including_repair_and_entry_door(self): with patch.object(sl, 'evaluate', return_value=self.result): _, plan, _ = sl.screen_one((self.plan, dict(self.options, usable_walls='flag'))) self.assertEqual(plan['rplan_screening']['usable_wall_check']['rooms']['living']['max_usable_wall_m'], 0) def test_cli_default_reject_and_explicit_modes_are_written_to_report(self): with tempfile.TemporaryDirectory(prefix='rplan_wall_modes_') as tmp: source, dest = Path(tmp) / 'input.json', Path(tmp) / 'screened.json' source.write_text(json.dumps(dict(plans=[self.plan]))) for mode in (None, 'reject', 'flag', 'off'): args = ['--src', tmp, '--library', str(source), '--out', str(dest)] if mode: args += ['--usable-walls', mode] with patch.object(sl, 'init_worker'), patch.object(sl, 'evaluate', return_value=self.result), redirect_stdout(io.StringIO()): self.assertEqual(sl.main(args), 0) report = json.loads(dest.read_text()) self.assertEqual(report['screening_options']['usable_walls'], mode or 'reject') self.assertEqual(report['plan_count'], 1 if mode in ('flag', 'off') else 0) if mode in (None, 'reject'): self.assertEqual(report['rejected_by_reason'], {'no_usable_wall:living': 1}) if __name__ == '__main__': unittest.main()