automationRestartEligibility.test.ts 6.3 KB

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