webRemoteControlActionRequests.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { describe, expect, it, vi } from "vitest";
  2. import { respondToWebRemoteControlActionRequest } from "#src/main/web-remote-relay/index.js";
  3. describe("respondToWebRemoteControlActionRequest", () => {
  4. const pin = {
  5. zcode_type: "task-pin-request" as const,
  6. requestId: "pin-1",
  7. taskId: "t1",
  8. workspacePath: "/w",
  9. pinned: false,
  10. };
  11. it("Renderer 失败折成 success:false 应答", async () => {
  12. const run = vi.fn(async () => {
  13. throw new Error("Web remote control action timed out.");
  14. });
  15. await expect(respondToWebRemoteControlActionRequest(1, pin, run)).resolves.toEqual({
  16. zcode_type: "task-pin-response",
  17. requestId: "pin-1",
  18. taskId: "t1",
  19. success: false,
  20. error: "Web remote control action timed out.",
  21. });
  22. });
  23. it("未接 Renderer 时三类请求都回失败而不是抛错", async () => {
  24. await expect(
  25. respondToWebRemoteControlActionRequest(
  26. 1,
  27. { zcode_type: "task-archive-request", requestId: "a", taskId: "t1", workspacePath: "/w" },
  28. undefined,
  29. ),
  30. ).resolves.toMatchObject({ zcode_type: "task-archive-response", success: false });
  31. await expect(
  32. respondToWebRemoteControlActionRequest(
  33. 1,
  34. { zcode_type: "workspace-create-request", requestId: "c", name: "demo" },
  35. undefined,
  36. ),
  37. ).resolves.toMatchObject({ zcode_type: "workspace-create-response", success: false });
  38. });
  39. it("取消归档:archived:false 透传给 Renderer 并回显", async () => {
  40. const run = vi.fn(async () => ({ requestId: "ipc", success: true as const }));
  41. await expect(
  42. respondToWebRemoteControlActionRequest(
  43. 1,
  44. {
  45. zcode_type: "task-archive-request",
  46. requestId: "u",
  47. taskId: "t1",
  48. workspacePath: "/w",
  49. archived: false,
  50. },
  51. run,
  52. ),
  53. ).resolves.toEqual({
  54. zcode_type: "task-archive-response",
  55. requestId: "u",
  56. taskId: "t1",
  57. success: true,
  58. archived: false,
  59. });
  60. expect(run).toHaveBeenCalledWith(1, {
  61. kind: "task-archive",
  62. taskId: "t1",
  63. workspacePath: "/w",
  64. archived: false,
  65. });
  66. });
  67. it("新建项目缺路径视为失败", async () => {
  68. const run = vi.fn(async () => ({ requestId: "ipc", success: true as const }));
  69. await expect(
  70. respondToWebRemoteControlActionRequest(
  71. 1,
  72. { zcode_type: "workspace-create-request", requestId: "c", name: "demo" },
  73. run,
  74. ),
  75. ).resolves.toEqual({
  76. zcode_type: "workspace-create-response",
  77. requestId: "c",
  78. success: false,
  79. error: "Workspace creation returned no path.",
  80. });
  81. });
  82. });