diff --git a/packages/loot-core/src/server/api.test.ts b/packages/loot-core/src/server/api.test.ts index acae5bec07..fabc1a35a0 100644 --- a/packages/loot-core/src/server/api.test.ts +++ b/packages/loot-core/src/server/api.test.ts @@ -1,33 +1,29 @@ import { getBankSyncError } from '../shared/errors'; -import type { ServerHandlers } from '../types/server-handlers'; -import { installAPI } from './api'; +import { app as apiApp } from './api'; + vi.mock('../shared/errors', () => ({ getBankSyncError: vi.fn(error => `Bank sync error: ${error}`), })); describe('API handlers', () => { - const handlers = installAPI({} as unknown as ServerHandlers); - describe('api/bank-sync', () => { it('should sync a single account when accountId is provided', async () => { - handlers['accounts-bank-sync'] = vi - .fn() - .mockResolvedValue({ errors: [] }); + apiApp['accounts-bank-sync'] = vi.fn().mockResolvedValue({ errors: [] }); - await handlers['api/bank-sync']({ accountId: 'account1' }); - expect(handlers['accounts-bank-sync']).toHaveBeenCalledWith({ + await apiApp['api/bank-sync']({ accountId: 'account1' }); + expect(apiApp['accounts-bank-sync']).toHaveBeenCalledWith({ ids: ['account1'], }); }); it('should handle errors in non batch sync', async () => { - handlers['accounts-bank-sync'] = vi.fn().mockResolvedValue({ + apiApp['accounts-bank-sync'] = vi.fn().mockResolvedValue({ errors: ['connection-failed'], }); await expect( - handlers['api/bank-sync']({ accountId: 'account2' }), + apiApp['api/bank-sync']({ accountId: 'account2' }), ).rejects.toThrow('Bank sync error: connection-failed'); expect(getBankSyncError).toHaveBeenCalledWith('connection-failed'); diff --git a/packages/loot-core/src/server/main.ts b/packages/loot-core/src/server/main.ts index 359e5a2fc9..6d40054493 100644 --- a/packages/loot-core/src/server/main.ts +++ b/packages/loot-core/src/server/main.ts @@ -9,7 +9,7 @@ import { q } from '../shared/query'; import type { QueryState } from '../shared/query'; import { amountToInteger, integerToAmount } from '../shared/util'; import type { ApiHandlers } from '../types/api-handlers'; -import type { Handlers } from '../types/handlers'; +import type { Handlers, ServerHandlers } from '../types/handlers'; import { app as accountsApp } from './accounts/app'; import { app as adminApp } from './admin/app'; @@ -110,7 +110,7 @@ async function appFocused() { } } -export type ServerHandlers = { +export type MiscHandlers = { undo: () => Promise; redo: () => Promise; @@ -136,7 +136,7 @@ export type ServerHandlers = { 'app-focused': () => Promise; }; -const serverApp = createApp({ +const miscApp = createApp({ undo: mutator(undo), redo: mutator(redo), 'make-filters-from-conditions': makeFiltersFromConditions, @@ -147,15 +147,14 @@ const serverApp = createApp({ 'app-focused': appFocused, }); -// Main app -export const mainApp = createApp(); +const serverApp = createApp(); -mainApp.events.on('sync', event => { +serverApp.events.on('sync', event => { connection.send('sync-event', event); }); -mainApp.combine( - serverApp, +serverApp.combine( + miscApp, authApp, schedulesApp, budgetApp, @@ -177,6 +176,9 @@ mainApp.combine( tagsApp, ); +export const mainApp = createApp(); +mainApp.combine(apiApp, serverApp); + export function getDefaultDocumentDir() { return fs.join(process.env.ACTUAL_DOCUMENT_DIR, 'Actual'); } @@ -337,9 +339,6 @@ export async function init(config: InitConfig) { // Export a few things required for the platform -const combinedApp = createApp(); -combinedApp.combine(apiApp, mainApp); - export const lib = { getDataDir: fs.getDataDir, sendMessage: (msg, args) => connection.send(msg, args), @@ -347,10 +346,10 @@ export const lib = { name: K, args?: Parameters[0], ): Promise>> => { - const res = await combinedApp.runHandler(name, args); + const res = await mainApp.runHandler(name, args); return res as Awaited>; }, - on: (name, func) => combinedApp.events.on(name, func), + on: (name, func) => mainApp.events.on(name, func), q, db, amountToInteger, diff --git a/packages/loot-core/src/types/handlers.ts b/packages/loot-core/src/types/handlers.ts index fbd5f8062d..5926e50388 100644 --- a/packages/loot-core/src/types/handlers.ts +++ b/packages/loot-core/src/types/handlers.ts @@ -6,7 +6,7 @@ import type { BudgetFileHandlers } from '../server/budgetfiles/app'; import type { DashboardHandlers } from '../server/dashboard/app'; import type { EncryptionHandlers } from '../server/encryption/app'; import type { FiltersHandlers } from '../server/filters/app'; -import type { ServerHandlers } from '../server/main'; +import type { MiscHandlers } from '../server/main'; import type { NotesHandlers } from '../server/notes/app'; import type { PayeeHandlers } from '../server/payees/app'; import type { PreferencesHandlers } from '../server/preferences/app'; @@ -19,7 +19,9 @@ import type { TagsHandlers } from '../server/tags/app'; import type { ToolsHandlers } from '../server/tools/app'; import type { TransactionHandlers } from '../server/transactions/app'; -export type Handlers = {} & ServerHandlers & +import type { ApiHandlers } from './api-handlers'; + +export type ServerHandlers = MiscHandlers & BudgetHandlers & DashboardHandlers & FiltersHandlers & @@ -40,4 +42,6 @@ export type Handlers = {} & ServerHandlers & TagsHandlers & AuthHandlers; +export type Handlers = {} & ServerHandlers & ApiHandlers; + export type HandlerFunctions = Handlers[keyof Handlers];