173 lines
8.8 KiB
Python
173 lines
8.8 KiB
Python
import json
|
|
import pytest
|
|
from pathlib import Path
|
|
|
|
from dx_productivity.ui.app import create_main_window
|
|
|
|
|
|
def setup_window(qtbot, fixture_path, tmp_path, monkeypatch):
|
|
monkeypatch.setenv('LOCALAPPDATA', str(tmp_path / 'local'))
|
|
data = json.loads((fixture_path / 'request-basic.json').read_text(encoding='utf-8'))
|
|
data.update(active_block_name='ROOT', current_sheet_number=1, current_sheet_name='1')
|
|
for component in data['components']:
|
|
component['attributes'] = [{'name': 'Value', 'value': component['value'], 'visible': True}]
|
|
request = tmp_path / 'request.json'
|
|
request.write_text(json.dumps(data), encoding='utf-8')
|
|
window = create_main_window(request)
|
|
qtbot.addWidget(window)
|
|
return window, data
|
|
|
|
|
|
def publish(tmp_path, data, keys, *, snapshot_id=None, design_path=None, sheet='1', current_keys=None):
|
|
payload = {'state': 'ready', 'run_id': data['run_id'], 'snapshot_id': snapshot_id or data['snapshot_id'],
|
|
'design_path': design_path or data['design_path'], 'active_block_name': 'ROOT',
|
|
'current_sheet_number': int(sheet), 'current_sheet_name': sheet,
|
|
'selected_keys': keys, 'current_sheet_keys': current_keys or ['uid:uid-r-a', 'uid:uid-c-a'], 'selected_nets': []}
|
|
(tmp_path / 'selection.json').write_text(json.dumps(payload), encoding='utf-8')
|
|
|
|
|
|
def test_selection_updates_all_feature_scopes_and_reports_clicked_refdes(qtbot, fixture_path, tmp_path, monkeypatch):
|
|
window, data = setup_window(qtbot, fixture_path, tmp_path, monkeypatch)
|
|
publish(tmp_path, data, ['uid:uid-c-a'])
|
|
window.selection_watcher._check()
|
|
for controller in (window.refdes_controller, window.property_change_controller,
|
|
window.property_visibility_controller, window.bom_controller, window.pdf_export_controller):
|
|
assert [c.refdes for c in controller.snapshot.components if c.selected] == ['C1']
|
|
assert controller.snapshot.snapshot_id == data['snapshot_id']
|
|
assert 'C1' in window.selection_status.text()
|
|
assert window.property_change_controller.page.model.item(0, 0).text() == 'C1'
|
|
assert 'R?' not in window.selection_status.text()
|
|
publish(tmp_path, data, [])
|
|
window.selection_watcher._check()
|
|
assert not any(c.selected for c in window.refdes_controller.snapshot.components)
|
|
assert '0' in window.selection_status.text()
|
|
|
|
|
|
def test_selection_preserves_retained_property_edits_and_pending_pdf_form(qtbot, fixture_path, tmp_path, monkeypatch):
|
|
window, data = setup_window(qtbot, fixture_path, tmp_path, monkeypatch)
|
|
page = window.property_change_controller.page
|
|
page.model.item(0, 2).setText('user pending value')
|
|
pdf = window.pdf_export_controller.page
|
|
pdf.output_input.setText('user pending PDF folder')
|
|
publish(tmp_path, data, ['uid:uid-r-a', 'uid:uid-c-a'])
|
|
window.selection_watcher._check()
|
|
assert page.model.item(0, 2).text() == 'user pending value'
|
|
assert page.model.item(1, 2).text() == '100nF'
|
|
assert pdf.output_input.text() == 'user pending PDF folder'
|
|
|
|
|
|
def test_selection_rejects_old_snapshot_and_other_project(qtbot, fixture_path, tmp_path, monkeypatch):
|
|
window, data = setup_window(qtbot, fixture_path, tmp_path, monkeypatch)
|
|
original = window.refdes_controller.snapshot
|
|
for kwargs in ({'snapshot_id': 'stale'}, {'design_path': 'C:/other.prj'}):
|
|
publish(tmp_path, data, ['uid:uid-c-a'], **kwargs)
|
|
window.selection_watcher._check()
|
|
assert window.refdes_controller.snapshot is original
|
|
|
|
|
|
def test_selection_is_deferred_during_apply_then_consumed(qtbot, fixture_path, tmp_path, monkeypatch):
|
|
window, data = setup_window(qtbot, fixture_path, tmp_path, monkeypatch)
|
|
original = window.refdes_controller.snapshot
|
|
window.refdes_controller._apply_in_flight = True
|
|
publish(tmp_path, data, ['uid:uid-c-a'])
|
|
window.selection_watcher._check()
|
|
assert window.refdes_controller.snapshot is original
|
|
window.refdes_controller._apply_in_flight = False
|
|
window.selection_watcher._check()
|
|
assert [c.refdes for c in window.refdes_controller.snapshot.components if c.selected] == ['C1']
|
|
|
|
|
|
def test_selection_switches_current_sheet_without_overwriting_form(qtbot, fixture_path, tmp_path, monkeypatch):
|
|
window, data = setup_window(qtbot, fixture_path, tmp_path, monkeypatch)
|
|
publish(tmp_path, data, [], sheet='2', current_keys=[])
|
|
# An empty current page must clear the old page's component scope.
|
|
payload = json.loads((tmp_path / 'selection.json').read_text(encoding='utf-8'))
|
|
payload['current_sheet_keys'] = []
|
|
(tmp_path / 'selection.json').write_text(json.dumps(payload), encoding='utf-8')
|
|
window.selection_watcher._check()
|
|
assert window.pdf_export_controller.snapshot.current_sheet_name == '2'
|
|
assert not any(c.current_sheet for c in window.bom_controller.snapshot.components)
|
|
|
|
|
|
def test_hidden_toolbar_selection_disconnect_recovery_and_project_switch(qtbot, fixture_path, tmp_path, monkeypatch):
|
|
import os
|
|
import time
|
|
window, data = setup_window(qtbot, fixture_path, tmp_path, monkeypatch)
|
|
window.show_toolbar()
|
|
publish(tmp_path, data, ['uid:uid-c-a'])
|
|
window.selection_watcher._check()
|
|
assert 'C1' in window.toolbar.status_label.text()
|
|
assert 'ROOT / 1' in window.toolbar.status_label.toolTip()
|
|
width = window.toolbar.width()
|
|
publish(tmp_path, data, ['uid:uid-r-a', 'uid:uid-c-a'])
|
|
window.selection_watcher._check()
|
|
assert '2 个' in window.toolbar.status_label.text()
|
|
assert 'C1' in window.toolbar.status_label.toolTip()
|
|
assert window.toolbar.width() == width
|
|
publish(tmp_path, data, [])
|
|
window.selection_watcher._check()
|
|
assert '0' in window.toolbar.status_label.text()
|
|
heartbeat = tmp_path / 'apply-progress.json'
|
|
heartbeat.write_text('{}', encoding='utf-8')
|
|
os.utime(heartbeat, (time.time() - 10, time.time() - 10))
|
|
window.selection_watcher._check()
|
|
assert '暂停' in window.toolbar.status_label.text()
|
|
assert 'C1' not in window.toolbar.status_label.text()
|
|
assert window.bridge_status.text() == '桥接已断开'
|
|
heartbeat.unlink()
|
|
publish(tmp_path, data, ['uid:uid-c-a'])
|
|
window.selection_watcher._check()
|
|
assert 'C1' in window.toolbar.status_label.text()
|
|
publish(tmp_path, data, ['uid:uid-c-a'], design_path='C:/other.prj')
|
|
window.selection_watcher._check()
|
|
assert '切换' in window.toolbar.status_label.text()
|
|
assert 'C1' not in window.toolbar.status_label.text()
|
|
assert not window.isVisible()
|
|
|
|
|
|
def test_repeated_panel_toggle_keeps_single_watcher_and_page_instances(qtbot, fixture_path, tmp_path, monkeypatch):
|
|
from PySide6.QtCore import QTimer
|
|
from dx_productivity.ui.selection_watcher import SelectionWatcher
|
|
window, data = setup_window(qtbot, fixture_path, tmp_path, monkeypatch)
|
|
pages = tuple(window.pages.widget(i) for i in range(window.pages.count()))
|
|
timers = tuple(window.selection_watcher.findChildren(QTimer))
|
|
for index in range(window.pages.count()):
|
|
window.show_panel(index)
|
|
window.hide_panel()
|
|
publish(tmp_path, data, ['uid:uid-c-a'])
|
|
window.selection_watcher._check()
|
|
window.show_panel(1)
|
|
assert tuple(window.pages.widget(i) for i in range(window.pages.count())) == pages
|
|
assert tuple(window.selection_watcher.findChildren(QTimer)) == timers
|
|
assert len(window.findChildren(SelectionWatcher)) == 1
|
|
assert window.property_change_controller.page.model.item(0, 0).text() == 'C1'
|
|
|
|
|
|
@pytest.mark.parametrize('content', [None, '{malformed', '[]', '{"state":"ready"}'])
|
|
def test_initial_unavailable_selection_waits_then_pauses_and_recovers(qtbot, fixture_path, tmp_path, monkeypatch, content):
|
|
from dx_productivity.ui import selection_watcher as watcher_module
|
|
now = [100.0]
|
|
monkeypatch.setattr(watcher_module.time, 'monotonic', lambda: now[0])
|
|
if content is not None:
|
|
(tmp_path / 'selection.json').write_text(content, encoding='utf-8')
|
|
window, data = setup_window(qtbot, fixture_path, tmp_path, monkeypatch)
|
|
window.show_toolbar()
|
|
original = window.refdes_controller.snapshot
|
|
assert '等待' in window.selection_status.text()
|
|
assert window.bridge_status.text() != '桥接已连接'
|
|
assert '已选' not in window.toolbar.status_label.text()
|
|
now[0] += 10
|
|
window.selection_watcher._check()
|
|
assert '暂停' in window.selection_status.text()
|
|
assert window.refdes_controller.snapshot is original
|
|
publish(tmp_path, data, ['uid:uid-c-a'])
|
|
window.selection_watcher._check()
|
|
assert window.bridge_status.text() == '桥接已连接'
|
|
assert 'C1' in window.toolbar.status_label.text()
|
|
payload = json.loads((tmp_path / 'selection.json').read_text(encoding='utf-8'))
|
|
payload['selected_keys'] = 12
|
|
(tmp_path / 'selection.json').write_text(json.dumps(payload), encoding='utf-8')
|
|
window.selection_watcher._check()
|
|
assert '暂停' in window.selection_status.text()
|
|
assert 'C1' not in window.toolbar.status_label.text()
|