Files
accounted/lib/agent/categorize/__tests__/candidates.test.ts
T
e7a5e65ecf feat(categorize): Tier 1 candidate gathering + the proposal route (#1781)
The auto-booking cascade end to end (retrieval → selector), minus the write.

- lib/agent/categorize/candidates.ts (Tier 1): assembles the deterministic
  candidate slate for a transaction — the learned counterparty template
  (strongest, carries its own VAT) plus mapping rules / patterns / per-merchant
  history via the same engine gnubok_suggest_categories uses. No model call.
  Deduped by account (highest confidence wins), capped; suggestions get the
  category's default VAT treatment derived.
- POST /api/agent/categorize: loads the transaction + company VAT context,
  runs Tier 1 → Tier 2 selectAccount, returns the proposed account + VAT +
  confidence + reasoning + the candidate slate. Never posts anything — the
  caller renders an approval card. Gated on configured (any provider incl.
  local), same gates as /api/agent/ask.

12 tests: candidate merge/dedupe/VAT-derivation, and the route (401/429/400/
403/404/503 + happy path threading entity type, VAT, underlag, samples).

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 13:32:09 +02:00

108 lines
4.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { SupabaseClient } from '@supabase/supabase-js'
import type { Transaction } from '@/types'
const getSuggestedCategories = vi.fn()
vi.mock('@/lib/transactions/category-suggestions', () => ({
getSuggestedCategories: (...a: unknown[]) => getSuggestedCategories(...a),
buildMerchantHistory: () => new Map(),
merchantHistoryFor: () => ({}),
}))
const findCounterpartyTemplate = vi.fn()
vi.mock('@/lib/bookkeeping/counterparty-templates', () => ({
findCounterpartyTemplate: (...a: unknown[]) => findCounterpartyTemplate(...a),
formatCounterpartyName: (n: string) => n,
}))
import { gatherCandidates } from '../candidates'
// Permissive chainable supabase: every builder method returns the chain, and
// the chain is awaitable (resolves {data: []}) so the two list queries settle.
function mockSupabase(): SupabaseClient {
const chain: Record<string, unknown> = {}
for (const m of ['from', 'select', 'or', 'eq', 'is', 'not', 'neq', 'order', 'limit']) {
chain[m] = () => chain
}
chain.then = (resolve: (v: { data: unknown[] }) => unknown) => resolve({ data: [] })
return chain as unknown as SupabaseClient
}
const TX = { id: 't1', merchant_name: 'Biltema', description: 'Kortköp', original_description: 'BILTEMA' } as unknown as Transaction
function cpMatch(over: Record<string, unknown> = {}) {
return {
template: {
debit_account: '5410',
counterparty_name: 'Biltema',
vat_treatment: 'standard_25',
occurrence_count: 4,
category: 'expense_consumables',
...over,
},
matchMethod: 'exact_normalized',
confidence: 0.9,
}
}
beforeEach(() => {
vi.clearAllMocks()
findCounterpartyTemplate.mockResolvedValue(null)
getSuggestedCategories.mockReturnValue([])
})
describe('gatherCandidates', () => {
it('puts the counterparty template first, carrying its own VAT', async () => {
findCounterpartyTemplate.mockResolvedValue(cpMatch())
const out = await gatherCandidates(mockSupabase(), 'c1', TX)
expect(out[0]).toMatchObject({
account: '5410',
source: 'counterparty_template',
vatTreatment: 'standard_25',
confidence: 0.9,
})
expect(out[0].matchReason).toContain('4 tidigare')
})
it('derives VAT for rule/pattern/history suggestions from their category', async () => {
getSuggestedCategories.mockReturnValue([
{ category: 'expense_bank_fees', label: 'Bankavgift', account: '6570', confidence: 0.8, source: 'mapping_rule' },
{ category: 'expense_software', label: 'Programvara', account: '5420', confidence: 0.6, source: 'pattern' },
])
const out = await gatherCandidates(mockSupabase(), 'c1', TX)
const bank = out.find((c) => c.account === '6570')!
const soft = out.find((c) => c.account === '5420')!
expect(bank.vatTreatment).toBeNull() // bank fees are VAT-exempt
expect(soft.vatTreatment).toBe('standard_25')
})
it('de-duplicates by account, keeping the highest confidence', async () => {
findCounterpartyTemplate.mockResolvedValue(cpMatch({ debit_account: '5410' }))
getSuggestedCategories.mockReturnValue([
{ category: 'expense_consumables', label: 'Material', account: '5410', confidence: 0.56, source: 'history' },
])
const out = await gatherCandidates(mockSupabase(), 'c1', TX)
const fivefour = out.filter((c) => c.account === '5410')
expect(fivefour).toHaveLength(1)
expect(fivefour[0].source).toBe('counterparty_template') // 0.9 > 0.56
})
it('skips suggestions with no account and sorts by confidence, capped', async () => {
getSuggestedCategories.mockReturnValue([
{ category: 'expense_other', label: 'x', account: null, confidence: 0.9, source: 'pattern' },
{ category: 'expense_office', label: 'Kontor', account: '6110', confidence: 0.5, source: 'history' },
{ category: 'expense_travel', label: 'Resor', account: '5800', confidence: 0.7, source: 'mapping_rule' },
])
const out = await gatherCandidates(mockSupabase(), 'c1', TX, 2)
expect(out.map((c) => c.account)).toEqual(['5800', '6110']) // no null, sorted desc, capped at 2
})
it('returns just the suggestions when there is no counterparty match', async () => {
getSuggestedCategories.mockReturnValue([
{ category: 'expense_office', label: 'Kontor', account: '6110', confidence: 0.5, source: 'history' },
])
const out = await gatherCandidates(mockSupabase(), 'c1', TX)
expect(out).toHaveLength(1)
expect(out[0].source).toBe('history')
})
})