|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #29023 — a dashboard declares each global filter key (its `name`, else its |
| 5 | + * `field`) once. |
| 6 | + * |
| 7 | + * Measured on `main` @ `12aad143` before the rule: `DashboardSchema` accepted |
| 8 | + * `globalFilters: [{ name: 'r', field: 'region' }, { name: 'r', field: 'status' }]`, |
| 9 | + * the save door answered 200, and the board drew ONE control whose value |
| 10 | + * filtered `status`, the second definition's field. The refusal is located at |
| 11 | + * the second declaration's key and names every index. |
| 12 | + */ |
| 13 | +import { describe, expect, it } from 'vitest'; |
| 14 | +import { DashboardSchema } from './dashboard.zod'; |
| 15 | +import { duplicateGlobalFilterNames } from './duplicate-global-filter-names'; |
| 16 | +import { globalFilterKey } from '../system/i18n-resolver'; |
| 17 | + |
| 18 | +const board = (globalFilters: unknown[]) => ({ |
| 19 | + name: 'qa_probe_dfn', |
| 20 | + label: 'QA probe', |
| 21 | + widgets: [], |
| 22 | + globalFilters, |
| 23 | +}); |
| 24 | + |
| 25 | +const customIssues = (input: unknown) => { |
| 26 | + const result = DashboardSchema.safeParse(input); |
| 27 | + if (result.success) return []; |
| 28 | + return result.error.issues.filter((i) => i.code === 'custom'); |
| 29 | +}; |
| 30 | + |
| 31 | +describe('#29023 DashboardSchema refuses two globalFilters under one name', () => { |
| 32 | + it('CONTROL: two filters with distinct names parse', () => { |
| 33 | + const result = DashboardSchema.safeParse(board([ |
| 34 | + { name: 'r', field: 'region', type: 'text' }, |
| 35 | + { name: 's', field: 'status', type: 'text' }, |
| 36 | + ])); |
| 37 | + expect(result.success).toBe(true); |
| 38 | + }); |
| 39 | + |
| 40 | + it('CONTROL: two distinct names over the SAME field parse (the key is the name, not the field)', () => { |
| 41 | + const result = DashboardSchema.safeParse(board([ |
| 42 | + { name: 'region_a', field: 'region', type: 'text' }, |
| 43 | + { name: 'region_b', field: 'region', type: 'select', options: [{ value: 'north', label: 'North' }] }, |
| 44 | + ])); |
| 45 | + expect(result.success).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it('the card reproduction is refused at globalFilters[1].name, naming both indices, the name and the fix', () => { |
| 49 | + const result = DashboardSchema.safeParse(board([ |
| 50 | + { name: 'r', field: 'region', type: 'text' }, |
| 51 | + { name: 'r', field: 'status', type: 'text' }, |
| 52 | + ])); |
| 53 | + expect(result.success).toBe(false); |
| 54 | + const issues = result.error!.issues; |
| 55 | + expect(issues).toHaveLength(1); |
| 56 | + expect(issues[0]).toMatchObject({ code: 'custom', path: ['globalFilters', 1, 'name'] }); |
| 57 | + expect(issues[0]!.message.startsWith( |
| 58 | + "This dashboard declares the global filter 'r' twice in globalFilters: globalFilters[0] and globalFilters[1].", |
| 59 | + )).toBe(true); |
| 60 | + expect(issues[0]!.message).toMatch(/Rename all but one of them .* or delete the copy\.$/); |
| 61 | + // Runtime strings carry no tracker number (check:doc-authoring). |
| 62 | + expect(issues[0]!.message).not.toMatch(/#\d{3,}/); |
| 63 | + }); |
| 64 | + |
| 65 | + it('two UNNAMED filters on one field collide on the field, located at globalFilters[1].field', () => { |
| 66 | + const issues = customIssues(board([ |
| 67 | + { field: 'region', type: 'text' }, |
| 68 | + { field: 'region', type: 'select', options: [{ value: 'north', label: 'North' }] }, |
| 69 | + ])); |
| 70 | + expect(issues).toHaveLength(1); |
| 71 | + expect(issues[0]).toMatchObject({ path: ['globalFilters', 1, 'field'] }); |
| 72 | + expect(issues[0]!.message).toContain("the global filter 'region' twice"); |
| 73 | + expect(issues[0]!.message).toContain('A filter with no `name` is keyed by its `field`.'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('a name colliding with another entry\'s field (no name) is the same key and is refused', () => { |
| 77 | + const issues = customIssues(board([ |
| 78 | + { field: 'region', type: 'text' }, |
| 79 | + { name: 'region', field: 'sales_region', type: 'text' }, |
| 80 | + ])); |
| 81 | + expect(issues).toHaveLength(1); |
| 82 | + expect(issues[0]).toMatchObject({ path: ['globalFilters', 1, 'name'] }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('an empty `name` keys by `field`, as `globalFilterKey` does', () => { |
| 86 | + const issues = customIssues(board([ |
| 87 | + { name: '', field: 'region', type: 'text' }, |
| 88 | + { field: 'region', type: 'text' }, |
| 89 | + ])); |
| 90 | + expect(issues).toHaveLength(1); |
| 91 | + expect(issues[0]).toMatchObject({ path: ['globalFilters', 1, 'field'] }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('three declarations of one name answer ONE issue at the second, listing every index', () => { |
| 95 | + const issues = customIssues(board([ |
| 96 | + { name: 'r', field: 'region' }, |
| 97 | + { name: 'other', field: 'owner' }, |
| 98 | + { name: 'r', field: 'status' }, |
| 99 | + { name: 'r', field: 'stage' }, |
| 100 | + ])); |
| 101 | + expect(issues).toHaveLength(1); |
| 102 | + expect(issues[0]).toMatchObject({ path: ['globalFilters', 2, 'name'] }); |
| 103 | + expect(issues[0]!.message.startsWith( |
| 104 | + "This dashboard declares the global filter 'r' 3 times in globalFilters: " |
| 105 | + + 'globalFilters[0], globalFilters[2] and globalFilters[3].', |
| 106 | + )).toBe(true); |
| 107 | + }); |
| 108 | + |
| 109 | + it('two repeated names answer one issue each', () => { |
| 110 | + const issues = customIssues(board([ |
| 111 | + { name: 'a', field: 'region' }, |
| 112 | + { name: 'b', field: 'owner' }, |
| 113 | + { name: 'a', field: 'status' }, |
| 114 | + { name: 'b', field: 'stage' }, |
| 115 | + ])); |
| 116 | + expect(issues.map((i) => i.path)).toEqual([['globalFilters', 2, 'name'], ['globalFilters', 3, 'name']]); |
| 117 | + }); |
| 118 | + |
| 119 | + it('the rule travels with `DashboardSchema.shape.globalFilters` (a mirror spreading the shape keeps it)', () => { |
| 120 | + const slot = (DashboardSchema as unknown as { shape: Record<string, { safeParse(v: unknown): { success: boolean } }> }) |
| 121 | + .shape.globalFilters!; |
| 122 | + expect(slot.safeParse([{ name: 'r', field: 'region' }, { name: 'r', field: 'status' }]).success).toBe(false); |
| 123 | + expect(slot.safeParse([{ name: 'r', field: 'region' }, { name: 's', field: 'status' }]).success).toBe(true); |
| 124 | + expect(slot.safeParse(undefined).success).toBe(true); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +describe('#29023 duplicateGlobalFilterNames keys a filter the way globalFilterKey does', () => { |
| 129 | + const filters = [ |
| 130 | + { name: 'dept', field: 'department' }, |
| 131 | + { field: 'created_at' }, |
| 132 | + { name: '', field: 'owner_id' }, |
| 133 | + { name: 'stage' , field: 'stage' }, |
| 134 | + { label: 'Unkeyed' }, |
| 135 | + ]; |
| 136 | + |
| 137 | + it('every keyed filter resolves to globalFilterKey(filter), and an unkeyed one is passed over', () => { |
| 138 | + // Doubling each filter makes every keyed one a duplicate, so the list reads back the predicate's keys. |
| 139 | + const keys = duplicateGlobalFilterNames([...filters, ...filters]).map((d) => d.name); |
| 140 | + expect(keys).toEqual(filters.map((f) => globalFilterKey(f)).filter((k): k is string => k !== undefined)); |
| 141 | + }); |
| 142 | + |
| 143 | + it('a non-array, a non-object entry and an entry with neither key are left to the schema', () => { |
| 144 | + expect(duplicateGlobalFilterNames(undefined)).toEqual([]); |
| 145 | + expect(duplicateGlobalFilterNames({ name: 'r' })).toEqual([]); |
| 146 | + expect(duplicateGlobalFilterNames([null, 'r', 7, { label: 'x' }, { label: 'y' }])).toEqual([]); |
| 147 | + }); |
| 148 | +}); |
0 commit comments