fix: wait for stable native replacement target before submission

This commit is contained in:
阿白叔 committed 2026-10-17 14:10:27 +08:00
1 parent ea024da325
commit e13b4e295f
2 files changed
+93 -9

No files matched your search

@@ -157,6 +157,27 @@ def target_symbol_matches(displayed, library_symbol, slot_count):
return suffix.isdigit() and 1 <= int(suffix) <= int(slot_count)
def wait_for_native_target(callback, target, library_symbol, slot_count, *, timeout=15,
stable_for=0.45, poll_interval=0.15):
"""Wait for the part, symbol/gate and enabled submit button as one state."""
deadline = time.monotonic() + timeout
previous = None
stable_since = None
while time.monotonic() < deadline:
state = callback()
now = time.monotonic()
part, symbol, enabled = state
if part != target or not target_symbol_matches(symbol, library_symbol, slot_count) or not enabled:
previous, stable_since = None, None
else:
if state != previous:
previous, stable_since = state, now
if now - stable_since >= stable_for:
return state
time.sleep(poll_interval)
raise TimeoutError('Native target part/symbol did not become stable and ready')
def is_replace_symbol_title(title):
normalized = str(title).replace("&", "").casefold()
return "\u66ff\u6362\u7b26\u53f7" in normalized or "replace symbol" in normalized
@@ -572,22 +593,26 @@ def run_native(snapshot, report):
position = (point[1] << 16) | (point[0] & 0xFFFF)
for code, key in [(win32con.WM_LBUTTONDOWN, 1), (win32con.WM_LBUTTONUP, 0), (win32con.WM_LBUTTONDBLCLK, 1), (win32con.WM_LBUTTONUP, 0)]:
win32gui.PostMessage(list_handle, code, key, position)
def target_ready():
def read_target():
control = dlg_item(dialog, 1703)
inner = dlg_item(control, 1001)
report["target_text"] = control_text(inner or control)
report["symbol_text"] = control_text(dlg_item(dialog, 1701))
return report["target_text"] == metadata[3]
wait_for(target_ready, "Native target part did not update")
symbol = control_text(dlg_item(dialog, 1701))
if not target_symbol_matches(symbol, columns[2], slot_count):
raise ValueError(f"Native symbol does not match library row: {symbol}")
report['target_submit_enabled'] = bool(win32gui.IsWindowEnabled(dlg_item(dialog, 1695)))
return report['target_text'], report['symbol_text'], report['target_submit_enabled']
ready = wait_for_native_target(read_target, metadata[3], columns[2], slot_count)
components = check_context(app, api, metadata, records, require_selection=False)
for component in components:
selection(component, str(component.UID) in wanted)
for identifier in (1704, 1105, 1697, 1700):
check_radio(dialog, identifier)
check_context(app, api, metadata, records)
if read_target() != ready:
ready = wait_for_native_target(read_target, metadata[3], columns[2], slot_count)
check_context(app, api, metadata, records)
if read_target() != ready:
raise ValueError('Native target changed during final context verification')
symbol = ready[1]
report["transaction"] = transaction_binding(snapshot, metadata, records, columns, symbol, slot_count)
report["stage"] = "native_replace_started"
save_report(snapshot, report)
@@ -609,9 +634,29 @@ def run_native(snapshot, report):
)
return None
report["replaced_uids"] = wait_for(
selected_replaced, "Native replacement did not complete", timeout=30
)
try:
report["replaced_uids"] = wait_for(
selected_replaced, "Native replacement did not complete", timeout=30
)
except TimeoutError:
# Preserve the last observed identities without retrying a mutation.
report['replacement_timeout'] = {'polls': replacement_poll_count}
try:
current = components_on_sheet(app, api)
refs = {record[2] for record in records}
report['replacement_timeout'].update(
component_count=len(current),
components=[dict(uid=str(c.UID), refdes=str(c.Refdes),
attributes={name: attribute_value(c, name)
for name in ('Part Number', 'Part Name', 'Part Label', 'Value')})
for c in current if str(c.UID) in wanted or str(c.Refdes) in refs])
except Exception as error:
report['replacement_timeout']['component_readback_error'] = str(error)
try:
report['replacement_timeout']['target_control'] = read_target()
except Exception as error:
report['replacement_timeout']['target_control_error'] = str(error)
raise
current = components_on_sheet(app, api)
if len(metadata) == 10:
verify_unselected_snapshot(snapshot, current, wanted)
@@ -0,0 +1,39 @@
from types import SimpleNamespace
import pytest
from dx_productivity.trial_task_center import native_helper as native
def clock(monkeypatch):
now = [0.0]
monkeypatch.setattr(native, 'time', SimpleNamespace(
monotonic=lambda: now[0], sleep=lambda seconds: now.__setitem__(0, now[0] + seconds)))
return now
def test_same_part_text_cannot_accept_stale_symbol_disabled_button_or_changing_gate(monkeypatch):
now = clock(monkeypatch)
states = iter([
('NEW', 'lib:OTHER.1', True),
('NEW', 'lib:RES.1', False),
('NEW', 'lib:RES.1', True),
('NEW', 'lib:RES', True),
('NEW', 'lib:RES.1', True),
])
read = lambda: next(states, ('NEW', 'lib:RES.1', True))
state = native.wait_for_native_target(read, 'NEW', 'lib:RES', 1,
timeout=2, stable_for=.3, poll_interval=.1)
assert state == ('NEW', 'lib:RES.1', True)
assert now[0] >= .7
@pytest.mark.parametrize('state', [
('OLD', 'lib:RES.1', True), ('NEW', 'lib:OTHER.1', True),
('NEW', 'lib:RES.2', True), ('NEW', 'lib:RES.1', False),
])
def test_unready_target_times_out_without_becoming_a_valid_selection(monkeypatch, state):
clock(monkeypatch)
with pytest.raises(TimeoutError, match='Native target'):
native.wait_for_native_target(lambda: state, 'NEW', 'lib:RES', 1,
timeout=.5, stable_for=.2, poll_interval=.1)