mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 17:03:15 +00:00
refactor(server): plan 4 — convert the program's orphaned raw-SQL callers
This commit is contained in:
@@ -1,57 +1,35 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
// prepare() has to answer per statement now: the resolver reads the addon row
|
||||
// AND the caller's role, and a single shared stub would hand the role lookup the
|
||||
// addon row (silently green tests for the #1772 gate).
|
||||
const { dbMock } = vi.hoisted(() => {
|
||||
const addonStmt = { get: vi.fn() };
|
||||
const roleStmt = { get: vi.fn() };
|
||||
const other = { get: vi.fn(), all: vi.fn(), run: vi.fn() };
|
||||
return {
|
||||
dbMock: {
|
||||
prepare: vi.fn((sql: string) => {
|
||||
if (sql.includes('FROM addons')) return addonStmt;
|
||||
if (sql.includes('FROM users')) return roleStmt;
|
||||
return other;
|
||||
}),
|
||||
_addon: addonStmt,
|
||||
_role: roleStmt,
|
||||
},
|
||||
};
|
||||
});
|
||||
vi.mock('../../../../src/db/database', () => ({ db: dbMock, closeDb: () => {}, reinitialize: () => {} }));
|
||||
|
||||
const isAddonEnabled = vi.fn();
|
||||
const findById = vi.fn();
|
||||
|
||||
import { LlmConfigResolver } from '../../../../src/nest/llm-parse/llm-config.resolver';
|
||||
import { DatabaseService } from '../../../../src/nest/database/database.service';
|
||||
import type { AddonsRepository } from '../../../../src/db/repositories/Addons.repository';
|
||||
import type { SettingsService } from '../../../../src/nest/settings/settings.service';
|
||||
import type { AddonsService } from '../../../../src/nest/addons/addons.service';
|
||||
|
||||
// The resolver injects SettingsService — a stub instance instead of the old
|
||||
// legacy-module path mock (same behaviors as before the DI move). The
|
||||
// DatabaseService rides the same prepare/get mock the module-level db used.
|
||||
// The resolver injects SettingsService — a stub instance (same behaviors as
|
||||
// before the DI move). Plan 4 Task 1: the addon-row read is now
|
||||
// `AddonsRepository.findById`, stubbed the same way — `config` comes back
|
||||
// already-parsed (the repository's own `p.json()` column), never a JSON
|
||||
// string this test has to stringify.
|
||||
const getUserSettings = vi.fn(() => ({}) as Record<string, unknown>);
|
||||
const getAdminUserDefaults = vi.fn(() => ({}) as Record<string, unknown>);
|
||||
const getDecryptedUserSetting = vi.fn(() => null as string | null);
|
||||
const settingsStub = { getUserSettings, getAdminUserDefaults, getDecryptedUserSetting } as unknown as SettingsService;
|
||||
|
||||
const addonsStub = { isAddonEnabled } as unknown as AddonsService;
|
||||
const resolver = new LlmConfigResolver(settingsStub, new DatabaseService(dbMock as never), addonsStub);
|
||||
const addonsRepoStub = { findById } as unknown as AddonsRepository;
|
||||
const resolver = new LlmConfigResolver(settingsStub, addonsRepoStub, addonsStub);
|
||||
|
||||
function setInstanceConfig(config: unknown) {
|
||||
dbMock._addon.get.mockReturnValue(config === undefined ? undefined : { config: JSON.stringify(config) });
|
||||
}
|
||||
|
||||
function setRole(role: 'user' | 'admin' | undefined) {
|
||||
dbMock._role.get.mockReturnValue(role === undefined ? undefined : { role });
|
||||
function setInstanceConfig(config: Record<string, unknown> | undefined) {
|
||||
findById.mockResolvedValue(config === undefined ? null : { id: 'llm_parsing', config });
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
isAddonEnabled.mockReturnValue(true);
|
||||
setInstanceConfig(undefined);
|
||||
setRole('user');
|
||||
getUserSettings.mockReturnValue({});
|
||||
getAdminUserDefaults.mockReturnValue({});
|
||||
getDecryptedUserSetting.mockReturnValue(null);
|
||||
@@ -61,6 +39,8 @@ describe('resolveLlmConfig', () => {
|
||||
it('returns null when the addon is disabled', async () => {
|
||||
isAddonEnabled.mockReturnValue(false);
|
||||
expect(await resolver.resolve(1)).toBeNull();
|
||||
// Gated BEFORE the addon-row read: a disabled addon never even queries it.
|
||||
expect(findById).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('uses instance config when present (and decrypts the key)', async () => {
|
||||
@@ -72,11 +52,11 @@ describe('resolveLlmConfig', () => {
|
||||
apiKey: 'sk-plain',
|
||||
multimodal: true,
|
||||
});
|
||||
expect(findById).toHaveBeenCalledWith('llm_parsing');
|
||||
});
|
||||
|
||||
it('instance config with a base URL still wins for a plain user (#1772 does not touch it)', async () => {
|
||||
setInstanceConfig({ provider: 'local', model: 'nuextract', baseUrl: 'http://ollama:11434' });
|
||||
setRole('user');
|
||||
expect(await resolver.resolve(7)).toMatchObject({ provider: 'local', baseUrl: 'http://ollama:11434' });
|
||||
});
|
||||
|
||||
@@ -99,6 +79,11 @@ describe('resolveLlmConfig', () => {
|
||||
expect(await resolver.resolve(1)).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null when the addon row has no config at all', async () => {
|
||||
setInstanceConfig(undefined);
|
||||
expect(await resolver.resolve(1)).toBeNull();
|
||||
});
|
||||
|
||||
// #1772: the endpoint is instance configuration, so it may only come from an
|
||||
// admin-controlled source, whoever is asking.
|
||||
it('#1772: picking local personally gets no config at all (no silent reroute)', async () => {
|
||||
@@ -126,13 +111,14 @@ describe('resolveLlmConfig', () => {
|
||||
expect(await resolver.resolve(7)).toMatchObject({ provider: 'local', baseUrl: 'http://ollama.internal:11434' });
|
||||
});
|
||||
|
||||
it('#1772: the caller\'s role does not change the answer, and no role is looked up', async () => {
|
||||
it('#1772: the caller\'s identity does not change the answer — the resolver never reads anything keyed by it besides the settings service', async () => {
|
||||
// An instance has one endpoint. An admin who parked one in their own row is
|
||||
// in exactly the same position as anyone else, and the resolver no longer
|
||||
// reads the users table at all.
|
||||
setRole('admin');
|
||||
// in exactly the same position as anyone else.
|
||||
getUserSettings.mockReturnValue({ llm_provider: 'local', llm_model: 'nuextract', llm_base_url: 'http://192.168.1.5:11434' });
|
||||
expect(await resolver.resolve(7)).toBeNull();
|
||||
expect(dbMock._role.get).not.toHaveBeenCalled();
|
||||
// The addon-row read is a single, userId-independent lookup — called once,
|
||||
// with no user-scoped argument.
|
||||
expect(findById).toHaveBeenCalledTimes(1);
|
||||
expect(findById).toHaveBeenCalledWith('llm_parsing');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user