Files
accounted/extensions/general/mcp-server/__tests__/categorize-already-booked.test.ts
T
67e878fb23 fix(mcp): reject unparseable voucher lines and allocation kinds; already-booked and unlinkable say why; N:1 reconcile groups survive staging (#2171)
* fix(mcp): reject unparseable voucher lines and allocation kinds; already-booked and unlinkable say why; N:1 reconcile groups survive staging

Four MCP feedback reports about the same failure class: the server does no
runtime validation of inputSchema, so a shape mistake was coerced into a
wrong-but-well-formed call, and the error the agent finally saw pointed at
the wrong thing.

- create_voucher / correct_entry: a line naming neither debit_amount nor
  credit_amount was `Number(undefined) || 0`-ed into 0/0, and the balance
  check reported "debits 0 SEK, credits 0 SEK" for four perfectly balanced
  formats ({debit}, {amount, side}, {debitAmount}, signed amount). The line
  shape is now checked first, the error names the keys it got and shows a
  valid line, and a non-numeric amount is rejected as such (seq 318571).
- match_batch_allocate: kind is the key every guard branches on (direction
  vs sign, required id per kind, tenant pre-check on the invoices). With
  kind absent none of them fired: an incoming +50 359 SEK payment against
  three kundfakturor staged as allocations_kind "supplier_invoice" with zero
  invoice checks. A missing or unknown kind is now rejected before any
  query, with the id field that goes with each kind (seq 319919).
- categorize_transaction on an already-booked transaction returned the
  core's success-shaped object, which fails STAGED_OPERATION_SCHEMA on
  strict clients: the agent saw "Structured content does not match the
  tool's output schema" and never the reason. It now throws, naming the
  existing journal_entry_id (seq 288574).
- reconcile_match: the dry run flattens a pair into one link per outside
  row, and the staging rebuild put each back as its own 1:1 pair, so an N:1
  group (Skatteverket "Avdragen skatt" + "Arbetsgivaravgift" against one
  1630 verifikat, sum exact) reached the executor as N pairs each asked to
  settle the whole verifikat: PAIR_NOT_CLOSED on all 18. Links sharing a
  verifikat now fold back into one pair, mirroring the existing 1:N fold,
  and "No linkable pairs" carries the dry run's skip reasons (seq 292682).

No tools/list payload change: no schema text touched.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013yw62FMXGSzo6icFDiBwP3

* docs(decisions): N:1 reconcile fold at staging

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013yw62FMXGSzo6icFDiBwP3

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-04 09:27:26 +02:00

64 lines
2.1 KiB
TypeScript

/**
* gnubok_categorize_transaction on a transaction that already has a
* journal entry.
*
* categorizeTransactionCore returns a success-shaped object for this case
* ({ success: true, journal_entry_created: false, journal_entry_error }),
* which the tool used to pass through as its result. STAGED_OPERATION_SCHEMA
* requires staged/risk_level/actor/message/preview, so strict clients
* rejected the structured content and the agent saw only "Structured content
* does not match the tool's output schema" with the real reason swallowed
* (feedback seq 288574). The dispatcher's isError path carries no
* structuredContent, so an Error is the only schema-conformant way out.
*/
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createQueuedMockSupabase } from '@/tests/helpers'
vi.mock('@/lib/transactions/booking-duplicate-detection', () => ({
detectBookingDuplicate: vi.fn().mockResolvedValue(null),
}))
import { tools } from '../server'
const categorize = tools.find((t) => t.name === 'gnubok_categorize_transaction')!
const TX_ID = '00000000-0000-4000-8000-0000000000aa'
const JE_ID = '00000000-0000-4000-8000-0000000000bb'
beforeEach(() => {
vi.clearAllMocks()
})
describe('gnubok_categorize_transaction: already booked', () => {
it('throws a plain error naming the existing verifikat instead of returning an off-schema object', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({
data: {
id: TX_ID,
date: '2026-08-26',
amount: 25000,
currency: 'SEK',
amount_sek: 25000,
exchange_rate: null,
description: 'Aktiekapital',
merchant_name: null,
cash_account_id: null,
document_id: null,
journal_entry_id: JE_ID,
is_business: true,
},
error: null,
}) // core: transactions select('*')
await expect(
categorize.execute(
{ transaction_id: TX_ID, category: 'income_other' },
'company-1',
'user-1',
supabase as never,
{ type: 'api_key' } as never,
),
).rejects.toThrow(new RegExp(`already booked \\(journal_entry_id ${JE_ID}\\)`))
})
})