Skip to content

A local-first personal finance app

8.5 KB
Raw Blame
import * as monthUtils from '@actual-app/core/shared/months';
import { describe, expect, it } from 'vitest';

import {
  boundMonthRange,
  calculateSpendingReportTimeRange,
  calculateTimeRange,
  getFullFutureRange,
  getLatestRange,
  validateEnd,
  validateStart,
} from './reportRanges';

// In test mode, monthUtils.currentMonth() returns '2017-01'
describe('calculateTimeRange', () => {
  it('keeps last month as a live time range when restoring a saved widget', () => {
    const [start, end, mode] = calculateTimeRange({
      start: '2016-11',
      end: '2016-11',
      mode: 'lastMonth',
    });

    expect(start).toBe('2016-12');
    expect(end).toBe('2016-12');
    expect(mode).toBe('lastMonth');
  });

  it('anchors a live window that ends at the current month to now', () => {
    // Saved 6-month window ending at the (then-)current month.
    const [start, end, mode] = calculateTimeRange({
      start: '2016-08',
      end: '2017-01',
      mode: 'sliding-window',
    });

    expect(start).toBe('2016-08');
    expect(end).toBe('2017-01');
    expect(mode).toBe('sliding-window');
  });

  it('anchors a live window that ends before the current month to now', () => {
    // Saved 6-month window ending one month before the (then-)current month;
    // a live window always slides so its end is the current month.
    const [start, end, mode] = calculateTimeRange({
      start: '2016-07',
      end: '2016-12',
      mode: 'sliding-window',
    });

    expect(end).toBe('2017-01');
    expect(start).toBe('2016-08'); // width of 5 preserved
    expect(mode).toBe('sliding-window');
  });

  it('anchors a live day-shaped window to today, preserving its width in days', () => {
    const [start, end, mode] = calculateTimeRange({
      start: '2016-12-15',
      end: '2016-12-29',
      mode: 'sliding-window',
    });

    expect(end).toBe('2017-01-01'); // currentDay() in test mode
    expect(start).toBe('2016-12-18'); // width of 14 days preserved
    expect(mode).toBe('sliding-window');
  });

  it('keeps current quarter as a live time range when restoring a saved widget', () => {
    const [start, end, mode] = calculateTimeRange({
      start: '2016-10',
      end: '2016-12',
      mode: 'currentQuarter',
    });

    expect(start).toBe('2017-01');
    expect(end).toBe('2017-03');
    expect(mode).toBe('currentQuarter');
  });

  it('keeps previous quarter as a live time range when restoring a saved widget', () => {
    const [start, end, mode] = calculateTimeRange({
      start: '2016-07',
      end: '2016-09',
      mode: 'previousQuarter',
    });

    expect(start).toBe('2016-10');
    expect(end).toBe('2016-12');
    expect(mode).toBe('previousQuarter');
  });
});

// In test mode, monthUtils.currentMonth() returns '2017-01'
describe('getLatestRange', () => {
  it('ends at the current month', () => {
    expect(getLatestRange(5)).toEqual(['2016-08', '2017-01', 'sliding-window']);
  });
});

// In test mode, monthUtils.currentMonth() returns '2017-01'
describe('calculateSpendingReportTimeRange', () => {
  it('slides live average reports to the current month', () => {
    const [compare, compareTo] = calculateSpendingReportTimeRange({
      compare: '2016-12',
      isLive: true,
      mode: 'average',
    });

    expect(compare).toBe('2017-01');
    expect(compareTo).toBe('2017-01');
  });

  it('slides live budget reports to the current month', () => {
    const [compare, compareTo] = calculateSpendingReportTimeRange({
      compare: '2016-12',
      isLive: true,
      mode: 'budget',
    });

    expect(compare).toBe('2017-01');
    expect(compareTo).toBe('2017-01');
  });

  it('slides live single month reports to the current month, preserving the gap', () => {
    const [compare, compareTo] = calculateSpendingReportTimeRange({
      compare: '2016-12',
      compareTo: '2016-11',
      isLive: true,
      mode: 'single-month',
    });

    expect(compare).toBe('2017-01');
    expect(compareTo).toBe('2016-12');
  });

  it('preserves a wider gap when sliding live single month reports', () => {
    const [compare, compareTo] = calculateSpendingReportTimeRange({
      compare: '2016-12',
      compareTo: '2016-09',
      isLive: true,
      mode: 'single-month',
    });

    expect(compare).toBe('2017-01');
    expect(compareTo).toBe('2016-10'); // gap of 3 preserved
  });

  it('defaults a missing compareTo to the month before compare', () => {
    const [compare, compareTo] = calculateSpendingReportTimeRange({
      compare: '2016-01',
      isLive: true,
      mode: 'single-month',
    });

    expect(compare).toBe('2017-01');
    expect(compareTo).toBe('2016-12');
  });

  it('defaults a missing compareTo for static single month reports', () => {
    const [compare, compareTo] = calculateSpendingReportTimeRange({
      compare: '2016-01',
      isLive: false,
      mode: 'single-month',
    });

    expect(compare).toBe('2016-01');
    expect(compareTo).toBe('2015-12');
  });

  it('preserves the saved compare months for static single month reports', () => {
    const [compare, compareTo] = calculateSpendingReportTimeRange({
      compare: '2016-12',
      compareTo: '2016-11',
      isLive: false,
      mode: 'single-month',
    });

    expect(compare).toBe('2016-12');
    expect(compareTo).toBe('2016-11');
  });

  it('preserves the saved compare month for static budget reports', () => {
    const [compare] = calculateSpendingReportTimeRange({
      compare: '2016-12',
      compareTo: '2016-12',
      isLive: false,
      mode: 'budget',
    });

    expect(compare).toBe('2016-12');
  });

  it('defaults live average reports to the current month without a saved compare month', () => {
    const [compare, compareTo] = calculateSpendingReportTimeRange({
      isLive: true,
      mode: 'average',
    });

    expect(compare).toBe('2017-01');
    expect(compareTo).toBe('2017-01');
  });
});

describe('getFullFutureRange', () => {
  it('uses a future month as the end of the range', () => {
    const start = monthUtils.currentMonth();
    const futureMonth = monthUtils.addMonths(start, 36);

    expect(getFullFutureRange(futureMonth)).toEqual([
      start,
      futureMonth,
      'static',
    ]);
  });

  it('falls back to a default future horizon without a future month', () => {
    const start = monthUtils.currentMonth();

    expect(getFullFutureRange()).toEqual([
      start,
      monthUtils.addMonths(start, 24),
      'static',
    ]);
  });
});

describe('boundMonthRange', () => {
  it('keeps a range that is already within bounds', () => {
    expect(boundMonthRange('2026-08', '2026-09', '2026-08', '2026-09')).toEqual(
      ['2026-08', '2026-09'],
    );
  });

  it('bounds both ends to the available range', () => {
    expect(boundMonthRange('2026-08', '2026-09', '2025-10', '2027-01')).toEqual(
      ['2026-08', '2026-09'],
    );
  });

  it('collapses to a single month when the bounded end is before start', () => {
    expect(boundMonthRange('2026-08', '2026-09', '2026-09', '2026-07')).toEqual(
      ['2026-09', '2026-09'],
    );
  });
});

describe('Quarterly range validation', () => {
  it('starts a range on the first day of the chosen quarter', () => {
    expect(
      validateStart(
        '2016-01-01',
        '2016-12-31',
        '2016-04',
        '2016-12-31',
        'Quarterly',
      ),
    ).toEqual(['2016-04-01', '2016-12-31', 'static']);
  });

  it('ends a range on the last day of the chosen quarter', () => {
    expect(
      validateEnd(
        '2016-01-01',
        '2016-12-31',
        '2016-01-01',
        '2016-04',
        'Quarterly',
      ),
    ).toEqual(['2016-01-01', '2016-06-30', 'static']);
  });

  it('keeps From when To moves before it on whole-quarter bounds', () => {
    expect(
      validateStart(
        '2010-01-01',
        '2016-12-31',
        '2014-07',
        '2014-06-30',
        'Quarterly',
      ),
    ).toEqual(['2014-07-01', '2015-06-30', 'static']);
  });

  it('keeps To when From moves after it on whole-quarter bounds', () => {
    expect(
      validateEnd(
        '2010-01-01',
        '2016-12-31',
        '2014-07-01',
        '2014-04',
        'Quarterly',
      ),
    ).toEqual(['2013-07-01', '2014-06-30', 'static']);
  });
});

describe('same-period range validation', () => {
  it.each([
    ['Monthly', '2016-04-01', '2016-04', '2016-04-30'],
    ['Quarterly', '2016-04-01', '2016-04', '2016-06-30'],
    ['Yearly', '2016-01-01', '2016', '2016-12-31'],
  ])(
    'keeps From when %s To is the same period',
    (interval, start, end, expectedEnd) => {
      expect(
        validateEnd('2015-01-01', '2016-12-31', start, end, interval),
      ).toEqual([start, expectedEnd, 'static']);
    },
  );
});