110 lines
5.6 KiB
Python
110 lines
5.6 KiB
Python
"""Regression checks for repeatable proposal review and null mounting geometry."""
|
||
from copy import deepcopy
|
||
import json
|
||
from pathlib import Path
|
||
import sys
|
||
import tempfile
|
||
import unittest
|
||
from unittest.mock import patch
|
||
|
||
BACKEND = Path(__file__).resolve().parents[1]
|
||
sys.path[:0] = [str(BACKEND / 'easyeda_backend'), str(BACKEND)]
|
||
|
||
from design_runtime.layout import place
|
||
from design_runtime.mounting import confirmed_holes, plan_holes
|
||
from design_runtime.service import DesignService
|
||
from design_runtime.state import DesignError
|
||
|
||
|
||
def request_with_holes():
|
||
return {
|
||
'board': {'width_mm': 45, 'height_mm': 75, 'shape': 'rectangle'},
|
||
'power_inputs': [{'id': 'VIN'}],
|
||
'mounting': {
|
||
'requirement': 'required', 'description': '四个M2定位孔',
|
||
'hole_count': 4, 'asset_ids': [], 'spacing_constraints': [],
|
||
'holes': [{'id': f'MH{i}', 'diameter_mm': None, 'x_mm': None,
|
||
'y_mm': None, 'plating': 'unknown'} for i in range(1, 5)],
|
||
},
|
||
}
|
||
|
||
|
||
class MountingTests(unittest.TestCase):
|
||
def test_delegated_four_hole_pattern_reaches_layout(self):
|
||
request = request_with_holes()
|
||
original = deepcopy(request)
|
||
holes = plan_holes(request)
|
||
self.assertEqual(request, original)
|
||
self.assertEqual([(h['x_mm'], h['y_mm']) for h in holes],
|
||
[(4, 4), (41, 4), (41, 71), (4, 71)])
|
||
self.assertTrue(all(h['diameter_mm'] == 2.2 and h['plating'] == 'npth' for h in holes))
|
||
with self.assertRaisesRegex(DesignError, '尚未确认'):
|
||
confirmed_holes(request)
|
||
request['mounting']['holes'] = holes
|
||
self.assertTrue(place([], request['board'], confirmed_holes(request))['passed'])
|
||
|
||
def test_mechanical_constraints_block_guessed_positions(self):
|
||
request = request_with_holes()
|
||
request['mounting']['asset_ids'] = ['asset-drawing']
|
||
with self.assertRaisesRegex(DesignError, 'X、Y 坐标'):
|
||
plan_holes(request)
|
||
|
||
def test_legacy_null_layout_is_reported_as_incomplete_geometry(self):
|
||
request = request_with_holes()
|
||
with self.assertRaisesRegex(DesignError, 'MH1.*孔径和坐标'):
|
||
place([], request['board'], request['mounting']['holes'])
|
||
|
||
|
||
class ProposalReviewTests(unittest.TestCase):
|
||
def test_two_comment_rounds_and_confirmation_of_current_proposal_only(self):
|
||
events, contexts = [], []
|
||
|
||
def agent(task, role, payload, schema, name):
|
||
if role == 'intake':
|
||
return {'request_json': json.dumps(request_with_holes()), 'questions': []}
|
||
self.assertEqual(role, 'proposal')
|
||
contexts.append(payload)
|
||
return {'options': [{'id': 'A'}], 'recommended_option_id': 'A'}
|
||
|
||
with tempfile.TemporaryDirectory() as directory:
|
||
service = DesignService(directory, event_callback=events.append)
|
||
self.addCleanup(service.close)
|
||
with (patch.object(service, '_agent', side_effect=agent),
|
||
patch.object(service, '_design') as design,
|
||
patch('design_runtime.service.check', return_value={'design_allowed': True}),
|
||
patch('design_runtime.service.search', return_value={'catalog_complete': True, 'matches': []})):
|
||
task = service.handle({'request_id': 'create', 'action': 'create',
|
||
'payload': {'description': '45×75 mm,四个M2孔'}})
|
||
first_hash = task['proposal_hash']
|
||
tid = task['task_id']
|
||
for index, comment in enumerate(('把电源接口移到左边', '保留接口位置,补充 USB 保护'), 1):
|
||
previous = task
|
||
command = {'request_id': f'comment-{index}', 'action': 'message',
|
||
'payload': {'task_id': tid, 'proposal_hash': previous['proposal_hash'], 'text': comment}}
|
||
task = service.handle(command)
|
||
self.assertEqual(task['state'], 'awaiting_selection')
|
||
self.assertEqual(task['proposal_version'], index + 1)
|
||
self.assertNotEqual(task['proposal_hash'], previous['proposal_hash'])
|
||
self.assertEqual(task['proposal_feedback'], comment)
|
||
self.assertEqual(contexts[-1]['proposal_revision'],
|
||
{'previous_proposal': previous['proposal'], 'comment': comment})
|
||
self.assertEqual(len(task['mounting_holes']), 4)
|
||
self.assertEqual(service.handle(command), task)
|
||
invalid = service.handle({'request_id': 'old-confirmation', 'action': 'select',
|
||
'payload': {'task_id': tid, 'proposal_hash': first_hash, 'option_id': 'A'}})
|
||
self.assertIn('Stale proposal', invalid['message'])
|
||
design.assert_not_called()
|
||
current = service.handle({'request_id': 'confirm', 'action': 'select',
|
||
'payload': {'task_id': tid, 'proposal_hash': task['proposal_hash'], 'option_id': 'A'}})
|
||
self.assertEqual(current['state'], 'designing')
|
||
design.assert_called_once()
|
||
saved = service.state.get(tid)
|
||
self.assertEqual([c['kind'] for c in saved['conversation'][1:]], ['proposal_feedback'] * 2)
|
||
self.assertEqual(len(confirmed_holes(saved['contract']['request'])), 4)
|
||
self.assertEqual([e['snapshot']['state'] for e in events if e['kind'] == 'proposal.revision_requested'],
|
||
['assessing', 'assessing'])
|
||
|
||
|
||
if __name__ == '__main__':
|
||
unittest.main()
|