mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-22 13:59:33 +01:00
feat(local-model): install a model from the server, with a job that can pause
POST /api/local-model/install, cancel, resume and runtime, and DELETE /api/local-model. The GET gains an `install` object the window polls. All of it is experimental and admin only in multi-user mode. One job of five steps (runtime, model, verify, agent, connect) driven by a pure reducer over explicit state, step and transition tables. One job at a time (409). The consent is bound to the size shown: `acknowledgedBytes` must be the server's own total (409 size-changed). A job a server restart interrupted comes back paused and never continues without a click. Every failure is a code with one next step. The model is judged against the budget the runtime measured before it is downloaded. A missing coding agent is installed with its stock command through the shared runner, audited. Connect writes one endpoint record under the reserved id through mutateCustomModelHosts() and switches customModelEndpointsEnabled on through mutateSettingsFile(), remembering the flag's value. The model's digest is stored and read again at boot and before every session start on that endpoint; a changed model refuses the launch. Remove answers 409 with the session names while a live session uses the endpoint, undoes the two writes first, restores the flag only when this feature switched it on and no other endpoint is saved, and deletes only inside local-model/. The coding agent stays. The server starts the runtime at boot when a model is installed and stops it at shutdown beside stopDeepSeekWeb(). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
a864acc52c
commit
3af94df4f6
17 files changed
+6290
-156
No files matched your search
@@ -0,0 +1,304 @@
|
||||
/**
|
||||
* @fileoverview The launch guard of the endpoint the local model install manages: both ways a
|
||||
* session gets onto a custom model endpoint ask the install service first.
|
||||
*
|
||||
* - `POST /api/sessions/:id/custom-model` (a running session switches to the endpoint), and
|
||||
* - `POST /api/quick-start` with `customModel` (a session is created on it)
|
||||
*
|
||||
* call `LocalModelService.launchRefusalMessage(endpointId, modelId)` once the endpoint is
|
||||
* found and before anything is written, restarted or created. A sentence back is the answer
|
||||
* (`OPERATION_FAILED`); `undefined` lets the launch go on exactly as before. That is how a
|
||||
* model whose digest is no longer the one stored at install time is not launched, and how
|
||||
* nothing is launched against a port our runtime is not on.
|
||||
*
|
||||
* The service is replaced by a fake for most cases, and is the REAL one (over the real state
|
||||
* file, with no runtime running) for the cases that pin the default wiring.
|
||||
*
|
||||
* Port: N/A (app.inject, no real port needed)
|
||||
*/
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import Fastify, { type FastifyInstance } from 'fastify';
|
||||
import fastifyCookie from '@fastify/cookie';
|
||||
import { rmSync, writeFileSync } from 'node:fs';
|
||||
import { rm } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
import { createMockRouteContext, safeRmHomeTree, type MockRouteContext } from '../mocks/index.js';
|
||||
import { createRouteTestHarness } from './_route-test-utils.js';
|
||||
import { installRouteErrorHandler } from '../../src/web/route-error-handler.js';
|
||||
import { registerSessionRoutes } from '../../src/web/routes/session-routes.js';
|
||||
import { dataPath, getDataDir } from '../../src/config/instance.js';
|
||||
import { CASES_DIR } from '../../src/web/route-helpers.js';
|
||||
import { Session } from '../../src/session.js';
|
||||
import { writeCustomModelHosts, type CustomModelHost } from '../../src/custom-model-hosts.js';
|
||||
import { webviewFetch } from '../../src/web/webview-egress.js';
|
||||
import type { LocalModelService } from '../../src/local-model/install-job.js';
|
||||
|
||||
vi.mock('../../src/web/webview-egress.js', async () => {
|
||||
const actual = await vi.importActual<typeof import('../../src/web/webview-egress.js')>(
|
||||
'../../src/web/webview-egress.js'
|
||||
);
|
||||
return { ...actual, webviewFetch: vi.fn() };
|
||||
});
|
||||
const fetchMock = vi.mocked(webviewFetch);
|
||||
|
||||
// See test/routes/quick-start-custom-model.test.ts: the CLI availability gate runs before the
|
||||
// code under test and would refuse every launch on a machine without the binary.
|
||||
vi.mock('../../src/utils/cli-launcher.js', async () => {
|
||||
const actual = await vi.importActual<typeof import('../../src/utils/cli-launcher.js')>(
|
||||
'../../src/utils/cli-launcher.js'
|
||||
);
|
||||
return { ...actual, resolveCliLaunchError: vi.fn().mockResolvedValue(null) };
|
||||
});
|
||||
|
||||
/** The service the session routes get: a fake by default, the real one where a test says so. */
|
||||
const guard = vi.hoisted(() => ({
|
||||
launchRefusalMessage: vi.fn<(endpointId: string, modelId: string) => Promise<string | undefined>>(),
|
||||
real: undefined as LocalModelService | undefined,
|
||||
}));
|
||||
vi.mock('../../src/local-model/install-job.js', async () => {
|
||||
const actual = await vi.importActual<typeof import('../../src/local-model/install-job.js')>(
|
||||
'../../src/local-model/install-job.js'
|
||||
);
|
||||
return {
|
||||
...actual,
|
||||
getLocalModelService: () => guard.real ?? { launchRefusalMessage: guard.launchRefusalMessage },
|
||||
};
|
||||
});
|
||||
|
||||
const MANAGED: CustomModelHost = {
|
||||
id: 'local-ollama',
|
||||
label: 'This Mac',
|
||||
baseUrl: 'http://127.0.0.1:11435',
|
||||
authStyle: 'bearer',
|
||||
models: ['qwen3.6:35b-a3b'],
|
||||
defaultModelId: 'qwen3.6:35b-a3b',
|
||||
defaultContextLength: 131072,
|
||||
};
|
||||
|
||||
const OTHER: CustomModelHost = { id: 'ep1', label: 'llama.cpp box', baseUrl: 'http://192.168.1.50:8080', apiKey: 'k' };
|
||||
|
||||
const CHANGED = 'The model on this Mac changed since it was installed. Remove it and install it again.';
|
||||
const STATE = () => dataPath('local-model.json');
|
||||
|
||||
/** The state file of an installed model whose connection is the managed record. */
|
||||
function writeInstalledState(): void {
|
||||
writeFileSync(
|
||||
STATE(),
|
||||
JSON.stringify({
|
||||
schema: 1,
|
||||
state: 'installed',
|
||||
model: { id: 'qwen3.6:35b-a3b', label: 'Qwen3.6 35B-A3B', context: 131072, digest: 'abc123' },
|
||||
cli: { id: 'opencode', label: 'OpenCode' },
|
||||
runtime: { version: '0.40.15', port: 11435, enabled: true },
|
||||
connection: { endpointId: 'local-ollama', flagWasEnabled: false },
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
async function realService(): Promise<LocalModelService> {
|
||||
const actual = await vi.importActual<typeof import('../../src/local-model/install-job.js')>(
|
||||
'../../src/local-model/install-job.js'
|
||||
);
|
||||
// The real service with its real state file. No runtime was ever started by it.
|
||||
return actual.createLocalModelService();
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
guard.launchRefusalMessage.mockReset();
|
||||
guard.launchRefusalMessage.mockResolvedValue(undefined);
|
||||
guard.real = undefined;
|
||||
fetchMock.mockReset();
|
||||
fetchMock.mockResolvedValue(new Response('not found', { status: 404 }));
|
||||
await writeCustomModelHosts(getDataDir(), [MANAGED, OTHER]);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
rmSync(STATE(), { force: true });
|
||||
await rm(join(getDataDir(), 'custom-model-hosts.json'), { force: true });
|
||||
await rm(join(getDataDir(), 'custom-model-configs'), { recursive: true, force: true });
|
||||
});
|
||||
|
||||
describe('POST /api/sessions/:id/custom-model on the managed endpoint', () => {
|
||||
async function setup() {
|
||||
const harness = await createRouteTestHarness(registerSessionRoutes);
|
||||
const session = harness.ctx.sessions.get('test-session-1')!;
|
||||
session.mode = 'claude';
|
||||
return { ...harness, session };
|
||||
}
|
||||
|
||||
const apply = (app: FastifyInstance, payload: Record<string, unknown>) =>
|
||||
app.inject({ method: 'POST', url: '/api/sessions/test-session-1/custom-model', payload });
|
||||
|
||||
it('asks the install service with the endpoint and the model, before anything is applied', async () => {
|
||||
const { app, session } = await setup();
|
||||
const res = await apply(app, { endpointId: 'local-ollama', modelId: 'qwen3.6:35b-a3b' });
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(guard.launchRefusalMessage).toHaveBeenCalledTimes(1);
|
||||
expect(guard.launchRefusalMessage).toHaveBeenCalledWith('local-ollama', 'qwen3.6:35b-a3b');
|
||||
// No refusal: the model is applied and the CLI restarted, as for any endpoint.
|
||||
expect(session.setCustomModel).toHaveBeenCalledTimes(1);
|
||||
expect(session.restartCli).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('a changed model refuses the switch in one plain sentence, and nothing is written or restarted', async () => {
|
||||
const { app, session } = await setup();
|
||||
guard.launchRefusalMessage.mockResolvedValue(CHANGED);
|
||||
const res = await apply(app, { endpointId: 'local-ollama', modelId: 'qwen3.6:35b-a3b' });
|
||||
expect(res.json()).toEqual({ success: false, error: CHANGED, errorCode: 'OPERATION_FAILED' });
|
||||
expect(session.setCustomModel).not.toHaveBeenCalled();
|
||||
expect(session.restartCli).not.toHaveBeenCalled();
|
||||
// The runtime was not even asked what it has loaded.
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('every other endpoint is asked about too, and the service lets it through', async () => {
|
||||
const { app, session } = await setup();
|
||||
const res = await apply(app, { endpointId: 'ep1', modelId: 'qwen3' });
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(guard.launchRefusalMessage).toHaveBeenCalledWith('ep1', 'qwen3');
|
||||
expect(session.setCustomModel).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('clearing a selection is never refused: it is the way OFF the endpoint', async () => {
|
||||
const { app, session } = await setup();
|
||||
guard.launchRefusalMessage.mockResolvedValue(CHANGED);
|
||||
session.setCustomModel.mockReturnValue({ previousConfigDir: undefined });
|
||||
const res = await apply(app, { clear: true });
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(guard.launchRefusalMessage).not.toHaveBeenCalled();
|
||||
expect(session.setCustomModel).toHaveBeenCalledWith(undefined);
|
||||
});
|
||||
|
||||
it('an endpoint that does not exist is still a 404 of its own, asked about nothing', async () => {
|
||||
const { app } = await setup();
|
||||
const res = await apply(app, { endpointId: 'nope', modelId: 'x' });
|
||||
expect(res.json()).toMatchObject({ success: false, errorCode: 'NOT_FOUND' });
|
||||
expect(guard.launchRefusalMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
describe('with the real install service', () => {
|
||||
it('an installed model whose runtime is not running is not switched to', async () => {
|
||||
writeInstalledState();
|
||||
guard.real = await realService();
|
||||
const { app, session } = await setup();
|
||||
const res = await apply(app, { endpointId: 'local-ollama', modelId: 'qwen3.6:35b-a3b' });
|
||||
expect(res.json()).toEqual({
|
||||
success: false,
|
||||
error: 'The model on this Mac is not running. Start it first.',
|
||||
errorCode: 'OPERATION_FAILED',
|
||||
});
|
||||
expect(session.setCustomModel).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('a record with the reserved id that nobody installed is an endpoint like any other', async () => {
|
||||
// No state file: this feature wrote nothing, so there is nothing of ours to guard.
|
||||
guard.real = await realService();
|
||||
const { app, session } = await setup();
|
||||
const res = await apply(app, { endpointId: 'local-ollama', modelId: 'qwen3.6:35b-a3b' });
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(session.setCustomModel).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('any other endpoint is untouched by an installed model', async () => {
|
||||
writeInstalledState();
|
||||
guard.real = await realService();
|
||||
const { app, session } = await setup();
|
||||
const res = await apply(app, { endpointId: 'ep1', modelId: 'qwen3' });
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(session.setCustomModel).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/quick-start with customModel on the managed endpoint', () => {
|
||||
let app: FastifyInstance;
|
||||
let ctx: MockRouteContext;
|
||||
|
||||
const quickStart = (payload: Record<string, unknown>) =>
|
||||
app.inject({ method: 'POST', url: '/api/quick-start', payload });
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.spyOn(Session.prototype, 'startInteractive').mockResolvedValue(undefined);
|
||||
vi.spyOn(Session.prototype, 'startShell').mockResolvedValue(undefined);
|
||||
vi.spyOn(Session.prototype, 'restartCli').mockResolvedValue(true);
|
||||
app = Fastify({ logger: false });
|
||||
await app.register(fastifyCookie);
|
||||
ctx = createMockRouteContext();
|
||||
registerSessionRoutes(app, ctx);
|
||||
installRouteErrorHandler(app);
|
||||
await app.ready();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await app.close();
|
||||
vi.restoreAllMocks();
|
||||
safeRmHomeTree(CASES_DIR);
|
||||
});
|
||||
|
||||
it('asks the install service, and with no refusal the session starts on the local model', async () => {
|
||||
const before = ctx.sessions.size;
|
||||
const res = await quickStart({
|
||||
caseName: 'lm-ok',
|
||||
mode: 'claude',
|
||||
customModel: { endpointId: 'local-ollama', modelId: 'qwen3.6:35b-a3b' },
|
||||
});
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.json().sessionId).toBeTruthy();
|
||||
expect(guard.launchRefusalMessage).toHaveBeenCalledWith('local-ollama', 'qwen3.6:35b-a3b');
|
||||
expect(ctx.sessions.size).toBe(before + 1);
|
||||
const session = ctx.sessions.get(res.json().sessionId) as unknown as Session;
|
||||
expect(session.customModel).toMatchObject({ endpointId: 'local-ollama', modelId: 'qwen3.6:35b-a3b' });
|
||||
});
|
||||
|
||||
it('a changed model refuses the launch in one plain sentence, and no session is created', async () => {
|
||||
guard.launchRefusalMessage.mockResolvedValue(CHANGED);
|
||||
const before = ctx.sessions.size;
|
||||
const res = await quickStart({
|
||||
caseName: 'lm-changed',
|
||||
mode: 'claude',
|
||||
customModel: { endpointId: 'local-ollama', modelId: 'qwen3.6:35b-a3b' },
|
||||
});
|
||||
expect(res.json()).toEqual({ success: false, error: CHANGED, errorCode: 'OPERATION_FAILED' });
|
||||
expect(ctx.sessions.size).toBe(before);
|
||||
expect(Session.prototype.startInteractive).not.toHaveBeenCalled();
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('a launch without a custom model never asks', async () => {
|
||||
const res = await quickStart({ caseName: 'lm-plain', mode: 'claude' });
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(guard.launchRefusalMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
describe('with the real install service', () => {
|
||||
it('an installed model whose runtime is not running is not launched', async () => {
|
||||
writeInstalledState();
|
||||
guard.real = await realService();
|
||||
const before = ctx.sessions.size;
|
||||
const res = await quickStart({
|
||||
caseName: 'lm-stopped',
|
||||
mode: 'claude',
|
||||
customModel: { endpointId: 'local-ollama', modelId: 'qwen3.6:35b-a3b' },
|
||||
});
|
||||
expect(res.json()).toEqual({
|
||||
success: false,
|
||||
error: 'The model on this Mac is not running. Start it first.',
|
||||
errorCode: 'OPERATION_FAILED',
|
||||
});
|
||||
expect(ctx.sessions.size).toBe(before);
|
||||
});
|
||||
|
||||
it('a launch on any other endpoint goes on as before', async () => {
|
||||
writeInstalledState();
|
||||
guard.real = await realService();
|
||||
const res = await quickStart({
|
||||
caseName: 'lm-other',
|
||||
mode: 'claude',
|
||||
customModel: { endpointId: 'ep1', modelId: 'qwen3' },
|
||||
});
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.json().sessionId).toBeTruthy();
|
||||
expect(res.json().success).not.toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,12 +1,17 @@
|
||||
/**
|
||||
* @fileoverview GET /api/local-model: what this Mac is and which local model fits it.
|
||||
* @fileoverview The local model routes: GET /api/local-model (what this Mac is and which local
|
||||
* model fits it, plus where the install stands) and the verbs that install, cancel, resume,
|
||||
* start, stop and remove.
|
||||
*
|
||||
* The route only composes four injected functions (the hardware probe, the catalogue getter,
|
||||
* the harness facts and the pure `recommend()`), so every test passes its own and no test
|
||||
* depends on what those modules compute. What is pinned here is the route's own contract:
|
||||
* the envelope, the admin gate in multi-user mode, that an unsupported platform and a failed
|
||||
* probe are answers (200) and never a 500, what does answer 500, and that a request writes
|
||||
* nothing.
|
||||
* The routes only compose injected functions (the hardware probe, the catalogue getter, the
|
||||
* harness facts, the pure `recommend()` and the install service), so every test passes its own
|
||||
* and no test depends on what those modules compute. What is pinned here is the routes' own
|
||||
* contract: the envelope, the admin gate on EVERY verb in multi-user mode, that an unsupported
|
||||
* platform and a failed probe are answers (200) and never a 500, what does answer 500, how a
|
||||
* verb's refusal becomes a 400 or a 409 with its conflict, that DELETE hands the live sessions
|
||||
* to the service, and that a GET with nothing installed writes nothing.
|
||||
*
|
||||
* The install service itself is tested in test/local-model-install-service.test.ts.
|
||||
*
|
||||
* Port: N/A (app.inject()).
|
||||
*/
|
||||
@@ -24,12 +29,22 @@ import {
|
||||
} from '../../src/web/routes/local-model-routes.js';
|
||||
import { buildHarnessFacts } from '../../src/local-model/harness-facts.js';
|
||||
import { formatGb, memClassGb } from '../../src/local-model/format.js';
|
||||
import { getDataDir } from '../../src/config/instance.js';
|
||||
import {
|
||||
createLocalModelService,
|
||||
getLocalModelService,
|
||||
type LocalModelService,
|
||||
type LocalModelVerbResult,
|
||||
} from '../../src/local-model/install-job.js';
|
||||
import { CustomModelHostsWriteRefusedError } from '../../src/custom-model-hosts-writer.js';
|
||||
import { SettingsWriteRefusedError } from '../../src/web/settings-writer.js';
|
||||
import { dataPath, getDataDir } from '../../src/config/instance.js';
|
||||
import {
|
||||
LOCAL_MODEL_INSTALL_LABELS,
|
||||
LOCAL_MODEL_LABELS,
|
||||
type HardwareFacts,
|
||||
type HarnessFact,
|
||||
type LocalModelCatalog,
|
||||
type LocalModelInstallView,
|
||||
type Recommendation,
|
||||
} from '../../src/types.js';
|
||||
|
||||
@@ -37,10 +52,6 @@ const CATALOG: LocalModelCatalog = {
|
||||
schema: 1,
|
||||
updated: '2026-11-06',
|
||||
minMacosMajor: 14,
|
||||
promptCacheTiers: [
|
||||
{ minMemGb: 0, bytes: 512 * 1024 * 1024 },
|
||||
{ minMemGb: 32, bytes: 2 * 1024 ** 3 },
|
||||
],
|
||||
models: [],
|
||||
harnesses: [
|
||||
{ cli: 'opencode', minContext: 65536, rank: 3 },
|
||||
@@ -111,6 +122,10 @@ const OK: Recommendation = {
|
||||
speed: 'unknown',
|
||||
speedLabel: LOCAL_MODEL_LABELS.speedUnknown,
|
||||
tested: false,
|
||||
needSource: 'estimated',
|
||||
harnesses: [{ cli: 'opencode', cliLabel: 'OpenCode', installed: true }],
|
||||
installBytes: 17740000000,
|
||||
installGb: '17.7',
|
||||
},
|
||||
alternatives: [],
|
||||
budgetBytes: 37580963840,
|
||||
@@ -133,12 +148,58 @@ const FACTS: HarnessFact[] = [
|
||||
{ cli: 'opencode', label: 'OpenCode', installed: true, installable: false, acceptsModelId: () => true },
|
||||
];
|
||||
|
||||
/** The install view of a Mac with nothing installed, as the service would answer it. */
|
||||
const IDLE_VIEW: LocalModelInstallView = {
|
||||
state: 'idle',
|
||||
running: false,
|
||||
developerTools: 'missing',
|
||||
runtime: { version: '0.40.15', installed: false, downloadBytes: 185971671, downloadGb: '0.2', unpackedGb: '0.6' },
|
||||
folder: '/Users/me/.codeman/local-model',
|
||||
actions: ['install'],
|
||||
};
|
||||
|
||||
const INSTALLING_VIEW: LocalModelInstallView = {
|
||||
...IDLE_VIEW,
|
||||
state: 'installing',
|
||||
step: 'runtime',
|
||||
steps: ['runtime', 'model', 'verify', 'agent', 'connect'],
|
||||
model: { id: 'qwen3.8:27b', label: 'Qwen3.8 27B' },
|
||||
cli: 'opencode',
|
||||
cliLabel: 'OpenCode',
|
||||
context: 131072,
|
||||
actions: ['cancel'],
|
||||
};
|
||||
|
||||
const OK_RESULT: LocalModelVerbResult = { ok: true, view: INSTALLING_VIEW };
|
||||
|
||||
type ServiceMock = { [K in keyof LocalModelService]: Mock<LocalModelService[K]> };
|
||||
|
||||
/** An install service whose every verb answers `OK_RESULT` and whose view is idle. */
|
||||
function fakeService(): ServiceMock {
|
||||
return {
|
||||
view: vi.fn(async () => IDLE_VIEW),
|
||||
launchRefusal: vi.fn(async () => undefined),
|
||||
launchRefusalMessage: vi.fn(async () => undefined),
|
||||
recommendOptions: vi.fn(async () => ({ runtimeDownloadBytes: 185971671 })),
|
||||
install: vi.fn(async () => OK_RESULT),
|
||||
cancel: vi.fn(async () => OK_RESULT),
|
||||
resume: vi.fn(async () => OK_RESULT),
|
||||
runtime: vi.fn(async () => OK_RESULT),
|
||||
remove: vi.fn(async () => OK_RESULT),
|
||||
boot: vi.fn(async () => {}),
|
||||
shutdown: vi.fn(async () => {}),
|
||||
onRuntimeChange: vi.fn(),
|
||||
idle: vi.fn(async () => {}),
|
||||
};
|
||||
}
|
||||
|
||||
interface Fakes {
|
||||
deps: LocalModelRouteDeps;
|
||||
readHardware: Mock<LocalModelRouteDeps['readHardware']>;
|
||||
getCatalog: Mock<LocalModelRouteDeps['getCatalog']>;
|
||||
buildHarnessFacts: Mock<LocalModelRouteDeps['buildHarnessFacts']>;
|
||||
recommend: Mock<LocalModelRouteDeps['recommend']>;
|
||||
service: ServiceMock;
|
||||
}
|
||||
|
||||
function fakes(hardware: HardwareFacts = MAC, recommendation: Recommendation = OK): Fakes {
|
||||
@@ -146,17 +207,28 @@ function fakes(hardware: HardwareFacts = MAC, recommendation: Recommendation = O
|
||||
const getCatalog = vi.fn<LocalModelRouteDeps['getCatalog']>(() => CATALOG);
|
||||
const buildFacts = vi.fn<LocalModelRouteDeps['buildHarnessFacts']>(async () => FACTS);
|
||||
const recommend = vi.fn<LocalModelRouteDeps['recommend']>(() => recommendation);
|
||||
const service = fakeService();
|
||||
return {
|
||||
deps: { readHardware, getCatalog, buildHarnessFacts: buildFacts, recommend },
|
||||
deps: { readHardware, getCatalog, buildHarnessFacts: buildFacts, recommend, service: () => service },
|
||||
readHardware,
|
||||
getCatalog,
|
||||
buildHarnessFacts: buildFacts,
|
||||
recommend,
|
||||
service,
|
||||
};
|
||||
}
|
||||
|
||||
const harness = (deps: Partial<LocalModelRouteDeps>, authUser?: { username: string; role: 'admin' | 'user' }) =>
|
||||
createRouteTestHarness((app) => registerLocalModelRoutes(app, deps), authUser ? { authUser } : undefined);
|
||||
type AuthUser = { username: string; role: 'admin' | 'user' };
|
||||
|
||||
/** The live sessions the DELETE looks at: a name, and for some the endpoint their model comes from. */
|
||||
type SessionLike = { name: string; customModel?: { endpointId: string } };
|
||||
|
||||
const harness = (deps: Partial<LocalModelRouteDeps>, authUser?: AuthUser, sessions: SessionLike[] = []) =>
|
||||
createRouteTestHarness(
|
||||
(app) =>
|
||||
registerLocalModelRoutes(app, { sessions: new Map(sessions.map((session, i) => [`s${i}`, session])) }, deps),
|
||||
authUser ? { authUser } : undefined
|
||||
);
|
||||
|
||||
const get = async (deps: Partial<LocalModelRouteDeps>, authUser?: { username: string; role: 'admin' | 'user' }) => {
|
||||
const { app } = await harness(deps, authUser);
|
||||
@@ -191,10 +263,30 @@ describe('GET /api/local-model', () => {
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.json()).toEqual({
|
||||
success: true,
|
||||
data: { hardware: MAC, recommendation: OK, catalogUpdated: '2026-11-06' },
|
||||
data: { hardware: MAC, recommendation: OK, catalogUpdated: '2026-11-06', install: IDLE_VIEW },
|
||||
});
|
||||
});
|
||||
|
||||
it('recommends with what the install service knows: the measured budget and the runtime still to download', async () => {
|
||||
const f = fakes();
|
||||
f.service.recommendOptions.mockResolvedValue({ measuredTotalBytes: 40_157_944_217, runtimeDownloadBytes: 0 });
|
||||
await get(f.deps);
|
||||
expect(f.service.recommendOptions).toHaveBeenCalledTimes(1);
|
||||
expect(f.recommend.mock.calls[0][3]).toEqual({ measuredTotalBytes: 40_157_944_217, runtimeDownloadBytes: 0 });
|
||||
});
|
||||
|
||||
it('carries the install view of the service as it is, in every state', async () => {
|
||||
const f = fakes();
|
||||
f.service.view.mockResolvedValue(INSTALLING_VIEW);
|
||||
const res = await get(f.deps);
|
||||
expect(res.json().data.install).toEqual(INSTALLING_VIEW);
|
||||
expect(f.service.view).toHaveBeenCalledTimes(1);
|
||||
// Looking is not doing: no verb of the service was called.
|
||||
for (const verb of ['install', 'cancel', 'resume', 'runtime', 'remove', 'boot', 'shutdown'] as const) {
|
||||
expect(f.service[verb]).not.toHaveBeenCalled();
|
||||
}
|
||||
});
|
||||
|
||||
it('hands recommend() exactly what the probe, the catalogue and the harness facts answered', async () => {
|
||||
const f = fakes();
|
||||
await get(f.deps);
|
||||
@@ -211,7 +303,7 @@ describe('GET /api/local-model', () => {
|
||||
it('sends the payload as the modules built it: no size is reformatted and no function leaks', async () => {
|
||||
const res = await get(fakes().deps);
|
||||
const { data } = res.json();
|
||||
expect(Object.keys(data).sort()).toEqual(['catalogUpdated', 'hardware', 'recommendation']);
|
||||
expect(Object.keys(data).sort()).toEqual(['catalogUpdated', 'hardware', 'install', 'recommendation']);
|
||||
expect(data.hardware.memGb).toBe('51.5');
|
||||
expect(data.hardware.freeDiskGb).toBe('912');
|
||||
expect(data.recommendation.pick.needGb).toBe('29.6');
|
||||
@@ -221,10 +313,13 @@ describe('GET /api/local-model', () => {
|
||||
expect(res.body).not.toContain('installable');
|
||||
});
|
||||
|
||||
it('registers a GET and nothing else: this stage has no route that changes anything', async () => {
|
||||
const { app } = await harness(fakes().deps);
|
||||
expect((await app.inject({ method: 'POST', url: '/api/local-model', payload: {} })).statusCode).toBe(404);
|
||||
expect((await app.inject({ method: 'DELETE', url: '/api/local-model' })).statusCode).toBe(404);
|
||||
it('is the only thing a GET does: there is no POST, PUT or PATCH on the same path', async () => {
|
||||
const f = fakes();
|
||||
const { app } = await harness(f.deps);
|
||||
for (const method of ['POST', 'PUT', 'PATCH'] as const) {
|
||||
expect((await app.inject({ method, url: '/api/local-model', payload: {} })).statusCode).toBe(404);
|
||||
}
|
||||
expect(f.service.install).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
describe('multi-user mode', () => {
|
||||
@@ -239,6 +334,8 @@ describe('GET /api/local-model', () => {
|
||||
expect(f.getCatalog).not.toHaveBeenCalled();
|
||||
expect(f.buildHarnessFacts).not.toHaveBeenCalled();
|
||||
expect(f.recommend).not.toHaveBeenCalled();
|
||||
expect(f.service.view).not.toHaveBeenCalled();
|
||||
expect(f.service.recommendOptions).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('answers an admin', async () => {
|
||||
@@ -364,9 +461,16 @@ describe('GET /api/local-model', () => {
|
||||
describe('read-only', () => {
|
||||
it('writes nothing: the home directory and the data dir are byte for byte what they were', async () => {
|
||||
// The real harness facts (they read the CLI registry and run the availability probe),
|
||||
// the route's own composition, and a hardware answer and a recommendation of the test's.
|
||||
// the REAL install service over the real state file (nothing is installed, so its view
|
||||
// is idle), the route's own composition, and a hardware answer and a recommendation of
|
||||
// the test's.
|
||||
const f = fakes();
|
||||
const { app } = await harness({ ...f.deps, buildHarnessFacts: (catalog) => buildHarnessFacts(catalog) });
|
||||
const service = createLocalModelService();
|
||||
const { app } = await harness({
|
||||
...f.deps,
|
||||
buildHarnessFacts: (catalog) => buildHarnessFacts(catalog),
|
||||
service: () => service,
|
||||
});
|
||||
|
||||
const home = homedir();
|
||||
const before = snapshot(home);
|
||||
@@ -375,6 +479,7 @@ describe('GET /api/local-model', () => {
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const res = await app.inject({ method: 'GET', url: '/api/local-model' });
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.json().data.install).toMatchObject({ state: 'idle', running: false, actions: ['install'] });
|
||||
}
|
||||
// A refused request and a failing one write nothing either.
|
||||
f.recommend.mockImplementationOnce(() => {
|
||||
@@ -405,8 +510,28 @@ describe('GET /api/local-model', () => {
|
||||
const server = readFileSync(join(root, 'src/web/server.ts'), 'utf8');
|
||||
const barrel = readFileSync(join(root, 'src/web/routes/index.ts'), 'utf8');
|
||||
expect(barrel).toContain("export { registerLocalModelRoutes } from './local-model-routes.js';");
|
||||
// No second argument: the server runs the real probe, catalogue, facts and recommend().
|
||||
expect(server).toContain('registerLocalModelRoutes(this.app);');
|
||||
// The route context for the live sessions, and no third argument: the server runs the
|
||||
// real probe, catalogue, facts, recommend() and install service.
|
||||
expect(server).toContain('registerLocalModelRoutes(this.app, ctx);');
|
||||
});
|
||||
|
||||
it('the server starts the install service at boot and stops it at shutdown, outside tests only', () => {
|
||||
const root = fileURLToPath(new URL('../../', import.meta.url));
|
||||
const server = readFileSync(join(root, 'src/web/server.ts'), 'utf8');
|
||||
const boot = server.indexOf('void getLocalModelService().boot();');
|
||||
const shutdown = server.indexOf('void getLocalModelService().shutdown();');
|
||||
expect(boot).toBeGreaterThan(-1);
|
||||
expect(shutdown).toBeGreaterThan(-1);
|
||||
// Each sits in a block that tests never enter: nothing of the runtime runs under vitest.
|
||||
for (const at of [boot, shutdown]) {
|
||||
const before = server.slice(0, at).trimEnd();
|
||||
expect(before.endsWith('if (!this.testMode) {')).toBe(true);
|
||||
}
|
||||
// Shutdown stands beside the other supervised child's stop.
|
||||
const deepseek = server.indexOf('void stopDeepSeekWeb();');
|
||||
expect(deepseek).toBeGreaterThan(-1);
|
||||
expect(shutdown).toBeGreaterThan(deepseek);
|
||||
expect(shutdown - deepseek).toBeLessThan(700);
|
||||
});
|
||||
|
||||
it('the default dependencies are the real modules, read at call time', () => {
|
||||
@@ -415,8 +540,406 @@ describe('GET /api/local-model', () => {
|
||||
'getCatalog',
|
||||
'readHardware',
|
||||
'recommend',
|
||||
'service',
|
||||
]);
|
||||
for (const dep of Object.values(defaultLocalModelRouteDeps)) expect(typeof dep).toBe('function');
|
||||
// The one install service of the process, the same object every time.
|
||||
expect(defaultLocalModelRouteDeps.service()).toBe(getLocalModelService());
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('the install verbs', () => {
|
||||
const INSTALL_BODY = { modelId: 'qwen3.8:27b', cli: 'opencode', context: 131072, acknowledgedBytes: 17926000000 };
|
||||
|
||||
/** Every verb as a request, with a body the route accepts. */
|
||||
const VERBS = [
|
||||
{ name: 'install', method: 'POST', url: '/api/local-model/install', payload: INSTALL_BODY },
|
||||
{ name: 'cancel', method: 'POST', url: '/api/local-model/cancel', payload: undefined },
|
||||
{ name: 'resume', method: 'POST', url: '/api/local-model/resume', payload: {} },
|
||||
{ name: 'runtime', method: 'POST', url: '/api/local-model/runtime', payload: { action: 'stop' } },
|
||||
{ name: 'remove', method: 'DELETE', url: '/api/local-model', payload: { removeModel: true } },
|
||||
] as const;
|
||||
|
||||
const call = (app: Awaited<ReturnType<typeof harness>>['app'], verb: (typeof VERBS)[number]) =>
|
||||
app.inject({ method: verb.method, url: verb.url, ...(verb.payload ? { payload: verb.payload } : {}) });
|
||||
|
||||
describe.each(VERBS)('$method $url', (verb) => {
|
||||
it('answers the view after the verb in the success envelope', async () => {
|
||||
const f = fakes();
|
||||
const { app } = await harness(f.deps);
|
||||
const res = await call(app, verb);
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.json()).toEqual({ success: true, data: INSTALLING_VIEW });
|
||||
expect(f.service[verb.name]).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('multi-user mode: a non-admin is refused with 403 before the service is asked anything', async () => {
|
||||
process.env.CODEMAN_MULTIUSER = '1';
|
||||
const f = fakes();
|
||||
const { app } = await harness(f.deps, { username: 'bob', role: 'user' });
|
||||
const res = await call(app, verb);
|
||||
expect(res.statusCode).toBe(403);
|
||||
expect(res.json()).toMatchObject({ success: false, errorCode: 'FORBIDDEN' });
|
||||
for (const name of ['install', 'cancel', 'resume', 'runtime', 'remove', 'view', 'recommendOptions'] as const) {
|
||||
expect(f.service[name]).not.toHaveBeenCalled();
|
||||
}
|
||||
});
|
||||
|
||||
it('multi-user mode: the gate comes before the body is even parsed', async () => {
|
||||
process.env.CODEMAN_MULTIUSER = '1';
|
||||
const f = fakes();
|
||||
const { app } = await harness(f.deps, { username: 'bob', role: 'user' });
|
||||
const res = await app.inject({ method: verb.method, url: verb.url, payload: { nonsense: true } });
|
||||
expect(res.statusCode).toBe(403);
|
||||
});
|
||||
|
||||
it('multi-user mode: an admin is served', async () => {
|
||||
process.env.CODEMAN_MULTIUSER = '1';
|
||||
const f = fakes();
|
||||
const { app } = await harness(f.deps, { username: 'root', role: 'admin' });
|
||||
expect((await call(app, verb)).statusCode).toBe(200);
|
||||
expect(f.service[verb.name]).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('is not gated while multi-user mode is off', async () => {
|
||||
const f = fakes();
|
||||
const { app } = await harness(f.deps, { username: 'bob', role: 'user' });
|
||||
expect((await call(app, verb)).statusCode).toBe(200);
|
||||
});
|
||||
|
||||
it('a 409 of the service is the error envelope plus the conflict as data', async () => {
|
||||
const f = fakes();
|
||||
f.service[verb.name].mockResolvedValue({
|
||||
ok: false,
|
||||
status: 409,
|
||||
conflict: { reason: 'job-running', message: { text: LOCAL_MODEL_INSTALL_LABELS.conflictJobRunning } },
|
||||
});
|
||||
const { app } = await harness(f.deps);
|
||||
const res = await call(app, verb);
|
||||
expect(res.statusCode).toBe(409);
|
||||
expect(res.json()).toEqual({
|
||||
success: false,
|
||||
error: 'An install is already running.',
|
||||
errorCode: 'CONFLICT',
|
||||
data: { reason: 'job-running', message: { text: 'An install is already running.' } },
|
||||
});
|
||||
});
|
||||
|
||||
it('an exception of the service is a 500 in the envelope', async () => {
|
||||
const f = fakes();
|
||||
f.service[verb.name].mockRejectedValue(new Error('disk on fire'));
|
||||
const { app } = await harness(f.deps);
|
||||
const res = await call(app, verb);
|
||||
expect(res.statusCode).toBe(500);
|
||||
expect(res.json()).toMatchObject({ success: false, errorCode: 'INTERNAL_ERROR' });
|
||||
expect(res.json().error).toContain('disk on fire');
|
||||
});
|
||||
|
||||
it.each([
|
||||
['the endpoint store', new CustomModelHostsWriteRefusedError('custom-model-hosts.json is not a valid JSON list')],
|
||||
['the settings file', new SettingsWriteRefusedError('settings.json is not a valid JSON object')],
|
||||
])('a refused write of %s is a 409 with the fix and no conflict data', async (_label, error) => {
|
||||
const f = fakes();
|
||||
f.service[verb.name].mockRejectedValue(error);
|
||||
const { app } = await harness(f.deps);
|
||||
const res = await call(app, verb);
|
||||
expect(res.statusCode).toBe(409);
|
||||
expect(res.json()).toEqual({ success: false, error: error.message, errorCode: 'CONFLICT' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/local-model/install', () => {
|
||||
it('hands the service the four choices and who asked', async () => {
|
||||
const f = fakes();
|
||||
const { app } = await harness(f.deps, { username: 'alice', role: 'admin' });
|
||||
await app.inject({ method: 'POST', url: '/api/local-model/install', payload: INSTALL_BODY });
|
||||
expect(f.service.install).toHaveBeenCalledWith(INSTALL_BODY, { username: 'alice', ip: '127.0.0.1' });
|
||||
});
|
||||
|
||||
it('a row the server does not offer is a 400 with the service own sentence', async () => {
|
||||
const f = fakes();
|
||||
f.service.install.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 400,
|
||||
message: 'This model at this context is not offered for this Mac right now.',
|
||||
});
|
||||
const { app } = await harness(f.deps);
|
||||
const res = await app.inject({ method: 'POST', url: '/api/local-model/install', payload: INSTALL_BODY });
|
||||
expect(res.statusCode).toBe(400);
|
||||
expect(res.json()).toEqual({
|
||||
success: false,
|
||||
error: 'This model at this context is not offered for this Mac right now.',
|
||||
errorCode: 'INVALID_INPUT',
|
||||
});
|
||||
});
|
||||
|
||||
it('consent bound to the size: the 409 carries the server own total, so the window can ask again', async () => {
|
||||
const f = fakes();
|
||||
f.service.install.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 409,
|
||||
conflict: {
|
||||
reason: 'size-changed',
|
||||
message: { text: LOCAL_MODEL_INSTALL_LABELS.conflictSizeChanged, vars: { size: '23.3' } },
|
||||
expectedBytes: 23291770627,
|
||||
expectedGb: '23.3',
|
||||
},
|
||||
});
|
||||
const { app } = await harness(f.deps);
|
||||
const res = await app.inject({ method: 'POST', url: '/api/local-model/install', payload: INSTALL_BODY });
|
||||
expect(res.statusCode).toBe(409);
|
||||
expect(res.json()).toEqual({
|
||||
success: false,
|
||||
error: 'The download is 23.3 GB now. Check the sizes and confirm again.',
|
||||
errorCode: 'CONFLICT',
|
||||
data: {
|
||||
reason: 'size-changed',
|
||||
message: {
|
||||
text: 'The download is {size} GB now. Check the sizes and confirm again.',
|
||||
vars: { size: '23.3' },
|
||||
},
|
||||
expectedBytes: 23291770627,
|
||||
expectedGb: '23.3',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
['no body', undefined],
|
||||
['a missing size', { modelId: 'qwen3.8:27b', cli: 'opencode', context: 131072 }],
|
||||
['a model id with a registry host in it', { ...INSTALL_BODY, modelId: 'evil.example/x/y:z' }],
|
||||
['a model id without a tag', { ...INSTALL_BODY, modelId: 'qwen3.8' }],
|
||||
['an upper-case model id', { ...INSTALL_BODY, modelId: 'Qwen3.8:27B' }],
|
||||
['a coding agent id with a space', { ...INSTALL_BODY, cli: 'open code' }],
|
||||
['a coding agent id that is a path', { ...INSTALL_BODY, cli: '../bin/sh' }],
|
||||
['a context of zero', { ...INSTALL_BODY, context: 0 }],
|
||||
['a context that is not a whole number', { ...INSTALL_BODY, context: 1.5 }],
|
||||
['a negative size', { ...INSTALL_BODY, acknowledgedBytes: -1 }],
|
||||
['a size as text', { ...INSTALL_BODY, acknowledgedBytes: '17926000000' }],
|
||||
['a member nobody defined', { ...INSTALL_BODY, url: 'https://example.invalid/ollama.tgz' }],
|
||||
])('%s is a 400 before the service is asked', async (_label, payload) => {
|
||||
const f = fakes();
|
||||
const { app } = await harness(f.deps);
|
||||
const res = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/local-model/install',
|
||||
...(payload ? { payload } : {}),
|
||||
});
|
||||
expect(res.statusCode).toBe(400);
|
||||
expect(res.json()).toMatchObject({ success: false, errorCode: 'INVALID_INPUT' });
|
||||
expect(f.service.install).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/local-model/resume and /runtime', () => {
|
||||
it('resume takes no body, an empty one, or newPort', async () => {
|
||||
const f = fakes();
|
||||
const { app } = await harness(f.deps);
|
||||
await app.inject({ method: 'POST', url: '/api/local-model/resume' });
|
||||
await app.inject({ method: 'POST', url: '/api/local-model/resume', payload: {} });
|
||||
await app.inject({ method: 'POST', url: '/api/local-model/resume', payload: { newPort: true } });
|
||||
expect(f.service.resume.mock.calls.map(([body]) => body)).toEqual([{}, {}, { newPort: true }]);
|
||||
expect(f.service.resume.mock.calls[0][1]).toEqual({ username: 'admin', ip: '127.0.0.1' });
|
||||
});
|
||||
|
||||
it('resume refuses a body it does not know', async () => {
|
||||
const f = fakes();
|
||||
const { app } = await harness(f.deps);
|
||||
const res = await app.inject({ method: 'POST', url: '/api/local-model/resume', payload: { port: 80 } });
|
||||
expect(res.statusCode).toBe(400);
|
||||
expect(f.service.resume).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('runtime takes start or stop, and newPort with it', async () => {
|
||||
const f = fakes();
|
||||
const { app } = await harness(f.deps);
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/local-model/runtime',
|
||||
payload: { action: 'start', newPort: true },
|
||||
});
|
||||
await app.inject({ method: 'POST', url: '/api/local-model/runtime', payload: { action: 'stop' } });
|
||||
expect(f.service.runtime.mock.calls.map(([body]) => body)).toEqual([
|
||||
{ action: 'start', newPort: true },
|
||||
{ action: 'stop' },
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['no body', undefined],
|
||||
['no action', {}],
|
||||
['an action that is neither', { action: 'restart' }],
|
||||
['a port number of the caller own choosing', { action: 'start', port: 22 }],
|
||||
])('runtime with %s is a 400', async (_label, payload) => {
|
||||
const f = fakes();
|
||||
const { app } = await harness(f.deps);
|
||||
const res = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/local-model/runtime',
|
||||
...(payload ? { payload } : {}),
|
||||
});
|
||||
expect(res.statusCode).toBe(400);
|
||||
expect(f.service.runtime).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('DELETE /api/local-model', () => {
|
||||
const SESSIONS: SessionLike[] = [
|
||||
{ name: 'w1-app', customModel: { endpointId: 'local-ollama' } },
|
||||
{ name: 'w2-app' },
|
||||
{ name: 'w3-lib', customModel: { endpointId: 'office-gpu' } },
|
||||
{ name: 'w4-lib', customModel: { endpointId: 'local-ollama' } },
|
||||
];
|
||||
|
||||
it('hands the service the names of the live sessions on the model, and who asked', async () => {
|
||||
const f = fakes();
|
||||
const { app } = await harness(f.deps, undefined, SESSIONS);
|
||||
await app.inject({ method: 'DELETE', url: '/api/local-model', payload: { removeModel: true } });
|
||||
expect(f.service.remove).toHaveBeenCalledWith({ removeModel: true }, ['w1-app', 'w4-lib'], {
|
||||
username: 'admin',
|
||||
ip: '127.0.0.1',
|
||||
});
|
||||
});
|
||||
|
||||
it('without a live session on the model the list is empty, and no body means the model stays', async () => {
|
||||
const f = fakes();
|
||||
const { app } = await harness(f.deps, undefined, [SESSIONS[1], SESSIONS[2]]);
|
||||
const res = await app.inject({ method: 'DELETE', url: '/api/local-model' });
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(f.service.remove.mock.calls[0][0]).toEqual({});
|
||||
expect(f.service.remove.mock.calls[0][1]).toEqual([]);
|
||||
});
|
||||
|
||||
it('the sessions are read at the moment of the request, not when the routes were registered', async () => {
|
||||
const f = fakes();
|
||||
const sessions = new Map<string, SessionLike>();
|
||||
const { app } = await createRouteTestHarness((instance) =>
|
||||
registerLocalModelRoutes(instance, { sessions }, f.deps)
|
||||
);
|
||||
await app.inject({ method: 'DELETE', url: '/api/local-model' });
|
||||
sessions.set('late', { name: 'w9-late', customModel: { endpointId: 'local-ollama' } });
|
||||
await app.inject({ method: 'DELETE', url: '/api/local-model' });
|
||||
expect(f.service.remove.mock.calls.map((args) => args[1])).toEqual([[], ['w9-late']]);
|
||||
});
|
||||
|
||||
it('while a live session uses the model: 409 with the session names', async () => {
|
||||
const f = fakes();
|
||||
f.service.remove.mockImplementation(async (_request, names) => ({
|
||||
ok: false,
|
||||
status: 409,
|
||||
conflict: {
|
||||
reason: 'sessions-open',
|
||||
message: { text: LOCAL_MODEL_INSTALL_LABELS.conflictSessionsOpen, vars: { sessions: names.join(', ') } },
|
||||
sessions: [...names],
|
||||
},
|
||||
}));
|
||||
const { app } = await harness(f.deps, undefined, SESSIONS);
|
||||
const res = await app.inject({ method: 'DELETE', url: '/api/local-model' });
|
||||
expect(res.statusCode).toBe(409);
|
||||
expect(res.json()).toEqual({
|
||||
success: false,
|
||||
error: 'Sessions are still using this model: w1-app, w4-lib. Close them first.',
|
||||
errorCode: 'CONFLICT',
|
||||
data: {
|
||||
reason: 'sessions-open',
|
||||
message: {
|
||||
text: 'Sessions are still using this model: {sessions}. Close them first.',
|
||||
vars: { sessions: 'w1-app, w4-lib' },
|
||||
},
|
||||
sessions: ['w1-app', 'w4-lib'],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('refuses a body it does not know', async () => {
|
||||
const f = fakes();
|
||||
const { app } = await harness(f.deps);
|
||||
const res = await app.inject({ method: 'DELETE', url: '/api/local-model', payload: { path: '/' } });
|
||||
expect(res.statusCode).toBe(400);
|
||||
expect(f.service.remove).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('the routes over the real install service', () => {
|
||||
const STATE = () => dataPath('local-model.json');
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(STATE(), { force: true });
|
||||
});
|
||||
|
||||
/** The real service with the real state file. Only the hardware is the test's: a server that is not a Mac. */
|
||||
function realService(): LocalModelService {
|
||||
return createLocalModelService({ readHardware: async () => LINUX });
|
||||
}
|
||||
|
||||
it('on a server that is not a Mac nothing can be installed: the install is a 400 and nothing is written', async () => {
|
||||
const service = realService();
|
||||
const { app } = await harness({ readHardware: async () => LINUX, service: () => service });
|
||||
const res = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/local-model/install',
|
||||
payload: { modelId: 'qwen3.6:35b-a3b', cli: 'opencode', context: 131072, acknowledgedBytes: 1 },
|
||||
});
|
||||
expect(res.statusCode).toBe(400);
|
||||
expect(res.json()).toMatchObject({ success: false, errorCode: 'INVALID_INPUT' });
|
||||
expect(existsSync(STATE())).toBe(false);
|
||||
const get = await app.inject({ method: 'GET', url: '/api/local-model' });
|
||||
expect(get.json().data.install).toMatchObject({ state: 'idle', actions: ['install'] });
|
||||
expect(get.json().data.recommendation).toMatchObject({ verdict: 'none', reason: 'not-macos' });
|
||||
});
|
||||
|
||||
it('with nothing installed: cancel, resume and a runtime action are 409 wrong-state', async () => {
|
||||
const service = realService();
|
||||
const { app } = await harness({ service: () => service });
|
||||
for (const [url, payload] of [
|
||||
['/api/local-model/cancel', undefined],
|
||||
['/api/local-model/resume', {}],
|
||||
['/api/local-model/runtime', { action: 'start' }],
|
||||
] as const) {
|
||||
const res = await app.inject({ method: 'POST', url, ...(payload ? { payload } : {}) });
|
||||
expect(res.statusCode).toBe(409);
|
||||
expect(res.json()).toMatchObject({
|
||||
success: false,
|
||||
errorCode: 'CONFLICT',
|
||||
error: 'That is not possible in the current state.',
|
||||
data: { reason: 'wrong-state' },
|
||||
});
|
||||
}
|
||||
expect(existsSync(STATE())).toBe(false);
|
||||
});
|
||||
|
||||
it('a job the state file still calls installing comes back paused on the first GET of a new server', async () => {
|
||||
writeFileSync(
|
||||
STATE(),
|
||||
JSON.stringify({
|
||||
schema: 1,
|
||||
state: 'installing',
|
||||
step: 'model',
|
||||
steps: ['runtime', 'model', 'verify', 'agent', 'connect'],
|
||||
model: { id: 'qwen3.6:35b-a3b', label: 'Qwen3.6 35B-A3B', context: 131072 },
|
||||
cli: { id: 'opencode', label: 'OpenCode' },
|
||||
runtime: { version: '0.40.15', port: 11435, enabled: true },
|
||||
progress: { completedBytes: 6200000000, totalBytes: 23105798956 },
|
||||
})
|
||||
);
|
||||
const service = realService();
|
||||
const { app } = await harness({ readHardware: async () => LINUX, service: () => service });
|
||||
const res = await app.inject({ method: 'GET', url: '/api/local-model' });
|
||||
expect(res.json().data.install).toMatchObject({
|
||||
state: 'paused',
|
||||
step: 'model',
|
||||
running: false,
|
||||
model: { id: 'qwen3.6:35b-a3b', label: 'Qwen3.6 35B-A3B' },
|
||||
cli: 'opencode',
|
||||
port: 11435,
|
||||
progress: { completedBytes: 6200000000, totalBytes: 23105798956, completedGb: '6.2', totalGb: '23.1' },
|
||||
actions: ['resume', 'install', 'remove'],
|
||||
});
|
||||
expect(JSON.parse(readFileSync(STATE(), 'utf8')).state).toBe('paused');
|
||||
// A second install while that job is only paused is refused for what it asks, not started.
|
||||
const cancel = await app.inject({ method: 'POST', url: '/api/local-model/cancel' });
|
||||
expect(cancel.statusCode).toBe(409);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user