|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #26644 — the list normalizer read two malformed values as the absent default. |
| 5 | + * |
| 6 | + * - **Projection.** A string `fields` (or its folded `select` / `$select` |
| 7 | + * spellings) was `split(',').map(trim).filter(Boolean)`, so a blank or |
| 8 | + * comma-only value became `[]` — and `assertProjectionFieldsExist` has |
| 9 | + * nothing to refuse in an empty list, so an empty projection was served as |
| 10 | + * NO projection: every column. An explicit `fields: []` took the same road. |
| 11 | + * Absent means "every column"; an empty projection asks for none, so reading |
| 12 | + * it as absent is the silent widening `pageWindowValue` already refuses on |
| 13 | + * the row axis (a blank `$top`). |
| 14 | + * - **Count.** The boolean step lowered only `'true'` / `'false'`; any other |
| 15 | + * value stayed as it was, `countOptOut = options.count === false` was false, |
| 16 | + * and the count ran — `?$count=maybe` reported a `total` like `?$count=true`. |
| 17 | + * |
| 18 | + * Both are now refused `400 VALIDATION_ERROR` naming the parameter the caller |
| 19 | + * wrote and the value, before the engine is asked. The accepted `count` values |
| 20 | + * are the spec's (`QueryTransportParamsSchema.count`): the boolean, or the two |
| 21 | + * strings a querystring carries it as. |
| 22 | + * |
| 23 | + * `getData` (`GET /data/:object/:id`) reads `select` against the same field |
| 24 | + * map, and its own comment promised "gated exactly as on the list path", so its |
| 25 | + * projection takes the same refusal: one rule for both routes. |
| 26 | + * |
| 27 | + * ## Two assertion classes |
| 28 | + * |
| 29 | + * 1. **REFUSAL** — `code`, `status` and `param` on the throw, and the engine |
| 30 | + * never reached. The unfixed normalizer never threw for these, so a bare |
| 31 | + * `toThrow()` would be half a pin. |
| 32 | + * 2. **PRESERVATION** — a well-formed projection reaches `engine.find` as the |
| 33 | + * same list it always did, and `count` absent / `true` / `false` (either |
| 34 | + * spelling, `null` as absent) counts or skips exactly as before. |
| 35 | + * |
| 36 | + * The door-level half (both REST list routes and the by-id route over a real |
| 37 | + * `SqlDriver`) is `packages/rest/src/data-empty-projection-count-door.test.ts`; |
| 38 | + * the runtime dispatcher's is |
| 39 | + * `packages/runtime/src/domains/data-empty-projection-count.test.ts`. |
| 40 | + */ |
| 41 | + |
| 42 | +import { describe, it, expect, vi } from 'vitest'; |
| 43 | +import { ObjectStackProtocolImplementation } from './protocol.js'; |
| 44 | + |
| 45 | +const SCHEMA = { |
| 46 | + name: 'invoice', |
| 47 | + nameField: 'name', |
| 48 | + fields: { |
| 49 | + name: { name: 'name', type: 'text' }, |
| 50 | + status: { name: 'status', type: 'text' }, |
| 51 | + }, |
| 52 | +}; |
| 53 | + |
| 54 | +function makeProtocol() { |
| 55 | + const find = vi.fn(async () => [{ id: 'r1', name: 'a', status: 'open' }] as unknown[]); |
| 56 | + const findOne = vi.fn(async () => ({ id: 'r1', name: 'a', status: 'open' })); |
| 57 | + const count = vi.fn(async () => 7); |
| 58 | + const engine = { |
| 59 | + registry: { getObject: (n: string) => (n === 'invoice' ? SCHEMA : undefined) }, |
| 60 | + find, |
| 61 | + findOne, |
| 62 | + count, |
| 63 | + aggregate: vi.fn(async () => [] as unknown[]), |
| 64 | + }; |
| 65 | + return { p: new ObjectStackProtocolImplementation(engine as any), find, findOne, count }; |
| 66 | +} |
| 67 | + |
| 68 | +type Refusal = { message: string; status?: number; code?: string; param?: string }; |
| 69 | + |
| 70 | +async function refusal(run: (p: any) => Promise<unknown>, reached: (m: ReturnType<typeof makeProtocol>) => unknown[]): Promise<Refusal> { |
| 71 | + const m = makeProtocol(); |
| 72 | + let answered: unknown; |
| 73 | + try { |
| 74 | + answered = await run(m.p); |
| 75 | + } catch (e) { |
| 76 | + const err = e as Error & { status?: number; code?: string; param?: string }; |
| 77 | + for (const spy of reached(m)) expect(spy, 'the engine was reached before the refusal').not.toHaveBeenCalled(); |
| 78 | + return { message: err.message, status: err.status, code: err.code, param: err.param }; |
| 79 | + } |
| 80 | + throw new Error(`ACCEPTED (answered ${JSON.stringify(answered)}) instead of refused`); |
| 81 | +} |
| 82 | + |
| 83 | +const listRefusal = (query: Record<string, unknown>) => |
| 84 | + refusal((p) => p.findData({ object: 'invoice', query }), (m) => [m.find, m.count]); |
| 85 | +const getRefusal = (select: unknown) => |
| 86 | + refusal((p) => p.getData({ object: 'invoice', id: 'r1', select }), (m) => [m.findOne]); |
| 87 | + |
| 88 | +// --------------------------------------------------------------------------- |
| 89 | +// 1. REFUSAL |
| 90 | +// --------------------------------------------------------------------------- |
| 91 | + |
| 92 | +describe('#26644 — an empty projection is refused, naming the spelling the caller wrote', () => { |
| 93 | + it.each<[string, Record<string, unknown>]>([ |
| 94 | + ['$select', { $select: '' }], |
| 95 | + ['$select', { $select: ' ' }], |
| 96 | + ['$select', { $select: ',' }], |
| 97 | + ['$select', { $select: ' , ,' }], |
| 98 | + ['select', { select: '' }], |
| 99 | + ['fields', { fields: '' }], |
| 100 | + ['fields', { fields: [] }], |
| 101 | + ['select', { select: [] }], |
| 102 | + ])('list: %s in %j answers 400 VALIDATION_ERROR (was: every column)', async (param, query) => { |
| 103 | + const err = await listRefusal(query); |
| 104 | + |
| 105 | + expect(err.code).toBe('VALIDATION_ERROR'); |
| 106 | + expect(err.status).toBe(400); |
| 107 | + expect(err.param).toBe(param); |
| 108 | + expect(err.message).toContain(`Query parameter '${param}' names no field`); |
| 109 | + }); |
| 110 | + |
| 111 | + it.each<[unknown]>([[''], [' '], [','], [[]]])('by id: select %j answers 400 VALIDATION_ERROR (was: every column)', async (select) => { |
| 112 | + const err = await getRefusal(select); |
| 113 | + |
| 114 | + expect(err.code).toBe('VALIDATION_ERROR'); |
| 115 | + expect(err.status).toBe(400); |
| 116 | + expect(err.param).toBe('select'); |
| 117 | + expect(err.message).toContain("Query parameter 'select' names no field"); |
| 118 | + }); |
| 119 | + |
| 120 | + it('quotes the value it refused', async () => { |
| 121 | + const err = await listRefusal({ $select: ' , ' }); |
| 122 | + |
| 123 | + expect(err.message).toContain('got " , "'); |
| 124 | + }); |
| 125 | +}); |
| 126 | + |
| 127 | +describe('#26644 — a count flag that is not true or false is refused', () => { |
| 128 | + it.each<[string, Record<string, unknown>]>([ |
| 129 | + ['$count', { $count: 'maybe' }], |
| 130 | + ['$count', { $count: '' }], |
| 131 | + ['$count', { $count: '1' }], |
| 132 | + ['$count', { $count: 'TRUE' }], |
| 133 | + ['count', { count: 'yes' }], |
| 134 | + ['count', { count: 0 }], |
| 135 | + ['count', { count: 1 }], |
| 136 | + ])('%s in %j answers 400 VALIDATION_ERROR (was: counted)', async (param, query) => { |
| 137 | + const err = await listRefusal(query); |
| 138 | + |
| 139 | + expect(err.code).toBe('VALIDATION_ERROR'); |
| 140 | + expect(err.status).toBe(400); |
| 141 | + expect(err.param).toBe(param); |
| 142 | + expect(err.message).toContain(`Query parameter '${param}' must be true or false`); |
| 143 | + }); |
| 144 | + |
| 145 | + it('quotes the value it refused, bounded', async () => { |
| 146 | + const err = await listRefusal({ $count: 'x'.repeat(500) }); |
| 147 | + |
| 148 | + expect(err.message).toContain('got "xxxx'); |
| 149 | + expect(err.message.length).toBeLessThan(400); |
| 150 | + }); |
| 151 | +}); |
| 152 | + |
| 153 | +// --------------------------------------------------------------------------- |
| 154 | +// 2. PRESERVATION |
| 155 | +// --------------------------------------------------------------------------- |
| 156 | + |
| 157 | +describe('#26644 — a well-formed projection and count flag answer exactly as before', () => { |
| 158 | + it.each<[Record<string, unknown>, unknown]>([ |
| 159 | + [{ $select: 'name,status' }, ['name', 'status']], |
| 160 | + [{ $select: ' name , status ,' }, ['name', 'status']], |
| 161 | + [{ select: 'name' }, ['name']], |
| 162 | + [{ fields: ['name'] }, ['name']], |
| 163 | + [{}, undefined], |
| 164 | + [{ fields: null }, undefined], |
| 165 | + ])('list: %j → fields %j', async (query, fields) => { |
| 166 | + const { p, find } = makeProtocol(); |
| 167 | + await p.findData({ object: 'invoice', query } as never); |
| 168 | + |
| 169 | + expect(find).toHaveBeenCalledTimes(1); |
| 170 | + expect(((find.mock.calls[0] as unknown[])[1] as Record<string, unknown>).fields).toEqual(fields); |
| 171 | + }); |
| 172 | + |
| 173 | + it.each<[unknown, unknown]>([ |
| 174 | + ['name,status', ['name', 'status']], |
| 175 | + [['name'], ['name']], |
| 176 | + [undefined, undefined], |
| 177 | + [null, undefined], |
| 178 | + ])('by id: select %j → fields %j', async (select, fields) => { |
| 179 | + const { p, findOne } = makeProtocol(); |
| 180 | + await p.getData({ object: 'invoice', id: 'r1', select } as never); |
| 181 | + |
| 182 | + expect(findOne).toHaveBeenCalledTimes(1); |
| 183 | + expect(((findOne.mock.calls[0] as unknown[])[1] as Record<string, unknown>).fields).toEqual(fields); |
| 184 | + }); |
| 185 | + |
| 186 | + it.each<[Record<string, unknown>, boolean]>([ |
| 187 | + [{}, true], |
| 188 | + [{ $count: 'true' }, true], |
| 189 | + [{ count: true }, true], |
| 190 | + [{ count: null }, true], |
| 191 | + [{ $count: 'false' }, false], |
| 192 | + [{ count: false }, false], |
| 193 | + ])('%j with a window → counted: %s', async (query, counted) => { |
| 194 | + const { p, count } = makeProtocol(); |
| 195 | + // The count runs only for a paged request: unpaged, the page IS the total. |
| 196 | + const res: any = await p.findData({ object: 'invoice', query: { $top: '1', ...query } } as never); |
| 197 | + |
| 198 | + expect(count).toHaveBeenCalledTimes(counted ? 1 : 0); |
| 199 | + expect('total' in res).toBe(counted); |
| 200 | + }); |
| 201 | +}); |
0 commit comments