feat(native): add correlated read-only snapshot adapter

This commit is contained in:
阿白叔 committed 2026-10-19 10:54:25 +08:00
1 parent 5b625b076e
commit 2f6060dfe3
4 files changed
+440

No files matched your search

+166
View File
@@ -0,0 +1,166 @@
"""Correlated full-read frames and execution of the generated read-only script."""
from pathlib import Path
import re
import subprocess
import pytest
import test_accuracy_vbs_guards as guards
ROOT = Path(__file__).resolve().parents[2]
MAIN = ROOT / 'integration/dxdesigner795/QuickPartReplace.vbs'
ADAPTER = MAIN.with_name('NativeSnapshotReader.vbs')
PAIR = ('U\tu2\tR2\t\t\t\t\r\n'
'G\tu2\tC%0009u2%0009R2%00090%00090%00090%00091%0009NO_PINS%000D%000A\r\n')
FRAME = 'NR1\tnonce\tc:\\board.prj\tMAIN:1\r\n' + PAIR + 'END\tnonce\t2\r\n'
def quote(value):
return '"' + str(value).replace('"', '""') + '"'
def run(tmp_path, body, *, prelude=''):
assert ADAPTER.is_file(), 'native snapshot adapter is missing'
source = ADAPTER.read_text(encoding='ascii')
common = '\n'.join(guards.routine(n) for n in ('QPEncode', 'QPDecode', 'QPUntouchedIdentity', 'QPReadUtf8', 'QPWriteUtf8'))
script = tmp_path / 'native-reader-test.vbs'
script.write_text(prelude + '\n' + common + '\n' + source + '\n' + body, encoding='ascii')
result = subprocess.run(['cscript.exe', '//nologo', str(script)], capture_output=True, text=True, timeout=15)
assert result.returncode == 0, result.stdout + result.stderr
return result.stdout.strip()
@pytest.mark.parametrize('mutation', [
'', 'token', 'project', 'sheet', 'truncated', 'duplicate', 'wrong_g_uid',
'identity', 'count', 'selected', 'error', 'extra_after_end', 'no_header',
])
def test_native_snapshot_frame_requires_exact_complete_proof(tmp_path, mutation):
text = FRAME
if mutation == 'token':
text = text.replace('NR1\tnonce', 'NR1\told')
elif mutation == 'project':
text = text.replace('c:\\board.prj', 'c:\\other.prj')
elif mutation == 'sheet':
text = text.replace('MAIN:1', 'MAIN:2')
elif mutation == 'truncated':
text = text[:text.index('END\t')]
elif mutation == 'duplicate':
text = text.replace('END\t', PAIR + 'END\t')
elif mutation == 'wrong_g_uid':
text = text.replace('G\tu2\t', 'G\tother\t')
elif mutation == 'identity':
text = text.replace('U\tu2\tR2', 'U\tu2\tR99')
elif mutation == 'count':
text = text.replace('END\tnonce\t2', 'END\tnonce\t3')
elif mutation == 'selected':
text = text.replace('u2', 'u1')
elif mutation == 'error':
text = 'ERROR\tnonce\t1234\tFakeCOM\tread denied\r\n'
elif mutation == 'extra_after_end':
text += PAIR
elif mutation == 'no_header':
text = text[text.index('U\t'):]
path = tmp_path / 'frame.tsv'
path.write_bytes(text.encode('ascii'))
output = run(tmp_path, '''
Dim Selected, Result, Failure
Set Selected=CreateObject("Scripting.Dictionary"): Selected.Add "u1",True
On Error Resume Next
Set Result=QPParseNativeSnapshot(QPReadUtf8(''' + quote(path) + '''),"nonce","c:\\board.prj","MAIN:1",Selected)
Failure=Err.Number
Err.Clear
On Error GoTo 0
WScript.Echo CStr(Failure<>0)
If Failure=0 Then
WScript.Echo Result("total")
WScript.Echo Join(Result("rows").Keys,",")
ElseIf Failure=1234 Then
WScript.Echo "read error propagated"
End If
''')
expected = ['False', '2', 'u2'] if not mutation else ['True']
if mutation == 'error':
expected.append('read error propagated')
assert output.splitlines() == expected
NATIVE_FAKES = guards.FULL_FAKE[guards.FULL_FAKE.index('Class FakePoint'):guards.FULL_FAKE.index('Function QPProjectPath()')] + '''
Class OwnedNativeApp
Public ActiveView
Private Sub Class_Initialize()
Set ActiveView=New OwnedNativeView
End Sub
Function Guid()
Guid="OWNED"
End Function
Function GetProjectData()
Set GetProjectData=Me
End Function
Function GetProjectFilePath()
GetProjectFilePath="C:\\board.prj"
End Function
End Class
Class OwnedNativeView
Public Components, Pins
Private Sub Class_Initialize()
Set Components=CreateObject("Scripting.Dictionary")
Set Pins=CreateObject("Scripting.Dictionary")
End Sub
Function Query(ByVal Kind,ByVal Scope)
If Kind=128 Then Set Query=Components Else Set Query=Pins
End Function
Function GetName(ByVal Flags)
GetName="MAIN"
End Function
Public Property Get Block
Set Block=Me
End Property
Public Property Get SheetNum
SheetNum="1"
End Property
End Class
Function MakeNativeFake()
Dim App,Comp
Set App=New OwnedNativeApp
Set Comp=New FullComponent: Comp.UID="u1": Comp.Refdes="R1"
App.ActiveView.Components.Add Comp,True
Set Comp=New FullComponent: Comp.UID="u2": Comp.Refdes="R2"
App.ActiveView.Components.Add Comp,True
Set MakeNativeFake=App
End Function
'''
@pytest.mark.parametrize('fault', ['', 'foreign_guid', 'cancel', 'missing_target'])
def test_generated_native_script_collects_complete_readonly_frame(tmp_path, fault):
source = tmp_path / 'combined.vbs'
assert ADAPTER.is_file(), 'native snapshot adapter is missing'
source.write_text(MAIN.read_text(encoding='ascii') + '\n' + ADAPTER.read_text(encoding='ascii'), encoding='ascii')
generated, result = tmp_path/'generated.vbs', tmp_path/'result.tsv'
selected = 'missing' if fault == 'missing_target' else 'u1'
guid = 'FOREIGN' if fault == 'foreign_guid' else 'OWNED'
if fault == 'cancel':
(tmp_path/'monitor-stop-token.stop').write_text('stop-token', encoding='ascii')
run(tmp_path, '''
Dim QP_Root,Selected,Text
QP_Root=''' + quote(tmp_path) + '''
Set Selected=CreateObject("Scripting.Dictionary"): Selected.Add "''' + selected + '''",True
Text=QPBuildNativeSnapshotScript(QPReadUtf8(''' + quote(source) + '''),Selected,"nonce","c:\\board.prj","MAIN:1","''' + guid + '''",''' + quote(result) + ''',"stop-token",True)
QPWriteUtf8 ''' + quote(generated) + ''',Text
''')
text = generated.read_text(encoding='utf-8-sig')
text = text.replace('Set QP_App=GetObject(,"Viewdraw.Application")', 'Set QP_App=MakeNativeFake()')
# Supply the enum constants provided by the real CAD script host.
text += '\nConst VDM_COMP=128\nConst VDM_COMPPIN=4096\nConst VD_ALL=0\n' + NATIVE_FAKES
generated.write_text(text, encoding='ascii')
completed = subprocess.run(['cscript.exe', '//nologo', str(generated)], capture_output=True, text=True, timeout=15)
assert completed.returncode == 0, completed.stdout + completed.stderr
actual = result.read_bytes().decode('utf-8-sig')
if fault:
assert actual.startswith('ERROR\tnonce\t')
assert 'END\t' not in actual
if fault == 'cancel':
assert 'QuickPartReplace.SafeStop' in actual
else:
assert actual == FRAME