primaryWindowCoordinator.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. import { createPrimaryWindowCoordinator } from "../src/main/primaryWindowCoordinator.js";
  3. function createDeferred<T>() {
  4. let resolve!: (value: T | PromiseLike<T>) => void;
  5. let reject!: (reason?: unknown) => void;
  6. const promise = new Promise<T>((res, rej) => {
  7. resolve = res;
  8. reject = rej;
  9. });
  10. return { promise, resolve, reject };
  11. }
  12. describe("primaryWindowCoordinator", () => {
  13. const windows: Array<{
  14. isDestroyed: () => boolean;
  15. isVisible: () => boolean;
  16. isMinimized: () => boolean;
  17. isRendererCrashed?: () => boolean;
  18. restore: ReturnType<typeof vi.fn>;
  19. show: ReturnType<typeof vi.fn>;
  20. focus: ReturnType<typeof vi.fn>;
  21. destroy?: ReturnType<typeof vi.fn>;
  22. }> = [];
  23. beforeEach(() => {
  24. windows.length = 0;
  25. });
  26. it("ready 和 activate 并发时只会创建一个主窗口", async () => {
  27. const deferred = createDeferred<{ initialWorkspacePath?: string }>();
  28. const createWindow = vi.fn();
  29. const resolveStartupWindowBootstrap = vi.fn(() => deferred.promise);
  30. const coordinator = createPrimaryWindowCoordinator({
  31. listWindows: () => windows,
  32. resolveStartupWindowBootstrap,
  33. createWindow,
  34. logger: { info: vi.fn() },
  35. });
  36. const readyPromise = coordinator.ensurePrimaryWindow("app-ready");
  37. const activatePromise = coordinator.ensurePrimaryWindow("app-activate");
  38. expect(resolveStartupWindowBootstrap).toHaveBeenCalledTimes(1);
  39. expect(createWindow).not.toHaveBeenCalled();
  40. deferred.resolve({ initialWorkspacePath: "/Users/dev/test/my-react-app" });
  41. await Promise.all([readyPromise, activatePromise]);
  42. expect(createWindow).toHaveBeenCalledTimes(1);
  43. expect(createWindow).toHaveBeenCalledWith({
  44. initialWorkspacePath: "/Users/dev/test/my-react-app",
  45. });
  46. });
  47. it("已有隐藏窗口时优先复用而不是新建", async () => {
  48. const hiddenWindow = {
  49. isDestroyed: () => false,
  50. isVisible: () => false,
  51. isMinimized: () => false,
  52. restore: vi.fn(),
  53. show: vi.fn(),
  54. focus: vi.fn(),
  55. };
  56. windows.push(hiddenWindow);
  57. const createWindow = vi.fn();
  58. const resolveStartupWindowBootstrap = vi.fn(async () => ({
  59. initialWorkspacePath: "/Users/dev/test/my-react-app",
  60. }));
  61. const coordinator = createPrimaryWindowCoordinator({
  62. listWindows: () => windows,
  63. resolveStartupWindowBootstrap,
  64. createWindow,
  65. logger: { info: vi.fn() },
  66. });
  67. await coordinator.ensurePrimaryWindow("app-activate");
  68. expect(hiddenWindow.show).toHaveBeenCalledTimes(1);
  69. expect(hiddenWindow.focus).toHaveBeenCalledTimes(1);
  70. expect(resolveStartupWindowBootstrap).not.toHaveBeenCalled();
  71. expect(createWindow).not.toHaveBeenCalled();
  72. });
  73. it("已有窗口 renderer 崩溃时销毁旧窗口并创建新主窗口", async () => {
  74. let destroyed = false;
  75. const crashedWindow = {
  76. isDestroyed: () => destroyed,
  77. isRendererCrashed: () => true,
  78. isVisible: () => false,
  79. isMinimized: () => false,
  80. restore: vi.fn(),
  81. show: vi.fn(),
  82. focus: vi.fn(),
  83. destroy: vi.fn(() => {
  84. destroyed = true;
  85. }),
  86. };
  87. windows.push(crashedWindow);
  88. const createWindow = vi.fn();
  89. const resolveStartupWindowBootstrap = vi.fn(async () => ({
  90. initialWorkspacePath: "/Users/dev/test/my-react-app",
  91. }));
  92. const coordinator = createPrimaryWindowCoordinator({
  93. listWindows: () => windows,
  94. resolveStartupWindowBootstrap,
  95. createWindow,
  96. logger: { info: vi.fn() },
  97. });
  98. await coordinator.ensurePrimaryWindow("app-activate");
  99. expect(crashedWindow.destroy).toHaveBeenCalledTimes(1);
  100. expect(crashedWindow.show).not.toHaveBeenCalled();
  101. expect(crashedWindow.focus).not.toHaveBeenCalled();
  102. expect(resolveStartupWindowBootstrap).toHaveBeenCalledTimes(1);
  103. expect(createWindow).toHaveBeenCalledWith({
  104. initialWorkspacePath: "/Users/dev/test/my-react-app",
  105. });
  106. });
  107. it("强制升级 gate 阻断时不会复用或创建主窗口", async () => {
  108. const hiddenWindow = {
  109. isDestroyed: () => false,
  110. isVisible: () => false,
  111. isMinimized: () => false,
  112. restore: vi.fn(),
  113. show: vi.fn(),
  114. focus: vi.fn(),
  115. };
  116. windows.push(hiddenWindow);
  117. const createWindow = vi.fn();
  118. const resolveStartupWindowBootstrap = vi.fn(async () => ({
  119. initialWorkspacePath: "/Users/dev/test/my-react-app",
  120. }));
  121. const coordinator = createPrimaryWindowCoordinator({
  122. listWindows: () => windows,
  123. resolveStartupWindowBootstrap,
  124. createWindow,
  125. canCreateWindow: () => false,
  126. logger: { info: vi.fn() },
  127. });
  128. await coordinator.ensurePrimaryWindow("app-activate");
  129. expect(hiddenWindow.show).not.toHaveBeenCalled();
  130. expect(hiddenWindow.focus).not.toHaveBeenCalled();
  131. expect(resolveStartupWindowBootstrap).not.toHaveBeenCalled();
  132. expect(createWindow).not.toHaveBeenCalled();
  133. });
  134. });