114 lines
4.5 KiB
JavaScript
114 lines
4.5 KiB
JavaScript
import { describe, expect, it, vi } from "vitest";
|
|
import { __resetChatSupportForTests, getProvider } from "@/lib/llm/provider.js";
|
|
|
|
describe("OllamaLlmProvider chat capability detection", () => {
|
|
it("uses the configured model for the chat probe and keeps the chat path", async () => {
|
|
const originalBaseUrl = process.env.OLLAMA_BASE_URL;
|
|
const fetchSpy = vi.fn()
|
|
.mockResolvedValueOnce({ ok: true, body: { consume: vi.fn() } })
|
|
.mockResolvedValueOnce({
|
|
ok: true,
|
|
json: async () => ({ message: { content: "{}" } }),
|
|
});
|
|
vi.stubGlobal("fetch", fetchSpy);
|
|
process.env.OLLAMA_BASE_URL = "http://ollama.test";
|
|
|
|
try {
|
|
__resetChatSupportForTests();
|
|
const result = await getProvider().generateReconstruction("prompt", "configured-model");
|
|
|
|
expect(JSON.parse(fetchSpy.mock.calls[0][1].body)).toMatchObject({
|
|
model: "configured-model",
|
|
stream: false,
|
|
});
|
|
expect(fetchSpy.mock.calls[0][0]).toBe("http://ollama.test/api/chat");
|
|
expect(fetchSpy.mock.calls[1][0]).toBe("http://ollama.test/api/chat");
|
|
expect(fetchSpy.mock.calls[1][0]).not.toContain("/api/generate");
|
|
const chatRequest = JSON.parse(fetchSpy.mock.calls[1][1].body);
|
|
expect(chatRequest).toMatchObject({
|
|
model: "configured-model",
|
|
messages: [{ role: "user", content: "prompt" }],
|
|
stream: false,
|
|
});
|
|
expect(chatRequest.format).toBeTypeOf("object");
|
|
expect(chatRequest.format).not.toBe("json");
|
|
const formatText = JSON.stringify(chatRequest.format);
|
|
expect(formatText).toContain("relationship");
|
|
expect(formatText).toContain("evidenceType");
|
|
expect(result).toMatchObject({
|
|
response: {},
|
|
providerApiPath: "/api/chat",
|
|
providerExecution: {
|
|
chatCapabilityDetected: true,
|
|
chatRequestAttempted: true,
|
|
chatRequestSucceeded: true,
|
|
generateRequestAttempted: false,
|
|
},
|
|
});
|
|
} finally {
|
|
vi.unstubAllGlobals();
|
|
if (originalBaseUrl === undefined) delete process.env.OLLAMA_BASE_URL;
|
|
else process.env.OLLAMA_BASE_URL = originalBaseUrl;
|
|
}
|
|
});
|
|
|
|
it("reports chat-skipped generate fallback execution", async () => {
|
|
const originalBaseUrl = process.env.OLLAMA_BASE_URL;
|
|
const fetchSpy = vi.fn()
|
|
.mockResolvedValueOnce({ ok: false, status: 501, body: { consume: vi.fn() } })
|
|
.mockResolvedValueOnce({ ok: false, status: 500, text: async () => "failure" });
|
|
vi.stubGlobal("fetch", fetchSpy);
|
|
process.env.OLLAMA_BASE_URL = "http://ollama.test";
|
|
|
|
try {
|
|
__resetChatSupportForTests();
|
|
await expect(
|
|
getProvider().generateReconstruction("prompt", "configured-model"),
|
|
).rejects.toMatchObject({
|
|
providerApiPath: "/api/generate",
|
|
providerExecution: {
|
|
chatCapabilityDetected: false,
|
|
chatRequestAttempted: false,
|
|
chatRequestSucceeded: false,
|
|
generateRequestAttempted: true,
|
|
},
|
|
});
|
|
expect(fetchSpy.mock.calls[1][0]).toBe("http://ollama.test/api/generate");
|
|
} finally {
|
|
vi.unstubAllGlobals();
|
|
if (originalBaseUrl === undefined) delete process.env.OLLAMA_BASE_URL;
|
|
else process.env.OLLAMA_BASE_URL = originalBaseUrl;
|
|
}
|
|
});
|
|
|
|
it("reports chat-attempt-failed generate fallback execution", async () => {
|
|
const originalBaseUrl = process.env.OLLAMA_BASE_URL;
|
|
const fetchSpy = vi.fn()
|
|
.mockResolvedValueOnce({ ok: true, body: { consume: vi.fn() } })
|
|
.mockResolvedValueOnce({ ok: false, body: { consume: vi.fn() } })
|
|
.mockResolvedValueOnce({ ok: false, status: 500, text: async () => "failure" });
|
|
vi.stubGlobal("fetch", fetchSpy);
|
|
process.env.OLLAMA_BASE_URL = "http://ollama.test";
|
|
|
|
try {
|
|
__resetChatSupportForTests();
|
|
await expect(
|
|
getProvider().generateReconstruction("prompt", "configured-model"),
|
|
).rejects.toMatchObject({
|
|
providerApiPath: "/api/generate",
|
|
providerExecution: {
|
|
chatCapabilityDetected: true,
|
|
chatRequestAttempted: true,
|
|
chatRequestSucceeded: false,
|
|
generateRequestAttempted: true,
|
|
},
|
|
});
|
|
expect(fetchSpy.mock.calls[1][0]).toBe("http://ollama.test/api/chat");
|
|
expect(fetchSpy.mock.calls[2][0]).toBe("http://ollama.test/api/generate");
|
|
} finally {
|
|
vi.unstubAllGlobals();
|
|
if (originalBaseUrl === undefined) delete process.env.OLLAMA_BASE_URL;
|
|
else process.env.OLLAMA_BASE_URL = originalBaseUrl;
|
|
}
|
|
});
|
|
}); |