|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +// [#38433] A list view `grouping.fields[].field` naming a field the engine |
| 4 | +// does not group by. The grouping compiles to the group header query |
| 5 | +// (`compileListViewGroupQuery`), whose `groupBy` `engine.aggregate` refuses |
| 6 | +// with `400 INVALID_FIELD` over a multi-value or structured-JSON field |
| 7 | +// (`assertGroupByNamesNoJsonStoredField`) and over a `formula` field |
| 8 | +// (`assertGroupByTargetsAreStored`). The rule asks the same spec predicates, |
| 9 | +// so each class is pinned here from the predicate the engine door reads. |
| 10 | + |
| 11 | +import { describe, expect, it } from 'vitest'; |
| 12 | + |
| 13 | +import { STRUCTURED_JSON_TYPES, isMultiValueField, isVirtualSearchField } from '@objectstack/spec/data'; |
| 14 | +import { ListViewSchema } from '@objectstack/spec/ui'; |
| 15 | + |
| 16 | +import { runAuthoringRules, splitBySeverity } from './authoring-rules.js'; |
| 17 | +import { explainRule } from './rule-explanations.js'; |
| 18 | +import { runRuntimeAuthoringRules } from './runtime-gate.js'; |
| 19 | +import { |
| 20 | + LIST_VIEW_GROUP_FIELD_UNGROUPABLE, |
| 21 | + validateListViewFieldRefs, |
| 22 | +} from './validate-list-view-field-refs.js'; |
| 23 | + |
| 24 | +type AnyRec = Record<string, unknown>; |
| 25 | + |
| 26 | +const OBJECTS = [ |
| 27 | + { |
| 28 | + name: 'grp_task', |
| 29 | + label: 'Task', |
| 30 | + fields: [ |
| 31 | + { name: 'title', type: 'text', label: 'Title' }, |
| 32 | + { name: 'status', type: 'select', label: 'Status', options: [{ value: 'open', label: 'Open' }] }, |
| 33 | + { name: 'labels', type: 'multiselect', label: 'Labels', options: [{ value: 'a', label: 'A' }] }, |
| 34 | + { name: 'owners', type: 'lookup', reference: 'grp_account', multiple: true, label: 'Owners' }, |
| 35 | + { name: 'account', type: 'lookup', reference: 'grp_account', label: 'Account' }, |
| 36 | + { name: 'meta', type: 'json', label: 'Meta' }, |
| 37 | + { name: 'score', type: 'formula', label: 'Score' }, |
| 38 | + ], |
| 39 | + }, |
| 40 | + { |
| 41 | + name: 'grp_account', |
| 42 | + label: 'Account', |
| 43 | + fields: [ |
| 44 | + { name: 'name', type: 'text', label: 'Name' }, |
| 45 | + { name: 'tags', type: 'tags', label: 'Tags' }, |
| 46 | + ], |
| 47 | + }, |
| 48 | +]; |
| 49 | + |
| 50 | +const groupedBy = (field: string): AnyRec => ({ |
| 51 | + name: 'all', |
| 52 | + type: 'grid', |
| 53 | + data: { provider: 'object', object: 'grp_task' }, |
| 54 | + columns: ['title'], |
| 55 | + grouping: { fields: [{ field }] }, |
| 56 | +}); |
| 57 | + |
| 58 | +const stackWith = (listView: AnyRec): AnyRec => ({ |
| 59 | + name: 'probe', |
| 60 | + objects: OBJECTS, |
| 61 | + views: [{ name: 'grp_task', object: 'grp_task', list: listView }], |
| 62 | +}); |
| 63 | + |
| 64 | +const ungroupable = (field: string) => |
| 65 | + validateListViewFieldRefs(stackWith(groupedBy(field))).filter((f) => f.rule === LIST_VIEW_GROUP_FIELD_UNGROUPABLE); |
| 66 | + |
| 67 | +const declared = (name: string) => |
| 68 | + OBJECTS.flatMap((o) => o.fields as AnyRec[]).find((f) => f.name === name) as { type: string; multiple?: boolean }; |
| 69 | + |
| 70 | +describe('[#38433] a list view grouped by a field the engine does not group by', () => { |
| 71 | + it.each([ |
| 72 | + ['a multi-value field', 'labels', () => isMultiValueField(declared('labels') as never)], |
| 73 | + ['a multi-value reference (`multiple: true` lookup)', 'owners', () => isMultiValueField(declared('owners') as never)], |
| 74 | + ['a structured-JSON field', 'meta', () => STRUCTURED_JSON_TYPES.has(declared('meta').type)], |
| 75 | + ['a formula field', 'score', () => isVirtualSearchField(declared('score') as never)], |
| 76 | + ])('refuses %s, as the engine door\'s own predicate does', (_label, field, enginePredicate) => { |
| 77 | + expect(enginePredicate(), 'the engine door refuses this declaration').toBe(true); |
| 78 | + const findings = ungroupable(field); |
| 79 | + expect(findings.map((f) => [f.rule, f.severity, f.where, f.path])).toEqual([ |
| 80 | + [LIST_VIEW_GROUP_FIELD_UNGROUPABLE, 'error', 'view "grp_task" › list › grouping', 'views[0].list.grouping.fields[0].field'], |
| 81 | + ]); |
| 82 | + expect(findings[0].message).toContain('400 INVALID_FIELD'); |
| 83 | + // The fix holds: a grid column, or a grouping the engine accepts — never a |
| 84 | + // grid grouping on this field. |
| 85 | + expect(findings[0].hint).toContain(`Show "${field}" as a grid column`); |
| 86 | + expect(findings[0].hint).not.toContain('grid grouping'); |
| 87 | + for (const line of [findings[0].message, findings[0].hint]) { |
| 88 | + expect(line).not.toContain('\n'); |
| 89 | + expect(line.length, line).toBeLessThanOrEqual(200); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + it.each([ |
| 94 | + ['a single-valued select', 'status'], |
| 95 | + ['a text field', 'title'], |
| 96 | + ['a single-valued lookup', 'account'], |
| 97 | + ])('CONTROL — %s, stored and single-valued, is quiet', (_label, field) => { |
| 98 | + expect(ungroupable(field)).toEqual([]); |
| 99 | + expect(validateListViewFieldRefs(stackWith(groupedBy(field)))).toEqual([]); |
| 100 | + }); |
| 101 | + |
| 102 | + it('a dotted name is not this verdict: the engine refuses every dotted groupBy name on its own ground, multi-valued leaf or not', () => { |
| 103 | + // `account.tags` ends on a `tags` field, `account.name` on a text one; the |
| 104 | + // engine's dotted-name branch refuses both alike, so the storage class of |
| 105 | + // the leaf is not what the engine judges there. |
| 106 | + expect(ungroupable('account.tags')).toEqual([]); |
| 107 | + expect(ungroupable('account.name')).toEqual([]); |
| 108 | + }); |
| 109 | + |
| 110 | + // [M1 of the contract review] Only a grid sends its grouping to |
| 111 | + // `engine.aggregate` (objectui's `ListView.tsx` asks the group header query |
| 112 | + // under `currentView === 'grid'` alone). A gallery groups the cards it |
| 113 | + // fetched; a kanban board keeps drawing its cards when its swimlane count |
| 114 | + // read is refused; calendar, timeline, gantt and map do not read grouping. |
| 115 | + it('an absent `type` is a grid, the schema\'s default, and is judged', () => { |
| 116 | + const parsed = ListViewSchema.safeParse({ columns: ['title'] }); |
| 117 | + expect(parsed.success && (parsed.data as { type?: unknown }).type).toBe('grid'); |
| 118 | + const { type: _type, ...untyped } = groupedBy('labels'); |
| 119 | + const findings = validateListViewFieldRefs(stackWith(untyped)).filter((f) => f.rule === LIST_VIEW_GROUP_FIELD_UNGROUPABLE); |
| 120 | + expect(findings.map((f) => [f.severity, f.path])).toEqual([['error', 'views[0].list.grouping.fields[0].field']]); |
| 121 | + }); |
| 122 | + |
| 123 | + it.each(['kanban', 'gallery', 'calendar', 'timeline', 'gantt', 'map'])( |
| 124 | + 'CONTROL — a `%s` list view is not judged: its grouping does not reach the engine', |
| 125 | + (type) => { |
| 126 | + for (const field of ['labels', 'owners', 'meta', 'score']) { |
| 127 | + const findings = validateListViewFieldRefs(stackWith({ ...groupedBy(field), type })) |
| 128 | + .filter((f) => f.rule === LIST_VIEW_GROUP_FIELD_UNGROUPABLE); |
| 129 | + expect(findings, `${type} grouped by ${field}`).toEqual([]); |
| 130 | + } |
| 131 | + }, |
| 132 | + ); |
| 133 | + |
| 134 | + it('a name that resolves to nothing stays `list-view-field-unknown`', () => { |
| 135 | + expect(ungroupable('nope')).toEqual([]); |
| 136 | + }); |
| 137 | + |
| 138 | + it('judges every level of a multi-level grouping', () => { |
| 139 | + const listView = { ...groupedBy('status'), grouping: { fields: [{ field: 'status' }, { field: 'score' }] } }; |
| 140 | + const findings = validateListViewFieldRefs(stackWith(listView)).filter((f) => f.rule === LIST_VIEW_GROUP_FIELD_UNGROUPABLE); |
| 141 | + expect(findings.map((f) => f.path)).toEqual(['views[0].list.grouping.fields[1].field']); |
| 142 | + }); |
| 143 | + |
| 144 | + for (const command of ['validate', 'build'] as const) { |
| 145 | + it(`fails \`${command}\``, () => { |
| 146 | + const { errors } = splitBySeverity(runAuthoringRules(command, { normalized: stackWith(groupedBy('labels')) })); |
| 147 | + expect(errors.map((f) => f.rule)).toContain(LIST_VIEW_GROUP_FIELD_UNGROUPABLE); |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + it('is refused at the runtime `view` save door, and admitted once the grouping names a stored single-valued field', () => { |
| 152 | + const item = (field: string) => ({ name: 'grp_task', object: 'grp_task', list: groupedBy(field) }); |
| 153 | + const refused = runRuntimeAuthoringRules({ type: 'view', item: item('meta'), context: { objects: OBJECTS } }); |
| 154 | + expect(refused.errors.map((f) => [f.rule, f.severity])).toEqual([[LIST_VIEW_GROUP_FIELD_UNGROUPABLE, 'error']]); |
| 155 | + expect(refused.errors[0].path).toMatch(/grouping\.fields\[0\]\.field$/); |
| 156 | + const clean = runRuntimeAuthoringRules({ type: 'view', item: item('status'), context: { objects: OBJECTS } }); |
| 157 | + expect(clean.errors).toEqual([]); |
| 158 | + }); |
| 159 | + |
| 160 | + it('`os explain` carries the reasoning and the fix', () => { |
| 161 | + const text = explainRule(LIST_VIEW_GROUP_FIELD_UNGROUPABLE)!.paragraphs.join('\n'); |
| 162 | + for (const fact of ['400 INVALID_FIELD', 'grid column', 'stored single-valued field', '`list-view-field-unknown`']) { |
| 163 | + expect(text, fact).toContain(fact); |
| 164 | + } |
| 165 | + }); |
| 166 | +}); |
0 commit comments