import os from pathlib import Path import subprocess import pytest from cbb_refresh_verify.runtime_timing import parse_save_progress @pytest.mark.parametrize('save_ok', [True, False]) @pytest.mark.parametrize('other_document', [True, False]) def test_packaged_vbs_measures_real_save_call_and_preserves_failure_gate(tmp_path, save_ok, other_document): # Execute the real helper in its target 32-bit host. Only external COM # object creation is replaced; Save/Validate/Close order and timers are real. script = Path(__file__).parents[1] / 'src/cbb_refresh_verify/save_verified_block.vbs' source = script.read_text(encoding='utf-8') classes = r''' Class FakePi Public ProjectFile, ForwardAnnotationStatus, SynchCESStatus, LoadCESStatus, BackAnnotationStatus Private Sub Class_Initialize ProjectFile = WScript.Arguments(0) ForwardAnnotationStatus = 2 SynchCESStatus = 2 LoadCESStatus = 2 BackAnnotationStatus = 2 End Sub End Class Class FakeDoc Public Property Get ProjectIntegration Set ProjectIntegration = New FakePi End Property Public Function Validate(token) Validate = 17 End Function Public Function Save() WScript.Sleep 120 Save = SAVE_RETURN End Function Public Function IsSaved() IsSaved = True End Function Public Function Close(saveChanges) closedDoc = True Close = True End Function End Class Class FakeApp Public Property Get ActiveDocument If closedDoc And Not OTHER_DOCUMENT Then Set ActiveDocument = Nothing Else Set ActiveDocument = New FakeDoc End If End Property Public Sub Quit() WScript.Echo "TEST_QUIT" End Sub End Class Class FakeLic Public Function GetToken(value) GetToken = value End Function End Class ''' source = source.replace('Option Explicit', 'Option Explicit\nDim closedDoc\n' + classes.replace('SAVE_RETURN', str(save_ok)).replace('OTHER_DOCUMENT', str(other_document))) source = source.replace('GetObject(, "MGCPCB.Application")', 'New FakeApp') source = source.replace('CreateObject("MGCPCBAutomationLicensing.Application")', 'New FakeLic') harness = tmp_path/'save_harness.vbs' harness.write_text(source, encoding='ascii') cscript = Path(os.environ['WINDIR'])/'SysWOW64/cscript.exe' completed = subprocess.run([str(cscript), '//nologo', str(harness), 'expected.prj'], capture_output=True, text=True, timeout=10) parsed = parse_save_progress(completed.stdout) assert parsed['timings_seconds']['save'] >= .08 if save_ok: assert completed.returncode == (10 if other_document else 0), completed.stderr assert 'SAVE_OK' in parsed['markers'] assert 'CLOSE_OK' in parsed['markers'] assert parsed['timings_seconds']['post_save_check'] >= 0 assert parsed['timings_seconds']['close'] >= 0 assert ('TEST_QUIT' in completed.stdout) is (not other_document) assert ('APP_RETAINED_OTHER_DOCUMENT' in completed.stdout) is other_document else: assert completed.returncode == 4, completed.stderr assert 'SAVE_OK' not in parsed['markers'] assert 'CLOSE_OK' not in parsed['markers']