offPeakUiPresentation.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. formatOffPeakRemainingWait,
  4. resolveFailedOffPeakQueueFooter,
  5. resolveOffPeakCreateBlockReason,
  6. resolveLocalizedOffPeakCreateTitle,
  7. resolveOffPeakStatusFooter,
  8. shouldShowOffPeakModelSelectionIssue,
  9. } from "@/settings/offPeakUiPresentation.js";
  10. const remainingWaitMessages: Record<string, string> = {
  11. "offPeak.create.remaining.hoursMinutes": "{hours} 小时 {minutes} 分钟",
  12. "offPeak.create.remaining.hours": "{hours} 小时",
  13. "offPeak.create.remaining.minutes": "{minutes} 分钟",
  14. "offPeak.create.remaining.lessThanMinute": "不到 1 分钟",
  15. };
  16. const remainingWaitIntl = {
  17. formatMessage({ id }: { id: string }, values?: Record<string, string | number>) {
  18. let message = remainingWaitMessages[id] ?? id;
  19. for (const [key, value] of Object.entries(values ?? {})) {
  20. message = message.replaceAll(`{${key}}`, String(value));
  21. }
  22. return message;
  23. },
  24. };
  25. describe("resolveLocalizedOffPeakCreateTitle", () => {
  26. it("updates an untouched localized default when the locale changes", () => {
  27. expect(
  28. resolveLocalizedOffPeakCreateTitle({
  29. currentTitle: "未命名",
  30. hasInitialTitle: false,
  31. isEditing: false,
  32. nextDefaultTitle: "Untitled",
  33. previousDefaultTitle: "未命名",
  34. titleTouched: false,
  35. }),
  36. ).toBe("Untitled");
  37. });
  38. it.each([
  39. { hasInitialTitle: false, isEditing: false, titleTouched: true },
  40. { hasInitialTitle: true, isEditing: false, titleTouched: false },
  41. { hasInitialTitle: false, isEditing: true, titleTouched: false },
  42. ])("preserves user, template, and persisted titles: %o", (state) => {
  43. expect(
  44. resolveLocalizedOffPeakCreateTitle({
  45. currentTitle: "用户标题",
  46. nextDefaultTitle: "Untitled",
  47. previousDefaultTitle: "未命名",
  48. ...state,
  49. }),
  50. ).toBe("用户标题");
  51. });
  52. });
  53. describe("resolveOffPeakCreateBlockReason", () => {
  54. it.each([
  55. ["idle", undefined, "unavailable"],
  56. ["loading", undefined, "unavailable"],
  57. ["error", undefined, "unavailable"],
  58. ["ready", false, "quota"],
  59. ["ready", true, null],
  60. ] as const)(
  61. "fails closed for availability status %s / %s",
  62. (availabilityStatus, canTakeNumber, expected) => {
  63. expect(
  64. resolveOffPeakCreateBlockReason({
  65. availabilityStatus,
  66. canTakeNumber,
  67. grayEnabled: true,
  68. noPlan: false,
  69. }),
  70. ).toBe(expected);
  71. },
  72. );
  73. it("prioritizes ineligibility and ignores admission when the feature is hidden", () => {
  74. expect(
  75. resolveOffPeakCreateBlockReason({
  76. availabilityStatus: "ready",
  77. canTakeNumber: true,
  78. grayEnabled: true,
  79. noPlan: true,
  80. }),
  81. ).toBe("plan");
  82. expect(
  83. resolveOffPeakCreateBlockReason({
  84. availabilityStatus: "error",
  85. canTakeNumber: undefined,
  86. grayEnabled: true,
  87. noPlan: true,
  88. }),
  89. ).toBe("unavailable");
  90. expect(
  91. resolveOffPeakCreateBlockReason({
  92. availabilityStatus: "error",
  93. canTakeNumber: undefined,
  94. grayEnabled: false,
  95. noPlan: false,
  96. }),
  97. ).toBeNull();
  98. });
  99. });
  100. describe("shouldShowOffPeakModelSelectionIssue", () => {
  101. it.each([
  102. ["queued", true],
  103. ["paused", true],
  104. ["running", true],
  105. ["completed", false],
  106. ["failed", false],
  107. ["cancelled", false],
  108. ] as const)("只对仍可能执行的 %s 任务显示修复提示", (status, expected) => {
  109. expect(shouldShowOffPeakModelSelectionIssue(status)).toBe(expected);
  110. });
  111. });
  112. describe("formatOffPeakRemainingWait", () => {
  113. const now = 1_000_000;
  114. it.each([
  115. [125 * 60_000, "2 小时 5 分钟"],
  116. [120 * 60_000, "2 小时"],
  117. [42 * 60_000, "42 分钟"],
  118. ])("formats a %i ms wait with hour/minute granularity", (waitMs, expected) => {
  119. expect(formatOffPeakRemainingWait(now + waitMs, now, remainingWaitIntl)).toBe(expected);
  120. });
  121. it("rounds a positive partial minute up", () => {
  122. const label = formatOffPeakRemainingWait(now + 60_001, now, remainingWaitIntl);
  123. expect(label).toBe("2 分钟");
  124. });
  125. it("uses a sub-minute label once the recovery point is reached", () => {
  126. expect(formatOffPeakRemainingWait(now, now, remainingWaitIntl)).toBe("不到 1 分钟");
  127. });
  128. });
  129. describe("resolveOffPeakStatusFooter", () => {
  130. it("uses pause for paused tasks even when a queue position is present", () => {
  131. expect(resolveOffPeakStatusFooter({ status: "paused", queuePosition: 1 })).toMatchObject({
  132. icon: "pause",
  133. className: "text-idle-task",
  134. labelId: "offPeak.badge.pausedPosition",
  135. });
  136. });
  137. it("keeps moon for queued tasks and warning for failures", () => {
  138. expect(resolveOffPeakStatusFooter({ status: "queued", queuePosition: 2 }).icon).toBe("moon");
  139. expect(resolveOffPeakStatusFooter({ status: "failed" }).icon).toBe("warning");
  140. });
  141. });
  142. describe("resolveFailedOffPeakQueueFooter", () => {
  143. it("retains a failed task's queue position as a secondary status", () => {
  144. expect(resolveFailedOffPeakQueueFooter({ status: "failed", queuePosition: 212 })).toMatchObject(
  145. {
  146. icon: "moon",
  147. className: "text-idle-task",
  148. labelId: "offPeak.badge.queuePosition",
  149. labelValues: { position: "212" },
  150. },
  151. );
  152. });
  153. it("does not add a secondary status without both failure and a queue position", () => {
  154. expect(resolveFailedOffPeakQueueFooter({ status: "failed" })).toBeNull();
  155. expect(resolveFailedOffPeakQueueFooter({ status: "queued", queuePosition: 212 })).toBeNull();
  156. });
  157. });
  158. describe("off-peak terminal status presentation", () => {
  159. it("completed tasks resolve to the terminal status footer", () => {
  160. expect(resolveOffPeakStatusFooter({ status: "completed" })).toMatchObject({
  161. icon: "success",
  162. className: "text-foreground-subtle",
  163. labelId: "offPeak.status.completed",
  164. });
  165. });
  166. });