Files
accounted/lib/supabase/__tests__/fetch-all.test.ts
T
Jakob Wennberg fce6faff2c fix(api): stabilize report pagination + declare real { data, meta } envelope on v1 single/write endpoints (#811)
* fix(reports): stabilize fetchAllRows paging to stop doubled/dropped balances (#790, #791)

PostgREST `.range()` paging is only correct when the underlying query has a
stable TOTAL order. Several aggregating report queries (general ledger, trial
balance, grundbok, supplier/AR ledgers, etc.) paginated without `.order()`, so
on datasets larger than one 1000-row page Postgres could return rows in a
different order between requests — silently DUPLICATING or SKIPPING rows on a
page boundary and doubling or dropping financial totals.

- fetch-all.ts: document the ordering invariant and add an optional
  `dedupeBy` defense-in-depth that drops cross-page duplicates and warns when
  it fires (surfaces a missing `.order()` in logs instead of corrupting money).
- Add a stable `.order()` (line PK or account_number) to every paginated query
  in lib/reports/ and the account-balances route; pass `dedupeBy` on the
  money-aggregating line queries.
- Add fetch-all unit tests and update report test fixtures to carry row ids.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(api): declare the real { data, meta } envelope on v1 single/write/204 endpoints (#794)

The OpenAPI generator derives each endpoint's documented body purely from its
registered `response.success` Zod schema, and that schema is never validated at
runtime — so a route could advertise a shape its handler never sends. #802
fixed this for list endpoints; the same drift was latent on single-resource and
write endpoints, which declared the bare resource schema instead of the
`{ data, meta }` envelope the handlers actually return.

- registry.ts: extend `ResponseMetaSchema` with the optional `audit` block and
  `partial_expansions` list that writes/expansions emit; add the `NoBodyResponse`
  sentinel so 204 DELETE handlers document a bare 204 instead of a phantom 200.
- Wrap every single/write endpoint's `response.success` in `dataEnvelope(...)`
  (or `NoBodyResponse` for 204s) across the v1 routes.
- Add a response-envelope contract test that fails CI if any JSON endpoint
  forgets to wrap its schema, with binary downloads and 204s as the only
  exemptions.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(reports): extend paging dedupeBy to rc-basis-gaps and opening-balances

Address PR review: these two money-aggregating line queries already had the
stable `.order('id')` (so paging was correct) but didn't carry `id` in the
select, so they couldn't use the `dedupeBy` defense-in-depth that general-ledger
and trial-balance got. Select `id` and pass `dedupeBy: r => r.id` so the whole
report layer applies the ordering invariant consistently.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-28 13:42:50 +02:00

89 lines
3.4 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { fetchAllRows } from '../fetch-all'
const PAGE_SIZE = 1000
type Row = { id: string; value?: number }
/**
* Build a queryFn that serves predefined pages keyed by the `from` offset.
* Mirrors how `fetchAllRows` drives PostgREST `.range(from, to)`.
*/
function pagedQuery(pages: Record<number, Row[]>) {
return ({ from }: { from: number; to: number }) =>
Promise.resolve({ data: pages[from] ?? [], error: null })
}
function makeRows(start: number, count: number): Row[] {
return Array.from({ length: count }, (_, i) => ({ id: String(start + i), value: 1 }))
}
describe('fetchAllRows', () => {
it('returns a single page as-is and stops (page < PAGE_SIZE)', async () => {
const rows = makeRows(0, 3)
const out = await fetchAllRows<Row>(pagedQuery({ 0: rows }))
expect(out).toHaveLength(3)
expect(out.map((r) => r.id)).toEqual(['0', '1', '2'])
})
it('paginates across multiple pages and concatenates in order', async () => {
const page1 = makeRows(0, PAGE_SIZE) // full page → fetch continues
const page2 = makeRows(PAGE_SIZE, 5) // partial page → stop
const out = await fetchAllRows<Row>(pagedQuery({ 0: page1, [PAGE_SIZE]: page2 }))
expect(out).toHaveLength(PAGE_SIZE + 5)
expect(out[0].id).toBe('0')
expect(out[out.length - 1].id).toBe(String(PAGE_SIZE + 4))
})
it('throws when the query returns an error', async () => {
await expect(
fetchAllRows<Row>(() => Promise.resolve({ data: null, error: { message: 'boom' } })),
).rejects.toThrow('boom')
})
it('returns [] when the first page is empty', async () => {
const out = await fetchAllRows<Row>(pagedQuery({ 0: [] }))
expect(out).toEqual([])
})
// ── The regression-critical behaviour: an unstable cross-page order ──
// (a query missing a stable .order()) can return the same row on two
// pages. This is the mechanism behind the doubled-balance bugs (#790/#791).
it('dedupeBy drops a row duplicated across page boundaries (keeps first)', async () => {
const page1 = makeRows(0, PAGE_SIZE) // ids 0..999
// Unstable order: page 2 re-serves id "999" (already on page 1) plus a new id.
const page2: Row[] = [
{ id: '999', value: 1 },
{ id: '1000', value: 1 },
]
const out = await fetchAllRows<Row>(
pagedQuery({ 0: page1, [PAGE_SIZE]: page2 }),
{ dedupeBy: (r) => r.id },
)
// 1001 unique ids (0..1000), the duplicate "999" removed → no doubling.
expect(out).toHaveLength(PAGE_SIZE + 1)
const ids = out.map((r) => r.id)
expect(ids.filter((id) => id === '999')).toHaveLength(1)
expect(new Set(ids).size).toBe(out.length)
})
it('without dedupeBy, cross-page duplicates pass through (unsafe default)', async () => {
const page1 = makeRows(0, PAGE_SIZE)
const page2: Row[] = [{ id: '999', value: 1 }]
const out = await fetchAllRows<Row>(pagedQuery({ 0: page1, [PAGE_SIZE]: page2 }))
expect(out).toHaveLength(PAGE_SIZE + 1)
expect(out.map((r) => r.id).filter((id) => id === '999')).toHaveLength(2)
})
it('dedupeBy is a no-op for a single page (no cross-page duplicates possible)', async () => {
const rows: Row[] = [
{ id: 'a' },
{ id: 'b' },
{ id: 'a' }, // an intra-page repeat is left untouched — single page is trusted
]
const out = await fetchAllRows<Row>(pagedQuery({ 0: rows }), { dedupeBy: (r) => r.id })
expect(out).toHaveLength(3)
})
})