|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { DashboardSchema } from '@objectstack/spec/ui'; |
| 5 | +import { |
| 6 | + checkDashboardWidgetOptions, |
| 7 | + CONSUMED_WIDGET_OPTION_KEYS, |
| 8 | + UNCONSUMED_WIDGET_OPTION, |
| 9 | +} from '@objectstack/sdui-parser'; |
| 10 | +import { validateDashboardWidgetOptions } from './validate-dashboard-widget-options.js'; |
| 11 | +import { AUTHORING_COMMANDS, authoringRulesFor, runAuthoringRules } from './authoring-rules.js'; |
| 12 | + |
| 13 | +type AnyRec = Record<string, unknown>; |
| 14 | + |
| 15 | +/** A dataset-bound widget: the only spec-legal shape, and the one the check censuses. */ |
| 16 | +function widget(id: string, options?: AnyRec, extra: AnyRec = {}): AnyRec { |
| 17 | + return { |
| 18 | + id, |
| 19 | + type: 'metric', |
| 20 | + title: id, |
| 21 | + dataset: 'opportunity_metrics', |
| 22 | + values: ['total_amount'], |
| 23 | + layout: { x: 0, y: 0, w: 4, h: 2 }, |
| 24 | + ...(options === undefined ? {} : { options }), |
| 25 | + ...extra, |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +/** The dashboard as the parsed tier hands it over: through the real schema. */ |
| 30 | +function parsedDashboard(widgets: AnyRec[]): AnyRec { |
| 31 | + return DashboardSchema.parse({ name: 'pipeline', label: 'Pipeline', widgets }) as AnyRec; |
| 32 | +} |
| 33 | + |
| 34 | +describe('validateDashboardWidgetOptions — the SDUI widget-option check at the metadata door', () => { |
| 35 | + it('flags an unread options key by name, at the check\'s own code and level', () => { |
| 36 | + const stack = { dashboards: [parsedDashboard([widget('won_revenue', { format: 'currency' })])] }; |
| 37 | + const findings = validateDashboardWidgetOptions(stack); |
| 38 | + |
| 39 | + expect(findings).toHaveLength(1); |
| 40 | + expect(findings[0]).toMatchObject({ |
| 41 | + rule: UNCONSUMED_WIDGET_OPTION, |
| 42 | + severity: 'warning', |
| 43 | + where: 'dashboard "pipeline" › widget "won_revenue"', |
| 44 | + path: 'dashboards[0].widgets[0].options', |
| 45 | + }); |
| 46 | + // The named subject: the finding says WHICH key. |
| 47 | + expect(findings[0].message).toContain('options.format'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('passes every consumed key (the control) on the same widget shape', () => { |
| 51 | + // Every key the read set names, with a value the schema accepts, survives |
| 52 | + // the parse and is not flagged. `stageOrder` is the funnel's own key: the |
| 53 | + // schema refuses it on any other type, so the control widget is a funnel. |
| 54 | + const values: AnyRec = { |
| 55 | + dateGranularity: 'month', |
| 56 | + description: 'sub-caption', |
| 57 | + limit: 10, |
| 58 | + sortBy: 'total_amount', |
| 59 | + sortOrder: 'desc', |
| 60 | + stageOrder: ['prospecting', 'closed_won'], |
| 61 | + }; |
| 62 | + expect(Object.keys(values).sort()).toEqual([...CONSUMED_WIDGET_OPTION_KEYS].sort()); |
| 63 | + const control = widget('stages', values, { type: 'funnel', dimensions: ['stage'] }); |
| 64 | + const stack = { dashboards: [parsedDashboard([control])] }; |
| 65 | + |
| 66 | + expect(validateDashboardWidgetOptions(stack)).toEqual([]); |
| 67 | + }); |
| 68 | + |
| 69 | + it('flags exactly what the check flags — one finding per diagnostic, message unchanged', () => { |
| 70 | + // The adapter decides nothing: over the same widgets, the lint findings' |
| 71 | + // codes, levels and messages ARE the check's diagnostics on the SDUI node. |
| 72 | + const widgets = [ |
| 73 | + widget('tile_a', { color: 'green', sortBy: 'total_amount' }), |
| 74 | + widget('tile_b'), |
| 75 | + widget('tile_c', { showLegend: true, horizontal: true, limit: 5 }), |
| 76 | + ]; |
| 77 | + const dash = parsedDashboard(widgets); |
| 78 | + const findings = validateDashboardWidgetOptions({ dashboards: [dash] }); |
| 79 | + const diagnostics = checkDashboardWidgetOptions({ type: 'dashboard', widgets: dash.widgets }); |
| 80 | + |
| 81 | + expect(diagnostics).toHaveLength(3); |
| 82 | + expect(findings.map((f) => [f.rule, f.severity, f.message])).toEqual( |
| 83 | + diagnostics.map((d) => [d.code, d.severity, d.message]), |
| 84 | + ); |
| 85 | + expect(findings.map((f) => f.path)).toEqual([ |
| 86 | + 'dashboards[0].widgets[0].options', |
| 87 | + 'dashboards[0].widgets[2].options', |
| 88 | + 'dashboards[0].widgets[2].options', |
| 89 | + ]); |
| 90 | + }); |
| 91 | + |
| 92 | + it('honours the check\'s own exemptions rather than restating them', () => { |
| 93 | + const suppressed = widget('quiet', { format: 'currency' }, { |
| 94 | + suppressWarnings: [UNCONSUMED_WIDGET_OPTION], |
| 95 | + }); |
| 96 | + const stack = { dashboards: [parsedDashboard([suppressed])] }; |
| 97 | + expect(validateDashboardWidgetOptions(stack)).toEqual([]); |
| 98 | + |
| 99 | + // A widget with no `dataset` is outside the check's census (unparsed input: |
| 100 | + // the schema requires `dataset`, which is the point of the exemption). |
| 101 | + const inline = { dashboards: [{ name: 'legacy', widgets: [{ id: 'x', type: 'metric', options: { icon: 'x' } }] }] }; |
| 102 | + expect(validateDashboardWidgetOptions(inline)).toEqual([]); |
| 103 | + }); |
| 104 | + |
| 105 | + it('keeps each widget at its own index, so an id-less widget is still named correctly', () => { |
| 106 | + // `os lint` never parses, so a widget can arrive with no `id`. |
| 107 | + const stack = { |
| 108 | + dashboards: { |
| 109 | + ops: { widgets: [widget('first'), { type: 'metric', dataset: 'd', options: { suffix: '%' } }] }, |
| 110 | + }, |
| 111 | + }; |
| 112 | + const findings = validateDashboardWidgetOptions(stack); |
| 113 | + |
| 114 | + expect(findings).toHaveLength(1); |
| 115 | + expect(findings[0]).toMatchObject({ |
| 116 | + rule: UNCONSUMED_WIDGET_OPTION, |
| 117 | + severity: 'warning', |
| 118 | + where: 'dashboard "ops" › widget "(widget 1)"', |
| 119 | + path: 'dashboards[0].widgets[1].options', |
| 120 | + }); |
| 121 | + expect(findings[0].message).toContain('"#1"'); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +describe('the registry runs it on all three authoring commands', () => { |
| 126 | + const stack = { dashboards: [parsedDashboard([widget('won_revenue', { currency: 'USD' })])] }; |
| 127 | + |
| 128 | + it.each(AUTHORING_COMMANDS)('os %s reports the unread key as an advisory', (command) => { |
| 129 | + expect(authoringRulesFor(command).map((r) => r.name)).toContain('validateDashboardWidgetOptions'); |
| 130 | + const hits = runAuthoringRules(command, { normalized: stack, parsed: stack }).filter( |
| 131 | + (f) => f.rule === UNCONSUMED_WIDGET_OPTION, |
| 132 | + ); |
| 133 | + expect(hits).toHaveLength(1); |
| 134 | + expect(hits[0]).toMatchObject({ severity: 'warning', path: 'dashboards[0].widgets[0].options' }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments