time-utils.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  2. // Mock resolveSystemTimezone before importing time-utils
  3. vi.mock("@/lib/utils/timezone", () => ({
  4. resolveSystemTimezone: vi.fn(async () => "Asia/Shanghai"),
  5. }));
  6. import { resolveSystemTimezone } from "@/lib/utils/timezone";
  7. import {
  8. getDailyResetTime,
  9. getResetInfo,
  10. getResetInfoWithMode,
  11. getSecondsUntilMidnight,
  12. getTimeRangeForPeriod,
  13. getTimeRangeForPeriodWithMode,
  14. getTTLForPeriod,
  15. getTTLForPeriodWithMode,
  16. normalizeResetTime,
  17. } from "@/lib/rate-limit/time-utils";
  18. describe("rate-limit time-utils", () => {
  19. const nowMs = 1_700_000_000_000;
  20. beforeEach(() => {
  21. vi.useFakeTimers();
  22. vi.setSystemTime(new Date(nowMs));
  23. });
  24. afterEach(() => {
  25. vi.useRealTimers();
  26. });
  27. it("normalizeResetTime: illegal time should fallback to safe default", () => {
  28. expect(normalizeResetTime("abc")).toBe("00:00");
  29. expect(normalizeResetTime("99:10")).toBe("00:10");
  30. expect(normalizeResetTime("12:70")).toBe("12:00");
  31. });
  32. it("getTimeRangeForPeriodWithMode: daily rolling should return past 24h window", async () => {
  33. const { startTime, endTime } = await getTimeRangeForPeriodWithMode("daily", "00:00", "rolling");
  34. expect(endTime.getTime()).toBe(nowMs);
  35. expect(startTime.getTime()).toBe(nowMs - 24 * 60 * 60 * 1000);
  36. });
  37. it("getResetInfoWithMode: daily rolling should return rolling semantics", async () => {
  38. const info = await getResetInfoWithMode("daily", "00:00", "rolling");
  39. expect(info.type).toBe("rolling");
  40. expect(info.period).toBe("24 小时");
  41. });
  42. it("getTTLForPeriodWithMode: daily rolling TTL should be 24h", async () => {
  43. expect(await getTTLForPeriodWithMode("daily", "00:00", "rolling")).toBe(24 * 3600);
  44. });
  45. it("getTTLForPeriod: 5h TTL should be 5h", async () => {
  46. expect(await getTTLForPeriod("5h")).toBe(5 * 3600);
  47. });
  48. it("getSecondsUntilMidnight/getDailyResetTime: should compute reasonable daily reset time", async () => {
  49. const seconds = await getSecondsUntilMidnight();
  50. expect(seconds).toBeGreaterThan(0);
  51. expect(seconds).toBeLessThanOrEqual(24 * 3600);
  52. const resetAt = await getDailyResetTime();
  53. expect(resetAt.getTime()).toBeGreaterThan(nowMs);
  54. });
  55. });
  56. /**
  57. * Timezone Consistency Tests
  58. *
  59. * Verify that all time calculations use resolveSystemTimezone() consistently
  60. * and produce correct results across different timezone configurations.
  61. */
  62. describe("timezone consistency", () => {
  63. beforeEach(() => {
  64. vi.useFakeTimers();
  65. vi.clearAllMocks();
  66. });
  67. afterEach(() => {
  68. vi.useRealTimers();
  69. });
  70. it("should use timezone from resolveSystemTimezone for daily fixed calculations", async () => {
  71. // Set time to 2024-01-15 02:00:00 UTC
  72. // In Asia/Shanghai (+8), this is 2024-01-15 10:00:00
  73. const utcTime = new Date("2024-01-15T02:00:00.000Z");
  74. vi.setSystemTime(utcTime);
  75. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  76. // Reset time 08:00 Shanghai = 00:00 UTC
  77. // At Shanghai 10:00, we've passed 08:00, so window starts at today's 08:00 Shanghai = 00:00 UTC
  78. const { startTime } = await getTimeRangeForPeriod("daily", "08:00");
  79. // Verify resolveSystemTimezone was called
  80. expect(resolveSystemTimezone).toHaveBeenCalled();
  81. // Start should be 2024-01-15 00:00:00 UTC (08:00 Shanghai)
  82. expect(startTime.toISOString()).toBe("2024-01-15T00:00:00.000Z");
  83. });
  84. it("should calculate daily fixed window correctly for Asia/Shanghai", async () => {
  85. // 2024-01-15 00:00:00 UTC = 2024-01-15 08:00:00 Shanghai
  86. // Reset at 08:00 Shanghai, we're exactly at reset time
  87. const utcTime = new Date("2024-01-15T00:00:00.000Z");
  88. vi.setSystemTime(utcTime);
  89. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  90. const { startTime, endTime } = await getTimeRangeForPeriod("daily", "08:00");
  91. // At exactly 08:00 Shanghai, window starts at 08:00 Shanghai today = 00:00 UTC
  92. expect(startTime.toISOString()).toBe("2024-01-15T00:00:00.000Z");
  93. expect(endTime.toISOString()).toBe("2024-01-15T00:00:00.000Z");
  94. });
  95. it("should calculate daily fixed window correctly before reset time", async () => {
  96. // 2024-01-14 23:00:00 UTC = 2024-01-15 07:00:00 Shanghai
  97. // Reset at 08:00 Shanghai, we haven't reached it yet
  98. const utcTime = new Date("2024-01-14T23:00:00.000Z");
  99. vi.setSystemTime(utcTime);
  100. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  101. const { startTime } = await getTimeRangeForPeriod("daily", "08:00");
  102. // Before 08:00 Shanghai, window starts at yesterday's 08:00 Shanghai = 2024-01-14 00:00 UTC
  103. expect(startTime.toISOString()).toBe("2024-01-14T00:00:00.000Z");
  104. });
  105. it("should calculate daily fixed window correctly for America/New_York", async () => {
  106. // 2024-01-15 14:00:00 UTC = 2024-01-15 09:00:00 New York (EST, -5)
  107. // Reset at 08:00 New York, we've passed it
  108. const utcTime = new Date("2024-01-15T14:00:00.000Z");
  109. vi.setSystemTime(utcTime);
  110. vi.mocked(resolveSystemTimezone).mockResolvedValue("America/New_York");
  111. const { startTime } = await getTimeRangeForPeriod("daily", "08:00");
  112. // 08:00 New York = 13:00 UTC
  113. expect(startTime.toISOString()).toBe("2024-01-15T13:00:00.000Z");
  114. });
  115. it("should calculate weekly window start in configured timezone", async () => {
  116. // 2024-01-17 00:00:00 UTC = Wednesday
  117. // In Asia/Shanghai (+8), this is 2024-01-17 08:00:00 (still Wednesday)
  118. // Week starts Monday 00:00 Shanghai = 2024-01-15 00:00 Shanghai = 2024-01-14 16:00 UTC
  119. const utcTime = new Date("2024-01-17T00:00:00.000Z");
  120. vi.setSystemTime(utcTime);
  121. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  122. const { startTime } = await getTimeRangeForPeriod("weekly");
  123. // Monday 00:00 Shanghai = Sunday 16:00 UTC
  124. expect(startTime.toISOString()).toBe("2024-01-14T16:00:00.000Z");
  125. });
  126. it("should calculate monthly window start in configured timezone", async () => {
  127. // 2024-01-15 00:00:00 UTC = 2024-01-15 08:00:00 Shanghai
  128. // Month starts Jan 1 00:00 Shanghai = Dec 31 16:00 UTC
  129. const utcTime = new Date("2024-01-15T00:00:00.000Z");
  130. vi.setSystemTime(utcTime);
  131. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  132. const { startTime } = await getTimeRangeForPeriod("monthly");
  133. // Jan 1 00:00 Shanghai = Dec 31 16:00 UTC
  134. expect(startTime.toISOString()).toBe("2023-12-31T16:00:00.000Z");
  135. });
  136. it("should handle day boundary crossing between UTC and local TZ", async () => {
  137. // Edge case: 2024-01-15 23:30:00 UTC = 2024-01-16 07:30:00 Shanghai
  138. // Reset at 08:00 Shanghai - we're in Shanghai's "tomorrow" but before reset
  139. const utcTime = new Date("2024-01-15T23:30:00.000Z");
  140. vi.setSystemTime(utcTime);
  141. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  142. const { startTime } = await getTimeRangeForPeriod("daily", "08:00");
  143. // In Shanghai it's Jan 16 07:30, before 08:00 reset
  144. // So window starts at Jan 15 08:00 Shanghai = Jan 15 00:00 UTC
  145. expect(startTime.toISOString()).toBe("2024-01-15T00:00:00.000Z");
  146. });
  147. it("should use rolling mode regardless of timezone for daily rolling", async () => {
  148. const utcTime = new Date("2024-01-15T12:00:00.000Z");
  149. vi.setSystemTime(utcTime);
  150. vi.mocked(resolveSystemTimezone).mockResolvedValue("America/New_York");
  151. const { startTime, endTime } = await getTimeRangeForPeriodWithMode("daily", "08:00", "rolling");
  152. // Rolling mode: always 24 hours back, timezone doesn't matter
  153. expect(endTime.toISOString()).toBe("2024-01-15T12:00:00.000Z");
  154. expect(startTime.toISOString()).toBe("2024-01-14T12:00:00.000Z");
  155. });
  156. it("should use 5h rolling window regardless of timezone", async () => {
  157. const utcTime = new Date("2024-01-15T12:00:00.000Z");
  158. vi.setSystemTime(utcTime);
  159. vi.mocked(resolveSystemTimezone).mockResolvedValue("Europe/London");
  160. const { startTime, endTime } = await getTimeRangeForPeriod("5h");
  161. // 5h rolling: always 5 hours back
  162. expect(endTime.toISOString()).toBe("2024-01-15T12:00:00.000Z");
  163. expect(startTime.toISOString()).toBe("2024-01-15T07:00:00.000Z");
  164. });
  165. });
  166. /**
  167. * TTL Calculation Timezone Tests
  168. *
  169. * Verify that TTL calculations use server timezone consistently
  170. */
  171. describe("TTL timezone consistency", () => {
  172. beforeEach(() => {
  173. vi.useFakeTimers();
  174. vi.clearAllMocks();
  175. });
  176. afterEach(() => {
  177. vi.useRealTimers();
  178. });
  179. it("should calculate daily fixed TTL based on configured timezone", async () => {
  180. // 2024-01-15 02:00:00 UTC = 2024-01-15 10:00:00 Shanghai
  181. // Reset at 08:00 Shanghai, next reset is tomorrow 08:00 Shanghai = 2024-01-16 00:00 UTC
  182. // TTL = 22 hours = 79200 seconds
  183. const utcTime = new Date("2024-01-15T02:00:00.000Z");
  184. vi.setSystemTime(utcTime);
  185. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  186. const ttl = await getTTLForPeriod("daily", "08:00");
  187. // From 10:00 Shanghai to next 08:00 Shanghai = 22 hours
  188. expect(ttl).toBe(22 * 3600);
  189. });
  190. it("should calculate daily fixed TTL correctly when close to reset time", async () => {
  191. // 2024-01-14 23:30:00 UTC = 2024-01-15 07:30:00 Shanghai
  192. // Reset at 08:00 Shanghai, next reset is in 30 minutes
  193. const utcTime = new Date("2024-01-14T23:30:00.000Z");
  194. vi.setSystemTime(utcTime);
  195. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  196. const ttl = await getTTLForPeriod("daily", "08:00");
  197. // 30 minutes = 1800 seconds
  198. expect(ttl).toBe(30 * 60);
  199. });
  200. it("should calculate weekly TTL based on configured timezone", async () => {
  201. // 2024-01-17 00:00:00 UTC = Wednesday 08:00 Shanghai
  202. // Next Monday 00:00 Shanghai = 2024-01-22 00:00 Shanghai = 2024-01-21 16:00 UTC
  203. const utcTime = new Date("2024-01-17T00:00:00.000Z");
  204. vi.setSystemTime(utcTime);
  205. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  206. const ttl = await getTTLForPeriod("weekly");
  207. // From Wed 08:00 to Mon 00:00 = 4 days + 16 hours = 112 hours
  208. expect(ttl).toBe(112 * 3600);
  209. });
  210. it("should calculate monthly TTL based on configured timezone", async () => {
  211. // 2024-01-30 00:00:00 UTC = 2024-01-30 08:00:00 Shanghai
  212. // Next month Feb 1 00:00 Shanghai = 2024-01-31 16:00 UTC
  213. const utcTime = new Date("2024-01-30T00:00:00.000Z");
  214. vi.setSystemTime(utcTime);
  215. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  216. const ttl = await getTTLForPeriod("monthly");
  217. // From Jan 30 08:00 to Feb 1 00:00 Shanghai = 1 day + 16 hours = 40 hours
  218. expect(ttl).toBe(40 * 3600);
  219. });
  220. it("should return 24h TTL for daily rolling regardless of timezone", async () => {
  221. vi.mocked(resolveSystemTimezone).mockResolvedValue("Pacific/Auckland");
  222. const ttl = await getTTLForPeriodWithMode("daily", "08:00", "rolling");
  223. expect(ttl).toBe(24 * 3600);
  224. });
  225. it("should return 5h TTL for 5h period regardless of timezone", async () => {
  226. vi.mocked(resolveSystemTimezone).mockResolvedValue("America/Los_Angeles");
  227. const ttl = await getTTLForPeriod("5h");
  228. expect(ttl).toBe(5 * 3600);
  229. });
  230. });
  231. /**
  232. * ResetInfo Timezone Tests
  233. *
  234. * Verify that reset info calculations use server timezone consistently
  235. */
  236. describe("ResetInfo timezone consistency", () => {
  237. beforeEach(() => {
  238. vi.useFakeTimers();
  239. vi.clearAllMocks();
  240. });
  241. afterEach(() => {
  242. vi.useRealTimers();
  243. });
  244. it("should return next reset time in configured timezone for daily", async () => {
  245. // 2024-01-15 02:00:00 UTC = 2024-01-15 10:00:00 Shanghai
  246. // Next reset at 08:00 Shanghai = 2024-01-16 00:00:00 UTC
  247. const utcTime = new Date("2024-01-15T02:00:00.000Z");
  248. vi.setSystemTime(utcTime);
  249. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  250. const info = await getResetInfo("daily", "08:00");
  251. expect(info.type).toBe("custom");
  252. expect(info.resetAt?.toISOString()).toBe("2024-01-16T00:00:00.000Z");
  253. });
  254. it("should return next Monday for weekly in configured timezone", async () => {
  255. // 2024-01-17 00:00:00 UTC = Wednesday 08:00 Shanghai
  256. // Next Monday 00:00 Shanghai = 2024-01-21 16:00 UTC
  257. const utcTime = new Date("2024-01-17T00:00:00.000Z");
  258. vi.setSystemTime(utcTime);
  259. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  260. const info = await getResetInfo("weekly");
  261. expect(info.type).toBe("natural");
  262. expect(info.resetAt?.toISOString()).toBe("2024-01-21T16:00:00.000Z");
  263. });
  264. it("should return next month start for monthly in configured timezone", async () => {
  265. // 2024-01-15 00:00:00 UTC = 2024-01-15 08:00 Shanghai
  266. // Feb 1 00:00 Shanghai = 2024-01-31 16:00 UTC
  267. const utcTime = new Date("2024-01-15T00:00:00.000Z");
  268. vi.setSystemTime(utcTime);
  269. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  270. const info = await getResetInfo("monthly");
  271. expect(info.type).toBe("natural");
  272. expect(info.resetAt?.toISOString()).toBe("2024-01-31T16:00:00.000Z");
  273. });
  274. it("should return rolling type for 5h period", async () => {
  275. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  276. const info = await getResetInfo("5h");
  277. expect(info.type).toBe("rolling");
  278. expect(info.period).toBe("5 小时");
  279. expect(info.resetAt).toBeUndefined();
  280. });
  281. it("should return rolling type for daily rolling mode", async () => {
  282. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  283. const info = await getResetInfoWithMode("daily", "08:00", "rolling");
  284. expect(info.type).toBe("rolling");
  285. expect(info.period).toBe("24 小时");
  286. });
  287. });
  288. /**
  289. * Edge Cases and Boundary Tests
  290. */
  291. describe("timezone edge cases", () => {
  292. beforeEach(() => {
  293. vi.useFakeTimers();
  294. vi.clearAllMocks();
  295. });
  296. afterEach(() => {
  297. vi.useRealTimers();
  298. });
  299. it("should handle midnight reset time (00:00)", async () => {
  300. // 2024-01-15 18:00:00 UTC = 2024-01-16 02:00:00 Shanghai
  301. // Reset at 00:00 Shanghai, window starts at 2024-01-16 00:00 Shanghai = 2024-01-15 16:00 UTC
  302. const utcTime = new Date("2024-01-15T18:00:00.000Z");
  303. vi.setSystemTime(utcTime);
  304. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  305. const { startTime } = await getTimeRangeForPeriod("daily", "00:00");
  306. expect(startTime.toISOString()).toBe("2024-01-15T16:00:00.000Z");
  307. });
  308. it("should handle late night reset time (23:59)", async () => {
  309. // 2024-01-15 16:30:00 UTC = 2024-01-16 00:30:00 Shanghai
  310. // Reset at 23:59 Shanghai, we're past it (just after midnight)
  311. // Window starts at 2024-01-15 23:59 Shanghai = 2024-01-15 15:59 UTC
  312. const utcTime = new Date("2024-01-15T16:30:00.000Z");
  313. vi.setSystemTime(utcTime);
  314. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  315. const { startTime } = await getTimeRangeForPeriod("daily", "23:59");
  316. expect(startTime.toISOString()).toBe("2024-01-15T15:59:00.000Z");
  317. });
  318. it("should handle year boundary for monthly window", async () => {
  319. // 2024-01-05 00:00:00 UTC = 2024-01-05 08:00:00 Shanghai
  320. // Month starts Jan 1 00:00 Shanghai = 2023-12-31 16:00 UTC
  321. const utcTime = new Date("2024-01-05T00:00:00.000Z");
  322. vi.setSystemTime(utcTime);
  323. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  324. const { startTime } = await getTimeRangeForPeriod("monthly");
  325. expect(startTime.toISOString()).toBe("2023-12-31T16:00:00.000Z");
  326. });
  327. it("should handle week boundary crossing year", async () => {
  328. // 2024-01-03 00:00:00 UTC = Wednesday = 2024-01-03 08:00 Shanghai
  329. // Week started Monday 2024-01-01 00:00 Shanghai = 2023-12-31 16:00 UTC
  330. const utcTime = new Date("2024-01-03T00:00:00.000Z");
  331. vi.setSystemTime(utcTime);
  332. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  333. const { startTime } = await getTimeRangeForPeriod("weekly");
  334. expect(startTime.toISOString()).toBe("2023-12-31T16:00:00.000Z");
  335. });
  336. it("should handle negative UTC offset timezone (America/New_York)", async () => {
  337. // 2024-01-15 03:00:00 UTC = 2024-01-14 22:00:00 New York (EST -5)
  338. // Reset at 08:00 New York, we're before it (still previous day in NY)
  339. // Window starts at 2024-01-14 08:00 NY = 2024-01-14 13:00 UTC
  340. const utcTime = new Date("2024-01-15T03:00:00.000Z");
  341. vi.setSystemTime(utcTime);
  342. vi.mocked(resolveSystemTimezone).mockResolvedValue("America/New_York");
  343. const { startTime } = await getTimeRangeForPeriod("daily", "08:00");
  344. expect(startTime.toISOString()).toBe("2024-01-14T13:00:00.000Z");
  345. });
  346. it("should handle UTC timezone", async () => {
  347. // 2024-01-15 10:00:00 UTC
  348. // Reset at 08:00 UTC, we've passed it
  349. // Window starts at 2024-01-15 08:00 UTC
  350. const utcTime = new Date("2024-01-15T10:00:00.000Z");
  351. vi.setSystemTime(utcTime);
  352. vi.mocked(resolveSystemTimezone).mockResolvedValue("UTC");
  353. const { startTime } = await getTimeRangeForPeriod("daily", "08:00");
  354. expect(startTime.toISOString()).toBe("2024-01-15T08:00:00.000Z");
  355. });
  356. it("should handle large positive UTC offset (Pacific/Auckland +13)", async () => {
  357. // 2024-01-15 10:00:00 UTC = 2024-01-15 23:00:00 Auckland
  358. // Reset at 08:00 Auckland, we've passed it
  359. // Window starts at 2024-01-15 08:00 Auckland = 2024-01-14 19:00 UTC
  360. const utcTime = new Date("2024-01-15T10:00:00.000Z");
  361. vi.setSystemTime(utcTime);
  362. vi.mocked(resolveSystemTimezone).mockResolvedValue("Pacific/Auckland");
  363. const { startTime } = await getTimeRangeForPeriod("daily", "08:00");
  364. expect(startTime.toISOString()).toBe("2024-01-14T19:00:00.000Z");
  365. });
  366. it("should calculate correct TTL at exact reset moment", async () => {
  367. // 2024-01-15 00:00:00 UTC = 2024-01-15 08:00:00 Shanghai (exactly at reset)
  368. // Next reset is 2024-01-16 08:00 Shanghai = 2024-01-16 00:00 UTC
  369. // TTL = 24 hours
  370. const utcTime = new Date("2024-01-15T00:00:00.000Z");
  371. vi.setSystemTime(utcTime);
  372. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  373. const ttl = await getTTLForPeriod("daily", "08:00");
  374. expect(ttl).toBe(24 * 3600);
  375. });
  376. it("should handle different reset times consistently", async () => {
  377. // Test multiple reset times to ensure consistency
  378. const utcTime = new Date("2024-01-15T12:00:00.000Z"); // 20:00 Shanghai
  379. vi.setSystemTime(utcTime);
  380. vi.mocked(resolveSystemTimezone).mockResolvedValue("Asia/Shanghai");
  381. // 06:00 Shanghai = passed, window starts today 06:00 = 2024-01-14 22:00 UTC
  382. const range06 = await getTimeRangeForPeriod("daily", "06:00");
  383. expect(range06.startTime.toISOString()).toBe("2024-01-14T22:00:00.000Z");
  384. // 18:00 Shanghai = passed, window starts today 18:00 = 2024-01-15 10:00 UTC
  385. const range18 = await getTimeRangeForPeriod("daily", "18:00");
  386. expect(range18.startTime.toISOString()).toBe("2024-01-15T10:00:00.000Z");
  387. // 21:00 Shanghai = not yet, window starts yesterday 21:00 = 2024-01-14 13:00 UTC
  388. const range21 = await getTimeRangeForPeriod("daily", "21:00");
  389. expect(range21.startTime.toISOString()).toBe("2024-01-14T13:00:00.000Z");
  390. });
  391. });