Skip to content

A local-first personal finance app

27.7 KB
Raw Blame
import MockDate from 'mockdate';

import type { RuleConditionEntity, ScheduleEntity } from '#types/models';

import * as monthUtils from './months';
import {
  computeSchedulePreviewTransactions,
  getHasTransactionsQuery,
  getNextDate,
  getNextDateAfter,
  getOccurrencesBetween,
  getScheduleOccurrenceMatchStartDate,
  getStatus,
  getUpcomingDays,
  indexPostedScheduleTransactions,
  isCustomUpcomingLength,
  isScheduleOccurrencePosted,
  UPCOMING_LENGTH_PRESET_LABELS,
  UPCOMING_LENGTH_PRESET_OPTIONS,
  UPCOMING_LENGTH_PRESET_VALUES,
} from './schedules';
import type { ScheduleStatuses } from './schedules';

describe('schedules', () => {
  const today = new Date(2017, 0, 1); // Global date when testing is set to 2017-01-01 per monthUtils.currentDay()
  const dateFormat = 'yyyy-MM-dd';
  const todayString = monthUtils.format(today, dateFormat);

  beforeEach(() => {
    MockDate.set(new Date(2021, 4, 14));
  });
  afterEach(() => {
    MockDate.reset();
  });

  describe('getStatus', () => {
    it('returns completed if completed', () => {
      expect(getStatus(todayString, true, false, '7')).toBe('completed');
    });

    it('returns paid if has transactions', () => {
      expect(getStatus(todayString, false, true, '7')).toBe('paid');
    });

    it('returns due if today', () => {
      expect(getStatus(todayString, false, false, '7')).toBe('due');
    });

    it.each([1, 7, 14, 30])(
      'returns upcoming if within upcoming range %n',
      (upcomingLength: number) => {
        const daysOut = upcomingLength;
        const tomorrow = monthUtils.addDays(today, 1);
        const upcomingDate = monthUtils.addDays(today, daysOut);
        const scheduledDate = monthUtils.addDays(today, daysOut + 1);
        expect(
          getStatus(tomorrow, false, false, upcomingLength.toString()),
        ).toBe('upcoming');
        expect(
          getStatus(upcomingDate, false, false, upcomingLength.toString()),
        ).toBe('upcoming');
        expect(
          getStatus(scheduledDate, false, false, upcomingLength.toString()),
        ).toBe('scheduled');
      },
    );

    it('returns missed if past', () => {
      expect(getStatus(monthUtils.addDays(today, -1), false, false, '7')).toBe(
        'missed',
      );
    });

    it('returns scheduled if not due, upcoming, or missed', () => {
      expect(getStatus(monthUtils.addDays(today, 8), false, false, '7')).toBe(
        'scheduled',
      );
    });
  });

  describe('getUpcomingDays', () => {
    it.each([
      ['1', 1, '2017-01-01'],
      ['7', 7, '2017-01-01'],
      ['14', 14, '2017-01-01'],
      ['oneMonth', 31, '2017-01-01'],
      ['oneMonth', 30, '2017-04-01'],
      ['oneMonth', 30, '2017-04-15'],
      ['oneMonth', 28, '2017-02-01'],
      ['oneMonth', 29, '2020-02-01'], // leap-year
      ['currentMonth', 30, '2017-01-01'],
      ['currentMonth', 27, '2017-02-01'],
      ['currentMonth', 20, '2017-02-08'],
      ['currentMonth', 28, '2020-02-01'], // leap-year
      ['2-day', 2, '2017-01-01'],
      ['5-week', 35, '2017-01-01'],
      ['3-month', 91, '2017-01-01'],
      ['4-year', 1462, '2017-01-01'],
      ['1-year', 366, '2017-06-15'], // Test year from mid-year (Jun 1, 2017 to Jun 1, 2018 + 1)
      ['1-year', 367, '2019-06-15'], // Test year from mid-year with leap year 2020
      ['2-year', 731, '2017-06-15'], // Test 2 years from mid-year (Jun 1, 2017 to Jun 1, 2019 + 1)
    ])(
      'value of %s on returns %i days on %s',
      (value: string, expected: number, date: string) => {
        expect(getUpcomingDays(value, date)).toEqual(expected);
      },
    );
  });

  describe('computeSchedulePreviewTransactions', () => {
    describe('forceUpcoming flag', () => {
      function makeSchedule(
        overrides: Partial<ScheduleEntity> &
          Pick<ScheduleEntity, 'id' | 'next_date' | '_conditions'>,
      ): ScheduleEntity {
        return {
          rule: 'rule-1',
          completed: false,
          posts_transaction: false,
          tombstone: false,
          _payee: 'payee-1',
          _account: 'acct-1',
          _amount: -10000,
          _amountOp: 'is',
          _date: overrides.next_date,
          _actions: [],
          ...overrides,
        };
      }

      it('sets forceUpcoming=false for past dates of a missed recurring schedule', () => {
        const schedule = makeSchedule({
          id: 'sched-1',
          next_date: '2016-12-19',
          _conditions: [
            {
              field: 'date',
              op: 'isapprox',
              value: { start: '2016-12-01', frequency: 'weekly' },
            },
          ],
        });

        const statuses: ScheduleStatuses = new Map([['sched-1', 'missed']]);
        const result = computeSchedulePreviewTransactions(
          [schedule],
          statuses,
          '7',
        );

        const pastEntries = result.filter(r => r.date < '2017-01-01');
        expect(pastEntries.length).toBeGreaterThan(0);
        expect(pastEntries.every(r => r.forceUpcoming === false)).toBe(true);
      });

      it('sets forceUpcoming=true for future dates that differ from next_date', () => {
        const schedule = makeSchedule({
          id: 'sched-1',
          next_date: '2016-12-19',
          _conditions: [
            {
              field: 'date',
              op: 'isapprox',
              value: { start: '2016-12-01', frequency: 'weekly' },
            },
          ],
        });

        const statuses: ScheduleStatuses = new Map([['sched-1', 'missed']]);
        const result = computeSchedulePreviewTransactions(
          [schedule],
          statuses,
          '7',
        );

        const futureEntries = result.filter(r => r.date > '2017-01-01');
        expect(futureEntries.length).toBeGreaterThan(0);
        expect(futureEntries.every(r => r.forceUpcoming === true)).toBe(true);
      });

      it('sets forceUpcoming=false for next_date when not paid', () => {
        const schedule = makeSchedule({
          id: 'sched-1',
          next_date: '2017-01-03',
          _conditions: [{ field: 'date', op: 'is', value: '2017-01-03' }],
        });

        const statuses: ScheduleStatuses = new Map([['sched-1', 'upcoming']]);
        const result = computeSchedulePreviewTransactions(
          [schedule],
          statuses,
          '7',
        );

        expect(result).toHaveLength(1);
        expect(result[0].forceUpcoming).toBe(false);
      });

      it('shifts next_date and forces upcoming for paid schedules', () => {
        const schedule = makeSchedule({
          id: 'sched-1',
          next_date: '2017-01-02',
          _conditions: [
            {
              field: 'date',
              op: 'isapprox',
              value: { start: '2016-12-01', frequency: 'weekly' },
            },
          ],
        });

        const statuses: ScheduleStatuses = new Map([['sched-1', 'paid']]);
        const result = computeSchedulePreviewTransactions(
          [schedule],
          statuses,
          '7',
        );

        expect(result.find(r => r.date === '2017-01-02')).toBeUndefined();
        expect(
          result
            .filter(r => r.date >= '2017-01-01')
            .every(r => r.forceUpcoming === true),
        ).toBe(true);
      });
    });

    it('does not crash when a recurring schedule has an end date in the past', () => {
      function makeSchedule(
        overrides: Partial<ScheduleEntity> &
          Pick<ScheduleEntity, 'id' | 'next_date' | '_conditions'>,
      ): ScheduleEntity {
        return {
          rule: 'rule-1',
          completed: false,
          posts_transaction: false,
          tombstone: false,
          _payee: 'payee-1',
          _account: 'acct-1',
          _amount: -10000,
          _amountOp: 'is',
          _date: overrides.next_date,
          _actions: [],
          ...overrides,
        };
      }

      // Schedule that recurs monthly but ended in the past (2016-08-25)
      // while current date is 2017-01-01
      const schedule = makeSchedule({
        id: 'sched-expired',
        next_date: '2016-08-25',
        _conditions: [
          {
            field: 'date',
            op: 'isapprox',
            value: {
              start: '2016-01-25',
              frequency: 'monthly',
              endMode: 'on_date',
              endDate: '2016-08-25',
            },
          },
        ],
      });

      const statuses: ScheduleStatuses = new Map([['sched-expired', 'missed']]);
      const result = computeSchedulePreviewTransactions(
        [schedule],
        statuses,
        '7',
      );

      // Should not crash; schedule with past end date produces its next_date entry only
      expect(result).toBeDefined();
    });
  });

  describe('getHasTransactionsQuery', () => {
    it('matches nothing when there are no schedules', () => {
      // An empty `$or` compiles away to no constraint at all, which would make
      // this scan every transaction in the budget. It must never do that.
      const filters = getHasTransactionsQuery([]).serialize().filterExpressions;

      expect(filters).toEqual([{ id: null }]);
      expect(filters[0]).not.toHaveProperty('$or');
    });

    it('filters by schedule and date when schedules are given', () => {
      const filters = getHasTransactionsQuery([
        {
          id: 'schedule-1',
          next_date: '2024-03-10',
          _conditions: [{ op: 'is', field: 'date', value: '2024-03-10' }],
        },
      ]).serialize().filterExpressions;

      expect(filters).toEqual([
        {
          $or: [
            { $and: { schedule: 'schedule-1', date: { $gte: '2024-03-10' } } },
          ],
        },
      ]);
    });
  });

  describe('getScheduleOccurrenceMatchStartDate', () => {
    const occurrenceDate = '2024-03-10';

    it('uses exact date for one-time schedules', () => {
      expect(
        getScheduleOccurrenceMatchStartDate(
          {
            _conditions: [{ op: 'is', field: 'date', value: occurrenceDate }],
          },
          occurrenceDate,
        ),
      ).toBe(occurrenceDate);
    });

    it('uses exact date for auto-posted recurring schedules', () => {
      expect(
        getScheduleOccurrenceMatchStartDate(
          { posts_transaction: true },
          occurrenceDate,
        ),
      ).toBe(occurrenceDate);
    });

    it('uses a 2-day lookback for manual recurring schedules', () => {
      expect(
        getScheduleOccurrenceMatchStartDate(
          { posts_transaction: false },
          occurrenceDate,
        ),
      ).toBe('2024-03-08');
    });

    it('uses exact date for recurring schedules with op is', () => {
      expect(
        getScheduleOccurrenceMatchStartDate(
          {
            posts_transaction: false,
            _conditions: [
              {
                op: 'is',
                field: 'date',
                value: { start: occurrenceDate, frequency: 'monthly' },
              },
            ],
          },
          occurrenceDate,
        ),
      ).toBe(occurrenceDate);
    });

    it('uses exact date for daily recurring schedules with op is', () => {
      expect(
        getScheduleOccurrenceMatchStartDate(
          {
            posts_transaction: false,
            _conditions: [
              {
                op: 'is',
                field: 'date',
                value: { start: occurrenceDate, frequency: 'daily' },
              },
            ],
          },
          occurrenceDate,
        ),
      ).toBe(occurrenceDate);
    });
  });

  describe('indexPostedScheduleTransactions', () => {
    it('groups schedule-linked transactions by schedule id', () => {
      const indexed = indexPostedScheduleTransactions([
        { schedule: 'sched-1', date: '2024-03-09' },
        { schedule: 'sched-2', date: '2024-03-10' },
        { schedule: 'sched-1', date: '2024-04-10' },
        { date: '2024-03-11' },
      ]);

      expect(indexed.get('sched-1')).toEqual([
        { schedule: 'sched-1', date: '2024-03-09' },
        { schedule: 'sched-1', date: '2024-04-10' },
      ]);
      expect(indexed.get('sched-2')).toEqual([
        { schedule: 'sched-2', date: '2024-03-10' },
      ]);
      expect(indexed.has('missing')).toBe(false);
    });
  });

  describe('isScheduleOccurrencePosted', () => {
    const scheduleId = 'sched-1';
    const occurrenceDate = '2024-03-10';
    const manualRecurringSchedule = { posts_transaction: false };
    const autoPostSchedule = { posts_transaction: true };
    const oneTimeSchedule = {
      _conditions: [
        { op: 'is', field: 'date', value: occurrenceDate } as const,
      ],
    };
    const manualRecurringWithIsOp = {
      posts_transaction: false,
      _conditions: [
        {
          op: 'is',
          field: 'date',
          value: { start: occurrenceDate, frequency: 'monthly' },
        },
      ] satisfies RuleConditionEntity[],
    };

    function expectPosted(
      schedule: Parameters<typeof getScheduleOccurrenceMatchStartDate>[0],
      txDate: string,
      expected: boolean,
    ) {
      expect(
        isScheduleOccurrencePosted({
          schedule,
          scheduleId,
          occurrenceDate,
          postedTransactions: [{ schedule: scheduleId, date: txDate }],
        }),
      ).toBe(expected);
    }

    it.each([
      [
        'same-day manual recurring',
        manualRecurringSchedule,
        occurrenceDate,
        true,
      ],
      [
        'early pay day before due for recurring date cond',
        manualRecurringWithIsOp,
        '2024-03-09',
        false,
      ],
      ['early pay within 2 days', manualRecurringSchedule, '2024-03-09', true],
      [
        'early pay outside window',
        manualRecurringSchedule,
        '2024-03-07',
        false,
      ],
      ['auto-post day before due', autoPostSchedule, '2024-03-09', false],
      ['auto-post on due date', autoPostSchedule, occurrenceDate, true],
      ['one-time on due date', oneTimeSchedule, occurrenceDate, true],
      ['one-time day before due', oneTimeSchedule, '2024-03-09', false],
      [
        'later month tx does not satisfy earlier occurrence',
        manualRecurringSchedule,
        '2024-04-10',
        false,
      ],
    ] as const)('%s', (_label, schedule, txDate, expected) => {
      expectPosted(schedule, txDate, expected);
    });
  });

  describe('getNextDate', () => {
    it('returns last occurrence for a recurring schedule with an end date in the past', () => {
      const dateCond = {
        op: 'isapprox',
        value: {
          start: '2016-01-25',
          frequency: 'monthly',
          endMode: 'on_date',
          endDate: '2016-08-25',
        },
      };

      // Current date is 2017-01-01 via MockDate
      const result = getNextDate(dateCond);
      expect(result).not.toBeNull();
      // The last occurrence should be returned (reverse lookup)
      expect(result).toBe('2016-08-25');
    });

    it('returns null when the end date is before the start date', () => {
      const dateCond = {
        op: 'isapprox',
        value: {
          start: '2016-03-25',
          frequency: 'monthly',
          endMode: 'on_date',
          endDate: '2016-01-25',
        },
      };

      const result = getNextDate(dateCond, new Date(2017, 0, 1));
      expect(result).toBeNull();
    });
  });

  describe('getNextDateAfter', () => {
    /* Dec 2020 calendar for reference:
      | Su | Mo | Tu | We | Th | Fr | Sa |
      |    |    | 01 | 02 | 03 | 04 | 05 |
      | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
      | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
      | 20 | 21 | 22 | 23 | 24 | 25 | 26 |
      | 27 | 28 | 29 | 30 | 31 |
      */
    function weeklyOnSaturday(extra = {}) {
      return {
        op: 'isapprox',
        value: {
          start: '2020-12-05',
          frequency: 'weekly',
          patterns: [],
          ...extra,
        },
      };
    }

    it('returns the next occurrence after the given date', () => {
      expect(getNextDateAfter(weeklyOnSaturday(), '2020-12-05')).toBe(
        '2020-12-12',
      );
    });

    it('returns the next occurrence when moving `after` the weekend', () => {
      const dateCond = weeklyOnSaturday({
        skipWeekend: true,
        weekendSolveMode: 'after',
      });

      expect(getNextDateAfter(dateCond, '2020-12-07')).toBe('2020-12-14');
    });

    it('includes a weekend occurrence moved `after` the given date', () => {
      const dateCond = weeklyOnSaturday({
        skipWeekend: true,
        weekendSolveMode: 'after',
      });

      expect(getNextDateAfter(dateCond, '2020-12-13')).toBe('2020-12-14');
    });

    it('does not return the same occurrence when moving `after` the weekend', () => {
      const dateCond = {
        op: 'isapprox',
        value: {
          start: '2020-12-06',
          frequency: 'weekly',
          patterns: [],
          skipWeekend: true,
          weekendSolveMode: 'after',
        },
      };

      expect(getNextDateAfter(dateCond, '2020-12-07')).toBe('2020-12-14');
    });

    it('does not return the same occurrence when moving `before` the weekend', () => {
      const dateCond = weeklyOnSaturday({
        skipWeekend: true,
        weekendSolveMode: 'before',
      });

      expect(getNextDateAfter(dateCond, '2020-12-04')).toBe('2020-12-11');
    });

    it('keeps a Monday occurrence that follows a `before` weekend adjustment', () => {
      const dateCond = {
        op: 'isapprox',
        value: {
          start: '2020-12-04',
          frequency: 'daily',
          patterns: [],
          skipWeekend: true,
          weekendSolveMode: 'before',
        },
      };

      expect(getNextDateAfter(dateCond, '2020-12-04')).toBe('2020-12-07');
    });

    it('walks past every occurrence that resolves on or before the given date', () => {
      /* Aug 2026 calendar for reference:
        | Su | Mo | Tu | We | Th | Fr | Sa |
        | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
        | 30 | 31 |
        */
      const dateCond = {
        op: 'isapprox',
        value: {
          start: '2026-08-24',
          frequency: 'daily',
          patterns: [],
          skipWeekend: true,
          weekendSolveMode: 'before',
        },
      };

      expect(getNextDateAfter(dateCond, '2026-08-28')).toBe('2026-08-31');
    });

    it('returns null when the schedule has no further occurrences', () => {
      const dateCond = weeklyOnSaturday({
        endMode: 'after_n_occurrences',
        endOccurrences: 2,
      });

      expect(getNextDateAfter(dateCond, '2020-12-05')).toBe('2020-12-12');
      expect(getNextDateAfter(dateCond, '2020-12-12')).toBeNull();
    });
  });

  describe('shared presets', () => {
    it('preset values and options align', () => {
      const valuesFromOptions = UPCOMING_LENGTH_PRESET_OPTIONS.map(
        o => o.value,
      );
      expect(valuesFromOptions).toEqual(
        UPCOMING_LENGTH_PRESET_VALUES as readonly string[],
      );
    });

    it('every preset has a label entry', () => {
      for (const v of UPCOMING_LENGTH_PRESET_VALUES) {
        expect(UPCOMING_LENGTH_PRESET_LABELS[v]).toBeDefined();
        expect(typeof UPCOMING_LENGTH_PRESET_LABELS[v]).toBe('string');
      }
    });

    it('isCustomUpcomingLength recognizes presets and custom values', () => {
      for (const v of UPCOMING_LENGTH_PRESET_VALUES) {
        expect(isCustomUpcomingLength(v)).toBe(false);
      }

      expect(isCustomUpcomingLength('1-day')).toBe(true);
      expect(isCustomUpcomingLength('2-week')).toBe(true);
      expect(isCustomUpcomingLength(null)).toBe(false);
      expect(isCustomUpcomingLength(undefined)).toBe(false);
    });
  });

  describe('getOccurrencesBetween', () => {
    it('returns every monthly occurrence within the range', () => {
      const dateCond = {
        op: 'is' as const,
        field: 'date' as const,
        value: {
          start: '2024-01-15',
          interval: 1,
          frequency: 'monthly' as const,
          patterns: [],
          skipWeekend: false,
          weekendSolveMode: 'before' as const,
          endMode: 'never' as const,
          endOccurrences: 1,
          endDate: '2099-01-01',
        },
      };
      const result = getOccurrencesBetween(
        dateCond,
        '2024-01-01',
        '2024-04-01',
      );
      expect(result).toEqual(['2024-01-15', '2024-02-15', '2024-03-15']);
    });

    it('returns an empty array when the single non-repeating date is outside the range', () => {
      const dateCond = {
        op: 'is' as const,
        field: 'date' as const,
        value: '2023-06-01',
      };
      expect(
        getOccurrencesBetween(dateCond, '2024-01-01', '2024-04-01'),
      ).toEqual([]);
    });

    it('returns the single date when a non-repeating date is inside the range', () => {
      const dateCond = {
        op: 'is' as const,
        field: 'date' as const,
        value: '2024-02-01',
      };
      expect(
        getOccurrencesBetween(dateCond, '2024-01-01', '2024-04-01'),
      ).toEqual(['2024-02-01']);
    });

    it('excludes an occurrence that falls exactly on the exclusive endDay', () => {
      const dateCond = {
        op: 'is' as const,
        field: 'date' as const,
        value: {
          start: '2024-01-15',
          interval: 1,
          frequency: 'monthly' as const,
          patterns: [],
          skipWeekend: false,
          weekendSolveMode: 'before' as const,
          endMode: 'never' as const,
          endOccurrences: 1,
          endDate: '2099-01-01',
        },
      };
      // The next occurrence after 2024-01-15 is 2024-02-15, which is used
      // as the (exclusive) endDay here, so it should not be included.
      const result = getOccurrencesBetween(
        dateCond,
        '2024-01-01',
        '2024-02-15',
      );
      expect(result).toEqual(['2024-01-15']);
    });

    it('returns an empty array without hanging when a recurring schedule ended before the range', () => {
      const dateCond = {
        op: 'isapprox' as const,
        field: 'date' as const,
        value: {
          start: '2016-01-25',
          interval: 1,
          frequency: 'monthly' as const,
          patterns: [],
          skipWeekend: false,
          weekendSolveMode: 'before' as const,
          endMode: 'on_date' as const,
          endOccurrences: 1,
          endDate: '2016-08-25',
        },
      };
      // getNextDate falls back to the schedule's last-ever occurrence
      // (2016-08-25) when asked for the next occurrence after the schedule
      // has ended. getOccurrencesBetween must recognize that stale fallback
      // date is behind the search cursor and stop, rather than looping
      // forever re-requesting the same date.
      const result = getOccurrencesBetween(
        dateCond,
        '2016-08-26',
        '2026-01-01',
      );
      expect(result).toEqual([]);
    });

    it('does not truncate occurrences when weekendSolveMode "before" shifts a date behind the search cursor', () => {
      // Weekly on Saturday, skipWeekend + weekendSolveMode: 'before' means
      // every occurrence is reported on the Friday before it. Querying with
      // startDay set to a raw Saturday occurrence means getNextDate's
      // adjusted result (the Friday before) lands *behind* the search
      // cursor even though the schedule hasn't ended and more occurrences
      // remain — this must not be mistaken for "the schedule ended".
      const dateCond = {
        op: 'isapprox' as const,
        field: 'date' as const,
        value: {
          start: '2020-12-05', // a Saturday
          interval: 1,
          frequency: 'weekly' as const,
          patterns: [],
          skipWeekend: true,
          weekendSolveMode: 'before' as const,
          endMode: 'never' as const,
          endOccurrences: 1,
          endDate: '2099-01-01',
        },
      };

      const result = getOccurrencesBetween(
        dateCond,
        '2020-12-05',
        '2020-12-26',
      );

      expect(result).toEqual(['2020-12-11', '2020-12-18', '2020-12-25']);
    });

    it('includes a raw occurrence landing exactly on the exclusive endDay when the weekend adjustment shifts it backward into range', () => {
      // Weekly on Saturday, skipWeekend + weekendSolveMode: 'before'.
      // Raw occurrences: 12/5, 12/12, 12/19, 12/26, ...
      // The raw occurrence on 2020-12-26 sits exactly on the (exclusive)
      // endDay, so a naive raw-range query would drop it. But its
      // weekend-adjusted date (2020-12-25, the Friday before) lands inside
      // [startDay, endDay) and must be included.
      const dateCond = {
        op: 'isapprox' as const,
        field: 'date' as const,
        value: {
          start: '2020-12-05',
          interval: 1,
          frequency: 'weekly' as const,
          patterns: [],
          skipWeekend: true,
          weekendSolveMode: 'before' as const,
          endMode: 'never' as const,
          endOccurrences: 1,
          endDate: '2099-01-01',
        },
      };

      const result = getOccurrencesBetween(
        dateCond,
        '2020-12-20',
        '2020-12-26',
      );

      expect(result).toEqual(['2020-12-25']);
    });

    it('excludes a raw occurrence landing exactly on startDay when the weekend adjustment shifts it backward out of range', () => {
      // Same schedule as above. The raw occurrence on 2020-12-19 sits
      // exactly on startDay, so a naive raw-range query would keep it. But
      // its weekend-adjusted date (2020-12-18, the Friday before) lands
      // before startDay and must be excluded.
      const dateCond = {
        op: 'isapprox' as const,
        field: 'date' as const,
        value: {
          start: '2020-12-05',
          interval: 1,
          frequency: 'weekly' as const,
          patterns: [],
          skipWeekend: true,
          weekendSolveMode: 'before' as const,
          endMode: 'never' as const,
          endOccurrences: 1,
          endDate: '2099-01-01',
        },
      };

      const result = getOccurrencesBetween(
        dateCond,
        '2020-12-19',
        '2020-12-26',
      );

      expect(result).toEqual(['2020-12-25']);
    });

    it('includes a raw occurrence landing before startDay when the weekend adjustment shifts it forward into range', () => {
      // Weekly on Saturday, skipWeekend + weekendSolveMode: 'after'.
      // Raw occurrences: 12/5, 12/12, 12/19, 12/26, ...
      // The raw occurrence on 2020-12-05 sits 2 days before startDay, so a
      // naive raw-range query would drop it. But its weekend-adjusted date
      // (2020-12-07, the Monday after) lands exactly on startDay and must
      // be included.
      const dateCond = {
        op: 'isapprox' as const,
        field: 'date' as const,
        value: {
          start: '2020-12-05',
          interval: 1,
          frequency: 'weekly' as const,
          patterns: [],
          skipWeekend: true,
          weekendSolveMode: 'after' as const,
          endMode: 'never' as const,
          endOccurrences: 1,
          endDate: '2099-01-01',
        },
      };

      const result = getOccurrencesBetween(
        dateCond,
        '2020-12-07',
        '2020-12-12',
      );

      expect(result).toEqual(['2020-12-07']);
    });

    it('excludes a raw occurrence landing before endDay when the weekend adjustment shifts it forward out of range', () => {
      // Same schedule as above. The raw occurrence on 2020-12-12 sits
      // before endDay, so a naive raw-range query would keep it. But its
      // weekend-adjusted date (2020-12-14, the Monday after) lands exactly
      // on the exclusive endDay and must be excluded.
      const dateCond = {
        op: 'isapprox' as const,
        field: 'date' as const,
        value: {
          start: '2020-12-05',
          interval: 1,
          frequency: 'weekly' as const,
          patterns: [],
          skipWeekend: true,
          weekendSolveMode: 'after' as const,
          endMode: 'never' as const,
          endOccurrences: 1,
          endDate: '2099-01-01',
        },
      };

      const result = getOccurrencesBetween(
        dateCond,
        '2020-12-05',
        '2020-12-14',
      );

      expect(result).toEqual(['2020-12-07']);
    });
  });
});