archivedTaskDeletion.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { IZCodeTaskService } from "@zcode/services";
  3. import {
  4. collectArchivedTaskDeletion,
  5. deleteArchivedTaskSelection,
  6. type ArchivedTaskDeletionWorkspace,
  7. } from "@/lib/archivedTaskDeletion.js";
  8. function workspace(identity?: string, count = 25): ArchivedTaskDeletionWorkspace {
  9. return {
  10. workspacePath: "/repo",
  11. workspaceIdentity: identity,
  12. label: identity ?? "local",
  13. service: {
  14. listArchivedTasks: vi.fn(async () =>
  15. Array.from({ length: count }, (_, index) => ({
  16. taskId: `task-${index}`,
  17. workspacePath: "/repo",
  18. workspaceIdentity: identity,
  19. })),
  20. ) as unknown as IZCodeTaskService["listArchivedTasks"],
  21. deleteArchivedTasks: vi.fn(async ({ taskIds }: { taskIds: string[] }) => ({
  22. deletedTaskIds: taskIds,
  23. skippedTaskIds: [],
  24. failedTaskIds: [],
  25. })),
  26. },
  27. };
  28. }
  29. describe("归档任务批量删除", () => {
  30. it("完整收集跨 workspace 目标,重复 workspace 去重,超过 20 条也不截断;收集不写入", async () => {
  31. const local = workspace();
  32. const remote = workspace("remote:ssh:host:/repo", 2);
  33. const selection = await collectArchivedTaskDeletion([local, local, remote]);
  34. expect(selection.count).toBe(27);
  35. expect(local.service!.listArchivedTasks).toHaveBeenCalledTimes(1);
  36. expect(local.service!.deleteArchivedTasks).not.toHaveBeenCalled();
  37. expect(remote.service!.listArchivedTasks).toHaveBeenCalledWith({
  38. workspacePath: "/repo",
  39. workspaceIdentity: remote.workspaceIdentity,
  40. });
  41. });
  42. it("不连接不可访问项目,单 source 查询失败不阻断其它 source", async () => {
  43. const failed = workspace("remote:ssh:failed:/repo");
  44. vi.mocked(failed.service!.listArchivedTasks).mockRejectedValue(new Error("offline"));
  45. const offline = { ...workspace("remote:ssh:offline:/repo"), service: undefined };
  46. const selection = await collectArchivedTaskDeletion([workspace(), failed, offline]);
  47. expect(selection.count).toBe(25);
  48. expect(selection.unavailableWorkspaces).toEqual([failed.label, offline.label]);
  49. });
  50. it("只提交确认时的集合;跳过项和失败项不移除,后续项继续处理", async () => {
  51. const local = workspace(undefined, 4);
  52. const selection = await collectArchivedTaskDeletion([local]);
  53. vi.mocked(local.service!.listArchivedTasks).mockResolvedValue([]);
  54. vi.mocked(local.service!.deleteArchivedTasks).mockResolvedValueOnce({
  55. deletedTaskIds: ["task-0", "task-3"],
  56. skippedTaskIds: ["task-1"],
  57. failedTaskIds: ["task-2"],
  58. });
  59. const onDeleted = vi.fn();
  60. const result = await deleteArchivedTaskSelection(selection, onDeleted);
  61. expect(result).toMatchObject({ deleted: 2, skipped: 1, failed: 1 });
  62. expect(onDeleted.mock.calls.map(([target]) => target.taskId)).toEqual(["task-0", "task-3"]);
  63. expect(local.service!.listArchivedTasks).toHaveBeenCalledTimes(1);
  64. expect(local.service!.deleteArchivedTasks).toHaveBeenCalledExactlyOnceWith({
  65. workspacePath: "/repo",
  66. workspaceIdentity: undefined,
  67. taskIds: ["task-0", "task-1", "task-2", "task-3"],
  68. });
  69. });
  70. it("每个 workspace 只提交一次批次;一个 source 失败不阻断同路径其它 identity", async () => {
  71. const local = workspace(undefined, 72);
  72. const remote = workspace("remote:ssh:host:/repo", 8);
  73. vi.mocked(remote.service!.deleteArchivedTasks).mockRejectedValueOnce(new Error("offline"));
  74. const selection = await collectArchivedTaskDeletion([local, remote]);
  75. const onDeleted = vi.fn();
  76. expect(await deleteArchivedTaskSelection(selection, onDeleted)).toMatchObject({
  77. deleted: 72,
  78. skipped: 0,
  79. failed: 8,
  80. });
  81. expect(onDeleted).toHaveBeenCalledTimes(72);
  82. expect(local.service!.deleteArchivedTasks).toHaveBeenCalledTimes(1);
  83. expect(remote.service!.deleteArchivedTasks).toHaveBeenCalledExactlyOnceWith({
  84. workspacePath: "/repo",
  85. workspaceIdentity: remote.workspaceIdentity,
  86. taskIds: Array.from({ length: 8 }, (_, i) => `task-${i}`),
  87. });
  88. });
  89. it("空 workspace 不发送删除请求", async () => {
  90. const empty = workspace(undefined, 0);
  91. const selection = await collectArchivedTaskDeletion([empty]);
  92. expect(await deleteArchivedTaskSelection(selection, vi.fn())).toMatchObject({
  93. deleted: 0,
  94. skipped: 0,
  95. failed: 0,
  96. });
  97. expect(empty.service!.deleteArchivedTasks).not.toHaveBeenCalled();
  98. });
  99. });