* feat: multi-tenant company refactor (GNU-19) Introduce companies table, company_members, and user_preferences to support multiple companies per user. All data scoping changes from user_id to company_id across the entire codebase. Key changes: - Database migration: new tables, company_id on 40+ tables, backfill, RLS rewrite from user_id to company-member-based, updated RPCs - Types: Company, CompanyMember, CompanyRole, UserPreferences types; company_id added to all entity interfaces; companyId on all events - Engine: all 7 core functions take companyId; storno, period, year-end services updated; 16 report generators updated - Middleware: company context resolution (cookie → prefs → first company) - API routes: ~120 routes updated with requireCompanyId() - Frontend: CompanyProvider context, layout/dashboard/onboarding updated - Extensions: context factory, 9 extensions, all lib files updated - Tests: 1880 tests passing, all helpers updated with company_id defaults Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add database migrations for multi-tenant company and team system (GNU-19) Adds company_invitations, company creation RPC, team_members, account deletion RPC, and teams table refactor migrations. Updates base multi-tenant migration with cascading FKs and onboarding_step column. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add team types and update core infrastructure for multi-tenancy (GNU-19) Adds TeamRole, MemberSource, and Team types. Refactors Supabase service client to be stateless, updates middleware for team-aware routing, extends CompanyContext with team/role fields, and updates extension service types to accept companyId. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor: thread company_id through business logic functions (GNU-19) Replaces user_id scoping with company_id across all lib modules: bookkeeping, documents, transactions, invoices, reconciliation, tax, deadlines, and import. Updates corresponding tests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor: thread company_id through API routes and extensions (GNU-19) Updates all existing API routes to extract and pass companyId. Updates enable-banking and arcim-migration extensions for company-scoped transaction ingestion and sync. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add company and team management API routes (GNU-19) Adds CRUD endpoints for company members, company invitations, team members, and team invitations. Includes invite token utilities, email templates, and company switch server action. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add team/company UI components, pages, and dashboard updates (GNU-19) Adds CompanySwitcher, ConsultantEmptyState, Step0RoleChoice, company members and team management panels. Updates dashboard layout for team-aware routing, onboarding for multi-step role choice, and auth callback for team invite acceptance. Ignores supabase/.branches/. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add null guards for company in import page (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: move appUrl declaration to outer scope in invite route (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add optional chaining for company.name in members section (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add optional chaining for second company.name in members section (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add null guards for company in extension components (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: pass companyId to executeSIEImport in arcim-migration extension (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: update tests to use companyId instead of userId and improve type handling --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
359 lines
12 KiB
TypeScript
359 lines
12 KiB
TypeScript
/**
|
|
* Tests for receipt matcher tools and MCP Apps protocol additions
|
|
*/
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import {
|
|
createQueuedMockSupabase,
|
|
makeTransaction,
|
|
} from '@/tests/helpers'
|
|
|
|
// ── Mocks ──
|
|
vi.mock('@/lib/supabase/server', () => ({
|
|
createClient: vi.fn(),
|
|
createServiceClient: vi.fn().mockResolvedValue({
|
|
storage: {
|
|
getBucket: vi.fn().mockResolvedValue({ data: { name: 'documents' } }),
|
|
},
|
|
}),
|
|
}))
|
|
|
|
vi.mock('@/lib/auth/api-keys', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('@/lib/auth/api-keys')>()
|
|
return {
|
|
...actual,
|
|
extractBearerToken: vi.fn().mockReturnValue('test-token'),
|
|
validateApiKey: vi.fn().mockResolvedValue({
|
|
userId: 'user-1',
|
|
scopes: ['transactions:read', 'transactions:write', 'customers:read', 'customers:write', 'invoices:read', 'invoices:write', 'suppliers:read', 'reports:read'],
|
|
}),
|
|
createServiceClientNoCookies: vi.fn(),
|
|
}
|
|
})
|
|
|
|
vi.mock('@/lib/bookkeeping/category-mapping', () => ({
|
|
buildMappingResultFromCategory: vi.fn().mockReturnValue({
|
|
debit_account: '6110',
|
|
credit_account: '1930',
|
|
vat_lines: [{ account_number: '2641', amount: 74.75 }],
|
|
}),
|
|
}))
|
|
|
|
vi.mock('@/lib/bookkeeping/transaction-entries', () => ({
|
|
createTransactionJournalEntry: vi.fn().mockResolvedValue({ id: 'je-123' }),
|
|
}))
|
|
|
|
vi.mock('@/lib/events/bus', () => ({
|
|
eventBus: { emit: vi.fn().mockResolvedValue(undefined), clear: vi.fn() },
|
|
}))
|
|
|
|
vi.mock('@/lib/invoices/vat-rules', () => ({
|
|
getVatRules: vi.fn(),
|
|
getAvailableVatRates: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/currency/riksbanken', () => ({
|
|
fetchExchangeRate: vi.fn(),
|
|
convertToSEK: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/reports/income-statement', () => ({
|
|
generateIncomeStatement: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/reports/kpi', () => ({
|
|
calculateGrossMargin: vi.fn(),
|
|
calculateCashPosition: vi.fn(),
|
|
calculateExpenseRatio: vi.fn(),
|
|
calculateAvgPaymentDays: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/reports/trial-balance', () => ({
|
|
generateTrialBalance: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/reports/ar-ledger', () => ({
|
|
generateARLedger: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/reports/monthly-breakdown', () => ({
|
|
generateMonthlyBreakdown: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/reports/balance-sheet', () => ({
|
|
generateBalanceSheet: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/reports/general-ledger', () => ({
|
|
generateGeneralLedger: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/reports/supplier-ledger', () => ({
|
|
generateSupplierLedger: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/reconciliation/bank-reconciliation', () => ({
|
|
getReconciliationStatus: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/bookkeeping/invoice-entries', () => ({
|
|
createInvoicePaymentJournalEntry: vi.fn(),
|
|
createInvoiceCashEntry: vi.fn(),
|
|
createInvoiceJournalEntry: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/bookkeeping/engine', () => ({
|
|
reverseEntry: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/transactions/category-suggestions', () => ({
|
|
getSuggestedCategories: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/bookkeeping/counterparty-templates', () => ({
|
|
upsertCounterpartyTemplate: vi.fn(),
|
|
findCounterpartyTemplatesBatch: vi.fn(),
|
|
formatCounterpartyName: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@react-pdf/renderer', () => ({
|
|
renderToBuffer: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/invoices/pdf-template', () => ({
|
|
InvoicePDF: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/email/service', () => ({
|
|
getEmailService: vi.fn().mockReturnValue({ sendInvoice: vi.fn() }),
|
|
}))
|
|
|
|
vi.mock('@/lib/email/invoice-templates', () => ({
|
|
generateInvoiceEmailHtml: vi.fn(),
|
|
generateInvoiceEmailText: vi.fn(),
|
|
generateInvoiceEmailSubject: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/core/documents/document-service', () => ({
|
|
uploadDocument: vi.fn(),
|
|
}))
|
|
|
|
import { handleMcpRequest } from '../server'
|
|
import { createServiceClientNoCookies } from '@/lib/auth/api-keys'
|
|
import { eventBus } from '@/lib/events/bus'
|
|
|
|
// Helper: make a JSON-RPC request
|
|
function mcpRequest(method: string, params?: Record<string, unknown>, id: number | string = 1): Request {
|
|
return new Request('http://localhost:3000/api/extensions/ext/mcp-server/mcp', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', Authorization: 'Bearer test-token' },
|
|
body: JSON.stringify({ jsonrpc: '2.0', id, method, params }),
|
|
})
|
|
}
|
|
|
|
async function parseResult(response: Response) {
|
|
const json = await response.json()
|
|
return json.result
|
|
}
|
|
|
|
describe('MCP Receipt Matcher', () => {
|
|
let supabase: ReturnType<typeof createQueuedMockSupabase>['supabase']
|
|
let enqueueMany: ReturnType<typeof createQueuedMockSupabase>['enqueueMany']
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
eventBus.clear()
|
|
const mock = createQueuedMockSupabase()
|
|
supabase = mock.supabase
|
|
enqueueMany = mock.enqueueMany
|
|
vi.mocked(createServiceClientNoCookies).mockReturnValue(supabase as never)
|
|
})
|
|
|
|
// ── Protocol: initialize includes resources capability ──
|
|
|
|
describe('initialize', () => {
|
|
it('returns resources capability', async () => {
|
|
const res = await handleMcpRequest(mcpRequest('initialize', { protocolVersion: '2025-03-26' }))
|
|
const result = await parseResult(res)
|
|
|
|
expect(result.capabilities.tools).toEqual({ listChanged: false })
|
|
expect(result.capabilities.resources).toEqual({ listChanged: false })
|
|
})
|
|
})
|
|
|
|
// ── Protocol: tools/list includes _meta for receipt matcher ──
|
|
|
|
describe('tools/list', () => {
|
|
it('includes _meta.ui for gnubok_receipt_matcher', async () => {
|
|
const res = await handleMcpRequest(mcpRequest('tools/list'))
|
|
const result = await parseResult(res)
|
|
|
|
const receiptTool = result.tools.find((t: { name: string }) => t.name === 'gnubok_receipt_matcher')
|
|
expect(receiptTool).toBeDefined()
|
|
expect(receiptTool._meta).toEqual({
|
|
ui: { resourceUri: 'ui://receipt-matcher/app.html' },
|
|
})
|
|
})
|
|
|
|
it('does not include _meta for tools without it', async () => {
|
|
const res = await handleMcpRequest(mcpRequest('tools/list'))
|
|
const result = await parseResult(res)
|
|
|
|
const categorizeTool = result.tools.find(
|
|
(t: { name: string }) => t.name === 'gnubok_categorize_transaction'
|
|
)
|
|
expect(categorizeTool).toBeDefined()
|
|
expect(categorizeTool._meta).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
// ── Protocol: resources/list ──
|
|
|
|
describe('resources/list', () => {
|
|
it('returns the receipt-matcher resource', async () => {
|
|
const res = await handleMcpRequest(mcpRequest('resources/list'))
|
|
const result = await parseResult(res)
|
|
|
|
expect(result.resources).toHaveLength(1)
|
|
expect(result.resources[0]).toEqual({
|
|
uri: 'ui://receipt-matcher/app.html',
|
|
name: 'Receipt Matcher',
|
|
description: 'Interactive widget for matching receipts to uncategorized transactions',
|
|
mimeType: 'text/html;profile=mcp-app',
|
|
})
|
|
})
|
|
})
|
|
|
|
// ── Protocol: resources/read ──
|
|
|
|
describe('resources/read', () => {
|
|
it('returns HTML for the receipt matcher', async () => {
|
|
const res = await handleMcpRequest(
|
|
mcpRequest('resources/read', { uri: 'ui://receipt-matcher/app.html' })
|
|
)
|
|
const result = await parseResult(res)
|
|
|
|
expect(result.contents).toHaveLength(1)
|
|
expect(result.contents[0].uri).toBe('ui://receipt-matcher/app.html')
|
|
expect(result.contents[0].mimeType).toBe('text/html;profile=mcp-app')
|
|
expect(result.contents[0].text).toContain('<!DOCTYPE html>')
|
|
expect(result.contents[0].text).toContain('Kvittomatchning')
|
|
})
|
|
|
|
it('returns error for unknown resource URI', async () => {
|
|
const res = await handleMcpRequest(
|
|
mcpRequest('resources/read', { uri: 'ui://unknown/thing' })
|
|
)
|
|
const json = await res.json()
|
|
|
|
expect(json.error).toBeDefined()
|
|
expect(json.error.code).toBe(-32602)
|
|
expect(json.error.message).toContain('Resource not found')
|
|
})
|
|
})
|
|
|
|
// ── gnubok_receipt_matcher tool ──
|
|
|
|
describe('gnubok_receipt_matcher', () => {
|
|
it('returns uncategorized transactions with categories and vat_treatments', async () => {
|
|
const tx1 = makeTransaction({ id: 'tx-1', description: 'ICA', amount: -150 })
|
|
const tx2 = makeTransaction({ id: 'tx-2', description: 'Consulting', amount: 15000 })
|
|
enqueueMany([
|
|
{ data: [tx1, tx2], error: null },
|
|
])
|
|
|
|
const res = await handleMcpRequest(
|
|
mcpRequest('tools/call', { name: 'gnubok_receipt_matcher', arguments: {} })
|
|
)
|
|
const result = await parseResult(res)
|
|
|
|
// Should have structuredContent for MCP Apps
|
|
expect(result.structuredContent).toBeDefined()
|
|
expect(result.structuredContent.transactions).toHaveLength(2)
|
|
expect(result.structuredContent.categories).toContain('expense_office')
|
|
expect(result.structuredContent.vat_treatments).toContain('standard_25')
|
|
|
|
// Should also have regular content
|
|
expect(result.content).toHaveLength(1)
|
|
expect(result.content[0].type).toBe('text')
|
|
})
|
|
|
|
it('returns empty array when no uncategorized transactions', async () => {
|
|
enqueueMany([{ data: [], error: null }])
|
|
|
|
const res = await handleMcpRequest(
|
|
mcpRequest('tools/call', { name: 'gnubok_receipt_matcher', arguments: {} })
|
|
)
|
|
const result = await parseResult(res)
|
|
|
|
expect(result.structuredContent.transactions).toHaveLength(0)
|
|
})
|
|
})
|
|
|
|
// ── gnubok_categorize_transaction still works after refactor ──
|
|
|
|
describe('gnubok_categorize_transaction (staging)', () => {
|
|
it('always stages the operation directly', async () => {
|
|
const tx = makeTransaction({ id: 'tx-1', amount: -500 })
|
|
|
|
enqueueMany([
|
|
{ data: tx, error: null }, // fetch transaction (preview)
|
|
{ data: { entity_type: 'enskild_firma', fiscal_year_start_month: 1 }, error: null },
|
|
{ data: tx, error: null }, // fetch transaction for title
|
|
{ data: { id: 'op-1' }, error: null }, // insert into pending_operations
|
|
])
|
|
|
|
const res = await handleMcpRequest(
|
|
mcpRequest('tools/call', {
|
|
name: 'gnubok_categorize_transaction',
|
|
arguments: { transaction_id: 'tx-1', category: 'expense_office' },
|
|
})
|
|
)
|
|
const result = await parseResult(res)
|
|
const parsed = JSON.parse(result.content[0].text)
|
|
|
|
expect(parsed.staged).toBe(true)
|
|
expect(parsed.operation_id).toBe('op-1')
|
|
expect(parsed.message).toContain('staged')
|
|
expect(parsed.preview).toBeDefined()
|
|
expect(parsed.preview.debit_account).toBeDefined()
|
|
})
|
|
})
|
|
|
|
// ── tools/call structuredContent ──
|
|
|
|
describe('tools/call structuredContent', () => {
|
|
it('includes structuredContent for tools with _meta.ui', async () => {
|
|
enqueueMany([{ data: [], error: null }])
|
|
|
|
const res = await handleMcpRequest(
|
|
mcpRequest('tools/call', { name: 'gnubok_receipt_matcher', arguments: {} })
|
|
)
|
|
const result = await parseResult(res)
|
|
|
|
expect(result.structuredContent).toBeDefined()
|
|
expect(result.content).toBeDefined()
|
|
})
|
|
|
|
it('does not include structuredContent for regular tools', async () => {
|
|
const tx = makeTransaction({ id: 'tx-1', amount: -500 })
|
|
enqueueMany([
|
|
{ data: tx, error: null },
|
|
{ data: { entity_type: 'enskild_firma', fiscal_year_start_month: 1 }, error: null },
|
|
{ data: tx, error: null }, // fetch transaction for title
|
|
{ data: { id: 'op-1' }, error: null }, // insert into pending_operations
|
|
])
|
|
|
|
const res = await handleMcpRequest(
|
|
mcpRequest('tools/call', {
|
|
name: 'gnubok_categorize_transaction',
|
|
arguments: { transaction_id: 'tx-1', category: 'expense_office' },
|
|
})
|
|
)
|
|
const result = await parseResult(res)
|
|
|
|
expect(result.structuredContent).toBeUndefined()
|
|
expect(result.content).toBeDefined()
|
|
})
|
|
})
|
|
})
|