automationRestartEligibility.test.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { readFileSync } from "node:fs";
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. canRestartAutomation,
  5. canToggleAutomation,
  6. getAutomationActionErrorToastId,
  7. getAutomationCreateErrorToastId,
  8. getAutomationRunNowToastId,
  9. resolveAutomationDetailNavigation,
  10. resolveAutomationTabNavigation,
  11. resolveAutomationDetailTarget,
  12. resolveAutomationTemplateVisibility,
  13. resolveAutomationTabAfterOffPeakChange,
  14. resolveVisibleAutomationTabs,
  15. } from "@/settings/AutomationsSection.js";
  16. const automationsSectionSource = readFileSync(
  17. new URL("../src/settings/AutomationsSection.tsx", import.meta.url),
  18. "utf8",
  19. );
  20. const automationEditViewSource = readFileSync(
  21. new URL("../src/settings/AutomationEditView.tsx", import.meta.url),
  22. "utf8",
  23. );
  24. describe("canRestartAutomation", () => {
  25. it("只允许 failed 定时任务展示重启入口", () => {
  26. expect(canRestartAutomation({ lifecycleStatus: "failed" })).toBe(true);
  27. expect(canRestartAutomation({ lifecycleStatus: "completed" })).toBe(false);
  28. expect(canRestartAutomation({ lifecycleStatus: "active" })).toBe(false);
  29. expect(canRestartAutomation({ lifecycleStatus: "paused" })).toBe(false);
  30. });
  31. it("completed 和 failed 终态不展示暂停或恢复入口", () => {
  32. expect(canToggleAutomation({ lifecycleStatus: "active" })).toBe(true);
  33. expect(canToggleAutomation({ lifecycleStatus: "paused" })).toBe(true);
  34. expect(canToggleAutomation({ lifecycleStatus: "completed" })).toBe(false);
  35. expect(canToggleAutomation({ lifecycleStatus: "failed" })).toBe(false);
  36. });
  37. it("重复立即运行会映射到正在运行提示", () => {
  38. expect(getAutomationRunNowToastId("queued")).toBe("automations.runNowQueued");
  39. expect(getAutomationRunNowToastId("duplicate")).toBe(
  40. "automations.runNowAlreadyRunning",
  41. );
  42. expect(getAutomationRunNowToastId("failed")).toBe("automations.runNowFailed");
  43. });
  44. it("立即运行入口只跟随当前请求的瞬时 busy 状态", () => {
  45. const menuStart = automationsSectionSource.indexOf(
  46. "function AutomationActionsMenu",
  47. );
  48. const menuEnd = automationsSectionSource.indexOf(
  49. "export function AutomationsSection",
  50. menuStart,
  51. );
  52. const menuSource = automationsSectionSource.slice(menuStart, menuEnd);
  53. expect(menuStart).toBeGreaterThanOrEqual(0);
  54. expect(menuSource).toContain("disabled={busy}");
  55. expect(menuSource).not.toContain("runNowDisabled");
  56. expect(automationsSectionSource).not.toContain(
  57. "runNowBlockedAutomationIds",
  58. );
  59. expect(automationEditViewSource).not.toContain("runNowDisabled");
  60. });
  61. it("管理操作失败映射为产品文案,不直接展示 RPC 原文", () => {
  62. expect(getAutomationActionErrorToastId("create")).toBe("automations.error.create");
  63. expect(getAutomationActionErrorToastId("update")).toBe("automations.error.update");
  64. expect(getAutomationActionErrorToastId("toggle")).toBe("automations.error.toggle");
  65. expect(getAutomationActionErrorToastId("restart")).toBe("automations.error.restart");
  66. expect(getAutomationActionErrorToastId("delete")).toBe("automations.error.delete");
  67. expect(
  68. getAutomationCreateErrorToastId(
  69. "[AUTOMATION_CREATE_LIMIT_REACHED] automation limit reached",
  70. ),
  71. ).toBe("automations.error.createLimit");
  72. expect(getAutomationCreateErrorToastId("rpc unavailable")).toBe(
  73. "automations.error.create",
  74. );
  75. });
  76. it("会话创建卡片按 automationId 定位对应详情", () => {
  77. const target = { automationId: "automation-target" } as never;
  78. const other = { automationId: "automation-other" } as never;
  79. expect(resolveAutomationDetailTarget([other, target], " automation-target ")).toBe(target);
  80. expect(resolveAutomationDetailTarget([other], "automation-target")).toBeNull();
  81. expect(resolveAutomationDetailTarget([target], undefined)).toBeNull();
  82. });
  83. it("详情导航等待列表完成,并在目标不存在时进入可消费的失败终态", () => {
  84. const target = { automationId: "automation-target" } as never;
  85. expect(resolveAutomationDetailNavigation([], "automation-target", false)).toEqual({
  86. status: "pending",
  87. });
  88. expect(resolveAutomationDetailNavigation([target], "automation-target", true)).toEqual({
  89. status: "found",
  90. target,
  91. });
  92. expect(resolveAutomationDetailNavigation([], "automation-target", true)).toEqual({
  93. status: "missing",
  94. });
  95. });
  96. it("后台关闭闲时灰度后只保留 Scheduled tab", () => {
  97. expect(
  98. resolveVisibleAutomationTabs({
  99. hasAnyTasks: true,
  100. offPeakVisible: false,
  101. }),
  102. ).toEqual(["scheduled"]);
  103. });
  104. it("仅在闲时灰度可见或有闲时存量时展示 Idle tab", () => {
  105. expect(
  106. resolveVisibleAutomationTabs({
  107. hasAnyTasks: true,
  108. offPeakVisible: true,
  109. }),
  110. ).toEqual(["scheduled", "idle"]);
  111. expect(
  112. resolveVisibleAutomationTabs({
  113. hasAnyTasks: false,
  114. offPeakVisible: true,
  115. }),
  116. ).toEqual([]);
  117. });
  118. it("灰度关闭时将已选中的 Idle tab 收敛回 Scheduled", () => {
  119. expect(resolveAutomationTabAfterOffPeakChange("idle", false)).toBe("scheduled");
  120. expect(resolveAutomationTabAfterOffPeakChange("scheduled", false)).toBe("scheduled");
  121. expect(resolveAutomationTabAfterOffPeakChange("idle", true)).toBe("idle");
  122. });
  123. it("仅在 tabs 数据就绪且 Idle tab 可见时应用 OFFPEAK 导航", () => {
  124. expect(
  125. resolveAutomationTabNavigation({
  126. requestedTab: "idle",
  127. tabsReady: false,
  128. visibleTabs: [],
  129. }),
  130. ).toEqual({ status: "pending" });
  131. expect(
  132. resolveAutomationTabNavigation({
  133. requestedTab: "idle",
  134. tabsReady: true,
  135. visibleTabs: ["scheduled", "idle"],
  136. }),
  137. ).toEqual({ status: "settled", tab: "idle" });
  138. expect(
  139. resolveAutomationTabNavigation({
  140. requestedTab: "idle",
  141. tabsReady: true,
  142. visibleTabs: ["scheduled"],
  143. }),
  144. ).toEqual({ status: "settled", tab: "scheduled" });
  145. });
  146. it("无任务时同时展示可用的闲时模板和定时任务模板", () => {
  147. expect(
  148. resolveAutomationTemplateVisibility({
  149. hasAnyTasks: false,
  150. offPeakCreationEnabled: true,
  151. tab: "scheduled",
  152. }),
  153. ).toEqual({
  154. showOffPeakTemplates: true,
  155. showScheduledTemplates: true,
  156. });
  157. expect(
  158. resolveAutomationTemplateVisibility({
  159. hasAnyTasks: false,
  160. offPeakCreationEnabled: true,
  161. tab: "idle",
  162. }),
  163. ).toEqual({
  164. showOffPeakTemplates: true,
  165. showScheduledTemplates: true,
  166. });
  167. expect(
  168. resolveAutomationTemplateVisibility({
  169. hasAnyTasks: false,
  170. offPeakCreationEnabled: false,
  171. tab: "scheduled",
  172. }),
  173. ).toEqual({
  174. showOffPeakTemplates: false,
  175. showScheduledTemplates: true,
  176. });
  177. });
  178. it("有任务时继续按 Scheduled 和 Idle tab 分流模板", () => {
  179. expect(
  180. resolveAutomationTemplateVisibility({
  181. hasAnyTasks: true,
  182. offPeakCreationEnabled: true,
  183. tab: "scheduled",
  184. }),
  185. ).toEqual({
  186. showOffPeakTemplates: false,
  187. showScheduledTemplates: true,
  188. });
  189. expect(
  190. resolveAutomationTemplateVisibility({
  191. hasAnyTasks: true,
  192. offPeakCreationEnabled: true,
  193. tab: "idle",
  194. }),
  195. ).toEqual({
  196. showOffPeakTemplates: true,
  197. showScheduledTemplates: false,
  198. });
  199. });
  200. });