mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
test(ai): fix the model-not-found pattern, guard the fetch mocks, and read the booking in an ORM context
This commit is contained in:
@@ -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`.
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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\)/);
|
||||
|
||||
Reference in New Issue
Block a user