Files
vibemake-pcb-studio/backend/tests/test_easyeda_dispatch.py
T
2026-11-10 19:04:27 +08:00

102 lines
5.0 KiB
Python

"""Offline regression checks. No real provider, drawing job or editor write."""
import json
import os
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), str(BACKEND / 'api')]
from app.core_runner import task_engine
from design_runtime.agent import DeepSeekAgent, instructions
from design_runtime.contracts import INTAKE
from design_runtime.state import State, write
from design_runtime.service import DesignService
from design_runtime.visual import board_visual
from tools.search_references import search
class EasyEDADispatchTests(unittest.TestCase):
def test_new_tasks_default_to_easyeda(self):
with patch.dict(os.environ, {}, clear=True):
self.assertEqual(task_engine('/unused', {'action': 'create'}), 'easyeda')
def test_frozen_task_keeps_its_engine_after_default_changes(self):
with tempfile.TemporaryDirectory() as tmp:
tid = 'pcb-0123456789abcdef1234'
path = Path(tmp) / 'tasks' / tid / 'task.json'
command = {'action': 'status', 'payload': {'task_id': tid}}
with patch.dict(os.environ, {'AI_PCB_EDA_BACKEND': 'easyeda'}):
write(path, {'eda_engine': 'kicad'})
self.assertEqual(task_engine(tmp, command), 'kicad')
write(path, {})
self.assertEqual(task_engine(tmp, command), 'easyeda')
write(path, {'eda_engine': 'unknown'})
with self.assertRaises(ValueError):
task_engine(tmp, command)
def test_invalid_identity_cannot_escape_task_directory(self):
with self.assertRaises(ValueError):
task_engine('/unused', {'action': 'status', 'payload': {'task_id': '../outside'}})
def test_direct_provider_receives_easyeda_rules_and_native_nested_schema(self):
schema = {'type': 'object', 'properties': {'purpose': {'type': 'string'}},
'required': ['purpose'], 'additionalProperties': False}
expected = {'request_json': {'purpose': 'sample board'}, 'questions': []}
with tempfile.TemporaryDirectory() as tmp, patch('design_runtime.agent.make_model') as model:
model.return_value.call.return_value = expected
agent = DeepSeekAgent()
result = agent.call('intake', {'instruction': 'Normalize request.', 'request_schema': schema},
INTAKE, Path(tmp) / 'project', Path(tmp) / 'call')
self.assertEqual(json.loads(result['request_json']), expected['request_json'])
self.assertEqual(model.return_value.call.call_args.args[2]['properties']['request_json'], schema)
self.assertIn('EasyEDA Pro', model.call_args.kwargs['guidance'])
self.assertTrue(agent.reconcile(Path(tmp) / 'call')['valid_response'])
def test_design_author_has_real_api_signatures(self):
prompt = instructions('design-author')
self.assertIn('function search(', prompt)
self.assertIn('function searchByProperties(', prompt)
self.assertIn('function create(', prompt)
self.assertIn('small local catalog is never an approval gate', prompt)
def test_status_does_not_touch_eda_or_model(self):
with tempfile.TemporaryDirectory() as tmp:
tid = 'pcb-0123456789abcdef1234'
state = State(tmp)
state.save({'task_id': tid, 'eda_engine': 'easyeda', 'state': 'failed', 'revision': 'RevA'})
state.close()
with patch('design_runtime.agent.make_model') as model:
service = DesignService(tmp)
try:
result = service.handle({'request_id': 'readonly', 'action': 'status', 'payload': {'task_id': tid}})
self.assertEqual(result['eda_engine'], 'easyeda')
self.assertEqual(result['state'], 'failed')
model.assert_not_called()
finally:
service.close()
def test_empty_reference_library_is_valid(self):
with tempfile.TemporaryDirectory() as tmp:
result = search(tmp, 'never-seen-component', 'component_selection')
self.assertTrue(result['catalog_complete'])
self.assertEqual(result['matches'], [])
def test_preview_comes_from_actual_copper_and_pin_nets(self):
part = {'designator': 'J1', 'bbox': {'minX': 0, 'minY': 0, 'maxX': 100, 'maxY': 100},
'pads': [{'n': '1', 'net': 'VCC', 'x': 10, 'y': 20},
{'n': '2', 'net': 'GND', 'x': 30, 'y': 40}]}
copper = {'tracks': [{'net': 'VCC', 'x1': 10, 'y1': 20, 'x2': 90, 'y2': 20}]}
visual = board_visual([part], copper)
self.assertAlmostEqual(visual['pads'][0]['x'], .254)
self.assertAlmostEqual(visual['pads'][0]['y'], -.508)
self.assertFalse(visual['paths'][0]['logical'])
self.assertEqual(visual['nets'], ['GND', 'VCC'])
self.assertEqual(visual['parts'][0]['ref'], 'J1')
if __name__ == '__main__':
unittest.main()