mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-16 03:09:33 +01:00
Merge feat/local-model-recommendation: recommend and install a local coding model on this Mac (experimental)
Conflicts resolved as on test/desktop-macbook: config/test-suites.ts keeps both browser tests (desktop-connect-page and local-model-ui), and the CLAUDE.md state-files line keeps #778's cli-installed entry with the local model's local-model.json and local-model/ entries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
e8a63ede00
80 files changed
+31268
-261
No files matched your search
@@ -8,7 +8,10 @@
|
||||
* been bypassed.
|
||||
* Port: N/A (app.inject, no real port needed)
|
||||
*/
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest';
|
||||
import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest';
|
||||
import { readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
||||
import { dataPath, getDataDir } from '../../src/config/instance.js';
|
||||
import { customModelHostsPath, readCustomModelHosts, type CustomModelHost } from '../../src/custom-model-hosts.js';
|
||||
import { registerCustomModelRoutes } from '../../src/web/routes/custom-model-routes.js';
|
||||
import { webviewFetch } from '../../src/web/webview-egress.js';
|
||||
import { createRouteTestHarness, type RouteTestHarness } from './_route-test-utils.js';
|
||||
@@ -524,10 +527,9 @@ describe('context-length discovery from the /v1/models listing', () => {
|
||||
defaultContextLength: 65536,
|
||||
});
|
||||
fetchMock.mockResolvedValue(
|
||||
new Response(
|
||||
JSON.stringify({ data: [{ id: 're-listed', context_length: 32768 }, { id: 'untouched' }] }),
|
||||
{ status: 200 }
|
||||
)
|
||||
new Response(JSON.stringify({ data: [{ id: 're-listed', context_length: 32768 }, { id: 'untouched' }] }), {
|
||||
status: 200,
|
||||
})
|
||||
);
|
||||
|
||||
await app.inject({ method: 'POST', url: '/api/model-endpoints/ep-ctx-keep/discover-models' });
|
||||
@@ -580,3 +582,278 @@ describe('defaultContextLength (the user-set fallback discovery never overwrites
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('every write goes through the serialized writer', () => {
|
||||
const HOSTS = () => customModelHostsPath(getDataDir());
|
||||
|
||||
beforeEach(() => {
|
||||
rmSync(HOSTS(), { force: true });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
fetchMock.mockReset();
|
||||
rmSync(HOSTS(), { force: true });
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('ten creates sent at once all land (each used to read, change and write on its own)', async () => {
|
||||
const { app } = await setup();
|
||||
const ids = Array.from({ length: 10 }, (_, i) => `burst${i}`);
|
||||
const responses = await Promise.all(
|
||||
ids.map((id) =>
|
||||
app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/model-endpoints',
|
||||
payload: { id, label: id, baseUrl: 'http://localhost:8200', apiKey: `sk-${id}` },
|
||||
})
|
||||
)
|
||||
);
|
||||
for (const res of responses) expect(res.json().success).toBe(true);
|
||||
const stored = await readCustomModelHosts(getDataDir());
|
||||
expect(stored.map((host) => host.id).sort()).toEqual([...ids].sort());
|
||||
// Every key survived: none of the writes replaced the list with its own record.
|
||||
for (const host of stored) expect(host.apiKey).toBe(`sk-${host.id}`);
|
||||
});
|
||||
|
||||
it('two creates of one id sent at once: one is stored, the other is told it exists', async () => {
|
||||
const { app } = await setup();
|
||||
const create = (label: string) =>
|
||||
app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/model-endpoints',
|
||||
payload: { id: 'race', label, baseUrl: 'http://localhost:8201' },
|
||||
});
|
||||
const [first, second] = await Promise.all([create('one'), create('two')]);
|
||||
expect([first.json().success, second.json().success].sort()).toEqual([false, true]);
|
||||
expect([first.json().errorCode, second.json().errorCode]).toContain('ALREADY_EXISTS');
|
||||
const stored = await readCustomModelHosts(getDataDir());
|
||||
expect(stored).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('a discovery that takes a moment does not undo an edit made while it ran', async () => {
|
||||
const { app } = await setup();
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/model-endpoints',
|
||||
payload: { id: 'slow', label: 'Before', baseUrl: 'http://localhost:8202' },
|
||||
});
|
||||
let answer: (response: Response) => void = () => {};
|
||||
fetchMock.mockImplementation(
|
||||
(async () => new Promise<Response>((resolve) => (answer = resolve))) as unknown as typeof webviewFetch
|
||||
);
|
||||
const discovering = app.inject({ method: 'POST', url: '/api/model-endpoints/slow/discover-models' });
|
||||
await vi.waitFor(() => expect(fetchMock).toHaveBeenCalled());
|
||||
|
||||
// The endpoint is renamed while the probe is still out.
|
||||
const renamed = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/model-endpoints/slow',
|
||||
payload: { label: 'After', baseUrl: 'http://localhost:8202' },
|
||||
});
|
||||
expect(renamed.json().success).toBe(true);
|
||||
|
||||
answer(new Response(JSON.stringify({ data: [{ id: 'qwen3' }] }), { status: 200 }));
|
||||
expect((await discovering).json()).toEqual({ success: true, data: { models: ['qwen3'] } });
|
||||
|
||||
const [stored] = await readCustomModelHosts(getDataDir());
|
||||
// Both changes are there: the new name and the discovered list.
|
||||
expect(stored.label).toBe('After');
|
||||
expect(stored.models).toEqual(['qwen3']);
|
||||
});
|
||||
|
||||
it('an endpoint deleted while its discovery ran stays deleted', async () => {
|
||||
const { app } = await setup();
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/model-endpoints',
|
||||
payload: { id: 'gone', label: 'Gone', baseUrl: 'http://localhost:8203' },
|
||||
});
|
||||
let answer: (response: Response) => void = () => {};
|
||||
fetchMock.mockImplementation(
|
||||
(async () => new Promise<Response>((resolve) => (answer = resolve))) as unknown as typeof webviewFetch
|
||||
);
|
||||
const discovering = app.inject({ method: 'POST', url: '/api/model-endpoints/gone/discover-models' });
|
||||
await vi.waitFor(() => expect(fetchMock).toHaveBeenCalled());
|
||||
await app.inject({ method: 'DELETE', url: '/api/model-endpoints/gone' });
|
||||
answer(new Response(JSON.stringify({ data: [{ id: 'qwen3' }] }), { status: 200 }));
|
||||
expect((await discovering).json()).toMatchObject({ success: false, errorCode: 'NOT_FOUND' });
|
||||
expect(await readCustomModelHosts(getDataDir())).toEqual([]);
|
||||
});
|
||||
|
||||
it('deleting an id that is not saved leaves the file as it is', async () => {
|
||||
const { app } = await setup();
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/model-endpoints',
|
||||
payload: { id: 'keep', label: 'Keep', baseUrl: 'http://localhost:8204' },
|
||||
});
|
||||
const before = { text: readFileSync(HOSTS(), 'utf8'), mtime: statSync(HOSTS()).mtimeMs };
|
||||
const res = await app.inject({ method: 'DELETE', url: '/api/model-endpoints/never-saved' });
|
||||
expect(res.json()).toEqual({ success: true, data: { id: 'never-saved' } });
|
||||
expect(readFileSync(HOSTS(), 'utf8')).toBe(before.text);
|
||||
expect(statSync(HOSTS()).mtimeMs).toBe(before.mtime);
|
||||
});
|
||||
|
||||
describe('a store that does not parse', () => {
|
||||
const BROKEN = '[{"id":"mine","label":"Mine","baseUrl":"http://localhost:9","apiKey":"sk-keep-me"},';
|
||||
|
||||
beforeEach(() => {
|
||||
writeFileSync(HOSTS(), BROKEN);
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
it.each([
|
||||
['create', 'POST', '/api/model-endpoints', { id: 'new', label: 'New', baseUrl: 'http://localhost:8205' }],
|
||||
['update', 'PUT', '/api/model-endpoints/mine', { label: 'Renamed', baseUrl: 'http://localhost:8205' }],
|
||||
['delete', 'DELETE', '/api/model-endpoints/mine', undefined],
|
||||
] as const)(
|
||||
'%s is refused with 409 and the file is left byte for byte (it used to be replaced by one record)',
|
||||
async (_label, method, url, payload) => {
|
||||
const { app } = await setup();
|
||||
const res = await app.inject({ method, url, ...(payload ? { payload } : {}) });
|
||||
expect(res.statusCode).toBe(409);
|
||||
expect(res.json()).toMatchObject({ success: false, errorCode: 'CONFLICT' });
|
||||
expect(res.json().error).toContain('custom-model-hosts.json');
|
||||
// The refusal never repeats the file, which holds a key.
|
||||
expect(res.body).not.toContain('sk-keep-me');
|
||||
expect(readFileSync(HOSTS(), 'utf8')).toBe(BROKEN);
|
||||
}
|
||||
);
|
||||
|
||||
it('reading still answers an empty list, as before', async () => {
|
||||
const { app } = await setup();
|
||||
expect((await app.inject({ method: 'GET', url: '/api/model-endpoints' })).json()).toEqual([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('the endpoint the local model install manages', () => {
|
||||
const HOSTS = () => customModelHostsPath(getDataDir());
|
||||
const STATE = () => dataPath('local-model.json');
|
||||
|
||||
const RECORD: 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,
|
||||
};
|
||||
|
||||
/** The state file of an installed model whose connection is the record above. */
|
||||
function markManaged(): void {
|
||||
writeFileSync(
|
||||
STATE(),
|
||||
JSON.stringify({
|
||||
schema: 1,
|
||||
state: 'installed',
|
||||
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 },
|
||||
connection: { endpointId: 'local-ollama', flagWasEnabled: false },
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
rmSync(STATE(), { force: true });
|
||||
writeFileSync(HOSTS(), JSON.stringify([RECORD]));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
fetchMock.mockReset();
|
||||
rmSync(STATE(), { force: true });
|
||||
rmSync(HOSTS(), { force: true });
|
||||
});
|
||||
|
||||
it('a new endpoint may not take the reserved id, whether or not a model is installed', async () => {
|
||||
rmSync(HOSTS(), { force: true });
|
||||
const { app } = await setup();
|
||||
const res = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/model-endpoints',
|
||||
payload: { id: 'local-ollama', label: 'Mine', baseUrl: 'http://127.0.0.1:11434' },
|
||||
});
|
||||
expect(res.json()).toEqual({
|
||||
success: false,
|
||||
error: 'This endpoint id is reserved for the model Codeman runs on this Mac. Pick another id.',
|
||||
errorCode: 'INVALID_INPUT',
|
||||
});
|
||||
expect(await readCustomModelHosts(getDataDir())).toEqual([]);
|
||||
});
|
||||
|
||||
it('while it is managed it is not edited: its port stays the runtime Codeman supervises', async () => {
|
||||
markManaged();
|
||||
const { app } = await setup();
|
||||
const res = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/model-endpoints/local-ollama',
|
||||
payload: { label: 'This Mac', baseUrl: 'http://192.168.1.99:11434' },
|
||||
});
|
||||
expect(res.json()).toEqual({
|
||||
success: false,
|
||||
error:
|
||||
'This endpoint belongs to the model Codeman runs on this Mac. Change or remove it in the local model window.',
|
||||
errorCode: 'INVALID_INPUT',
|
||||
});
|
||||
expect(await readCustomModelHosts(getDataDir())).toEqual([RECORD]);
|
||||
});
|
||||
|
||||
it('while it is managed it is not deleted from here: the runtime would keep running without it', async () => {
|
||||
markManaged();
|
||||
const { app } = await setup();
|
||||
const res = await app.inject({ method: 'DELETE', url: '/api/model-endpoints/local-ollama' });
|
||||
expect(res.json()).toMatchObject({ success: false, errorCode: 'INVALID_INPUT' });
|
||||
expect(await readCustomModelHosts(getDataDir())).toEqual([RECORD]);
|
||||
});
|
||||
|
||||
it('while it is managed a discovery answers the one model the install checked and asks the runtime nothing', async () => {
|
||||
markManaged();
|
||||
const { app } = await setup();
|
||||
const res = await app.inject({ method: 'POST', url: '/api/model-endpoints/local-ollama/discover-models' });
|
||||
expect(res.json()).toEqual({ success: true, data: { models: ['qwen3.6:35b-a3b'] } });
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
expect(await readCustomModelHosts(getDataDir())).toEqual([RECORD]);
|
||||
});
|
||||
|
||||
it('it is listed like any other endpoint', async () => {
|
||||
markManaged();
|
||||
const { app } = await setup();
|
||||
const list = (await app.inject({ method: 'GET', url: '/api/model-endpoints' })).json();
|
||||
expect(list).toEqual([{ ...RECORD, apiKeySet: false }]);
|
||||
});
|
||||
|
||||
it('a record with that id that nobody installed (made by hand) can still be edited and deleted by its owner', async () => {
|
||||
// No state file: this feature wrote nothing, so the record is the person's own.
|
||||
const { app } = await setup();
|
||||
const update = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/model-endpoints/local-ollama',
|
||||
payload: { label: 'My own Ollama', baseUrl: 'http://127.0.0.1:11434' },
|
||||
});
|
||||
expect(update.json().success).toBe(true);
|
||||
expect((await readCustomModelHosts(getDataDir()))[0].label).toBe('My own Ollama');
|
||||
const del = await app.inject({ method: 'DELETE', url: '/api/model-endpoints/local-ollama' });
|
||||
expect(del.json()).toEqual({ success: true, data: { id: 'local-ollama' } });
|
||||
expect(await readCustomModelHosts(getDataDir())).toEqual([]);
|
||||
});
|
||||
|
||||
it('any other endpoint is edited and deleted as before while a model is installed', async () => {
|
||||
markManaged();
|
||||
const { app } = await setup();
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/model-endpoints',
|
||||
payload: { id: 'office', label: 'Office', baseUrl: 'http://localhost:8210' },
|
||||
});
|
||||
const update = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/model-endpoints/office',
|
||||
payload: { label: 'Office GPU', baseUrl: 'http://localhost:8210' },
|
||||
});
|
||||
expect(update.json().data.host.label).toBe('Office GPU');
|
||||
await app.inject({ method: 'DELETE', url: '/api/model-endpoints/office' });
|
||||
expect(await readCustomModelHosts(getDataDir())).toEqual([RECORD]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,309 @@
|
||||
/**
|
||||
* @fileoverview The launch guard of the endpoint the local model install manages, at the two
|
||||
* routes that put a session onto a custom model endpoint: they ask the install service before
|
||||
* anything is written.
|
||||
*
|
||||
* - `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.
|
||||
*
|
||||
* Every OTHER way a new agent process comes to be on the endpoint (the attach that finds no
|
||||
* pane, the respawn of a dead pane, a restart in place, the rebuild after a reboot) is
|
||||
* covered below the routes, by `Session`'s launch guard: test/local-model-session-launch-guard.test.ts.
|
||||
*
|
||||
* 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user