index.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { getLunarFestivals } from "../../src";
  2. import dayjs from "../../src/utils/dayjs";
  3. describe("lunarFestivals", () => {
  4. test("getLunarFestivals should return fixed lunar festivals", () => {
  5. // 测试常规固定节日
  6. const result = getLunarFestivals("2025-01-29");
  7. expect(result).toEqual([
  8. {
  9. date: "2025-01-29",
  10. name: ["春节", "鸡日", "元始天尊诞辰"],
  11. }
  12. ]);
  13. });
  14. test("should handle solar term related festivals", () => {
  15. // 测试寒食节(清明前一日)
  16. const result = getLunarFestivals("2025-04-03");
  17. expect(result).toEqual([{
  18. date: "2025-04-03",
  19. name: ["寒食节"],
  20. }]);
  21. });
  22. test("should handle special festivals", () => {
  23. // 测试除夕(农历腊月最后一日)
  24. const result = getLunarFestivals("2025-01-28");
  25. expect(result).toEqual([{
  26. date: "2025-01-28",
  27. name: ["除夕", "封井", "祭井神", "贴春联", "迎财神"],
  28. }]);
  29. });
  30. test("should filter leap month festivals", () => {
  31. const result1 = getLunarFestivals("2025-06-30")
  32. expect(result1).toEqual([{
  33. date: "2025-06-30",
  34. name: ["晒衣节"],
  35. }]);
  36. // 测试闰月不返回节日
  37. const result2 = getLunarFestivals("2025-07-30") // 2025年有闰六月。2025-07-30 是闰六月初五。
  38. expect(result2).toEqual([]); // 闰六月初五没有节日
  39. });
  40. describe("Specific Special Festival Handlers (Based on current constants.ts)", () => { // (基于当前 constants.ts 的特定节日处理器)
  41. test("Mother's Day (solar 2024-05-12) should NOT be identified as handler is missing, and no fixed festival on actual L04-05", () => { // 母亲节 (公历 2024-05-12) 因处理器缺失不应被识别,且实际农历四月初五无固定节日
  42. // 公历 "2024-05-12" 通过 getLunarDate 转换为农历 L2024-04-05。LUNAR_FESTIVAL_MAP[4][5] 未定义。
  43. expect(getLunarFestivals("2024-05-12", "2024-05-12")).toEqual([]);
  44. // 公历 "2025-05-11" 通过 getLunarDate 转换为农历 L2025-04-14。LUNAR_FESTIVAL_MAP[4][14] 是 ["吕洞宾诞辰"]。
  45. expect(getLunarFestivals("2025-05-11")).toEqual([{ date: "2025-05-11", name: ["吕洞宾诞辰"] }]);
  46. });
  47. test("Father's Day (solar 2024-06-16) should NOT be identified as handler is missing, and no fixed festival on actual lunar date", () => { // 父亲节 (公历 2024-06-16) 因处理器缺失不应被识别,且实际农历日期无固定节日
  48. // 公历 "2024-06-16" 是农历 L2024-05-11。LUNAR_FESTIVAL_MAP[5][11] 未定义。
  49. expect(getLunarFestivals("2024-06-16", "2024-06-16")).toEqual([]);
  50. // 公历 "2025-06-15" 是农历 L2025-05-20。LUNAR_FESTIVAL_MAP[5][20] 未定义。
  51. expect(getLunarFestivals("2025-06-15")).toEqual([]);
  52. });
  53. test("Thanksgiving Day (solar 2024-11-28) should NOT be identified as handler is missing, and no fixed festival on actual lunar date", () => { // 感恩节 (公历 2024-11-28) 因处理器缺失不应被识别,且实际农历日期无固定节日
  54. // 公历 "2024-11-28" 是农历 L2024-10-28。LUNAR_FESTIVAL_MAP[10][28] 未定义。
  55. expect(getLunarFestivals("2024-11-28", "2024-11-28")).toEqual([]);
  56. // 公历 "2025-11-27" 是农历 L2025-10-07。LUNAR_FESTIVAL_MAP[10][7] 未定义。
  57. expect(getLunarFestivals("2025-11-27")).toEqual([]);
  58. });
  59. });
  60. describe("Edge Cases and Other Scenarios", () => { // 边缘情况及其他场景
  61. test("should return empty array for a range with no festivals", () => {
  62. expect(getLunarFestivals("2024-01-02", "2024-01-03")).toEqual([]);
  63. });
  64. test("should return empty array if start date is after end date", () => {
  65. expect(getLunarFestivals("2024-01-10", "2024-01-01")).toEqual([]);
  66. });
  67. test("should handle date with multiple fixed festivals not overlapping with special ones", () => {
  68. // 例如:七月初七 (乞巧节)
  69. // 2024-08-10 是农历2024年七月初七
  70. const result = getLunarFestivals("2024-08-10");
  71. // 根据 constants.ts, LUNAR_FESTIVAL_MAP[7][7] 是 ["乞巧节"]
  72. expect(result).toEqual([
  73. { date: "2024-08-10", name: ["乞巧节"] }
  74. ]);
  75. });
  76. test("should handle a long range with multiple festivals", () => {
  77. // 大约2个月的范围
  78. const results = getLunarFestivals("2024-08-01", "2024-09-30");
  79. // 检查是否包含一些关键节日,而不是完整的列表(可能很长)
  80. const festivalDates = results.map(r => r.date);
  81. expect(festivalDates).toContain("2024-08-10"); // 乞巧节 (农历7/7)
  82. expect(festivalDates).toContain("2024-08-18"); // 中元节 (农历7/15)
  83. expect(festivalDates).toContain("2024-09-17"); // 中秋节 (农历8/15)
  84. const qixi = results.find(r => r.date === "2024-08-10");
  85. expect(qixi?.name).toEqual(expect.arrayContaining(["乞巧节"])); // 已修正名称
  86. const zhongqiu = results.find(r => r.date === "2024-09-17");
  87. expect(zhongqiu?.name).toEqual(expect.arrayContaining(["中秋节"]));
  88. });
  89. test("should NOT find Dragon Boat Festival for solar 2028-05-30 as it's L05-07", () => { // 公历2028-05-30因对应农历五月初七,不应找到端午节
  90. // 根据getLunarDate,公历 "2028-05-30" 是农历 L2028-05-07 (五月初七)。
  91. // LUNAR_FESTIVAL_MAP[5][7] 未定义。
  92. const dragonBoatResult = getLunarFestivals("2028-05-30");
  93. expect(dragonBoatResult).toEqual([]);
  94. });
  95. test("should find no fixed festival for solar 2028-05-26 as it's L05-03", () => { // 公历2028-05-26因对应农历五月初三,不应找到固定节日
  96. // 根据getLunarDate,公历 "2028-05-26" 是农历 L2028-05-03。
  97. // LUNAR_FESTIVAL_MAP[5][3] 未定义。
  98. const result = getLunarFestivals("2028-05-26");
  99. expect(result).toEqual([]);
  100. // 背景信息:实际的端午节 (农历L2028-05-05) 会在另一个公历日期。
  101. // 对公历 "2028-06-29" (即农历L2028-06-07) 的测试正确地预期 []。
  102. const leapTestDateResult = getLunarFestivals("2028-06-29"); // 这是农历L2028-06-07。
  103. expect(leapTestDateResult).toEqual([]);
  104. });
  105. test("getLunarFestivals with undefined start/end should query for current day (mocked to solar 2024-05-12, which is L04-05)", () => { // getLunarFestivals 未定义起止日期时应查询当天 (模拟当前日期为公历2024-05-12, 即农历四月初五)
  106. const mockToday = "2024-05-12"; // 公历 "2024-05-12" 是农历 L2024-04-05。
  107. // LUNAR_FESTIVAL_MAP[4][5] 未定义,无固定节日。
  108. // 母亲节处理器缺失。
  109. const originalDayjs = dayjs;
  110. // @ts-ignore
  111. dayjs = jest.fn((dateInput?: any) => {
  112. if (dateInput === undefined || dateInput === null || dateInput === '') {
  113. return originalDayjs(mockToday);
  114. }
  115. return originalDayjs(dateInput);
  116. });
  117. Object.assign(dayjs, originalDayjs);
  118. expect(getLunarFestivals()).toEqual([]); // 预期为空,因为农历L04-05没有节日
  119. // @ts-ignore
  120. dayjs = originalDayjs; // 恢复原始的 dayjs
  121. });
  122. });
  123. test("should handle cross-year scenarios", () => {
  124. // 测试多天与跨年场景
  125. const result = getLunarFestivals("2024-11-15", "2025-01-30");
  126. expect(result).toEqual([
  127. {
  128. date: "2024-11-15",
  129. name: ["下元节", "水官诞辰"],
  130. },
  131. {
  132. date: "2025-01-07",
  133. name: ["腊八节"],
  134. },
  135. {
  136. date: "2025-01-22",
  137. name: ["官家送灶"],
  138. },
  139. {
  140. date: "2025-01-23",
  141. name: ["民间送灶"],
  142. },
  143. {
  144. date: "2025-01-24",
  145. name: ["接玉皇"],
  146. },
  147. {
  148. date: "2025-01-28",
  149. name: ["除夕", "封井", "祭井神", "贴春联", "迎财神"],
  150. },
  151. {
  152. date: "2025-01-29",
  153. name: ["春节", "鸡日", "元始天尊诞辰"],
  154. },
  155. {
  156. date: "2025-01-30",
  157. name: ["犬日"],
  158. },
  159. ]);
  160. });
  161. });