refactor(nest): inject DatabaseService in db-using controllers

This commit is contained in:
jubnl
2026-07-25 18:15:15 +02:00
parent fbe200424d
commit dd2c2d6668
16 changed files with 74 additions and 36 deletions
@@ -16,6 +16,8 @@ vi.mock('../../../src/db/database', () => ({
db: { prepare: () => ({ all: () => tripDays.value }) },
canAccessTrip,
}));
import { db as dbConn } from '../../../src/db/database';
import { DatabaseService } from '../../../src/nest/database/database.service';
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
import { DayScheduleController } from '../../../src/nest/plugins/day-schedule.controller';
@@ -28,7 +30,7 @@ function controller(invoke: (id: string) => unknown, providers = ['p1']) {
providersOf: vi.fn(() => providers),
invokeHook: vi.fn(async (id: string) => invoke(id)),
} as unknown as PluginRuntimeService;
return { c: new DayScheduleController(runtime), runtime };
return { c: new DayScheduleController(runtime, new DatabaseService(dbConn)), runtime };
}
const item = (over: Record<string, unknown> = {}) => ({ id: 's1', dayId: 10, label: 'Charging', ...over });
@@ -17,6 +17,8 @@ vi.mock('../../../src/db/database', () => ({
db: { prepare: () => ({ get: (entryId: number) => entryJourney(entryId) }) },
canAccessTrip: vi.fn(),
}));
import { db as dbConn } from '../../../src/db/database';
import { DatabaseService } from '../../../src/nest/database/database.service';
vi.mock('../../../src/services/journeyService', () => ({ canAccessJourney }));
vi.mock('../../../src/services/adminService', () => ({ isAddonEnabled }));
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
@@ -31,7 +33,7 @@ function controller(invoke: (id: string) => unknown, providers = ['p1']) {
providersOf: vi.fn(() => providers),
invokeHook: vi.fn(async (id: string) => invoke(id)),
} as unknown as PluginRuntimeService;
return { c: new JournalEntryRowsController(runtime), runtime };
return { c: new JournalEntryRowsController(runtime, new DatabaseService(dbConn)), runtime };
}
const row = (over: Record<string, unknown> = {}) => ({ label: 'Distance', value: '12 km', ...over });
@@ -9,6 +9,8 @@ vi.mock('../../../src/db/database', () => ({
db: { prepare: () => ({ get: (placeId: number) => placeTrip(placeId) }) },
canAccessTrip,
}));
import { db as dbConn } from '../../../src/db/database';
import { DatabaseService } from '../../../src/nest/database/database.service';
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
import { PlaceDetailsController } from '../../../src/nest/plugins/place-details.controller';
@@ -22,7 +24,7 @@ function controller(over: Partial<PluginRuntimeService> = {}) {
invokeHook: vi.fn(async (id: string) => (id === 'p2' ? [{ label: 'Rating', value: '4.5' }] : [{ label: 'Reviews', value: '12', url: 'https://x' }])),
...over,
} as unknown as PluginRuntimeService;
return { c: new PlaceDetailsController(runtime), runtime };
return { c: new PlaceDetailsController(runtime, new DatabaseService(dbConn)), runtime };
}
describe('PlaceDetailsController', () => {
@@ -12,6 +12,8 @@ const { pluginsEnabled, getMock } = vi.hoisted(() => ({
}));
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
vi.mock('../../../src/db/database', () => ({ db: { prepare: () => ({ get: getMock }) } }));
import { db as dbConn } from '../../../src/db/database';
import { DatabaseService } from '../../../src/nest/database/database.service';
import { PluginOAuthController } from '../../../src/nest/plugins/plugin-oauth.controller';
import type { PluginOAuthService } from '../../../src/nest/plugins/plugin-oauth.service';
@@ -30,7 +32,7 @@ function ctrl(over: Partial<PluginOAuthService> = {}) {
disconnect: vi.fn(),
...over,
} as unknown as PluginOAuthService;
return { c: new PluginOAuthController(svc), svc };
return { c: new PluginOAuthController(svc, new DatabaseService(dbConn)), svc };
}
describe('PluginOAuthController', () => {
@@ -16,6 +16,8 @@ vi.mock('../../../src/db/database', () => ({
db: { prepare: () => ({ get: () => (capabilitiesRow.value === undefined ? undefined : { capabilities: capabilitiesRow.value }) }) },
canAccessTrip,
}));
import { db as dbConn } from '../../../src/db/database';
import { DatabaseService } from '../../../src/nest/database/database.service';
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
import { PluginRoutesController } from '../../../src/nest/plugins/plugin-routes.controller';
@@ -28,7 +30,7 @@ function controller(invoke: () => unknown, providers = ['ev-plug']) {
providersOf: vi.fn(() => providers),
invokeHook: vi.fn(async () => invoke()),
} as unknown as PluginRuntimeService;
return { c: new PluginRoutesController(runtime), runtime };
return { c: new PluginRoutesController(runtime, new DatabaseService(dbConn)), runtime };
}
const wp = (n = 3) => Array.from({ length: n }, (_, i) => ({ lat: 48 + i * 0.1, lng: 2 + i * 0.1 }));
const goodRoute = (n = 3, over: Record<string, unknown> = {}) => ({
@@ -11,6 +11,8 @@ const { pluginsEnabled, getMock } = vi.hoisted(() => ({
}));
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
vi.mock('../../../src/db/database', () => ({ db: { prepare: () => ({ get: getMock }) } }));
import { db as dbConn } from '../../../src/db/database';
import { DatabaseService } from '../../../src/nest/database/database.service';
import { PluginUserSettingsController } from '../../../src/nest/plugins/plugin-user-settings.controller';
import type { PluginRuntimeService } from '../../../src/nest/plugins/plugin-runtime.service';
@@ -26,7 +28,7 @@ function ctrl() {
} as unknown as PluginsService;
// The controller now also takes the runtime (for settings-page actions).
const runtime = { actionsOf: () => [], invokeAction: vi.fn() } as unknown as PluginRuntimeService;
return { c: new PluginUserSettingsController(svc, runtime), svc, runtime };
return { c: new PluginUserSettingsController(svc, runtime, new DatabaseService(dbConn)), svc, runtime };
}
describe('PluginUserSettingsController', () => {
@@ -13,6 +13,8 @@ const { rows, pluginsEnabled } = vi.hoisted(() => ({
pluginsEnabled: vi.fn(() => true),
}));
vi.mock('../../../src/db/database', () => ({ db: { prepare: () => ({ all: () => rows.value }) } }));
import { db as dbConn } from '../../../src/db/database';
import { DatabaseService } from '../../../src/nest/database/database.service';
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
import { PluginsFeedController } from '../../../src/nest/plugins/plugins-feed.controller';
@@ -24,7 +26,7 @@ const row = (over: Record<string, unknown> = {}) => ({
});
describe('PluginsFeedController', () => {
const c = new PluginsFeedController();
const c = new PluginsFeedController(new DatabaseService(dbConn));
beforeEach(() => { pluginsEnabled.mockReturnValue(true); rows.value = []; });
it('returns an empty feed when the runtime is disabled', () => {