- Remove all sector-specific extensions (construction, ecommerce, export, hotel, restaurant, tech) — only general-purpose extensions remain - Move NE-bilaga and SRU export from extensions to core reports (lib/reports/) - Move moms-box-mapping from extensions/export/shared to lib/vat/ - Replace per-extension API routes with catch-all dispatcher (app/api/extensions/ext/[...path]/route.ts) - Add manifest.json for each extension with metadata, env vars, and deps - Add api-routes.ts pattern for extension-defined API endpoints - Add code generation scripts (generate-extension-registry, create-extension) - Add extensions.config.json for opt-in extension loading - Add extensions.schema.json for config validation - Add email service interface with noop default (lib/email/service.ts) - Add CI workflow (core-build.yml) to verify core builds with zero extensions - Add migration 045: expand account_type CHECK for untaxed_reserves - Update CLAUDE.md with comprehensive extension system documentation - Update all report engines and bookkeeping services for new imports - Clean up extensions.schema.json to only list existing extensions Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
126 lines
3.8 KiB
TypeScript
126 lines
3.8 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { eventBus } from '@/lib/events/bus'
|
|
import {
|
|
createQueuedMockSupabase,
|
|
makeSupplierInvoice,
|
|
} from '@/tests/helpers'
|
|
|
|
vi.mock('@/lib/supabase/server', () => ({
|
|
createClient: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/bookkeeping/supplier-invoice-entries', () => ({
|
|
createSupplierInvoiceRegistrationEntry: vi.fn(),
|
|
}))
|
|
|
|
import { createClient } from '@/lib/supabase/server'
|
|
import { createSupplierInvoiceRegistrationEntry } from '@/lib/bookkeeping/supplier-invoice-entries'
|
|
import { registerSupplierInvoiceHandler } from '../supplier-invoice-handler'
|
|
|
|
const mockCreateClient = vi.mocked(createClient)
|
|
const mockCreateEntry = vi.mocked(createSupplierInvoiceRegistrationEntry)
|
|
|
|
describe('Supplier Invoice Core Handler', () => {
|
|
let unsubscribe: () => void
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
eventBus.clear()
|
|
unsubscribe = registerSupplierInvoiceHandler()
|
|
})
|
|
|
|
afterEach(() => {
|
|
unsubscribe()
|
|
})
|
|
|
|
it('creates registration journal entry for accrual method', async () => {
|
|
const { supabase, enqueueMany } = createQueuedMockSupabase()
|
|
enqueueMany([
|
|
// 1. company_settings
|
|
{ data: { accounting_method: 'accrual' }, error: null },
|
|
// 2. supplier_invoice_items
|
|
{ data: [{ id: 'item-1', account_number: '6200', line_total: 1000, sort_order: 0 }], error: null },
|
|
// 3. supplier (type)
|
|
{ data: { supplier_type: 'swedish_business' }, error: null },
|
|
// 4. Update invoice with journal entry id
|
|
{ data: null, error: null },
|
|
])
|
|
mockCreateClient.mockResolvedValue(supabase as never)
|
|
mockCreateEntry.mockResolvedValue({ id: 'je-1' } as never)
|
|
|
|
const invoice = makeSupplierInvoice({ id: 'si-1', supplier_id: 'sup-1' })
|
|
|
|
await eventBus.emit({
|
|
type: 'supplier_invoice.confirmed',
|
|
payload: {
|
|
inboxItem: { id: 'inbox-1' } as never,
|
|
supplierInvoice: invoice,
|
|
userId: 'user-1',
|
|
},
|
|
})
|
|
|
|
expect(mockCreateEntry).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
'user-1',
|
|
invoice,
|
|
expect.any(Array),
|
|
'swedish_business'
|
|
)
|
|
})
|
|
|
|
it('skips journal entry for cash method', async () => {
|
|
const { supabase, enqueueMany } = createQueuedMockSupabase()
|
|
enqueueMany([
|
|
// company_settings with cash method
|
|
{ data: { accounting_method: 'cash' }, error: null },
|
|
])
|
|
mockCreateClient.mockResolvedValue(supabase as never)
|
|
|
|
const invoice = makeSupplierInvoice({ id: 'si-2' })
|
|
|
|
await eventBus.emit({
|
|
type: 'supplier_invoice.confirmed',
|
|
payload: {
|
|
inboxItem: { id: 'inbox-2' } as never,
|
|
supplierInvoice: invoice,
|
|
userId: 'user-1',
|
|
},
|
|
})
|
|
|
|
expect(mockCreateEntry).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('handles journal entry creation failure gracefully', async () => {
|
|
const { supabase, enqueueMany } = createQueuedMockSupabase()
|
|
enqueueMany([
|
|
{ data: { accounting_method: 'accrual' }, error: null },
|
|
{ data: [{ id: 'item-1', account_number: '6200', line_total: 500, sort_order: 0 }], error: null },
|
|
{ data: { supplier_type: 'swedish_business' }, error: null },
|
|
])
|
|
mockCreateClient.mockResolvedValue(supabase as never)
|
|
mockCreateEntry.mockRejectedValue(new Error('No fiscal period'))
|
|
|
|
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
|
|
const invoice = makeSupplierInvoice({ id: 'si-3' })
|
|
|
|
// Should not throw
|
|
await eventBus.emit({
|
|
type: 'supplier_invoice.confirmed',
|
|
payload: {
|
|
inboxItem: { id: 'inbox-3' } as never,
|
|
supplierInvoice: invoice,
|
|
userId: 'user-1',
|
|
},
|
|
})
|
|
|
|
expect(consoleSpy).toHaveBeenCalledWith(
|
|
'[supplier-invoice-handler]',
|
|
'Failed to create registration journal entry:',
|
|
expect.any(Error)
|
|
)
|
|
|
|
consoleSpy.mockRestore()
|
|
})
|
|
})
|