|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { |
| 5 | + validateReferenceIntegrity, |
| 6 | + REFERENCE_INTEGRITY_RULES, |
| 7 | +} from './reference-integrity-suite.js'; |
| 8 | +import { validateObjectReferences } from './validate-object-references.js'; |
| 9 | +import { validateTranslationReferences } from './validate-translation-references.js'; |
| 10 | + |
| 11 | +describe('reference-integrity suite — membership', () => { |
| 12 | + // Deliberately a written-out list: adding a rule to the suite should be a |
| 13 | + // conscious edit in two places (the suite and this test), not something that |
| 14 | + // slips in unreviewed. The list is also the answer to "which rules run on |
| 15 | + // `validate` / `lint` / `compile`?". |
| 16 | + it('holds exactly the reference-resolution rules, in report order', () => { |
| 17 | + expect(REFERENCE_INTEGRITY_RULES.map((r) => r.name)).toEqual([ |
| 18 | + 'validateObjectReferences', |
| 19 | + 'validateActionNameRefs', |
| 20 | + 'validatePageFieldBindings', |
| 21 | + 'validateChartBindings', |
| 22 | + 'validateNavAccess', |
| 23 | + 'validateTranslationReferences', |
| 24 | + ]); |
| 25 | + }); |
| 26 | + |
| 27 | + it('wires the same function the package exports', () => { |
| 28 | + const byName = new Map(REFERENCE_INTEGRITY_RULES.map((r) => [r.name, r.run])); |
| 29 | + expect(byName.get('validateObjectReferences')).toBe(validateObjectReferences); |
| 30 | + expect(byName.get('validateTranslationReferences')).toBe(validateTranslationReferences); |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +describe('reference-integrity suite — every member actually runs', () => { |
| 35 | + /** |
| 36 | + * One live instance per rule, so a member that silently stops being invoked |
| 37 | + * (or is dropped from the list) fails here instead of quietly narrowing what |
| 38 | + * the CLI checks. A suite that returns nothing is indistinguishable from a |
| 39 | + * clean stack — which is exactly how the dead quick-actions check in #3684 |
| 40 | + * survived its own tests. |
| 41 | + */ |
| 42 | + const stack = { |
| 43 | + objects: [ |
| 44 | + { |
| 45 | + name: 'crm_lead', |
| 46 | + fields: { name: { type: 'text', label: 'Name' } }, |
| 47 | + permissions: {}, |
| 48 | + }, |
| 49 | + ], |
| 50 | + actions: [ |
| 51 | + // validateObjectReferences: a param pointing at an object nothing declares. |
| 52 | + { name: 'assign', label: 'Assign', params: [{ name: 'owner', reference: 'user' }] }, |
| 53 | + ], |
| 54 | + views: [ |
| 55 | + { |
| 56 | + list: { |
| 57 | + type: 'grid', |
| 58 | + name: 'all_leads', |
| 59 | + data: { provider: 'object', object: 'crm_lead' }, |
| 60 | + // validateActionNameRefs: no such action. |
| 61 | + bulkActions: ['mass_update'], |
| 62 | + }, |
| 63 | + }, |
| 64 | + ], |
| 65 | + pages: [ |
| 66 | + { |
| 67 | + name: 'lead_detail', |
| 68 | + object: 'crm_lead', |
| 69 | + regions: [ |
| 70 | + { |
| 71 | + components: [ |
| 72 | + // validatePageFieldBindings: `budget` is not a field on crm_lead. |
| 73 | + { type: 'record:highlights', properties: { fields: [{ name: 'budget' }] } }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + ], |
| 79 | + datasets: [ |
| 80 | + { |
| 81 | + name: 'lead_metrics', |
| 82 | + object: 'crm_lead', |
| 83 | + dimensions: [{ name: 'source' }], |
| 84 | + measures: [{ name: 'count_leads', aggregate: 'count' }], |
| 85 | + }, |
| 86 | + ], |
| 87 | + reports: [ |
| 88 | + { |
| 89 | + name: 'leads_by_source', |
| 90 | + dataset: 'lead_metrics', |
| 91 | + values: ['count_leads'], |
| 92 | + // validateChartBindings: a raw field where a declared measure is required. |
| 93 | + chart: { type: 'bar', xAxis: 'source', yAxis: 'lead_score' }, |
| 94 | + }, |
| 95 | + ], |
| 96 | + apps: [ |
| 97 | + { |
| 98 | + name: 'crm_app', |
| 99 | + navigation: [{ id: 'nav_leads', type: 'object', objectName: 'crm_lead' }], |
| 100 | + }, |
| 101 | + ], |
| 102 | + // validateNavAccess: a declared permission set that grants nothing on the |
| 103 | + // nav-exposed object. |
| 104 | + permissions: [{ name: 'sales_user', label: 'Sales User', objects: {} }], |
| 105 | + translations: [ |
| 106 | + // validateTranslationReferences: a field the object does not declare. |
| 107 | + { en: { objects: { crm_lead: { label: 'Lead', fields: { assigned_to: { label: 'Owner' } } } } } }, |
| 108 | + ], |
| 109 | + }; |
| 110 | + |
| 111 | + it('reports at least one finding from every member', () => { |
| 112 | + const findings = validateReferenceIntegrity(stack); |
| 113 | + const rules = new Set(findings.map((f) => f.rule)); |
| 114 | + |
| 115 | + expect(rules).toContain('object-reference-unknown'); |
| 116 | + expect(rules).toContain('action-name-undefined'); |
| 117 | + expect(rules).toContain('page-field-unknown'); |
| 118 | + expect(rules).toContain('chart-measure-unknown'); |
| 119 | + expect(rules).toContain('nav-object-ungranted'); |
| 120 | + expect(rules).toContain('translation-target-unknown'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('concatenates in list order and carries the common finding shape', () => { |
| 124 | + const findings = validateReferenceIntegrity(stack); |
| 125 | + expect(findings.length).toBeGreaterThan(0); |
| 126 | + for (const f of findings) { |
| 127 | + expect(['error', 'warning']).toContain(f.severity); |
| 128 | + expect(typeof f.rule).toBe('string'); |
| 129 | + expect(typeof f.where).toBe('string'); |
| 130 | + expect(typeof f.path).toBe('string'); |
| 131 | + expect(typeof f.message).toBe('string'); |
| 132 | + expect(typeof f.hint).toBe('string'); |
| 133 | + } |
| 134 | + // Object references run first, translations last. |
| 135 | + expect(findings[0].rule).toBe('object-reference-unknown'); |
| 136 | + expect(findings[findings.length - 1].rule).toBe('translation-target-unknown'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('returns nothing for an empty stack', () => { |
| 140 | + expect(validateReferenceIntegrity({})).toEqual([]); |
| 141 | + }); |
| 142 | +}); |
0 commit comments