|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { |
| 5 | + validateDashboardActionRefs, |
| 6 | + DASHBOARD_ACTION_TARGET_UNDEFINED, |
| 7 | + DASHBOARD_ACTION_ROUTE_UNRESOLVED, |
| 8 | +} from './validate-dashboard-action-refs'; |
| 9 | + |
| 10 | +/** Build a stack with a single dashboard whose header carries `actions`. */ |
| 11 | +function dashWithHeaderActions(actions: unknown[], extra: Record<string, unknown> = {}) { |
| 12 | + return { |
| 13 | + ...extra, |
| 14 | + dashboards: [ |
| 15 | + { name: 'exec', label: 'Executive', header: { actions }, widgets: [] }, |
| 16 | + ], |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +describe('validateDashboardActionRefs (ADR-0049 references / #3367)', () => { |
| 21 | + it('passes a script action that names a defined global action', () => { |
| 22 | + const findings = validateDashboardActionRefs( |
| 23 | + dashWithHeaderActions( |
| 24 | + [{ label: 'Recalc', actionType: 'script', actionUrl: 'recalc_totals' }], |
| 25 | + { actions: [{ name: 'recalc_totals', type: 'script' }] }, |
| 26 | + ), |
| 27 | + ); |
| 28 | + expect(findings).toEqual([]); |
| 29 | + }); |
| 30 | + |
| 31 | + it('passes a script action that names a defined object-embedded action', () => { |
| 32 | + const findings = validateDashboardActionRefs( |
| 33 | + dashWithHeaderActions( |
| 34 | + [{ label: 'Close', actionType: 'script', actionUrl: 'close_deal' }], |
| 35 | + { objects: [{ name: 'opportunity', actions: [{ name: 'close_deal', type: 'script' }] }] }, |
| 36 | + ), |
| 37 | + ); |
| 38 | + expect(findings).toEqual([]); |
| 39 | + }); |
| 40 | + |
| 41 | + it('ERRORS on a script action whose target is defined nowhere', () => { |
| 42 | + const findings = validateDashboardActionRefs( |
| 43 | + dashWithHeaderActions([ |
| 44 | + { label: 'Export PDF', actionType: 'script', actionUrl: 'export_dashboard_pdf' }, |
| 45 | + ]), |
| 46 | + ); |
| 47 | + expect(findings).toHaveLength(1); |
| 48 | + expect(findings[0]).toMatchObject({ |
| 49 | + severity: 'error', |
| 50 | + rule: DASHBOARD_ACTION_TARGET_UNDEFINED, |
| 51 | + where: 'dashboard "exec" · header action "Export PDF"', |
| 52 | + path: 'dashboards[0].header.actions[0].actionUrl', |
| 53 | + }); |
| 54 | + expect(findings[0].message).toContain('export_dashboard_pdf'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('passes a modal action that names a defined action', () => { |
| 58 | + const findings = validateDashboardActionRefs( |
| 59 | + dashWithHeaderActions( |
| 60 | + [{ label: 'New Deal', actionType: 'modal', actionUrl: 'quick_create_deal' }], |
| 61 | + { actions: [{ name: 'quick_create_deal', type: 'modal' }] }, |
| 62 | + ), |
| 63 | + ); |
| 64 | + expect(findings).toEqual([]); |
| 65 | + }); |
| 66 | + |
| 67 | + it('passes a modal action using the <verb>_<object> convention against a real object', () => { |
| 68 | + const findings = validateDashboardActionRefs( |
| 69 | + dashWithHeaderActions( |
| 70 | + [{ label: 'New Deal', actionType: 'modal', actionUrl: 'create_opportunity' }], |
| 71 | + { objects: [{ name: 'opportunity' }] }, |
| 72 | + ), |
| 73 | + ); |
| 74 | + expect(findings).toEqual([]); |
| 75 | + }); |
| 76 | + |
| 77 | + it('passes a modal action that is a bare object name (create-form fallback)', () => { |
| 78 | + const findings = validateDashboardActionRefs( |
| 79 | + dashWithHeaderActions( |
| 80 | + [{ label: 'Add Lead', actionType: 'modal', actionUrl: 'lead' }], |
| 81 | + { objects: [{ name: 'lead' }] }, |
| 82 | + ), |
| 83 | + ); |
| 84 | + expect(findings).toEqual([]); |
| 85 | + }); |
| 86 | + |
| 87 | + it('ERRORS on a modal action whose target is neither a defined action nor a real object', () => { |
| 88 | + const findings = validateDashboardActionRefs( |
| 89 | + dashWithHeaderActions( |
| 90 | + [{ label: 'New Deal', actionType: 'modal', actionUrl: 'create_opportunity' }], |
| 91 | + { objects: [{ name: 'account' }] }, // no `opportunity` |
| 92 | + ), |
| 93 | + ); |
| 94 | + expect(findings).toHaveLength(1); |
| 95 | + expect(findings[0]).toMatchObject({ |
| 96 | + severity: 'error', |
| 97 | + rule: DASHBOARD_ACTION_TARGET_UNDEFINED, |
| 98 | + }); |
| 99 | + expect(findings[0].message).toContain('create_opportunity'); |
| 100 | + expect(findings[0].message).toContain('or object'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('passes a url action pointing at a registered report route', () => { |
| 104 | + const findings = validateDashboardActionRefs( |
| 105 | + dashWithHeaderActions( |
| 106 | + [{ label: 'Forecast', actionType: 'url', actionUrl: '/reports/forecast' }], |
| 107 | + { reports: [{ name: 'forecast' }] }, |
| 108 | + ), |
| 109 | + ); |
| 110 | + expect(findings).toEqual([]); |
| 111 | + }); |
| 112 | + |
| 113 | + it('WARNS on a url action pointing at a non-existent in-app route', () => { |
| 114 | + const findings = validateDashboardActionRefs( |
| 115 | + dashWithHeaderActions([ |
| 116 | + { label: 'Forecast', actionType: 'url', actionUrl: '/reports/forecast' }, |
| 117 | + ]), |
| 118 | + ); |
| 119 | + expect(findings).toHaveLength(1); |
| 120 | + expect(findings[0]).toMatchObject({ |
| 121 | + severity: 'warning', |
| 122 | + rule: DASHBOARD_ACTION_ROUTE_UNRESOLVED, |
| 123 | + where: 'dashboard "exec" · header action "Forecast"', |
| 124 | + path: 'dashboards[0].header.actions[0].actionUrl', |
| 125 | + }); |
| 126 | + expect(findings[0].message).toContain('/reports/forecast'); |
| 127 | + expect(findings[0].message).toContain('report named "forecast"'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('resolves an object route embedded mid-path (app-scoped route)', () => { |
| 131 | + const findings = validateDashboardActionRefs( |
| 132 | + dashWithHeaderActions( |
| 133 | + [{ label: 'Deals', actionType: 'url', actionUrl: '/apps/crm/objects/deal' }], |
| 134 | + { objects: [{ name: 'deal' }] }, |
| 135 | + ), |
| 136 | + ); |
| 137 | + expect(findings).toEqual([]); |
| 138 | + }); |
| 139 | + |
| 140 | + it('skips external URLs, interpolated targets, and opaque routes (no false positives)', () => { |
| 141 | + const findings = validateDashboardActionRefs( |
| 142 | + dashWithHeaderActions([ |
| 143 | + { label: 'Docs', actionType: 'url', actionUrl: 'https://example.com/x' }, |
| 144 | + { label: 'Proto', actionType: 'url', actionUrl: '//cdn.example.com/y' }, |
| 145 | + { label: 'Dyn', actionType: 'url', actionUrl: '/reports/${ctx.reportId}' }, |
| 146 | + { label: 'Home', actionType: 'url', actionUrl: '/home' }, |
| 147 | + { label: 'Settings', actionType: 'url', actionUrl: '/settings/profile' }, |
| 148 | + { label: 'Bare', actionType: 'url', actionUrl: 'some-handler' }, |
| 149 | + ]), |
| 150 | + ); |
| 151 | + expect(findings).toEqual([]); |
| 152 | + }); |
| 153 | + |
| 154 | + it('defaults a missing actionType to url (never errors on an unqualified target)', () => { |
| 155 | + const findings = validateDashboardActionRefs( |
| 156 | + dashWithHeaderActions([{ label: 'Mystery', actionUrl: 'do_something' }]), |
| 157 | + ); |
| 158 | + // `do_something` has no leading slash → treated as opaque url → skipped. |
| 159 | + expect(findings).toEqual([]); |
| 160 | + }); |
| 161 | + |
| 162 | + it('checks per-widget actionUrl buttons (script)', () => { |
| 163 | + const findings = validateDashboardActionRefs({ |
| 164 | + dashboards: [ |
| 165 | + { |
| 166 | + name: 'ops', |
| 167 | + label: 'Ops', |
| 168 | + widgets: [ |
| 169 | + { id: 'kpi', dataset: 'd', values: ['x'], actionType: 'script', actionUrl: 'ghost_action' }, |
| 170 | + { id: 'noaction', dataset: 'd', values: ['x'] }, |
| 171 | + ], |
| 172 | + }, |
| 173 | + ], |
| 174 | + }); |
| 175 | + expect(findings).toHaveLength(1); |
| 176 | + expect(findings[0]).toMatchObject({ |
| 177 | + severity: 'error', |
| 178 | + rule: DASHBOARD_ACTION_TARGET_UNDEFINED, |
| 179 | + where: 'dashboard "ops" · widget "kpi" action', |
| 180 | + path: 'dashboards[0].widgets[0].actionUrl', |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + it('covers the issue #3367 repro: one script + one modal error, one url warning', () => { |
| 185 | + // Faithful to the runtime: `create_opportunity` resolves via the modal |
| 186 | + // <verb>_<object> convention ONLY when an `opportunity` object exists. Here |
| 187 | + // it does not, so all three targets are dead. |
| 188 | + const findings = validateDashboardActionRefs( |
| 189 | + dashWithHeaderActions([ |
| 190 | + { label: 'Export PDF', actionType: 'script', actionUrl: 'export_dashboard_pdf' }, |
| 191 | + { label: 'New Deal', actionType: 'modal', actionUrl: 'create_opportunity' }, |
| 192 | + { label: 'Forecast', actionType: 'url', actionUrl: '/reports/forecast' }, |
| 193 | + ]), |
| 194 | + ); |
| 195 | + const errors = findings.filter((f) => f.severity === 'error'); |
| 196 | + const warnings = findings.filter((f) => f.severity === 'warning'); |
| 197 | + expect(errors).toHaveLength(2); |
| 198 | + expect(warnings).toHaveLength(1); |
| 199 | + expect(errors.map((e) => e.path)).toEqual([ |
| 200 | + 'dashboards[0].header.actions[0].actionUrl', |
| 201 | + 'dashboards[0].header.actions[1].actionUrl', |
| 202 | + ]); |
| 203 | + }); |
| 204 | + |
| 205 | + it('tolerates junk / empty input and dashboards without actions', () => { |
| 206 | + expect(validateDashboardActionRefs({})).toEqual([]); |
| 207 | + expect(validateDashboardActionRefs(undefined as unknown as Record<string, unknown>)).toEqual([]); |
| 208 | + expect(validateDashboardActionRefs({ dashboards: [] })).toEqual([]); |
| 209 | + expect(validateDashboardActionRefs({ dashboards: [null, 42] as unknown })).toEqual([]); |
| 210 | + expect( |
| 211 | + validateDashboardActionRefs({ dashboards: [{ name: 'd', widgets: [], header: {} }] }), |
| 212 | + ).toEqual([]); |
| 213 | + }); |
| 214 | +}); |
0 commit comments