|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#27008] The in-process engine refuses an empty projection, as every |
| 5 | + * published declaration of a read's projection does. |
| 6 | + * |
| 7 | + * #26817, #26909 and #26954 settled one rule: an empty projection is refused, |
| 8 | + * never read as every column. The HTTP doors refuse `select: []` |
| 9 | + * (`projectionFieldsValue`, `400 VALIDATION_ERROR`) and |
| 10 | + * `EngineQueryOptionsSchema.fields` declares the same refusal, but |
| 11 | + * `ObjectQL.find` / `findOne` never parse their options with that schema. Their |
| 12 | + * projection guards ran only when `ast.fields.length > 0`, so `fields: []` |
| 13 | + * reached the driver, and every driver serves an empty projection as none: |
| 14 | + * owner, organization and audit columns came back, and the caller was not told. |
| 15 | + * |
| 16 | + * Driven against a REAL {@link ObjectQL} engine over a driver double that |
| 17 | + * projects exactly (an empty list answers every column, as `SqlDriver` and |
| 18 | + * `InMemoryDriver` do) and counts its reads, so "refused before any driver |
| 19 | + * call" is measured rather than assumed. |
| 20 | + */ |
| 21 | + |
| 22 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 23 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 24 | +import { EngineQueryOptionsSchema } from '@objectstack/spec/data'; |
| 25 | +import { mapDataError } from '@objectstack/types'; |
| 26 | +import { ObjectQL } from './engine.js'; |
| 27 | + |
| 28 | +const projectObject = { |
| 29 | + name: 'proj_27008', |
| 30 | + label: 'Project', |
| 31 | + fields: { |
| 32 | + id: { name: 'id', label: 'ID', type: 'text' as const, primaryKey: true }, |
| 33 | + name: { name: 'name', label: 'Name', type: 'text' as const }, |
| 34 | + }, |
| 35 | +}; |
| 36 | + |
| 37 | +const taskObject = { |
| 38 | + name: 'task_27008', |
| 39 | + label: 'Task', |
| 40 | + fields: { |
| 41 | + id: { name: 'id', label: 'ID', type: 'text' as const, primaryKey: true }, |
| 42 | + title: { name: 'title', label: 'Title', type: 'text' as const }, |
| 43 | + project_id: { name: 'project_id', label: 'Project', type: 'lookup' as const, reference: 'proj_27008' }, |
| 44 | + }, |
| 45 | +}; |
| 46 | + |
| 47 | +/** The declarations' own refusal words, read off the published schema. */ |
| 48 | +const declaredRefusal = (): string => { |
| 49 | + const parsed = EngineQueryOptionsSchema.shape.fields.safeParse([]); |
| 50 | + expect(parsed.success).toBe(false); |
| 51 | + return parsed.error!.issues[0].message; |
| 52 | +}; |
| 53 | + |
| 54 | +function makeDriver() { |
| 55 | + const rows: Record<string, Record<string, unknown>[]> = { |
| 56 | + proj_27008: [{ id: 'p1', name: 'Apollo', owner_id: 'usr_1', organization_id: 'org_1' }], |
| 57 | + task_27008: [{ |
| 58 | + id: 't1', title: 'Ship', project_id: 'p1', |
| 59 | + owner_id: 'usr_1', organization_id: 'org_1', created_at: '2026-10-28T00:00:00.000Z', |
| 60 | + }], |
| 61 | + }; |
| 62 | + const reads: Array<{ object: string; fields: unknown }> = []; |
| 63 | + const project = (row: Record<string, unknown>, fields: unknown) => |
| 64 | + Array.isArray(fields) && fields.length > 0 |
| 65 | + ? Object.fromEntries(fields.map((f: string) => [f, row[f]])) |
| 66 | + : { ...row }; |
| 67 | + const matches = (row: Record<string, unknown>, where: any): boolean => { |
| 68 | + if (!where || typeof where !== 'object') return true; |
| 69 | + for (const [k, v] of Object.entries(where)) { |
| 70 | + if (k === '$and' && Array.isArray(v)) { |
| 71 | + if (!v.every((arm) => matches(row, arm))) return false; |
| 72 | + continue; |
| 73 | + } |
| 74 | + if (k.startsWith('$')) throw new Error(`driver double does not implement the '${k}' combinator`); |
| 75 | + if (v && typeof v === 'object' && '$in' in (v as any)) { |
| 76 | + if (!(v as any).$in.map(String).includes(String(row[k]))) return false; |
| 77 | + continue; |
| 78 | + } |
| 79 | + const expected = v && typeof v === 'object' && '$eq' in (v as any) ? (v as any).$eq : v; |
| 80 | + if (row[k] !== expected) return false; |
| 81 | + } |
| 82 | + return true; |
| 83 | + }; |
| 84 | + const driver: any = { |
| 85 | + name: 'memory', |
| 86 | + version: '0.0.0', |
| 87 | + supports: {}, |
| 88 | + async connect() {}, async disconnect() {}, async checkHealth() { return true; }, async execute() { return null; }, |
| 89 | + async find(object: string, ast: any) { |
| 90 | + reads.push({ object, fields: ast?.fields }); |
| 91 | + const matched = (rows[object] ?? []).filter((r) => matches(r, ast?.where)); |
| 92 | + const page = typeof ast?.limit === 'number' ? matched.slice(0, ast.limit) : matched; |
| 93 | + return page.map((r) => project(r, ast?.fields)); |
| 94 | + }, |
| 95 | + async findOne(object: string, ast: any) { |
| 96 | + reads.push({ object, fields: ast?.fields }); |
| 97 | + const hit = (rows[object] ?? []).find((r) => matches(r, ast?.where)); |
| 98 | + return hit ? project(hit, ast?.fields) : null; |
| 99 | + }, |
| 100 | + async create(_o: string, d: any) { return d; }, |
| 101 | + async update(_o: string, _id: string, d: any) { return d; }, |
| 102 | + async delete() { return true; }, |
| 103 | + async count(object: string) { return (rows[object] ?? []).length; }, |
| 104 | + async bulkCreate(_o: string, r: any[]) { return r; }, |
| 105 | + async bulkUpdate() { return []; }, async bulkDelete() {}, |
| 106 | + async beginTransaction() { return { commit: async () => {}, rollback: async () => {} }; }, |
| 107 | + async commit() {}, async rollback() {}, |
| 108 | + }; |
| 109 | + return { driver, reads }; |
| 110 | +} |
| 111 | + |
| 112 | +/** The refusal as the REST door would answer it: `mapDataError`'s status and code. */ |
| 113 | +async function wireAnswer(read: Promise<unknown>) { |
| 114 | + const err = await read.then( |
| 115 | + () => { throw new Error('expected a refusal, got an answer'); }, |
| 116 | + (e: unknown) => e, |
| 117 | + ); |
| 118 | + const mapped = mapDataError(err); |
| 119 | + return { status: mapped.status, code: mapped.body.code, message: (err as Error).message }; |
| 120 | +} |
| 121 | + |
| 122 | +describe('[#27008] the in-process engine refuses an empty projection', () => { |
| 123 | + let engine: ObjectQL; |
| 124 | + let reads: Array<{ object: string; fields: unknown }>; |
| 125 | + |
| 126 | + beforeEach(async () => { |
| 127 | + engine = new ObjectQL(); |
| 128 | + const made = makeDriver(); |
| 129 | + reads = made.reads; |
| 130 | + engine.registerDriver(made.driver, true); |
| 131 | + await engine.init(); |
| 132 | + engine.registry.registerObject(projectObject, 'test-package'); |
| 133 | + engine.registry.registerObject(taskObject, 'test-package'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('find with fields: [] is refused 400 VALIDATION_ERROR in the declarations\' words, before any driver read', async () => { |
| 137 | + const answer = await wireAnswer(engine.find('task_27008', { fields: [] })); |
| 138 | + expect(answer).toMatchObject({ status: 400, code: 'VALIDATION_ERROR' }); |
| 139 | + expect(answer.message).toContain(declaredRefusal()); |
| 140 | + expect(answer.message).toMatch(/^ObjectQL\.find\('task_27008'\) names no field in 'fields'/); |
| 141 | + expect(reads).toEqual([]); |
| 142 | + }); |
| 143 | + |
| 144 | + it('findOne with fields: [] is refused the same way, before any driver read', async () => { |
| 145 | + const answer = await wireAnswer(engine.findOne('task_27008', { where: { id: 't1' }, fields: [] })); |
| 146 | + expect(answer).toMatchObject({ status: 400, code: 'VALIDATION_ERROR' }); |
| 147 | + expect(answer.message).toContain(declaredRefusal()); |
| 148 | + expect(answer.message).toMatch(/^ObjectQL\.findOne\('task_27008'\)/); |
| 149 | + expect(reads).toEqual([]); |
| 150 | + }); |
| 151 | + |
| 152 | + it('a system-context read is refused too: the rule is about the shape, not the caller', async () => { |
| 153 | + const answer = await wireAnswer(engine.find('task_27008', { fields: [], context: { isSystem: true } })); |
| 154 | + expect(answer).toMatchObject({ status: 400, code: 'VALIDATION_ERROR' }); |
| 155 | + expect(reads).toEqual([]); |
| 156 | + }); |
| 157 | + |
| 158 | + it('an expand sub-read with fields: [] is refused at the entry, not swallowed into a bare foreign key', async () => { |
| 159 | + // The expansion's own `catch` keeps the foreign key on a sub-read failure, |
| 160 | + // so a refusal raised only inside the sub-read would degrade silently. The |
| 161 | + // entry judges the nested projection before the parent row is read. |
| 162 | + const answer = await wireAnswer(engine.find('task_27008', { |
| 163 | + expand: { project_id: { object: 'proj_27008', fields: [] } }, |
| 164 | + })); |
| 165 | + expect(answer).toMatchObject({ status: 400, code: 'VALIDATION_ERROR' }); |
| 166 | + expect(answer.message).toContain(declaredRefusal()); |
| 167 | + expect(answer.message).toContain("expand['project_id']"); |
| 168 | + expect(reads).toEqual([]); |
| 169 | + }); |
| 170 | + |
| 171 | + it('a deeper expand level is judged too', async () => { |
| 172 | + const answer = await wireAnswer(engine.findOne('task_27008', { |
| 173 | + where: { id: 't1' }, |
| 174 | + expand: { project_id: { object: 'proj_27008', fields: ['name'], expand: { owner: { object: 'sys_user', fields: [] } } } }, |
| 175 | + })); |
| 176 | + expect(answer).toMatchObject({ status: 400, code: 'VALIDATION_ERROR' }); |
| 177 | + expect(answer.message).toContain("expand['project_id'].expand['owner']"); |
| 178 | + expect(reads).toEqual([]); |
| 179 | + }); |
| 180 | + |
| 181 | + it('the REST door answers the same status and code for the same projection', async () => { |
| 182 | + const protocol = new ObjectStackProtocolImplementation(engine); |
| 183 | + const door = await wireAnswer(protocol.findData({ object: 'task_27008', query: { select: [] } })); |
| 184 | + const inProcess = await wireAnswer(engine.find('task_27008', { fields: [] })); |
| 185 | + expect({ status: inProcess.status, code: inProcess.code }).toEqual({ status: door.status, code: door.code }); |
| 186 | + expect(door).toMatchObject({ status: 400, code: 'VALIDATION_ERROR' }); |
| 187 | + expect(door.message).toContain(declaredRefusal()); |
| 188 | + expect(reads).toEqual([]); |
| 189 | + }); |
| 190 | + |
| 191 | + // ─── Controls ──────────────────────────────────────────────────── |
| 192 | + |
| 193 | + it('CONTROL: an absent fields still reads every column', async () => { |
| 194 | + const [row] = await engine.find('task_27008', {}); |
| 195 | + expect(row).toMatchObject({ id: 't1', title: 'Ship', owner_id: 'usr_1', organization_id: 'org_1' }); |
| 196 | + expect(reads).toHaveLength(1); |
| 197 | + }); |
| 198 | + |
| 199 | + it('CONTROL: fields: null is a withdrawal and reads every column', async () => { |
| 200 | + const [row] = await engine.find('task_27008', { fields: null as unknown as string[] }); |
| 201 | + expect(row).toMatchObject({ id: 't1', title: 'Ship', owner_id: 'usr_1' }); |
| 202 | + }); |
| 203 | + |
| 204 | + it('CONTROL: a named projection projects', async () => { |
| 205 | + const [row] = await engine.find('task_27008', { fields: ['title'] }); |
| 206 | + expect(row).toEqual({ title: 'Ship' }); |
| 207 | + const one = await engine.findOne('task_27008', { where: { id: 't1' }, fields: ['title'] }); |
| 208 | + expect(one).toEqual({ title: 'Ship' }); |
| 209 | + }); |
| 210 | + |
| 211 | + it('CONTROL: an expand with a named nested projection still expands', async () => { |
| 212 | + const [row] = await engine.find('task_27008', { |
| 213 | + fields: ['title', 'project_id'], |
| 214 | + expand: { project_id: { object: 'proj_27008', fields: ['name'] } }, |
| 215 | + }); |
| 216 | + expect(row).toEqual({ title: 'Ship', project_id: { name: 'Apollo' } }); |
| 217 | + }); |
| 218 | +}); |
0 commit comments