Files
accounted/app/api/invoices/recurring/__tests__/route.test.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

335 lines
12 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import {
createMockRequest,
parseJsonResponse,
createQueuedMockSupabase,
} from '@/tests/helpers'
import { eventBus } from '@/lib/events'
const { supabase: mockSupabase, enqueue, reset, findCall } = createQueuedMockSupabase()
vi.mock('@/lib/supabase/server', () => ({
createClient: () => Promise.resolve(mockSupabase),
}))
vi.mock('@/lib/init', () => ({
ensureInitialized: vi.fn(),
}))
vi.mock('@/lib/company/context', () => ({
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
}))
vi.mock('@/lib/auth/require-write', () => ({
requireWritePermission: vi.fn().mockResolvedValue({ ok: true }),
}))
import { GET, POST } from '../route'
const mockUser = { id: 'user-1', email: 'test@test.se' }
describe('GET /api/invoices/recurring', () => {
beforeEach(() => {
vi.clearAllMocks()
reset()
eventBus.clear()
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: mockUser } })
})
it('returns 401 when not authenticated', async () => {
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: null } })
const response = await GET(createMockRequest('/api/invoices/recurring'), { params: Promise.resolve({}) })
const { status } = await parseJsonResponse(response)
expect(status).toBe(401)
})
it('returns schedule list', async () => {
const schedules = [
{ id: 's-1', name: 'Acme retainer', day_of_month: 15, status: 'active' },
]
enqueue({ data: schedules, error: null })
const response = await GET(createMockRequest('/api/invoices/recurring'), { params: Promise.resolve({}) })
const { status, body } = await parseJsonResponse<{ data: unknown[] }>(response)
expect(status).toBe(200)
expect(body.data).toEqual(schedules)
})
})
describe('POST /api/invoices/recurring', () => {
beforeEach(() => {
vi.clearAllMocks()
reset()
eventBus.clear()
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: mockUser } })
})
it('returns 400 on validation error (missing items)', async () => {
const request = createMockRequest('/api/invoices/recurring', {
method: 'POST',
body: {
customer_id: '550e8400-e29b-41d4-a716-446655440000',
name: 'Test',
day_of_month: 15,
payment_terms_days: 30,
currency: 'SEK',
auto_send: false,
items: [],
},
})
const response = await POST(request, { params: Promise.resolve({}) })
const { status, body } = await parseJsonResponse<{ type: string }>(response)
expect(status).toBe(400)
expect(body.type).toBe('validation_error')
})
it('returns 404 when customer does not exist', async () => {
enqueue({ data: null, error: null }) // customer lookup → null
const request = createMockRequest('/api/invoices/recurring', {
method: 'POST',
body: {
customer_id: '550e8400-e29b-41d4-a716-446655440000',
name: 'Test',
day_of_month: 15,
payment_terms_days: 30,
currency: 'SEK',
auto_send: false,
items: [
{ description: 'Service', quantity: 1, unit: 'st', unit_price: 1000 },
],
},
})
const response = await POST(request, { params: Promise.resolve({}) })
const { status, body } = await parseJsonResponse<{ type: string }>(response)
expect(status).toBe(404)
expect(body.type).toBe('not_found')
})
it('rejects auto_send when the customer has no email', async () => {
// customer lookup: exists but without email
enqueue({ data: { id: '550e8400-e29b-41d4-a716-446655440000', email: null }, error: null })
const request = createMockRequest('/api/invoices/recurring', {
method: 'POST',
body: {
customer_id: '550e8400-e29b-41d4-a716-446655440000',
name: 'Test',
day_of_month: 15,
payment_terms_days: 30,
currency: 'SEK',
auto_send: true,
items: [
{ description: 'Service', quantity: 1, unit: 'st', unit_price: 1000 },
],
},
})
const response = await POST(request, { params: Promise.resolve({}) })
const { status, body } = await parseJsonResponse<{ type: string }>(response)
expect(status).toBe(400)
expect(body.type).toBe('validation_error')
})
it('rejects a malformed dimensions bag with 400', async () => {
const request = createMockRequest('/api/invoices/recurring', {
method: 'POST',
body: {
customer_id: '550e8400-e29b-41d4-a716-446655440000',
name: 'Test',
day_of_month: 15,
payment_terms_days: 30,
currency: 'SEK',
auto_send: false,
// Key must be a SIE dim number: 'projekt' is not.
default_dimensions: { projekt: 'P001' },
items: [
{ description: 'Service', quantity: 1, unit: 'st', unit_price: 1000 },
],
},
})
const response = await POST(request, { params: Promise.resolve({}) })
const { status, body } = await parseJsonResponse<{ type: string }>(response)
expect(status).toBe(400)
expect(body.type).toBe('validation_error')
})
it('persists schedule and item dimension bags', async () => {
// The queued mock's chain proxy discards call args by design, so capture
// .insert/.update payloads per table with a thin wrapper.
const inserted: Record<string, unknown[]> = {}
const originalFrom = mockSupabase.from.getMockImplementation()!
mockSupabase.from.mockImplementation((table: string) => {
const chain = originalFrom(table) as object
return new Proxy(chain, {
get(target, prop, receiver) {
if (prop === 'insert') {
return (rows: unknown) => {
;(inserted[table] ??= []).push(rows)
return (Reflect.get(target, prop, receiver) as (r: unknown) => unknown)(rows)
}
}
return Reflect.get(target, prop, receiver)
},
})
})
const createdSchedule = { id: 's-1', name: 'Acme retainer' }
enqueue({ data: { id: '550e8400-e29b-41d4-a716-446655440000' }, error: null })
enqueue({ data: createdSchedule, error: null })
enqueue({ data: null, error: null })
enqueue({ data: { ...createdSchedule, items: [] }, error: null })
const request = createMockRequest('/api/invoices/recurring', {
method: 'POST',
body: {
customer_id: '550e8400-e29b-41d4-a716-446655440000',
name: 'Acme retainer',
day_of_month: 15,
payment_terms_days: 30,
currency: 'SEK',
auto_send: false,
default_dimensions: { '1': 'KS1', '6': 'P001' },
items: [
{
description: 'Konsultarvode',
quantity: 10,
unit: 'tim',
unit_price: 1200,
dimensions: { '6': 'P002' },
},
{ description: 'Resor', quantity: 1, unit: 'st', unit_price: 500 },
],
},
})
const response = await POST(request, { params: Promise.resolve({}) })
const { status } = await parseJsonResponse(response)
expect(status).toBe(201)
expect(inserted['recurring_invoice_schedules'][0]).toMatchObject({
default_dimensions: { '1': 'KS1', '6': 'P001' },
})
const itemRows = inserted['recurring_invoice_schedule_items'][0] as Array<
Record<string, unknown>
>
expect(itemRows[0].dimensions).toEqual({ '6': 'P002' })
expect(itemRows[1].dimensions).toEqual({})
})
it('rejects a start_date that is not on the day_of_month grid', async () => {
enqueue({ data: { id: '550e8400-e29b-41d4-a716-446655440000' }, error: null })
const request = createMockRequest('/api/invoices/recurring', {
method: 'POST',
body: {
customer_id: '550e8400-e29b-41d4-a716-446655440000',
name: 'Årsavgift',
day_of_month: 15,
interval_months: 12,
start_date: '2999-02-14',
items: [{ description: 'Licens', quantity: 1, unit: 'st', unit_price: 12000 }],
},
})
const response = await POST(request, { params: Promise.resolve({}) })
const { status, body } = await parseJsonResponse<{ type: string; error: string }>(response)
expect(status).toBe(400)
expect(body.type).toBe('validation_error')
expect(body.error).toMatch(/day_of_month/)
expect(findCall('recurring_invoice_schedules', 'insert')).toBeUndefined()
})
it('rejects a start_date in the past', async () => {
enqueue({ data: { id: '550e8400-e29b-41d4-a716-446655440000' }, error: null })
const request = createMockRequest('/api/invoices/recurring', {
method: 'POST',
body: {
customer_id: '550e8400-e29b-41d4-a716-446655440000',
name: 'Årsavgift',
day_of_month: 15,
interval_months: 12,
start_date: '2020-02-15',
items: [{ description: 'Licens', quantity: 1, unit: 'st', unit_price: 12000 }],
},
})
const response = await POST(request, { params: Promise.resolve({}) })
const { status, body } = await parseJsonResponse<{ type: string; error: string }>(response)
expect(status).toBe(400)
expect(body.error).toMatch(/past/)
expect(findCall('recurring_invoice_schedules', 'insert')).toBeUndefined()
})
it('uses an explicit start_date as the first run so a yearly schedule keeps its month', async () => {
const createdSchedule = {
id: 's-2',
company_id: 'company-1',
customer_id: '550e8400-e29b-41d4-a716-446655440000',
name: 'Årsavgift',
day_of_month: 15,
interval_months: 12,
next_run_date: '2999-02-15',
status: 'active',
}
enqueue({ data: { id: '550e8400-e29b-41d4-a716-446655440000' }, error: null })
enqueue({ data: createdSchedule, error: null })
enqueue({ data: null, error: null })
enqueue({ data: { ...createdSchedule, items: [] }, error: null })
const request = createMockRequest('/api/invoices/recurring', {
method: 'POST',
body: {
customer_id: '550e8400-e29b-41d4-a716-446655440000',
name: 'Årsavgift',
day_of_month: 15,
interval_months: 12,
start_date: '2999-02-15',
items: [{ description: 'Licens', quantity: 1, unit: 'st', unit_price: 12000 }],
},
})
const response = await POST(request, { params: Promise.resolve({}) })
const { status } = await parseJsonResponse<{ data: { id: string } }>(response)
expect(status).toBe(201)
const insertArgs = findCall('recurring_invoice_schedules', 'insert')
expect(insertArgs?.[0]).toMatchObject({ next_run_date: '2999-02-15', interval_months: 12 })
})
it('creates a schedule on the happy path', async () => {
const createdSchedule = {
id: 's-1',
company_id: 'company-1',
user_id: 'user-1',
customer_id: '550e8400-e29b-41d4-a716-446655440000',
name: 'Acme retainer',
day_of_month: 15,
next_run_date: '2026-05-15',
status: 'active',
}
// 1. customer lookup ok
enqueue({ data: { id: '550e8400-e29b-41d4-a716-446655440000' }, error: null })
// 2. schedule insert returns the row
enqueue({ data: createdSchedule, error: null })
// 3. items insert ok
enqueue({ data: null, error: null })
// 4. final re-fetch
enqueue({ data: { ...createdSchedule, items: [] }, error: null })
const request = createMockRequest('/api/invoices/recurring', {
method: 'POST',
body: {
customer_id: '550e8400-e29b-41d4-a716-446655440000',
name: 'Acme retainer',
day_of_month: 15,
payment_terms_days: 30,
currency: 'SEK',
auto_send: false,
items: [
{ description: 'Konsultarvode', quantity: 10, unit: 'tim', unit_price: 1200 },
],
},
})
const response = await POST(request, { params: Promise.resolve({}) })
const { status, body } = await parseJsonResponse<{ data: { id: string } }>(response)
expect(status).toBe(201)
expect(body.data.id).toBe('s-1')
})
})