webRemoteControlStatusEvents.test.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { describe, expect, it, vi } from "vitest";
  2. import { PlatformChannels, type WebRemoteControlStatus } from "@zcode/shared";
  3. import { sendWebRemoteControlStatusChangedToWindow } from "@desktop/main/webRemoteControlStatusEvents.js";
  4. function createTargetWindow({
  5. windowDestroyed = false,
  6. webContentsDestroyed = false,
  7. }: {
  8. windowDestroyed?: boolean;
  9. webContentsDestroyed?: boolean;
  10. } = {}) {
  11. return {
  12. isDestroyed: vi.fn(() => windowDestroyed),
  13. webContents: {
  14. isDestroyed: vi.fn(() => webContentsDestroyed),
  15. send: vi.fn(),
  16. },
  17. };
  18. }
  19. describe("sendWebRemoteControlStatusChangedToWindow", () => {
  20. const status: WebRemoteControlStatus = {
  21. status: "running",
  22. sessionId: "sid-1",
  23. };
  24. it("sends the status event to a live renderer", () => {
  25. const targetWindow = createTargetWindow();
  26. sendWebRemoteControlStatusChangedToWindow(targetWindow, status);
  27. expect(targetWindow.webContents.send).toHaveBeenCalledWith(
  28. PlatformChannels.WebRemoteControlStatusChanged,
  29. status,
  30. );
  31. });
  32. it("skips the status event after webContents has been destroyed", () => {
  33. const targetWindow = createTargetWindow({ webContentsDestroyed: true });
  34. sendWebRemoteControlStatusChangedToWindow(targetWindow, status);
  35. expect(targetWindow.webContents.send).not.toHaveBeenCalled();
  36. });
  37. it("skips the status event after the window has been destroyed", () => {
  38. const targetWindow = createTargetWindow({ windowDestroyed: true });
  39. sendWebRemoteControlStatusChangedToWindow(targetWindow, status);
  40. expect(targetWindow.webContents.send).not.toHaveBeenCalled();
  41. });
  42. });