webRemoteProjectTreeActivity.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { IProjectService, ProjectTree } from "../src/project/contract.js";
  3. import type { WindowHostControllerTaskRow } from "@zcode/shared/zcode-protocol-v4";
  4. import {
  5. buildPendingInteractionSignature,
  6. createWebRemoteProjectTreeActivityBridge,
  7. overlayProjectTreeTaskActivity,
  8. } from "../src/window-project-directory/webRemoteProjectTreeActivity.js";
  9. function row(
  10. taskId: string,
  11. counts?: { permissionCount: number; userInputCount: number },
  12. workspaceIdentity?: string,
  13. ): WindowHostControllerTaskRow {
  14. return {
  15. address: { taskId, workspacePath: "/w", ...(workspaceIdentity ? { workspaceIdentity } : {}) },
  16. activity: counts
  17. ? { phase: "idle", lastActivityAt: 1, hasBackgroundWork: false, pendingInteractions: counts }
  18. : undefined,
  19. } as unknown as WindowHostControllerTaskRow;
  20. }
  21. function tree(...taskIds: string[]): ProjectTree {
  22. return {
  23. tasks: taskIds.map((taskId) => ({ taskId, workspacePath: "/w" })),
  24. } as unknown as ProjectTree;
  25. }
  26. describe("overlayProjectTreeTaskActivity", () => {
  27. it("给有待处理交互的任务补计数和标记,权限优先", () => {
  28. const result = overlayProjectTreeTaskActivity(tree("a", "b", "c"), [
  29. row("a", { permissionCount: 1, userInputCount: 2 }),
  30. row("b", { permissionCount: 0, userInputCount: 1 }),
  31. row("c", { permissionCount: 0, userInputCount: 0 }),
  32. ]);
  33. expect(result.tasks[0]).toMatchObject({
  34. pendingInteractions: { permissionCount: 1, userInputCount: 2 },
  35. pendingInteraction: { kind: "permission" },
  36. });
  37. expect(result.tasks[1]).toMatchObject({ pendingInteraction: { kind: "userInput" } });
  38. expect(result.tasks[2]).not.toHaveProperty("pendingInteractions");
  39. });
  40. it("远程任务按 workspaceIdentity 匹配,不串到同路径本地任务", () => {
  41. const result = overlayProjectTreeTaskActivity(tree("a"), [
  42. row("a", { permissionCount: 1, userInputCount: 0 }, "remote:ssh:h:/w"),
  43. ]);
  44. expect(result.tasks[0]).not.toHaveProperty("pendingInteractions");
  45. });
  46. });
  47. describe("buildPendingInteractionSignature", () => {
  48. it("与行顺序无关,忽略无交互行", () => {
  49. const a = row("a", { permissionCount: 1, userInputCount: 0 });
  50. const b = row("b", { permissionCount: 0, userInputCount: 1 });
  51. expect(buildPendingInteractionSignature([a, b, row("c")])).toBe(
  52. buildPendingInteractionSignature([b, a]),
  53. );
  54. });
  55. });
  56. describe("createWebRemoteProjectTreeActivityBridge", () => {
  57. it("引用计数订阅;签名变化才广播", () => {
  58. let rows: WindowHostControllerTaskRow[] = [];
  59. let onChange: (() => void) | undefined;
  60. const unsubscribe = vi.fn();
  61. const subscribeRows = vi.fn((listener: () => void) => {
  62. onChange = listener;
  63. listener(); // 与投影订阅一致:订阅即回放一次快照。
  64. return { dispose: unsubscribe };
  65. });
  66. const notifyProjectChange = vi.fn();
  67. const bridge = createWebRemoteProjectTreeActivityBridge({
  68. readRows: () => rows,
  69. subscribeRows,
  70. notifyProjectChange,
  71. });
  72. const first = bridge.retain();
  73. const second = bridge.retain();
  74. expect(subscribeRows).toHaveBeenCalledTimes(1);
  75. expect(notifyProjectChange).not.toHaveBeenCalled();
  76. rows = [row("a", { permissionCount: 1, userInputCount: 0 })];
  77. onChange?.();
  78. onChange?.();
  79. expect(notifyProjectChange).toHaveBeenCalledTimes(1);
  80. first.dispose();
  81. first.dispose();
  82. expect(unsubscribe).not.toHaveBeenCalled();
  83. second.dispose();
  84. expect(unsubscribe).toHaveBeenCalledTimes(1);
  85. });
  86. it("decorate 只改写 readTree,其余方法透传", async () => {
  87. const base = {
  88. readTree: vi.fn(async () => tree("a")),
  89. readProjects: vi.fn(async () => []),
  90. } as unknown as IProjectService;
  91. const bridge = createWebRemoteProjectTreeActivityBridge({
  92. readRows: () => [row("a", { permissionCount: 0, userInputCount: 3 })],
  93. subscribeRows: () => ({ dispose() {} }),
  94. notifyProjectChange: () => {},
  95. });
  96. const decorated = bridge.decorate(base);
  97. expect((await decorated.readTree()).tasks[0]).toMatchObject({
  98. pendingInteractions: { permissionCount: 0, userInputCount: 3 },
  99. });
  100. await decorated.readProjects();
  101. expect(base.readProjects).toHaveBeenCalledTimes(1);
  102. });
  103. });