Files
accounted/app/api/pending-operations/__tests__/route.test.ts
T
Jakob WennbergandClaude Opus 4.6 f3ae3cd361 feat: event log, pending operations, and MCP staging (#135)
* feat: event log, pending operations, and MCP staging

- Event log system: persist bus events to event_log table for external
  automation platforms. Batch insert for transaction.synced. Daily
  cleanup cron at 02:00 UTC.
- Pending operations: MCP write tools (categorize, create customer,
  create invoice) now stage to pending_operations instead of executing
  directly. Users review and commit/reject from /pending in the web UI.
- Granskning page: card-based review UI with expandable previews,
  commit/reject dialogs. Only shown in nav when pending ops exist.
- Commit route re-executes using core lib functions (no extension
  imports). Guards against stale state (double-commit, deleted entities).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: stage new MCP write tools after main merge

Add staging for 4 new write tools from #133:
- mark_invoice_paid, send_invoice, mark_invoice_sent,
  match_transaction_invoice
- Expand pending_operations CHECK constraint
- Add commit executors with full execution logic
- Add UI labels and generic preview component
- Remove confirm parameter from categorize (single-call staging)
- Fix UUID in pending op title (fetch transaction description)
- Hide Granskning nav when no pending ops

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: address PR review feedback

- Fix TS build error: use `select('*, customer:customers(*)')` for
  match_transaction_invoice to avoid array type inference
- Add status guard to commitSendInvoice (prevents duplicate sends)
- Replace auth.admin.getUserById with user email from session auth
- Restore optimistic lock check in commitMatchTransactionInvoice
- Fix tool description typo: expense_software → expense_office

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 10:07:36 +01:00

97 lines
2.8 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import {
createMockRequest,
parseJsonResponse,
createQueuedMockSupabase,
} from '@/tests/helpers'
const { supabase: mockSupabase, enqueue, reset } = createQueuedMockSupabase()
vi.mock('@/lib/supabase/server', () => ({
createClient: () => Promise.resolve(mockSupabase),
}))
import { GET } from '../route'
describe('GET /api/pending-operations', () => {
const mockUser = { id: 'user-1', email: 'test@test.se' }
const sampleOps = [
{
id: 'op-1',
user_id: 'user-1',
operation_type: 'categorize_transaction',
status: 'pending',
title: 'Kategorisera: CLAS OHLSON -523 SEK',
params: { transaction_id: 'tx-1', category: 'expense_office' },
preview_data: { debit_account: '6100', credit_account: '1930', amount: 523 },
result_data: null,
created_at: '2026-03-25T10:00:00Z',
},
{
id: 'op-2',
user_id: 'user-1',
operation_type: 'create_customer',
status: 'pending',
title: 'Ny kund: Acme AB',
params: { name: 'Acme AB', customer_type: 'swedish_business' },
preview_data: { name: 'Acme AB', customer_type: 'swedish_business' },
result_data: null,
created_at: '2026-03-25T10:01:00Z',
},
]
beforeEach(() => {
vi.clearAllMocks()
reset()
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: mockUser } })
})
it('returns 401 when not authenticated', async () => {
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: null } })
const request = createMockRequest('/api/pending-operations')
const response = await GET(request)
const { status, body } = await parseJsonResponse(response)
expect(status).toBe(401)
expect(body).toEqual({ error: 'Unauthorized' })
})
it('returns pending operations', async () => {
enqueue({ data: sampleOps, count: 2 })
const request = createMockRequest('/api/pending-operations')
const response = await GET(request)
const { status, body } = await parseJsonResponse<{
data: typeof sampleOps
count: number
}>(response)
expect(status).toBe(200)
expect(body.data).toHaveLength(2)
expect(body.count).toBe(2)
})
it('filters by status parameter', async () => {
enqueue({ data: [], count: 0 })
const request = createMockRequest('/api/pending-operations', {
searchParams: { status: 'committed' },
})
const response = await GET(request)
const { status } = await parseJsonResponse(response)
expect(status).toBe(200)
})
it('rejects invalid status parameter', async () => {
const request = createMockRequest('/api/pending-operations', {
searchParams: { status: 'invalid' },
})
const response = await GET(request)
const { status } = await parseJsonResponse(response)
expect(status).toBe(400)
})
})