@actual-app/api 6.3.0 → 6.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- /* eslint-disable import/no-unused-modules */
3
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
3
  if (k2 === undefined) k2 = k;
5
4
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -0,0 +1,9 @@
1
+ export const moduleFileExtensions: string[];
2
+ export const testEnvironment: string;
3
+ export const testPathIgnorePatterns: string[];
4
+ export const watchPathIgnorePatterns: string[];
5
+ export const setupFilesAfterEnv: string[];
6
+ export const transformIgnorePatterns: string[];
7
+ export const transform: {
8
+ '^.+\\.(t|j)sx?$': string;
9
+ };
@@ -0,0 +1,24 @@
1
+ module.exports = {
2
+ moduleFileExtensions: [
3
+ 'testing.js',
4
+ 'testing.ts',
5
+ 'api.js',
6
+ 'api.ts',
7
+ 'api.tsx',
8
+ 'electron.js',
9
+ 'electron.ts',
10
+ 'mjs',
11
+ 'js',
12
+ 'ts',
13
+ 'tsx',
14
+ 'json',
15
+ ],
16
+ testEnvironment: 'node',
17
+ testPathIgnorePatterns: ['/node_modules/'],
18
+ watchPathIgnorePatterns: ['<rootDir>/mocks/budgets/'],
19
+ setupFilesAfterEnv: ['<rootDir>/../loot-core/src/mocks/setup.ts'],
20
+ transformIgnorePatterns: ['/node_modules/'],
21
+ transform: {
22
+ '^.+\\.(t|j)sx?$': '@swc/jest',
23
+ },
24
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,231 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const fs = __importStar(require("fs/promises"));
27
+ const path = __importStar(require("path"));
28
+ const api = __importStar(require("./index"));
29
+ const budgetName = 'test-budget';
30
+ beforeEach(async () => {
31
+ // we need real datetime if we are going to mix new timestamps with our mock data
32
+ global.restoreDateNow();
33
+ const budgetPath = path.join(__dirname, '/mocks/budgets/', budgetName);
34
+ await fs.rm(budgetPath, { force: true, recursive: true });
35
+ await createTestBudget('default-budget-template', budgetName);
36
+ await api.init({
37
+ dataDir: path.join(__dirname, '/mocks/budgets/'),
38
+ });
39
+ });
40
+ afterEach(async () => {
41
+ global.currentMonth = null;
42
+ await api.shutdown();
43
+ });
44
+ async function createTestBudget(templateName, name) {
45
+ const templatePath = path.join(__dirname, '/../loot-core/src/mocks/files', templateName);
46
+ const budgetPath = path.join(__dirname, '/mocks/budgets/', name);
47
+ await fs.mkdir(budgetPath);
48
+ await fs.copyFile(path.join(templatePath, 'metadata.json'), path.join(budgetPath, 'metadata.json'));
49
+ await fs.copyFile(path.join(templatePath, 'db.sqlite'), path.join(budgetPath, 'db.sqlite'));
50
+ }
51
+ describe('API setup and teardown', () => {
52
+ // apis: loadBudget, getBudgetMonths
53
+ test('successfully loads budget', async () => {
54
+ await expect(api.loadBudget(budgetName)).resolves.toBeUndefined();
55
+ await expect(api.getBudgetMonths()).resolves.toMatchSnapshot();
56
+ });
57
+ });
58
+ describe('API CRUD operations', () => {
59
+ beforeEach(async () => {
60
+ // load test budget
61
+ await api.loadBudget(budgetName);
62
+ });
63
+ // apis: createCategoryGroup, updateCategoryGroup, deleteCategoryGroup
64
+ test('CategoryGroups: successfully update category groups', async () => {
65
+ const month = '2023-10';
66
+ global.currentMonth = month;
67
+ // create our test category group
68
+ const mainGroupId = await api.createCategoryGroup({
69
+ name: 'test-group',
70
+ });
71
+ let budgetMonth = await api.getBudgetMonth(month);
72
+ expect(budgetMonth.categoryGroups).toEqual(expect.arrayContaining([
73
+ expect.objectContaining({
74
+ id: mainGroupId,
75
+ }),
76
+ ]));
77
+ // update group
78
+ await api.updateCategoryGroup(mainGroupId, {
79
+ name: 'update-tests',
80
+ });
81
+ budgetMonth = await api.getBudgetMonth(month);
82
+ expect(budgetMonth.categoryGroups).toEqual(expect.arrayContaining([
83
+ expect.objectContaining({
84
+ id: mainGroupId,
85
+ }),
86
+ ]));
87
+ // delete group
88
+ await api.deleteCategoryGroup(mainGroupId);
89
+ budgetMonth = await api.getBudgetMonth(month);
90
+ expect(budgetMonth.categoryGroups).toEqual(expect.arrayContaining([
91
+ expect.not.objectContaining({
92
+ id: mainGroupId,
93
+ }),
94
+ ]));
95
+ });
96
+ // apis: createCategory, getCategories, updateCategory, deleteCategory
97
+ test('Categories: successfully update categories', async () => {
98
+ const month = '2023-10';
99
+ global.currentMonth = month;
100
+ // create our test category group
101
+ const mainGroupId = await api.createCategoryGroup({
102
+ name: 'test-group',
103
+ });
104
+ const secondaryGroupId = await api.createCategoryGroup({
105
+ name: 'test-secondary-group',
106
+ });
107
+ const categoryId = await api.createCategory({
108
+ name: 'test-budget',
109
+ group_id: mainGroupId,
110
+ });
111
+ const categoryIdHidden = await api.createCategory({
112
+ name: 'test-budget-hidden',
113
+ group_id: mainGroupId,
114
+ hidden: true,
115
+ });
116
+ let categories = await api.getCategories();
117
+ expect(categories).toEqual(expect.arrayContaining([
118
+ expect.objectContaining({
119
+ id: categoryId,
120
+ name: 'test-budget',
121
+ hidden: false,
122
+ group_id: mainGroupId,
123
+ }),
124
+ expect.objectContaining({
125
+ id: categoryIdHidden,
126
+ name: 'test-budget-hidden',
127
+ hidden: true,
128
+ group_id: mainGroupId,
129
+ }),
130
+ ]));
131
+ // update/move category
132
+ await api.updateCategory(categoryId, {
133
+ name: 'updated-budget',
134
+ group_id: secondaryGroupId,
135
+ });
136
+ await api.updateCategory(categoryIdHidden, {
137
+ name: 'updated-budget-hidden',
138
+ group_id: secondaryGroupId,
139
+ hidden: false,
140
+ });
141
+ categories = await api.getCategories();
142
+ expect(categories).toEqual(expect.arrayContaining([
143
+ expect.objectContaining({
144
+ id: categoryId,
145
+ name: 'updated-budget',
146
+ hidden: false,
147
+ group_id: secondaryGroupId,
148
+ }),
149
+ expect.objectContaining({
150
+ id: categoryIdHidden,
151
+ name: 'updated-budget-hidden',
152
+ hidden: false,
153
+ group_id: secondaryGroupId,
154
+ }),
155
+ ]));
156
+ // delete categories
157
+ await api.deleteCategory(categoryId);
158
+ expect(categories).toEqual(expect.arrayContaining([
159
+ expect.not.objectContaining({
160
+ id: categoryId,
161
+ }),
162
+ ]));
163
+ });
164
+ // apis: setBudgetAmount, setBudgetCarryover, getBudgetMonth
165
+ test('Budgets: successfully update budgets', async () => {
166
+ const month = '2023-10';
167
+ global.currentMonth = month;
168
+ // create some new categories to test with
169
+ const groupId = await api.createCategoryGroup({
170
+ name: 'tests',
171
+ });
172
+ const categoryId = await api.createCategory({
173
+ name: 'test-budget',
174
+ group_id: groupId,
175
+ });
176
+ await api.setBudgetAmount(month, categoryId, 100);
177
+ await api.setBudgetCarryover(month, categoryId, true);
178
+ const budgetMonth = await api.getBudgetMonth(month);
179
+ expect(budgetMonth.categoryGroups).toEqual(expect.arrayContaining([
180
+ expect.objectContaining({
181
+ id: groupId,
182
+ categories: expect.arrayContaining([
183
+ expect.objectContaining({
184
+ id: categoryId,
185
+ budgeted: 100,
186
+ carryover: true,
187
+ }),
188
+ ]),
189
+ }),
190
+ ]));
191
+ });
192
+ //apis: createAccount, getAccounts, updateAccount, closeAccount, deleteAccount, reopenAccount
193
+ test('Accounts: successfully complete account operators', async () => {
194
+ const accountId1 = await api.createAccount({ name: 'test-account1', offbudget: true }, 1000);
195
+ const accountId2 = await api.createAccount({ name: 'test-account2' }, 0);
196
+ let accounts = await api.getAccounts();
197
+ // accounts successfully created
198
+ expect(accounts).toEqual(expect.arrayContaining([
199
+ expect.objectContaining({
200
+ id: accountId1,
201
+ name: 'test-account1',
202
+ offbudget: true,
203
+ }),
204
+ expect.objectContaining({ id: accountId2, name: 'test-account2' }),
205
+ ]));
206
+ await api.updateAccount(accountId1, { offbudget: false });
207
+ await api.closeAccount(accountId1, accountId2, null);
208
+ await api.deleteAccount(accountId2);
209
+ // accounts successfully updated, and one of them deleted
210
+ accounts = await api.getAccounts();
211
+ expect(accounts).toEqual(expect.arrayContaining([
212
+ expect.objectContaining({
213
+ id: accountId1,
214
+ name: 'test-account1',
215
+ closed: true,
216
+ offbudget: false,
217
+ }),
218
+ expect.not.objectContaining({ id: accountId2 }),
219
+ ]));
220
+ await api.reopenAccount(accountId1);
221
+ // the non-deleted account is reopened
222
+ accounts = await api.getAccounts();
223
+ expect(accounts).toEqual(expect.arrayContaining([
224
+ expect.objectContaining({
225
+ id: accountId1,
226
+ name: 'test-account1',
227
+ closed: false,
228
+ }),
229
+ ]));
230
+ });
231
+ });
@@ -31,25 +31,25 @@ CREATE TABLE kvcache (key TEXT PRIMARY KEY, value TEXT);
31
31
  CREATE TABLE kvcache_key (id INTEGER PRIMARY KEY, key REAL);
32
32
  `);
33
33
  // Migrate budget amounts and carryover
34
- let budget = db.runQuery(`SELECT * FROM spreadsheet_cells WHERE name LIKE 'budget%!budget-%'`, [], true);
34
+ const budget = db.runQuery(`SELECT * FROM spreadsheet_cells WHERE name LIKE 'budget%!budget-%'`, [], true);
35
35
  db.transaction(() => {
36
36
  budget.forEach(monthBudget => {
37
- let match = monthBudget.name.match(/^(budget-report|budget)(\d+)!budget-(.+)$/);
37
+ const match = monthBudget.name.match(/^(budget-report|budget)(\d+)!budget-(.+)$/);
38
38
  if (match == null) {
39
39
  console.log('Warning: invalid budget month name', monthBudget.name);
40
40
  return;
41
41
  }
42
- let type = match[1];
43
- let month = match[2].slice(0, 4) + '-' + match[2].slice(4);
44
- let dbmonth = parseInt(match[2]);
45
- let cat = match[3];
42
+ const type = match[1];
43
+ const month = match[2].slice(0, 4) + '-' + match[2].slice(4);
44
+ const dbmonth = parseInt(match[2]);
45
+ const cat = match[3];
46
46
  let amount = parseInt(getValue(monthBudget));
47
47
  if (isNaN(amount)) {
48
48
  amount = 0;
49
49
  }
50
- let sheetName = monthBudget.name.split('!')[0];
51
- let carryover = db.runQuery('SELECT * FROM spreadsheet_cells WHERE name = ?', [`${sheetName}!carryover-${cat}`], true);
52
- let table = type === 'budget-report' ? 'reflect_budgets' : 'zero_budgets';
50
+ const sheetName = monthBudget.name.split('!')[0];
51
+ const carryover = db.runQuery('SELECT * FROM spreadsheet_cells WHERE name = ?', [`${sheetName}!carryover-${cat}`], true);
52
+ const table = type === 'budget-report' ? 'reflect_budgets' : 'zero_budgets';
53
53
  db.runQuery(`INSERT INTO ${table} (id, month, category, amount, carryover) VALUES (?, ?, ?, ?, ?)`, [
54
54
  `${month}-${cat}`,
55
55
  dbmonth,
@@ -60,12 +60,12 @@ CREATE TABLE kvcache_key (id INTEGER PRIMARY KEY, key REAL);
60
60
  });
61
61
  });
62
62
  // Migrate buffers
63
- let buffers = db.runQuery(`SELECT * FROM spreadsheet_cells WHERE name LIKE 'budget%!buffered'`, [], true);
63
+ const buffers = db.runQuery(`SELECT * FROM spreadsheet_cells WHERE name LIKE 'budget%!buffered'`, [], true);
64
64
  db.transaction(() => {
65
65
  buffers.forEach(buffer => {
66
- let match = buffer.name.match(/^budget(\d+)!buffered$/);
66
+ const match = buffer.name.match(/^budget(\d+)!buffered$/);
67
67
  if (match) {
68
- let month = match[1].slice(0, 4) + '-' + match[1].slice(4);
68
+ const month = match[1].slice(0, 4) + '-' + match[1].slice(4);
69
69
  let amount = parseInt(getValue(buffer));
70
70
  if (isNaN(amount)) {
71
71
  amount = 0;
@@ -75,10 +75,10 @@ CREATE TABLE kvcache_key (id INTEGER PRIMARY KEY, key REAL);
75
75
  });
76
76
  });
77
77
  // Migrate notes
78
- let notes = db.runQuery(`SELECT * FROM spreadsheet_cells WHERE name LIKE 'notes!%'`, [], true);
79
- let parseNote = str => {
78
+ const notes = db.runQuery(`SELECT * FROM spreadsheet_cells WHERE name LIKE 'notes!%'`, [], true);
79
+ const parseNote = str => {
80
80
  try {
81
- let value = JSON.parse(str);
81
+ const value = JSON.parse(str);
82
82
  return value && value !== '' ? value : null;
83
83
  }
84
84
  catch (e) {
@@ -87,9 +87,9 @@ CREATE TABLE kvcache_key (id INTEGER PRIMARY KEY, key REAL);
87
87
  };
88
88
  db.transaction(() => {
89
89
  notes.forEach(note => {
90
- let parsed = parseNote(getValue(note));
90
+ const parsed = parseNote(getValue(note));
91
91
  if (parsed) {
92
- let [, id] = note.name.split('!');
92
+ const [, id] = note.name.split('!');
93
93
  db.runQuery(`INSERT INTO notes (id, note) VALUES (?, ?)`, [id, parsed]);
94
94
  }
95
95
  });
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@actual-app/api",
3
- "version": "6.3.0",
3
+ "version": "6.4.0",
4
4
  "license": "MIT",
5
5
  "description": "An API for Actual",
6
6
  "engines": {
@@ -16,7 +16,8 @@
16
16
  "build:node": "tsc --p tsconfig.dist.json",
17
17
  "build:migrations": "cp migrations/*.sql dist/migrations",
18
18
  "build:default-db": "cp default-db.sqlite dist/",
19
- "build": "rm -rf dist && yarn run build:app && yarn run build:node && yarn run build:migrations && yarn run build:default-db"
19
+ "build": "rm -rf dist && yarn run build:app && yarn run build:node && yarn run build:migrations && yarn run build:default-db",
20
+ "test": "yarn run build:app && jest -c jest.config.js"
20
21
  },
21
22
  "dependencies": {
22
23
  "better-sqlite3": "^9.1.1",
@@ -25,7 +26,11 @@
25
26
  "uuid": "^9.0.0"
26
27
  },
27
28
  "devDependencies": {
29
+ "@swc/core": "^1.3.82",
30
+ "@swc/jest": "^0.2.29",
31
+ "@types/jest": "^27.5.0",
28
32
  "@types/uuid": "^9.0.2",
33
+ "jest": "^27.0.0",
29
34
  "typescript": "^5.0.2"
30
35
  }
31
36
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@actual-app/api",
3
- "version": "6.3.0",
3
+ "version": "6.4.0",
4
4
  "license": "MIT",
5
5
  "description": "An API for Actual",
6
6
  "engines": {
@@ -16,7 +16,8 @@
16
16
  "build:node": "tsc --p tsconfig.dist.json",
17
17
  "build:migrations": "cp migrations/*.sql dist/migrations",
18
18
  "build:default-db": "cp default-db.sqlite dist/",
19
- "build": "rm -rf dist && yarn run build:app && yarn run build:node && yarn run build:migrations && yarn run build:default-db"
19
+ "build": "rm -rf dist && yarn run build:app && yarn run build:node && yarn run build:migrations && yarn run build:default-db",
20
+ "test": "yarn run build:app && jest -c jest.config.js"
20
21
  },
21
22
  "dependencies": {
22
23
  "better-sqlite3": "^9.1.1",
@@ -25,7 +26,11 @@
25
26
  "uuid": "^9.0.0"
26
27
  },
27
28
  "devDependencies": {
29
+ "@swc/core": "^1.3.82",
30
+ "@swc/jest": "^0.2.29",
31
+ "@types/jest": "^27.5.0",
28
32
  "@types/uuid": "^9.0.2",
33
+ "jest": "^27.0.0",
29
34
  "typescript": "^5.0.2"
30
35
  }
31
36
  }