browserScreenshotSurfaceCoordinatorWiring.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { describe, expect, it, vi } from "vitest";
  2. import { PlatformChannels } from "@zcode/shared";
  3. import { createDesktopBrowserScreenshotSurfaceCoordinator } from "../src/main/browserView/browserScreenshotSurfaceCoordinatorWiring.js";
  4. function prepareInput(requestId: string) {
  5. return {
  6. requestId,
  7. windowId: 7,
  8. workspaceKey: "workspace-7",
  9. sessionId: "session-7",
  10. browserId: "browser-7",
  11. browserGeneration: 2,
  12. tabId: "tab-7",
  13. webContentsId: 70,
  14. viewport: { width: 800, height: 600 },
  15. signal: new AbortController().signal,
  16. };
  17. }
  18. function readyPayload(requestId: string) {
  19. // Ready 回执必须包含有效的 surfaceScale;缺失时协调器会按真实协议拒绝该回执。
  20. return { ...prepareInput(requestId), signal: undefined, surfaceScale: 1 };
  21. }
  22. function withBackgroundActivity<
  23. T extends { isDestroyed(): boolean; send(...args: unknown[]): unknown },
  24. >(webContents: T) {
  25. return {
  26. ...webContents,
  27. id: 700,
  28. capturePage: vi.fn(() => new Promise<never>(() => undefined)),
  29. };
  30. }
  31. function guestFor(ownerWebContents: { id: number }) {
  32. return {
  33. id: 70,
  34. hostWebContents: ownerWebContents,
  35. isDestroyed: () => false,
  36. capturePage: vi.fn(() => new Promise<never>(() => undefined)),
  37. };
  38. }
  39. describe("createDesktopBrowserScreenshotSurfaceCoordinator", () => {
  40. it("只向 owner BrowserWindow 定向发送 prepare/release,并拒绝已销毁窗口", async () => {
  41. const send = vi.fn();
  42. const destroyed = vi.fn(() => false);
  43. const webContentsDestroyed = vi.fn(() => false);
  44. const webContents = withBackgroundActivity({
  45. isDestroyed: webContentsDestroyed,
  46. send,
  47. });
  48. const fromId = vi.fn((windowId: number) =>
  49. windowId === 7 ? { isDestroyed: destroyed, webContents } : null,
  50. );
  51. const guest = guestFor(webContents);
  52. const coordinator = createDesktopBrowserScreenshotSurfaceCoordinator({
  53. fromId,
  54. fromWebContentsId: () => guest,
  55. log: vi.fn(),
  56. });
  57. const pending = coordinator.prepare(prepareInput("request-7"));
  58. expect(send).toHaveBeenCalledWith(
  59. PlatformChannels.BrowserViewScreenshotSurfacePrepare,
  60. expect.objectContaining({ requestId: "request-7", tabId: "tab-7" }),
  61. );
  62. coordinator.handleReady({
  63. windowId: 7,
  64. senderWebContentsId: 700,
  65. payload: readyPayload("request-7"),
  66. });
  67. const lease = await pending;
  68. lease.release();
  69. await Promise.resolve();
  70. expect(send).toHaveBeenCalledWith(
  71. PlatformChannels.BrowserViewScreenshotSurfaceRelease,
  72. expect.objectContaining({ requestId: "request-7", tabId: "tab-7" }),
  73. );
  74. expect(webContents.capturePage).toHaveBeenCalledTimes(2);
  75. expect(guest.capturePage).toHaveBeenCalledTimes(2);
  76. destroyed.mockReturnValue(true);
  77. await expect(coordinator.prepare(prepareInput("request-destroyed"))).rejects.toThrow(
  78. "activity could not be acquired",
  79. );
  80. expect(fromId).toHaveBeenCalledWith(7);
  81. });
  82. it("webContents 已销毁时 prepare 失败且不调用 send", async () => {
  83. const send = vi.fn();
  84. const log = vi.fn();
  85. const ownerWebContents = withBackgroundActivity({ isDestroyed: () => true, send });
  86. const coordinator = createDesktopBrowserScreenshotSurfaceCoordinator({
  87. fromId: () => ({
  88. isDestroyed: () => false,
  89. webContents: ownerWebContents,
  90. }),
  91. fromWebContentsId: () => guestFor(ownerWebContents),
  92. log,
  93. });
  94. await expect(coordinator.prepare(prepareInput("webcontents-destroyed"))).rejects.toThrow(
  95. "activity could not be acquired",
  96. );
  97. expect(send).not.toHaveBeenCalled();
  98. expect(log).toHaveBeenCalled();
  99. });
  100. it("prepare send 抛错时转换为 preparation failure", async () => {
  101. const log = vi.fn();
  102. const ownerWebContents = withBackgroundActivity({
  103. isDestroyed: () => false,
  104. send: () => {
  105. throw new Error("renderer gone");
  106. },
  107. });
  108. const coordinator = createDesktopBrowserScreenshotSurfaceCoordinator({
  109. fromId: () => ({
  110. isDestroyed: () => false,
  111. webContents: ownerWebContents,
  112. }),
  113. fromWebContentsId: () => guestFor(ownerWebContents),
  114. log,
  115. });
  116. await expect(coordinator.prepare(prepareInput("prepare-send-throw"))).rejects.toThrow(
  117. "could not be sent",
  118. );
  119. expect(log).toHaveBeenCalled();
  120. });
  121. it("ready 后 release send 抛错不会从 lease.release 反向抛出", async () => {
  122. const log = vi.fn();
  123. const send = vi.fn(() => {
  124. if (send.mock.calls.length > 1) {
  125. throw new Error("window closing");
  126. }
  127. });
  128. const ownerWebContents = withBackgroundActivity({ isDestroyed: () => false, send });
  129. const coordinator = createDesktopBrowserScreenshotSurfaceCoordinator({
  130. fromId: () => ({
  131. isDestroyed: () => false,
  132. webContents: ownerWebContents,
  133. }),
  134. fromWebContentsId: () => guestFor(ownerWebContents),
  135. log,
  136. });
  137. const pending = coordinator.prepare(prepareInput("release-send-throw"));
  138. coordinator.handleReady({
  139. windowId: 7,
  140. senderWebContentsId: 700,
  141. payload: readyPayload("release-send-throw"),
  142. });
  143. const lease = await pending;
  144. expect(() => lease.release()).not.toThrow();
  145. expect(log).toHaveBeenCalled();
  146. });
  147. it("timeout 清理 release 时 send 抛错不会产生未处理异常", async () => {
  148. const log = vi.fn();
  149. const send = vi.fn(() => {
  150. if (send.mock.calls.length > 1) {
  151. throw new Error("window closing");
  152. }
  153. });
  154. const ownerWebContents = withBackgroundActivity({ isDestroyed: () => false, send });
  155. const coordinator = createDesktopBrowserScreenshotSurfaceCoordinator({
  156. fromId: () => ({
  157. isDestroyed: () => false,
  158. webContents: ownerWebContents,
  159. }),
  160. fromWebContentsId: () => guestFor(ownerWebContents),
  161. log,
  162. timeoutMs: 1,
  163. });
  164. await expect(coordinator.prepare(prepareInput("timeout-release-throw"))).rejects.toThrow(
  165. "timed out",
  166. );
  167. expect(log).toHaveBeenCalled();
  168. });
  169. });