docs(production): standardiserad produktionsberedskap och gap-analys
Lägger docs/production-readiness.md och siax/production-gaps.json enligt SIAX Master Definition of Done. Underlag: git-, kod- och live-evidens 2026-11-11. Nivån är evidensbaserad, inte ett leveransgodkännande.
This commit is contained in:
@@ -1,141 +0,0 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
|
||||
const supabaseState = vi.hoisted(() => ({
|
||||
result: { data: null as unknown, error: null as unknown },
|
||||
calls: [] as Array<{ method: string; args: unknown[] }>,
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/supabase/client', () => ({
|
||||
createClient: () => {
|
||||
const chain: Record<string, unknown> = {}
|
||||
const proxy: unknown = new Proxy(chain, {
|
||||
get(_t, prop) {
|
||||
if (prop === 'then') {
|
||||
return (resolve: (v: unknown) => void) => resolve(supabaseState.result)
|
||||
}
|
||||
return (...args: unknown[]) => {
|
||||
supabaseState.calls.push({ method: String(prop), args })
|
||||
return proxy
|
||||
}
|
||||
},
|
||||
})
|
||||
return { from: (table: string) => (supabaseState.calls.push({ method: 'from', args: [table] }), proxy) }
|
||||
},
|
||||
}))
|
||||
|
||||
import {
|
||||
ReferenceFetchError,
|
||||
fetchAccounts,
|
||||
fetchArticles,
|
||||
fetchBookingTemplates,
|
||||
fetchCashAccounts,
|
||||
fetchCustomers,
|
||||
fetchDimensions,
|
||||
fetchFiscalPeriods,
|
||||
fetchSuppliers,
|
||||
} from '../fetchers'
|
||||
|
||||
function jsonResponse(body: unknown, status = 200) {
|
||||
return new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } })
|
||||
}
|
||||
|
||||
describe('browser Supabase fetchers', () => {
|
||||
beforeEach(() => {
|
||||
supabaseState.calls = []
|
||||
supabaseState.result = { data: [{ id: 'row-1' }], error: null }
|
||||
})
|
||||
|
||||
it('fetchFiscalPeriods mirrors period.list: company filter, newest period first', async () => {
|
||||
const rows = await fetchFiscalPeriods('c1')
|
||||
expect(rows).toEqual([{ id: 'row-1' }])
|
||||
expect(supabaseState.calls).toEqual([
|
||||
{ method: 'from', args: ['fiscal_periods'] },
|
||||
{ method: 'select', args: ['*'] },
|
||||
{ method: 'eq', args: ['company_id', 'c1'] },
|
||||
{ method: 'order', args: ['period_start', { ascending: false }] },
|
||||
])
|
||||
})
|
||||
|
||||
it('fetchCashAccounts mirrors listForCompany: primary first, then ledger account', async () => {
|
||||
await fetchCashAccounts('c1')
|
||||
expect(supabaseState.calls).toEqual([
|
||||
{ method: 'from', args: ['cash_accounts'] },
|
||||
{ method: 'select', args: ['*'] },
|
||||
{ method: 'eq', args: ['company_id', 'c1'] },
|
||||
{ method: 'order', args: ['is_primary', { ascending: false }] },
|
||||
{ method: 'order', args: ['ledger_account', { ascending: true }] },
|
||||
])
|
||||
})
|
||||
|
||||
it('throws the Supabase error so SWR surfaces it instead of caching an empty list', async () => {
|
||||
supabaseState.result = { data: null, error: { message: 'boom' } }
|
||||
await expect(fetchFiscalPeriods('c1')).rejects.toEqual({ message: 'boom' })
|
||||
})
|
||||
|
||||
it('resolves to an empty list when the query returns no rows', async () => {
|
||||
supabaseState.result = { data: null, error: null }
|
||||
expect(await fetchCashAccounts('c1')).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('API fetchers', () => {
|
||||
const fetchMock = vi.fn()
|
||||
|
||||
beforeEach(() => {
|
||||
fetchMock.mockReset()
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
it('unwraps { data } and hits the documented URLs', async () => {
|
||||
fetchMock.mockImplementation(async () => jsonResponse({ data: [{ id: 'x' }] }))
|
||||
|
||||
expect(await fetchAccounts()).toEqual([{ id: 'x' }])
|
||||
expect(await fetchAccounts(false)).toEqual([{ id: 'x' }])
|
||||
expect(await fetchBookingTemplates()).toEqual([{ id: 'x' }])
|
||||
expect(await fetchCustomers()).toEqual([{ id: 'x' }])
|
||||
expect(await fetchSuppliers()).toEqual([{ id: 'x' }])
|
||||
expect(await fetchArticles()).toEqual([{ id: 'x' }])
|
||||
expect(await fetchArticles(true)).toEqual([{ id: 'x' }])
|
||||
|
||||
expect(fetchMock.mock.calls.map((c) => c[0])).toEqual([
|
||||
'/api/bookkeeping/accounts',
|
||||
'/api/bookkeeping/accounts?active=false',
|
||||
'/api/settings/booking-templates',
|
||||
'/api/customers',
|
||||
'/api/suppliers',
|
||||
'/api/articles',
|
||||
'/api/articles?include_inactive=1',
|
||||
])
|
||||
})
|
||||
|
||||
it('reads the dimensions route from its `dimensions` field', async () => {
|
||||
fetchMock.mockImplementation(async () => jsonResponse({ dimensions: [{ id: 'd' }] }))
|
||||
expect(await fetchDimensions()).toEqual([{ id: 'd' }])
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/dimensions')
|
||||
})
|
||||
|
||||
it('returns an empty list when the payload field is missing', async () => {
|
||||
fetchMock.mockImplementation(async () => jsonResponse({}))
|
||||
expect(await fetchCustomers()).toEqual([])
|
||||
})
|
||||
|
||||
it('throws a ReferenceFetchError carrying status and body on a non-2xx response', async () => {
|
||||
fetchMock.mockImplementation(async () => jsonResponse({ error: 'nope' }, 403))
|
||||
const err = await fetchSuppliers().catch((e) => e)
|
||||
expect(err).toBeInstanceOf(ReferenceFetchError)
|
||||
expect(err.status).toBe(403)
|
||||
expect(err.body).toEqual({ error: 'nope' })
|
||||
})
|
||||
|
||||
it('still throws with a null body when the error response is not JSON', async () => {
|
||||
fetchMock.mockImplementation(async () => new Response('gateway', { status: 502 }))
|
||||
const err = await fetchArticles().catch((e) => e)
|
||||
expect(err).toBeInstanceOf(ReferenceFetchError)
|
||||
expect(err.status).toBe(502)
|
||||
expect(err.body).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -1,89 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { prepareFiscalPeriods, resolveInitialFiscalScope } from '../fiscal-scope'
|
||||
import { ALL_YEARS_VALUE } from '@/components/common/fiscal-year-storage'
|
||||
import type { FiscalPeriod } from '@/types'
|
||||
|
||||
const period = (id: string, start: string, end: string) =>
|
||||
({ id, name: id, period_start: start, period_end: end }) as unknown as FiscalPeriod
|
||||
|
||||
const p2024 = period('p2024', '2024-01-01', '2024-12-31')
|
||||
const p2025 = period('p2025', '2025-01-01', '2025-12-31')
|
||||
const p2026 = period('p2026', '2026-01-01', '2026-12-31')
|
||||
const p2027 = period('p2027', '2027-01-01', '2027-12-31')
|
||||
const TODAY = '2026-08-26'
|
||||
|
||||
describe('prepareFiscalPeriods', () => {
|
||||
it('sorts newest first and can hide periods that have not started', () => {
|
||||
const all = prepareFiscalPeriods([p2024, p2027, p2026, p2025], false, TODAY)
|
||||
expect(all.map((p) => p.id)).toEqual(['p2027', 'p2026', 'p2025', 'p2024'])
|
||||
const started = prepareFiscalPeriods([p2024, p2027, p2026, p2025], true, TODAY)
|
||||
expect(started.map((p) => p.id)).toEqual(['p2026', 'p2025', 'p2024'])
|
||||
})
|
||||
|
||||
it('does not mutate the input', () => {
|
||||
const input = [p2024, p2026]
|
||||
prepareFiscalPeriods(input, false, TODAY)
|
||||
expect(input.map((p) => p.id)).toEqual(['p2024', 'p2026'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('resolveInitialFiscalScope', () => {
|
||||
const periods = prepareFiscalPeriods([p2024, p2025, p2026], false, TODAY)
|
||||
|
||||
it('restores a persisted period that still exists', () => {
|
||||
expect(resolveInitialFiscalScope(periods, 'p2025', { includeAllOption: true })).toEqual({
|
||||
periodId: 'p2025',
|
||||
period: p2025,
|
||||
})
|
||||
})
|
||||
|
||||
it('ignores a stale persisted id and falls back per the surface', () => {
|
||||
expect(resolveInitialFiscalScope(periods, 'gone', { includeAllOption: true })).toBeNull()
|
||||
expect(resolveInitialFiscalScope(periods, 'gone', { includeAllOption: false })).toEqual({
|
||||
periodId: 'p2026',
|
||||
period: p2026,
|
||||
})
|
||||
})
|
||||
|
||||
it('honours an explicit "all years" only where the surface allows it', () => {
|
||||
expect(resolveInitialFiscalScope(periods, ALL_YEARS_VALUE, { includeAllOption: true })).toEqual({
|
||||
periodId: null,
|
||||
period: null,
|
||||
})
|
||||
expect(resolveInitialFiscalScope(periods, ALL_YEARS_VALUE, { includeAllOption: false })).toEqual({
|
||||
periodId: 'p2026',
|
||||
period: p2026,
|
||||
})
|
||||
})
|
||||
|
||||
it('with nothing stored: all-years surfaces stay unfiltered, others pick the newest', () => {
|
||||
expect(resolveInitialFiscalScope(periods, null, { includeAllOption: true })).toBeNull()
|
||||
expect(resolveInitialFiscalScope(periods, null, { includeAllOption: false })).toEqual({
|
||||
periodId: 'p2026',
|
||||
period: p2026,
|
||||
})
|
||||
})
|
||||
|
||||
it('preferLatestEnded opens on the most recently ended period and ignores the stored choice', () => {
|
||||
expect(
|
||||
resolveInitialFiscalScope(periods, 'p2024', { includeAllOption: false, preferLatestEnded: true, today: TODAY }),
|
||||
).toEqual({ periodId: 'p2025', period: p2025 })
|
||||
})
|
||||
|
||||
it('preferLatestEnded falls back to the newest period when none has ended', () => {
|
||||
const onlyCurrent = prepareFiscalPeriods([p2026], false, TODAY)
|
||||
expect(
|
||||
resolveInitialFiscalScope(onlyCurrent, null, { includeAllOption: false, preferLatestEnded: true, today: TODAY }),
|
||||
).toEqual({ periodId: 'p2026', period: p2026 })
|
||||
})
|
||||
|
||||
it('returns null for an empty list on every path', () => {
|
||||
expect(resolveInitialFiscalScope([], null, { includeAllOption: false })).toBeNull()
|
||||
expect(resolveInitialFiscalScope([], ALL_YEARS_VALUE, { includeAllOption: false })).toBeNull()
|
||||
expect(resolveInitialFiscalScope([], 'x', { includeAllOption: false, preferLatestEnded: true })).toBeNull()
|
||||
expect(resolveInitialFiscalScope([], ALL_YEARS_VALUE, { includeAllOption: true })).toEqual({
|
||||
periodId: null,
|
||||
period: null,
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,57 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { REFERENCE_KINDS, isReferenceKey, refKeys } from '../keys'
|
||||
|
||||
describe('refKeys', () => {
|
||||
it('returns null for every builder when there is no active company', () => {
|
||||
expect(refKeys.companySettings(null)).toBeNull()
|
||||
expect(refKeys.fiscalPeriods(null)).toBeNull()
|
||||
expect(refKeys.cashAccounts(null)).toBeNull()
|
||||
expect(refKeys.accounts(null)).toBeNull()
|
||||
expect(refKeys.dimensions(null)).toBeNull()
|
||||
expect(refKeys.bookingTemplates(null)).toBeNull()
|
||||
expect(refKeys.customers(null)).toBeNull()
|
||||
expect(refKeys.suppliers(null)).toBeNull()
|
||||
expect(refKeys.articles(null)).toBeNull()
|
||||
})
|
||||
|
||||
it('puts the kind first and the company id second on every key', () => {
|
||||
const keys = [
|
||||
refKeys.companySettings('c1'),
|
||||
refKeys.fiscalPeriods('c1'),
|
||||
refKeys.cashAccounts('c1'),
|
||||
refKeys.accounts('c1'),
|
||||
refKeys.dimensions('c1'),
|
||||
refKeys.bookingTemplates('c1'),
|
||||
refKeys.customers('c1'),
|
||||
refKeys.suppliers('c1'),
|
||||
refKeys.articles('c1'),
|
||||
]
|
||||
for (const key of keys) {
|
||||
expect(key).not.toBeNull()
|
||||
expect(REFERENCE_KINDS).toContain(key![0])
|
||||
expect(key![1]).toBe('c1')
|
||||
}
|
||||
expect(new Set(keys.map((k) => k![0])).size).toBe(keys.length)
|
||||
})
|
||||
|
||||
it('keeps the legacy company_settings key shape used by useCompanySettings', () => {
|
||||
expect(refKeys.companySettings('c1')).toEqual(['company_settings', 'c1'])
|
||||
})
|
||||
|
||||
it('varies the accounts and articles keys on their filter flag', () => {
|
||||
expect(refKeys.accounts('c1')).toEqual(['ref:accounts', 'c1', true])
|
||||
expect(refKeys.accounts('c1', false)).toEqual(['ref:accounts', 'c1', false])
|
||||
expect(refKeys.articles('c1')).toEqual(['ref:articles', 'c1', false])
|
||||
expect(refKeys.articles('c1', true)).toEqual(['ref:articles', 'c1', true])
|
||||
})
|
||||
})
|
||||
|
||||
describe('isReferenceKey', () => {
|
||||
it('accepts keys from the builders and rejects everything else', () => {
|
||||
expect(isReferenceKey(refKeys.fiscalPeriods('c1'))).toBe(true)
|
||||
expect(isReferenceKey(['company_settings', 'c1'])).toBe(true)
|
||||
expect(isReferenceKey(['worklist-badges', 'c1'])).toBe(false)
|
||||
expect(isReferenceKey('/api/settings')).toBe(false)
|
||||
expect(isReferenceKey(null)).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -1,37 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { unstable_serialize } from 'swr'
|
||||
import { buildReferenceFallback } from '../seed'
|
||||
import { refKeys } from '../keys'
|
||||
import type { CashAccount, CompanySettings, FiscalPeriod } from '@/types'
|
||||
|
||||
const periods = [{ id: 'p1' }] as unknown as FiscalPeriod[]
|
||||
const cash = [{ id: 'a1' }] as unknown as CashAccount[]
|
||||
const settings = { company_id: 'c1', company_name: 'Bolaget AB' } as unknown as CompanySettings
|
||||
|
||||
describe('buildReferenceFallback', () => {
|
||||
it('keys the seed exactly as the hooks key their cache entries', () => {
|
||||
const fallback = buildReferenceFallback('c1', { fiscalPeriods: periods, cashAccounts: cash, settings })
|
||||
expect(Object.keys(fallback).sort()).toEqual(
|
||||
[
|
||||
unstable_serialize(refKeys.fiscalPeriods('c1')),
|
||||
unstable_serialize(refKeys.cashAccounts('c1')),
|
||||
unstable_serialize(refKeys.companySettings('c1')),
|
||||
].sort(),
|
||||
)
|
||||
expect(fallback[unstable_serialize(refKeys.fiscalPeriods('c1'))]).toBe(periods)
|
||||
expect(fallback[unstable_serialize(refKeys.cashAccounts('c1'))]).toBe(cash)
|
||||
expect(fallback[unstable_serialize(refKeys.companySettings('c1'))]).toBe(settings)
|
||||
})
|
||||
|
||||
it('seeds a null settings row (no row yet) but omits settings when not fetched', () => {
|
||||
const withNull = buildReferenceFallback('c1', { fiscalPeriods: [], cashAccounts: [], settings: null })
|
||||
expect(withNull[unstable_serialize(refKeys.companySettings('c1'))]).toBeNull()
|
||||
|
||||
const notFetched = buildReferenceFallback('c1', { fiscalPeriods: [], cashAccounts: [] })
|
||||
expect(unstable_serialize(refKeys.companySettings('c1')) in notFetched).toBe(false)
|
||||
})
|
||||
|
||||
it('seeds nothing without an active company', () => {
|
||||
expect(buildReferenceFallback(null, { fiscalPeriods: periods, cashAccounts: cash, settings })).toEqual({})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user