|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #35143: the second batch of publish-time checks, Data model family, the |
| 5 | + * two field-shape rules of `lintFieldReferenceShapes`. Each has a pin that |
| 6 | + * fires, a pin that stays silent, and the runtime publish gate's own verdict on |
| 7 | + * the write it judges. The family's other lines are pinned in the harness of |
| 8 | + * the rule they extend: the summary relation arm in |
| 9 | + * `data-model-rules.summary-rollup.test.ts`, the approval detail table in |
| 10 | + * `validate-approval-approvers.test.ts`, and the formula-over-a-bare-summary |
| 11 | + * line (covered by `field/summary-without-operations`) in |
| 12 | + * `validate-functional-completeness.test.ts`. |
| 13 | + */ |
| 14 | + |
| 15 | +import { describe, expect, it } from 'vitest'; |
| 16 | +import { runAuthoringRules } from './authoring-rules.js'; |
| 17 | +import { runRuntimeAuthoringRules } from './runtime-gate.js'; |
| 18 | +import { lintFieldReferenceShapes } from './data-model-rules.js'; |
| 19 | + |
| 20 | +type AnyRec = Record<string, any>; |
| 21 | + |
| 22 | +const REFERENCE_ON_NON_REFERENCE_TYPE = 'field/reference-on-non-reference-type'; |
| 23 | +const COUNT_NAMED_LOOKUP = 'field/count-named-lookup'; |
| 24 | + |
| 25 | +const rulesOf = (findings: readonly { rule: string }[]) => findings.map((f) => f.rule); |
| 26 | + |
| 27 | +describe('field/reference-on-non-reference-type', () => { |
| 28 | + const object = (fields: AnyRec): AnyRec => ({ name: 'deal', label: 'Deal', fields: { name: { type: 'text', label: 'Name' }, ...fields } }); |
| 29 | + |
| 30 | + it('refuses reference on a text, number or date field, whose inline editor becomes the record picker', () => { |
| 31 | + for (const def of [ |
| 32 | + { type: 'text', label: 'Account', reference: 'account' }, |
| 33 | + { type: 'number', label: 'Account', reference: 'account' }, |
| 34 | + { type: 'date', label: 'Account', reference: 'account' }, |
| 35 | + ]) { |
| 36 | + const hit = lintFieldReferenceShapes([object({ account: def })]).filter((f) => f.rule === REFERENCE_ON_NON_REFERENCE_TYPE); |
| 37 | + expect(hit, def.type).toHaveLength(1); |
| 38 | + expect(hit[0]).toMatchObject({ severity: 'error', path: 'objects[0].fields.account.reference' }); |
| 39 | + expect(hit[0].message).toContain('record picker'); |
| 40 | + expect(hit[0].fix).toContain("{ type: 'lookup', reference: 'account' }"); |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + it('refuses it on a select too, saying only that it links nothing: the inline editor draws the select first', () => { |
| 45 | + const hit = lintFieldReferenceShapes([ |
| 46 | + object({ account: { type: 'select', label: 'Account', reference: 'account', options: [{ value: 'a', label: 'A' }] } }), |
| 47 | + ]).filter((f) => f.rule === REFERENCE_ON_NON_REFERENCE_TYPE); |
| 48 | + expect(hit).toHaveLength(1); |
| 49 | + expect(hit[0]).toMatchObject({ severity: 'error', path: 'objects[0].fields.account.reference' }); |
| 50 | + expect(hit[0].message).not.toContain('record picker'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('on a formula it says only that the key links nothing: the record page gives a formula no editor', () => { |
| 54 | + const hit = lintFieldReferenceShapes([ |
| 55 | + object({ account_name: { type: 'formula', label: 'Account', expression: 'record.name', reference: 'account' } }), |
| 56 | + ]).filter((f) => f.rule === REFERENCE_ON_NON_REFERENCE_TYPE); |
| 57 | + expect(hit).toHaveLength(1); |
| 58 | + expect(hit[0].message).not.toContain('record picker'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('is silent on the four reference types, on a summary, and on a blank reference', () => { |
| 62 | + const findings = lintFieldReferenceShapes([ |
| 63 | + object({ |
| 64 | + account: { type: 'lookup', label: 'Account', reference: 'account' }, |
| 65 | + parent_deal: { type: 'master_detail', label: 'Parent', reference: 'deal' }, |
| 66 | + rep: { type: 'user', label: 'Rep', reference: 'sys_user' }, |
| 67 | + parent: { type: 'tree', label: 'Parent', reference: 'deal' }, |
| 68 | + lines: { type: 'summary', label: 'Lines', reference: 'deal_line', summaryOperations: { object: 'deal_line', function: 'count' } }, |
| 69 | + note: { type: 'text', label: 'Note', reference: '' }, |
| 70 | + }), |
| 71 | + ]); |
| 72 | + expect(rulesOf(findings)).not.toContain(REFERENCE_ON_NON_REFERENCE_TYPE); |
| 73 | + }); |
| 74 | + |
| 75 | + it('judges the fields an object extension adds, located in the extension', () => { |
| 76 | + const cli = runAuthoringRules('validate', { |
| 77 | + normalized: { objects: [object({})], objectExtensions: [{ extend: 'deal', fields: { region_code: { type: 'text', label: 'Region', reference: 'region' } } }] }, |
| 78 | + parsed: { objects: [object({})], objectExtensions: [{ extend: 'deal', fields: { region_code: { type: 'text', label: 'Region', reference: 'region' } } }] }, |
| 79 | + } as never); |
| 80 | + const hit = cli.filter((f) => f.rule === REFERENCE_ON_NON_REFERENCE_TYPE); |
| 81 | + expect(hit).toHaveLength(1); |
| 82 | + expect(hit[0]).toMatchObject({ severity: 'error', path: 'objectExtensions[0].fields.region_code.reference' }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('the runtime publish gate refuses the object write and the object_extension write', () => { |
| 86 | + const objectVerdict = runRuntimeAuthoringRules({ |
| 87 | + type: 'object', |
| 88 | + item: object({ account: { type: 'text', label: 'Account', reference: 'account' } }), |
| 89 | + context: { objects: [] }, |
| 90 | + }); |
| 91 | + expect(rulesOf(objectVerdict.errors)).toContain(REFERENCE_ON_NON_REFERENCE_TYPE); |
| 92 | + const extensionVerdict = runRuntimeAuthoringRules({ |
| 93 | + type: 'object_extension', |
| 94 | + item: { extend: 'deal', fields: { account: { type: 'text', label: 'Account', reference: 'account' } } }, |
| 95 | + context: { objects: [object({})] }, |
| 96 | + }); |
| 97 | + expect(rulesOf(extensionVerdict.errors)).toContain(REFERENCE_ON_NON_REFERENCE_TYPE); |
| 98 | + const clean = runRuntimeAuthoringRules({ |
| 99 | + type: 'object', |
| 100 | + item: object({ account: { type: 'lookup', label: 'Account', reference: 'account' } }), |
| 101 | + context: { objects: [] }, |
| 102 | + }); |
| 103 | + expect(rulesOf(clean.errors)).not.toContain(REFERENCE_ON_NON_REFERENCE_TYPE); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +describe('field/count-named-lookup', () => { |
| 108 | + const object = (fields: AnyRec): AnyRec => ({ name: 'account', label: 'Account', fields: { name: { type: 'text', label: 'Name' }, ...fields } }); |
| 109 | + |
| 110 | + it('warns on a lookup or master_detail named like a count or a total', () => { |
| 111 | + for (const name of ['contact_count', 'total_amount', 'count', 'deal_totals']) { |
| 112 | + const hit = lintFieldReferenceShapes([object({ [name]: { type: 'lookup', label: 'X', reference: 'contact' } })]).filter( |
| 113 | + (f) => f.rule === COUNT_NAMED_LOOKUP, |
| 114 | + ); |
| 115 | + expect(hit, name).toHaveLength(1); |
| 116 | + expect(hit[0]).toMatchObject({ severity: 'warning', path: `objects[0].fields.${name}.type` }); |
| 117 | + expect(hit[0].fix).toContain("type: 'number'"); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + it('is silent on names that only contain the letters (account, discount, country) and on a number named so', () => { |
| 122 | + const findings = lintFieldReferenceShapes([ |
| 123 | + object({ |
| 124 | + account: { type: 'lookup', label: 'Account', reference: 'account' }, |
| 125 | + discount: { type: 'lookup', label: 'Discount', reference: 'discount' }, |
| 126 | + country: { type: 'lookup', label: 'Country', reference: 'country' }, |
| 127 | + contact_count: { type: 'number', label: 'Contacts' }, |
| 128 | + }), |
| 129 | + ]); |
| 130 | + expect(rulesOf(findings)).not.toContain(COUNT_NAMED_LOOKUP); |
| 131 | + }); |
| 132 | + |
| 133 | + it('rides the runtime publish gate as an advisory, never a refusal', () => { |
| 134 | + const verdict = runRuntimeAuthoringRules({ |
| 135 | + type: 'object', |
| 136 | + item: object({ contact_count: { type: 'lookup', label: 'Contacts', reference: 'contact' } }), |
| 137 | + context: { objects: [] }, |
| 138 | + }); |
| 139 | + expect(rulesOf(verdict.errors)).not.toContain(COUNT_NAMED_LOOKUP); |
| 140 | + expect(rulesOf(verdict.advisories)).toContain(COUNT_NAMED_LOOKUP); |
| 141 | + }); |
| 142 | +}); |
| 143 | + |
0 commit comments