Skip to content

Commit 15294a2

Browse files
Bill LeoutsakosBill Leoutsakos
authored andcommitted
fix(oracle-fusion): treat collection totals as estimates
1 parent 29d3eee commit 15294a2

2 files changed

Lines changed: 43 additions & 14 deletions

File tree

apps/sim/lib/internal/oracle-fusion/protocol.test.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,48 @@ describe('parseOracleFusionCollection', () => {
6363
{ items: [], count: 0, hasMore: false, limit: 25, offset: 5, totalResults: 6 },
6464
{ items: [{}], count: 1, hasMore: false, limit: 25, offset: 5, totalResults: 7 },
6565
{ items: [{}], count: 1, hasMore: true, limit: 25, offset: 5, totalResults: 6 },
66-
])('rejects pagination metadata that contradicts total results %#', (value) => {
67-
expect(() => parseOracleFusionCollection(value, (item) => item)).toThrow(
68-
'hasMore contradicts totalResults'
69-
)
66+
{ items: [{}], count: 1, hasMore: false, limit: 25, offset: 4, totalResults: 4 },
67+
{ items: [{}], count: 1, hasMore: true, limit: 25, offset: 5, totalResults: 4 },
68+
{ items: [{}], count: 1, hasMore: false, limit: 25, offset: 5, totalResults: 0 },
69+
{ items: [{}], count: 1, hasMore: true, limit: 25, offset: 5, totalResults: 0 },
70+
])('preserves estimated totals independently of pagination metadata %#', (value) => {
71+
expect(parseOracleFusionCollection(value, (item) => item)).toEqual({
72+
...value,
73+
nextOffset: value.offset + value.count,
74+
})
75+
})
76+
77+
it.each(
78+
[
79+
-1,
80+
0.5,
81+
'1',
82+
null,
83+
true,
84+
{},
85+
[],
86+
Number.NaN,
87+
Number.POSITIVE_INFINITY,
88+
Number.NEGATIVE_INFINITY,
89+
Number.MAX_SAFE_INTEGER + 1,
90+
].map((totalResults) => ({ totalResults }))
91+
)('rejects malformed estimated total $totalResults', ({ totalResults }) => {
92+
const parseItem = vi.fn((item) => item)
93+
expect(() =>
94+
parseOracleFusionCollection(
95+
{ items: [{}], count: 1, hasMore: false, limit: 25, offset: 0, totalResults },
96+
parseItem
97+
)
98+
).toThrow('Oracle collection totalResults must be a non-negative safe integer')
99+
expect(parseItem).not.toHaveBeenCalled()
100+
})
101+
102+
it('does not synthesize an omitted total for a nonempty page', () => {
103+
const page = { items: [{}], count: 1, hasMore: true, limit: 25, offset: 5 }
104+
const result = parseOracleFusionCollection(page, (item) => item)
105+
106+
expect(result).toEqual({ ...page, nextOffset: 6 })
107+
expect(result).not.toHaveProperty('totalResults')
70108
})
71109

72110
it('accepts omitted items only for an unambiguous empty terminal page', () => {
@@ -118,7 +156,6 @@ describe('parseOracleFusionCollection', () => {
118156
[{ items: [{}], count: 0, hasMore: false, limit: 25, offset: 0 }, 'match'],
119157
[{ items: [], count: 0, hasMore: true, limit: 25, offset: 0 }, 'empty page'],
120158
[{ items: [], count: 0, hasMore: false, limit: 0, offset: 0 }, 'positive'],
121-
[{ items: [{}], count: 1, hasMore: false, limit: 25, offset: 4, totalResults: 4 }, 'smaller'],
122159
[
123160
{ items: [{}], count: 1, hasMore: true, limit: 25, offset: Number.MAX_SAFE_INTEGER },
124161
'safe integer range',

apps/sim/lib/internal/oracle-fusion/protocol.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export function parseOracleFusionCollection<T>(
7676
throw new Error('Oracle collection cannot report hasMore for an empty page')
7777
}
7878

79+
/** Estimated metadata only; it does not constrain the returned page or hasMore. */
7980
const totalResults =
8081
envelope.totalResults === undefined
8182
? undefined
@@ -84,15 +85,6 @@ export function parseOracleFusionCollection<T>(
8485
if (!Number.isSafeInteger(pageEnd)) {
8586
throw new Error('Oracle collection next offset exceeds the safe integer range')
8687
}
87-
if (totalResults !== undefined && count > 0 && totalResults < pageEnd) {
88-
throw new Error('Oracle collection totalResults is smaller than the returned page')
89-
}
90-
if (totalResults !== undefined && !envelope.hasMore && totalResults > pageEnd) {
91-
throw new Error('Oracle collection hasMore contradicts totalResults')
92-
}
93-
if (totalResults !== undefined && envelope.hasMore && totalResults <= pageEnd) {
94-
throw new Error('Oracle collection hasMore contradicts totalResults')
95-
}
9688
if (options.expectedOffset !== undefined) {
9789
const expectedOffset = nonNegativeInteger(
9890
options.expectedOffset,

0 commit comments

Comments
 (0)