Files
Jakob Wennberg 97907a5a5c fix: article ordering, free-text rows, invoice back-nav, onboarding resilience (#1053) (#1056)
* fix: article number ordering, free-text rows, invoice back-nav, onboarding resilience (#1053)

Four fixes from Discord feedback in issue #1053:

- Articles now order by article number with numeric-aware comparison
  ('2' before '10', unnumbered last, name tiebreak) in the invoice
  editor's article picker and as the register's default sort, via a
  shared lib/articles/sort.ts. Name order put article "1" last.

- Invoice rows with no amounts (quantity 0, unit price 0) render as
  pure text rows on the PDF, the invoice detail page, and the review
  step via shared isTextLikeLine(), instead of printing
  "0 / 0,00 SEK / 0,00 SEK". Display-only; booking untouched.

- The invoice editor navigates with router.replace after saving, so
  the detail page's back arrow returns to the list instead of
  reopening a fresh editor from history.

- A transient query failure no longer reads as "no companies" /
  "onboarding not done": getActiveCompanyId throws
  CompanyContextError('resolution_failed') instead of returning null,
  the Edge middleware fails open on a degraded resolution (no
  onboarding redirect, no cookie clearing, no locale overwrite), and
  the dashboard page only redirects to /onboarding on a positively
  read incomplete/missing settings row. This is the likely cause of
  the completed onboarding wizard reappearing.

Fixes #1053

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs: CLAUDE.md tenancy line matches actual resolution order (prefs-first, cookie not read)

The middleware stopped reading the gnubok-company-id cookie when
user_preferences became authoritative (RLS parity); the stale doc line
still described cookie-first order and misled review tooling.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-17 14:45:43 +02:00

72 lines
2.5 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { compareArticles, sortArticles } from '@/lib/articles/sort'
describe('sortArticles', () => {
it('orders by article number numerically, not alphabetically', () => {
const sorted = sortArticles([
{ article_number: '10', name: 'Tio' },
{ article_number: '2', name: 'Två' },
{ article_number: '1', name: 'Ett' },
])
expect(sorted.map((a) => a.article_number)).toEqual(['1', '2', '10'])
})
it('reproduces issue #1053: numbered register no longer sorts by name', () => {
const sorted = sortArticles([
{ article_number: '2', name: 'Delbehandling' },
{ article_number: '4', name: 'Fotvårdsremiss 85+' },
{ article_number: '5', name: 'Fotvårdsremiss ej frikort' },
{ article_number: '3', name: 'Fotvårdsremiss frikort' },
{ article_number: '1', name: 'Medicinsk fotvård' },
])
expect(sorted.map((a) => a.article_number)).toEqual(['1', '2', '3', '4', '5'])
})
it('puts articles without a number last, sorted by name', () => {
const sorted = sortArticles([
{ article_number: null, name: 'Zeta' },
{ article_number: '7', name: 'Sju' },
{ article_number: null, name: 'Alfa' },
{ article_number: undefined, name: 'Beta' },
])
expect(sorted.map((a) => a.name)).toEqual(['Sju', 'Alfa', 'Beta', 'Zeta'])
})
it('treats blank article numbers as unnumbered', () => {
const sorted = sortArticles([
{ article_number: ' ', name: 'Blank' },
{ article_number: '1', name: 'Ett' },
])
expect(sorted.map((a) => a.name)).toEqual(['Ett', 'Blank'])
})
it('breaks ties on equal numbers by name', () => {
const sorted = sortArticles([
{ article_number: '1', name: 'B' },
{ article_number: '1', name: 'A' },
])
expect(sorted.map((a) => a.name)).toEqual(['A', 'B'])
})
it('handles alphanumeric numbers with embedded digits', () => {
const sorted = sortArticles([
{ article_number: 'A10', name: 'x' },
{ article_number: 'A2', name: 'y' },
])
expect(sorted.map((a) => a.article_number)).toEqual(['A2', 'A10'])
})
it('does not mutate the input array', () => {
const input = [
{ article_number: '2', name: 'b' },
{ article_number: '1', name: 'a' },
]
sortArticles(input)
expect(input.map((a) => a.article_number)).toEqual(['2', '1'])
})
it('compareArticles is exported for column sorters', () => {
expect(compareArticles({ article_number: '2', name: 'x' }, { article_number: '10', name: 'y' })).toBeLessThan(0)
})
})