taskListOrdering.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { describe, expect, it } from "vitest";
  2. import type { ZCodeTaskMeta } from "@zcode/shared";
  3. import { compareZCodeTaskListItems } from "@/lib/taskListOrdering.js";
  4. import { attachTaskListRowActivity } from "@/v4/taskListRowActivity.js";
  5. function task(id: string, overrides: Partial<ZCodeTaskMeta> = {}): ZCodeTaskMeta {
  6. return {
  7. taskId: id,
  8. traceId: `trace-${id}`,
  9. title: id,
  10. workspacePath: "/ws",
  11. createdAt: 0,
  12. updatedAt: 0,
  13. mode: "default",
  14. ...overrides,
  15. };
  16. }
  17. function liveTask(
  18. id: string,
  19. phase: "prewarming" | "running" | "completedSuccess",
  20. overrides: Partial<ZCodeTaskMeta> = {},
  21. ): ZCodeTaskMeta {
  22. const meta = task(id, overrides);
  23. return attachTaskListRowActivity(meta, {
  24. phase,
  25. lastActivityAt: meta.updatedAt,
  26. hasBackgroundWork: false,
  27. });
  28. }
  29. function sortedIds(tasks: ZCodeTaskMeta[], sortBy: "created" | "updated"): string[] {
  30. return [...tasks]
  31. .sort((left, right) => compareZCodeTaskListItems(left, right, sortBy))
  32. .map((item) => item.taskId);
  33. }
  34. describe("taskListOrdering", () => {
  35. it("把 prewarming/running 放在非运行任务之前", () => {
  36. const tasks = [
  37. liveTask("completed-newest", "completedSuccess", {
  38. createdAt: 300,
  39. updatedAt: 1_000,
  40. }),
  41. liveTask("running", "running", { createdAt: 100, updatedAt: 1 }),
  42. liveTask("prewarming", "prewarming", { createdAt: 200, updatedAt: 2 }),
  43. ];
  44. expect(sortedIds(tasks, "updated")).toEqual(["prewarming", "running", "completed-newest"]);
  45. expect(sortedIds(tasks, "created")).toEqual(["prewarming", "running", "completed-newest"]);
  46. });
  47. it("并发 running 只按 createdAt 排序,updatedAt 交替时 fork 位置不抖动", () => {
  48. const before = [
  49. liveTask("parent", "running", { createdAt: 100, updatedAt: 900 }),
  50. liveTask("fork", "running", { createdAt: 200, updatedAt: 100 }),
  51. ];
  52. const after = [
  53. liveTask("parent", "running", { createdAt: 100, updatedAt: 100 }),
  54. liveTask("fork", "running", { createdAt: 200, updatedAt: 900 }),
  55. ];
  56. expect(sortedIds(before, "updated")).toEqual(["fork", "parent"]);
  57. expect(sortedIds(after, "updated")).toEqual(["fork", "parent"]);
  58. });
  59. it("running 创建时间相同时使用稳定 taskId 决胜,不读取 updatedAt", () => {
  60. const before = [
  61. liveTask("task-a", "running", { createdAt: 100, updatedAt: 900 }),
  62. liveTask("task-b", "running", { createdAt: 100, updatedAt: 100 }),
  63. ];
  64. const after = [
  65. liveTask("task-a", "running", { createdAt: 100, updatedAt: 100 }),
  66. liveTask("task-b", "running", { createdAt: 100, updatedAt: 900 }),
  67. ];
  68. expect(sortedIds(before, "updated")).toEqual(["task-b", "task-a"]);
  69. expect(sortedIds(after, "updated")).toEqual(["task-b", "task-a"]);
  70. });
  71. it("有后台工作(如 dwf run)的已收口会话进入运行层,updatedAt 交替时不换位", () => {
  72. // Bug 场景:两个会话各挂一个后台 workflow run,父会话 phase 已是 completedSuccess,
  73. // 但每条 run 进度事件都会推进 lastActivityAt;若只按 phase 判定运行层,两行会随事件互换。
  74. const backgrounded = (id: string, createdAt: number, updatedAt: number) =>
  75. attachTaskListRowActivity(task(id, { createdAt, updatedAt }), {
  76. phase: "completedSuccess",
  77. lastActivityAt: updatedAt,
  78. hasBackgroundWork: true,
  79. });
  80. const before = [
  81. backgrounded("run-a", 100, 900),
  82. backgrounded("run-b", 200, 100),
  83. liveTask("idle-newest", "completedSuccess", { createdAt: 300, updatedAt: 5_000 }),
  84. ];
  85. const after = [
  86. backgrounded("run-a", 100, 100),
  87. backgrounded("run-b", 200, 900),
  88. liveTask("idle-newest", "completedSuccess", { createdAt: 300, updatedAt: 5_000 }),
  89. ];
  90. expect(sortedIds(before, "updated")).toEqual(["run-b", "run-a", "idle-newest"]);
  91. expect(sortedIds(after, "updated")).toEqual(["run-b", "run-a", "idle-newest"]);
  92. expect(sortedIds(after, "created")).toEqual(["run-b", "run-a", "idle-newest"]);
  93. });
  94. it("后台工作结束后会话退出运行层,按 Updated 偏好落位", () => {
  95. const settled = attachTaskListRowActivity(task("settled", { createdAt: 100, updatedAt: 900 }), {
  96. phase: "completedSuccess",
  97. lastActivityAt: 900,
  98. hasBackgroundWork: false,
  99. });
  100. const running = liveTask("running", "running", { createdAt: 50, updatedAt: 1 });
  101. const older = liveTask("older", "completedSuccess", { createdAt: 300, updatedAt: 500 });
  102. expect(sortedIds([older, settled, running], "updated")).toEqual([
  103. "running",
  104. "settled",
  105. "older",
  106. ]);
  107. });
  108. it("非运行任务继续尊重 Created/Updated 偏好", () => {
  109. const tasks = [
  110. liveTask("newer-created", "completedSuccess", { createdAt: 20, updatedAt: 10 }),
  111. liveTask("newer-updated", "completedSuccess", { createdAt: 10, updatedAt: 20 }),
  112. ];
  113. expect(sortedIds(tasks, "created")).toEqual(["newer-created", "newer-updated"]);
  114. expect(sortedIds(tasks, "updated")).toEqual(["newer-updated", "newer-created"]);
  115. });
  116. it("不采信缺少 sessions-index activity 的历史 status=running", () => {
  117. const staleRunning = task("stale-running", {
  118. status: "running",
  119. createdAt: 10,
  120. updatedAt: 10,
  121. });
  122. const liveCompleted = liveTask("completed", "completedSuccess", {
  123. createdAt: 20,
  124. updatedAt: 20,
  125. });
  126. expect(sortedIds([staleRunning, liveCompleted], "updated")).toEqual([
  127. "completed",
  128. "stale-running",
  129. ]);
  130. });
  131. });