|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25229] The Data API's page window — `$top` / `top` / `limit` and `$skip` / |
| 5 | + * `skip` / `offset` — is a non-negative integer on BOTH list doors |
| 6 | + * (`GET /data/:object`'s querystring, `POST /data/:object/query`'s body), or a |
| 7 | + * `400 INVALID_REQUEST` naming the parameter the caller wrote, before any read; |
| 8 | + * over a real `SqlDriver` (SQLite), five rows. |
| 9 | + * |
| 10 | + * Measured on the base through these two routes (the stock showcase answered |
| 11 | + * the same, `showcase_account`, admin bearer): |
| 12 | + * |
| 13 | + * MEASURED_TABLE |
| 14 | + * |
| 15 | + * The refusal lives once, in the shared normalizer both doors hand their |
| 16 | + * request to (`metadata-protocol`'s `findData`); the per-spelling pin with a |
| 17 | + * recording engine is that package's `protocol.page-window.test.ts`. This file |
| 18 | + * is the door-level half: the status and `code` each door answers, the |
| 19 | + * spelling it names, that the driver was never asked, and the controls paging |
| 20 | + * exactly as before. |
| 21 | + */ |
| 22 | + |
| 23 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 24 | +import { ObjectQL } from '@objectstack/objectql'; |
| 25 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 26 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 27 | +import { RestServer } from './rest-server'; |
| 28 | +import { installDefaultOrganization } from './default-organization.testkit.js'; |
| 29 | + |
| 30 | +const OBJECT = 'rest_page_window_25229'; |
| 31 | + |
| 32 | +const ITEMS = { |
| 33 | + name: OBJECT, |
| 34 | + label: 'Items 25229', |
| 35 | + fields: { |
| 36 | + label: { name: 'label', type: 'text' as const }, |
| 37 | + }, |
| 38 | +}; |
| 39 | + |
| 40 | +const LABELS = ['a', 'b', 'c', 'd', 'e']; |
| 41 | + |
| 42 | +/** name · the querystring `GET /data/:object` receives · the spelling the refusal names. */ |
| 43 | +const REFUSED_GET: ReadonlyArray<readonly [string, Record<string, string>, string]> = [ |
| 44 | + ['$skip=-5 (the card)', { $skip: '-5' }, '$skip'], |
| 45 | + ['$skip=-1&$top=3 (the card)', { $skip: '-1', $top: '3' }, '$skip'], |
| 46 | + ['$skip=abc', { $skip: 'abc' }, '$skip'], |
| 47 | + ['$top=abc (the card)', { $top: 'abc' }, '$top'], |
| 48 | + ['$top=-1 (the card)', { $top: '-1' }, '$top'], |
| 49 | + ['$top= blank (the card)', { $top: '' }, '$top'], |
| 50 | + ['$top=1.5', { $top: '1.5' }, '$top'], |
| 51 | + ['top=abc', { top: 'abc' }, 'top'], |
| 52 | + ['skip=-1', { skip: '-1' }, 'skip'], |
| 53 | + ['limit=-1', { limit: '-1' }, 'limit'], |
| 54 | + ['offset=-1', { offset: '-1' }, 'offset'], |
| 55 | +]; |
| 56 | + |
| 57 | +/** name · the JSON body `POST /data/:object/query` receives · the spelling the refusal names. */ |
| 58 | +const REFUSED_POST: ReadonlyArray<readonly [string, Record<string, unknown>, string]> = [ |
| 59 | + ['offset: -5 (the card)', { offset: -5 }, 'offset'], |
| 60 | + ['limit: -1 (the card)', { limit: -1 }, 'limit'], |
| 61 | + ['offset: 1.5', { offset: 1.5 }, 'offset'], |
| 62 | + ['limit: 1.5', { limit: 1.5 }, 'limit'], |
| 63 | +]; |
| 64 | + |
| 65 | +function createMockServer() { |
| 66 | + const noop = () => {}; |
| 67 | + return { get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, listen: async () => {}, close: async () => {} }; |
| 68 | +} |
| 69 | + |
| 70 | +function makeRes() { |
| 71 | + const res: any = { |
| 72 | + write: () => true, end: () => {}, |
| 73 | + header: () => res, |
| 74 | + status: (code: number) => { res._status = code; return res; }, |
| 75 | + json: (body: any) => { res._json = body; return res; }, |
| 76 | + }; |
| 77 | + return res; |
| 78 | +} |
| 79 | + |
| 80 | +describe('[#25229] the page window on both list doors — sqlite', () => { |
| 81 | + let engine: ObjectQL; |
| 82 | + let driver: any; |
| 83 | + const reads = { n: 0 }; |
| 84 | + let list: (query: Record<string, string>) => Promise<{ status: number; body: any }>; |
| 85 | + let query: (body: Record<string, unknown>) => Promise<{ status: number; body: any }>; |
| 86 | + |
| 87 | + beforeAll(async () => { |
| 88 | + driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true } as any); |
| 89 | + engine = new ObjectQL(); |
| 90 | + engine.registerDriver(driver, true); |
| 91 | + await engine.init(); |
| 92 | + engine.registry.registerObject(ITEMS as any); |
| 93 | + await engine.syncSchemas(); |
| 94 | + // A real `single` install's Default Organization: the system writes below |
| 95 | + // derive it (ADR-0131 D9), as a fresh NOT NULL tenant column requires (D1). |
| 96 | + await installDefaultOrganization(engine); |
| 97 | + for (const label of LABELS) await engine.insert(OBJECT, { id: `r${label}`, label } as any); |
| 98 | + |
| 99 | + // Reads of THIS object — the protocol's own metadata traffic is not the question. |
| 100 | + for (const verb of ['find', 'findOne', 'count', 'aggregate'] as const) { |
| 101 | + const real = driver[verb].bind(driver); |
| 102 | + driver[verb] = (o: string, ...rest: unknown[]) => { if (o === OBJECT) reads.n += 1; return real(o, ...rest); }; |
| 103 | + } |
| 104 | + |
| 105 | + const protocol = new ObjectStackProtocolImplementation(engine as any); |
| 106 | + const rest = new RestServer(createMockServer() as any, protocol as any, { api: { requireAuth: false } } as any); |
| 107 | + (rest as any).resolveExecCtx = async () => ({ userId: 'test-user' }); |
| 108 | + rest.registerRoutes(); |
| 109 | + const getRoute = rest.getRoutes().find((r: any) => r.method === 'GET' && r.path === '/api/v1/data/:object'); |
| 110 | + const postRoute = rest.getRoutes().find((r: any) => r.method === 'POST' && r.path === '/api/v1/data/:object/query'); |
| 111 | + expect(getRoute).toBeDefined(); |
| 112 | + expect(postRoute).toBeDefined(); |
| 113 | + list = async (q) => { |
| 114 | + const res = makeRes(); |
| 115 | + // What a querystring carries: strings. |
| 116 | + await getRoute!.handler({ params: { object: OBJECT }, query: { $orderby: 'label', ...q }, headers: {} } as any, res); |
| 117 | + return { status: res._status ?? 200, body: res._json }; |
| 118 | + }; |
| 119 | + query = async (body) => { |
| 120 | + const res = makeRes(); |
| 121 | + // What the wire carries: JSON. |
| 122 | + await postRoute!.handler({ params: { object: OBJECT }, body: JSON.parse(JSON.stringify({ orderBy: [{ field: 'label', order: 'asc' }], ...body })), query: {}, headers: {} } as any, res); |
| 123 | + return { status: res._status ?? 200, body: res._json }; |
| 124 | + }; |
| 125 | + }); |
| 126 | + |
| 127 | + afterAll(async () => { |
| 128 | + try { await engine?.destroy(); } catch { /* noop */ } |
| 129 | + }); |
| 130 | + |
| 131 | + const labelsOf = (body: any): string[] => (body?.records ?? []).map((r: any) => r.label); |
| 132 | + |
| 133 | + it('GET: 400 INVALID_REQUEST naming the querystring spelling — no read', async () => { |
| 134 | + const before = reads.n; |
| 135 | + for (const [name, q, param] of REFUSED_GET) { |
| 136 | + const res = await list(q); |
| 137 | + expect(res.status, `${name}: ${JSON.stringify(res.body)}`).toBe(400); |
| 138 | + expect(res.body.code, name).toBe('INVALID_REQUEST'); |
| 139 | + expect(res.body.error, name).toContain(`Query parameter '${param}' must be a non-negative integer`); |
| 140 | + } |
| 141 | + expect(reads.n - before, 'no read of the object — every refusal precedes the driver').toBe(0); |
| 142 | + }); |
| 143 | + |
| 144 | + it('POST: 400 INVALID_REQUEST naming the body key — no read', async () => { |
| 145 | + const before = reads.n; |
| 146 | + for (const [name, body, param] of REFUSED_POST) { |
| 147 | + const res = await query(body); |
| 148 | + expect(res.status, `${name}: ${JSON.stringify(res.body)}`).toBe(400); |
| 149 | + expect(res.body.code, name).toBe('INVALID_REQUEST'); |
| 150 | + expect(res.body.error, name).toContain(`Query parameter '${param}' must be a non-negative integer`); |
| 151 | + } |
| 152 | + expect(reads.n - before, 'no read of the object — every refusal precedes the driver').toBe(0); |
| 153 | + }); |
| 154 | + |
| 155 | + it('POST: a non-number window is still the schema door\'s 400 VALIDATION_FAILED, unchanged', async () => { |
| 156 | + const res = await query({ limit: 'ten' }); |
| 157 | + expect(res.status, JSON.stringify(res.body)).toBe(400); |
| 158 | + expect(res.body.code).toBe('VALIDATION_FAILED'); |
| 159 | + }); |
| 160 | + |
| 161 | + it('controls: a well-formed window pages exactly as before on both doors', async () => { |
| 162 | + const get = await list({ $top: '2', $skip: '1' }); |
| 163 | + expect(get.status, JSON.stringify(get.body)).toBe(200); |
| 164 | + expect(labelsOf(get.body)).toEqual(['b', 'c']); |
| 165 | + |
| 166 | + const post = await query({ limit: 2, offset: 1 }); |
| 167 | + expect(post.status, JSON.stringify(post.body)).toBe(200); |
| 168 | + expect(labelsOf(post.body)).toEqual(['b', 'c']); |
| 169 | + |
| 170 | + const bare = await list({ top: '2', skip: '3' }); |
| 171 | + expect(labelsOf(bare.body)).toEqual(['d', 'e']); |
| 172 | + |
| 173 | + const noWindow = await list({}); |
| 174 | + expect(labelsOf(noWindow.body)).toEqual(LABELS); |
| 175 | + |
| 176 | + // `0` is a count, not a malformed window: no rows, and still a 200. |
| 177 | + const zero = await query({ limit: 0 }); |
| 178 | + expect(zero.status, JSON.stringify(zero.body)).toBe(200); |
| 179 | + expect(labelsOf(zero.body)).toEqual([]); |
| 180 | + const zeroSkip = await list({ $skip: '0', $top: '1' }); |
| 181 | + expect(labelsOf(zeroSkip.body)).toEqual(['a']); |
| 182 | + }); |
| 183 | +}); |
0 commit comments