fix: acknowledge each legacy tip once per cleanup intent

This commit is contained in:
阿白叔 committed 2026-11-17 11:33:49 +08:00
1 parent 701e5cd3b4
commit 8818fa9841
3 files changed
+48 -16

No files matched your search

+6 -3
View File
@@ -698,7 +698,7 @@ def _close_registered_startup(row, handle, proof, *, stop=None):
if not active_owner() or time.monotonic() >= deadline:
raise ValueError('STARTUP_ACTION_UNCONFIRMED')
_post_window_message({'desktop':job['desktop']}, hwnd, row['pid'], message)
state = {'clicked':set()}; closed = proof['closed_frame']
state = {'clicked':set()}; closed = proof['closed_frame']; dismissed_tips = set()
try:
while time.monotonic() < deadline:
if win32event.WaitForSingleObject(handle, 0) == win32con.WAIT_OBJECT_0:
@@ -712,8 +712,11 @@ def _close_registered_startup(row, handle, proof, *, stop=None):
if closed is not None:
tip_row['startup_frame'] = closed
has_tip = any(w['title'] == 'Tip of the Day' for w in snapshot.get(row['pid'],[]))
if has_tip and confirmed() and _dismiss_startup_tip(tip_row,handle,snapshot.get(row['pid'],[]),
incomplete=_window_error(snapshot,row['pid']),deadline=deadline,stop=stop) is not None:
tip = (_dismiss_startup_tip(tip_row,handle,snapshot.get(row['pid'],[]),
incomplete=_window_error(snapshot,row['pid']),dismissed=dismissed_tips,deadline=deadline,stop=stop)
if has_tip and confirmed() else None)
if tip is not None:
dismissed_tips.add(tip)
if closed is None:
continue # Tip may precede loading; the same intent still owns its one Close.
while time.monotonic() < deadline:
+23 -10
View File
@@ -218,34 +218,47 @@ def test_independent_backup_job_registration_retains_descendants_after_caller_ha
def test_common_admission_refusal_publishes_bound_receipt_and_ui_can_reopen(qtbot, tmp_path, monkeypatch):
import win32api
import subprocess,sys
from threading import Event, Thread
from test_native_backup import project_fixture
from test_ui_filters import load_window
from cbb_refresh_verify import native_backup as native, desktop_worker
from cbb_refresh_verify.application import main
request, _ = project_fixture(tmp_path)
monkeypatch.setattr(native, 'native_backup_executable', lambda *_: tmp_path/'native.exe')
monkeypatch.setattr(native.subprocess, 'Popen', lambda *a, **k:
SimpleNamespace(pid=os.getpid(), handle=win32api.GetCurrentProcess(), poll=lambda: None))
session = native.launch_native_backup(request)
# A cold observer reads kernel identity: poll=exited on pytest's live PID
# cannot represent a released worker. Use a real, harmless refused child.
invoke = tmp_path/'invoke-refusal.signal'
child = tmp_path/'refused_worker.py'
child.write_text('import sys,time\nfrom pathlib import Path\n'
f'gate=Path({str(invoke)!r})\ndeadline=time.monotonic()+10\n'
'while not gate.exists() and time.monotonic()<deadline:time.sleep(.01)\n'
'assert gate.exists()\n'
'from cbb_refresh_verify import native_backup\n'
'def forbidden(*args):raise AssertionError("refused action entered")\n'
'native_backup.native_backup_worker_main=forbidden\n'
'from cbb_refresh_verify.application import main\n'
'raise SystemExit(main(["--native-backup-worker",sys.argv[1]]))\n')
popen = subprocess.Popen
with monkeypatch.context() as launch:
launch.setattr(native.subprocess, 'Popen', lambda command, **kwargs:
popen([sys.executable,str(child),command[-1]], **kwargs))
session = native.launch_native_backup(request)
entered, release = Event(), Event()
monkeypatch.setattr(desktop_worker, '_automation_clients', [])
def held_writer():
with desktop_worker.worker_native_channel():
entered.set(); release.wait(5)
entered.set(); release.wait(10)
holder = Thread(target=held_writer); holder.start()
assert entered.wait(2)
monkeypatch.setattr(native, 'native_backup_worker_main', lambda *a: pytest.fail('refused action entered'))
try:
assert main(['--native-backup-worker', str(session.job)]) == 1
invoke.touch()
assert session.process.wait(timeout=8) == 1
finally:
release.set(); holder.join(3)
record = json.loads(session.job.with_name('result.json').read_text('utf-8'))
assert record['safe_to_continue'] is True and record['native_started'] is False
assert record['operation_id'] == json.loads(session.job.read_text('utf-8'))['operation_id']
assert record['worker_identity']['pid'] == os.getpid()
session.process.poll = lambda: 1
assert record['worker_identity']['pid'] == session.process.pid != os.getpid()
launched = []
window = load_window(qtbot, tmp_path, monkeypatch,
native_backup_launcher=lambda req: (launched.append(req) or session),
+19 -3
View File
@@ -263,7 +263,8 @@ def test_real_legacy_adapter_keeps_hidden_documents_before_any_native_action(tmp
@pytest.mark.parametrize('closed',[False,True])
def test_legacy_tip_continues_to_loaded_cleanup_only_without_prior_close(tmp_path,monkeypatch,closed):
@pytest.mark.parametrize('delayed_tip',[False,True])
def test_legacy_tip_continues_to_loaded_cleanup_only_without_prior_close(tmp_path,monkeypatch,closed,delayed_tip):
import win32con,win32gui
from copy import deepcopy
job,row,records,messages,state=registered_fixture(tmp_path,monkeypatch,closed=closed)
@@ -272,13 +273,28 @@ def test_legacy_tip_continues_to_loaded_cleanup_only_without_prior_close(tmp_pat
desktop=job['desktop'],children=[],documents=[]),
dict(hwnd=200,**{'class':'#32770'},title='Tip of the Day',enabled=True,
desktop=job['desktop'],documents=[],children=[dict(hwnd=201,**{'class':'Button'},id=1,text='OK',enabled=True)])]
monkeypatch.setattr(sessions,'_process_windows',lambda pids:{12:deepcopy(tip_windows) if state['tip'] else []})
def windows(pids):
state['ticks']+=.02
if delayed_tip and state.get('tip_posted') and state['ticks']>=.3:
state['tip']=False
if closed:records.clear()
return {12:deepcopy(tip_windows) if state['tip'] else []}
monkeypatch.setattr(sessions,'_process_windows',windows)
monkeypatch.setattr(win32gui,'GetWindow',lambda h,k:100 if h==200 and k==win32con.GW_OWNER else 0)
original=win32gui.PostMessage
def post(hwnd,message,*args):
original(hwnd,message,*args)
if closed and hwnd==201:records.clear()
if hwnd==201:
if delayed_tip:state.update(tip=True,tip_posted=True)
elif closed:records.clear()
monkeypatch.setattr(win32gui,'PostMessage',post)
if delayed_tip and closed:
original_wait=__import__('win32event').WaitForSingleObject
def wait(handle,ms):
value=original_wait(handle,ms)
if state.get('tip_posted') and state['ticks']>=.3:records.clear()
return value
monkeypatch.setattr(__import__('win32event'),'WaitForSingleObject',wait)
assert sessions._request_close(row)['release_status']=='RELEASED'
assert messages==([(201,245)] if closed else [(201,245),(31,245),(100,16)])
assert not records and state['ticks']<30