Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

test([memory-mgmt-new-endpoint-tests]): cover the 5 memory-management routes

[._.]/ Adam Eivy4c75dbd33f21
diff --git a/server/routes/localLlm.test.js b/server/routes/localLlm.test.js
index 77633c956d..b81e73ad04 100644
--- a/server/routes/localLlm.test.js
+++ b/server/routes/localLlm.test.js
@@ -3,7 +3,14 @@ import express from 'express';
import { request } from '../lib/testHelper.js';
import localLlmRoutes from './localLlm.js';
import { runLocalLlmTest, compareLocalLlmModels } from '../services/localLlmPlayground.js';
+import { getLoadedModels, unloadModel } from '../services/ollamaManager.js';
import { localLlmCompareSchema, localLlmTestSchema } from '../lib/validation.js';
+import { errorEvents } from '../lib/errorHandler.js';
+
+// asyncHandler emits to errorEvents on every route failure; with `io` set on
+// the app it always fires. Swallow it so a validation-rejection test doesn't
+// trip Node's "unhandled 'error' event" — assertions go through the response.
+errorEvents.on('error', () => {});
vi.mock('../services/localLlm.js', () => ({
getStatus: vi.fn(),
@@ -111,3 +118,74 @@ describe('local LLM playground routes', () => {
expect(parsed.success).toBe(false);
});
});
+
+describe('local LLM memory-management routes', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it('GET /loaded reports the models Ollama currently has resident', async () => {
+ getLoadedModels.mockResolvedValue([{ name: 'llama3.2', sizeVram: 4096 }]);
+
+ const res = await request(makeApp()).get('/api/local-llm/loaded');
+
+ expect(res.status).toBe(200);
+ expect(res.body).toEqual({ ollama: [{ name: 'llama3.2', sizeVram: 4096 }] });
+ expect(getLoadedModels).toHaveBeenCalledTimes(1);
+ });
+
+ it('POST /unload evicts a resident model and echoes the service result', async () => {
+ unloadModel.mockResolvedValue({ unloaded: true, modelId: 'llama3.2' });
+
+ const res = await request(makeApp())
+ .post('/api/local-llm/unload')
+ .send({ backend: 'ollama', modelId: 'llama3.2' });
+
+ expect(res.status).toBe(200);
+ expect(res.body).toEqual({ success: true, unloaded: true, modelId: 'llama3.2' });
+ expect(unloadModel).toHaveBeenCalledWith('llama3.2');
+ });
+
+ it('POST /unload treats an already-evicted model as an idempotent 200 no-op', async () => {
+ unloadModel.mockResolvedValue({ unloaded: false, reason: 'not loaded' });
+
+ const res = await request(makeApp())
+ .post('/api/local-llm/unload')
+ .send({ backend: 'ollama', modelId: 'llama3.2' });
+
+ expect(res.status).toBe(200);
+ expect(res.body).toEqual({ success: true, unloaded: false, reason: 'not loaded', modelId: 'llama3.2' });
+ });
+
+ it('POST /unload surfaces a genuine unload failure as 502', async () => {
+ unloadModel.mockResolvedValue({ unloaded: false, reason: 'Ollama unreachable' });
+
+ const res = await request(makeApp())
+ .post('/api/local-llm/unload')
+ .send({ backend: 'ollama', modelId: 'llama3.2' });
+
+ expect(res.status).toBe(502);
+ expect(res.body).toEqual({ error: 'Ollama unreachable', modelId: 'llama3.2' });
+ expect(unloadModel).toHaveBeenCalledWith('llama3.2');
+ });
+
+ it('POST /unload refuses a non-ollama backend before calling the service', async () => {
+ const res = await request(makeApp())
+ .post('/api/local-llm/unload')
+ .send({ backend: 'lmstudio', modelId: 'some-model' });
+
+ expect(res.status).toBe(400);
+ expect(res.body.error).toMatch(/use \/api\/lmstudio\/unload/);
+ expect(unloadModel).not.toHaveBeenCalled();
+ });
+
+ it('POST /unload rejects a flag-like modelId via Zod validation', async () => {
+ const res = await request(makeApp())
+ .post('/api/local-llm/unload')
+ .send({ backend: 'ollama', modelId: '-rf' });
+
+ expect(res.status).toBe(400);
+ expect(res.body.code).toBe('VALIDATION_ERROR');
+ expect(unloadModel).not.toHaveBeenCalled();
+ });
+});
diff --git a/server/routes/voice.test.js b/server/routes/voice.test.js
index 1bf690c113..96e5d3b74d 100644
--- a/server/routes/voice.test.js
+++ b/server/routes/voice.test.js
@@ -28,6 +28,13 @@ vi.mock('../services/voice/tts.js', () => ({
listVoices: vi.fn(),
VALID_ENGINES: new Set(['kokoro', 'piper']),
}));
+// GET /api/voice/tts/status + POST /api/voice/tts/unload destructure these at
+// module load — mock them so the route resolves without spinning up Kokoro.
+vi.mock('../services/voice/tts-kokoro.js', () => ({
+ readyState: vi.fn(() => 'lazy'),
+ unloadKokoro: vi.fn(() => ({ unloaded: false })),
+ loadedModelKey: vi.fn(() => null),
+}));
vi.mock('../services/voice/piper-voices.js', () => ({
findPiperVoice: vi.fn(),
}));
@@ -46,6 +53,7 @@ import * as tts from '../services/voice/tts.js';
import { ServerError } from '../lib/errorHandler.js';
import * as piperVoices from '../services/voice/piper-voices.js';
import * as proactiveSpeech from '../services/voice/proactiveSpeech.js';
+import * as kokoro from '../services/voice/tts-kokoro.js';
import voiceRoutes from './voice.js';
import { errorEvents } from '../lib/errorHandler.js';
@@ -312,4 +320,54 @@ describe('Voice Routes', () => {
expect(args.source).toBeUndefined();
});
});
+
+ describe('GET /api/voice/tts/status', () => {
+ it('returns the Kokoro residency snapshot', async () => {
+ kokoro.readyState.mockReturnValue('loaded');
+ kokoro.loadedModelKey.mockReturnValue('kokoro-v1:af_heart');
+ const res = await request(buildApp()).get('/api/voice/tts/status');
+ expect(res.status).toBe(200);
+ expect(res.body).toEqual({ kokoro: { state: 'loaded', loadedKey: 'kokoro-v1:af_heart' } });
+ });
+ });
+
+ describe('POST /api/voice/tts/unload', () => {
+ it('drops the cached Kokoro instance and echoes its result', async () => {
+ kokoro.unloadKokoro.mockReturnValue({ unloaded: true });
+ const res = await request(buildApp()).post('/api/voice/tts/unload');
+ expect(res.status).toBe(200);
+ expect(res.body).toEqual({ unloaded: true });
+ expect(kokoro.unloadKokoro).toHaveBeenCalledTimes(1);
+ });
+ });
+
+ describe('POST /api/voice/whisper', () => {
+ it('stops whisper and invalidates the health cache', async () => {
+ const res = await request(buildApp()).post('/api/voice/whisper').send({ action: 'stop' });
+ expect(res.status).toBe(200);
+ expect(res.body).toEqual({ success: true, action: 'stop' });
+ expect(bootstrap.stopWhisper).toHaveBeenCalledTimes(1);
+ expect(bootstrap.startWhisper).not.toHaveBeenCalled();
+ expect(health.invalidateHealthCache).toHaveBeenCalled();
+ });
+
+ it('starts whisper from the current config and returns the bootstrap result', async () => {
+ bootstrap.startWhisper.mockResolvedValue({ restarted: true });
+ const res = await request(buildApp()).post('/api/voice/whisper').send({ action: 'start' });
+ expect(res.status).toBe(200);
+ expect(res.body).toEqual({ success: true, action: 'start', restarted: true });
+ expect(config.getVoiceConfig).toHaveBeenCalled();
+ expect(bootstrap.startWhisper).toHaveBeenCalledWith(DEFAULT_CFG);
+ expect(bootstrap.stopWhisper).not.toHaveBeenCalled();
+ expect(health.invalidateHealthCache).toHaveBeenCalled();
+ });
+
+ it('rejects an invalid action with 400 without touching whisper', async () => {
+ const res = await request(buildApp()).post('/api/voice/whisper').send({ action: 'restart' });
+ expect(res.status).toBe(400);
+ expect(res.body.code).toBe('VALIDATION_ERROR');
+ expect(bootstrap.startWhisper).not.toHaveBeenCalled();
+ expect(bootstrap.stopWhisper).not.toHaveBeenCalled();
+ });
+ });
});