Files
accounted/extensions/general/mcp-server/__tests__/list-transactions-without-documents.test.ts
T
Jakob WennbergandClaude Fable 5 21512db81a feat(mcp): unify missing-document surfaces on one predicate (#876)
* feat(mcp): unify missing-document surfaces on one predicate (P1-3)

The two MCP surfaces told different truths: the transactions tool keyed
'has underlag' on transactions.document_id while the verifikat tool
keyed on document_attachments — and neither respected the source-type
semantics, version chains, or journal_entry_no_doc_required waivers
that lib/worklist's canonical count applies. Measured on prod: 22,046
waived verifikat still listed to agents, 2,370 doc-exempt source types
listed, ~87 docs attached to transactions but never propagated to the
verifikat, 1,100 transactions flagged missing-receipt although their
verifikat HAS the underlag.

One predicate now lives in SQL — posted, needs-doc source type
(mirrors NEEDS_DOC_SOURCE_TYPES), no current-version doc, no waiver:

- verifikat_without_documents RPC v2 adopts the canonical predicate.
- New transactions_without_documents RPC: the bank-driven subset of the
  same predicate, joined through transactions.journal_entry_id — a
  strict subset of the verifikat surface by construction. Rows expose
  qualified transaction_id (P1-2 forward-compat); bare id deprecated.
- Both tools become thin RPC wrappers; descriptions state the actual
  set relationship.
- lib/worklist countVerifikatMissingDocument delegates to the RPC
  (previously three full-table pulls set-differenced client-side) —
  badge count and agent surfaces can no longer drift.
- Backfill: propagate transaction-attached docs to their verifikat
  where the attachment was never linked (open periods only; never
  steals a doc linked to another verifikat).

pg-real: fixture matrix (no-doc/with-doc/waived/stale-version/
doc-exempt-source/import), strict-subset assertion, per-source-type pin
of the SQL list against the TS constant, tenant guard.

Part of dev_docs/mcp_optimization_plan.md (P1-3).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs(mcp): explicit grants restated + count-call comment (#876 review)

- Restate REVOKE/GRANT on verifikat_without_documents so the migration
  is self-contained (CREATE OR REPLACE preserves the 20260703130000
  grants — verified on prod: authenticated + service_role only).
- Comment on the p_limit:1 count call: total_count is computed over the
  full filtered set, independent of page size.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 10:56:14 +02:00

131 lines
4.3 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest'
import { createQueuedMockSupabase } from '@/tests/helpers'
import { tools } from '../server'
/**
* The tool is a thin wrapper over the transactions_without_documents RPC —
* the bank-driven subset of the verifikat surface, keyed on the SAME document
* truth (document_attachments + waivers), never transactions.document_id.
* Predicate semantics are pinned by
* tests/pg/document-surfaces-unification.pg.test.ts; these tests cover the
* wrapper contract (envelope unwrap, pagination math, error paths).
*/
const tool = tools.find((t) => t.name === 'gnubok_list_transactions_without_documents')!
function envelope(transactions: unknown[], totalCount: number) {
return { data: { ok: true, total_count: totalCount, transactions }, error: null }
}
const row = (id: string, jeId: string) => ({
id,
transaction_id: id,
date: '2026-04-12',
description: 'HOTELL ANGLAIS',
amount: -1247,
currency: 'SEK',
merchant_name: null,
reference: null,
is_business: null,
category: null,
journal_entry_id: jeId,
})
beforeEach(() => {
vi.clearAllMocks()
})
describe('gnubok_list_transactions_without_documents', () => {
it('is registered as a read-only paginated tool with qualified transaction_id', () => {
expect(tool).toBeDefined()
expect(tool.annotations?.readOnlyHint).toBe(true)
const schema = tool.outputSchema as { properties: Record<string, unknown> }
expect(schema.properties.transactions).toBeDefined()
expect(schema.properties.total_count).toBeDefined()
const items = (schema.properties.transactions as { items: { properties: Record<string, unknown> } })
.items
expect(items.properties.transaction_id).toBeDefined()
expect(items.properties.journal_entry_id).toBeDefined()
})
it('unwraps the RPC envelope and passes filters through', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue(envelope([row('t1', 'je-1')], 1))
const result = (await tool.execute(
{ limit: 20, since: '2026-01-01' },
'company-1',
'user-1',
supabase as never,
)) as {
transactions: Array<{ id: string; transaction_id: string; journal_entry_id: string }>
count: number
total_count: number
has_more: boolean
}
expect(supabase.rpc).toHaveBeenCalledWith('transactions_without_documents', {
p_company_id: 'company-1',
p_since: '2026-01-01',
p_limit: 20,
p_offset: 0,
})
expect(result.count).toBe(1)
expect(result.total_count).toBe(1)
expect(result.has_more).toBe(false)
expect(result.transactions[0].transaction_id).toBe('t1')
expect(result.transactions[0].journal_entry_id).toBe('je-1')
})
it('returns empty result when nothing matches', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue(envelope([], 0))
const result = (await tool.execute({}, 'company-1', 'user-1', supabase as never)) as {
count: number
total_count: number
has_more: boolean
}
expect(result.count).toBe(0)
expect(result.total_count).toBe(0)
expect(result.has_more).toBe(false)
})
it('signals more pages with next_offset advancing by rows consumed', async () => {
const page = Array.from({ length: 20 }, (_, i) => row(`t${i}`, `je-${i}`))
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue(envelope(page, 45))
const result = (await tool.execute(
{ limit: 20 },
'company-1',
'user-1',
supabase as never,
)) as {
has_more: boolean
next_offset?: number
}
expect(result.has_more).toBe(true)
expect(result.next_offset).toBe(20)
})
it('throws on an RPC error', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({ data: null, error: { message: 'connection refused' } })
await expect(tool.execute({}, 'company-1', 'user-1', supabase as never)).rejects.toThrow(
/connection refused/,
)
})
it('throws on a not-ok envelope (tenant guard)', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({ data: { ok: false, code: 'TRANSACTIONS_WITHOUT_DOCUMENTS_FORBIDDEN' }, error: null })
await expect(tool.execute({}, 'company-1', 'user-1', supabase as never)).rejects.toThrow(
/TRANSACTIONS_WITHOUT_DOCUMENTS_FORBIDDEN/,
)
})
})