file-application-menu-probe.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {
  2. PlatformChannels,
  3. type FileApplicationInfo,
  4. type FileApplicationTarget,
  5. } from "@zcode/shared";
  6. type Handler = (...args: unknown[]) => unknown;
  7. declare global {
  8. var __apcFileApplications:
  9. | {
  10. calls: Array<{ applicationId: string; target: FileApplicationTarget }>;
  11. revealCalls: string[];
  12. locateCalls: Array<{ editorId: string; path: string; pathKind?: string }>;
  13. restore(): void;
  14. }
  15. | undefined;
  16. }
  17. /**
  18. * 只在隔离 E2E 主进程替换目标文件的 IPC;不启动或安装任何真实外部应用。
  19. * 安装期间“在文件管理器中打开”(OpenInFileManager 打开目录、OpenInEditor 定位并选中文件)
  20. * 全部只记录参数,不真的拉起 Finder/资源管理器。
  21. */
  22. export async function installFileApplicationMenuProbe(
  23. targetPath: string,
  24. applications: FileApplicationInfo[],
  25. ) {
  26. await browser.electron.execute(
  27. (electron, channels, targetPath, applications) => {
  28. if (globalThis.__apcFileApplications)
  29. throw new Error("File application probe already installed");
  30. const handlers = (electron.ipcMain as unknown as { _invokeHandlers: Map<string, Handler> })
  31. ._invokeHandlers;
  32. const query = handlers.get(channels.GetFileApplications);
  33. const open = handlers.get(channels.OpenFileWithApplication);
  34. const reveal = handlers.get(channels.OpenInFileManager);
  35. const locate = handlers.get(channels.OpenInEditor);
  36. if (!query || !open || !reveal || !locate)
  37. throw new Error("File application IPC handlers unavailable");
  38. const probe = {
  39. calls: [] as Array<{ applicationId: string; target: FileApplicationTarget }>,
  40. revealCalls: [] as string[],
  41. locateCalls: [] as Array<{ editorId: string; path: string; pathKind?: string }>,
  42. restore() {
  43. handlers.set(channels.GetFileApplications, query);
  44. handlers.set(channels.OpenFileWithApplication, open);
  45. handlers.set(channels.OpenInFileManager, reveal);
  46. handlers.set(channels.OpenInEditor, locate);
  47. delete globalThis.__apcFileApplications;
  48. },
  49. };
  50. handlers.set(channels.GetFileApplications, (event, target) =>
  51. (target as FileApplicationTarget)?.path === targetPath
  52. ? applications
  53. : query(event, target),
  54. );
  55. handlers.set(channels.OpenFileWithApplication, (event, payload) => {
  56. const request = payload as { applicationId: string; target: FileApplicationTarget };
  57. if (request?.target?.path !== targetPath) return open(event, payload);
  58. probe.calls.push(request);
  59. return { success: true };
  60. });
  61. handlers.set(channels.OpenInFileManager, (_event, path) => {
  62. probe.revealCalls.push(path as string);
  63. return { success: true };
  64. });
  65. handlers.set(channels.OpenInEditor, (_event, payload) => {
  66. const request = payload as {
  67. editorId: string;
  68. path: string;
  69. options?: { pathKind?: string };
  70. };
  71. probe.locateCalls.push({
  72. editorId: request.editorId,
  73. path: request.path,
  74. pathKind: request.options?.pathKind,
  75. });
  76. return { success: true };
  77. });
  78. globalThis.__apcFileApplications = probe;
  79. },
  80. PlatformChannels,
  81. targetPath,
  82. applications,
  83. );
  84. }
  85. export async function readFileApplicationMenuProbe() {
  86. return browser.electron.execute(() => globalThis.__apcFileApplications?.calls ?? []);
  87. }
  88. export async function readFileManagerRevealProbe() {
  89. return browser.electron.execute(() => globalThis.__apcFileApplications?.revealCalls ?? []);
  90. }
  91. export async function readFileLocateProbe() {
  92. return browser.electron.execute(() => globalThis.__apcFileApplications?.locateCalls ?? []);
  93. }
  94. export async function restoreFileApplicationMenuProbe() {
  95. await browser.electron.execute(() => globalThis.__apcFileApplications?.restore());
  96. }