Files
simstudioai--sim/apps/sim/hooks/queries/utils/table-rows-pagination.test.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

82 lines
2.7 KiB
TypeScript

/**
* @vitest-environment node
*/
import { describe, expect, it } from 'vitest'
import {
countLoadedTableRows,
getNextTableRowsPageParam,
hasMoreTableRows,
} from '@/hooks/queries/utils/table-rows-pagination'
function makePage(count: number, totalCount: number | null, startAt = 0, withOrderKey = true) {
return {
rows: Array.from({ length: count }, (_, i) => ({
id: `r${startAt + i}`,
...(withOrderKey ? { orderKey: `k${String(startAt + i).padStart(6, '0')}` } : {}),
})),
totalCount,
}
}
describe('countLoadedTableRows', () => {
it('sums rows across pages', () => {
expect(countLoadedTableRows([])).toBe(0)
expect(countLoadedTableRows([makePage(3, 10), makePage(2, null, 3)])).toBe(5)
})
})
describe('hasMoreTableRows', () => {
it('returns false with no pages', () => {
expect(hasMoreTableRows([])).toBe(false)
})
it('returns false when the last page is empty', () => {
expect(hasMoreTableRows([makePage(1000, null), makePage(0, null, 1000)])).toBe(false)
})
it('returns false when the page-0 count is covered', () => {
expect(hasMoreTableRows([makePage(3, 3)])).toBe(false)
})
it('returns true for a short page when the count says more exist', () => {
// The regression this module exists for: a page shorter than the requested
// size must never be read as end-of-table on its own.
expect(hasMoreTableRows([makePage(36, 100)])).toBe(true)
})
it('returns true when the count is unknown and the last page is non-empty', () => {
expect(hasMoreTableRows([makePage(1000, null)])).toBe(true)
})
it('returns false when a stale-low count is already exceeded', () => {
expect(hasMoreTableRows([makePage(10, 5)])).toBe(false)
})
})
describe('getNextTableRowsPageParam', () => {
it('returns undefined when no more rows exist', () => {
expect(getNextTableRowsPageParam([makePage(3, 3)], false)).toBeUndefined()
expect(getNextTableRowsPageParam([makePage(1000, null), makePage(0, null)], false)).toBe(
undefined
)
})
it('returns the keyset cursor of the last loaded row on the default order', () => {
const pages = [makePage(1000, 2000), makePage(500, null, 1000)]
expect(getNextTableRowsPageParam(pages, false)).toEqual({
orderKey: 'k001499',
id: 'r1499',
})
})
it('returns the loaded-row offset for sorted views, even after short pages', () => {
const pages = [makePage(1000, 2000), makePage(36, null, 1000)]
expect(getNextTableRowsPageParam(pages, true)).toBe(1036)
})
it('falls back to the loaded-row offset when the last row has no order key', () => {
const pages = [makePage(700, 2000, 0, false)]
expect(getNextTableRowsPageParam(pages, false)).toBe(700)
})
})