From 1ecc449f3ee52870f1a3f090e512eba603a08d31 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 26 Oct 2026 10:20:45 +0100 Subject: [PATCH] test(ai): fix the model-not-found pattern, guard the fetch mocks, and read the booking in an ORM context --- .../llm-parse/router/ollama-format.client.ts | 2 +- server/tests/e2e/ai-own-server.e2e.test.ts | 16 ++++++------- .../llm-parse/ollama-format.client.test.ts | 23 ++++++++++--------- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/server/src/nest/llm-parse/router/ollama-format.client.ts b/server/src/nest/llm-parse/router/ollama-format.client.ts index 6f2d4517c..0369b4fc5 100644 --- a/server/src/nest/llm-parse/router/ollama-format.client.ts +++ b/server/src/nest/llm-parse/router/ollama-format.client.ts @@ -49,7 +49,7 @@ export function toNativeBase(baseUrl: string): string { * Ollama's own 404 for a model it has not pulled (OLLAMA_MISSING_MODEL). */ const NOT_OLLAMA_STATUSES = new Set([404, 405, 501]); -const OLLAMA_MISSING_MODEL = /model.*not found/i; +const OLLAMA_MISSING_MODEL = /\bmodel\b.*\bnot found\b/i; /** * Run one schema-constrained chat completion against Ollama's native `/api/chat`. diff --git a/server/tests/e2e/ai-own-server.e2e.test.ts b/server/tests/e2e/ai-own-server.e2e.test.ts index dc5ea6854..a1c80667c 100644 --- a/server/tests/e2e/ai-own-server.e2e.test.ts +++ b/server/tests/e2e/ai-own-server.e2e.test.ts @@ -9,6 +9,7 @@ * fail with the server's 404 (#2806). */ import { buildApp } from '../../src/bootstrap'; +import { withRequestContext } from '../../src/nest/database/request-context'; import { LlmParseService } from '../../src/nest/llm-parse/llm-parse.service'; import { setAddonEnabled } from '../helpers/factories/settings'; import { makeUser } from '../helpers/factories/users'; @@ -136,15 +137,12 @@ describe('An OpenAI-compatible own server', () => { expect((await connect()).status).toBe(200); await setAddonEnabled(orm, 'llm_parsing', true); server.seen = []; - const out = await nestApp - .get(LlmParseService) - .parse( - { - buffer: Buffer.from('Booking confirmation\nHotel Sakura, Tokyo\nCheck-in 2 Nov 2026, check-out 5 Nov 2026'), - originalName: 'booking.txt', - }, - adminId, - ); + // Outside a request, so it needs its own ORM context, as a job would. + const booking = { + buffer: Buffer.from('Booking confirmation\nHotel Sakura, Tokyo\nCheck-in 2 Nov 2026, check-out 5 Nov 2026'), + originalName: 'booking.txt', + }; + const out = await withRequestContext(orm, () => nestApp.get(LlmParseService).parse(booking, adminId)); expect(out.warnings.filter((w) => /failed/i.test(w))).toEqual([]); expect(out.kiItems).toHaveLength(1); expect(server.seen).toContain('POST /openai/api/chat'); diff --git a/server/tests/unit/nest/llm-parse/ollama-format.client.test.ts b/server/tests/unit/nest/llm-parse/ollama-format.client.test.ts index 6cf87d813..ab1f309f9 100644 --- a/server/tests/unit/nest/llm-parse/ollama-format.client.test.ts +++ b/server/tests/unit/nest/llm-parse/ollama-format.client.test.ts @@ -114,7 +114,7 @@ describe('extractEnforced on an OpenAI-compatible server (#2806)', () => { ); it('sends the extraction to /chat/completions at the address as entered when /api/chat is missing', async () => { - const fetchFn = mockFetch((url) => + const fetchFn = mockFetch((url = '') => url.endsWith('/api/chat') ? unknownUrl(url) : chatAnswer('{"result":[{"name":"Hotel"}]}'), ); expect(await extractEnforced(GROQ)).toEqual({ name: 'Hotel' }); @@ -136,23 +136,24 @@ describe('extractEnforced on an OpenAI-compatible server (#2806)', () => { }); it('falls back on a 405 or 501 too, and on a 200 that is not an Ollama answer (LM Studio)', async () => { - for (const first of [ - jsonResponse({}, false, 405), - jsonResponse({}, false, 501), - jsonResponse({ error: 'Unexpected endpoint' }), - ]) { - mockFetch((url) => (url.endsWith('/api/chat') ? first : chatAnswer('{"result":[{"a":1}]}'))); + const fallsBackAfter = async (first: Response) => { + mockFetch((url = '') => (url.endsWith('/api/chat') ? first : chatAnswer('{"result":[{"a":1}]}'))); expect(await extractEnforced(GROQ)).toEqual({ a: 1 }); - } + }; + await fallsBackAfter(jsonResponse({}, false, 405)); + await fallsBackAfter(jsonResponse({}, false, 501)); + await fallsBackAfter(jsonResponse({ error: 'Unexpected endpoint' })); }); it('answers null when the server returns no object', async () => { - mockFetch((url) => (url.endsWith('/api/chat') ? unknownUrl(url) : chatAnswer('{"result":[]}'))); + mockFetch((url = '') => (url.endsWith('/api/chat') ? unknownUrl(url) : chatAnswer('{"result":[]}'))); expect(await extractEnforced(GROQ)).toBeNull(); }); it('passes the images on as image parts with their type', async () => { - const fetchFn = mockFetch((url) => (url.endsWith('/api/chat') ? unknownUrl(url) : chatAnswer('{"result":[{}]}'))); + const fetchFn = mockFetch((url = '') => + url.endsWith('/api/chat') ? unknownUrl(url) : chatAnswer('{"result":[{}]}'), + ); await extractEnforced({ ...GROQ, images: ['iVBORw0K', '/9j/4AAQ', 'UklGRiQA', 'R0lGODlh'] }); const parts = JSON.parse((fetchFn.mock.calls[1][1] as RequestInit).body as string).messages[1].content; expect(parts.slice(1).map((p: { image_url: { url: string } }) => p.image_url.url.split(';')[0])).toEqual([ @@ -164,7 +165,7 @@ describe('extractEnforced on an OpenAI-compatible server (#2806)', () => { }); it('reports the OpenAI-compatible failure when that path fails too', async () => { - mockFetch((url) => + mockFetch((url = '') => url.endsWith('/api/chat') ? unknownUrl(url) : jsonResponse({ error: 'bad model' }, false, 404), ); await expect(extractEnforced(GROQ)).rejects.toThrow(/LLM request failed \(404\)/);