Skip to content

A local-first personal finance app

13.9 KB
Raw Blame
// @ts-strict-ignore
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import * as db from '#server/db';
import * as sheet from '#server/sheet';

import {
  copyUntilYearEnd,
  coverOverbudgeted,
  getCategoryWeeklyAverage,
  getSheetValue,
  set3MonthAvg,
  setBudget,
  setCategoryCarryover,
  setNMonthAvg,
  transferAvailable,
} from './actions';
import * as budget from './base';

describe('copyUntilYearEnd', () => {
  beforeEach(global.emptyDatabase());
  afterEach(global.emptyDatabase());

  async function setupDatabase() {
    await db.insertCategoryGroup({
      id: 'income-group',
      name: 'Income',
      is_income: 1,
    });
    await db.insertCategory({
      id: 'income-cat',
      name: 'Income',
      cat_group: 'income-group',
      is_income: 1,
    });
    await db.insertCategoryGroup({
      id: 'group1',
      name: 'group1',
      is_income: 0,
    });
    await db.insertCategory({
      id: 'cat1',
      name: 'cat1',
      cat_group: 'group1',
      is_income: 0,
    });
    await sheet.loadSpreadsheet(db);
    await budget.createBudget(['2024-01', '2024-02', '2024-03']);
  }

  it('copies the current month budget to all future months in the same year', async () => {
    await setupDatabase();

    await setBudget({ category: 'cat1', month: '2024-01', amount: 5000 });
    await setBudget({ category: 'cat1', month: '2024-02', amount: 1000 });
    await setBudget({ category: 'cat1', month: '2024-03', amount: 2000 });
    await sheet.waitOnSpreadsheet();

    await copyUntilYearEnd({ month: '2024-01', category: 'cat1' });
    await sheet.waitOnSpreadsheet();

    expect(await getSheetValue('budget202401', 'budget-cat1')).toBe(5000);
    expect(await getSheetValue('budget202402', 'budget-cat1')).toBe(5000);
    expect(await getSheetValue('budget202403', 'budget-cat1')).toBe(5000);
  });

  it('overwrites future months including those with zero budgets', async () => {
    await setupDatabase();

    await setBudget({ category: 'cat1', month: '2024-01', amount: 5000 });
    // 2024-02 intentionally left at 0
    await setBudget({ category: 'cat1', month: '2024-03', amount: 2000 });
    await sheet.waitOnSpreadsheet();

    await copyUntilYearEnd({ month: '2024-01', category: 'cat1' });
    await sheet.waitOnSpreadsheet();

    expect(await getSheetValue('budget202401', 'budget-cat1')).toBe(5000);
    expect(await getSheetValue('budget202402', 'budget-cat1')).toBe(5000);
    expect(await getSheetValue('budget202403', 'budget-cat1')).toBe(5000);
  });

  it('does not affect months before or equal to the current month', async () => {
    await setupDatabase();

    await setBudget({ category: 'cat1', month: '2024-01', amount: 1000 });
    await setBudget({ category: 'cat1', month: '2024-02', amount: 5000 });
    await setBudget({ category: 'cat1', month: '2024-03', amount: 2000 });
    await sheet.waitOnSpreadsheet();

    await copyUntilYearEnd({ month: '2024-02', category: 'cat1' });
    await sheet.waitOnSpreadsheet();

    expect(await getSheetValue('budget202401', 'budget-cat1')).toBe(1000);
    expect(await getSheetValue('budget202402', 'budget-cat1')).toBe(5000);
    expect(await getSheetValue('budget202403', 'budget-cat1')).toBe(5000);
  });

  it('copies the current month budget to future months in tracking budget mode', async () => {
    await setupDatabase();
    db.runQuery(
      `INSERT INTO preferences (id, value) VALUES ('budgetType', 'tracking')`,
    );

    await setBudget({ category: 'cat1', month: '2024-01', amount: 5000 });
    await setBudget({ category: 'cat1', month: '2024-02', amount: 1000 });
    await setBudget({ category: 'cat1', month: '2024-03', amount: 2000 });
    await sheet.waitOnSpreadsheet();

    await copyUntilYearEnd({ month: '2024-01', category: 'cat1' });
    await sheet.waitOnSpreadsheet();

    expect(await getSheetValue('budget202401', 'budget-cat1')).toBe(5000);
    expect(await getSheetValue('budget202402', 'budget-cat1')).toBe(5000);
    expect(await getSheetValue('budget202403', 'budget-cat1')).toBe(5000);
  });

  it('does not copy to months beyond the current calendar year', async () => {
    await setupDatabase();
    await budget.createBudget(['2024-11', '2024-12', '2025-01']);

    await setBudget({ category: 'cat1', month: '2024-11', amount: 5000 });
    await setBudget({ category: 'cat1', month: '2024-12', amount: 1000 });
    await setBudget({ category: 'cat1', month: '2025-01', amount: 2000 });
    await sheet.waitOnSpreadsheet();

    await copyUntilYearEnd({ month: '2024-11', category: 'cat1' });
    await sheet.waitOnSpreadsheet();

    expect(await getSheetValue('budget202411', 'budget-cat1')).toBe(5000);
    expect(await getSheetValue('budget202412', 'budget-cat1')).toBe(5000);
    expect(await getSheetValue('budget202501', 'budget-cat1')).toBe(2000); // unchanged
  });
});

describe('set budget average', () => {
  let originalCurrentMonth: string | null;

  beforeEach(async () => {
    await global.emptyDatabase()();
    originalCurrentMonth = global.currentMonth;
    global.currentMonth = '2024-02';
    await setupAverageDatabase();
  });

  afterEach(async () => {
    global.currentMonth = originalCurrentMonth;
    await global.emptyDatabase()();
  });

  it('sets a single category average from complete months', async () => {
    await setNMonthAvg({ month: '2024-04', N: 3, category: 'cat1' });
    await sheet.waitOnSpreadsheet();

    expect(await getSheetValue('budget202404', 'budget-cat1')).toBe(600);
  });

  it('sets a single category average from the first activity month', async () => {
    await setBudget({ category: 'cat1', month: '2023-12', amount: 1000 });

    await setNMonthAvg({ month: '2024-04', N: 3, category: 'cat1' });
    await sheet.waitOnSpreadsheet();

    expect(await getSheetValue('budget202404', 'budget-cat1')).toBe(600);
  });

  it('rounds a single category average to an integer amount', async () => {
    await db.insertTransaction({
      date: '2023-12-20',
      amount: -100,
      account: 'account1',
      category: 'cat1',
    });
    await sheet.waitOnSpreadsheet();

    await setNMonthAvg({ month: '2024-04', N: 3, category: 'cat1' });
    await sheet.waitOnSpreadsheet();

    expect(await getSheetValue('budget202404', 'budget-cat1')).toBe(633);
  });

  it('sets a bulk 3 month average from complete months', async () => {
    await set3MonthAvg({ month: '2024-04' });
    await sheet.waitOnSpreadsheet();

    expect(await getSheetValue('budget202404', 'budget-cat1')).toBe(600);
  });

  it('sets a bulk 3 month average from the first activity month', async () => {
    await setBudget({ category: 'cat1', month: '2023-12', amount: 1000 });

    await set3MonthAvg({ month: '2024-04' });
    await sheet.waitOnSpreadsheet();

    expect(await getSheetValue('budget202404', 'budget-cat1')).toBe(600);
  });
});

describe('getCategoryWeeklyAverage', () => {
  beforeEach(global.emptyDatabase());

  it('averages the complete weeks just before the budget month', async () => {
    await db.insertAccount({ id: 'account1', name: 'Account 1' });
    await db.insertCategoryGroup({ id: 'group1', name: 'group1' });
    await db.insertCategory({ id: 'cat1', name: 'cat1', cat_group: 'group1' });
    // 2 weeks before 2016-11 is 2016-10-18 through 2016-10-31; the days on
    // either side are outside the window.
    for (const [date, amount] of [
      ['2016-10-17', -1000],
      ['2016-10-18', -300],
      ['2016-10-31', -101],
      ['2016-11-01', -1000],
    ] as const) {
      await db.insertTransaction({
        date,
        amount,
        account: 'account1',
        category: 'cat1',
      });
    }

    expect(
      await getCategoryWeeklyAverage({
        month: '2016-11',
        numWeeks: 2,
        categoryId: 'cat1',
      }),
    ).toBe(-200.5);
  });
});

describe('coverOverbudgeted', () => {
  beforeEach(global.emptyDatabase());
  afterEach(global.emptyDatabase());

  it('fully covers the overbudget when category has sufficient leftover', async () => {
    // Setup: cat1 has 100 leftover, overbudget is 90
    await prepareDatabase();

    await coverOverbudgeted({
      month: '2024-02',
      category: 'cat1',
      currencyCode: 'USD',
    });
    await sheet.waitOnSpreadsheet();

    const sheetName = 'budget202402';
    expect(await getSheetValue(sheetName, 'to-budget')).toBe(0);
    expect(await getSheetValue(sheetName, 'leftover-cat1')).toBe(10);
  });

  it('partially covers the overbudget when category leftover is insufficient', async () => {
    // Setup: cat3 has 10 leftover, overbudget is 90
    await prepareDatabase();

    await coverOverbudgeted({
      month: '2024-02',
      category: 'cat3',
      currencyCode: 'USD',
    });
    await sheet.waitOnSpreadsheet();

    const sheetName = 'budget202402';
    expect(await getSheetValue(sheetName, 'to-budget')).toBe(-80);
    expect(await getSheetValue(sheetName, 'leftover-cat3')).toBe(0);
  });
});

describe('transferAvailable', () => {
  beforeEach(global.emptyDatabase());
  afterEach(global.emptyDatabase());

  // Setup: 2024-01 with 100.00 income, cat1 budgeted 10.00, so 90.00 to budget
  async function setupFundedDatabase() {
    await db.insertAccount({ id: 'account1', name: 'Account 1' });
    await db.insertCategoryGroup({
      id: 'income-group',
      name: 'Income',
      is_income: 1,
    });
    await db.insertCategory({
      id: 'income-cat',
      name: 'Income',
      cat_group: 'income-group',
      is_income: 1,
    });
    await db.insertCategoryGroup({
      id: 'group1',
      name: 'group1',
      is_income: 0,
    });
    await db.insertCategory({
      id: 'cat1',
      name: 'cat1',
      cat_group: 'group1',
      is_income: 0,
    });

    await sheet.loadSpreadsheet(db);
    await budget.createBudget(['2024-01']);

    await db.insertTransaction({
      date: '2024-01-15',
      amount: 10000,
      account: 'account1',
      category: 'income-cat',
    });
    await setBudget({ category: 'cat1', month: '2024-01', amount: 1000 });
    await sheet.waitOnSpreadsheet();
  }

  it('adds the full amount when it is within To Budget', async () => {
    await setupFundedDatabase();
    expect(await getSheetValue('budget202401', 'to-budget')).toBe(9000);

    await transferAvailable({
      month: '2024-01',
      amount: 3000,
      category: 'cat1',
    });
    await sheet.waitOnSpreadsheet();

    expect(await getSheetValue('budget202401', 'budget-cat1')).toBe(4000);
    expect(await getSheetValue('budget202401', 'to-budget')).toBe(6000);
  });

  it('caps the amount at To Budget so it never goes negative', async () => {
    await setupFundedDatabase();

    await transferAvailable({
      month: '2024-01',
      amount: 20000,
      category: 'cat1',
    });
    await sheet.waitOnSpreadsheet();

    expect(await getSheetValue('budget202401', 'budget-cat1')).toBe(10000);
    expect(await getSheetValue('budget202401', 'to-budget')).toBe(0);
  });

  it('does not change the budget when To Budget is not positive', async () => {
    // Setup: 2024-02 is 90 overbudgeted
    await prepareDatabase();

    await transferAvailable({
      month: '2024-02',
      amount: 50,
      category: 'cat1',
    });
    await sheet.waitOnSpreadsheet();

    expect(await getSheetValue('budget202402', 'budget-cat1')).toBe(0);
    expect(await getSheetValue('budget202402', 'to-budget')).toBe(-90);
  });

  it('never lowers the budget for a negative amount', async () => {
    await setupFundedDatabase();

    await transferAvailable({
      month: '2024-01',
      amount: -500,
      category: 'cat1',
    });
    await sheet.waitOnSpreadsheet();

    expect(await getSheetValue('budget202401', 'budget-cat1')).toBe(1000);
    expect(await getSheetValue('budget202401', 'to-budget')).toBe(9000);
  });
});

// Setup: 2024-02, is 90 overbudgeted
// with balances of cat1 = 100, cat2 = -20, cat3 = 10
async function prepareDatabase() {
  await db.insertCategoryGroup({
    id: 'income-group',
    name: 'Income',
    is_income: 1,
  });
  await db.insertCategory({
    id: 'income-cat',
    name: 'Income',
    cat_group: 'income-group',
    is_income: 1,
  });

  await db.insertCategoryGroup({ id: 'group1', name: 'group1', is_income: 0 });
  await db.insertCategory({
    id: 'cat1',
    name: 'cat1',
    cat_group: 'group1',
    is_income: 0,
  });
  await db.insertCategory({
    id: 'cat2',
    name: 'cat2',
    cat_group: 'group1',
    is_income: 0,
  });
  await db.insertCategory({
    id: 'cat3',
    name: 'cat3',
    cat_group: 'group1',
    is_income: 0,
  });

  await setBudget({ category: 'cat1', month: '2024-01', amount: 100 });
  await setBudget({ category: 'cat2', month: '2024-01', amount: -20 });
  await setBudget({ category: 'cat3', month: '2024-01', amount: 10 });

  await sheet.loadSpreadsheet(db);
  await budget.createBudget(['2024-01', '2024-02']);

  await setCategoryCarryover({
    startMonth: '2024-01',
    category: 'cat2',
    flag: true,
  });
  await sheet.waitOnSpreadsheet();
}

async function setupAverageDatabase() {
  await db.insertAccount({ id: 'account1', name: 'Account 1' });

  await db.insertCategoryGroup({
    id: 'income-group',
    name: 'Income',
    is_income: 1,
  });
  await db.insertCategoryGroup({ id: 'group1', name: 'group1', is_income: 0 });
  await db.insertCategory({
    id: 'cat1',
    name: 'cat1',
    cat_group: 'group1',
    is_income: 0,
  });

  await sheet.loadSpreadsheet(db);
  await budget.createBudget([
    '2023-11',
    '2023-12',
    '2024-01',
    '2024-02',
    '2024-03',
    '2024-04',
  ]);

  await db.insertTransaction({
    date: '2023-11-15',
    amount: -300,
    account: 'account1',
    category: 'cat1',
  });
  await db.insertTransaction({
    date: '2023-12-15',
    amount: -600,
    account: 'account1',
    category: 'cat1',
  });
  await db.insertTransaction({
    date: '2024-01-15',
    amount: -900,
    account: 'account1',
    category: 'cat1',
  });
  await db.insertTransaction({
    date: '2024-02-15',
    amount: -3000,
    account: 'account1',
    category: 'cat1',
  });
  await db.insertTransaction({
    date: '2024-03-15',
    amount: -1200,
    account: 'account1',
    category: 'cat1',
  });

  await sheet.waitOnSpreadsheet();
}