|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { validateWidgetBindings, TABLE_COUNT_ONLY } from './validate-widget-bindings.js'; |
| 3 | + |
| 4 | +/** The downstream repro from issue #1719 — dataset with a count AND a sum |
| 5 | + * measure plus a dimension; the widget selects only the count, no dims. */ |
| 6 | +function reproStack(widgetOverrides: Record<string, unknown> = {}) { |
| 7 | + return { |
| 8 | + datasets: [{ |
| 9 | + name: 'expense_report_metrics', |
| 10 | + label: 'Expense report metrics', |
| 11 | + object: 'expense_report', |
| 12 | + measures: [ |
| 13 | + { name: 'report_count', label: 'report_count', aggregate: 'count' }, |
| 14 | + { name: 'total_amount', label: 'total_amount', aggregate: 'sum', field: 'total_amount' }, |
| 15 | + ], |
| 16 | + dimensions: [{ name: 'cost_center', field: 'cost_center' }], |
| 17 | + }], |
| 18 | + dashboards: [{ |
| 19 | + name: 'expenses_overview_dashboard', |
| 20 | + label: 'Expenses Overview', |
| 21 | + widgets: [{ |
| 22 | + id: 'pending_reports_table', |
| 23 | + type: 'table', |
| 24 | + dataset: 'expense_report_metrics', |
| 25 | + values: ['report_count'], |
| 26 | + filter: { status: 'submitted' }, |
| 27 | + layout: { x: 0, y: 0, w: 6, h: 4 }, |
| 28 | + ...widgetOverrides, |
| 29 | + }], |
| 30 | + }], |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +describe('validateWidgetBindings (table-count-only, issue #1719)', () => { |
| 35 | + it('warns on the issue repro: count-only table widget without dimensions', () => { |
| 36 | + const warnings = validateWidgetBindings(reproStack()); |
| 37 | + expect(warnings).toHaveLength(1); |
| 38 | + expect(warnings[0].rule).toBe(TABLE_COUNT_ONLY); |
| 39 | + expect(warnings[0].where).toContain('expenses_overview_dashboard'); |
| 40 | + expect(warnings[0].where).toContain('pending_reports_table'); |
| 41 | + expect(warnings[0].path).toBe('dashboards[0].widgets[0]'); |
| 42 | + expect(warnings[0].message).toContain('report_count'); |
| 43 | + expect(warnings[0].message).toContain('single summary row'); |
| 44 | + expect(warnings[0].hint).toContain('ListView (ADR-0017)'); |
| 45 | + expect(warnings[0].hint).toContain(`suppressWarnings: ['${TABLE_COUNT_ONLY}']`); |
| 46 | + }); |
| 47 | + |
| 48 | + it('warns for pivot widgets too', () => { |
| 49 | + const warnings = validateWidgetBindings(reproStack({ type: 'pivot' })); |
| 50 | + expect(warnings).toHaveLength(1); |
| 51 | + expect(warnings[0].message).toContain("'pivot' widget"); |
| 52 | + }); |
| 53 | + |
| 54 | + it('is keyed on the WIDGET binding — selecting the sum measure is clean', () => { |
| 55 | + expect(validateWidgetBindings(reproStack({ values: ['total_amount'] }))).toHaveLength(0); |
| 56 | + }); |
| 57 | + |
| 58 | + it('mixed count + non-count selection is clean', () => { |
| 59 | + expect(validateWidgetBindings(reproStack({ values: ['report_count', 'total_amount'] }))).toHaveLength(0); |
| 60 | + }); |
| 61 | + |
| 62 | + it('declaring a dimension on the widget is clean', () => { |
| 63 | + expect(validateWidgetBindings(reproStack({ dimensions: ['cost_center'] }))).toHaveLength(0); |
| 64 | + }); |
| 65 | + |
| 66 | + it('metric widgets are exactly what a count-only binding wants — clean', () => { |
| 67 | + expect(validateWidgetBindings(reproStack({ type: 'metric' }))).toHaveLength(0); |
| 68 | + }); |
| 69 | + |
| 70 | + it('suppressWarnings opts a deliberate single-row table out', () => { |
| 71 | + expect(validateWidgetBindings(reproStack({ suppressWarnings: [TABLE_COUNT_ONLY] }))).toHaveLength(0); |
| 72 | + }); |
| 73 | + |
| 74 | + it('unrelated suppressWarnings entries do not suppress', () => { |
| 75 | + expect(validateWidgetBindings(reproStack({ suppressWarnings: ['some-other-rule'] }))).toHaveLength(1); |
| 76 | + }); |
| 77 | + |
| 78 | + it('skips dangling dataset references (cross-reference finding, not this rule)', () => { |
| 79 | + expect(validateWidgetBindings(reproStack({ dataset: 'no_such_dataset' }))).toHaveLength(0); |
| 80 | + }); |
| 81 | + |
| 82 | + it('skips unresolvable measure names (a different diagnostic)', () => { |
| 83 | + expect(validateWidgetBindings(reproStack({ values: ['no_such_measure'] }))).toHaveLength(0); |
| 84 | + }); |
| 85 | + |
| 86 | + it('treats derived measures as non-count even when aggregate says count', () => { |
| 87 | + const stack = reproStack({ values: ['count_ratio'] }); |
| 88 | + (stack.datasets[0].measures as Record<string, unknown>[]).push({ |
| 89 | + name: 'count_ratio', |
| 90 | + aggregate: 'count', |
| 91 | + derived: { op: 'ratio', of: ['report_count', 'report_count'] }, |
| 92 | + }); |
| 93 | + expect(validateWidgetBindings(stack)).toHaveLength(0); |
| 94 | + }); |
| 95 | + |
| 96 | + it('count_distinct is a deliberate analytic — clean', () => { |
| 97 | + const stack = reproStack({ values: ['unique_requesters'] }); |
| 98 | + (stack.datasets[0].measures as Record<string, unknown>[]).push({ |
| 99 | + name: 'unique_requesters', |
| 100 | + aggregate: 'count_distinct', |
| 101 | + field: 'requester', |
| 102 | + }); |
| 103 | + expect(validateWidgetBindings(stack)).toHaveLength(0); |
| 104 | + }); |
| 105 | + |
| 106 | + it('handles map-keyed datasets/dashboards collections', () => { |
| 107 | + const arrayForm = reproStack(); |
| 108 | + const { name: _dsName, ...dsRest } = arrayForm.datasets[0]; |
| 109 | + const { name: _dashName, ...dashRest } = arrayForm.dashboards[0]; |
| 110 | + const stack = { |
| 111 | + datasets: { expense_report_metrics: dsRest }, |
| 112 | + dashboards: { expenses_overview_dashboard: dashRest }, |
| 113 | + }; |
| 114 | + const warnings = validateWidgetBindings(stack); |
| 115 | + expect(warnings).toHaveLength(1); |
| 116 | + expect(warnings[0].where).toContain('expenses_overview_dashboard'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('is silent on stacks without dashboards or datasets', () => { |
| 120 | + expect(validateWidgetBindings({})).toHaveLength(0); |
| 121 | + expect(validateWidgetBindings({ dashboards: [], datasets: [] })).toHaveLength(0); |
| 122 | + }); |
| 123 | +}); |
0 commit comments