|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `os lint` reports the list view's retired `filterableFields` BY NAME. |
| 5 | + * |
| 6 | + * @objectstack/spec 18 retired the key with a `retiredKey()` tombstone, so |
| 7 | + * `os validate` and `os build` refuse it at the parse. `os lint` never parses: |
| 8 | + * it judges the `normalizeStackInput` output, and the D2 conversion that |
| 9 | + * strips the key is retired from the load path, so on a non-`defineStack` |
| 10 | + * source the key reaches the list-view field-reference rule as written. That |
| 11 | + * rule used to walk it as a field position — a valid name passed `os lint`, |
| 12 | + * and a stale one was named as an unknown FIELD. Now it reports the |
| 13 | + * retirement, once per list view, with the tombstone's own prescription. |
| 14 | + * |
| 15 | + * This file authors the retired key as a key on purpose, so the spec's |
| 16 | + * tree-scoped absence walk (`view-list-filterable-fields-retirement.test.ts`) |
| 17 | + * excludes it by name, as a member of the retirement kit. |
| 18 | + */ |
| 19 | + |
| 20 | +import { describe, expect, it } from 'vitest'; |
| 21 | + |
| 22 | +import { runAuthoringRules } from './authoring-rules.js'; |
| 23 | +import { |
| 24 | + LIST_VIEW_FIELD_DOTTED, |
| 25 | + LIST_VIEW_FIELD_UNKNOWN, |
| 26 | + LIST_VIEW_KEY_RETIRED, |
| 27 | + LIST_VIEW_RETIRED_KEYS_REPORTED, |
| 28 | + listViewRetiredKeyPrescription, |
| 29 | + validateListViewFieldRefs, |
| 30 | +} from './validate-list-view-field-refs.js'; |
| 31 | +import { retiredKeyRemedy } from './validate-retired-permission-residue.js'; |
| 32 | + |
| 33 | +type AnyRec = Record<string, unknown>; |
| 34 | + |
| 35 | +const task = { |
| 36 | + name: 'duly_task', |
| 37 | + fields: { |
| 38 | + title: { type: 'text' }, |
| 39 | + status: { type: 'select', options: [{ label: 'Open', value: 'open' }] }, |
| 40 | + owner: { type: 'lookup', reference: 'duly_person' }, |
| 41 | + }, |
| 42 | +}; |
| 43 | +const person = { name: 'duly_person', fields: { name: { type: 'text' } } }; |
| 44 | + |
| 45 | +const listWith = (extra: AnyRec): AnyRec => ({ |
| 46 | + type: 'grid', |
| 47 | + data: { provider: 'object', object: 'duly_task' }, |
| 48 | + columns: ['title'], |
| 49 | + ...extra, |
| 50 | +}); |
| 51 | + |
| 52 | +const stackWithList = (list: AnyRec, objects: unknown[] = [task, person]): AnyRec => ({ |
| 53 | + objects, |
| 54 | + views: [{ name: 'task', objectName: 'duly_task', list }], |
| 55 | +}); |
| 56 | + |
| 57 | +const retiredOf = <F extends { rule: string }>(findings: F[]): F[] => findings.filter((f) => f.rule === LIST_VIEW_KEY_RETIRED); |
| 58 | + |
| 59 | +describe('the list view\'s retired `filterableFields` is reported by name, never as a field position', () => { |
| 60 | + const prescription = listViewRetiredKeyPrescription('filterableFields'); |
| 61 | + |
| 62 | + it('the tombstone publishes its prescription, and the rule reports exactly the retired key it used to walk', () => { |
| 63 | + expect(prescription).not.toBeNull(); |
| 64 | + expect(LIST_VIEW_RETIRED_KEYS_REPORTED).toEqual(['filterableFields']); |
| 65 | + // A key that is not a tombstone has no prescription to read. |
| 66 | + expect(listViewRetiredKeyPrescription('columns')).toBeNull(); |
| 67 | + expect(listViewRetiredKeyPrescription('no_such_key')).toBeNull(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('a VALID field name is reported as the retirement, where it used to pass in silence', () => { |
| 71 | + const findings = validateListViewFieldRefs(stackWithList(listWith({ filterableFields: ['status'] }))); |
| 72 | + expect(findings).toHaveLength(1); |
| 73 | + expect(findings[0]).toMatchObject({ |
| 74 | + rule: LIST_VIEW_KEY_RETIRED, |
| 75 | + severity: 'error', |
| 76 | + path: 'views[0].list.filterableFields', |
| 77 | + }); |
| 78 | + expect(findings[0]!.message).toContain('`filterableFields`'); |
| 79 | + // Read from the tombstone, never retyped. |
| 80 | + expect(findings[0]!.hint).toBe(retiredKeyRemedy(prescription!)); |
| 81 | + }); |
| 82 | + |
| 83 | + it('a stale or dotted name is reported as the retirement, never as a field reference', () => { |
| 84 | + const findings = validateListViewFieldRefs( |
| 85 | + stackWithList(listWith({ filterableFields: ['nope_field', 'owner.name'] })), |
| 86 | + ); |
| 87 | + expect(findings.map((f) => f.rule)).toEqual([LIST_VIEW_KEY_RETIRED]); |
| 88 | + expect(findings.filter((f) => f.rule === LIST_VIEW_FIELD_UNKNOWN || f.rule === LIST_VIEW_FIELD_DOTTED)).toEqual([]); |
| 89 | + }); |
| 90 | + |
| 91 | + it('is reported at every list-view rung the rule walks', () => { |
| 92 | + const lv = listWith({ filterableFields: [] }); |
| 93 | + const stack: AnyRec = { |
| 94 | + objects: [{ ...task, listViews: { mine: lv } }, person], |
| 95 | + views: [ |
| 96 | + { name: 'task', objectName: 'duly_task', list: lv, listViews: { open: lv } }, |
| 97 | + { name: 'duly_task.flat', object: 'duly_task', viewKind: 'list', ...lv }, |
| 98 | + { name: 'duly_task.item', object: 'duly_task', viewKind: 'list', config: lv }, |
| 99 | + ], |
| 100 | + }; |
| 101 | + expect(retiredOf(validateListViewFieldRefs(stack)).map((f) => f.path).sort()).toEqual([ |
| 102 | + 'objects[0].listViews.mine.filterableFields', |
| 103 | + 'views[0].list.filterableFields', |
| 104 | + 'views[0].listViews.open.filterableFields', |
| 105 | + 'views[1].filterableFields', |
| 106 | + 'views[2].config.filterableFields', |
| 107 | + ]); |
| 108 | + }); |
| 109 | + |
| 110 | + it('does not depend on the binding: an unresolvable object, a non-object provider and an empty graph all report it', () => { |
| 111 | + const unbound = validateListViewFieldRefs( |
| 112 | + stackWithList(listWith({ data: { provider: 'object', object: 'no_such_object' }, filterableFields: ['x'] })), |
| 113 | + ); |
| 114 | + expect(retiredOf(unbound)).toHaveLength(1); |
| 115 | + const inline = validateListViewFieldRefs( |
| 116 | + stackWithList(listWith({ data: { provider: 'value', items: [] }, filterableFields: ['x'] })), |
| 117 | + ); |
| 118 | + expect(retiredOf(inline)).toHaveLength(1); |
| 119 | + const noObjects = validateListViewFieldRefs(stackWithList(listWith({ filterableFields: ['x'] }), [])); |
| 120 | + expect(retiredOf(noObjects)).toHaveLength(1); |
| 121 | + }); |
| 122 | + |
| 123 | + it('lit control: a list view without the key reports no retirement, and its surviving filter positions are still judged', () => { |
| 124 | + const findings = validateListViewFieldRefs( |
| 125 | + stackWithList(listWith({ userFilters: { element: 'dropdown', fields: [{ field: 'nope_field' }] } })), |
| 126 | + ); |
| 127 | + expect(retiredOf(findings)).toEqual([]); |
| 128 | + expect(findings.map((f) => f.rule)).toEqual([LIST_VIEW_FIELD_UNKNOWN]); |
| 129 | + }); |
| 130 | + |
| 131 | + it('reaches `os lint`\'s registry run on the normalized (unparsed) stack, as an error', () => { |
| 132 | + const stack = stackWithList(listWith({ filterableFields: ['status'] })); |
| 133 | + const findings = runAuthoringRules('lint', { normalized: stack }); |
| 134 | + const retired = findings.filter((f) => f.rule === LIST_VIEW_KEY_RETIRED); |
| 135 | + expect(retired).toHaveLength(1); |
| 136 | + expect(retired[0]).toMatchObject({ severity: 'error', path: 'views[0].list.filterableFields' }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments