|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { validateActionLocations, ACTION_NO_PLACEMENT } from './validate-action-locations.js'; |
| 5 | + |
| 6 | +/** A stack whose single action declares a real placement. */ |
| 7 | +const placed = () => ({ |
| 8 | + objects: [{ name: 'crm_lead', fields: { name: { type: 'text' } } }], |
| 9 | + actions: [ |
| 10 | + { |
| 11 | + name: 'crm_convert_lead', |
| 12 | + label: 'Convert', |
| 13 | + type: 'script', |
| 14 | + locations: ['record_header'], |
| 15 | + }, |
| 16 | + ], |
| 17 | +}); |
| 18 | + |
| 19 | +/** The same action with the placement key absent. */ |
| 20 | +const unplaced = () => ({ |
| 21 | + objects: [{ name: 'crm_lead', fields: { name: { type: 'text' } } }], |
| 22 | + actions: [{ name: 'crm_convert_lead', label: 'Convert', type: 'script' }], |
| 23 | +}); |
| 24 | + |
| 25 | +describe('validateActionLocations', () => { |
| 26 | + it('flags an action that declares no locations and that no view places', () => { |
| 27 | + const findings = validateActionLocations(unplaced()); |
| 28 | + |
| 29 | + expect(findings).toHaveLength(1); |
| 30 | + expect(findings[0].severity).toBe('warning'); |
| 31 | + expect(findings[0].rule).toBe(ACTION_NO_PLACEMENT); |
| 32 | + expect(findings[0].path).toBe('actions[0]'); |
| 33 | + expect(findings[0].where).toBe('action "crm_convert_lead"'); |
| 34 | + expect(findings[0].message).toContain('renders on no surface'); |
| 35 | + expect(findings[0].hint).toContain('locations: []'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('accepts a declared placement', () => { |
| 39 | + expect(validateActionLocations(placed())).toEqual([]); |
| 40 | + }); |
| 41 | + |
| 42 | + it('walks object-embedded actions too', () => { |
| 43 | + const findings = validateActionLocations({ |
| 44 | + objects: [ |
| 45 | + { |
| 46 | + name: 'crm_lead', |
| 47 | + actions: [{ name: 'crm_score', label: 'Score', type: 'script' }], |
| 48 | + }, |
| 49 | + ], |
| 50 | + }); |
| 51 | + |
| 52 | + expect(findings).toHaveLength(1); |
| 53 | + expect(findings[0].path).toBe('objects[0].actions[0]'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('ignores a nameless action — that is action-name-*’s problem, not this rule’s', () => { |
| 57 | + expect(validateActionLocations({ actions: [{ label: 'Nameless', type: 'script' }] })).toEqual([]); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('— headless actions (`locations: []`) are never flagged', () => { |
| 61 | + it('accepts an explicitly empty placement', () => { |
| 62 | + // `content/docs/ui/actions.mdx` documents the empty array as the way to |
| 63 | + // declare a REST/MCP/AI-callable action with no UI surface. ADR-0110 D3 |
| 64 | + // refuses an UNdeclared handler, so this is the only legal shape for one |
| 65 | + // — flagging it would fight that ADR. |
| 66 | + const findings = validateActionLocations({ |
| 67 | + actions: [{ name: 'crm_sync_remote', label: 'Sync', type: 'script', locations: [] }], |
| 68 | + }); |
| 69 | + expect(findings).toEqual([]); |
| 70 | + }); |
| 71 | + |
| 72 | + it('distinguishes "nowhere, deliberately" from an unstated placement', () => { |
| 73 | + const findings = validateActionLocations({ |
| 74 | + actions: [ |
| 75 | + { name: 'said_nowhere', type: 'script', locations: [] }, |
| 76 | + { name: 'said_nothing', type: 'script' }, |
| 77 | + ], |
| 78 | + }); |
| 79 | + expect(findings.map((f) => f.where)).toEqual(['action "said_nothing"']); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('— a view that places the action by NAME exempts it', () => { |
| 84 | + it('exempts an action named in a list view’s bulkActions', () => { |
| 85 | + const findings = validateActionLocations({ |
| 86 | + ...unplaced(), |
| 87 | + views: [{ name: 'crm_lead', list: { bulkActions: ['crm_convert_lead'] } }], |
| 88 | + }); |
| 89 | + expect(findings).toEqual([]); |
| 90 | + }); |
| 91 | + |
| 92 | + it('exempts an action named in a bulkActionDefs entry (incl. aggregate defs)', () => { |
| 93 | + // objectui#3139: an aggregate bulk action has no single-record location |
| 94 | + // by construction — the view naming it IS the placement. |
| 95 | + const findings = validateActionLocations({ |
| 96 | + ...unplaced(), |
| 97 | + views: [ |
| 98 | + { |
| 99 | + name: 'crm_lead', |
| 100 | + list: { |
| 101 | + bulkActionDefs: [ |
| 102 | + { name: 'crm_convert_lead', operation: 'custom', execution: 'aggregate' }, |
| 103 | + ], |
| 104 | + }, |
| 105 | + }, |
| 106 | + ], |
| 107 | + }); |
| 108 | + expect(findings).toEqual([]); |
| 109 | + }); |
| 110 | + |
| 111 | + it('exempts an action named in rowActions', () => { |
| 112 | + const findings = validateActionLocations({ |
| 113 | + ...unplaced(), |
| 114 | + views: [{ name: 'crm_lead', list: { rowActions: ['crm_convert_lead'] } }], |
| 115 | + }); |
| 116 | + expect(findings).toEqual([]); |
| 117 | + }); |
| 118 | + |
| 119 | + it('exempts via a named listViews entry, not just the default list', () => { |
| 120 | + const findings = validateActionLocations({ |
| 121 | + ...unplaced(), |
| 122 | + views: [{ name: 'crm_lead', listViews: { hot: { bulkActions: ['crm_convert_lead'] } } }], |
| 123 | + }); |
| 124 | + expect(findings).toEqual([]); |
| 125 | + }); |
| 126 | + |
| 127 | + it('exempts via an OBJECT-embedded list view — an object has no top-level `list`', () => { |
| 128 | + const findings = validateActionLocations({ |
| 129 | + objects: [ |
| 130 | + { |
| 131 | + name: 'crm_lead', |
| 132 | + listViews: { all: { bulkActions: ['crm_convert_lead'] } }, |
| 133 | + }, |
| 134 | + ], |
| 135 | + actions: [{ name: 'crm_convert_lead', label: 'Convert', type: 'script' }], |
| 136 | + }); |
| 137 | + expect(findings).toEqual([]); |
| 138 | + }); |
| 139 | + |
| 140 | + it('still flags an action no view names, alongside one that is named', () => { |
| 141 | + const findings = validateActionLocations({ |
| 142 | + actions: [ |
| 143 | + { name: 'named_one', type: 'script' }, |
| 144 | + { name: 'orphan_one', type: 'script' }, |
| 145 | + ], |
| 146 | + views: [{ name: 'crm_lead', list: { bulkActions: ['named_one'] } }], |
| 147 | + }); |
| 148 | + expect(findings.map((f) => f.where)).toEqual(['action "orphan_one"']); |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + describe('— floor', () => { |
| 153 | + it('returns nothing for a clean stack', () => { |
| 154 | + expect(validateActionLocations(placed())).toEqual([]); |
| 155 | + }); |
| 156 | + |
| 157 | + it('returns nothing for an empty stack', () => { |
| 158 | + expect(validateActionLocations({})).toEqual([]); |
| 159 | + }); |
| 160 | + |
| 161 | + it('returns nothing for a null stack', () => { |
| 162 | + expect(validateActionLocations(null as unknown as Record<string, unknown>)).toEqual([]); |
| 163 | + }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments