|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// `rollup/non-numeric-aggregand` — the roll-up door. |
| 4 | +// |
| 5 | +// `FieldSchema.summaryOperations` admits `min`/`max` over ANY child field, and |
| 6 | +// `aggregateSummaryValue` (objectql) returns the driver's answer verbatim with |
| 7 | +// only an empty-set fallback. A `summary` field is a member of the spec's |
| 8 | +// `NUMERIC_VALUE_TYPES`, so `valueSchemaFor` answers `z.number().finite()` for |
| 9 | +// it and `driver-sql`'s `createColumn` emits `table.float(name)`. An ordinary |
| 10 | +// "latest shipment" roll-up — `max` over a `datetime` child field — therefore |
| 11 | +// computes an INSTANT into a column the value contract says holds a finite |
| 12 | +// number, and nothing between author and driver correlated the two. |
| 13 | +// |
| 14 | +// The load-bearing test in this file is `discriminates from the analytics |
| 15 | +// table`. The refusal CANNOT be `isAggregateCompatibleWithFieldType`: that |
| 16 | +// table deliberately accepts `min`/`max` over the temporal class, because |
| 17 | +// there the answer is RETURNED to a caller rather than stored, and it "return[s] |
| 18 | +// a value of the field's OWN type (#15768)". Reusing it here would accept the |
| 19 | +// very declaration this rule exists to refuse, and the rule would be green |
| 20 | +// because it never fires. The two questions look alike and are not: |
| 21 | +// "can every backend give one answer" vs "does that answer fit the column this |
| 22 | +// roll-up is stored into". |
| 23 | +import { describe, expect, it } from 'vitest'; |
| 24 | +import { |
| 25 | + BOOLEAN_VALUE_TYPES, |
| 26 | + FieldType, |
| 27 | + NUMERIC_VALUE_TYPES, |
| 28 | + isAggregateCompatibleWithFieldType, |
| 29 | +} from '@objectstack/spec/data'; |
| 30 | + |
| 31 | +import { lintDataModel } from './data-model-rules.js'; |
| 32 | + |
| 33 | +const RULE = 'rollup/non-numeric-aggregand'; |
| 34 | + |
| 35 | +/** A parent rolling up `fn(child.<field>)`, and a child declaring that field as `childType`. */ |
| 36 | +const model = (childType: string | undefined, fn = 'max', overrides: Record<string, unknown> = {}) => [ |
| 37 | + { |
| 38 | + name: 'invoice', |
| 39 | + fields: { |
| 40 | + name: { type: 'text' }, |
| 41 | + rolled_up: { |
| 42 | + type: 'summary', |
| 43 | + summaryOperations: { object: 'invoice_line', field: 'shipped_at', function: fn, ...overrides }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: 'invoice_line', |
| 49 | + fields: { |
| 50 | + name: { type: 'text' }, |
| 51 | + invoice: { type: 'master_detail', reference: 'invoice', required: true, deleteBehavior: 'cascade' }, |
| 52 | + ...(childType ? { shipped_at: { type: childType } } : {}), |
| 53 | + }, |
| 54 | + }, |
| 55 | +]; |
| 56 | + |
| 57 | +const findings = (objects: unknown[]) => lintDataModel(objects as any[]).filter((i) => i.rule === RULE); |
| 58 | + |
| 59 | +describe('rollup/non-numeric-aggregand — refuses a min/max roll-up whose answer cannot fit the column', () => { |
| 60 | + it('refuses the card\'s own example: max over a datetime child field', () => { |
| 61 | + const found = findings(model('datetime')); |
| 62 | + expect(found).toHaveLength(1); |
| 63 | + const issue = found[0]; |
| 64 | + expect(issue.severity).toBe('error'); |
| 65 | + expect(issue.rule).toBe(RULE); |
| 66 | + // The message must let an author act without opening the source: WHICH |
| 67 | + // child object, WHICH field, its DECLARED type, and why the answer cannot |
| 68 | + // be stored. |
| 69 | + expect(issue.message).toContain('invoice_line'); |
| 70 | + expect(issue.message).toContain('shipped_at'); |
| 71 | + expect(issue.message).toContain('datetime'); |
| 72 | + expect(issue.message).toContain('finite number'); |
| 73 | + expect(issue.path).toBe('objects[0].fields.rolled_up.summaryOperations.field'); |
| 74 | + expect(issue.fix).toBeTruthy(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('refuses every member of the temporal class, under both min and max', () => { |
| 78 | + for (const childType of ['date', 'datetime', 'time']) { |
| 79 | + for (const fn of ['min', 'max']) { |
| 80 | + expect(findings(model(childType, fn)), `${fn}(${childType})`).toHaveLength(1); |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + it('refuses a text / option / reference child field too', () => { |
| 86 | + for (const childType of ['text', 'select', 'lookup', 'json', 'autonumber']) { |
| 87 | + expect(findings(model(childType)), childType).toHaveLength(1); |
| 88 | + } |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +describe('rollup/non-numeric-aggregand — accepts the classes whose answer IS a number', () => { |
| 93 | + it('accepts the numeric class', () => { |
| 94 | + for (const childType of NUMERIC_VALUE_TYPES) { |
| 95 | + expect(findings(model(childType)), childType).toEqual([]); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + // #11152 (maintainer ruling, 2026-08-28), pinned by the spec's own |
| 100 | + // `AGGREGATION_CASES`: `min(flag)=0` / `max(flag)=1` on six backends. A |
| 101 | + // careless predicate — "numeric only" — breaks exactly this leg. |
| 102 | + it('accepts the boolean class', () => { |
| 103 | + for (const childType of BOOLEAN_VALUE_TYPES) { |
| 104 | + expect(findings(model(childType)), childType).toEqual([]); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + it('accepts exactly the numeric ∪ boolean classes over every declared FieldType', () => { |
| 109 | + const accepted = new Set([...NUMERIC_VALUE_TYPES, ...BOOLEAN_VALUE_TYPES]); |
| 110 | + const refused = FieldType.options.filter((t) => findings(model(t)).length > 0); |
| 111 | + expect(refused.sort()).toEqual(FieldType.options.filter((t) => !accepted.has(t)).sort()); |
| 112 | + // A floor, so a future edit that stops the rule firing at all cannot make |
| 113 | + // the equality above vacuously true. |
| 114 | + expect(refused.length).toBeGreaterThan(10); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +describe('rollup/non-numeric-aggregand — discriminates from the analytics table', () => { |
| 119 | + // ⭐ The assertion that stops someone "simplifying" this rule back into |
| 120 | + // `isAggregateCompatibleWithFieldType`. If this ever fails because the table |
| 121 | + // started refusing the temporal class, that is a spec change to read, not a |
| 122 | + // test to update: the two predicates would then answer the same question. |
| 123 | + it('the analytics table ACCEPTS the temporal pair this rule refuses', () => { |
| 124 | + for (const childType of ['date', 'datetime', 'time']) { |
| 125 | + for (const fn of ['min', 'max']) { |
| 126 | + expect(isAggregateCompatibleWithFieldType(fn, childType), `${fn}(${childType})`).toBe(true); |
| 127 | + expect(findings(model(childType, fn)), `${fn}(${childType})`).toHaveLength(1); |
| 128 | + } |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + it('the two agree everywhere else on min/max — the difference is exactly the temporal class', () => { |
| 133 | + const TEMPORAL = new Set(['date', 'datetime', 'time']); |
| 134 | + for (const childType of FieldType.options) { |
| 135 | + const tableAccepts = isAggregateCompatibleWithFieldType('max', childType); |
| 136 | + const doorAccepts = findings(model(childType)).length === 0; |
| 137 | + if (TEMPORAL.has(childType)) continue; |
| 138 | + expect(doorAccepts, `max(${childType})`).toBe(tableAccepts); |
| 139 | + } |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +describe('rollup/non-numeric-aggregand — stays silent where it cannot resolve, and outside its scope', () => { |
| 144 | + // "A consumer that cannot resolve a field's type must NOT call the predicate |
| 145 | + // with a guess" — `aggregate-field-type-compatibility.ts`. A refusal fired on |
| 146 | + // a partially-loaded model would redden an app over metadata never seen. |
| 147 | + it('is silent when the child object is not in the pass\'s object set', () => { |
| 148 | + const objects = model('datetime').slice(0, 1); // parent only — no `invoice_line` |
| 149 | + expect(findings(objects)).toEqual([]); |
| 150 | + }); |
| 151 | + |
| 152 | + it('is silent when the named field is not declared on the child', () => { |
| 153 | + expect(findings(model(undefined))).toEqual([]); |
| 154 | + }); |
| 155 | + |
| 156 | + it('is silent when the child field declares no type', () => { |
| 157 | + const objects = model('datetime') as any[]; |
| 158 | + objects[1].fields.shipped_at = { label: 'Shipped At' }; |
| 159 | + expect(findings(objects)).toEqual([]); |
| 160 | + }); |
| 161 | + |
| 162 | + it('is silent when summaryOperations names no object or no field', () => { |
| 163 | + expect(findings(model('datetime', 'max', { object: undefined }))).toEqual([]); |
| 164 | + expect(findings(model('datetime', 'max', { field: undefined }))).toEqual([]); |
| 165 | + expect(findings(model('datetime', 'max', { object: '' }))).toEqual([]); |
| 166 | + }); |
| 167 | + |
| 168 | + it('is scoped to min/max — count reads no value off the field', () => { |
| 169 | + expect(findings(model('datetime', 'count'))).toEqual([]); |
| 170 | + }); |
| 171 | + |
| 172 | + // `sum` / `avg` over a non-numeric child is a different shape (the analytics |
| 173 | + // table already refuses those pairs) and is deliberately not this rule's. |
| 174 | + it('does not fire for sum or avg', () => { |
| 175 | + expect(findings(model('datetime', 'sum'))).toEqual([]); |
| 176 | + expect(findings(model('datetime', 'avg'))).toEqual([]); |
| 177 | + }); |
| 178 | + |
| 179 | + it('is silent for a field that is not a summary at all', () => { |
| 180 | + const objects = model('datetime') as any[]; |
| 181 | + objects[0].fields.rolled_up.type = 'number'; |
| 182 | + expect(findings(objects)).toEqual([]); |
| 183 | + }); |
| 184 | +}); |
0 commit comments