|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #10757 — `$count=false` skips the COUNT query. |
| 5 | + * |
| 6 | + * ## What was wrong |
| 7 | + * |
| 8 | + * `$count` has been a fully-plumbed parameter for a long time: declared in the |
| 9 | + * spec (`ODataQuerySchema.$count`, `packages/spec/src/api/odata.zod.ts`), |
| 10 | + * aliased on the wire (`$count` → `count`, `WIRE_DOLLAR_ALIASES`), reserved out |
| 11 | + * of the implicit-field-filter bucket (`RESERVED_LIST_QUERY_PARAMS`), |
| 12 | + * arity-checked (`protocol.query-param-arity.test.ts`) and boolean-coerced — |
| 13 | + * and then DELETED unread by the protocol-key strip in `findData`. So every |
| 14 | + * paginated list issued `engine.count()` whether or not the caller wanted a |
| 15 | + * `total`, which on a remote database is a whole round trip per request. The |
| 16 | + * measured trace on a real stack put it at query 24 of 24 for one |
| 17 | + * `GET /data/:object?$top=1`. |
| 18 | + * |
| 19 | + * ## The two directions this suite pins, and why both are needed |
| 20 | + * |
| 21 | + * 1. **The OPT-OUT works** — an explicit `false` (either spelling) means no |
| 22 | + * `engine.count()` call and no `total` key. `expect(count).not.toHaveBeenCalled()` |
| 23 | + * is the load-bearing assertion; asserting only the absent `total` would |
| 24 | + * stay green if a future edit ran the query and merely dropped the number, |
| 25 | + * which is the whole cost with none of the saving. |
| 26 | + * |
| 27 | + * 2. **Nothing else changed** — absent `$count`, and explicit `$count=true`, |
| 28 | + * both still count and still report `total`. This is the direction that |
| 29 | + * makes the opt-out safe to ship: OData reads an ABSENT `$count` as "omit |
| 30 | + * the count", and taking that reading here would silently strip `total` |
| 31 | + * from every existing caller (none of them send the parameter, all of them |
| 32 | + * read the number). The asymmetry is deliberate, so it is pinned rather |
| 33 | + * than left to be "tidied up" later. |
| 34 | + * |
| 35 | + * `total` is OMITTED rather than estimated — `FindDataResponseSchema` declares |
| 36 | + * it optional ("if requested"), and a page-local guess handed back to a caller |
| 37 | + * who declined the real number is how an estimate ends up rendered as a record |
| 38 | + * count. `hasMore` is still answered from the page alone. |
| 39 | + */ |
| 40 | + |
| 41 | +import { describe, it, expect, vi } from 'vitest'; |
| 42 | +import { ObjectStackProtocolImplementation } from './protocol.js'; |
| 43 | + |
| 44 | +const SCHEMA = { |
| 45 | + name: 'invoice', |
| 46 | + nameField: 'name', |
| 47 | + fields: { |
| 48 | + name: { name: 'name', type: 'text' }, |
| 49 | + status: { name: 'status', type: 'text' }, |
| 50 | + }, |
| 51 | +}; |
| 52 | + |
| 53 | +function makeProtocol(pageSize: number) { |
| 54 | + const find = vi.fn(async () => Array.from({ length: pageSize }, (_, i) => ({ id: `r${i}` }))); |
| 55 | + const count = vi.fn(async () => 3125); |
| 56 | + const engine = { |
| 57 | + registry: { getObject: (n: string) => (n === 'invoice' ? SCHEMA : undefined) }, |
| 58 | + find, |
| 59 | + count, |
| 60 | + aggregate: vi.fn(async () => [] as unknown[]), |
| 61 | + }; |
| 62 | + return { p: new ObjectStackProtocolImplementation(engine as any), find, count }; |
| 63 | +} |
| 64 | + |
| 65 | +describe('[#10757] findData honours $count=false', () => { |
| 66 | + describe('opt-out — the COUNT query is not issued', () => { |
| 67 | + // Both wire spellings reach the same normalized `count` slot; a fix that |
| 68 | + // read only one of them would leave the other paying for the query. |
| 69 | + for (const spelling of ['$count', 'count'] as const) { |
| 70 | + it(`?${spelling}=false skips engine.count() and omits total`, async () => { |
| 71 | + const { p, count } = makeProtocol(1); |
| 72 | + |
| 73 | + const result = await p.findData({ |
| 74 | + object: 'invoice', |
| 75 | + query: { $top: 1, [spelling]: 'false' }, |
| 76 | + } as never); |
| 77 | + |
| 78 | + expect(count).not.toHaveBeenCalled(); |
| 79 | + expect('total' in (result as object)).toBe(false); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + it('accepts the already-boolean form a POST body carries', async () => { |
| 84 | + const { p, count } = makeProtocol(1); |
| 85 | + |
| 86 | + const result = await p.findData({ |
| 87 | + object: 'invoice', |
| 88 | + query: { $top: 1, count: false }, |
| 89 | + } as never); |
| 90 | + |
| 91 | + expect(count).not.toHaveBeenCalled(); |
| 92 | + expect('total' in (result as object)).toBe(false); |
| 93 | + }); |
| 94 | + |
| 95 | + it('still answers hasMore from the page: a FULL page means there may be more', async () => { |
| 96 | + const { p } = makeProtocol(10); |
| 97 | + |
| 98 | + const result = await p.findData({ |
| 99 | + object: 'invoice', |
| 100 | + query: { $top: 10, $count: 'false' }, |
| 101 | + } as never); |
| 102 | + |
| 103 | + expect(result.hasMore).toBe(true); |
| 104 | + }); |
| 105 | + |
| 106 | + it('…and a SHORT page means there are not', async () => { |
| 107 | + const { p } = makeProtocol(3); |
| 108 | + |
| 109 | + const result = await p.findData({ |
| 110 | + object: 'invoice', |
| 111 | + query: { $top: 10, $count: 'false' }, |
| 112 | + } as never); |
| 113 | + |
| 114 | + expect(result.hasMore).toBe(false); |
| 115 | + }); |
| 116 | + |
| 117 | + it('leaves `count` off the engine option bag (it is a protocol-layer flag)', async () => { |
| 118 | + const { p, find } = makeProtocol(1); |
| 119 | + |
| 120 | + await p.findData({ object: 'invoice', query: { $top: 1, $count: 'false' } } as never); |
| 121 | + |
| 122 | + const bag = (find.mock.calls[0] as unknown[])[1] as Record<string, unknown>; |
| 123 | + expect('count' in bag).toBe(false); |
| 124 | + expect('$count' in bag).toBe(false); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('unchanged for every caller that does not opt out', () => { |
| 129 | + it('an ABSENT $count still counts and still reports total', async () => { |
| 130 | + const { p, count } = makeProtocol(1); |
| 131 | + |
| 132 | + const result = await p.findData({ object: 'invoice', query: { $top: 1 } } as never); |
| 133 | + |
| 134 | + expect(count).toHaveBeenCalledTimes(1); |
| 135 | + expect(result.total).toBe(3125); |
| 136 | + expect(result.hasMore).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | + it('an explicit $count=true still counts and still reports total', async () => { |
| 140 | + const { p, count } = makeProtocol(1); |
| 141 | + |
| 142 | + const result = await p.findData({ |
| 143 | + object: 'invoice', |
| 144 | + query: { $top: 1, $count: 'true' }, |
| 145 | + } as never); |
| 146 | + |
| 147 | + expect(count).toHaveBeenCalledTimes(1); |
| 148 | + expect(result.total).toBe(3125); |
| 149 | + }); |
| 150 | + |
| 151 | + it('$count=false without a limit is a no-op — the full set is already the total', async () => { |
| 152 | + // No `limit` ⇒ the whole result set came back, so `records.length` IS |
| 153 | + // the total and `engine.count()` was never called even before #10757. |
| 154 | + // Pinned so the opt-out cannot accidentally start suppressing a total |
| 155 | + // that costs nothing. |
| 156 | + const { p, count } = makeProtocol(4); |
| 157 | + |
| 158 | + const result = await p.findData({ |
| 159 | + object: 'invoice', |
| 160 | + query: { $count: 'false' }, |
| 161 | + } as never); |
| 162 | + |
| 163 | + expect(count).not.toHaveBeenCalled(); |
| 164 | + expect(result.total).toBe(4); |
| 165 | + expect(result.hasMore).toBe(false); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments