feat(ai): LocalOllamaProvider with 120s timeout + integration harness
Task 12 of the slice plan. Implements the slice's only provider:
- generate(): POST {endpoint}/api/generate with Korean-first
prompt; AbortController-driven 120s timeout; parses
body.response as JSON and runs it through parseAiResponse.
- healthCheck(): GET /api/tags; returns ok when configured model
is in the listing, otherwise reports the missing-model reason.
- Constructor takes opts.endpoint / opts.model so Task 30 main
entry can inject INKLING_OLLAMA_ENDPOINT for LAN dogfood;
defaults are http://localhost:11434 and gemma4:e4b.
Tests: 6 unit cases via undici MockAgent (parse, non-JSON,
timeout abort, healthCheck ok / missing / connection error).
Integration test gated by INKLING_INTEGRATION=1 hits real
Ollama for Korean / English-stack / mixed input cases with a
180s per-test budget — also reads INKLING_OLLAMA_ENDPOINT so
LAN dogfood can be exercised end-to-end.
Plan deviation: undici@8 types treat MockAgent's `.reply()`
callback as sync-return-only, so the 500ms-delayed reply used
in the timeout test is cast `as never` to bypass the overload
mismatch. Behavior is correct at runtime; the cast is local to
the test.
Verification: `npx vitest run tests/unit/LocalOllamaProvider.test.ts`
6 passed.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
71
tests/unit/LocalOllamaProvider.test.ts
Normal file
71
tests/unit/LocalOllamaProvider.test.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { MockAgent, setGlobalDispatcher, getGlobalDispatcher } from 'undici';
|
||||
import { LocalOllamaProvider } from '@main/ai/LocalOllamaProvider.js';
|
||||
|
||||
describe('LocalOllamaProvider', () => {
|
||||
let mock: MockAgent;
|
||||
let original: ReturnType<typeof getGlobalDispatcher>;
|
||||
|
||||
beforeEach(() => {
|
||||
original = getGlobalDispatcher();
|
||||
mock = new MockAgent();
|
||||
mock.disableNetConnect();
|
||||
setGlobalDispatcher(mock);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
setGlobalDispatcher(original);
|
||||
await mock.close();
|
||||
});
|
||||
|
||||
it('generate parses Ollama JSON', async () => {
|
||||
mock.get('http://localhost:11434').intercept({ path: '/api/generate', method: 'POST' }).reply(200, {
|
||||
response: JSON.stringify({ title: '회의', summary: '첫\n둘\n셋', tags: ['api'] })
|
||||
});
|
||||
const r = await new LocalOllamaProvider().generate({ text: 'x' });
|
||||
expect(r.title).toBe('회의');
|
||||
});
|
||||
|
||||
it('generate throws on non-JSON', async () => {
|
||||
mock.get('http://localhost:11434').intercept({ path: '/api/generate', method: 'POST' }).reply(200, {
|
||||
response: 'not json'
|
||||
});
|
||||
await expect(new LocalOllamaProvider().generate({ text: 'x' })).rejects.toThrow(/json/i);
|
||||
});
|
||||
|
||||
it('generate aborts on timeout', async () => {
|
||||
mock.get('http://localhost:11434').intercept({ path: '/api/generate', method: 'POST' }).reply((async () => {
|
||||
await new Promise<void>((r) => setTimeout(r, 500));
|
||||
return { statusCode: 200, data: '{}' };
|
||||
}) as never);
|
||||
await expect(
|
||||
new LocalOllamaProvider({ timeoutMs: 50 }).generate({ text: 'x' })
|
||||
).rejects.toThrow();
|
||||
}, 2000);
|
||||
|
||||
it('healthCheck ok=true when model present', async () => {
|
||||
mock.get('http://localhost:11434').intercept({ path: '/api/tags', method: 'GET' }).reply(200, {
|
||||
models: [{ name: 'gemma4:e4b' }]
|
||||
});
|
||||
const h = await new LocalOllamaProvider().healthCheck();
|
||||
expect(h.ok).toBe(true);
|
||||
expect(h.model).toBe('gemma4:e4b');
|
||||
});
|
||||
|
||||
it('healthCheck ok=false when missing', async () => {
|
||||
mock.get('http://localhost:11434').intercept({ path: '/api/tags', method: 'GET' }).reply(200, {
|
||||
models: [{ name: 'other:latest' }]
|
||||
});
|
||||
const h = await new LocalOllamaProvider().healthCheck();
|
||||
expect(h.ok).toBe(false);
|
||||
expect(h.reason).toMatch(/gemma4:e4b/);
|
||||
});
|
||||
|
||||
it('healthCheck ok=false on connection error', async () => {
|
||||
mock.get('http://localhost:11434').intercept({ path: '/api/tags', method: 'GET' })
|
||||
.replyWithError(new Error('ECONNREFUSED'));
|
||||
const h = await new LocalOllamaProvider().healthCheck();
|
||||
expect(h.ok).toBe(false);
|
||||
expect(h.reason).toMatch(/connect|refused|unreachable/i);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user