A local-first personal finance app
Tests for goal templates (#4882)
youngcw01c68bcfc70b
diff --git a/packages/loot-core/src/server/budget/categoryTemplate.test.ts b/packages/loot-core/src/server/budget/categoryTemplate.test.tsnew file mode 100644index 0000000000..797d5fbbea--- /dev/null+++ b/packages/loot-core/src/server/budget/categoryTemplate.test.ts@@ -0,0 +1,1028 @@+import { vi } from 'vitest';++import { amountToInteger } from '../../shared/util';+import { type CategoryEntity } from '../../types/models';+import * as db from '../db';++import * as actions from './actions';+import { CategoryTemplate } from './categoryTemplate';+import { type Template } from './types/templates';++// Mock getSheetValue and getCategories+vi.mock('./actions', () => ({+ getSheetValue: vi.fn(),+ getSheetBoolean: vi.fn(),+}));++vi.mock('../db', () => ({+ getCategories: vi.fn(),+}));++// Test helper class to access constructor and methods+class TestCategoryTemplate extends CategoryTemplate {+ public constructor(+ templates: Template[],+ category: CategoryEntity,+ month: string,+ fromLastMonth: number,+ budgeted: number,+ ) {+ super(templates, category, month, fromLastMonth, budgeted);+ }+}++describe('CategoryTemplate', () => {+ describe('runSimple', () => {+ it('should return monthly amount when provided', () => {+ const template: Template = {+ type: 'simple',+ monthly: 100,+ directive: 'template',+ priority: 1,+ };+ const limit = 0;++ const result = CategoryTemplate.runSimple(template, limit);+ expect(result).toBe(amountToInteger(100));+ });++ it('should return limit when monthly is not provided', () => {+ const template: Template = {+ type: 'simple',+ limit: { amount: 500, hold: false },+ directive: 'template',+ priority: 1,+ };++ const result = CategoryTemplate.runSimple(template, 500);+ expect(result).toBe(500);+ });++ it('should handle weekly limit', async () => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ const template: Template = {+ type: 'simple',+ limit: {+ amount: 100,+ hold: false,+ period: 'weekly',+ start: '2024-01-01',+ },+ directive: 'template',+ priority: 1,+ };+ const instance = new TestCategoryTemplate(+ [template],+ category,+ '2024-01',+ 0,+ 0,+ );+ const result = await instance.runTemplatesForPriority(1, 100000, 100000);+ expect(result).toBe(50000); // 5 Mondays * 100+ });++ it('should handle daily limit', async () => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ const template: Template = {+ type: 'simple',+ limit: { amount: 10, hold: false, period: 'daily' },+ directive: 'template',+ priority: 1,+ };+ const instance = new TestCategoryTemplate(+ [template],+ category,+ '2024-01',+ 0,+ 0,+ );+ const result = await instance.runTemplatesForPriority(1, 100000, 100000);+ expect(result).toBe(31000); // 31 days * 10+ });+ });++ describe('runCopy', () => {+ let instance: TestCategoryTemplate;++ beforeEach(() => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ instance = new TestCategoryTemplate([], category, '2024-01', 0, 0);+ vi.clearAllMocks();+ });++ it('should copy budget from previous month', async () => {+ const template: Template = {+ type: 'copy',+ lookBack: 1,+ directive: 'template',+ priority: 1,+ };++ vi.mocked(actions.getSheetValue).mockResolvedValue(100);++ const result = await CategoryTemplate.runCopy(template, instance);+ expect(result).toBe(100);+ });++ it('should copy budget from multiple months back', async () => {+ const template: Template = {+ type: 'copy',+ lookBack: 3,+ directive: 'template',+ priority: 1,+ };++ vi.mocked(actions.getSheetValue).mockResolvedValue(200);++ const result = await CategoryTemplate.runCopy(template, instance);+ expect(result).toBe(200);+ });++ it('should handle zero budget amount', async () => {+ const template: Template = {+ type: 'copy',+ lookBack: 1,+ directive: 'template',+ priority: 1,+ };++ vi.mocked(actions.getSheetValue).mockResolvedValue(0);++ const result = await CategoryTemplate.runCopy(template, instance);+ expect(result).toBe(0);+ });+ });++ describe('runWeek', () => {+ let instance: TestCategoryTemplate;++ beforeEach(() => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ instance = new TestCategoryTemplate([], category, '2024-01', 0, 0);+ });++ //5 mondays in January 2024+ it('should calculate weekly amount for single week', () => {+ const template: Template = {+ type: 'week',+ amount: 100,+ weeks: 1,+ starting: '2024-01-01',+ directive: 'template',+ priority: 1,+ };++ const result = CategoryTemplate.runWeek(template, instance);+ expect(result).toBe(amountToInteger(500));+ });++ it('should calculate weekly amount for multiple weeks', () => {+ const template: Template = {+ type: 'week',+ amount: 100,+ weeks: 2,+ starting: '2024-01-01',+ directive: 'template',+ priority: 1,+ };++ const result = CategoryTemplate.runWeek(template, instance);+ expect(result).toBe(amountToInteger(300));+ });++ it('should handle weeks spanning multiple months', () => {+ const template: Template = {+ type: 'week',+ amount: 100,+ weeks: 7,+ starting: '2023-12-04',+ directive: 'template',+ priority: 1,+ };++ const result = CategoryTemplate.runWeek(template, instance);+ expect(result).toBe(amountToInteger(100));+ });+ });++ describe('runSpend', () => {+ let instance: TestCategoryTemplate;++ beforeEach(() => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ instance = new TestCategoryTemplate([], category, '2024-01', 0, 0);+ vi.clearAllMocks();+ });++ it('should calculate monthly amount needed to reach target', async () => {+ const template: Template = {+ type: 'spend',+ amount: 1000,+ from: '2023-11',+ month: '2024-01',+ directive: 'template',+ priority: 1,+ };++ vi.mocked(actions.getSheetValue)+ .mockResolvedValueOnce(-10000) // spent in Nov+ .mockResolvedValueOnce(20000) // leftover in Nov+ .mockResolvedValueOnce(10000); // budgeted in Dec++ const result = await CategoryTemplate.runSpend(template, instance);+ expect(result).toBe(60000);+ });++ it('should handle repeating spend template', async () => {+ const template: Template = {+ type: 'spend',+ amount: 1000,+ from: '2023-11',+ month: '2023-12',+ //@ts-ignore this is what the template expects+ repeat: 3,+ directive: 'template',+ priority: 1,+ };++ vi.mocked(actions.getSheetValue)+ .mockResolvedValueOnce(-10000)+ .mockResolvedValueOnce(20000)+ .mockResolvedValueOnce(10000);++ const result = await CategoryTemplate.runSpend(template, instance);+ expect(result).toBe(33333);+ });++ it('should return zero for past target date', async () => {+ const template: Template = {+ type: 'spend',+ amount: 1000,+ from: '2023-12',+ month: '2023-12',+ directive: 'template',+ priority: 1,+ };++ const result = await CategoryTemplate.runSpend(template, instance);+ expect(result).toBe(0);+ });+ });++ describe('runPercentage', () => {+ let instance: TestCategoryTemplate;++ beforeEach(() => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ instance = new TestCategoryTemplate([], category, '2024-01', 0, 0);+ vi.clearAllMocks();+ });++ it('should calculate percentage of all income', async () => {+ const template: Template = {+ type: 'percentage',+ percent: 10,+ category: 'all income',+ previous: false,+ directive: 'template',+ priority: 1,+ };++ vi.mocked(actions.getSheetValue).mockResolvedValue(10000);++ const result = await CategoryTemplate.runPercentage(+ template,+ 0,+ instance,+ );+ expect(result).toBe(1000); // 10% of 10000+ });++ it('should calculate percentage of available funds', async () => {+ const template: Template = {+ type: 'percentage',+ percent: 20,+ category: 'available funds',+ previous: false,+ directive: 'template',+ priority: 1,+ };++ const result = await CategoryTemplate.runPercentage(+ template,+ 500,+ instance,+ );+ expect(result).toBe(100); // 20% of 500+ });++ it('should calculate percentage of specific income category', async () => {+ const template: Template = {+ type: 'percentage',+ percent: 15,+ category: 'Salary',+ previous: false,+ directive: 'template',+ priority: 1,+ };++ vi.mocked(db.getCategories).mockResolvedValue([+ {+ id: 'income1',+ name: 'Salary',+ is_income: 1,+ cat_group: 'income',+ sort_order: 1,+ hidden: 0,+ tombstone: 0,+ },+ ]);+ vi.mocked(actions.getSheetValue).mockResolvedValue(2000);++ const result = await CategoryTemplate.runPercentage(+ template,+ 0,+ instance,+ );+ expect(result).toBe(300); // 15% of 2000+ });++ it('should calculate percentage of previous month income', async () => {+ const template: Template = {+ type: 'percentage',+ percent: 10,+ category: 'all income',+ previous: true,+ directive: 'template',+ priority: 1,+ };++ vi.mocked(actions.getSheetValue).mockResolvedValue(10000);++ const result = await CategoryTemplate.runPercentage(+ template,+ 0,+ instance,+ );+ expect(result).toBe(1000); // 10% of 10000+ expect(actions.getSheetValue).toHaveBeenCalledWith(+ 'budget202312',+ 'total-income',+ );+ });+ });++ describe('runAverage', () => {+ let instance: TestCategoryTemplate;++ beforeEach(() => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ instance = new TestCategoryTemplate([], category, '2024-01', 0, 0);+ vi.clearAllMocks();+ });++ it('should calculate average of 3 months', async () => {+ const template: Template = {+ type: 'average',+ numMonths: 3,+ directive: 'template',+ priority: 1,+ };++ vi.mocked(actions.getSheetValue)+ .mockResolvedValueOnce(-100) // Dec 2023+ .mockResolvedValueOnce(-200) // Nov 2023+ .mockResolvedValueOnce(-300); // Oct 2023++ const result = await CategoryTemplate.runAverage(template, instance);+ expect(result).toBe(200); // Average of -100, -200, -300+ });++ it('should handle zero amounts', async () => {+ const template: Template = {+ type: 'average',+ numMonths: 3,+ directive: 'template',+ priority: 1,+ };++ vi.mocked(actions.getSheetValue)+ .mockResolvedValueOnce(0)+ .mockResolvedValueOnce(0)+ .mockResolvedValueOnce(-300);++ const result = await CategoryTemplate.runAverage(template, instance);+ expect(result).toBe(100);+ });++ it('should handle mixed positive and negative amounts', async () => {+ const template: Template = {+ type: 'average',+ numMonths: 3,+ directive: 'template',+ priority: 1,+ };++ vi.mocked(actions.getSheetValue)+ .mockResolvedValueOnce(-100)+ .mockResolvedValueOnce(200)+ .mockResolvedValueOnce(-300);++ const result = await CategoryTemplate.runAverage(template, instance);+ expect(result).toBe(67); // Average of -100, 200, -300+ });+ });++ describe('runBy', () => {+ let instance: TestCategoryTemplate;++ beforeEach(() => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ instance = new TestCategoryTemplate(+ [+ {+ type: 'by',+ amount: 1000,+ month: '2024-03',+ directive: 'template',+ priority: 1,+ },+ {+ type: 'by',+ amount: 2000,+ month: '2024-06',+ directive: 'template',+ priority: 1,+ },+ ],+ category,+ '2024-01',+ 0,+ 0,+ );+ });++ it('should calculate monthly amount needed for multiple targets', () => {+ const result = CategoryTemplate.runBy(instance);+ expect(result).toBe(66667);+ });++ it('should handle repeating targets', () => {+ instance = new TestCategoryTemplate(+ [+ {+ type: 'by',+ amount: 1000,+ month: '2023-03',+ repeat: 12,+ directive: 'template',+ priority: 1,+ },+ {+ type: 'by',+ amount: 2000,+ month: '2023-06',+ repeat: 12,+ directive: 'template',+ priority: 1,+ },+ ],+ instance.category,+ '2024-01',+ 0,+ 0,+ );++ const result = CategoryTemplate.runBy(instance);+ expect(result).toBe(83333);+ });++ it('should handle existing balance', () => {+ instance = new TestCategoryTemplate(+ [+ {+ type: 'by',+ amount: 1000,+ month: '2024-03',+ directive: 'template',+ priority: 1,+ },+ {+ type: 'by',+ amount: 2000,+ month: '2024-06',+ directive: 'template',+ priority: 1,+ },+ ],+ instance.category,+ '2024-01',+ 500,+ 0,+ );++ const result = CategoryTemplate.runBy(instance);+ expect(result).toBe(66500); // (1000 + 2000 - 5) / 3+ });+ });++ describe('template priorities', () => {+ it('should handle multiple templates with priorities and insufficient funds', async () => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ const templates: Template[] = [+ {+ type: 'simple',+ monthly: 100,+ directive: 'template',+ priority: 1,+ },+ {+ type: 'simple',+ monthly: 200,+ directive: 'template',+ priority: 1,+ },+ ];+ const instance = new TestCategoryTemplate(+ templates,+ category,+ '2024-01',+ 0,+ 0,+ );+ const result = await instance.runTemplatesForPriority(1, 150, 150);+ expect(result).toBe(150); // Max out at available funds+ });+ });++ describe('category limits', () => {+ it('should not budget over monthly limit', async () => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ const templates: Template[] = [+ {+ type: 'simple',+ monthly: 100,+ limit: { amount: 150, hold: false, period: 'monthly' },+ directive: 'template',+ priority: 1,+ },+ ];+ const instance = new TestCategoryTemplate(+ templates,+ category,+ '2024-01',+ 9000,+ 0,+ );+ const result = await instance.runTemplatesForPriority(1, 10000, 10000);+ expect(result).toBe(6000); //150 - 90+ });++ it('should handle hold flag when limit is reached', async () => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ const templates: Template[] = [+ {+ type: 'simple',+ monthly: 100,+ limit: { amount: 200, hold: true, period: 'monthly' },+ directive: 'template',+ priority: 1,+ },+ ];+ const instance = new TestCategoryTemplate(+ templates,+ category,+ '2024-01',+ 300,+ 0,+ );+ const result = await instance.getLimitExcess();+ expect(result).toBe(0);+ });++ it('should remove funds if over limit', async () => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ const templates: Template[] = [+ {+ type: 'simple',+ monthly: 100,+ limit: { amount: 200, hold: false, period: 'monthly' },+ directive: 'template',+ priority: 1,+ },+ ];+ const instance = new TestCategoryTemplate(+ templates,+ category,+ '2024-01',+ 30000,+ 0,+ );+ const result = await instance.getLimitExcess();+ expect(result).toBe(10000);+ });+ });++ describe('remainder templates', () => {+ it('should distribute available funds based on weight', async () => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ const templates: Template[] = [+ {+ type: 'remainder',+ weight: 2,+ directive: 'template',+ },+ ];+ const instance = new TestCategoryTemplate(+ templates,+ category,+ '2024-01',+ 0,+ 0,+ );+ const result = instance.runRemainder(100, 50);+ expect(result).toBe(100); // 2 * 50 = 100+ });++ it('remainder should handle last cent', async () => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ const templates: Template[] = [+ {+ type: 'remainder',+ weight: 1,+ directive: 'template',+ },+ ];+ const instance = new TestCategoryTemplate(+ templates,+ category,+ '2024-01',+ 0,+ 0,+ );+ const result = instance.runRemainder(101, 100);+ expect(result).toBe(101);+ });++ it('remainder wont over budget', async () => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ const templates: Template[] = [+ {+ type: 'remainder',+ weight: 1,+ directive: 'template',+ },+ ];+ const instance = new TestCategoryTemplate(+ templates,+ category,+ '2024-01',+ 0,+ 0,+ );+ const result = instance.runRemainder(99, 100);+ expect(result).toBe(99);+ });+ });++ describe('full process', () => {+ it('should handle priority limits through the entire process', async () => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ const templates: Template[] = [+ {+ type: 'simple',+ monthly: 100,+ directive: 'template',+ priority: 1,+ },+ {+ type: 'simple',+ monthly: 200,+ directive: 'template',+ priority: 2,+ },+ {+ type: 'remainder',+ weight: 1,+ directive: 'template',+ },+ ];++ // Mock the sheet values needed for init+ vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0); // lastMonthBalance+ vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false); // carryover++ // Initialize the template+ const instance = await CategoryTemplate.init(+ templates,+ category,+ '2024-01',+ 0,+ );++ // Run each priority level separately+ const priority1Result = await instance.runTemplatesForPriority(+ 1,+ 15000,+ 15000,+ );+ const priority2Result = await instance.runTemplatesForPriority(+ 2,+ 15000 - priority1Result,+ 15000,+ );++ // Get the final values+ const values = instance.getValues();++ // Verify the results+ expect(priority1Result).toBe(10000); // Should get full amount for priority 1+ expect(priority2Result).toBe(5000); // Should get remaining funds for priority 2+ expect(values.budgeted).toBe(15000); // Should match the total of both priorities+ expect(values.goal).toBe(30000); // Should be the sum of all template amounts+ expect(values.longGoal).toBe(null); // No goal template+ });++ it('should handle category limits through the entire process', async () => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ const templates: Template[] = [+ {+ type: 'simple',+ monthly: 100,+ directive: 'template',+ priority: 1,+ },+ {+ type: 'simple',+ monthly: 200,+ directive: 'template',+ priority: 1,+ },+ {+ type: 'simple',+ limit: { amount: 150, hold: false, period: 'monthly' },+ directive: 'template',+ priority: 1,+ },+ ];++ // Mock the sheet values needed for init+ vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0); // lastMonthBalance+ vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false); // carryover++ // Initialize the template+ const instance = await CategoryTemplate.init(+ templates,+ category,+ '2024-01',+ 0,+ );++ // Run the templates with more than enough funds+ const result = await instance.runTemplatesForPriority(1, 100000, 100000);++ // Get the final values+ const values = instance.getValues();++ // Verify the results+ expect(result).toBe(15000); // Should be limited by the category limit+ expect(values.budgeted).toBe(15000); // Should match the limit+ expect(values.goal).toBe(15000); // Should be the limit amount+ expect(values.longGoal).toBe(null); // No goal template+ });++ it('should handle remainder template at the end of the process', async () => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ const templates: Template[] = [+ {+ type: 'simple',+ monthly: 100,+ directive: 'template',+ priority: 1,+ },+ {+ type: 'simple',+ monthly: 200,+ directive: 'template',+ priority: 1,+ },+ {+ type: 'remainder',+ weight: 1,+ directive: 'template',+ },+ ];++ // Mock the sheet values needed for init+ vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0); // lastMonthBalance+ vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false); // carryover++ // Initialize the template+ const instance = await CategoryTemplate.init(+ templates,+ category,+ '2024-01',+ 0,+ );+ const weight = instance.getRemainderWeight();++ // Run the templates with more than enough funds+ const result = await instance.runTemplatesForPriority(1, 100000, 100000);++ // Run the remainder template+ const perWeight = (100000 - result) / weight;+ const remainderResult = instance.runRemainder(perWeight, perWeight);++ // Get the final values+ const values = instance.getValues();++ // Verify the results+ expect(result).toBe(30000); // Should get full amount for both simple templates+ expect(remainderResult).toBe(70000); // Should get remaining funds+ expect(values.budgeted).toBe(100000); // Should match the total of all templates+ expect(values.goal).toBe(30000); // Should be the sum of the simple templates+ expect(values.longGoal).toBe(null); // No goal template+ });++ it('should handle goal template through the entire process', async () => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ const templates: Template[] = [+ {+ type: 'simple',+ monthly: 100,+ directive: 'template',+ priority: 1,+ },+ {+ type: 'simple',+ monthly: 200,+ directive: 'template',+ priority: 1,+ },+ {+ type: 'goal',+ amount: 1000,+ directive: 'goal',+ },+ ];++ // Mock the sheet values needed for init+ vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0); // lastMonthBalance+ vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false); // carryover++ // Initialize the template+ const instance = await CategoryTemplate.init(+ templates,+ category,+ '2024-01',+ 0,+ );++ // Run the templates with more than enough funds+ const result = await instance.runTemplatesForPriority(1, 100000, 100000);++ // Get the final values+ const values = instance.getValues();++ // Verify the results+ expect(result).toBe(30000); // Should get full amount for both simple templates+ expect(values.budgeted).toBe(30000); // Should match the result+ expect(values.goal).toBe(100000); // Should be the goal amount+ expect(values.longGoal).toBe(true); // Should have a long goal+ expect(instance.getGoalOnly()).toBe(false); // Should not be goal only+ });++ it('should handle goal-only template through the entire process', async () => {+ const category: CategoryEntity = {+ id: 'test',+ name: 'Test Category',+ group: 'test-group',+ is_income: false,+ };+ const templates: Template[] = [+ {+ type: 'goal',+ amount: 1000,+ directive: 'goal',+ },+ ];++ // Mock the sheet values needed for init+ vi.mocked(actions.getSheetValue).mockResolvedValueOnce(10000); // lastMonthBalance+ vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false); // carryover++ // Initialize the template+ const instance = await CategoryTemplate.init(+ templates,+ category,+ '2024-01',+ 10000,+ );++ expect(instance.getGoalOnly()).toBe(true); // Should be goal only+ // Get the final values+ const values = instance.getValues();++ // Verify the results+ expect(values.budgeted).toBe(10000);+ expect(values.goal).toBe(100000); // Should be the goal amount+ expect(values.longGoal).toBe(true); // Should have a long goal+ });+ });+});diff --git a/packages/loot-core/src/server/budget/categoryTemplate.ts b/packages/loot-core/src/server/budget/categoryTemplate.tsindex deeaa5ffcd..fd90b632ac 100644--- a/packages/loot-core/src/server/budget/categoryTemplate.ts+++ b/packages/loot-core/src/server/budget/categoryTemplate.ts@@ -120,29 +120,33 @@ export class CategoryTemplate {let newBudget = 0;switch (t[i].type) {case 'simple': {- newBudget = this.runSimple(t[i], this.limitAmount);+ newBudget = CategoryTemplate.runSimple(t[i], this.limitAmount);break;}case 'copy': {- newBudget = await this.runCopy(t[i]);+ newBudget = await CategoryTemplate.runCopy(t[i], this);break;}case 'week': {- newBudget = this.runWeek(t[i]);+ newBudget = CategoryTemplate.runWeek(t[i], this);break;}case 'spend': {- newBudget = await this.runSpend(t[i]);+ newBudget = await CategoryTemplate.runSpend(t[i], this);break;}case 'percentage': {- newBudget = await this.runPercentage(t[i], availStart);+ newBudget = await CategoryTemplate.runPercentage(+ t[i],+ availStart,+ this,+ );break;}case 'by': {// all by's get run at onceif (!byFlag) {- newBudget = this.runBy();+ newBudget = CategoryTemplate.runBy(this);} else {newBudget = 0;}@@ -170,7 +174,7 @@ export class CategoryTemplate {break;}case 'average': {- newBudget = await this.runAverage(t[i]);+ newBudget = await CategoryTemplate.runAverage(t[i], this);break;}}@@ -206,16 +210,20 @@ export class CategoryTemplate {// run all of the 'remainder' type templatesrunRemainder(budgetAvail: number, perWeight: number) {if (this.remainder.length === 0) return 0;- const toBudget = Math.round(this.remainderWeight * perWeight);+ let toBudget = Math.round(this.remainderWeight * perWeight);//check possible overbudget from rounding, 1cent leftoverif (toBudget > budgetAvail) {- this.toBudgetAmount += budgetAvail;+ toBudget = budgetAvail;+ this.toBudgetAmount += toBudget;+ return toBudget;} else if (budgetAvail - toBudget === 1) {- this.toBudgetAmount += toBudget + 1;+ toBudget += 1;+ this.toBudgetAmount += toBudget;+ return toBudget;} else {this.toBudgetAmount += toBudget;+ return toBudget;}- return toBudget;}getValues(): { budgeted; goal; longGoal } {@@ -248,7 +256,7 @@ export class CategoryTemplate {private limitHold = false;readonly previouslyBudgeted: number = 0;- private constructor(+ protected constructor(templates: Template[],category: CategoryEntity,month: string,@@ -315,6 +323,12 @@ export class CategoryTemplate {//-----------------------------------------------------------------------------// Template Validationstatic async checkByAndScheduleAndSpend(templates, month) {+ if (+ templates.filter(t => t.type === 'schedule' || t.type === 'by').length ===+ 0+ ) {+ return;+ }//check schedule namesconst scheduleNames = (await getActiveSchedules()).map(({ name }) =>name.trim(),@@ -442,7 +456,7 @@ export class CategoryTemplate {//-----------------------------------------------------------------------------// Processor Functions- private runSimple(template, limit): number {+ static runSimple(template, limit): number {if (template.monthly != null) {return amountToInteger(template.monthly);} else {@@ -450,22 +464,22 @@ export class CategoryTemplate {}}- private async runCopy(template): Promise<number> {+ static async runCopy(template, catObj: CategoryTemplate): Promise<number> {const sheetName = monthUtils.sheetForMonth(- monthUtils.subMonths(this.month, template.lookBack),+ monthUtils.subMonths(catObj.month, template.lookBack),);- return await getSheetValue(sheetName, `budget-${this.category.id}`);+ return await getSheetValue(sheetName, `budget-${catObj.category.id}`);}- private runWeek(template): number {+ static runWeek(template, catObj: CategoryTemplate): number {let toBudget = 0;const amount = amountToInteger(template.amount);const weeks = template.weeks != null ? Math.round(template.weeks) : 1;let w = template.starting;- const nextMonth = monthUtils.addMonths(this.month, 1);+ const nextMonth = monthUtils.addMonths(catObj.month, 1);while (w < nextMonth) {- if (w >= this.month) {+ if (w >= catObj.month) {toBudget += amount;}w = monthUtils.addWeeks(w, weeks);@@ -473,28 +487,28 @@ export class CategoryTemplate {return toBudget;}- private async runSpend(template): Promise<number> {+ static async runSpend(template, catObj: CategoryTemplate): Promise<number> {let fromMonth = `${template.from}`;let toMonth = `${template.month}`;- let alreadyBudgeted = this.fromLastMonth;+ let alreadyBudgeted = catObj.fromLastMonth;let firstMonth = true;//update months if neededconst repeat = template.annual? (template.repeat || 1) * 12: template.repeat;- let m = monthUtils.differenceInCalendarMonths(toMonth, this.month);+ let m = monthUtils.differenceInCalendarMonths(toMonth, catObj.month);if (repeat && m < 0) {while (m < 0) {toMonth = monthUtils.addMonths(toMonth, repeat);fromMonth = monthUtils.addMonths(fromMonth, repeat);- m = monthUtils.differenceInCalendarMonths(toMonth, this.month);+ m = monthUtils.differenceInCalendarMonths(toMonth, catObj.month);}}for (let m = fromMonth;- monthUtils.differenceInCalendarMonths(this.month, m) > 0;+ monthUtils.differenceInCalendarMonths(catObj.month, m) > 0;m = monthUtils.addMonths(m, 1)) {const sheetName = monthUtils.sheetForMonth(m);@@ -502,25 +516,25 @@ export class CategoryTemplate {//TODO figure out if I already found these values and can pass them inconst spent = await getSheetValue(sheetName,- `sum-amount-${this.category.id}`,+ `sum-amount-${catObj.category.id}`,);const balance = await getSheetValue(sheetName,- `leftover-${this.category.id}`,+ `leftover-${catObj.category.id}`,);alreadyBudgeted = balance - spent;firstMonth = false;} else {alreadyBudgeted += await getSheetValue(sheetName,- `budget-${this.category.id}`,+ `budget-${catObj.category.id}`,);}}const numMonths = monthUtils.differenceInCalendarMonths(toMonth,- this.month,+ catObj.month,);const target = amountToInteger(template.amount);if (numMonths < 0) {@@ -530,7 +544,11 @@ export class CategoryTemplate {}}- private async runPercentage(template, availableFunds): Promise<number> {+ static async runPercentage(+ template,+ availableFunds,+ catObj: CategoryTemplate,+ ): Promise<number> {const percent = template.percent;const cat = template.category.toLowerCase();const prev = template.previous;@@ -539,9 +557,11 @@ export class CategoryTemplate {//choose the sheet to find income forif (prev) {- sheetName = monthUtils.sheetForMonth(monthUtils.subMonths(this.month, 1));+ sheetName = monthUtils.sheetForMonth(+ monthUtils.subMonths(catObj.month, 1),+ );} else {- sheetName = monthUtils.sheetForMonth(this.month);+ sheetName = monthUtils.sheetForMonth(catObj.month);}if (cat === 'all income') {monthlyIncome = await getSheetValue(sheetName, `total-income`);@@ -560,19 +580,19 @@ export class CategoryTemplate {return Math.max(0, Math.round(monthlyIncome * (percent / 100)));}- private async runAverage(template): Promise<number> {+ static async runAverage(template, catObj: CategoryTemplate): Promise<number> {let sum = 0;for (let i = 1; i <= template.numMonths; i++) {const sheetName = monthUtils.sheetForMonth(- monthUtils.subMonths(this.month, i),+ monthUtils.subMonths(catObj.month, i),);- sum += await getSheetValue(sheetName, `sum-amount-${this.category.id}`);+ sum += await getSheetValue(sheetName, `sum-amount-${catObj.category.id}`);}return -Math.round(sum / template.numMonths);}- private runBy(): number {- const byTemplates = this.templates.filter(t => t.type === 'by');+ static runBy(catObj: CategoryTemplate): number {+ const byTemplates = catObj.templates.filter(t => t.type === 'by');const savedInfo = [];let totalNeeded = 0;let shortNumMonths;@@ -587,13 +607,13 @@ export class CategoryTemplate {: null;let numMonths = monthUtils.differenceInCalendarMonths(targetMonth,- this.month,+ catObj.month,);while (numMonths < 0 && period) {targetMonth = monthUtils.addMonths(targetMonth, period);numMonths = monthUtils.differenceInCalendarMonths(targetMonth,- this.month,+ catObj.month,);}savedInfo.push({ numMonths, period });@@ -626,7 +646,7 @@ export class CategoryTemplate {totalNeeded += amount;}return Math.round(- (totalNeeded - this.fromLastMonth) / (shortNumMonths + 1),+ (totalNeeded - catObj.fromLastMonth) / (shortNumMonths + 1),);}}diff --git a/packages/loot-core/src/server/budget/types/templates.d.ts b/packages/loot-core/src/server/budget/types/templates.d.tsindex ec27922085..50ba2ea7c5 100644--- a/packages/loot-core/src/server/budget/types/templates.d.ts+++ b/packages/loot-core/src/server/budget/types/templates.d.ts@@ -16,7 +16,12 @@ interface WeekTemplate extends BaseTemplate {amount: number;weeks: number | null;starting: string;- limit?: { amount: number; hold: boolean };+ limit?: {+ amount: number;+ hold: boolean;+ period?: 'daily' | 'weekly' | 'monthly';+ start?: string;+ };}interface ByTemplate extends BaseTemplate {@@ -39,7 +44,12 @@ interface SpendTemplate extends BaseTemplate {interface SimpleTemplate extends BaseTemplate {type: 'simple';monthly?: number;- limit?: { amount: number; hold: boolean };+ limit?: {+ amount: number;+ hold: boolean;+ period?: 'daily' | 'weekly' | 'monthly';+ start?: string;+ };}interface ScheduleTemplate extends BaseTemplate {@@ -52,7 +62,12 @@ interface ScheduleTemplate extends BaseTemplate {interface RemainderTemplate extends BaseTemplate {type: 'remainder';weight: number;- limit?: { amount: number; hold: boolean };+ limit?: {+ amount: number;+ hold: boolean;+ period?: 'daily' | 'weekly' | 'monthly';+ start?: string;+ };}interface AverageTemplate extends BaseTemplate {diff --git a/upcoming-release-notes/4882.md b/upcoming-release-notes/4882.mdnew file mode 100644index 0000000000..0ab0e6d467--- /dev/null+++ b/upcoming-release-notes/4882.md@@ -0,0 +1,6 @@+---+category: Maintenance+authors: [youngcw]+---++Add tests to goal template processor functions