5d66dd6bfc
* fix: prevent Chrome auto-translate from crashing React during onboarding
Chrome auto-translate modifies DOM text nodes when it detects a Swedish
page (lang="sv") in a browser set to English. React does not expect
external DOM mutations and throws, crashing the entire component tree
into global-error.tsx on every step transition.
Add translate="no" and <meta name="google" content="notranslate"> to
suppress browser translation. Also fix timezone-unsafe date parsing in
fiscal period validation (new Date("YYYY-MM-DD") + getDate() returns
local-timezone values, shifting dates by -1 day in Western timezones).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: add notranslate meta tag to global-error.tsx for consistency
Per review feedback — global-error.tsx renders its own <html> document,
so it needs the same <meta name="google" content="notranslate"> tag as
layout.tsx to fully suppress Chrome translation on error pages.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add MCP server extension with OAuth, API keys, and KPI dashboard
Let users do bookkeeping through Claude Desktop, Claude Code, or any
MCP-compatible client. "Show my uncategorized transactions." "Book that
as office supplies." "Invoice Acme for 15,000 kr."
MCP server (extension):
- 10 tools: transactions, categorization, customers, invoices,
trial balance, VAT report, KPI report, income statement
- JSON-RPC 2.0 protocol (no SDK dependency, works in serverless)
- Tool annotations, pagination, input validation per MCP best practices
- Same engine as web UI (VAT rules, exchange rates, event emission)
API key infrastructure (core):
- api_keys table with RLS, rate limiting (100 RPM), scopes column
- Atomic rate limit via DB RPC (validate_and_increment_api_key)
- Key management API routes + settings UI panel
OAuth 2.1 for Claude Desktop connectors:
- .well-known/oauth-protected-resource + oauth-authorization-server
- Authorization endpoint with consent page
- Token endpoint with PKCE verification
- Stateless encrypted auth codes (AES-256-GCM, no DB storage)
- Dynamic client registration
KPI dashboard:
- /nyckeltal page with hero cards, operational grid, trend chart
- GET /api/reports/kpi endpoint
- Gross margin, cash position, expense ratio, avg payment days,
VAT liability, revenue/expense trend
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: address OAuth security vulnerabilities from code review
Critical fixes:
- Auth code replay: Track used codes in oauth_used_codes table with
unique constraint. Codes are single-use per OAuth 2.1 §4.1.2.
- Open redirect: Validate redirect_uri against hardcoded allowlist
of known Claude callback URLs + localhost for dev.
P1 fixes:
- Move API key creation from /authorize to /token endpoint. Keys are
only created after PKCE verification, preventing orphaned keys on
abandoned OAuth flows.
- Add ensureInitialized() to MCP server so event handlers load and
transaction.categorized events reach extensions.
P2 fixes:
- Remove 'plain' from PKCE methods — only S256 is advertised and
accepted.
- Fix extension count in sectors test (10 → 11 for mcp-server).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: remove duplicate ensureInitialized() that caused circular import
The extension router (ext/[...path]/route.ts) already calls
ensureInitialized() before dispatching to handlers. The duplicate
call in server.ts created a circular import that Turbopack couldn't
resolve, breaking the Vercel build.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
178 lines
5.4 KiB
TypeScript
178 lines
5.4 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import {
|
|
calculateGrossMargin,
|
|
calculateCashPosition,
|
|
calculateRevenueGrowth,
|
|
calculateExpenseRatio,
|
|
calculateAvgPaymentDays,
|
|
} from '../kpi'
|
|
import type { IncomeStatementReport, TrialBalanceRow } from '@/types'
|
|
|
|
function makeIncomeStatement(
|
|
overrides: Partial<IncomeStatementReport> = {}
|
|
): IncomeStatementReport {
|
|
return {
|
|
revenue_sections: [],
|
|
total_revenue: 100000,
|
|
expense_sections: [],
|
|
total_expenses: 60000,
|
|
financial_sections: [],
|
|
total_financial: 0,
|
|
net_result: 40000,
|
|
period: { start: '2025-01-01', end: '2025-12-31' },
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
function makeTrialBalanceRow(
|
|
overrides: Partial<TrialBalanceRow> = {}
|
|
): TrialBalanceRow {
|
|
return {
|
|
account_number: '1930',
|
|
account_name: 'Företagskonto',
|
|
account_class: 1,
|
|
opening_debit: 0,
|
|
opening_credit: 0,
|
|
period_debit: 0,
|
|
period_credit: 0,
|
|
closing_debit: 0,
|
|
closing_credit: 0,
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
describe('calculateGrossMargin', () => {
|
|
it('returns margin when revenue and COGS exist', () => {
|
|
const stmt = makeIncomeStatement({
|
|
total_revenue: 200000,
|
|
expense_sections: [
|
|
{
|
|
title: 'Varor och material',
|
|
rows: [{ account_number: '4010', account_name: 'Inköp', amount: 80000 }],
|
|
subtotal: 80000,
|
|
},
|
|
{
|
|
title: 'Lokalkostnader',
|
|
rows: [{ account_number: '5010', account_name: 'Hyra', amount: 20000 }],
|
|
subtotal: 20000,
|
|
},
|
|
],
|
|
})
|
|
// (200000 - 80000) / 200000 * 100 = 60%
|
|
expect(calculateGrossMargin(stmt)).toBe(60)
|
|
})
|
|
|
|
it('returns null when total_revenue is 0', () => {
|
|
const stmt = makeIncomeStatement({ total_revenue: 0 })
|
|
expect(calculateGrossMargin(stmt)).toBeNull()
|
|
})
|
|
|
|
it('returns 100% when no class 4 expenses', () => {
|
|
const stmt = makeIncomeStatement({
|
|
total_revenue: 50000,
|
|
expense_sections: [
|
|
{
|
|
title: 'Lokalkostnader',
|
|
rows: [{ account_number: '5010', account_name: 'Hyra', amount: 10000 }],
|
|
subtotal: 10000,
|
|
},
|
|
],
|
|
})
|
|
expect(calculateGrossMargin(stmt)).toBe(100)
|
|
})
|
|
})
|
|
|
|
describe('calculateCashPosition', () => {
|
|
it('sums closing balances for 19xx accounts', () => {
|
|
const rows = [
|
|
makeTrialBalanceRow({ account_number: '1930', closing_debit: 50000, closing_credit: 0 }),
|
|
makeTrialBalanceRow({ account_number: '1931', closing_debit: 10000, closing_credit: 0 }),
|
|
makeTrialBalanceRow({ account_number: '1510', closing_debit: 25000, closing_credit: 0 }),
|
|
]
|
|
// Only 1930 + 1931 = 60000
|
|
expect(calculateCashPosition(rows)).toBe(60000)
|
|
})
|
|
|
|
it('returns 0 for empty rows', () => {
|
|
expect(calculateCashPosition([])).toBe(0)
|
|
})
|
|
|
|
it('handles credit balances on 19xx accounts', () => {
|
|
const rows = [
|
|
makeTrialBalanceRow({ account_number: '1930', closing_debit: 0, closing_credit: 5000 }),
|
|
]
|
|
expect(calculateCashPosition(rows)).toBe(-5000)
|
|
})
|
|
})
|
|
|
|
describe('calculateRevenueGrowth', () => {
|
|
it('returns positive growth', () => {
|
|
// (120000 - 100000) / 100000 * 100 = 20%
|
|
expect(calculateRevenueGrowth(120000, 100000)).toBe(20)
|
|
})
|
|
|
|
it('returns negative growth (decline)', () => {
|
|
// (80000 - 100000) / 100000 * 100 = -20%
|
|
expect(calculateRevenueGrowth(80000, 100000)).toBe(-20)
|
|
})
|
|
|
|
it('returns null when previous revenue is null', () => {
|
|
expect(calculateRevenueGrowth(100000, null)).toBeNull()
|
|
})
|
|
|
|
it('returns null when previous revenue is 0', () => {
|
|
expect(calculateRevenueGrowth(100000, 0)).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('calculateExpenseRatio', () => {
|
|
it('returns ratio for normal data', () => {
|
|
const stmt = makeIncomeStatement({ total_revenue: 200000, total_expenses: 120000 })
|
|
// 120000 / 200000 * 100 = 60%
|
|
expect(calculateExpenseRatio(stmt)).toBe(60)
|
|
})
|
|
|
|
it('returns null when total_revenue is 0', () => {
|
|
const stmt = makeIncomeStatement({ total_revenue: 0, total_expenses: 5000 })
|
|
expect(calculateExpenseRatio(stmt)).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('calculateAvgPaymentDays', () => {
|
|
it('returns average for >= 5 invoices', () => {
|
|
const invoices = [
|
|
{ invoice_date: '2025-01-01', paid_at: '2025-01-11' }, // 10 days
|
|
{ invoice_date: '2025-02-01', paid_at: '2025-02-21' }, // 20 days
|
|
{ invoice_date: '2025-03-01', paid_at: '2025-03-16' }, // 15 days
|
|
{ invoice_date: '2025-04-01', paid_at: '2025-04-26' }, // 25 days
|
|
{ invoice_date: '2025-05-01', paid_at: '2025-05-31' }, // 30 days
|
|
]
|
|
// avg = (10+20+15+25+30) / 5 = 20
|
|
expect(calculateAvgPaymentDays(invoices)).toBe(20)
|
|
})
|
|
|
|
it('returns null for fewer than 5 invoices', () => {
|
|
const invoices = [
|
|
{ invoice_date: '2025-01-01', paid_at: '2025-01-11' },
|
|
{ invoice_date: '2025-02-01', paid_at: '2025-02-21' },
|
|
]
|
|
expect(calculateAvgPaymentDays(invoices)).toBeNull()
|
|
})
|
|
|
|
it('returns null for empty array', () => {
|
|
expect(calculateAvgPaymentDays([])).toBeNull()
|
|
})
|
|
|
|
it('clamps negative days to 0', () => {
|
|
const invoices = [
|
|
{ invoice_date: '2025-01-10', paid_at: '2025-01-05' }, // would be -5, clamped to 0
|
|
{ invoice_date: '2025-02-01', paid_at: '2025-02-11' }, // 10
|
|
{ invoice_date: '2025-03-01', paid_at: '2025-03-11' }, // 10
|
|
{ invoice_date: '2025-04-01', paid_at: '2025-04-11' }, // 10
|
|
{ invoice_date: '2025-05-01', paid_at: '2025-05-11' }, // 10
|
|
]
|
|
// avg = (0+10+10+10+10) / 5 = 8
|
|
expect(calculateAvgPaymentDays(invoices)).toBe(8)
|
|
})
|
|
})
|