windowHostArchivedTaskDeletion.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { describe, expect, it, vi } from "vitest";
  2. import { Emitter } from "@zcode/rpc";
  3. import type { IZCodeTaskService } from "@zcode/services";
  4. import type { ZCodeTaskMeta, ZCodeWorkspaceEvent } from "@zcode/shared";
  5. import { createWindowHostControllerRuntime } from "@desktop/host/windowHostControllerService.js";
  6. import {
  7. collectArchivedTaskDeletion,
  8. deleteArchivedTaskSelection,
  9. } from "@/lib/archivedTaskDeletion.js";
  10. function createSource(count: number, workspacePath = "/isolated/probe") {
  11. const events = new Emitter<ZCodeWorkspaceEvent>();
  12. const rows = new Map<string, ZCodeTaskMeta>(
  13. Array.from({ length: count }, (_, index) => {
  14. const task: ZCodeTaskMeta = {
  15. taskId: `task-${index}`,
  16. traceId: `trace-${index}`,
  17. title: `task-${index}`,
  18. workspacePath,
  19. createdAt: 1,
  20. updatedAt: 1,
  21. mode: "default",
  22. };
  23. return [task.taskId, task];
  24. }),
  25. );
  26. const emitDeleted = () =>
  27. events.fire({
  28. type: "workspace_task_list_changed",
  29. workspacePath,
  30. reason: "task_deleted",
  31. });
  32. const source = {
  33. listTasks: vi.fn(async () => []),
  34. listPinnedTasks: vi.fn(async () => []),
  35. listArchivedTasks: vi.fn(async () => [...rows.values()]),
  36. onDynamicWorkspaceEvent: () => events.event,
  37. deleteArchivedTask: vi.fn(async ({ taskId }: { taskId: string }) => {
  38. const deleted = rows.delete(taskId);
  39. if (deleted) emitDeleted();
  40. return deleted;
  41. }),
  42. deleteArchivedTasks: vi.fn(async ({ taskIds }: { taskIds: string[] }) => {
  43. const deletedTaskIds = taskIds.filter((taskId) => rows.delete(taskId));
  44. if (deletedTaskIds.length) emitDeleted();
  45. return { deletedTaskIds, skippedTaskIds: [], failedTaskIds: [] };
  46. }),
  47. };
  48. return { source, rows };
  49. }
  50. describe("归档批次跨 UI / Controller 的刷新边界", () => {
  51. it.each([1, 8, 72])("%i 个目标只发一个批次,Host 分区读取不随目标数增长", async (count) => {
  52. const workspacePath = "/isolated/probe";
  53. const { source, rows } = createSource(count);
  54. const runtime = createWindowHostControllerRuntime({
  55. createId: () => "probe",
  56. resolveSource: () => ({
  57. scope: { kind: "local", workspacePath },
  58. sourceAvailability: "online",
  59. taskService: source as unknown as IZCodeTaskService,
  60. }),
  61. });
  62. try {
  63. const facade = {
  64. listArchivedTasks: source.listArchivedTasks,
  65. deleteArchivedTask: async (params: { taskId: string; workspacePath: string }) =>
  66. runtime.service.deleteArchivedTask({
  67. address: await runtime.resolveTaskAddress({ ...params, allowMissingTask: true }),
  68. }),
  69. deleteArchivedTasks: async (params: { taskIds: string[]; workspacePath: string }) =>
  70. runtime.service.deleteArchivedTasks({
  71. address: await runtime.resolveTaskAddress({
  72. ...params,
  73. taskId: params.taskIds[0]!,
  74. allowMissingTask: true,
  75. }),
  76. taskIds: params.taskIds,
  77. }),
  78. };
  79. await runtime.resolveTaskAddress({ workspacePath, taskId: "task-0" });
  80. const selection = await collectArchivedTaskDeletion([
  81. { workspacePath, label: "probe", service: facade },
  82. ]);
  83. source.listTasks.mockClear();
  84. source.listPinnedTasks.mockClear();
  85. source.listArchivedTasks.mockClear();
  86. expect(await deleteArchivedTaskSelection(selection, vi.fn())).toMatchObject({
  87. deleted: count,
  88. skipped: 0,
  89. failed: 0,
  90. });
  91. expect(rows.size).toBe(0);
  92. expect(source.deleteArchivedTasks).toHaveBeenCalledTimes(1);
  93. expect(source.deleteArchivedTask).not.toHaveBeenCalled();
  94. // 事件通知与 mutation 回包共享 single-flight;一次批次只读取三个持久分区。
  95. expect(source.listTasks).toHaveBeenCalledTimes(1);
  96. expect(source.listPinnedTasks).toHaveBeenCalledTimes(1);
  97. expect(source.listArchivedTasks).toHaveBeenCalledTimes(1);
  98. } finally {
  99. runtime.dispose();
  100. }
  101. });
  102. it("批次持久化成功后,列表读取失败不得改报删除失败", async () => {
  103. const workspacePath = "/isolated/probe";
  104. const { source, rows } = createSource(2);
  105. const onSourceError = vi.fn();
  106. const runtime = createWindowHostControllerRuntime({
  107. createId: () => "probe",
  108. onSourceError,
  109. resolveSource: () => ({
  110. scope: { kind: "local", workspacePath },
  111. sourceAvailability: "online",
  112. taskService: source as unknown as IZCodeTaskService,
  113. }),
  114. });
  115. try {
  116. const address = await runtime.resolveTaskAddress({ workspacePath, taskId: "task-0" });
  117. source.listTasks.mockRejectedValue(new Error("refresh failed"));
  118. expect(
  119. await runtime.service.deleteArchivedTasks({ address, taskIds: ["task-0", "task-1"] }),
  120. ).toEqual({ deletedTaskIds: ["task-0", "task-1"], skippedTaskIds: [], failedTaskIds: [] });
  121. expect(rows.size).toBe(0);
  122. expect(onSourceError).toHaveBeenCalledWith(expect.anything(), "refresh", expect.any(Error));
  123. } finally {
  124. runtime.dispose();
  125. }
  126. });
  127. it("远端批次保留完整地址;离线与已被替换的 remoteSessionId 禁止写入", async () => {
  128. const workspacePath = "/isolated/probe";
  129. const workspaceIdentity = "remote:ssh:host:/isolated/probe";
  130. const { source } = createSource(2);
  131. let online = true;
  132. let remoteSessionId = "remote-1";
  133. const runtime = createWindowHostControllerRuntime({
  134. createId: () => "probe",
  135. resolveSource: () => ({
  136. scope: { kind: "remote", workspacePath, workspaceIdentity, remoteSessionId },
  137. sourceAvailability: online ? "online" : "offline",
  138. taskService: source as unknown as IZCodeTaskService,
  139. }),
  140. });
  141. try {
  142. const address = await runtime.resolveTaskAddress({
  143. workspacePath,
  144. workspaceIdentity,
  145. taskId: "task-0",
  146. });
  147. expect(await runtime.service.deleteArchivedTasks({ address, taskIds: ["task-0"] })).toEqual({
  148. deletedTaskIds: ["task-0"],
  149. skippedTaskIds: [],
  150. failedTaskIds: [],
  151. });
  152. expect(source.deleteArchivedTasks).toHaveBeenCalledExactlyOnceWith({
  153. workspacePath,
  154. workspaceIdentity,
  155. taskIds: ["task-0"],
  156. });
  157. online = false;
  158. await expect(
  159. runtime.service.deleteArchivedTasks({ address, taskIds: ["task-1"] }),
  160. ).rejects.toThrow("离线");
  161. online = true;
  162. remoteSessionId = "remote-2";
  163. await expect(
  164. runtime.service.deleteArchivedTasks({ address, taskIds: ["task-1"] }),
  165. ).rejects.toThrow();
  166. expect(source.deleteArchivedTasks).toHaveBeenCalledTimes(1);
  167. } finally {
  168. runtime.dispose();
  169. }
  170. });
  171. });