File size: 1,820 Bytes
cf86710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { daysInHebrewMonth } from "./calendarMath.js";
import { toHebrewDate } from "./dateConversion.js";
import {
  clampHebrewDay,
  hebrewMonthNumber,
  monthIndexToHebrewDate,
  monthsSinceEpoch,
} from "./serial.js";

describe("hebrew serial helpers", () => {
  test("clamps invalid day when switching month", () => {
    const month = monthIndexToHebrewDate(0, 40);
    expect(month.day).toBeLessThanOrEqual(
      daysInHebrewMonth(month.year, month.monthIndex),
    );
  });

  test("monthsSinceEpoch round trip", () => {
    const dates = [
      new Date(2024, 3, 23),
      new Date(1990, 0, 1),
      new Date(1500, 5, 15),
    ];
    dates.forEach((date) => {
      const hebrew = toHebrewDate(date);
      const index = monthsSinceEpoch(hebrew);
      const roundTrip = monthIndexToHebrewDate(index, hebrew.day);
      expect(roundTrip.year).toBe(hebrew.year);
      expect(roundTrip.monthIndex).toBe(hebrew.monthIndex);
      expect(roundTrip.day).toBe(hebrew.day);
    });
  });

  test("monthsSinceEpoch handles negative indices", () => {
    const previousMonth = monthIndexToHebrewDate(-1, 1);
    expect(previousMonth.year).toBe(0);
    expect(previousMonth.monthIndex).toBeGreaterThanOrEqual(0);
  });

  test("clampHebrewDay returns original day when valid", () => {
    const hebrew = toHebrewDate(new Date(2024, 3, 10));
    const clamped = clampHebrewDay(hebrew.year, hebrew.monthIndex, hebrew.day);
    expect(clamped).toBe(hebrew.day);
  });

  test("clampHebrewDay returns month maximum", () => {
    const year = 5785;
    const cheshvanIndex = 1;
    const clamped = clampHebrewDay(year, cheshvanIndex, 30);
    expect(clamped).toBe(daysInHebrewMonth(year, cheshvanIndex));
  });

  test("hebrewMonthNumber converts to 1-based", () => {
    expect(hebrewMonthNumber(0)).toBe(1);
  });
});