fix(desktop): add precise local OCR reading
This commit is contained in:
parent
59cdddbecb
commit
25fd3ac004
30 changed files with 626 additions and 15 deletions
113
apps/desktop/tests/main/services/ollama-precision-engine.test.ts
Normal file
113
apps/desktop/tests/main/services/ollama-precision-engine.test.ts
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { ErrorCode } from '@d3ro/core/errors'
|
||||
import { createOllamaPrecisionOcrEngine } from '../../../src/main/services/screen-ocr/ollama-precision-engine'
|
||||
|
||||
type Handler = (url: string, init?: RequestInit) => Promise<Response>
|
||||
|
||||
function json(body: unknown): Response {
|
||||
return new Response(JSON.stringify(body), { status: 200, headers: { 'content-type': 'application/json' } })
|
||||
}
|
||||
|
||||
function harness(handler: Handler) {
|
||||
const calls: string[] = []
|
||||
const fetchFn: typeof fetch = async (input, init) => {
|
||||
const url = String(input)
|
||||
calls.push(url)
|
||||
return handler(url, init)
|
||||
}
|
||||
const ensureRunning = vi.fn(async () => 'running')
|
||||
const engine = createOllamaPrecisionOcrEngine({
|
||||
serverUrl: 'http://127.0.0.1:11434',
|
||||
fetchFn,
|
||||
ensureRunning,
|
||||
readImage: async () => Buffer.from('image')
|
||||
})
|
||||
return { engine, calls, ensureRunning }
|
||||
}
|
||||
|
||||
const modelPresent = json({ models: [{ name: 'glm-ocr:latest' }] })
|
||||
const generated = (text: string, reason = 'stop') => json({ done: true, done_reason: reason, response: text })
|
||||
|
||||
describe('Ollama 정밀 OCR 엔진', () => {
|
||||
it('정밀인식_모델이있으면_다운로드없이공식프롬프트와로컬이미지로호출한다', async () => {
|
||||
const requests: Array<{ url: string; body: Record<string, unknown> }> = []
|
||||
const { engine, calls } = harness(async (url, init) => {
|
||||
if (url.endsWith('/api/tags')) return modelPresent
|
||||
if (url.endsWith('/api/generate')) {
|
||||
requests.push({ url, body: JSON.parse(String(init?.body)) as Record<string, unknown> })
|
||||
return generated('정확한 결과')
|
||||
}
|
||||
throw new Error(`unexpected ${url}`)
|
||||
})
|
||||
const result = await engine.recognize({ imagePath: 'crop.png', maxOutputTokens: 1024 })
|
||||
expect(result).toBe('정확한 결과')
|
||||
expect(calls.some((url) => url.endsWith('/api/pull'))).toBe(false)
|
||||
expect(requests[0].body).toMatchObject({
|
||||
model: 'glm-ocr:latest',
|
||||
prompt: 'Text Recognition:',
|
||||
images: [Buffer.from('image').toString('base64')],
|
||||
stream: false,
|
||||
keep_alive: '2m',
|
||||
options: { temperature: 0, num_ctx: 8192, num_predict: 1024 }
|
||||
})
|
||||
})
|
||||
|
||||
it('정밀인식_모델이없으면_다운로드를완료한뒤인식한다', async () => {
|
||||
const { engine, calls } = harness(async (url) => {
|
||||
if (url.endsWith('/api/tags')) return json({ models: [] })
|
||||
if (url.endsWith('/api/pull')) return new Response('{"status":"downloading"}\n{"status":"success"}\n')
|
||||
if (url.endsWith('/api/generate')) return generated('결과')
|
||||
throw new Error(`unexpected ${url}`)
|
||||
})
|
||||
await expect(engine.recognize({ imagePath: 'crop.png', maxOutputTokens: 512 })).resolves.toBe('결과')
|
||||
expect(calls.findIndex((url) => url.endsWith('/api/pull'))).toBeLessThan(calls.findIndex((url) => url.endsWith('/api/generate')))
|
||||
})
|
||||
|
||||
it('정밀인식_토큰한도로잘린결과_성공으로처리하지않는다', async () => {
|
||||
const { engine } = harness(async (url) => url.endsWith('/api/tags') ? modelPresent : generated('불완전한 결과', 'length'))
|
||||
await expect(engine.recognize({ imagePath: 'crop.png', maxOutputTokens: 512 })).rejects.toMatchObject({
|
||||
code: ErrorCode.ScreenOcrRecognitionFailed
|
||||
})
|
||||
})
|
||||
|
||||
it('정밀인식_빈결과_성공으로처리하지않는다', async () => {
|
||||
const { engine } = harness(async (url) => url.endsWith('/api/tags') ? modelPresent : generated(' \n'))
|
||||
await expect(engine.recognize({ imagePath: 'crop.png', maxOutputTokens: 512 })).rejects.toMatchObject({
|
||||
code: ErrorCode.ScreenOcrRecognitionFailed
|
||||
})
|
||||
})
|
||||
|
||||
it('정밀인식_원격Ollama주소_화면을볼내지않고차단한다', async () => {
|
||||
const fetchFn = vi.fn(async () => json({}))
|
||||
const engine = createOllamaPrecisionOcrEngine({
|
||||
serverUrl: 'https://ollama.example.com',
|
||||
fetchFn,
|
||||
ensureRunning: async () => 'running',
|
||||
readImage: async () => Buffer.from('image')
|
||||
})
|
||||
await expect(engine.recognize({ imagePath: 'crop.png', maxOutputTokens: 512 })).rejects.toMatchObject({
|
||||
code: ErrorCode.ScreenOcrUnavailable
|
||||
})
|
||||
expect(fetchFn).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('정밀인식_다운로드중취소_인식을시작하지않는다', async () => {
|
||||
const controller = new AbortController()
|
||||
let pullStarted = false
|
||||
const { engine, calls } = harness(async (url, init) => {
|
||||
if (url.endsWith('/api/tags')) return json({ models: [] })
|
||||
if (url.endsWith('/api/pull')) {
|
||||
pullStarted = true
|
||||
return new Promise<Response>((_resolve, reject) => {
|
||||
init?.signal?.addEventListener('abort', () => reject(new Error('aborted')), { once: true })
|
||||
})
|
||||
}
|
||||
return generated('실행하면 안 되는 결과')
|
||||
})
|
||||
const pending = engine.recognize({ imagePath: 'crop.png', maxOutputTokens: 512, signal: controller.signal })
|
||||
await vi.waitFor(() => expect(pullStarted).toBe(true))
|
||||
controller.abort()
|
||||
await expect(pending).rejects.toMatchObject({ code: ErrorCode.ScreenOcrRecognitionFailed })
|
||||
expect(calls.some((url) => url.endsWith('/api/generate'))).toBe(false)
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue