|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { |
| 5 | + lintViewRefs, |
| 6 | + VIEW_KEY_COLLISION, |
| 7 | + VIEW_REF_FORM_TARGET_MISSING, |
| 8 | + VIEW_REF_FORM_TARGET_KIND, |
| 9 | +} from './lint-view-refs.js'; |
| 10 | + |
| 11 | +const listView = (object: string) => ({ |
| 12 | + type: 'grid', |
| 13 | + label: 'All', |
| 14 | + columns: ['title'], |
| 15 | + data: { provider: 'object', object }, |
| 16 | +}); |
| 17 | +const formView = (object: string) => ({ |
| 18 | + type: 'simple', |
| 19 | + data: { provider: 'object', object }, |
| 20 | + sections: [], |
| 21 | +}); |
| 22 | + |
| 23 | +describe('lintViewRefs — clean paths', () => { |
| 24 | + it('passes a container whose form key does not collide, with a correct form target', () => { |
| 25 | + const stack = { |
| 26 | + views: [{ name: 'task', list: listView('task'), formViews: { edit: formView('task') } }], |
| 27 | + actions: [{ name: 'log_time', type: 'form', target: 'task.edit' }], |
| 28 | + }; |
| 29 | + expect(lintViewRefs(stack)).toEqual([]); |
| 30 | + }); |
| 31 | + |
| 32 | + it('ignores non-form action types (their target is not a form-view ref)', () => { |
| 33 | + const stack = { |
| 34 | + views: [{ name: 'task', list: listView('task') }], |
| 35 | + actions: [ |
| 36 | + { name: 'open_docs', type: 'url', target: 'https://example.com' }, |
| 37 | + { name: 'gallery', type: 'modal', target: 'some_modal' }, |
| 38 | + ], |
| 39 | + }; |
| 40 | + expect(lintViewRefs(stack)).toEqual([]); |
| 41 | + }); |
| 42 | + |
| 43 | + it('skips dynamic (interpolated) and non-qualified targets', () => { |
| 44 | + const stack = { |
| 45 | + views: [{ name: 'task', list: listView('task'), formViews: { edit: formView('task') } }], |
| 46 | + actions: [ |
| 47 | + { name: 'a', type: 'form', target: 'task.${param.view}' }, |
| 48 | + { name: 'b', type: 'form', target: 'bare_key_no_dot' }, |
| 49 | + ], |
| 50 | + }; |
| 51 | + expect(lintViewRefs(stack)).toEqual([]); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe('lintViewRefs — object name derivation (real defineView shape)', () => { |
| 56 | + // `defineView({...})` containers carry NO top-level name/object — the object |
| 57 | + // lives only in `list.data.object`. The lint must derive it exactly like the |
| 58 | + // runtime loader, or every name-less container silently drops out of the index |
| 59 | + // and its targets read as "missing" (the false negative found dogfooding |
| 60 | + // app-showcase). |
| 61 | + it('indexes a name-less container via list.data.object and accepts a good target', () => { |
| 62 | + const stack = { |
| 63 | + views: [{ list: listView('showcase_task'), formViews: { edit: formView('showcase_task') } }], |
| 64 | + actions: [{ name: 'log_time', type: 'form', target: 'showcase_task.edit' }], |
| 65 | + }; |
| 66 | + expect(lintViewRefs(stack)).toEqual([]); |
| 67 | + }); |
| 68 | + |
| 69 | + it('still flags a genuinely missing target on a name-less container', () => { |
| 70 | + const stack = { |
| 71 | + views: [{ list: listView('showcase_task'), formViews: { edit: formView('showcase_task') } }], |
| 72 | + actions: [{ name: 'x', type: 'form', target: 'showcase_task.nope' }], |
| 73 | + }; |
| 74 | + expect(lintViewRefs(stack).some((f) => f.rule === VIEW_REF_FORM_TARGET_MISSING)).toBe(true); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +describe('lintViewRefs — view-key collisions (#2554)', () => { |
| 79 | + it('warns (does NOT fail the build) when formViews.default collides with the implicit default list', () => { |
| 80 | + const stack = { |
| 81 | + views: [{ name: 'task', list: listView('task'), formViews: { default: formView('task') } }], |
| 82 | + }; |
| 83 | + const out = lintViewRefs(stack); |
| 84 | + const collision = out.find((f) => f.rule === VIEW_KEY_COLLISION); |
| 85 | + expect(collision).toBeDefined(); |
| 86 | + // Fragile, not broken: a rename only breaks something if the name is referenced. |
| 87 | + expect(collision!.severity).toBe('warning'); |
| 88 | + expect(collision!.message).toContain("'task.default'"); |
| 89 | + expect(collision!.message).toContain("'task.default_2'"); |
| 90 | + }); |
| 91 | + |
| 92 | + it('detects the collision in object-nested listViews/formViews too', () => { |
| 93 | + const stack = { |
| 94 | + objects: [ |
| 95 | + { |
| 96 | + name: 'task', |
| 97 | + listViews: { mine: listView('task') }, |
| 98 | + formViews: { mine: formView('task') }, |
| 99 | + }, |
| 100 | + ], |
| 101 | + }; |
| 102 | + const out = lintViewRefs(stack); |
| 103 | + expect(out.some((f) => f.rule === VIEW_KEY_COLLISION && f.message.includes("'task.mine'"))).toBe(true); |
| 104 | + }); |
| 105 | + |
| 106 | + it('a collision-only stack yields NO error-severity finding — build is not blocked', () => { |
| 107 | + const stack = { |
| 108 | + views: [{ name: 'task', list: listView('task'), formViews: { default: formView('task') } }], |
| 109 | + }; |
| 110 | + const out = lintViewRefs(stack); |
| 111 | + expect(out.some((f) => f.rule === VIEW_KEY_COLLISION)).toBe(true); // collision surfaced… |
| 112 | + expect(out.some((f) => f.severity === 'error')).toBe(false); // …but nothing fails the build |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe('lintViewRefs — form action target resolution', () => { |
| 117 | + it('errors when a form target names a LIST view (the #2554 runtime symptom)', () => { |
| 118 | + // `default` is the list; the form collides to `default_2`, so `task.default` |
| 119 | + // resolves to the list — exactly what opened a blank form at runtime. |
| 120 | + const stack = { |
| 121 | + views: [{ name: 'task', list: listView('task'), formViews: { default: formView('task') } }], |
| 122 | + actions: [{ name: 'log_time', type: 'form', target: 'task.default' }], |
| 123 | + }; |
| 124 | + const out = lintViewRefs(stack); |
| 125 | + const kindErr = out.find((f) => f.rule === VIEW_REF_FORM_TARGET_KIND); |
| 126 | + expect(kindErr).toBeDefined(); |
| 127 | + expect(kindErr!.severity).toBe('error'); |
| 128 | + expect(kindErr!.message).toContain('list view, not a form view'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('warns (possible false positive, does NOT fail) when a form target resolves to no view at all', () => { |
| 132 | + const stack = { |
| 133 | + views: [{ name: 'task', list: listView('task'), formViews: { edit: formView('task') } }], |
| 134 | + actions: [{ name: 'log_time', type: 'form', target: 'task.nope' }], |
| 135 | + }; |
| 136 | + const out = lintViewRefs(stack); |
| 137 | + const missWarn = out.find((f) => f.rule === VIEW_REF_FORM_TARGET_MISSING); |
| 138 | + expect(missWarn).toBeDefined(); |
| 139 | + // Might be a view the lint failed to collect — warn rather than break the build. |
| 140 | + expect(missWarn!.severity).toBe('warning'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('accepts a form target that resolves to an actual form view', () => { |
| 144 | + const stack = { |
| 145 | + views: [{ name: 'task', list: listView('task'), formViews: { edit: formView('task') } }], |
| 146 | + actions: [{ name: 'log_time', type: 'form', target: 'task.edit' }], |
| 147 | + }; |
| 148 | + expect(lintViewRefs(stack).filter((f) => f.rule.startsWith('view-ref'))).toEqual([]); |
| 149 | + }); |
| 150 | + |
| 151 | + it('validates object-nested actions against object-nested form views', () => { |
| 152 | + const stack = { |
| 153 | + objects: [ |
| 154 | + { |
| 155 | + name: 'task', |
| 156 | + formViews: { edit: formView('task') }, |
| 157 | + actions: [{ name: 'nested_form', type: 'form', target: 'task.missing' }], |
| 158 | + }, |
| 159 | + ], |
| 160 | + }; |
| 161 | + const out = lintViewRefs(stack); |
| 162 | + expect(out.some((f) => f.rule === VIEW_REF_FORM_TARGET_MISSING && f.where.includes("object 'task'"))).toBe(true); |
| 163 | + }); |
| 164 | + |
| 165 | + it('reports a shared (top-level + object-nested) action only once', () => { |
| 166 | + const shared = { name: 'log_time', type: 'form', target: 'task.missing' }; |
| 167 | + const stack = { |
| 168 | + views: [{ name: 'task', list: listView('task'), formViews: { edit: formView('task') } }], |
| 169 | + actions: [shared], |
| 170 | + objects: [{ name: 'task', actions: [shared] }], |
| 171 | + }; |
| 172 | + const out = lintViewRefs(stack).filter((f) => f.rule === VIEW_REF_FORM_TARGET_MISSING); |
| 173 | + expect(out).toHaveLength(1); |
| 174 | + expect(out[0].where).toContain("object 'task'"); // object-nested context retained |
| 175 | + }); |
| 176 | +}); |
0 commit comments