Files
accounted/lib/api/v1/__tests__/expand.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

60 lines
2.0 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { parseExpand } from '../expand'
const ALLOWED = ['customer', 'items', 'payments'] as const
const urlWith = (q: string): URL => new URL(`https://x.test/path${q}`)
describe('parseExpand', () => {
it('returns an empty Set when no expand param is present', () => {
const r = parseExpand(urlWith(''), ALLOWED)
expect(r.ok).toBe(true)
if (r.ok) expect(r.expand.size).toBe(0)
})
it('parses a single key', () => {
const r = parseExpand(urlWith('?expand=customer'), ALLOWED)
expect(r.ok).toBe(true)
if (r.ok) expect([...r.expand]).toEqual(['customer'])
})
it('parses multiple comma-separated keys', () => {
const r = parseExpand(urlWith('?expand=customer,items'), ALLOWED)
expect(r.ok).toBe(true)
if (r.ok) expect([...r.expand].sort()).toEqual(['customer', 'items'])
})
it('trims whitespace around keys', () => {
const r = parseExpand(urlWith('?expand=customer , items'), ALLOWED)
expect(r.ok).toBe(true)
if (r.ok) expect([...r.expand].sort()).toEqual(['customer', 'items'])
})
it('collapses duplicates', () => {
const r = parseExpand(urlWith('?expand=customer,customer'), ALLOWED)
expect(r.ok).toBe(true)
if (r.ok) expect(r.expand.size).toBe(1)
})
it('rejects unknown keys with VALIDATION_ERROR-shaped result', () => {
const r = parseExpand(urlWith('?expand=customer,bogus'), ALLOWED)
expect(r.ok).toBe(false)
if (!r.ok) {
expect(r.invalidKeys).toEqual(['bogus'])
expect(r.allowed).toEqual(['customer', 'items', 'payments'])
}
})
it('reports all invalid keys, not just the first', () => {
const r = parseExpand(urlWith('?expand=foo,bar,customer'), ALLOWED)
expect(r.ok).toBe(false)
if (!r.ok) expect(r.invalidKeys.sort()).toEqual(['bar', 'foo'])
})
it('ignores empty entries from trailing commas', () => {
const r = parseExpand(urlWith('?expand=customer,'), ALLOWED)
expect(r.ok).toBe(true)
if (r.ok) expect([...r.expand]).toEqual(['customer'])
})
})