|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #22588 item 2 — a list's `total` is the filtered count whatever the page size, |
| 5 | + * `$top=0` included. |
| 6 | + * |
| 7 | + * ## What was wrong |
| 8 | + * |
| 9 | + * `findData` decided "is this a paged request?" with `limit > 0`. `limit: 0` |
| 10 | + * asks for NO rows (objectstack#6485 — every driver's pagination conformance |
| 11 | + * pins it), so the gate sent a zero-row request into the "no limit, the whole |
| 12 | + * result set came back, so its length IS the total" arm — the one arm that is |
| 13 | + * false for exactly this limit. Measured over HTTP on a booted stack at |
| 14 | + * `3d0eeefa` (five rows, two of them in stage `b`): |
| 15 | + * |
| 16 | + * GET /data/mz_task?top=0 total 0 ← the defect |
| 17 | + * GET /data/mz_task?top=1 total 5 |
| 18 | + * GET /data/mz_task?top=0&stage=b total 0 ← the defect |
| 19 | + * GET /data/mz_task?top=1&stage=b total 2 |
| 20 | + * GET /data/mz_task?top=0&$count=false total 0 ← the opt-out ignored too |
| 21 | + * |
| 22 | + * The Console's list footer sends exactly `$top: 0` to read the total and |
| 23 | + * nothing else, so every list page showed a record count of `0`. |
| 24 | + * |
| 25 | + * ## What this suite pins |
| 26 | + * |
| 27 | + * 1. `top=0` and `top=1` report the SAME `total`, and `top=0` still returns no |
| 28 | + * rows — the engine is asked for `limit: 0`, never for the whole set. |
| 29 | + * 2. CONTROL — a filter moves both alike. A fix that reported the UNFILTERED |
| 30 | + * count for a zero-row page would pass (1) and fail here. |
| 31 | + * 3. The two neighbouring arms keep their meaning for a zero-row page: |
| 32 | + * `$count=false` still issues no COUNT and omits `total` (it no longer |
| 33 | + * reports the page's `0` as if it were a count), and `search` still issues |
| 34 | + * no COUNT — `engine.count()` cannot reproduce a search, which is why that |
| 35 | + * arm reports a page-local estimate at every page size. |
| 36 | + * 4. Absent a limit nothing moves: no COUNT, `total` is the set's length. |
| 37 | + */ |
| 38 | + |
| 39 | +import { describe, it, expect, vi } from 'vitest'; |
| 40 | +import { ObjectStackProtocolImplementation } from './protocol.js'; |
| 41 | + |
| 42 | +const SCHEMA = { |
| 43 | + name: 'mz_task', |
| 44 | + nameField: 'name', |
| 45 | + fields: { |
| 46 | + name: { name: 'name', type: 'text' }, |
| 47 | + stage: { name: 'stage', type: 'text' }, |
| 48 | + }, |
| 49 | +}; |
| 50 | + |
| 51 | +const ROWS = [ |
| 52 | + { id: 't0', name: 't0', stage: 'a' }, |
| 53 | + { id: 't1', name: 't1', stage: 'a' }, |
| 54 | + { id: 't2', name: 't2', stage: 'a' }, |
| 55 | + { id: 't3', name: 't3', stage: 'b' }, |
| 56 | + { id: 't4', name: 't4', stage: 'b' }, |
| 57 | +]; |
| 58 | + |
| 59 | +/** |
| 60 | + * Equality-only `where` — the implicit field filter `?stage=b` lowers to. |
| 61 | + * Anything else (a combinator, an operator object) is REFUSED rather than |
| 62 | + * answered silently wrong. |
| 63 | + */ |
| 64 | +function matches(row: Record<string, unknown>, where: unknown): boolean { |
| 65 | + if (where == null) return true; |
| 66 | + if (typeof where !== 'object' || Array.isArray(where)) { |
| 67 | + throw new Error(`test double: unexpected where shape ${JSON.stringify(where)}`); |
| 68 | + } |
| 69 | + return Object.entries(where as Record<string, unknown>).every(([k, v]) => { |
| 70 | + if (k.startsWith('$')) throw new Error(`test double: combinator '${k}' is not implemented`); |
| 71 | + if (v !== null && typeof v === 'object') throw new Error(`test double: operator on '${k}' is not implemented`); |
| 72 | + return row[k] === v; |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * An engine double that honours `limit` AS WRITTEN — `limit: 0` is no rows, |
| 78 | + * the drivers' declared contract — and counts over the same `where`. |
| 79 | + */ |
| 80 | +function makeProtocol() { |
| 81 | + const find = vi.fn(async (_object: string, opts: any = {}) => { |
| 82 | + const hits = ROWS.filter((r) => matches(r, opts.where)); |
| 83 | + const offset = typeof opts.offset === 'number' ? opts.offset : 0; |
| 84 | + const sliced = hits.slice(offset); |
| 85 | + return opts.limit !== undefined ? sliced.slice(0, opts.limit) : sliced; |
| 86 | + }); |
| 87 | + const count = vi.fn(async (_object: string, opts: any = {}) => ROWS.filter((r) => matches(r, opts.where)).length); |
| 88 | + const engine = { |
| 89 | + registry: { getObject: (n: string) => (n === SCHEMA.name ? SCHEMA : undefined) }, |
| 90 | + find, |
| 91 | + count, |
| 92 | + aggregate: vi.fn(async () => [] as unknown[]), |
| 93 | + }; |
| 94 | + return { p: new ObjectStackProtocolImplementation(engine as any), find, count }; |
| 95 | +} |
| 96 | + |
| 97 | +async function list(query: Record<string, unknown>) { |
| 98 | + const h = makeProtocol(); |
| 99 | + const result: any = await h.p.findData({ object: SCHEMA.name, query } as never); |
| 100 | + return { result, ...h }; |
| 101 | +} |
| 102 | + |
| 103 | +describe('[#22588] a zero-row page reports the filtered total', () => { |
| 104 | + it('top=0 and top=1 report the same total, and top=0 returns no rows', async () => { |
| 105 | + const zero = await list({ top: '0' }); |
| 106 | + const one = await list({ top: '1' }); |
| 107 | + |
| 108 | + expect(zero.result.total).toBe(5); |
| 109 | + expect(zero.result.total).toBe(one.result.total); |
| 110 | + expect(zero.result.records).toEqual([]); |
| 111 | + // The engine was asked for NO rows — the fix counts, it never widens |
| 112 | + // the read to the whole set to measure it. |
| 113 | + expect((zero.find.mock.calls[0] as unknown[])[1]).toMatchObject({ limit: 0 }); |
| 114 | + expect(zero.result.hasMore).toBe(true); |
| 115 | + }); |
| 116 | + |
| 117 | + it('every spelling of the zero window reaches the same answer', async () => { |
| 118 | + for (const query of [{ $top: '0' }, { limit: '0' }, { top: 0 }]) { |
| 119 | + const { result, count } = await list(query); |
| 120 | + expect(result.total, JSON.stringify(query)).toBe(5); |
| 121 | + expect(count, JSON.stringify(query)).toHaveBeenCalledTimes(1); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + it('CONTROL — a filter changes both alike', async () => { |
| 126 | + const zero = await list({ top: '0', stage: 'b' }); |
| 127 | + const one = await list({ top: '1', stage: 'b' }); |
| 128 | + |
| 129 | + expect(zero.result.total).toBe(2); |
| 130 | + expect(one.result.total).toBe(2); |
| 131 | + expect(zero.result.records).toEqual([]); |
| 132 | + expect(one.result.records).toHaveLength(1); |
| 133 | + }); |
| 134 | + |
| 135 | + it('a zero-row page past an offset still reports the whole filtered total', async () => { |
| 136 | + const { result } = await list({ top: '0', skip: '2' }); |
| 137 | + |
| 138 | + expect(result.total).toBe(5); |
| 139 | + expect(result.records).toEqual([]); |
| 140 | + expect(result.hasMore).toBe(true); |
| 141 | + }); |
| 142 | +}); |
| 143 | + |
| 144 | +describe('[#22588] the neighbouring arms keep their meaning for a zero-row page', () => { |
| 145 | + it('$count=false still issues no COUNT — and omits total instead of reporting the page\'s 0', async () => { |
| 146 | + const { result, count } = await list({ $top: '0', $count: 'false' }); |
| 147 | + |
| 148 | + expect(count).not.toHaveBeenCalled(); |
| 149 | + expect('total' in result).toBe(false); |
| 150 | + }); |
| 151 | + |
| 152 | + it('search still issues no COUNT — engine.count() cannot reproduce a search', async () => { |
| 153 | + const { result, count } = await list({ $top: '0', $search: 't' }); |
| 154 | + |
| 155 | + expect(count).not.toHaveBeenCalled(); |
| 156 | + expect(result.records).toEqual([]); |
| 157 | + }); |
| 158 | + |
| 159 | + it('absent a limit nothing moves: no COUNT, total is the set\'s length', async () => { |
| 160 | + const { result, count } = await list({ stage: 'b' }); |
| 161 | + |
| 162 | + expect(count).not.toHaveBeenCalled(); |
| 163 | + expect(result.total).toBe(2); |
| 164 | + expect(result.hasMore).toBe(false); |
| 165 | + }); |
| 166 | +}); |
0 commit comments