fix: close batch execution and recovery lifecycle

This commit is contained in:
阿白叔 committed 2026-11-17 16:56:29 +08:00
1 parent b19afc6a53
commit d9cf6a07e9
91 files changed
+5779 -454

No files matched your search

@@ -1,18 +1,14 @@
import importlib.util
import inspect
from pathlib import Path
import subprocess
import tempfile
from types import SimpleNamespace
import unittest
from unittest.mock import call, patch
ROOT = Path(__file__).resolve().parents[2]
RUNTIME = ROOT / "integration" / "dxdesigner795"
SPEC = importlib.util.spec_from_file_location(
"native", ROOT / "src" / "dx_productivity" / "trial_task_center" / "native_helper.py"
)
native = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(native)
from dx_productivity.trial_task_center import native_helper as native
FAKES = r"""
Class TestPoint
@@ -107,7 +103,7 @@ End Sub
class NativeTests(unittest.TestCase):
def test_native_command_does_not_use_physical_mouse_fallback(self):
source = inspect.getsource(native.run_native)
source = inspect.getsource(inspect.unwrap(native.run_native))
self.assertIn("replacement_command_from_resources(executable)", source)
self.assertNotIn("win32api.SetCursorPos", source)
@@ -115,20 +111,24 @@ class NativeTests(unittest.TestCase):
self.assertNotIn("win32gui.SetCursorPos(screen_point)", source)
def test_snapshot_batch_is_reselected_before_the_strict_selection_gate(self):
app = object()
api = object()
metadata = ["M"]
first = type("Component", (), {"UID": "wanted"})()
other = type("Component", (), {"UID": "other"})()
first = SimpleNamespace(UID='wanted', Refdes='R1', Selected=False)
other = SimpleNamespace(UID='other', Refdes='R2', Selected=True)
first._oleobj_, other._oleobj_ = first, other
app = SimpleNamespace(ActiveView=SimpleNamespace(Query=lambda kind, scope:
[component for component in (first, other) if component.Selected]))
api = SimpleNamespace(IVdComp=lambda value:value)
records = [["C", "wanted"]]
with patch.object(native, "check_context", side_effect=([first, other], [first, other])) as check:
with patch.object(native, "selection") as select:
with patch.object(native, "check_context", return_value=[first, other]) as check:
with patch.object(native, "selection", side_effect=lambda component, selected:setattr(component,'Selected',selected)) as select, \
patch.object(native, "check_active_context"):
result = native.select_snapshot_batch(app, api, metadata, records)
self.assertEqual(result, [first, other])
self.assertEqual(check.call_args_list[0], call(app, api, metadata, records, require_selection=False))
self.assertEqual(check.call_args_list[1], call(app, api, metadata, records))
self.assertEqual(select.call_args_list, [call(first, True), call(other, False)])
self.assertTrue(first.Selected)
self.assertFalse(other.Selected)
def test_snapshot_escape_format(self):
self.assertEqual(native.decode("a%0009b%000Ac%0025"), "a\tb\nc%")
@@ -73,10 +73,11 @@ Class SessionApp
End Function
End Class
Class SessionDocuments
Public Owner, OpenCount, SheetCount
Public Owner, OpenCount, SheetCount, DocumentCount
Private Sub Class_Initialize()
OpenCount = 0
SheetCount = 1
DocumentCount = 1
End Sub
Public Function GetAvailableSchematics()
Dim Names
@@ -94,13 +95,14 @@ Class SessionDocuments
End Function
Public Function Open(ByVal Schematic, ByVal Sheet)
OpenCount = OpenCount + 1
DocumentCount = 1
Set Owner.ActiveView = Owner
Set Open = Me
End Function
Public Sub Activate()
End Sub
Public Property Get Count
Count = 1
Count = DocumentCount
End Property
Public Function Item(ByVal Index)
If Index <> 1 Then Err.Raise 51000,"Test","bad document index"
@@ -108,6 +110,7 @@ Class SessionDocuments
End Function
Public Sub Close(ByVal SaveChanges, ByVal FileName)
If SaveChanges Then Owner.SaveCount = Owner.SaveCount + 1
DocumentCount = DocumentCount - 1
Set Owner.ActiveView = Nothing
End Sub
End Class
@@ -141,6 +144,9 @@ def run_session(tmp_path, body):
target.touch()
setup = f'''
Set QP_Fso = CreateObject("Scripting.FileSystemObject")
Set QP_SessionItems=CreateObject("Scripting.Dictionary")
Set QP_SessionSheets=CreateObject("Scripting.Dictionary")
Set QP_SessionDocuments=CreateObject("Scripting.Dictionary")
QP_Root = "{tmp_path}"
Dim Target, Attached
Target = "{target}"
+21 -6
View File
@@ -90,6 +90,7 @@ Class ValueApp
End Function
Public Sub Close(ByVal SaveChanges,ByVal FileName)
CloseCount=CloseCount+1
DocumentCount=DocumentCount-1
If SaveChanges Then
SaveCalled=True
SaveCount=SaveCount+1
@@ -149,6 +150,9 @@ End Function
SETUP = r'''
Dim Comp, Pin, Attr, Ignored, Command, Snapshot, SnapshotPath, Failure, Saved
Set QP_Fso=CreateObject("Scripting.FileSystemObject")
Set QP_SessionItems=CreateObject("Scripting.Dictionary")
Set QP_SessionSheets=CreateObject("Scripting.Dictionary")
Set QP_SessionDocuments=CreateObject("Scripting.Dictionary")
QP_Root="__ROOT__"
QP_CurrentJobId="value-job"
Set QP_App=New ValueApp
@@ -220,7 +224,7 @@ Require QP_App.UndoCount=0,"recovery undid prior CAD work"
''')
@pytest.mark.parametrize('change', ['Comp.UID="imposter"', 'Comp.Refdes="R99"'])
@pytest.mark.parametrize('change', ['Comp.Refdes="R99"'])
def test_restore_rejects_changed_component_identity_before_any_write(tmp_path, change):
run_vbs(tmp_path, 'part_number', '''
On Error Resume Next
@@ -233,6 +237,16 @@ Require QP_App.TransactionCount=0,"identity failure must precede CAD writes"
''', after=change)
def test_restore_accepts_uid_churn_without_changing_reference_or_replaying_replace(tmp_path):
run_vbs(tmp_path, 'part_number', '''
QPRestoreAfterOperatorReplace Command,SnapshotPath
Require QP_App.TransactionCount=1,"restoration did not use exactly one transaction"
Require Comp.Refdes="R1","reference identity changed"
Require Comp.FindAttribute("Value").InstanceValue="NEW","Value policy was not verified"
Require QP_App.UndoCount=0,"UID churn replayed or undid the native replacement"
''', after='Comp.UID="changed-native-uid"')
def _batch_readback_fixture(routines, *, fault=''):
# Exercise real restore/finish/guards, replacing only the external open and
# command transport with an in-memory CAD-shaped application.
@@ -242,6 +256,7 @@ End Function''', routines)
routines = re.sub(r'(?ms)^Sub QPEnsureProject\([^\n]*\n.*?^End Sub', '''Sub QPEnsureProject(ByRef App, ByVal TargetPath)
App.ProjectPath=TargetPath
App.CloseCount=0
App.DocumentCount=1
QP_OwnedProject=True
QP_OwnedProjectPath=TargetPath
''' + ('Set Attr=Other.FindAttribute("Value"): Attr.Value="DRIFT"' if fault == 'after_reopen' else '') + '''
@@ -258,7 +273,7 @@ def test_save_then_project_close_refusal_retains_save_boundary(tmp_path):
Dim FullChecks
FullChecks=0
Command.Add "project_path",QPProjectPath()
QP_BatchMode=True: QP_SessionMode=True
QP_BatchMode=True: QP_SessionMode=False
QP_SessionToken=String(32,"b")
QP_OwnedProject=True: QP_OwnedProjectPath=QPProjectPath()
QPRestoreAfterOperatorReplace Command,SnapshotPath
@@ -274,7 +289,7 @@ QP_App.RefuseClose=False
QPDiscardFailedBatchProject CloseError
Require Len(CloseError)>0,"uncertain save must not discard the open project"
Require Len(QPProjectPath())>0,"failed saved project was silently discarded"
Require Not QP_Fso.FileExists(QP_Fso.BuildPath(QP_Root,"project-session-result.tsv")),"failure published success"
Require Not QP_Fso.FileExists(QP_Fso.BuildPath(QP_Root,"status.tsv")),"failure published success"
''', routines_transform=_batch_readback_fixture)
@@ -284,7 +299,7 @@ def test_owned_batch_checks_full_state_at_save_and_disk_boundaries(tmp_path, fau
Dim FullChecks
FullChecks=0
Command.Add "project_path",QPProjectPath()
QP_BatchMode=True: QP_SessionMode=True
QP_BatchMode=True: QP_SessionMode=False
QP_SessionToken=String(32,"b")
QP_OwnedProject=True: QP_OwnedProjectPath=QPProjectPath()
QPRestoreAfterOperatorReplace Command,SnapshotPath
@@ -298,10 +313,10 @@ On Error GoTo 0
Require Failure=0,"valid batch failed"
Require FullChecks=2,"expected one full check before save and one after reopen"
Require QP_App.SaveCalled,"valid batch did not save"
Require InStr(QPReadUtf8(QP_Fso.BuildPath(QP_Root,"project-session-result.tsv")),"saved_ok")>0,"no verified result"
Require InStr(QPReadUtf8(QP_Fso.BuildPath(QP_Root,"status.tsv")),"saved_ok")>0,"no verified result"
''' if not fault else '''
Require Failure<>0,"unselected drift passed the commit guard"
Require Not QP_Fso.FileExists(QP_Fso.BuildPath(QP_Root,"project-session-result.tsv")),"failure published a completion"
Require Not QP_Fso.FileExists(QP_Fso.BuildPath(QP_Root,"status.tsv")),"failure published a completion"
Require QP_App.SaveCalled=''' + ('True' if fault == 'after_reopen' else 'False') + ''',"wrong save boundary"
Require QP_BatchSaveCommitted=''' + ('True' if fault == 'after_reopen' else 'False') + ''',"saved uncertainty marker is wrong"
'''), after='''