Files
accounted/app/api/dimensions/__tests__/value-id.test.ts
T
Jakob Wennberg ec27228a8e style: remove em/en dashes repo-wide, add CLAUDE.md rule against them (#890)
Em dashes (—) and en dashes (–) had spread across comments, docs, tests,
and a few UI strings, reading as AI-generated boilerplate rather than
house style. Replaced each with punctuation matching its context: colon
for explanatory clauses, comma for asides, plain hyphen for numeric/legal
ranges (e.g. "21-23§"), "to"/"till" for date ranges, parentheses for
paired-dash asides. messages/en.json and messages/sv.json were fixed by
hand together to keep sv/en in sync.

Left untouched where the dash is the functional subject rather than
decorative punctuation: date-range-parser.ts's separator regex,
charset-repair.ts's CP1252 byte-mapping table (and its test), the SIE
encoding mojibake docs, generic-csv.ts's minus-sign normalizer, the
agent system-prompt files that already instruct against em dashes, and
a golden iXBRL test fixture compared byte-for-byte.

Also fixes two bugs surfaced along the way: an off-by-one in
ApiKeysPanel's scope-label split (a leftover from an earlier partial
pass), and a charset-repair test that had lost the literal en-dash it
exists to verify.

Regenerated the agent atom seed migration (skills:generate) since 27
SKILL.md files changed. Added a CLAUDE.md rule against em/en dashes,
with an explicit carve-out for the functional-dash cases above.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-04 15:58:06 +02:00

196 lines
7.0 KiB
TypeScript

/**
* Tests for PATCH/DELETE /api/dimensions/[id]/values/[valueId].
*
* The DELETE suite pins the retention-trigger contract: a P0001 raise from
* enforce_dimension_value_retention surfaces as 409 DIMENSION_VALUE_REFERENCED
* with the trigger's own Swedish message ("…arkivera det istället") so the UI
* can toast it verbatim.
*/
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { NextResponse } from 'next/server'
import {
createQueuedMockSupabase,
createMockRequest,
createMockRouteParams,
parseJsonResponse,
} from '@/tests/helpers'
const { supabase, enqueue, reset } = createQueuedMockSupabase()
const requireAuthMock = vi.fn()
vi.mock('@/lib/auth/require-auth', () => ({
requireAuth: (...args: unknown[]) => requireAuthMock(...args),
}))
vi.mock('@/lib/company/context', () => ({
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
}))
vi.mock('@/lib/auth/require-write', () => ({
requireWritePermission: vi.fn().mockResolvedValue({ ok: true }),
}))
vi.mock('@/lib/init', () => ({ ensureInitialized: vi.fn() }))
import { PATCH, DELETE } from '../[id]/values/[valueId]/route'
const params = () => createMockRouteParams({ id: 'dim-1', valueId: 'v1' })
const URL_PATH = '/api/dimensions/dim-1/values/v1'
describe('PATCH /api/dimensions/[id]/values/[valueId]', () => {
beforeEach(() => {
vi.clearAllMocks()
reset()
requireAuthMock.mockResolvedValue({ user: { id: 'user-1' }, supabase })
})
it('returns 401 when not authenticated', async () => {
requireAuthMock.mockResolvedValue({
error: NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
})
const request = createMockRequest(URL_PATH, { method: 'PATCH', body: { name: 'Nytt namn' } })
const response = await PATCH(request, params())
expect(response.status).toBe(401)
})
it('rejects an empty body with 400', async () => {
const request = createMockRequest(URL_PATH, { method: 'PATCH', body: {} })
const response = await PATCH(request, params())
expect(response.status).toBe(400)
})
it('returns 404 when the value does not exist in the company', async () => {
enqueue({ error: { code: 'PGRST116', message: 'No rows found' } })
const request = createMockRequest(URL_PATH, { method: 'PATCH', body: { name: 'Nytt namn' } })
const response = await PATCH(request, params())
const { status, body } = await parseJsonResponse<{ error: { code: string } }>(response)
expect(status).toBe(404)
expect(body.error.code).toBe('DIMENSION_VALUE_NOT_FOUND')
})
it('archives a value via is_active=false (happy path)', async () => {
// The request carries end_date, so the route first checks the parent
// dimension's resets_annually flag (accumulating → dates allowed).
enqueue({ data: { id: 'dim-1', resets_annually: false } })
enqueue({
data: {
id: 'v1', dimension_id: 'dim-1', code: 'BUTIK', name: 'Butiken',
is_active: false, start_date: null, end_date: '2026-06-30',
},
})
const request = createMockRequest(URL_PATH, {
method: 'PATCH',
body: { is_active: false, end_date: '2026-06-30' },
})
const response = await PATCH(request, params())
const { status, body } = await parseJsonResponse<{ data: { is_active: boolean; code: string } }>(response)
expect(status).toBe(200)
expect(body.data.is_active).toBe(false)
// Code untouched: immutable in v1.
expect(body.data.code).toBe('BUTIK')
})
it('rejects start/end dates on a resets-annually dimension with 400', async () => {
enqueue({ data: { id: 'dim-1', resets_annually: true } })
const request = createMockRequest(URL_PATH, {
method: 'PATCH',
body: { start_date: '2026-01-01' },
})
const response = await PATCH(request, params())
const { status, body } = await parseJsonResponse<{ error: { code: string; message: string } }>(response)
expect(status).toBe(400)
expect(body.error.code).toBe('DIMENSION_VALUE_DATES_NOT_ALLOWED')
expect(body.error.message).toBe(
'Datum kan bara sättas på ackumulerande dimensioner (t.ex. projekt).',
)
})
it('allows clearing dates (explicit null) without checking the dimension', async () => {
// start_date: null clears the field, a harmless no-op on any dimension,
// so the route must not spend a dimension fetch on it. The single queued
// result feeds the update itself.
enqueue({
data: {
id: 'v1', dimension_id: 'dim-1', code: 'BUTIK', name: 'Butiken',
is_active: true, start_date: null, end_date: null,
},
})
const request = createMockRequest(URL_PATH, {
method: 'PATCH',
body: { start_date: null, end_date: null },
})
const response = await PATCH(request, params())
const { status } = await parseJsonResponse(response)
expect(status).toBe(200)
})
})
describe('DELETE /api/dimensions/[id]/values/[valueId]', () => {
beforeEach(() => {
vi.clearAllMocks()
reset()
requireAuthMock.mockResolvedValue({ user: { id: 'user-1' }, supabase })
})
it('returns 401 when not authenticated', async () => {
requireAuthMock.mockResolvedValue({
error: NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
})
const request = createMockRequest(URL_PATH, { method: 'DELETE' })
const response = await DELETE(request, params())
expect(response.status).toBe(401)
})
it('surfaces the retention trigger as 409 with the trigger\'s Swedish message', async () => {
const triggerMessage =
'Värdet "BUTIK" används på bokförda verifikat och kan inte tas bort: arkivera det istället.'
enqueue({ error: { code: 'P0001', message: triggerMessage } })
const request = createMockRequest(URL_PATH, { method: 'DELETE' })
const response = await DELETE(request, params())
const { status, body } = await parseJsonResponse<{ error: { code: string; message: string } }>(response)
expect(status).toBe(409)
expect(body.error.code).toBe('DIMENSION_VALUE_REFERENCED')
// The trigger's message (naming the code) is surfaced verbatim for the toast.
expect(body.error.message).toBe(triggerMessage)
expect(body.error.message).toContain('arkivera det istället')
})
it('returns 404 when nothing was deleted', async () => {
enqueue({ data: [] })
const request = createMockRequest(URL_PATH, { method: 'DELETE' })
const response = await DELETE(request, params())
const { status, body } = await parseJsonResponse<{ error: { code: string } }>(response)
expect(status).toBe(404)
expect(body.error.code).toBe('DIMENSION_VALUE_NOT_FOUND')
})
it('deletes an unreferenced value (happy path)', async () => {
enqueue({ data: [{ id: 'v1' }] })
const request = createMockRequest(URL_PATH, { method: 'DELETE' })
const response = await DELETE(request, params())
const { status, body } = await parseJsonResponse<{ success: boolean }>(response)
expect(status).toBe(200)
expect(body.success).toBe(true)
})
})