|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #29213 — on a dashboard that declares the built-in `dateRange`, a |
| 5 | + * `globalFilters` entry keyed `dateRange` (its `name`, else its `field`) is |
| 6 | + * refused. |
| 7 | + * |
| 8 | + * Measured on `main` @ `5a2bdf9c` before the rule: `DashboardSchema` accepted |
| 9 | + * `{ dateRange: { field: 'close_date' }, globalFilters: [{ name: 'dateRange', field: 'region' }] }`, |
| 10 | + * and the console's `resolveDashboardFilterDefs` (like lint's |
| 11 | + * `dashboardFilterDefs`) built the built-in date range first and let the |
| 12 | + * entry replace it, so the board drew a `region` control where the date range |
| 13 | + * belonged and `filterBindings.dateRange` bound `region`. The refusal is |
| 14 | + * located at the entry's key. |
| 15 | + */ |
| 16 | +import { describe, expect, it } from 'vitest'; |
| 17 | +import { DashboardSchema } from './dashboard.zod'; |
| 18 | +import { reservedDateRangeGlobalFilters } from './duplicate-global-filter-names'; |
| 19 | + |
| 20 | +const board = (extra: Record<string, unknown>) => ({ |
| 21 | + name: 'qa_probe_drg', |
| 22 | + label: 'QA probe', |
| 23 | + widgets: [], |
| 24 | + ...extra, |
| 25 | +}); |
| 26 | + |
| 27 | +const customIssues = (input: unknown) => { |
| 28 | + const result = DashboardSchema.safeParse(input); |
| 29 | + if (result.success) return []; |
| 30 | + return result.error.issues.filter((i) => i.code === 'custom'); |
| 31 | +}; |
| 32 | + |
| 33 | +describe('#29213 DashboardSchema refuses a globalFilters entry keyed dateRange beside a declared dateRange', () => { |
| 34 | + it('CONTROL: a declared dateRange beside a differently named filter parses', () => { |
| 35 | + const result = DashboardSchema.safeParse(board({ |
| 36 | + dateRange: { field: 'close_date' }, |
| 37 | + globalFilters: [{ name: 'region', field: 'region', type: 'text' }], |
| 38 | + })); |
| 39 | + expect(result.success).toBe(true); |
| 40 | + }); |
| 41 | + |
| 42 | + it('CONTROL: with no dateRange declared, an entry named dateRange is an ordinary filter and parses', () => { |
| 43 | + const result = DashboardSchema.safeParse(board({ |
| 44 | + globalFilters: [{ name: 'dateRange', field: 'close_date', type: 'date' }], |
| 45 | + })); |
| 46 | + expect(result.success).toBe(true); |
| 47 | + }); |
| 48 | + |
| 49 | + it('the card reproduction is refused at globalFilters[0].name, naming the reserved word and the built-in date range', () => { |
| 50 | + const result = DashboardSchema.safeParse(board({ |
| 51 | + dateRange: { field: 'close_date' }, |
| 52 | + globalFilters: [{ name: 'dateRange', field: 'region', type: 'text' }], |
| 53 | + })); |
| 54 | + expect(result.success).toBe(false); |
| 55 | + const issues = result.error!.issues; |
| 56 | + expect(issues).toHaveLength(1); |
| 57 | + expect(issues[0]).toMatchObject({ code: 'custom', path: ['globalFilters', 0, 'name'] }); |
| 58 | + expect(issues[0]!.message.startsWith( |
| 59 | + "globalFilters[0] is keyed 'dateRange', the reserved name of the built-in date range this dashboard declares in `dateRange`.", |
| 60 | + )).toBe(true); |
| 61 | + expect(issues[0]!.message).toMatch(/Give this filter another `name` .* or delete it and keep the built-in `dateRange`\.$/); |
| 62 | + // Runtime strings carry no tracker number (check:doc-authoring). |
| 63 | + expect(issues[0]!.message).not.toMatch(/#\d{3,}/); |
| 64 | + }); |
| 65 | + |
| 66 | + it('an empty dateRange object is a declared dateRange (the shape both resolvers test)', () => { |
| 67 | + const issues = customIssues(board({ |
| 68 | + dateRange: {}, |
| 69 | + globalFilters: [{ name: 'dateRange', field: 'region', type: 'text' }], |
| 70 | + })); |
| 71 | + expect(issues).toHaveLength(1); |
| 72 | + expect(issues[0]).toMatchObject({ path: ['globalFilters', 0, 'name'] }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('an UNNAMED entry whose field is dateRange is keyed by the field, located at .field', () => { |
| 76 | + const issues = customIssues(board({ |
| 77 | + dateRange: {}, |
| 78 | + globalFilters: [{ name: 'region', field: 'region', type: 'text' }, { field: 'dateRange', type: 'text' }], |
| 79 | + })); |
| 80 | + expect(issues).toHaveLength(1); |
| 81 | + expect(issues[0]).toMatchObject({ path: ['globalFilters', 1, 'field'] }); |
| 82 | + expect(issues[0]!.message).toContain('(a filter with no `name` is keyed by its `field`)'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('CONTROL: a named entry over a field called dateRange is keyed by its name and parses', () => { |
| 86 | + const result = DashboardSchema.safeParse(board({ |
| 87 | + dateRange: {}, |
| 88 | + globalFilters: [{ name: 'period', field: 'dateRange', type: 'text' }], |
| 89 | + })); |
| 90 | + expect(result.success).toBe(true); |
| 91 | + }); |
| 92 | + |
| 93 | + it('two entries keyed dateRange answer one reserved-name issue each, beside the duplicate-key issue', () => { |
| 94 | + const issues = customIssues(board({ |
| 95 | + dateRange: {}, |
| 96 | + globalFilters: [{ name: 'dateRange', field: 'region' }, { name: 'dateRange', field: 'status' }], |
| 97 | + })); |
| 98 | + const reserved = issues.filter((i) => i.message.includes('reserved name of the built-in date range')); |
| 99 | + expect(reserved.map((i) => i.path)).toEqual([['globalFilters', 0, 'name'], ['globalFilters', 1, 'name']]); |
| 100 | + expect(issues.filter((i) => i.message.startsWith('This dashboard declares the global filter')).map((i) => i.path)) |
| 101 | + .toEqual([['globalFilters', 1, 'name']]); |
| 102 | + }); |
| 103 | + |
| 104 | + it('the refusal still answers when another key of the dashboard fails too', () => { |
| 105 | + const result = DashboardSchema.safeParse(board({ |
| 106 | + columns: 99, |
| 107 | + dateRange: {}, |
| 108 | + globalFilters: [{ name: 'dateRange', field: 'region', type: 'text' }], |
| 109 | + })); |
| 110 | + expect(result.success).toBe(false); |
| 111 | + const paths = result.error!.issues.map((i) => i.path.join('.')); |
| 112 | + expect(paths).toContain('columns'); |
| 113 | + expect(paths).toContain('globalFilters.0.name'); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe('#29213 reservedDateRangeGlobalFilters reads a stored body', () => { |
| 118 | + it('answers nothing without a declared dateRange object', () => { |
| 119 | + const filters = [{ name: 'dateRange', field: 'region' }]; |
| 120 | + expect(reservedDateRangeGlobalFilters(undefined, filters)).toEqual([]); |
| 121 | + expect(reservedDateRangeGlobalFilters(null, filters)).toEqual([]); |
| 122 | + expect(reservedDateRangeGlobalFilters('this_month', filters)).toEqual([]); |
| 123 | + }); |
| 124 | + |
| 125 | + it('a non-array, a non-object entry and an entry with neither key are left to the schema', () => { |
| 126 | + expect(reservedDateRangeGlobalFilters({}, undefined)).toEqual([]); |
| 127 | + expect(reservedDateRangeGlobalFilters({}, { name: 'dateRange' })).toEqual([]); |
| 128 | + expect(reservedDateRangeGlobalFilters({}, [null, 'dateRange', 7, { label: 'dateRange' }])).toEqual([]); |
| 129 | + }); |
| 130 | + |
| 131 | + it('an empty name falls back to the field, as the duplicate-key predicate does', () => { |
| 132 | + expect(reservedDateRangeGlobalFilters({}, [{ name: '', field: 'dateRange' }]).map((e) => [e.index, e.key])) |
| 133 | + .toEqual([[0, 'field']]); |
| 134 | + }); |
| 135 | +}); |
0 commit comments