Files
accounted/app/api/agent/categorize/__tests__/route.test.ts
T
72c81c21e7 feat(categorize): feed the selector the underlag, not just the bank line (#1785)
The highest-leverage quality lever for real users. A prod read showed the
majority are cold-start (365 companies, 32.7k unbooked transactions, median 0
counterparty templates), so the LLM selector carries them — and it was only
seeing the bank line (merchant + amount), never the receipt.

- lib/agent/categorize/underlag.ts: gathers the matched receipt/invoice text
  for a transaction (receipts.matched_transaction_id + invoice_inbox_items
  .matched_transaction_id + the transaction's own attached document) and renders
  it as bounded Swedish text — supplier, date, total, moms, line items. Same
  sources the categorization intent reads, as a string not a tool loop. Core
  queries the tables directly (no @/extensions import). Best-effort: '' on any
  failure.
- POST /api/agent/categorize gathers it server-side when the caller didn't
  supply `underlag`, so the model reasons over the actual supplier + line items.

Server-side only, no client change. 31 categorize tests green; lint + guards +
scoped typecheck clean.

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-21 16:00:43 +02:00

122 lines
5.9 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { NextResponse } from 'next/server'
import { createMockRequest, parseJsonResponse } from '@/tests/helpers'
const requireAuthMock = vi.fn()
vi.mock('@/lib/auth/require-auth', () => ({ requireAuth: () => requireAuthMock() }))
vi.mock('@/lib/company/context', () => ({ getActiveCompanyId: vi.fn().mockResolvedValue('company-1') }))
const checkRate = vi.fn()
vi.mock('@/lib/rate-limits/agent', () => ({
checkAgentRateLimit: () => checkRate(),
agentRateLimitResponseBody: () => ({ error: 'rate' }),
}))
vi.mock('@/lib/sandbox/guard', () => ({ guardSandbox: vi.fn().mockResolvedValue(null) }))
const requireCapability = vi.fn()
vi.mock('@/lib/entitlements/has-capability', () => ({ requireCapability: () => requireCapability() }))
vi.mock('@/lib/entitlements/keys', () => ({ CAPABILITY: { ai: 'ai' } }))
const aiStatus = vi.fn()
vi.mock('@/lib/ai', () => ({ getAiStatus: () => aiStatus() }))
const gatherCandidates = vi.fn()
vi.mock('@/lib/agent/categorize/candidates', () => ({ gatherCandidates: (...a: unknown[]) => gatherCandidates(...a) }))
const gatherUnderlag = vi.fn()
vi.mock('@/lib/agent/categorize/underlag', () => ({ gatherUnderlag: (...a: unknown[]) => gatherUnderlag(...a) }))
const selectAccount = vi.fn()
vi.mock('@/lib/agent/categorize/select-account', () => ({ selectAccount: (...a: unknown[]) => selectAccount(...a) }))
import { POST } from '../route'
// supabase router: membership + transactions + companies + company_settings.
function makeSupabase(opts: { tx?: unknown } = {}) {
return {
from(table: string) {
const rows: Record<string, unknown> = {
company_members: { user_id: 'user-1' },
transactions: opts.tx === undefined ? { id: 'tx-1' } : opts.tx,
companies: { entity_type: 'aktiebolag' },
company_settings: { vat_registered: true },
}
const chain = {
select: () => chain,
eq: () => chain,
maybeSingle: async () => ({ data: rows[table] ?? null }),
}
return chain
},
}
}
const supabase = makeSupabase()
const VALID_TX = '11111111-1111-4111-8111-111111111111'
const body = (o: Record<string, unknown> = {}) => ({ transaction_id: VALID_TX, ...o })
beforeEach(() => {
vi.clearAllMocks()
requireAuthMock.mockResolvedValue({ user: { id: 'user-1' }, supabase, error: null })
checkRate.mockResolvedValue({ ok: true })
requireCapability.mockResolvedValue(null)
aiStatus.mockReturnValue({ configured: true })
gatherCandidates.mockResolvedValue([{ account: '5410', label: 'Material', vatTreatment: 'standard_25', source: 'counterparty_template', confidence: 0.9 }])
gatherUnderlag.mockResolvedValue('Kvitto: Biltema, totalt 499 SEK.')
selectAccount.mockResolvedValue({
account: '5410', category: null, vatTreatment: 'standard_25', reverseCharge: false,
confidence: 0.86, modelConfidence: 'high', agreement: 1, reasoning: 'r',
choice: { kind: 'candidate', account: '5410' }, model: 'qwen3.8', fromCandidate: true,
})
})
describe('POST /api/agent/categorize', () => {
it('401 when unauthenticated', async () => {
requireAuthMock.mockResolvedValue({ user: null, supabase, error: NextResponse.json({ error: 'x' }, { status: 401 }) })
expect((await POST(createMockRequest('/x', { method: 'POST', body: body() }))).status).toBe(401)
})
it('429 when rate limited', async () => {
checkRate.mockResolvedValue({ ok: false })
expect((await POST(createMockRequest('/x', { method: 'POST', body: body() }))).status).toBe(429)
})
it('400 on a missing/invalid transaction_id', async () => {
expect((await POST(createMockRequest('/x', { method: 'POST', body: {} }))).status).toBe(400)
expect((await POST(createMockRequest('/x', { method: 'POST', body: { transaction_id: 'nope' } }))).status).toBe(400)
})
it('403 without the ai capability', async () => {
requireCapability.mockResolvedValue(NextResponse.json({ error: 'pay' }, { status: 403 }))
expect((await POST(createMockRequest('/x', { method: 'POST', body: body() }))).status).toBe(403)
})
it('503 when no backend is configured', async () => {
aiStatus.mockReturnValue({ configured: false })
const res = await POST(createMockRequest('/x', { method: 'POST', body: body() }))
const { status, body: b } = await parseJsonResponse<{ code: string }>(res)
expect(status).toBe(503)
expect(b.code).toBe('ai_unconfigured')
expect(selectAccount).not.toHaveBeenCalled()
})
it('404 when the transaction is not found / not this company', async () => {
requireAuthMock.mockResolvedValue({ user: { id: 'user-1' }, supabase: makeSupabase({ tx: null }), error: null })
const res = await POST(createMockRequest('/x', { method: 'POST', body: body() }))
expect(res.status).toBe(404)
expect(selectAccount).not.toHaveBeenCalled()
})
it('returns the selection + candidate slate on the happy path', async () => {
const res = await POST(createMockRequest('/x', { method: 'POST', body: body({ samples: 3, underlag: 'Biltema AB 499 kr' }) }))
const { status, body: b } = await parseJsonResponse<{
data: { account: string; confidence: number; candidates: { account: string }[] }
}>(res)
expect(status).toBe(200)
expect(b.data.account).toBe('5410')
expect(b.data.confidence).toBe(0.86)
expect(b.data.candidates[0].account).toBe('5410')
// A caller-supplied underlag is used verbatim (no server gather).
expect(gatherUnderlag).not.toHaveBeenCalled()
expect(selectAccount).toHaveBeenCalledWith(
expect.objectContaining({ entityType: 'aktiebolag', vatRegistered: true, underlag: 'Biltema AB 499 kr', samples: 3 }),
)
})
it('gathers underlag server-side when the caller did not supply it', async () => {
await POST(createMockRequest('/x', { method: 'POST', body: body() }))
expect(gatherUnderlag).toHaveBeenCalled()
expect(selectAccount).toHaveBeenCalledWith(
expect.objectContaining({ underlag: 'Kvitto: Biltema, totalt 499 SEK.' }),
)
})
})