from pathlib import Path import shutil import subprocess import pytest from dx_productivity.bridge.startup_assets import generate_startup_assets @pytest.fixture def portable_root(tmp_path): root = tmp_path / "一叶知秋 工具" integration = Path(__file__).parents[2] / "integration" / "dxdesigner795" root.mkdir() (root / "DxDesignerTools.exe").write_bytes(b"fixture") for name in ("MenuAdd.vbs", "DesignBridge.vbs", "CompatibilityProbe.vbs", "LoadMenu.vbs"): source = integration / name if source.exists(): shutil.copy2(source, root / name) runtime = root / "_runtime" / "vbs" runtime.mkdir(parents=True) shutil.copy2(integration / "BridgeCommon.vbs", runtime) for source in (integration / "core").glob("*.vbs"): shutil.copy2(source, runtime) return root def test_generated_menu_loads_without_host_script_path_metadata(portable_root, tmp_path): assets = generate_startup_assets(portable_root, tmp_path / "用户 profile") # Run the real VBS without DxDesigner script-path globals: the reported bug. fixture = Path(__file__).parents[1] / "vbs" / "test_menu_add.vbs" fake_host = fixture.read_text(encoding="ascii").split('Set Fso = CreateObject')[0] fake_host = fake_host.replace("Const cmdControlPopup = 1", "") collection = fake_host.split("Class FakeControlsCollection", 1)[1].split("End Class", 1)[0] popup_collection = "Class FakePopupControlsCollection" + collection.replace( "Public Function Add()", 'Public Function Add(controlType)\n If CStr(controlType) <> "1" Then WScript.Quit 5', ) + "End Class\n" harness = tmp_path / "host.vbs" harness.write_text( fake_host + popup_collection + '\nSet Fso = CreateObject("Scripting.FileSystemObject")\n' + 'Set FakeMenuBar = New FakeControl\n' + 'Set FakeMenuBar.Controls = New FakePopupControlsCollection\n' + 'Set FakeMainMenuBar = New FakeControl\n' + 'Set FakeMainMenuBar.Controls = New FakePopupControlsCollection\n' + 'MenuText = Fso.OpenTextFile(WScript.Arguments(0), 1).ReadAll\n' + 'ExecuteGlobal MenuText\n' + 'If FakeMenuBar.Controls.Count <> 1 Then WScript.Quit 1\n' + 'If FakeMainMenuBar.Controls.Count <> 1 Then WScript.Quit 6\n' + 'If FakeMenuBar.Controls.Item(1).Controls.Count <> 8 Then WScript.Quit 2\n' + 'For Each SourceMenu In Array(1, 2, 3, 4, 5, 6, 7, 8)\n' + ' If Left(FakeMenuBar.Controls.Item(1).Controls.Item(SourceMenu).OnAction, 5) <> "run """ Then WScript.Quit 3\n' + 'Next\n' + 'Call DxToolsInstallMenu()\n' + 'If FakeMenuBar.Controls.Count <> 1 Then WScript.Quit 4\n' + 'FakeMenuBar.Controls.Item(1).Delete\n' + 'FakeMainMenuBar.Controls.Item(1).Delete\n' + 'Call DxToolsHost_ProjectChanged(Nothing)\n' + 'If FakeMenuBar.Controls.Count <> 1 Then WScript.Quit 7\n' + 'If FakeMainMenuBar.Controls.Count <> 1 Then WScript.Quit 8\n' + 'WScript.Echo "menu events verified"\n', encoding="ascii", ) result = subprocess.run( ["cscript", "//nologo", str(harness), str(assets.menu_script)], capture_output=True, timeout=10, ) assert result.returncode == 0, result.stderr.decode(errors="replace") assert b"menu events verified" in result.stdout, result.stderr.decode(errors="replace") assert assets.ready_file.read_text(encoding="utf-16").strip() == assets.token assert set(assets.action_scripts) == { "refdes", "property-change", "property-visibility", "inspection", "bom", "pdf-export", "compatibility", "guide", } for script in assets.action_scripts.values(): assert script.read_bytes().isascii() compiler = tmp_path / "compile-action.vbs" compiler.write_text( 'Dim text\ntext = CreateObject("Scripting.FileSystemObject").OpenTextFile(WScript.Arguments(0), 1).ReadAll\n' 'ExecuteGlobal Split(text, "\' DXTOOLS_ENTRY")(0)\n', encoding="ascii", ) result = subprocess.run(["cscript", "//nologo", str(compiler), str(script)], capture_output=True, timeout=10) assert result.returncode == 0, result.stderr.decode(errors="replace") def test_startup_persists_in_normal_wdir_and_preserves_existing_actions(portable_root, tmp_path): from dx_productivity.bridge.startup import prepare_startup install = tmp_path / "Mentor" / "7.9.5EE" / "SDD_HOME" viewdraw = install / "wv" / "win32" / "bin" / "viewdraw.exe" viewdraw.parent.mkdir(parents=True) viewdraw.write_bytes(b"fixture") standard = install / "standard" standard.mkdir() vendor = standard / "scripts.ini" original = b"[LibraryStudio]\r\nScript#0=library.vbs\r\n[ViewDraw]\r\nScript#0=company.vbs\r\n" vendor.write_bytes(original) (standard / "vllib.vmb").write_bytes(b"vendor message catalog") environment = {"LOCALAPPDATA": str(tmp_path / "profile"), "WDIR": str(standard)} configuration = prepare_startup(portable_root, viewdraw, environment) configuration_again = prepare_startup(portable_root, viewdraw, environment) registered = vendor.read_text(encoding="gbk") assert "Script#0=company.vbs" in registered assert "Script#0=library.vbs" in registered assert registered.count(str(configuration.assets.menu_script)) == 2 assert f"Script#1={configuration.assets.menu_script}" in registered assert f"脚本#1={configuration.assets.menu_script}" in registered assert configuration.scripts_ini == vendor assert configuration_again.scripts_ini == configuration.scripts_ini assert configuration.launch_environment["WDIR"].split(";") == [str(standard)] backups = list((tmp_path / "profile" / "DxProductivity" / "backups").glob("startup-scripts.*.bak")) assert len(backups) == 1 assert backups[0].read_bytes() == original def test_incomplete_tool_package_is_rejected_before_registering_any_script(portable_root, tmp_path): from dx_productivity.bridge.startup import StartupError, prepare_startup (portable_root / "_runtime" / "vbs" / "Snapshot.vbs").unlink() profile = tmp_path / "untouched" with pytest.raises(StartupError, match="Snapshot.vbs"): prepare_startup(portable_root, tmp_path / "viewdraw.exe", {"LOCALAPPDATA": str(profile)}) assert not profile.exists() def test_start_never_reports_success_from_a_stale_host_receipt(portable_root, tmp_path, monkeypatch): from dx_productivity.bridge.startup import StartupError, start_dxdesigner install = tmp_path / "SDD_HOME" viewdraw = install / "wv/win32/bin/viewdraw.exe" viewdraw.parent.mkdir(parents=True) viewdraw.write_bytes(b"fixture") standard = install / "standard" standard.mkdir() (standard / "vllib.vmb").write_bytes(b"catalog") profile = tmp_path / "profile" ready = profile / "DxProductivity/startup-refactored/menu-ready.txt" ready.parent.mkdir(parents=True) ready.write_text("old-token", encoding="utf-16") monkeypatch.setattr(subprocess, "run", lambda *args, **kwargs: subprocess.CompletedProcess(args[0], 0, b"", b"")) with pytest.raises(StartupError, match="菜单加载回执"): start_dxdesigner(portable_root, viewdraw=viewdraw, environment={"LOCALAPPDATA": str(profile), "WDIR": str(standard)}, timeout=0.01) def test_start_launches_absent_host_with_private_configuration(portable_root, tmp_path, monkeypatch): import re from dx_productivity.bridge.startup import start_dxdesigner install = tmp_path / "SDD_HOME" viewdraw = install / "wv/win32/bin/viewdraw.exe" viewdraw.parent.mkdir(parents=True) viewdraw.write_bytes(b"fixture") standard = install / "standard" standard.mkdir() (standard / "vllib.vmb").write_bytes(b"catalog") profile = tmp_path / "profile" environment = {"LOCALAPPDATA": str(profile), "WDIR": str(standard)} launched = [] monkeypatch.setattr(subprocess, "run", lambda *args, **kwargs: subprocess.CompletedProcess(args[0], 3, b"", b"")) def launch(command, *, cwd, env): launched.append((command, cwd, env)) private = profile / "DxProductivity/startup-refactored" token = re.search(r'stream.WriteLine "([0-9a-f]+)"', (private / "MenuAdd.vbs").read_text(encoding="ascii"))[1] (private / "menu-ready.txt").write_text(token, encoding="utf-16") monkeypatch.setattr(subprocess, "Popen", launch) assert "已启动" in start_dxdesigner(portable_root, viewdraw=viewdraw, environment=environment, timeout=0.1) assert len(launched) == 1 command, cwd, child = launched[0] assert command == [str(viewdraw.resolve())] assert cwd == viewdraw.parent.resolve() assert child["WDIR"].split(";")[0] == str(standard) assert child["DXTOOLS_HOME"] == str(portable_root.resolve()) assert environment == {"LOCALAPPDATA": str(profile), "WDIR": str(standard)}