webRemoteControlTaskViewPublisher.test.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { expect, it, vi } from "vitest";
  2. import { createWebRemoteControlTaskViewPublisher } from "#src/web-remote-control/index.js";
  3. const first = {
  4. kind: "local" as const,
  5. bridgeSessionId: "first",
  6. bridgeGeneration: 1,
  7. workspacePath: "/one",
  8. workspaceKey: "/one",
  9. };
  10. const second = {
  11. kind: "remote" as const,
  12. bridgeSessionId: "second",
  13. bridgeGeneration: 2,
  14. workspacePath: "/one",
  15. workspaceKey: "remote:server:other:/one",
  16. workspaceIdentity: "remote:server:other:/one",
  17. remoteSessionId: "remote",
  18. };
  19. const view = { workspacePath: "/one", taskId: "task" };
  20. it("MRO03:先有可见事实再收到 epoch 仍立即声明,bootstrap 不覆盖更新的 epoch", () => {
  21. const send = vi.fn(() => true);
  22. const publisher = createWebRemoteControlTaskViewPublisher(send);
  23. publisher.setBridge(first);
  24. publisher.update(first, view);
  25. expect(send).not.toHaveBeenCalled();
  26. publisher.request("epoch-new");
  27. expect(send).toHaveBeenLastCalledWith({
  28. zcode_type: "mobile-task-view-update",
  29. epoch: "epoch-new",
  30. seq: 1,
  31. bridgeSessionId: "first",
  32. bridgeGeneration: 1,
  33. view,
  34. });
  35. publisher.seed("epoch-old");
  36. publisher.flush();
  37. expect(send).toHaveBeenCalledOnce();
  38. publisher.update(first, null);
  39. expect(send).toHaveBeenLastCalledWith(
  40. expect.objectContaining({ epoch: "epoch-new", seq: 2, view: null }),
  41. );
  42. });
  43. it("MRO03:断连期间更新 Home/隐藏事实,新 epoch 只重申当前事实", () => {
  44. const send = vi.fn(() => true);
  45. const publisher = createWebRemoteControlTaskViewPublisher(send);
  46. publisher.setBridge(first);
  47. publisher.seed("one");
  48. publisher.update(first, view);
  49. publisher.disconnected();
  50. send.mockClear();
  51. publisher.update(first, null);
  52. publisher.flush();
  53. expect(send).not.toHaveBeenCalled();
  54. publisher.request("two");
  55. expect(send).toHaveBeenLastCalledWith(
  56. expect.objectContaining({ epoch: "two", seq: 1, view: null }),
  57. );
  58. });
  59. it("MRO03:新 Root 不继承旧 bridge 查看,旧 Root cleanup 不能清新查看", () => {
  60. const send = vi.fn(() => true);
  61. const publisher = createWebRemoteControlTaskViewPublisher(send);
  62. publisher.seed("one");
  63. publisher.setBridge(first);
  64. publisher.update(first, view);
  65. publisher.setBridge(second);
  66. expect(send).toHaveBeenLastCalledWith(
  67. expect.objectContaining({ bridgeSessionId: "second", view: null }),
  68. );
  69. const current = { ...view, workspaceIdentity: second.workspaceIdentity };
  70. publisher.update(second, current);
  71. const count = send.mock.calls.length;
  72. publisher.update(first, null);
  73. publisher.update({ ...second, bridgeGeneration: 1 }, null);
  74. expect(send).toHaveBeenCalledTimes(count);
  75. expect(send).toHaveBeenLastCalledWith(
  76. expect.objectContaining({ bridgeSessionId: "second", view: current }),
  77. );
  78. });
  79. it("MRO03:发送未就绪只保留最新投影,send-ready 不持续重复已成功声明", () => {
  80. const send = vi.fn(() => false);
  81. const publisher = createWebRemoteControlTaskViewPublisher(send);
  82. publisher.setBridge(first);
  83. publisher.seed("one");
  84. publisher.update(first, view);
  85. publisher.update(first, null);
  86. send.mockReturnValue(true);
  87. publisher.flush();
  88. expect(send).toHaveBeenLastCalledWith(expect.objectContaining({ view: null }));
  89. const count = send.mock.calls.length;
  90. publisher.flush();
  91. expect(send).toHaveBeenCalledTimes(count);
  92. });