Files
TREK/client/src/repo/llmRepo.test.ts
T
RemEx b1c05bca83 feat(llm-parse): read a photo, as a booking or as a receipt in Costs
Whether the AI Parsing model reads images is an admin setting now, and it
lands with the two features that send a photo, so it has something to
control.

The setting is `vision: 'auto' | 'on' | 'off'` on the addon config. On
Automatic a local Ollama is asked what the model can do (`/api/show`), and a
cloud provider means no; Yes and No are applied as set. The old `multimodal`
flag is not read: the admin screens wrote it false on every save, so a
stored false meant nothing. The per-user toggle keeps its on/off meaning and
is read by the extraction now. The lookup is server-side only: capped read,
validated body, never throws, cached per server and model.
GET /api/llm/capabilities answers it for the signed-in caller, and both
photo pickers below ask it.

The booking import takes JPG, PNG and WEBP when the model reads images.
The photo goes as an image to every provider (image_url, an Anthropic image
block, Ollama's native chat with `images`), capped at 1600px, and skips KDE
Itinerary. A scanned PDF, which used to reach the model as empty text, has
its first two pages drawn by pdf-parse and sent the same way. The rest is
the import as it was: background job, review, the file attached to the
booking, the linked cost. On a real qwen3.5:4b a train ticket came back as
bare clock times until the router gave the model every type's field
meanings, and its price as an object inside a string, which read as 4900;
both are handled.

Costs gets Scan receipt: a dialog laid out like the booking import's takes
one photo, read on the import job queue, then reviewed in the existing
expense editor pre-filled with merchant, total, currency, day and lines,
the photo waiting in Receipts & Invoices to be uploaded on save. No table,
no migration, no category guess, no document-type detection.
2026-09-26 14:38:01 +02:00

50 lines
2.2 KiB
TypeScript

// FE-REPO-LLM-001 to FE-REPO-LLM-004
import { describe, it, expect, beforeEach } from 'vitest'
import { http, HttpResponse } from 'msw'
import { server } from '../../tests/helpers/msw/server'
import { readMultipart } from '../../tests/helpers/multipart'
import { llmRepo } from './llmRepo'
function setOnline(v: boolean): void {
Object.defineProperty(navigator, 'onLine', { value: v, writable: true, configurable: true })
}
beforeEach(() => setOnline(true))
describe('llmRepo', () => {
it('FE-REPO-LLM-001: readsPhotos answers what the server says the model reads', async () => {
server.use(http.get('/api/llm/capabilities', () => HttpResponse.json({ images: true })))
await expect(llmRepo.readsPhotos()).resolves.toBe(true)
server.use(http.get('/api/llm/capabilities', () => HttpResponse.json({ images: false })))
await expect(llmRepo.readsPhotos()).resolves.toBe(false)
})
it('FE-REPO-LLM-002: readsPhotos is false offline, without asking', async () => {
let asked = false
server.use(http.get('/api/llm/capabilities', () => { asked = true; return HttpResponse.json({ images: true }) }))
setOnline(false)
await expect(llmRepo.readsPhotos()).resolves.toBe(false)
expect(asked).toBe(false)
})
it('FE-REPO-LLM-003: scanReceipt uploads the photo as `file` and answers the job id', async () => {
let sent: string[] = []
server.use(http.post('/api/trips/7/budget/receipt-scan', async ({ request }) => {
sent = (await readMultipart(request)).filenames
return HttpResponse.json({ jobId: 'job-9' })
}))
const photo = new File(['jpeg'], 'bill.jpg', { type: 'image/jpeg' })
await expect(llmRepo.scanReceipt(7, photo)).resolves.toBe('job-9')
// One file part; jsdom names every File part 'blob' on the wire, so the name is not checked.
expect(sent).toHaveLength(1)
})
it('FE-REPO-LLM-004: scanReceipt refuses offline and rejects an answer without a job id', async () => {
setOnline(false)
await expect(llmRepo.scanReceipt(7, new File(['x'], 'a.jpg'))).rejects.toThrow(/connection/)
setOnline(true)
server.use(http.post('/api/trips/7/budget/receipt-scan', () => HttpResponse.json({})))
await expect(llmRepo.scanReceipt(7, new File(['x'], 'a.jpg'))).rejects.toThrow()
})
})