524d9978f1
* fix(migration): resumable underlag import without inline extraction, same-origin MCP storage URLs The Fortnox underlag import ran every file's AI extraction inline inside one request and hit the hosted 300 s function limit after ~17 of 113 files (twice on 2026-08-21); the UI showed the generic "underlagen kunde inte importeras" although the files it did reach were linked. The import now works in time-budgeted slices with a stable cursor (the UI loops until the server reports the end and shows "x av y") and opts out of extraction (extractionOwner 'none', stamped skipped:opted_out): every file is linked to its posted verifikat on arrival, so the booking is already known. MCP signed Storage URLs (upload_url, signed_url, download_url) are served through a same-origin proxy, /api/storage/[...path], because Claude Desktop's sandbox only reaches the MCP host and blocked the PUT to <project>.supabase.co. The signed token stays the only credential; the proxy forwards only signed documents-bucket paths to our own Storage host and is a no-op rewrite when NEXT_PUBLIC_APP_URL is unset. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013YoZ8iboyTj221axW6Gdtm * fix(mcp): keep the storage-proxy note out of the size-capped tool descriptions The per-tool 280-char cap and the tools/list payload ceiling both tripped on the two sentences added to gnubok_create_document_upload and gnubok_get_document_content; the why now lives in a code comment. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013YoZ8iboyTj221axW6Gdtm * fix(review): id cursor, stall = error, capped upload body, encoded dot segments Review follow-ups on #1783: - the import cursor is the last handled provider attachment id, not an index, so a file Fortnox adds or removes mid-sweep shifts nothing - a partial answer whose cursor does not advance (or the round guard) is reported as ARCIM_DOCUMENT_IMPORT_STALLED instead of "complete"; the slices already landed stay reported and the retry button resumes - the storage proxy reads the PUT body as a capped stream instead of buffering an unbounded payload before measuring it - object paths are rejected when any segment decodes to "." or ".." (or holds a separator), and the URL fetch() would actually request is re-checked against the allowlist after normalisation - download_url description no longer claims a direct Storage URL Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013YoZ8iboyTj221axW6Gdtm --------- Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
166 lines
6.7 KiB
TypeScript
166 lines
6.7 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { createQueuedMockSupabase } from '@/tests/helpers'
|
|
|
|
const { supabase, enqueue, reset, findCalls } = createQueuedMockSupabase()
|
|
|
|
vi.mock('@/lib/supabase/server', () => ({
|
|
createServiceClient: () => supabase,
|
|
}))
|
|
|
|
const extractMock = vi.fn()
|
|
vi.mock('@/extensions/general/invoice-inbox/lib/extract-invoice-fields', () => ({
|
|
extractInvoiceFields: (...args: unknown[]) => extractMock(...args),
|
|
}))
|
|
|
|
const hasCapabilityMock = vi.fn()
|
|
vi.mock('@/lib/entitlements/has-capability', () => ({
|
|
hasCapability: (...args: unknown[]) => hasCapabilityMock(...args),
|
|
}))
|
|
|
|
const aiStatusMock = vi.fn()
|
|
vi.mock('@/lib/ai', () => ({
|
|
getAiStatus: () => aiStatusMock(),
|
|
}))
|
|
|
|
import { documentExtractionExtension } from '../index'
|
|
|
|
const handler = documentExtractionExtension.eventHandlers![0].handler
|
|
|
|
function doc(overrides: Record<string, unknown> = {}) {
|
|
return {
|
|
id: 'doc-1',
|
|
company_id: 'company-1',
|
|
file_name: 'kvitto.pdf',
|
|
mime_type: 'application/pdf',
|
|
storage_path: 'company-1/user-1/kvitto.pdf',
|
|
upload_source: 'file_upload',
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
function payload(overrides: Record<string, unknown> = {}, document = doc()) {
|
|
return { document, userId: 'user-1', companyId: 'company-1', ...overrides }
|
|
}
|
|
|
|
/** extraction_model of the LAST document_attachments update, or undefined. */
|
|
function lastStamp(): string | undefined {
|
|
const updates = findCalls('document_attachments', 'update')
|
|
const last = updates[updates.length - 1]?.[0] as { extraction_model?: string } | undefined
|
|
return last?.extraction_model
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
reset()
|
|
aiStatusMock.mockReturnValue({ configured: true, assistantAvailable: true })
|
|
hasCapabilityMock.mockResolvedValue(true)
|
|
extractMock.mockResolvedValue({
|
|
data: { supplier: { name: 'Elgiganten' } },
|
|
rawText: '{"supplier":{"name":"Elgiganten"}}',
|
|
model: 'eu.anthropic.claude-sonnet-5',
|
|
})
|
|
})
|
|
|
|
describe('document-extraction handler', () => {
|
|
// THE dedupe: inbox-owned documents are extracted (and mirrored) by the
|
|
// inbox itself. The handler used to race it and pay a second model call.
|
|
it('stamps and skips when the uploader opted out (already-booked provider underlag)', async () => {
|
|
await handler(payload({ extractionOwner: 'none' }))
|
|
|
|
expect(extractMock).not.toHaveBeenCalled()
|
|
expect(lastStamp()).toBe('skipped:opted_out')
|
|
})
|
|
|
|
it('yields entirely when the inbox owns extraction', async () => {
|
|
await handler(payload({ extractionOwner: 'invoice-inbox' }))
|
|
expect(supabase.from).not.toHaveBeenCalled()
|
|
expect(extractMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('stamps unsupported types from the payload without reading the row', async () => {
|
|
enqueue({ data: null }) // the stamp update
|
|
await handler(payload({}, doc({ mime_type: 'application/json' })))
|
|
expect(findCalls('document_attachments', 'select')).toHaveLength(0)
|
|
expect(lastStamp()).toBe('skipped:unsupported_mime')
|
|
expect(extractMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
// Our own invoice PDFs, payout files, filings: nothing to read, paid calls
|
|
// to waste (on hosted and on a BYO-key self-host).
|
|
it('stamps system-generated documents instead of extracting them', async () => {
|
|
enqueue({ data: null })
|
|
await handler(payload({}, doc({ upload_source: 'system' })))
|
|
expect(lastStamp()).toBe('skipped:system_generated')
|
|
expect(extractMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('does nothing for a row that was already attempted', async () => {
|
|
enqueue({ data: { id: 'doc-1', mime_type: 'application/pdf', storage_path: 'p', extracted_at: '2026-08-20T00:00:00Z' } })
|
|
await handler(payload())
|
|
expect(findCalls('document_attachments', 'update')).toHaveLength(0)
|
|
expect(extractMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
// Self-host without an AI key: stamp so the status route answers
|
|
// 'disabled' on the first poll instead of after a 30 s timeout.
|
|
it('stamps ai_unconfigured when the deployment has no AI', async () => {
|
|
aiStatusMock.mockReturnValue({ configured: false, assistantAvailable: false })
|
|
enqueue({ data: { id: 'doc-1', mime_type: 'application/pdf', storage_path: 'p', extracted_at: null } })
|
|
enqueue({ data: null })
|
|
await handler(payload())
|
|
expect(lastStamp()).toBe('skipped:ai_unconfigured')
|
|
expect(hasCapabilityMock).not.toHaveBeenCalled()
|
|
expect(extractMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
// The paywall, made visible: 309 of the 327 never-extracted uploads in a
|
|
// 30-day prod window belonged to companies without the ai capability.
|
|
it('stamps no_ai_entitlement for companies without the ai capability', async () => {
|
|
hasCapabilityMock.mockResolvedValue(false)
|
|
enqueue({ data: { id: 'doc-1', mime_type: 'application/pdf', storage_path: 'p', extracted_at: null } })
|
|
enqueue({ data: null })
|
|
await handler(payload())
|
|
expect(lastStamp()).toBe('skipped:no_ai_entitlement')
|
|
expect(extractMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('stamps a storage download failure', async () => {
|
|
enqueue({ data: { id: 'doc-1', mime_type: 'application/pdf', storage_path: 'p', extracted_at: null } })
|
|
enqueue({ data: null })
|
|
supabase.storage.from.mockReturnValueOnce({
|
|
download: vi.fn().mockResolvedValue({ data: null, error: { message: 'boom' } }),
|
|
})
|
|
await handler(payload())
|
|
expect(lastStamp()).toBe('failed:storage_download')
|
|
expect(extractMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('persists the result with the model that answered', async () => {
|
|
enqueue({ data: { id: 'doc-1', mime_type: 'application/pdf', storage_path: 'p', extracted_at: null } })
|
|
enqueue({ data: null })
|
|
await handler(payload())
|
|
expect(extractMock).toHaveBeenCalledWith(expect.objectContaining({ mimeType: 'application/pdf', fileName: 'kvitto.pdf' }))
|
|
const updates = findCalls('document_attachments', 'update')
|
|
expect(updates[updates.length - 1][0]).toMatchObject({
|
|
extracted_data: { supplier: { name: 'Elgiganten' } },
|
|
extraction_model: 'eu.anthropic.claude-sonnet-5',
|
|
})
|
|
})
|
|
|
|
it('stamps the skip reason the extractor reports (no vision, rasterizer missing, ...)', async () => {
|
|
extractMock.mockResolvedValue({ data: {}, rawText: null, skipped: 'pdf_rasterizer_missing' })
|
|
enqueue({ data: { id: 'doc-1', mime_type: 'application/pdf', storage_path: 'p', extracted_at: null } })
|
|
enqueue({ data: null })
|
|
await handler(payload())
|
|
expect(lastStamp()).toBe('skipped:pdf_rasterizer_missing')
|
|
})
|
|
|
|
it('stamps failed:no_raw_text when the model call produced nothing parseable', async () => {
|
|
extractMock.mockResolvedValue({ data: {}, rawText: null })
|
|
enqueue({ data: { id: 'doc-1', mime_type: 'application/pdf', storage_path: 'p', extracted_at: null } })
|
|
enqueue({ data: null })
|
|
await handler(payload())
|
|
expect(lastStamp()).toBe('failed:no_raw_text')
|
|
})
|
|
})
|