|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#27166] The QueryAST's own `fields` (and so every nested `expand` value) |
| 5 | + * and the flow `get_record` node's `fields` take the projection's array arm |
| 6 | + * the other published declarations take: at least one name, none blank. The |
| 7 | + * in-process engine refuses an empty projection at its `find` / `findOne` |
| 8 | + * entry, top level and at any `expand` depth (#27008), and the `get_record` |
| 9 | + * executor hands its authored `fields` to that entry as written, so a |
| 10 | + * declaration admitting `[]` admitted a shape the runtime refuses. |
| 11 | + * |
| 12 | + * Every refusal is pinned against the sentence `EngineQueryOptionsSchema.fields` |
| 13 | + * gives for the same value, so the declarations cannot drift apart. |
| 14 | + */ |
| 15 | + |
| 16 | +import { describe, it, expect } from 'vitest'; |
| 17 | +import { QuerySchema } from './query.zod'; |
| 18 | +import { EngineQueryOptionsSchema, QueryTransportParamsSchema, QueryWithTransportSchema } from './data-engine.zod'; |
| 19 | +import { GetRecordConfigSchema } from '../automation/builtin-node-config.zod'; |
| 20 | +import { flowNodeConfigRefusals } from '../automation/flow-node-config-refusals'; |
| 21 | +import { defineStack } from '../stack.zod'; |
| 22 | + |
| 23 | +type Path = ReadonlyArray<string | number>; |
| 24 | + |
| 25 | +const REFUSED: ReadonlyArray<readonly [string, string[], Path]> = [ |
| 26 | + ['an empty list', [], []], |
| 27 | + ['a list with a blank name', ['name', ''], [1]], |
| 28 | + ['a list with a whitespace name', ['name', ' '], [1]], |
| 29 | +]; |
| 30 | +const KEPT: ReadonlyArray<readonly [string, string[]]> = [ |
| 31 | + ['one name', ['name']], |
| 32 | + ['two names', ['name', 'email']], |
| 33 | +]; |
| 34 | + |
| 35 | +/** The first issue a schema raises for `input`, or a failed expectation when it parses. */ |
| 36 | +function firstIssue(schema: { safeParse(v: unknown): { success: boolean; error?: { issues: Array<{ message: string; path: PropertyKey[] }> } } }, input: unknown) { |
| 37 | + const parsed = schema.safeParse(input); |
| 38 | + expect(parsed.success, `refuses ${JSON.stringify(input)}`).toBe(false); |
| 39 | + return parsed.error!.issues[0]!; |
| 40 | +} |
| 41 | + |
| 42 | +/** The sentence the engine-side declaration gives for the same projection value. */ |
| 43 | +const engineMessage = (value: string[]) => firstIssue(EngineQueryOptionsSchema, { fields: value }).message; |
| 44 | + |
| 45 | +describe('QuerySchema.fields admits exactly what EngineQueryOptionsSchema.fields admits', () => { |
| 46 | + it.each(REFUSED)('refuses %s at the top level, in the engine declaration\'s words', (_name, value, tail) => { |
| 47 | + const issue = firstIssue(QuerySchema, { object: 'task', fields: value }); |
| 48 | + expect(issue.path).toEqual(['fields', ...tail]); |
| 49 | + expect(issue.message).toBe(engineMessage(value)); |
| 50 | + }); |
| 51 | + |
| 52 | + it.each(REFUSED)('refuses %s on an expand entry, naming the entry', (_name, value, tail) => { |
| 53 | + const issue = firstIssue(QuerySchema, { object: 'task', fields: ['title', 'project_id'], expand: { project_id: { object: 'project', fields: value } } }); |
| 54 | + expect(issue.path).toEqual(['expand', 'project_id', 'fields', ...tail]); |
| 55 | + expect(issue.message).toBe(engineMessage(value)); |
| 56 | + }); |
| 57 | + |
| 58 | + it.each(REFUSED)('refuses %s on a second-level expand entry', (_name, value, tail) => { |
| 59 | + const query = { |
| 60 | + object: 'task', |
| 61 | + expand: { project_id: { object: 'project', expand: { org_id: { object: 'org', fields: value } } } }, |
| 62 | + }; |
| 63 | + const issue = firstIssue(QuerySchema, query); |
| 64 | + expect(issue.path).toEqual(['expand', 'project_id', 'expand', 'org_id', 'fields', ...tail]); |
| 65 | + expect(issue.message).toBe(engineMessage(value)); |
| 66 | + }); |
| 67 | + |
| 68 | + it.each(REFUSED)('EngineQueryOptionsSchema.expand refuses %s on an entry, as the engine entry does', (_name, value, tail) => { |
| 69 | + const issue = firstIssue(EngineQueryOptionsSchema, { expand: { project_id: { object: 'project', fields: value } } }); |
| 70 | + expect(issue.path).toEqual(['expand', 'project_id', 'fields', ...tail]); |
| 71 | + expect(issue.message).toBe(engineMessage(value)); |
| 72 | + }); |
| 73 | + |
| 74 | + it.each(REFUSED)('the transport-aware query slot refuses canonical `fields` %s as it refuses `$select`', (_name, value, tail) => { |
| 75 | + const issue = firstIssue(QueryWithTransportSchema, { object: 'task', fields: value }); |
| 76 | + expect(issue.path).toEqual(['fields', ...tail]); |
| 77 | + expect(issue.message).toBe(firstIssue(QueryTransportParamsSchema, { $select: value }).message); |
| 78 | + }); |
| 79 | + |
| 80 | + it.each(KEPT)('keeps %s unchanged, at the top level and on an expand entry', (_name, value) => { |
| 81 | + const query = { object: 'task', fields: value, expand: { project_id: { object: 'project', fields: value } } }; |
| 82 | + const parsed = QuerySchema.parse(query); |
| 83 | + expect(parsed.fields).toEqual(value); |
| 84 | + expect(parsed.expand?.project_id?.fields).toEqual(value); |
| 85 | + }); |
| 86 | + |
| 87 | + it('keeps an absent projection: absent still reads every column', () => { |
| 88 | + const parsed = QuerySchema.parse({ object: 'task', expand: { project_id: { object: 'project' } } }); |
| 89 | + expect(parsed.fields).toBeUndefined(); |
| 90 | + expect(parsed.expand?.project_id?.fields).toBeUndefined(); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +describe('GetRecordConfigSchema.fields: an empty projection is refused at save, not at run', () => { |
| 95 | + const config = (fields: unknown) => ({ objectName: 'gr_task', filter: { title: 'alpha' }, fields }); |
| 96 | + |
| 97 | + it.each(REFUSED)('refuses %s, in the engine declaration\'s words', (_name, value, tail) => { |
| 98 | + const issue = firstIssue(GetRecordConfigSchema, config(value)); |
| 99 | + expect(issue.path).toEqual(['fields', ...tail]); |
| 100 | + expect(issue.message).toBe(engineMessage(value)); |
| 101 | + }); |
| 102 | + |
| 103 | + it.each(REFUSED)('the save door\'s node judge refuses %s at `fields`, naming the blank member', (_name, value, tail) => { |
| 104 | + const at = `fields${tail.map((i) => `[${i}]`).join('')}`; |
| 105 | + expect(flowNodeConfigRefusals('get_record', config(value)).map(({ code, params, path }) => ({ code, params, path }))).toEqual([ |
| 106 | + { code: 'node-config-refused-by-contract', params: { nodeType: 'get_record', key: at }, path: at }, |
| 107 | + ]); |
| 108 | + }); |
| 109 | + |
| 110 | + it('defineStack refuses a get_record node with `fields: []` at its config path', () => { |
| 111 | + const stack = { |
| 112 | + manifest: { id: 'com.example.projection', name: 'projection', version: '1.0.0', type: 'app', namespace: 'gr' }, |
| 113 | + objects: [{ name: 'gr_task', label: 'Task', fields: { title: { type: 'text', label: 'Title' } } }], |
| 114 | + flows: [{ |
| 115 | + name: 'gr_read', |
| 116 | + label: 'Read', |
| 117 | + type: 'autolaunched', |
| 118 | + nodes: [ |
| 119 | + { id: 'start', type: 'start', label: 'Start' }, |
| 120 | + { id: 'read', type: 'get_record', label: 'Read', config: config([]) }, |
| 121 | + { id: 'end', type: 'end', label: 'End' }, |
| 122 | + ], |
| 123 | + edges: [ |
| 124 | + { id: 'e1', source: 'start', target: 'read' }, |
| 125 | + { id: 'e2', source: 'read', target: 'end' }, |
| 126 | + ], |
| 127 | + }], |
| 128 | + }; |
| 129 | + let refusal: { code?: unknown; status?: unknown; issues?: Array<{ path: unknown[] }> } | undefined; |
| 130 | + try { |
| 131 | + defineStack(stack as never); |
| 132 | + } catch (e) { |
| 133 | + refusal = e as typeof refusal; |
| 134 | + } |
| 135 | + expect(refusal, 'defineStack must refuse the flow').toBeDefined(); |
| 136 | + expect({ code: refusal!.code, status: refusal!.status }).toEqual({ code: 'STACK_SCHEMA_INVALID', status: 422 }); |
| 137 | + expect(refusal!.issues!.map((i) => i.path.join('.'))).toEqual(['flows.0.nodes.1.config.fields']); |
| 138 | + }); |
| 139 | + |
| 140 | + it.each(KEPT)('keeps %s unchanged, and the node judge finds nothing', (_name, value) => { |
| 141 | + expect(GetRecordConfigSchema.parse(config(value)).fields).toEqual(value); |
| 142 | + expect(flowNodeConfigRefusals('get_record', config(value))).toEqual([]); |
| 143 | + }); |
| 144 | + |
| 145 | + it('keeps an absent projection: the node reads every field', () => { |
| 146 | + expect(GetRecordConfigSchema.parse({ objectName: 'gr_task' }).fields).toBeUndefined(); |
| 147 | + expect(flowNodeConfigRefusals('get_record', { objectName: 'gr_task' })).toEqual([]); |
| 148 | + }); |
| 149 | +}); |
0 commit comments